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:
86 
87 public:
89  : pData(nullptr)
90  , mrPalette(rPalette)
91  {
92  }
93 
94  virtual void startLine(sal_uInt8* pLine) override { pData = pLine; }
95 
96  virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel; }
97 
98  virtual Color readPixel() override
99  {
100  const sal_uInt8 nIndex(*pData++);
101  if (nIndex < mrPalette.GetEntryCount())
102  return mrPalette[nIndex];
103  else
104  return COL_BLACK;
105  }
106 
107  virtual void writePixel(Color nColor) override
108  {
109  *pData++ = static_cast<sal_uInt8>(mrPalette.GetBestIndex(nColor));
110  }
111 };
112 
114 {
115 private:
118  sal_uInt32 mnX;
119  sal_uInt32 mnShift;
120 
121 public:
123  : pData(nullptr)
124  , mrPalette(rPalette)
125  , mnX(0)
126  , mnShift(0)
127  {
128  }
129 
130  virtual void skipPixel(sal_uInt32 nPixel) override
131  {
132  mnX += nPixel;
133  if (nPixel & 1) // is nPixel an odd number
134  mnShift ^= 4;
135  }
136 
137  virtual void startLine(sal_uInt8* pLine) override
138  {
139  pData = pLine;
140  mnX = 0;
141  mnShift = 4;
142  }
143 
144  virtual Color readPixel() override
145  {
146  const sal_uInt32 nDataIndex = mnX / 2;
147  const sal_uInt8 nIndex((pData[nDataIndex] >> mnShift) & 0x0f);
148  mnX++;
149  mnShift ^= 4;
150 
151  if (nIndex < mrPalette.GetEntryCount())
152  return mrPalette[nIndex];
153  else
154  return COL_BLACK;
155  }
156 
157  virtual void writePixel(Color nColor) override
158  {
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;
164  }
165 };
166 
168 {
169 private:
172  sal_uInt32 mnX;
173 
174 public:
176  : pData(nullptr)
177  , mrPalette(rPalette)
178  , mnX(0)
179  {
180  }
181 
182  virtual void skipPixel(sal_uInt32 nPixel) override { mnX += nPixel; }
183 
184  virtual void startLine(sal_uInt8* pLine) override
185  {
186  pData = pLine;
187  mnX = 0;
188  }
189 
190  virtual Color readPixel() override
191  {
192  const sal_uInt8 nIndex((pData[mnX >> 3] >> (7 - (mnX & 7))) & 1);
193  mnX++;
194 
195  if (nIndex < mrPalette.GetEntryCount())
196  return mrPalette[nIndex];
197  else
198  return COL_BLACK;
199  }
200 
201  virtual void writePixel(Color nColor) override
202  {
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++;
208  }
209 };
210 
211 std::unique_ptr<ScanlineTransformer> getScanlineTransformer(sal_uInt16 nBits,
212  const BitmapPalette& rPalette)
213 {
214  switch (nBits)
215  {
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;
229  }
230  return nullptr;
231 }
232 }
233 
234 #endif
235 
236 /* 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
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
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 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