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