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 if (!aBitmap.ImplMakeGreyscales())
28 return BitmapEx();
29
30 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
31 if (!pReadAcc)
32 return BitmapEx();
33
34 Bitmap aNewBmp(aBitmap.GetSizePixel(), vcl::PixelFormat::N8_BPP, &pReadAcc->GetPalette());
35 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
36 if (!pWriteAcc)
37 return BitmapEx();
38
39 BitmapColor aGrey(sal_uInt8(0));
40 const sal_Int32 nWidth = pWriteAcc->Width();
41 const sal_Int32 nHeight = pWriteAcc->Height();
42 sal_Int32 nGrey11, nGrey12, nGrey13;
43 sal_Int32 nGrey21, nGrey22, nGrey23;
44 sal_Int32 nGrey31, nGrey32, nGrey33;
45 double fAzim = basegfx::deg2rad<100>(mnAzimuthAngle100);
46 double fElev = basegfx::deg2rad<100>(mnElevationAngle100);
47 std::unique_ptr<sal_Int32[]> pHMap(new sal_Int32[nWidth + 2]);
48 std::unique_ptr<sal_Int32[]> pVMap(new sal_Int32[nHeight + 2]);
49 sal_Int32 nX, nY, nNx, nNy, nDotL;
50 const sal_Int32 nLx = FRound(cos(fAzim) * cos(fElev) * 255.0);
51 const sal_Int32 nLy = FRound(sin(fAzim) * cos(fElev) * 255.0);
52 const sal_Int32 nLz = FRound(sin(fElev) * 255.0);
53 const auto nZ2 = ((6 * 255) / 4) * ((6 * 255) / 4);
54 const sal_Int32 nNzLz = ((6 * 255) / 4) * nLz;
55 const sal_uInt8 cLz = static_cast<sal_uInt8>(std::clamp(nLz, sal_Int32(0), sal_Int32(255)));
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 nNx = nGrey11 + nGrey21 + nGrey31 - nGrey13 - nGrey23 - nGrey33;
92 nNy = nGrey31 + nGrey32 + nGrey33 - nGrey11 - nGrey12 - nGrey13;
93
94 if (!nNx && !nNy)
95 {
96 aGrey.SetIndex(cLz);
97 }
98 else if ((nDotL = nNx * nLx + nNy * nLy + nNzLz) < 0)
99 {
100 aGrey.SetIndex(0);
101 }
102 else
103 {
104 const double fGrey = nDotL / sqrt(static_cast<double>(nNx * nNx + nNy * nNy + nZ2));
105 aGrey.SetIndex(static_cast<sal_uInt8>(std::clamp(fGrey, 0.0, 255.0)));
106 }
107
108 pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
109
110 if (nX < (nWidth - 1))
111 {
112 const sal_Int32 nNextX = pHMap[nX + 3];
113
114 nGrey11 = nGrey12;
115 nGrey12 = nGrey13;
116 nGrey13 = pReadAcc->GetPixel(pVMap[nY], nNextX).GetIndex();
117 nGrey21 = nGrey22;
118 nGrey22 = nGrey23;
119 nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], nNextX).GetIndex();
120 nGrey31 = nGrey32;
121 nGrey32 = nGrey33;
122 nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], nNextX).GetIndex();
123 }
124 }
125 }
126
127 pHMap.reset();
128 pVMap.reset();
129 pWriteAcc.reset();
130 pReadAcc.reset();
131
132 const MapMode aMap(aBitmap.GetPrefMapMode());
133 const Size aPrefSize(aBitmap.GetPrefSize());
134
135 aBitmap = aNewBmp;
136
137 aBitmap.SetPrefMapMode(aMap);
138 aBitmap.SetPrefSize(aPrefSize);
139
140 return BitmapEx(aBitmap);
141}
142
143/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:70
void SetIndex(sal_uInt8 cIndex)
Definition: BitmapColor.hxx:75
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
const BitmapPalette & GetPalette() const
BitmapColor GetPixel(tools::Long nY, tools::Long nX) const
void SetPrefMapMode(const MapMode &rMapMode)
const MapMode & GetPrefMapMode() const
Size GetSizePixel() const
void SetPrefSize(const Size &rSize)
SAL_DLLPRIVATE bool ImplMakeGreyscales()
const Size & GetPrefSize() const
tools::Long FRound(double fVal)
HashMap_OWString_Interface aMap
unsigned char sal_uInt8