LibreOffice Module vcl (master)  1
BitmapEmbossGreyFilter.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 <tools/helpers.hxx>
14 
15 #include <vcl/bitmap.hxx>
16 #include <vcl/bitmapex.hxx>
18 
20 
21 #include <algorithm>
22 
24 {
25  Bitmap aBitmap(rBitmapEx.GetBitmap());
26 
27  bool bRet = aBitmap.ImplMakeGreyscales(256);
28 
29  if (bRet)
30  {
31  bRet = false;
32 
33  Bitmap::ScopedReadAccess pReadAcc(aBitmap);
34 
35  if (pReadAcc)
36  {
37  Bitmap aNewBmp(aBitmap.GetSizePixel(), 8, &pReadAcc->GetPalette());
38  BitmapScopedWriteAccess pWriteAcc(aNewBmp);
39 
40  if (pWriteAcc)
41  {
42  BitmapColor aGrey(sal_uInt8(0));
43  const tools::Long nWidth = pWriteAcc->Width();
44  const tools::Long nHeight = pWriteAcc->Height();
45  tools::Long nGrey11, nGrey12, nGrey13;
46  tools::Long nGrey21, nGrey22, nGrey23;
47  tools::Long nGrey31, nGrey32, nGrey33;
48  double fAzim = basegfx::deg2rad(mnAzimuthAngle100 * 0.01);
49  double fElev = basegfx::deg2rad(mnElevationAngle100 * 0.01);
50  std::unique_ptr<tools::Long[]> pHMap(new tools::Long[nWidth + 2]);
51  std::unique_ptr<tools::Long[]> pVMap(new tools::Long[nHeight + 2]);
52  tools::Long nX, nY, nNx, nNy, nDotL;
53  const tools::Long nLx = FRound(cos(fAzim) * cos(fElev) * 255.0);
54  const tools::Long nLy = FRound(sin(fAzim) * cos(fElev) * 255.0);
55  const tools::Long nLz = FRound(sin(fElev) * 255.0);
56  const auto nZ2 = ((6 * 255) / 4) * ((6 * 255) / 4);
57  const tools::Long nNzLz = ((6 * 255) / 4) * nLz;
58  const sal_uInt8 cLz
59  = static_cast<sal_uInt8>(std::clamp(nLz, tools::Long(0), tools::Long(255)));
60 
61  // fill mapping tables
62  pHMap[0] = 0;
63 
64  for (nX = 1; nX <= nWidth; nX++)
65  {
66  pHMap[nX] = nX - 1;
67  }
68 
69  pHMap[nWidth + 1] = nWidth - 1;
70 
71  pVMap[0] = 0;
72 
73  for (nY = 1; nY <= nHeight; nY++)
74  {
75  pVMap[nY] = nY - 1;
76  }
77 
78  pVMap[nHeight + 1] = nHeight - 1;
79 
80  for (nY = 0; nY < nHeight; nY++)
81  {
82  nGrey11 = pReadAcc->GetPixel(pVMap[nY], pHMap[0]).GetIndex();
83  nGrey12 = pReadAcc->GetPixel(pVMap[nY], pHMap[1]).GetIndex();
84  nGrey13 = pReadAcc->GetPixel(pVMap[nY], pHMap[2]).GetIndex();
85  nGrey21 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[0]).GetIndex();
86  nGrey22 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[1]).GetIndex();
87  nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[2]).GetIndex();
88  nGrey31 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[0]).GetIndex();
89  nGrey32 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[1]).GetIndex();
90  nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[2]).GetIndex();
91 
92  Scanline pScanline = pWriteAcc->GetScanline(nY);
93  for (nX = 0; nX < nWidth; nX++)
94  {
95  nNx = nGrey11 + nGrey21 + nGrey31 - nGrey13 - nGrey23 - nGrey33;
96  nNy = nGrey31 + nGrey32 + nGrey33 - nGrey11 - nGrey12 - nGrey13;
97 
98  if (!nNx && !nNy)
99  {
100  aGrey.SetIndex(cLz);
101  }
102  else if ((nDotL = nNx * nLx + nNy * nLy + nNzLz) < 0)
103  {
104  aGrey.SetIndex(0);
105  }
106  else
107  {
108  const double fGrey
109  = nDotL / sqrt(static_cast<double>(nNx * nNx + nNy * nNy + nZ2));
110  aGrey.SetIndex(static_cast<sal_uInt8>(std::clamp(fGrey, 0.0, 255.0)));
111  }
112 
113  pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
114 
115  if (nX < (nWidth - 1))
116  {
117  const tools::Long nNextX = pHMap[nX + 3];
118 
119  nGrey11 = nGrey12;
120  nGrey12 = nGrey13;
121  nGrey13 = pReadAcc->GetPixel(pVMap[nY], nNextX).GetIndex();
122  nGrey21 = nGrey22;
123  nGrey22 = nGrey23;
124  nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], nNextX).GetIndex();
125  nGrey31 = nGrey32;
126  nGrey32 = nGrey33;
127  nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], nNextX).GetIndex();
128  }
129  }
130  }
131 
132  pHMap.reset();
133  pVMap.reset();
134  pWriteAcc.reset();
135  bRet = true;
136  }
137 
138  pReadAcc.reset();
139 
140  if (bRet)
141  {
142  const MapMode aMap(aBitmap.GetPrefMapMode());
143  const Size aPrefSize(aBitmap.GetPrefSize());
144 
145  aBitmap = aNewBmp;
146 
147  aBitmap.SetPrefMapMode(aMap);
148  aBitmap.SetPrefSize(aPrefSize);
149  }
150  }
151  }
152 
153  if (bRet)
154  return BitmapEx(aBitmap);
155 
156  return BitmapEx();
157 }
158 
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:58
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
long Long
HashMap_OWString_Interface aMap
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
tools::Long FRound(double fVal)
constexpr double deg2rad(double v)
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