LibreOffice Module vcl (master)  1
BitmapSobelGreyFilter.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  */
10 
11 #include <sal/config.h>
12 
13 #include <algorithm>
14 
15 #include <vcl/bitmap.hxx>
16 #include <vcl/bitmapex.hxx>
17 #include <vcl/bitmapaccess.hxx>
19 
20 #include <bitmapwriteaccess.hxx>
21 
23 {
24  Bitmap aBitmap(rBitmapEx.GetBitmap());
25 
26  bool bRet = aBitmap.ImplMakeGreyscales(256);
27 
28  if (bRet)
29  {
30  bRet = false;
31 
32  Bitmap::ScopedReadAccess pReadAcc(aBitmap);
33 
34  if (pReadAcc)
35  {
36  Bitmap aNewBmp(aBitmap.GetSizePixel(), 8, &pReadAcc->GetPalette());
37  BitmapScopedWriteAccess pWriteAcc(aNewBmp);
38 
39  if (pWriteAcc)
40  {
41  BitmapColor aGrey(sal_uInt8(0));
42  const long nWidth = pWriteAcc->Width();
43  const long nHeight = pWriteAcc->Height();
44  const long nMask111 = -1, nMask121 = 0, nMask131 = 1;
45  const long nMask211 = -2, nMask221 = 0, nMask231 = 2;
46  const long nMask311 = -1, nMask321 = 0, nMask331 = 1;
47  const long nMask112 = 1, nMask122 = 2, nMask132 = 1;
48  const long nMask212 = 0, nMask222 = 0, nMask232 = 0;
49  const long nMask312 = -1, nMask322 = -2, nMask332 = -1;
50  long nGrey11, nGrey12, nGrey13;
51  long nGrey21, nGrey22, nGrey23;
52  long nGrey31, nGrey32, nGrey33;
53  std::unique_ptr<long[]> pHMap(new long[nWidth + 2]);
54  std::unique_ptr<long[]> pVMap(new long[nHeight + 2]);
55  long nX, nY, nSum1, nSum2;
56 
57  // fill mapping tables
58  pHMap[0] = 0;
59 
60  for (nX = 1; nX <= nWidth; nX++)
61  {
62  pHMap[nX] = nX - 1;
63  }
64 
65  pHMap[nWidth + 1] = nWidth - 1;
66 
67  pVMap[0] = 0;
68 
69  for (nY = 1; nY <= nHeight; nY++)
70  {
71  pVMap[nY] = nY - 1;
72  }
73 
74  pVMap[nHeight + 1] = nHeight - 1;
75 
76  for (nY = 0; nY < nHeight; nY++)
77  {
78  nGrey11 = pReadAcc->GetPixel(pVMap[nY], pHMap[0]).GetIndex();
79  nGrey12 = pReadAcc->GetPixel(pVMap[nY], pHMap[1]).GetIndex();
80  nGrey13 = pReadAcc->GetPixel(pVMap[nY], pHMap[2]).GetIndex();
81  nGrey21 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[0]).GetIndex();
82  nGrey22 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[1]).GetIndex();
83  nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[2]).GetIndex();
84  nGrey31 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[0]).GetIndex();
85  nGrey32 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[1]).GetIndex();
86  nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[2]).GetIndex();
87 
88  Scanline pScanline = pWriteAcc->GetScanline(nY);
89  for (nX = 0; nX < nWidth; nX++)
90  {
91  nSum1 = nSum2 = 0;
92 
93  nSum1 += nMask111 * nGrey11;
94  nSum2 += nMask112 * nGrey11;
95 
96  nSum1 += nMask121 * nGrey12;
97  nSum2 += nMask122 * nGrey12;
98 
99  nSum1 += nMask131 * nGrey13;
100  nSum2 += nMask132 * nGrey13;
101 
102  nSum1 += nMask211 * nGrey21;
103  nSum2 += nMask212 * nGrey21;
104 
105  nSum1 += nMask221 * nGrey22;
106  nSum2 += nMask222 * nGrey22;
107 
108  nSum1 += nMask231 * nGrey23;
109  nSum2 += nMask232 * nGrey23;
110 
111  nSum1 += nMask311 * nGrey31;
112  nSum2 += nMask312 * nGrey31;
113 
114  nSum1 += nMask321 * nGrey32;
115  nSum2 += nMask322 * nGrey32;
116 
117  nSum1 += nMask331 * nGrey33;
118  nSum2 += nMask332 * nGrey33;
119 
120  nSum1 = static_cast<long>(
121  sqrt(static_cast<double>(nSum1 * nSum1 + nSum2 * nSum2)));
122 
123  aGrey.SetIndex(~static_cast<sal_uInt8>(std::clamp(nSum1, 0L, 255L)));
124  pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
125 
126  if (nX < (nWidth - 1))
127  {
128  const long nNextX = pHMap[nX + 3];
129 
130  nGrey11 = nGrey12;
131  nGrey12 = nGrey13;
132  nGrey13 = pReadAcc->GetPixel(pVMap[nY], nNextX).GetIndex();
133  nGrey21 = nGrey22;
134  nGrey22 = nGrey23;
135  nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], nNextX).GetIndex();
136  nGrey31 = nGrey32;
137  nGrey32 = nGrey33;
138  nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], nNextX).GetIndex();
139  }
140  }
141  }
142 
143  pHMap.reset();
144  pVMap.reset();
145  pWriteAcc.reset();
146  bRet = true;
147  }
148 
149  pReadAcc.reset();
150 
151  if (bRet)
152  {
153  const MapMode aMap(aBitmap.GetPrefMapMode());
154  const Size aPrefSize(aBitmap.GetPrefSize());
155 
156  aBitmap = aNewBmp;
157 
158  aBitmap.SetPrefMapMode(aMap);
159  aBitmap.SetPrefSize(aPrefSize);
160  }
161  }
162  }
163 
164  if (bRet)
165  return BitmapEx(aBitmap);
166 
167  return BitmapEx();
168 }
169 
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:61
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
BitmapColor GetPixel(long nY, long nX) const
void SetIndex(sal_uInt8 cIndex)
Definition: BitmapColor.hxx:66
SAL_DLLPRIVATE bool ImplMakeGreyscales(sal_uInt16 nGreyscales)
Definition: bitmap3.cxx:320
unsigned char sal_uInt8
const BitmapPalette & GetPalette() const