1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#ifndef INCLUDED_VCL_INC_BITMAP_SCANLINETOOLS_HXX
#define INCLUDED_VCL_INC_BITMAP_SCANLINETOOLS_HXX
#include <tools/color.hxx>
#include <vcl/BitmapPalette.hxx>
namespace vcl::bitmap
{
class ScanlineTransformer
{
public:
virtual void startLine(sal_uInt8* pLine) = 0;
virtual void skipPixel(sal_uInt32 nPixel) = 0;
virtual Color readPixel() = 0;
virtual void writePixel(Color nColor) = 0;
virtual ~ScanlineTransformer() = default;
};
class ScanlineTransformer_ARGB : public ScanlineTransformer
{
private:
sal_uInt8* pData;
public:
virtual void startLine(sal_uInt8* pLine) override { pData = pLine; }
virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel << 2; }
virtual Color readPixel() override
{
const Color aColor(pData[4], pData[1], pData[2], pData[3]);
pData += 4;
return aColor;
}
virtual void writePixel(Color nColor) override
{
*pData++ = nColor.GetTransparency();
*pData++ = nColor.GetRed();
*pData++ = nColor.GetGreen();
*pData++ = nColor.GetBlue();
}
};
class ScanlineTransformer_BGR : public ScanlineTransformer
{
private:
sal_uInt8* pData;
public:
virtual void startLine(sal_uInt8* pLine) override { pData = pLine; }
virtual void skipPixel(sal_uInt32 nPixel) override { pData += (nPixel << 1) + nPixel; }
virtual Color readPixel() override
{
const Color aColor(pData[2], pData[1], pData[0]);
pData += 3;
return aColor;
}
virtual void writePixel(Color nColor) override
{
*pData++ = nColor.GetBlue();
*pData++ = nColor.GetGreen();
*pData++ = nColor.GetRed();
}
};
class ScanlineTransformer_RGB565 : public ScanlineTransformer
{
private:
sal_uInt16* pData;
public:
virtual void startLine(sal_uInt8* pLine) override
{
pData = reinterpret_cast<sal_uInt16*>(pLine);
}
virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel; }
virtual Color readPixel() override
{
const Color nColor((*pData & 0xf800) >> 8, (*pData & 0x07e0) >> 3, (*pData & 0x001f) << 3);
pData++;
return nColor;
}
virtual void writePixel(Color nColor) override
{
*pData++ = ((nColor.GetRed() & 0xf8) << 8) | ((nColor.GetGreen() & 0xfc) << 3)
| ((nColor.GetBlue() & 0xf8) >> 3);
}
};
class ScanlineTransformer_8BitPalette : public ScanlineTransformer
{
private:
sal_uInt8* pData;
const BitmapPalette& mrPalette;
public:
explicit ScanlineTransformer_8BitPalette(const BitmapPalette& rPalette)
: pData(nullptr)
, mrPalette(rPalette)
{
}
virtual void startLine(sal_uInt8* pLine) override { pData = pLine; }
virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel; }
virtual Color readPixel() override
{
const sal_uInt8 nIndex(*pData++);
if (nIndex < mrPalette.GetEntryCount())
return mrPalette[nIndex];
else
return COL_BLACK;
}
virtual void writePixel(Color nColor) override
{
*pData++ = static_cast<sal_uInt8>(mrPalette.GetBestIndex(nColor));
}
};
class ScanlineTransformer_4BitPalette : public ScanlineTransformer
{
private:
sal_uInt8* pData;
const BitmapPalette& mrPalette;
sal_uInt32 mnX;
sal_uInt32 mnShift;
public:
explicit ScanlineTransformer_4BitPalette(const BitmapPalette& rPalette)
: pData(nullptr)
, mrPalette(rPalette)
, mnX(0)
, mnShift(0)
{
}
virtual void skipPixel(sal_uInt32 nPixel) override
{
mnX += nPixel;
if (nPixel & 1) // is nPixel an odd number
mnShift ^= 4;
}
virtual void startLine(sal_uInt8* pLine) override
{
pData = pLine;
mnX = 0;
mnShift = 4;
}
virtual Color readPixel() override
{
const sal_uInt32 nDataIndex = mnX / 2;
const sal_uInt8 nIndex((pData[nDataIndex] >> mnShift) & 0x0f);
mnX++;
mnShift ^= 4;
if (nIndex < mrPalette.GetEntryCount())
return mrPalette[nIndex];
else
return COL_BLACK;
}
virtual void writePixel(Color nColor) override
{
const sal_uInt32 nDataIndex = mnX / 2;
const sal_uInt8 nColorIndex = mrPalette.GetBestIndex(nColor);
pData[nDataIndex] |= (nColorIndex & 0x0f) << mnShift;
mnX++;
mnShift ^= 4;
}
};
class ScanlineTransformer_1BitPalette : public ScanlineTransformer
{
private:
sal_uInt8* pData;
const BitmapPalette& mrPalette;
sal_uInt32 mnX;
public:
explicit ScanlineTransformer_1BitPalette(const BitmapPalette& rPalette)
: pData(nullptr)
, mrPalette(rPalette)
, mnX(0)
{
}
virtual void skipPixel(sal_uInt32 nPixel) override { mnX += nPixel; }
virtual void startLine(sal_uInt8* pLine) override
{
pData = pLine;
mnX = 0;
}
virtual Color readPixel() override
{
const sal_uInt8 nIndex((pData[mnX >> 3] >> (7 - (mnX & 7))) & 1);
mnX++;
if (nIndex < mrPalette.GetEntryCount())
return mrPalette[nIndex];
else
return COL_BLACK;
}
virtual void writePixel(Color nColor) override
{
if (mrPalette.GetBestIndex(nColor) & 1)
pData[mnX >> 3] |= 1 << (7 - (mnX & 7));
else
pData[mnX >> 3] &= ~(1 << (7 - (mnX & 7)));
mnX++;
}
};
std::unique_ptr<ScanlineTransformer> getScanlineTransformer(sal_uInt16 nBits,
const BitmapPalette& rPalette)
{
switch (nBits)
{
case 1:
return std::make_unique<ScanlineTransformer_1BitPalette>(rPalette);
case 4:
return std::make_unique<ScanlineTransformer_4BitPalette>(rPalette);
case 8:
return std::make_unique<ScanlineTransformer_8BitPalette>(rPalette);
case 16:
return std::make_unique<ScanlineTransformer_RGB565>();
case 24:
return std::make_unique<ScanlineTransformer_BGR>();
case 32:
return std::make_unique<ScanlineTransformer_ARGB>();
default:
break;
}
return nullptr;
}
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|