bump product version to 7.2.5.1
[LibreOffice.git] / vcl / source / bitmap / BitmapMosaicFilter.cxx
blob0426dc3ff044d82245fc4682bd578832bd05cf6c
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 <vcl/bitmap.hxx>
12 #include <vcl/bitmapex.hxx>
13 #include <vcl/BitmapMosaicFilter.hxx>
15 #include <bitmap/BitmapWriteAccess.hxx>
17 BitmapEx BitmapMosaicFilter::execute(BitmapEx const& rBitmapEx) const
19 Bitmap aBitmap(rBitmapEx.GetBitmap());
21 bool bRet = false;
23 if (mnTileWidth > 1 || mnTileHeight > 1)
25 std::unique_ptr<Bitmap> pNewBmp;
26 BitmapReadAccess* pReadAcc;
27 BitmapWriteAccess* pWriteAcc;
29 if (!isPalettePixelFormat(aBitmap.getPixelFormat()))
31 pReadAcc = pWriteAcc = aBitmap.AcquireWriteAccess();
33 else
35 pNewBmp.reset(new Bitmap(aBitmap.GetSizePixel(), vcl::PixelFormat::N24_BPP));
36 pReadAcc = aBitmap.AcquireReadAccess();
37 pWriteAcc = pNewBmp->AcquireWriteAccess();
40 bool bConditionsMet = false;
41 tools::Long nWidth(0);
42 tools::Long nHeight(0);
43 if (pReadAcc && pWriteAcc)
45 nWidth = pReadAcc->Width();
46 nHeight = pReadAcc->Height();
47 bConditionsMet = (nWidth > 0 && nHeight > 0);
50 if (bConditionsMet)
52 BitmapColor aCol;
53 tools::Long nX, nY, nX1, nX2, nY1, nY2, nSumR, nSumG, nSumB;
54 double fArea_1;
56 nY1 = 0;
57 nY2 = mnTileHeight - 1;
59 if (nY2 >= nHeight)
60 nY2 = nHeight - 1;
64 nX1 = 0;
65 nX2 = mnTileWidth - 1;
67 if (nX2 >= nWidth)
68 nX2 = nWidth - 1;
70 fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
72 if (!pNewBmp)
76 for (nY = nY1, nSumR = nSumG = nSumB = 0; nY <= nY2; nY++)
78 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
79 for (nX = nX1; nX <= nX2; nX++)
81 aCol = pReadAcc->GetPixelFromData(pScanlineRead, nX);
82 nSumR += aCol.GetRed();
83 nSumG += aCol.GetGreen();
84 nSumB += aCol.GetBlue();
88 aCol.SetRed(static_cast<sal_uInt8>(nSumR * fArea_1));
89 aCol.SetGreen(static_cast<sal_uInt8>(nSumG * fArea_1));
90 aCol.SetBlue(static_cast<sal_uInt8>(nSumB * fArea_1));
92 for (nY = nY1; nY <= nY2; nY++)
94 Scanline pScanline = pWriteAcc->GetScanline(nY);
95 for (nX = nX1; nX <= nX2; nX++)
96 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
99 nX1 += mnTileWidth;
100 nX2 += mnTileWidth;
102 if (nX2 >= nWidth)
104 nX2 = nWidth - 1;
105 fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
107 } while (nX1 < nWidth);
109 else
113 for (nY = nY1, nSumR = nSumG = nSumB = 0; nY <= nY2; nY++)
115 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
116 for (nX = nX1; nX <= nX2; nX++)
118 const BitmapColor& rCol = pReadAcc->GetPaletteColor(
119 pReadAcc->GetIndexFromData(pScanlineRead, nX));
120 nSumR += rCol.GetRed();
121 nSumG += rCol.GetGreen();
122 nSumB += rCol.GetBlue();
126 aCol.SetRed(static_cast<sal_uInt8>(nSumR * fArea_1));
127 aCol.SetGreen(static_cast<sal_uInt8>(nSumG * fArea_1));
128 aCol.SetBlue(static_cast<sal_uInt8>(nSumB * fArea_1));
130 for (nY = nY1; nY <= nY2; nY++)
132 Scanline pScanline = pWriteAcc->GetScanline(nY);
133 for (nX = nX1; nX <= nX2; nX++)
134 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
137 nX1 += mnTileWidth;
138 nX2 += mnTileWidth;
140 if (nX2 >= nWidth)
142 nX2 = nWidth - 1;
143 fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
145 } while (nX1 < nWidth);
148 nY1 += mnTileHeight;
149 nY2 += mnTileHeight;
151 if (nY2 >= nHeight)
152 nY2 = nHeight - 1;
154 } while (nY1 < nHeight);
156 bRet = true;
159 if (pWriteAcc == pReadAcc)
160 pWriteAcc = nullptr;
161 Bitmap::ReleaseAccess(pReadAcc);
163 if (pNewBmp)
165 Bitmap::ReleaseAccess(pWriteAcc);
167 if (bRet)
169 const MapMode aMap(aBitmap.GetPrefMapMode());
170 const Size aPrefSize(aBitmap.GetPrefSize());
172 aBitmap = *pNewBmp;
174 aBitmap.SetPrefMapMode(aMap);
175 aBitmap.SetPrefSize(aPrefSize);
180 if (bRet)
181 return BitmapEx(aBitmap);
183 return BitmapEx();
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */