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 // Write color information for 1, 4 and 8 bit palette bitmap scanlines.
30 {
32  sal_uInt8 const mnColorsPerByte; // number of colors that are stored in one byte
33  sal_uInt8 const mnColorBitSize; // number of bits a color takes
34  sal_uInt8 const mnColorBitMask; // bit mask used to isolate the color
37 
38 public:
39  ScanlineWriter(BitmapPalette& aPalette, sal_Int8 nColorsPerByte)
40  : maPalette(aPalette)
41  , mnColorsPerByte(nColorsPerByte)
42  , mnColorBitSize(
43  8
44  / 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 
51  static std::unique_ptr<ScanlineWriter> Create(sal_uInt16 nBits, BitmapPalette& aPalette)
52  {
53  switch (nBits)
54  {
55  case 1:
56  return std::make_unique<ScanlineWriter>(aPalette, 8);
57  case 4:
58  return std::make_unique<ScanlineWriter>(aPalette, 2);
59  case 8:
60  return std::make_unique<ScanlineWriter>(aPalette, 1);
61  default:
62  abort();
63  }
64  }
65 
67  {
68  // calculate to which index we will write
69  tools::Long nScanlineIndex = mnX / mnColorsPerByte;
70 
71  // calculate the number of shifts to get the color information to the right place
72  tools::Long nShift = (8 - mnColorBitSize) - ((mnX % mnColorsPerByte) * mnColorBitSize);
73 
74  sal_uInt16 nColorIndex = maPalette.GetBestIndex(BitmapColor(nR, nG, nB));
75  mpCurrentScanline[nScanlineIndex] &= ~(mnColorBitMask << nShift); // clear
76  mpCurrentScanline[nScanlineIndex] |= (nColorIndex & mnColorBitMask) << nShift; // set
77  mnX++;
78  }
79 
80  void nextLine(sal_uInt8* pScanline)
81  {
82  mnX = 0;
83  mpCurrentScanline = pScanline;
84  }
85 };
86 
87 } // namespace vcl
88 
89 #endif
90 
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 const mnColorsPerByte
signed char sal_Int8
long Long
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