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 #ifndef INCLUDED_VCL_INC_BITMAP_SCANLINETOOLS_HXX
12 #define INCLUDED_VCL_INC_BITMAP_SCANLINETOOLS_HXX
14 #include <tools/color.hxx>
15 #include <vcl/BitmapPalette.hxx>
19 class ScanlineTransformer
22 virtual void startLine(sal_uInt8
* pLine
) = 0;
23 virtual void skipPixel(sal_uInt32 nPixel
) = 0;
24 virtual Color
readPixel() = 0;
25 virtual void writePixel(Color nColor
) = 0;
27 virtual ~ScanlineTransformer() = default;
30 class ScanlineTransformer_ARGB final
: public ScanlineTransformer
36 virtual void startLine(sal_uInt8
* pLine
) override
{ pData
= pLine
; }
38 virtual void skipPixel(sal_uInt32 nPixel
) override
{ pData
+= nPixel
<< 2; }
40 virtual Color
readPixel() override
42 const Color
aColor(ColorTransparency
, pData
[4], pData
[1], pData
[2], pData
[3]);
47 virtual void writePixel(Color nColor
) override
49 *pData
++ = 255 - nColor
.GetAlpha();
50 *pData
++ = nColor
.GetRed();
51 *pData
++ = nColor
.GetGreen();
52 *pData
++ = nColor
.GetBlue();
56 class ScanlineTransformer_BGR final
: public ScanlineTransformer
62 virtual void startLine(sal_uInt8
* pLine
) override
{ pData
= pLine
; }
64 virtual void skipPixel(sal_uInt32 nPixel
) override
{ pData
+= (nPixel
<< 1) + nPixel
; }
66 virtual Color
readPixel() override
68 const Color
aColor(pData
[2], pData
[1], pData
[0]);
73 virtual void writePixel(Color nColor
) override
75 *pData
++ = nColor
.GetBlue();
76 *pData
++ = nColor
.GetGreen();
77 *pData
++ = nColor
.GetRed();
81 class ScanlineTransformer_8BitPalette final
: public ScanlineTransformer
85 const BitmapPalette
& mrPalette
;
88 explicit ScanlineTransformer_8BitPalette(const BitmapPalette
& rPalette
)
94 virtual void startLine(sal_uInt8
* pLine
) override
{ pData
= pLine
; }
96 virtual void skipPixel(sal_uInt32 nPixel
) override
{ pData
+= nPixel
; }
98 virtual Color
readPixel() override
100 const sal_uInt8
nIndex(*pData
++);
101 if (nIndex
< mrPalette
.GetEntryCount())
102 return mrPalette
[nIndex
];
107 virtual void writePixel(Color nColor
) override
109 *pData
++ = static_cast<sal_uInt8
>(mrPalette
.GetBestIndex(nColor
));
113 class ScanlineTransformer_4BitPalette final
: public ScanlineTransformer
117 const BitmapPalette
& mrPalette
;
122 explicit ScanlineTransformer_4BitPalette(const BitmapPalette
& rPalette
)
124 , mrPalette(rPalette
)
130 virtual void skipPixel(sal_uInt32 nPixel
) override
133 if (nPixel
& 1) // is nPixel an odd number
137 virtual void startLine(sal_uInt8
* pLine
) override
144 virtual Color
readPixel() override
146 const sal_uInt32 nDataIndex
= mnX
/ 2;
147 const sal_uInt8
nIndex((pData
[nDataIndex
] >> mnShift
) & 0x0f);
151 if (nIndex
< mrPalette
.GetEntryCount())
152 return mrPalette
[nIndex
];
157 virtual void writePixel(Color nColor
) override
159 const sal_uInt32 nDataIndex
= mnX
/ 2;
160 const sal_uInt8 nColorIndex
= mrPalette
.GetBestIndex(nColor
);
161 pData
[nDataIndex
] |= (nColorIndex
& 0x0f) << mnShift
;
167 class ScanlineTransformer_1BitPalette final
: public ScanlineTransformer
171 const BitmapPalette
& mrPalette
;
175 explicit ScanlineTransformer_1BitPalette(const BitmapPalette
& rPalette
)
177 , mrPalette(rPalette
)
182 virtual void skipPixel(sal_uInt32 nPixel
) override
{ mnX
+= nPixel
; }
184 virtual void startLine(sal_uInt8
* pLine
) override
190 virtual Color
readPixel() override
192 const sal_uInt8
nIndex((pData
[mnX
>> 3] >> (7 - (mnX
& 7))) & 1);
195 if (nIndex
< mrPalette
.GetEntryCount())
196 return mrPalette
[nIndex
];
201 virtual void writePixel(Color nColor
) override
203 if (mrPalette
.GetBestIndex(nColor
) & 1)
204 pData
[mnX
>> 3] |= 1 << (7 - (mnX
& 7));
206 pData
[mnX
>> 3] &= ~(1 << (7 - (mnX
& 7)));
211 std::unique_ptr
<ScanlineTransformer
> getScanlineTransformer(sal_uInt16 nBits
,
212 const BitmapPalette
& rPalette
)
217 return std::make_unique
<ScanlineTransformer_1BitPalette
>(rPalette
);
219 return std::make_unique
<ScanlineTransformer_4BitPalette
>(rPalette
);
221 return std::make_unique
<ScanlineTransformer_8BitPalette
>(rPalette
);
223 return std::make_unique
<ScanlineTransformer_BGR
>();
225 return std::make_unique
<ScanlineTransformer_ARGB
>();
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */