android: Update app-specific/MIME type icons
[LibreOffice.git] / vcl / source / bitmap / BitmapEmbossGreyFilter.cxx
blobf8b974a4367f089f7ebeaa8bd00fe55d45bd8028
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 */
11 #include <sal/config.h>
13 #include <tools/helpers.hxx>
15 #include <vcl/bitmap.hxx>
16 #include <vcl/bitmapex.hxx>
17 #include <vcl/BitmapEmbossGreyFilter.hxx>
19 #include <bitmap/BitmapWriteAccess.hxx>
21 #include <algorithm>
23 BitmapEx BitmapEmbossGreyFilter::execute(BitmapEx const& rBitmapEx) const
25 Bitmap aBitmap(rBitmapEx.GetBitmap());
27 if (!aBitmap.ImplMakeGreyscales())
28 return BitmapEx();
30 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
31 if (!pReadAcc)
32 return BitmapEx();
34 Bitmap aNewBmp(aBitmap.GetSizePixel(), vcl::PixelFormat::N8_BPP, &pReadAcc->GetPalette());
35 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
36 if (!pWriteAcc)
37 return BitmapEx();
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)));
57 // fill mapping tables
58 pHMap[0] = 0;
60 for (nX = 1; nX <= nWidth; nX++)
62 pHMap[nX] = nX - 1;
65 pHMap[nWidth + 1] = nWidth - 1;
67 pVMap[0] = 0;
69 for (nY = 1; nY <= nHeight; nY++)
71 pVMap[nY] = nY - 1;
74 pVMap[nHeight + 1] = nHeight - 1;
76 for (nY = 0; nY < nHeight; nY++)
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();
88 Scanline pScanline = pWriteAcc->GetScanline(nY);
89 for (nX = 0; nX < nWidth; nX++)
91 nNx = nGrey11 + nGrey21 + nGrey31 - nGrey13 - nGrey23 - nGrey33;
92 nNy = nGrey31 + nGrey32 + nGrey33 - nGrey11 - nGrey12 - nGrey13;
94 if (!nNx && !nNy)
96 aGrey.SetIndex(cLz);
98 else if ((nDotL = nNx * nLx + nNy * nLy + nNzLz) < 0)
100 aGrey.SetIndex(0);
102 else
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)));
108 pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
110 if (nX < (nWidth - 1))
112 const sal_Int32 nNextX = pHMap[nX + 3];
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();
127 pHMap.reset();
128 pVMap.reset();
129 pWriteAcc.reset();
130 pReadAcc.reset();
132 const MapMode aMap(aBitmap.GetPrefMapMode());
133 const Size aPrefSize(aBitmap.GetPrefSize());
135 aBitmap = aNewBmp;
137 aBitmap.SetPrefMapMode(aMap);
138 aBitmap.SetPrefSize(aPrefSize);
140 return BitmapEx(aBitmap);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */