LibreOffice Module vcl (master) 1
salbmp.cxx
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#include <salbmp.hxx>
21#include <o3tl/enumarray.hxx>
22
23static BitmapChecksum scanlineChecksum(BitmapChecksum nCrc, const sal_uInt8* bits, int lineBitsCount, sal_uInt8 extraBitsMask)
24{
25 if( lineBitsCount / 8 > 0 )
26 nCrc = vcl_get_checksum( nCrc, bits, lineBitsCount / 8 );
27 if( extraBitsMask != 0 )
28 {
29 sal_uInt8 extraByte = bits[ lineBitsCount / 8 ] & extraBitsMask;
30 nCrc = vcl_get_checksum( nCrc, &extraByte, 1 );
31 }
32 return nCrc;
33}
34
36{
38 return;
39
40 BitmapChecksum nCrc = 0;
41 SalBitmap* pThis = const_cast<SalBitmap*>(this);
43 if (pBuf)
44 {
45 nCrc = pBuf->maPalette.GetChecksum();
46 const int lineBitsCount = pBuf->mnWidth * pBuf->mnBitCount;
47 // With 1bpp/4bpp format we need to check only used bits in the last byte.
48 sal_uInt8 extraBitsMask = 0;
49 if( lineBitsCount % 8 != 0 )
50 {
51 const int extraBitsCount = lineBitsCount % 8;
52 switch( RemoveScanline( pBuf->mnFormat ))
53 {
55 {
56 static const sal_uInt8 mask1Bit[] = { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
57 extraBitsMask = mask1Bit[ extraBitsCount ];
58 break;
59 }
61 {
62 static const sal_uInt8 mask1Bit[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
63 extraBitsMask = mask1Bit[ extraBitsCount ];
64 break;
65 }
66 default:
67 break;
68 }
69 }
71 {
72 if( pBuf->mnScanlineSize == lineBitsCount / 8 )
73 nCrc = vcl_get_checksum(nCrc, pBuf->mpBits, pBuf->mnScanlineSize * pBuf->mnHeight);
74 else // Do not include padding with undefined content in the checksum.
75 for( tools::Long y = 0; y < pBuf->mnHeight; ++y )
76 nCrc = scanlineChecksum(nCrc, pBuf->mpBits + y * pBuf->mnScanlineSize, lineBitsCount, extraBitsMask);
77 }
78 else // Compute checksum in the order of scanlines, to make it consistent between different bitmap implementations.
79 {
80 for( tools::Long y = pBuf->mnHeight - 1; y >= 0; --y )
81 nCrc = scanlineChecksum(nCrc, pBuf->mpBits + y * pBuf->mnScanlineSize, lineBitsCount, extraBitsMask);
82 }
84 pThis->mnChecksum = nCrc;
85 pThis->mbChecksumValid = true;
86 }
87 else
88 {
89 pThis->mbChecksumValid = false;
90 }
91}
92
93namespace
94{
95
96class ImplPixelFormat
97{
98protected:
99 const sal_uInt8* mpData;
100public:
101 static ImplPixelFormat* GetFormat( sal_uInt16 nBits, const BitmapPalette& rPalette );
102
103 virtual void StartLine( const sal_uInt8* pLine ) { mpData = pLine; }
104 virtual const BitmapColor& ReadPixel() = 0;
105 virtual ~ImplPixelFormat() { }
106};
107
108class ImplPixelFormat8 : public ImplPixelFormat
109{
110private:
111 const BitmapPalette& mrPalette;
112
113public:
114 explicit ImplPixelFormat8( const BitmapPalette& rPalette )
115 : mrPalette( rPalette )
116 {
117 }
118 virtual const BitmapColor& ReadPixel() override
119 {
120 assert( mrPalette.GetEntryCount() > *mpData );
121 return mrPalette[ *mpData++ ];
122 }
123};
124
125class ImplPixelFormat4 : public ImplPixelFormat
126{
127private:
128 const BitmapPalette& mrPalette;
129 sal_uInt32 mnX;
130 sal_uInt32 mnShift;
131
132public:
133 explicit ImplPixelFormat4( const BitmapPalette& rPalette )
134 : mrPalette( rPalette )
135 , mnX(0)
136 , mnShift(4)
137 {
138 }
139 virtual void StartLine( const sal_uInt8* pLine ) override
140 {
141 mpData = pLine;
142 mnX = 0;
143 mnShift = 4;
144 }
145 virtual const BitmapColor& ReadPixel() override
146 {
147 sal_uInt32 nIdx = ( mpData[mnX >> 1] >> mnShift) & 0x0f;
148 assert( mrPalette.GetEntryCount() > nIdx );
149 const BitmapColor& rColor = mrPalette[nIdx];
150 mnX++;
151 mnShift ^= 4;
152 return rColor;
153 }
154};
155
156class ImplPixelFormat1 : public ImplPixelFormat
157{
158private:
159 const BitmapPalette& mrPalette;
160 sal_uInt32 mnX;
161
162public:
163 explicit ImplPixelFormat1( const BitmapPalette& rPalette )
164 : mrPalette(rPalette)
165 , mnX(0)
166 {
167 }
168 virtual void StartLine( const sal_uInt8* pLine ) override
169 {
170 mpData = pLine;
171 mnX = 0;
172 }
173 virtual const BitmapColor& ReadPixel() override
174 {
175 const BitmapColor& rColor = mrPalette[ (mpData[mnX >> 3 ] >> ( 7 - ( mnX & 7 ) )) & 1];
176 mnX++;
177 return rColor;
178 }
179};
180
181ImplPixelFormat* ImplPixelFormat::GetFormat( sal_uInt16 nBits, const BitmapPalette& rPalette )
182{
183 switch( nBits )
184 {
185 case 1: return new ImplPixelFormat1( rPalette );
186 case 4: return new ImplPixelFormat4( rPalette );
187 case 8: return new ImplPixelFormat8( rPalette );
188 }
189
190 return nullptr;
191}
192
193// Optimized conversion from 1bpp. Currently LO uses 1bpp bitmaps for masks, which is nowadays
194// a lousy obsolete format, as the memory saved is just not worth the cost of fiddling with the bits.
195// Ideally LO should move to RGBA bitmaps. Until then, try to be faster with 1bpp bitmaps.
196typedef void(*WriteColorFunction)( sal_uInt8 color8Bit, sal_uInt8*& dst );
197void writeColorA8(sal_uInt8 color8Bit, sal_uInt8*& dst ) { *dst++ = color8Bit; };
198void writeColorRGBA(sal_uInt8 color8Bit, sal_uInt8*& dst ) { *dst++ = color8Bit; *dst++ = color8Bit; *dst++ = color8Bit; *dst++ = 0xff; };
199typedef void(*WriteBlackWhiteFunction)( sal_uInt8*& dst, int count );
200void writeBlackA8(sal_uInt8*& dst, int count ) { memset( dst, 0, count ); dst += count; };
201void writeWhiteA8(sal_uInt8*& dst, int count ) { memset( dst, 0xff, count ); dst += count; };
202void writeWhiteRGBA(sal_uInt8*& dst, int count ) { memset( dst, 0xff, count * 4 ); dst += count * 4; };
203void writeBlackRGBA(sal_uInt8*& dst, int count )
204{
205 for( int i = 0; i < count; ++i )
206 {
207 dst[0] = 0x00;
208 dst[1] = 0x00;
209 dst[2] = 0x00;
210 dst[3] = 0xff;
211 dst += 4;
212 }
213};
214
215template< WriteColorFunction func, WriteBlackWhiteFunction funcBlack, WriteBlackWhiteFunction funcWhite >
216void writeBlackWhiteData( const sal_uInt8* src, sal_uInt8* dst, int width, int height, int bytesPerRow )
217{
218 for( int y = 0; y < height; ++y )
219 {
220 const sal_uInt8* srcLine = src;
221 int xsize = width;
222 while( xsize >= 64 )
223 {
224 // TODO alignment?
225 const sal_uInt64* src64 = reinterpret_cast< const sal_uInt64* >( src );
226 if( *src64 == 0x00 )
227 funcBlack( dst, 64 );
228 else if( *src64 == static_cast< sal_uInt64 >( -1 ))
229 funcWhite( dst, 64 );
230 else
231 break;
232 src += sizeof( sal_uInt64 );
233 xsize -= 64;
234 }
235 while( xsize >= 8 )
236 {
237 if( *src == 0x00 ) // => eight black pixels
238 funcBlack( dst, 8 );
239 else if( *src == 0xff ) // => eight white pixels
240 funcWhite( dst, 8 );
241 else
242 for( int bit = 7; bit >= 0; --bit )
243 func(( *src >> bit ) & 1 ? 0xff : 0, dst );
244 ++src;
245 xsize -= 8;
246 }
247 for( int bit = 7; bit > 7 - xsize; --bit )
248 func(( *src >> bit ) & 1 ? 0xff : 0, dst );
249 ++src;
250 src = srcLine + bytesPerRow;
251 }
252}
253
254} // namespace
255
256std::unique_ptr< sal_uInt8[] > SalBitmap::convertDataBitCount( const sal_uInt8* src,
257 int width, int height, int bitCount, int bytesPerRow, const BitmapPalette& palette, BitConvert type )
258{
259 assert( bitCount == 1 || bitCount == 4 || bitCount == 8 );
260 static const o3tl::enumarray<BitConvert, int> bpp = { 1, 4, 4 };
261 std::unique_ptr< sal_uInt8[] > data( new sal_uInt8[width * height * bpp[ type ]] );
262
263 if(type == BitConvert::A8 && bitCount == 8 && palette.IsGreyPalette8Bit())
264 { // no actual data conversion
265 for( int y = 0; y < height; ++y )
266 memcpy( data.get() + y * width, src + y * bytesPerRow, width );
267 return data;
268 }
269
270 if(bitCount == 1 && palette.GetEntryCount() == 2 && palette[ 0 ] == COL_BLACK && palette[ 1 ] == COL_WHITE)
271 {
272 switch( type )
273 {
274 case BitConvert::A8 :
275 writeBlackWhiteData< writeColorA8, writeBlackA8, writeWhiteA8 >
276 ( src, data.get(), width, height, bytesPerRow );
277 return data;
278 case BitConvert::BGRA :
279 case BitConvert::RGBA :
280 // BGRA/RGBA is the same, all 3 values get the same value
281 writeBlackWhiteData< writeColorRGBA, writeBlackRGBA, writeWhiteRGBA >
282 ( src, data.get(), width, height, bytesPerRow );
283 return data;
284 }
285 }
286
287 std::unique_ptr<ImplPixelFormat> pSrcFormat(ImplPixelFormat::GetFormat(bitCount, palette));
288
289 const sal_uInt8* pSrcData = src;
290 sal_uInt8* pDstData = data.get();
291
292 sal_uInt32 nY = height;
293 while( nY-- )
294 {
295 pSrcFormat->StartLine( pSrcData );
296
297 sal_uInt32 nX = width;
298 switch( type )
299 {
300 case BitConvert::A8 :
301 while( nX-- )
302 {
303 const BitmapColor& c = pSrcFormat->ReadPixel();
304 *pDstData++ = c.GetBlue();
305 }
306 break;
307 case BitConvert::BGRA :
308 while( nX-- )
309 {
310 const BitmapColor& c = pSrcFormat->ReadPixel();
311 *pDstData++ = c.GetBlue();
312 *pDstData++ = c.GetGreen();
313 *pDstData++ = c.GetRed();
314 *pDstData++ = 0xff;
315 }
316 break;
317 case BitConvert::RGBA :
318 while( nX-- )
319 {
320 const BitmapColor& c = pSrcFormat->ReadPixel();
321 *pDstData++ = c.GetRed();
322 *pDstData++ = c.GetGreen();
323 *pDstData++ = c.GetBlue();
324 *pDstData++ = 0xff;
325 }
326 break;
327 }
328
329 pSrcData += bytesPerRow;
330 }
331 return data;
332}
333
335{
336 // default has no support, returns nullptr
337 return nullptr;
338}
339
340/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScanlineFormat RemoveScanline(ScanlineFormat nFormat)
Definition: Scanline.hxx:54
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
BitmapChecksum vcl_get_checksum(BitmapChecksum Checksum, const void *Data, sal_uInt32 DatLen)
Definition: checksum.hxx:72
BitmapChecksum GetChecksum() const
sal_uInt16 GetEntryCount() const
bool IsGreyPalette8Bit() const
Returns true if the palette is 8-bit grey palette.
sal_uInt8 GetBlue() const
sal_uInt8 GetRed() const
sal_uInt8 GetGreen() const
BitmapChecksum mnChecksum
Definition: salbmp.hxx:115
void updateChecksum() const
Definition: salbmp.cxx:35
bool mbChecksumValid
Definition: salbmp.hxx:116
virtual void ReleaseBuffer(BitmapBuffer *pBuffer, BitmapAccessMode nMode)=0
static std::unique_ptr< sal_uInt8[] > convertDataBitCount(const sal_uInt8 *src, int width, int height, int bitCount, int bytesPerRow, const BitmapPalette &palette, BitConvert type)
Definition: salbmp.cxx:256
virtual const basegfx::SystemDependentDataHolder * accessSystemDependentDataHolder() const
Definition: salbmp.cxx:334
virtual BitmapBuffer * AcquireBuffer(BitmapAccessMode nMode)=0
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
virtual SotClipboardFormatId GetFormat(const TransferableDataHelper &aHelper) override
float y
int i
long Long
static BitmapChecksum scanlineChecksum(BitmapChecksum nCrc, const sal_uInt8 *bits, int lineBitsCount, sal_uInt8 extraBitsMask)
Definition: salbmp.cxx:23
BitmapPalette maPalette
tools::Long mnWidth
sal_uInt8 * mpBits
tools::Long mnScanlineSize
sal_uInt16 mnBitCount
ScanlineFormat mnFormat
tools::Long mnHeight
unsigned char sal_uInt8
ResultType type
#define bit(name)