bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / bitmap / BitmapMosaicFilter.cxx
blobee0d102750dc09718c670bd36f886f6fde6c0008
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/bitmapaccess.hxx>
14 #include <vcl/BitmapMosaicFilter.hxx>
16 #include <bitmapwriteaccess.hxx>
18 BitmapEx BitmapMosaicFilter::execute(BitmapEx const& rBitmapEx) const
20 Bitmap aBitmap(rBitmapEx.GetBitmap());
22 bool bRet = false;
24 if (mnTileWidth > 1 || mnTileHeight > 1)
26 std::unique_ptr<Bitmap> pNewBmp;
27 BitmapReadAccess* pReadAcc;
28 BitmapWriteAccess* pWriteAcc;
30 if (aBitmap.GetBitCount() > 8)
32 pReadAcc = pWriteAcc = aBitmap.AcquireWriteAccess();
34 else
36 pNewBmp.reset(new Bitmap(aBitmap.GetSizePixel(), 24));
37 pReadAcc = aBitmap.AcquireReadAccess();
38 pWriteAcc = pNewBmp->AcquireWriteAccess();
41 bool bConditionsMet = false;
42 long nWidth(0);
43 long nHeight(0);
44 if (pReadAcc && pWriteAcc)
46 nWidth = pReadAcc->Width();
47 nHeight = pReadAcc->Height();
48 bConditionsMet = (nWidth > 0 && nHeight > 0);
51 if (bConditionsMet)
53 BitmapColor aCol;
54 long nX, nY, nX1, nX2, nY1, nY2, nSumR, nSumG, nSumB;
55 double fArea_1;
57 nY1 = 0;
58 nY2 = mnTileHeight - 1;
60 if (nY2 >= nHeight)
61 nY2 = nHeight - 1;
65 nX1 = 0;
66 nX2 = mnTileWidth - 1;
68 if (nX2 >= nWidth)
69 nX2 = nWidth - 1;
71 fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
73 if (!pNewBmp)
77 for (nY = nY1, nSumR = nSumG = nSumB = 0; nY <= nY2; nY++)
79 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
80 for (nX = nX1; nX <= nX2; nX++)
82 aCol = pReadAcc->GetPixelFromData(pScanlineRead, nX);
83 nSumR += aCol.GetRed();
84 nSumG += aCol.GetGreen();
85 nSumB += aCol.GetBlue();
89 aCol.SetRed(static_cast<sal_uInt8>(nSumR * fArea_1));
90 aCol.SetGreen(static_cast<sal_uInt8>(nSumG * fArea_1));
91 aCol.SetBlue(static_cast<sal_uInt8>(nSumB * fArea_1));
93 for (nY = nY1; nY <= nY2; nY++)
95 Scanline pScanline = pWriteAcc->GetScanline(nY);
96 for (nX = nX1; nX <= nX2; nX++)
97 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
100 nX1 += mnTileWidth;
101 nX2 += mnTileWidth;
103 if (nX2 >= nWidth)
105 nX2 = nWidth - 1;
106 fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
108 } while (nX1 < nWidth);
110 else
114 for (nY = nY1, nSumR = nSumG = nSumB = 0; nY <= nY2; nY++)
116 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
117 for (nX = nX1; nX <= nX2; nX++)
119 const BitmapColor& rCol = pReadAcc->GetPaletteColor(
120 pReadAcc->GetIndexFromData(pScanlineRead, nX));
121 nSumR += rCol.GetRed();
122 nSumG += rCol.GetGreen();
123 nSumB += rCol.GetBlue();
127 aCol.SetRed(static_cast<sal_uInt8>(nSumR * fArea_1));
128 aCol.SetGreen(static_cast<sal_uInt8>(nSumG * fArea_1));
129 aCol.SetBlue(static_cast<sal_uInt8>(nSumB * fArea_1));
131 for (nY = nY1; nY <= nY2; nY++)
133 Scanline pScanline = pWriteAcc->GetScanline(nY);
134 for (nX = nX1; nX <= nX2; nX++)
135 pWriteAcc->SetPixelOnData(pScanline, nX, aCol);
138 nX1 += mnTileWidth;
139 nX2 += mnTileWidth;
141 if (nX2 >= nWidth)
143 nX2 = nWidth - 1;
144 fArea_1 = 1.0 / ((nX2 - nX1 + 1) * (nY2 - nY1 + 1));
146 } while (nX1 < nWidth);
149 nY1 += mnTileHeight;
150 nY2 += mnTileHeight;
152 if (nY2 >= nHeight)
153 nY2 = nHeight - 1;
155 } while (nY1 < nHeight);
157 bRet = true;
160 Bitmap::ReleaseAccess(pReadAcc);
162 if (pNewBmp)
164 Bitmap::ReleaseAccess(pWriteAcc);
166 if (bRet)
168 const MapMode aMap(aBitmap.GetPrefMapMode());
169 const Size aPrefSize(aBitmap.GetPrefSize());
171 aBitmap = *pNewBmp;
173 aBitmap.SetPrefMapMode(aMap);
174 aBitmap.SetPrefSize(aPrefSize);
179 if (bRet)
180 return BitmapEx(aBitmap);
182 return BitmapEx();
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */