1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <tools/long.hxx>
24 #include <vcl/BitmapPalette.hxx>
28 // Write color information for 1, 4 and 8 bit palette bitmap scanlines.
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
;
39 ScanlineWriter(BitmapPalette
& aPalette
, sal_Int8 nColorsPerByte
)
41 , mnColorsPerByte(nColorsPerByte
)
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)
51 static std::unique_ptr
<ScanlineWriter
> Create(sal_uInt16 nBits
, BitmapPalette
& aPalette
)
56 return std::make_unique
<ScanlineWriter
>(aPalette
, 8);
58 return std::make_unique
<ScanlineWriter
>(aPalette
, 2);
60 return std::make_unique
<ScanlineWriter
>(aPalette
, 1);
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
80 void nextLine(sal_uInt8
* pScanline
)
83 mpCurrentScanline
= pScanline
;
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */