LibreOffice Module vcl (master)  1
scanlinewriter.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_VCL_INC_SCANLINEWRITER_HXX
21 #define INCLUDED_VCL_INC_SCANLINEWRITER_HXX
22 
23 #include <vcl/bitmap.hxx>
24 #include <vcl/BitmapPalette.hxx>
25 
26 namespace vcl
27 {
28 
29 // Write color information for 1, 4 and 8 bit palette bitmap scanlines.
31 {
33  sal_uInt8 const mnColorsPerByte; // number of colors that are stored in one byte
34  sal_uInt8 const mnColorBitSize; // number of bits a color takes
35  sal_uInt8 const mnColorBitMask; // bit mask used to isolate the color
37  long mnX;
38 
39 public:
40 
41  ScanlineWriter(BitmapPalette& aPalette, sal_Int8 nColorsPerByte)
42  : maPalette(aPalette)
43  , mnColorsPerByte(nColorsPerByte)
44  , mnColorBitSize(8 / mnColorsPerByte) // bit size is number of bit in a byte divided by number of colors per byte (8 / 2 = 4 for 4-bit)
45  , mnColorBitMask((1 << mnColorBitSize) - 1) // calculate the bit mask from the bit size
46  , mpCurrentScanline(nullptr)
47  , mnX(0)
48  {}
49 
50  static std::unique_ptr<ScanlineWriter> Create(sal_uInt16 nBits, BitmapPalette& aPalette)
51  {
52  switch(nBits)
53  {
54  case 1:
55  return std::make_unique<ScanlineWriter>(aPalette, 8);
56  case 4:
57  return std::make_unique<ScanlineWriter>(aPalette, 2);
58  case 8:
59  return std::make_unique<ScanlineWriter>(aPalette, 1);
60  default:
61  abort();
62  }
63  }
64 
66  {
67  // calculate to which index we will write
68  long nScanlineIndex = mnX / mnColorsPerByte;
69 
70  // calculate the number of shifts to get the color information to the right place
71  long nShift = (8 - mnColorBitSize) - ((mnX % mnColorsPerByte) * mnColorBitSize);
72 
73  sal_uInt16 nColorIndex = maPalette.GetBestIndex(BitmapColor(nR, nG, nB));
74  mpCurrentScanline[nScanlineIndex] &= ~(mnColorBitMask << nShift); // clear
75  mpCurrentScanline[nScanlineIndex] |= (nColorIndex & mnColorBitMask) << nShift; // set
76  mnX++;
77  }
78 
79  void nextLine(sal_uInt8* pScanline)
80  {
81  mnX = 0;
82  mpCurrentScanline = pScanline;
83  }
84 };
85 
86 } // namespace vcl
87 
88 #endif
89 
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 const mnColorsPerByte
signed char sal_Int8
void nextLine(sal_uInt8 *pScanline)
void writeRGB(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB)
sal_uInt8 const mnColorBitSize
BitmapPalette & maPalette
sal_uInt8 * mpCurrentScanline
sal_uInt16 GetBestIndex(const BitmapColor &rCol) const
unsigned char sal_uInt8
static std::unique_ptr< ScanlineWriter > Create(sal_uInt16 nBits, BitmapPalette &aPalette)
ScanlineWriter(BitmapPalette &aPalette, sal_Int8 nColorsPerByte)
sal_uInt8 const mnColorBitMask