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 <vcl/bitmap.hxx>
14 #include <vcl/bitmapex.hxx>
16 
18 
19 #include <algorithm>
20 
22 {
23  Bitmap aBitmap(rBitmapEx.GetBitmap());
24 
25  bool bRet = aBitmap.ImplMakeGreyscales(256);
26 
27  if (bRet)
28  {
29  bRet = false;
30 
31  Bitmap::ScopedReadAccess pReadAcc(aBitmap);
32 
33  if (pReadAcc)
34  {
35  Bitmap aNewBmp(aBitmap.GetSizePixel(), 8, &pReadAcc->GetPalette());
36  BitmapScopedWriteAccess pWriteAcc(aNewBmp);
37 
38  if (pWriteAcc)
39  {
40  BitmapColor aGrey(sal_uInt8(0));
41  const tools::Long nWidth = pWriteAcc->Width();
42  const tools::Long nHeight = pWriteAcc->Height();
43  const tools::Long nMask111 = -1, nMask121 = 0, nMask131 = 1;
44  const tools::Long nMask211 = -2, nMask221 = 0, nMask231 = 2;
45  const tools::Long nMask311 = -1, nMask321 = 0, nMask331 = 1;
46  const tools::Long nMask112 = 1, nMask122 = 2, nMask132 = 1;
47  const tools::Long nMask212 = 0, nMask222 = 0, nMask232 = 0;
48  const tools::Long nMask312 = -1, nMask322 = -2, nMask332 = -1;
49  tools::Long nGrey11, nGrey12, nGrey13;
50  tools::Long nGrey21, nGrey22, nGrey23;
51  tools::Long nGrey31, nGrey32, nGrey33;
52  std::unique_ptr<long[]> pHMap(new long[nWidth + 2]);
53  std::unique_ptr<long[]> pVMap(new long[nHeight + 2]);
54  tools::Long nX, nY, nSum1, nSum2;
55 
56  // fill mapping tables
57  pHMap[0] = 0;
58 
59  for (nX = 1; nX <= nWidth; nX++)
60  {
61  pHMap[nX] = nX - 1;
62  }
63 
64  pHMap[nWidth + 1] = nWidth - 1;
65 
66  pVMap[0] = 0;
67 
68  for (nY = 1; nY <= nHeight; nY++)
69  {
70  pVMap[nY] = nY - 1;
71  }
72 
73  pVMap[nHeight + 1] = nHeight - 1;
74 
75  for (nY = 0; nY < nHeight; nY++)
76  {
77  nGrey11 = pReadAcc->GetPixel(pVMap[nY], pHMap[0]).GetIndex();
78  nGrey12 = pReadAcc->GetPixel(pVMap[nY], pHMap[1]).GetIndex();
79  nGrey13 = pReadAcc->GetPixel(pVMap[nY], pHMap[2]).GetIndex();
80  nGrey21 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[0]).GetIndex();
81  nGrey22 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[1]).GetIndex();
82  nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[2]).GetIndex();
83  nGrey31 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[0]).GetIndex();
84  nGrey32 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[1]).GetIndex();
85  nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[2]).GetIndex();
86 
87  Scanline pScanline = pWriteAcc->GetScanline(nY);
88  for (nX = 0; nX < nWidth; nX++)
89  {
90  nSum1 = nSum2 = 0;
91 
92  nSum1 += nMask111 * nGrey11;
93  nSum2 += nMask112 * nGrey11;
94 
95  nSum1 += nMask121 * nGrey12;
96  nSum2 += nMask122 * nGrey12;
97 
98  nSum1 += nMask131 * nGrey13;
99  nSum2 += nMask132 * nGrey13;
100 
101  nSum1 += nMask211 * nGrey21;
102  nSum2 += nMask212 * nGrey21;
103 
104  nSum1 += nMask221 * nGrey22;
105  nSum2 += nMask222 * nGrey22;
106 
107  nSum1 += nMask231 * nGrey23;
108  nSum2 += nMask232 * nGrey23;
109 
110  nSum1 += nMask311 * nGrey31;
111  nSum2 += nMask312 * nGrey31;
112 
113  nSum1 += nMask321 * nGrey32;
114  nSum2 += nMask322 * nGrey32;
115 
116  nSum1 += nMask331 * nGrey33;
117  nSum2 += nMask332 * nGrey33;
118 
119  nSum1 = static_cast<tools::Long>(
120  sqrt(static_cast<double>(nSum1 * nSum1 + nSum2 * nSum2)));
121 
122  aGrey.SetIndex(~static_cast<sal_uInt8>(
123  std::clamp(nSum1, tools::Long(0), tools::Long(255))));
124  pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
125 
126  if (nX < (nWidth - 1))
127  {
128  const tools::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:58
long Long
HashMap_OWString_Interface aMap
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
void SetIndex(sal_uInt8 cIndex)
Definition: BitmapColor.hxx:63
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:231
SAL_DLLPRIVATE bool ImplMakeGreyscales(sal_uInt16 nGreyscales)
unsigned char sal_uInt8
const BitmapPalette & GetPalette() const
BitmapColor GetPixel(tools::Long nY, tools::Long nX) const