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/bitmapaccess.hxx>
14 #include <vcl/BitmapMosaicFilter.hxx>
16 #include <bitmapwriteaccess.hxx>
18 BitmapEx
BitmapMosaicFilter::execute(BitmapEx
const& rBitmapEx
) const
20 Bitmap
aBitmap(rBitmapEx
.GetBitmap());
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();
36 pNewBmp
.reset(new Bitmap(aBitmap
.GetSizePixel(), 24));
37 pReadAcc
= aBitmap
.AcquireReadAccess();
38 pWriteAcc
= pNewBmp
->AcquireWriteAccess();
41 bool bConditionsMet
= false;
44 if (pReadAcc
&& pWriteAcc
)
46 nWidth
= pReadAcc
->Width();
47 nHeight
= pReadAcc
->Height();
48 bConditionsMet
= (nWidth
> 0 && nHeight
> 0);
54 long nX
, nY
, nX1
, nX2
, nY1
, nY2
, nSumR
, nSumG
, nSumB
;
58 nY2
= mnTileHeight
- 1;
66 nX2
= mnTileWidth
- 1;
71 fArea_1
= 1.0 / ((nX2
- nX1
+ 1) * (nY2
- nY1
+ 1));
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
);
106 fArea_1
= 1.0 / ((nX2
- nX1
+ 1) * (nY2
- nY1
+ 1));
108 } while (nX1
< nWidth
);
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
);
144 fArea_1
= 1.0 / ((nX2
- nX1
+ 1) * (nY2
- nY1
+ 1));
146 } while (nX1
< nWidth
);
155 } while (nY1
< nHeight
);
160 Bitmap::ReleaseAccess(pReadAcc
);
164 Bitmap::ReleaseAccess(pWriteAcc
);
168 const MapMode
aMap(aBitmap
.GetPrefMapMode());
169 const Size
aPrefSize(aBitmap
.GetPrefSize());
173 aBitmap
.SetPrefMapMode(aMap
);
174 aBitmap
.SetPrefSize(aPrefSize
);
180 return BitmapEx(aBitmap
);
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */