gb_var2file: remove now unused chunk-size parameter
[LibreOffice.git] / vcl / inc / bitmap / ScanlineTools.hxx
blobc343cf34f61e907869152aee57863b2ddbc1cb11
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 #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>
17 namespace vcl::bitmap
19 class ScanlineTransformer
21 public:
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
32 private:
33 sal_uInt8* pData;
35 public:
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]);
43 pData += 4;
44 return aColor;
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
58 private:
59 sal_uInt8* pData;
61 public:
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]);
69 pData += 3;
70 return aColor;
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
83 private:
84 sal_uInt8* pData;
85 const BitmapPalette& mrPalette;
87 public:
88 explicit ScanlineTransformer_8BitPalette(const BitmapPalette& rPalette)
89 : pData(nullptr)
90 , mrPalette(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];
103 else
104 return COL_BLACK;
107 virtual void writePixel(Color nColor) override
109 *pData++ = static_cast<sal_uInt8>(mrPalette.GetBestIndex(nColor));
113 class ScanlineTransformer_4BitPalette final : public ScanlineTransformer
115 private:
116 sal_uInt8* pData;
117 const BitmapPalette& mrPalette;
118 sal_uInt32 mnX;
119 sal_uInt32 mnShift;
121 public:
122 explicit ScanlineTransformer_4BitPalette(const BitmapPalette& rPalette)
123 : pData(nullptr)
124 , mrPalette(rPalette)
125 , mnX(0)
126 , mnShift(0)
130 virtual void skipPixel(sal_uInt32 nPixel) override
132 mnX += nPixel;
133 if (nPixel & 1) // is nPixel an odd number
134 mnShift ^= 4;
137 virtual void startLine(sal_uInt8* pLine) override
139 pData = pLine;
140 mnX = 0;
141 mnShift = 4;
144 virtual Color readPixel() override
146 const sal_uInt32 nDataIndex = mnX / 2;
147 const sal_uInt8 nIndex((pData[nDataIndex] >> mnShift) & 0x0f);
148 mnX++;
149 mnShift ^= 4;
151 if (nIndex < mrPalette.GetEntryCount())
152 return mrPalette[nIndex];
153 else
154 return COL_BLACK;
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;
162 mnX++;
163 mnShift ^= 4;
167 class ScanlineTransformer_1BitPalette final : public ScanlineTransformer
169 private:
170 sal_uInt8* pData;
171 const BitmapPalette& mrPalette;
172 sal_uInt32 mnX;
174 public:
175 explicit ScanlineTransformer_1BitPalette(const BitmapPalette& rPalette)
176 : pData(nullptr)
177 , mrPalette(rPalette)
178 , mnX(0)
182 virtual void skipPixel(sal_uInt32 nPixel) override { mnX += nPixel; }
184 virtual void startLine(sal_uInt8* pLine) override
186 pData = pLine;
187 mnX = 0;
190 virtual Color readPixel() override
192 const sal_uInt8 nIndex((pData[mnX >> 3] >> (7 - (mnX & 7))) & 1);
193 mnX++;
195 if (nIndex < mrPalette.GetEntryCount())
196 return mrPalette[nIndex];
197 else
198 return COL_BLACK;
201 virtual void writePixel(Color nColor) override
203 if (mrPalette.GetBestIndex(nColor) & 1)
204 pData[mnX >> 3] |= 1 << (7 - (mnX & 7));
205 else
206 pData[mnX >> 3] &= ~(1 << (7 - (mnX & 7)));
207 mnX++;
211 std::unique_ptr<ScanlineTransformer> getScanlineTransformer(sal_uInt16 nBits,
212 const BitmapPalette& rPalette)
214 switch (nBits)
216 case 1:
217 return std::make_unique<ScanlineTransformer_1BitPalette>(rPalette);
218 case 4:
219 return std::make_unique<ScanlineTransformer_4BitPalette>(rPalette);
220 case 8:
221 return std::make_unique<ScanlineTransformer_8BitPalette>(rPalette);
222 case 24:
223 return std::make_unique<ScanlineTransformer_BGR>();
224 case 32:
225 return std::make_unique<ScanlineTransformer_ARGB>();
226 default:
227 assert(false);
228 break;
230 return nullptr;
234 #endif
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */