1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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());
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();
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);
53 tools::Long nX
, nY
, nX1
, nX2
, nY1
, nY2
, nSumR
, nSumG
, nSumB
;
57 nY2
= mnTileHeight
- 1;
65 nX2
= mnTileWidth
- 1;
70 fArea_1
= 1.0 / ((nX2
- nX1
+ 1) * (nY2
- nY1
+ 1));
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
);
105 fArea_1
= 1.0 / ((nX2
- nX1
+ 1) * (nY2
- nY1
+ 1));
107 } while (nX1
< nWidth
);
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
);
143 fArea_1
= 1.0 / ((nX2
- nX1
+ 1) * (nY2
- nY1
+ 1));
145 } while (nX1
< nWidth
);
154 } while (nY1
< nHeight
);
159 if (pWriteAcc
== pReadAcc
)
161 Bitmap::ReleaseAccess(pReadAcc
);
165 Bitmap::ReleaseAccess(pWriteAcc
);
169 const MapMode
aMap(aBitmap
.GetPrefMapMode());
170 const Size
aPrefSize(aBitmap
.GetPrefSize());
174 aBitmap
.SetPrefMapMode(aMap
);
175 aBitmap
.SetPrefSize(aPrefSize
);
181 return BitmapEx(aBitmap
);
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */