android: Update app-specific/MIME type icons
[LibreOffice.git] / vcl / source / bitmap / BitmapSobelGreyFilter.cxx
blob043f9ad12feeeb9ec39f05140a97b18b11388208
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 <vcl/bitmap.hxx>
14 #include <vcl/bitmapex.hxx>
15 #include <vcl/BitmapSobelGreyFilter.hxx>
17 #include <bitmap/BitmapWriteAccess.hxx>
19 #include <algorithm>
21 BitmapEx BitmapSobelGreyFilter::execute(BitmapEx const& rBitmapEx) const
23 Bitmap aBitmap(rBitmapEx.GetBitmap());
25 if (!aBitmap.ImplMakeGreyscales())
26 return BitmapEx();
28 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
29 if (!pReadAcc)
30 return BitmapEx();
32 Bitmap aNewBmp(aBitmap.GetSizePixel(), vcl::PixelFormat::N8_BPP, &pReadAcc->GetPalette());
33 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
34 if (!pWriteAcc)
35 return BitmapEx();
37 BitmapColor aGrey(sal_uInt8(0));
38 const sal_Int32 nWidth = pWriteAcc->Width();
39 const sal_Int32 nHeight = pWriteAcc->Height();
40 const sal_Int32 nMask111 = -1, nMask121 = 0, nMask131 = 1;
41 const sal_Int32 nMask211 = -2, nMask221 = 0, nMask231 = 2;
42 const sal_Int32 nMask311 = -1, nMask321 = 0, nMask331 = 1;
43 const sal_Int32 nMask112 = 1, nMask122 = 2, nMask132 = 1;
44 const sal_Int32 nMask212 = 0, nMask222 = 0, nMask232 = 0;
45 const sal_Int32 nMask312 = -1, nMask322 = -2, nMask332 = -1;
46 sal_Int32 nGrey11, nGrey12, nGrey13;
47 sal_Int32 nGrey21, nGrey22, nGrey23;
48 sal_Int32 nGrey31, nGrey32, nGrey33;
49 std::unique_ptr<long[]> pHMap(new long[nWidth + 2]);
50 std::unique_ptr<long[]> pVMap(new long[nHeight + 2]);
51 sal_Int32 nX, nY, nSum1, nSum2;
53 // fill mapping tables
54 pHMap[0] = 0;
56 for (nX = 1; nX <= nWidth; nX++)
58 pHMap[nX] = nX - 1;
61 pHMap[nWidth + 1] = nWidth - 1;
63 pVMap[0] = 0;
65 for (nY = 1; nY <= nHeight; nY++)
67 pVMap[nY] = nY - 1;
70 pVMap[nHeight + 1] = nHeight - 1;
72 for (nY = 0; nY < nHeight; nY++)
74 nGrey11 = pReadAcc->GetPixel(pVMap[nY], pHMap[0]).GetIndex();
75 nGrey12 = pReadAcc->GetPixel(pVMap[nY], pHMap[1]).GetIndex();
76 nGrey13 = pReadAcc->GetPixel(pVMap[nY], pHMap[2]).GetIndex();
77 nGrey21 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[0]).GetIndex();
78 nGrey22 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[1]).GetIndex();
79 nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], pHMap[2]).GetIndex();
80 nGrey31 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[0]).GetIndex();
81 nGrey32 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[1]).GetIndex();
82 nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], pHMap[2]).GetIndex();
84 Scanline pScanline = pWriteAcc->GetScanline(nY);
85 for (nX = 0; nX < nWidth; nX++)
87 nSum1 = nSum2 = 0;
89 nSum1 += nMask111 * nGrey11;
90 nSum2 += nMask112 * nGrey11;
92 nSum1 += nMask121 * nGrey12;
93 nSum2 += nMask122 * nGrey12;
95 nSum1 += nMask131 * nGrey13;
96 nSum2 += nMask132 * nGrey13;
98 nSum1 += nMask211 * nGrey21;
99 nSum2 += nMask212 * nGrey21;
101 nSum1 += nMask221 * nGrey22;
102 nSum2 += nMask222 * nGrey22;
104 nSum1 += nMask231 * nGrey23;
105 nSum2 += nMask232 * nGrey23;
107 nSum1 += nMask311 * nGrey31;
108 nSum2 += nMask312 * nGrey31;
110 nSum1 += nMask321 * nGrey32;
111 nSum2 += nMask322 * nGrey32;
113 nSum1 += nMask331 * nGrey33;
114 nSum2 += nMask332 * nGrey33;
116 nSum1 = static_cast<sal_Int32>(std::hypot(nSum1, nSum2));
118 aGrey.SetIndex(
119 ~static_cast<sal_uInt8>(std::clamp(nSum1, sal_Int32(0), sal_Int32(255))));
120 pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
122 if (nX < (nWidth - 1))
124 const sal_Int32 nNextX = pHMap[nX + 3];
126 nGrey11 = nGrey12;
127 nGrey12 = nGrey13;
128 nGrey13 = pReadAcc->GetPixel(pVMap[nY], nNextX).GetIndex();
129 nGrey21 = nGrey22;
130 nGrey22 = nGrey23;
131 nGrey23 = pReadAcc->GetPixel(pVMap[nY + 1], nNextX).GetIndex();
132 nGrey31 = nGrey32;
133 nGrey32 = nGrey33;
134 nGrey33 = pReadAcc->GetPixel(pVMap[nY + 2], nNextX).GetIndex();
139 pHMap.reset();
140 pVMap.reset();
141 pWriteAcc.reset();
142 pReadAcc.reset();
144 const MapMode aMap(aBitmap.GetPrefMapMode());
145 const Size aPrefSize(aBitmap.GetPrefSize());
147 aBitmap = aNewBmp;
149 aBitmap.SetPrefMapMode(aMap);
150 aBitmap.SetPrefSize(aPrefSize);
152 return BitmapEx(aBitmap);
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */