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 if (!aBitmap.ImplMakeGreyscales())
26 return BitmapEx();
27
28 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
29 if (!pReadAcc)
30 return BitmapEx();
31
32 Bitmap aNewBmp(aBitmap.GetSizePixel(), vcl::PixelFormat::N8_BPP, &pReadAcc->GetPalette());
33 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
34 if (!pWriteAcc)
35 return BitmapEx();
36
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;
52
53 // fill mapping tables
54 pHMap[0] = 0;
55
56 for (nX = 1; nX <= nWidth; nX++)
57 {
58 pHMap[nX] = nX - 1;
59 }
60
61 pHMap[nWidth + 1] = nWidth - 1;
62
63 pVMap[0] = 0;
64
65 for (nY = 1; nY <= nHeight; nY++)
66 {
67 pVMap[nY] = nY - 1;
68 }
69
70 pVMap[nHeight + 1] = nHeight - 1;
71
72 for (nY = 0; nY < nHeight; nY++)
73 {
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();
83
84 Scanline pScanline = pWriteAcc->GetScanline(nY);
85 for (nX = 0; nX < nWidth; nX++)
86 {
87 nSum1 = nSum2 = 0;
88
89 nSum1 += nMask111 * nGrey11;
90 nSum2 += nMask112 * nGrey11;
91
92 nSum1 += nMask121 * nGrey12;
93 nSum2 += nMask122 * nGrey12;
94
95 nSum1 += nMask131 * nGrey13;
96 nSum2 += nMask132 * nGrey13;
97
98 nSum1 += nMask211 * nGrey21;
99 nSum2 += nMask212 * nGrey21;
100
101 nSum1 += nMask221 * nGrey22;
102 nSum2 += nMask222 * nGrey22;
103
104 nSum1 += nMask231 * nGrey23;
105 nSum2 += nMask232 * nGrey23;
106
107 nSum1 += nMask311 * nGrey31;
108 nSum2 += nMask312 * nGrey31;
109
110 nSum1 += nMask321 * nGrey32;
111 nSum2 += nMask322 * nGrey32;
112
113 nSum1 += nMask331 * nGrey33;
114 nSum2 += nMask332 * nGrey33;
115
116 nSum1 = static_cast<sal_Int32>(std::hypot(nSum1, nSum2));
117
118 aGrey.SetIndex(
119 ~static_cast<sal_uInt8>(std::clamp(nSum1, sal_Int32(0), sal_Int32(255))));
120 pWriteAcc->SetPixelOnData(pScanline, nX, aGrey);
121
122 if (nX < (nWidth - 1))
123 {
124 const sal_Int32 nNextX = pHMap[nX + 3];
125
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();
135 }
136 }
137 }
138
139 pHMap.reset();
140 pVMap.reset();
141 pWriteAcc.reset();
142 pReadAcc.reset();
143
144 const MapMode aMap(aBitmap.GetPrefMapMode());
145 const Size aPrefSize(aBitmap.GetPrefSize());
146
147 aBitmap = aNewBmp;
148
149 aBitmap.SetPrefMapMode(aMap);
150 aBitmap.SetPrefSize(aPrefSize);
151
152 return BitmapEx(aBitmap);
153}
154
155/* 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
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
const BitmapPalette & GetPalette() const
BitmapColor GetPixel(tools::Long nY, tools::Long nX) const
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
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
HashMap_OWString_Interface aMap
unsigned char sal_uInt8