bump product version to 7.2.5.1
[LibreOffice.git] / vcl / inc / scanlinewriter.hxx
blob43334f955c5099de23a6a3d799416c5e53b9d9f3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VCL_INC_SCANLINEWRITER_HXX
21 #define INCLUDED_VCL_INC_SCANLINEWRITER_HXX
23 #include <vcl/bitmap.hxx>
24 #include <vcl/BitmapPalette.hxx>
26 namespace vcl
28 // Write color information for 1, 4 and 8 bit palette bitmap scanlines.
29 class ScanlineWriter
31 BitmapPalette& maPalette;
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
35 sal_uInt8* mpCurrentScanline;
36 tools::Long mnX;
38 public:
39 ScanlineWriter(BitmapPalette& aPalette, sal_Int8 nColorsPerByte)
40 : maPalette(aPalette)
41 , mnColorsPerByte(nColorsPerByte)
42 , mnColorBitSize(
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)
51 static std::unique_ptr<ScanlineWriter> Create(sal_uInt16 nBits, BitmapPalette& aPalette)
53 switch (nBits)
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();
66 void writeRGB(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB)
68 // calculate to which index we will write
69 tools::Long nScanlineIndex = mnX / mnColorsPerByte;
71 // calculate the number of shifts to get the color information to the right place
72 tools::Long nShift = (8 - mnColorBitSize) - ((mnX % mnColorsPerByte) * mnColorBitSize);
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++;
80 void nextLine(sal_uInt8* pScanline)
82 mnX = 0;
83 mpCurrentScanline = pScanline;
87 } // namespace vcl
89 #endif
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */