Branch libreoffice-5-0-4
[LibreOffice.git] / include / vcl / salbtype.hxx
blob6d8e7e63d5818b24a6cb5dd76b94518c986d1251
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_SALBTYPE_HXX
21 #define INCLUDED_VCL_SALBTYPE_HXX
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <tools/debug.hxx>
26 #include <vcl/salgtype.hxx>
27 #include <tools/color.hxx>
28 #include <tools/helpers.hxx>
29 #include <tools/solar.h>
30 #include <vcl/dllapi.h>
33 // - Memory -
35 typedef sal_uInt8* HPBYTE;
36 typedef HPBYTE Scanline;
37 typedef const sal_uInt8* ConstHPBYTE;
38 typedef ConstHPBYTE ConstScanline;
41 // - Bitmap formats -
43 #define BMP_FORMAT_BOTTOM_UP 0x00000000UL
44 #define BMP_FORMAT_TOP_DOWN 0x80000000UL
46 #define BMP_FORMAT_1BIT_MSB_PAL 0x00000001UL
47 #define BMP_FORMAT_1BIT_LSB_PAL 0x00000002UL
49 #define BMP_FORMAT_4BIT_MSN_PAL 0x00000004UL
50 #define BMP_FORMAT_4BIT_LSN_PAL 0x00000008UL
52 #define BMP_FORMAT_8BIT_PAL 0x00000010UL
53 #define BMP_FORMAT_8BIT_TC_MASK 0x00000020UL
55 #define BMP_FORMAT_24BIT_TC_BGR 0x00000080UL
56 #define BMP_FORMAT_24BIT_TC_RGB 0x00000100UL
57 #define BMP_FORMAT_24BIT_TC_MASK 0x00000200UL
59 #define BMP_FORMAT_32BIT_TC_ABGR 0x00000400UL
60 #define BMP_FORMAT_32BIT_TC_ARGB 0x00000800UL
61 #define BMP_FORMAT_32BIT_TC_BGRA 0x00001000UL
62 #define BMP_FORMAT_32BIT_TC_RGBA 0x00002000UL
63 #define BMP_FORMAT_32BIT_TC_MASK 0x00004000UL
65 #define BMP_FORMAT_16BIT_TC_MSB_MASK 0x00008000UL
66 #define BMP_FORMAT_16BIT_TC_LSB_MASK 0x00010000UL
68 #define BMP_SCANLINE_ADJUSTMENT( Mac_nBmpFormat ) ( (Mac_nBmpFormat) & 0x80000000UL )
69 #define BMP_SCANLINE_FORMAT( Mac_nBmpFormat ) ( (Mac_nBmpFormat) & 0x7FFFFFFFUL )
73 #define MASK_TO_COLOR( d_nVal, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS, d_Col ) \
74 sal_uLong _def_cR = (sal_uInt8) ( d_RS < 0L ? ( (d_nVal) & d_RM ) << -d_RS : ( (d_nVal) & d_RM ) >> d_RS ); \
75 sal_uLong _def_cG = (sal_uInt8) ( d_GS < 0L ? ( (d_nVal) & d_GM ) << -d_GS : ( (d_nVal) & d_GM ) >> d_GS ); \
76 sal_uLong _def_cB = (sal_uInt8) ( d_BS < 0L ? ( (d_nVal) & d_BM ) << -d_BS : ( (d_nVal) & d_BM ) >> d_BS ); \
77 d_Col = BitmapColor( (sal_uInt8) ( _def_cR | ( ( _def_cR & mnROr ) >> mnROrShift ) ), \
78 (sal_uInt8) ( _def_cG | ( ( _def_cG & mnGOr ) >> mnGOrShift ) ), \
79 (sal_uInt8) ( _def_cB | ( ( _def_cB & mnBOr ) >> mnBOrShift ) ) );
83 #define COLOR_TO_MASK( d_rCol, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS, d_ALPHA ) \
84 ( ( ( ( d_RS < 0L ) ? ( (sal_uInt32) (d_rCol).GetRed() >> -d_RS ) : \
85 ( (sal_uInt32) (d_rCol).GetRed() << d_RS ) ) & d_RM ) | \
86 ( ( ( d_GS < 0L ) ? ( (sal_uInt32) (d_rCol).GetGreen() >> -d_GS ) : \
87 ( (sal_uInt32) (d_rCol).GetGreen() << d_GS ) ) & d_GM ) | \
88 ( ( ( d_BS < 0L ) ? ( (sal_uInt32) (d_rCol).GetBlue() >> -d_BS ) : \
89 ( (sal_uInt32) (d_rCol).GetBlue() << d_BS ) ) & d_BM ) | \
90 d_ALPHA )
92 // - BitmapColor -
94 class Color;
96 class VCL_DLLPUBLIC BitmapColor
98 private:
100 // ATTENTION:
101 // Because the members of this class are accessed via memcpy,
102 // you MUST NOT CHANGE the order of the members or the size of this class!
103 sal_uInt8 mcBlueOrIndex;
104 sal_uInt8 mcGreen;
105 sal_uInt8 mcRed;
106 sal_uInt8 mbIndex; // should be bool, but see above warning
108 public:
110 inline BitmapColor();
111 inline BitmapColor( const BitmapColor& rBitmapColor );
112 inline BitmapColor( sal_uInt8 cRed, sal_uInt8 cGreen, sal_uInt8 cBlue );
113 inline BitmapColor( const Color& rColor );
114 explicit inline BitmapColor( sal_uInt8 cIndex );
116 inline ~BitmapColor() {};
118 inline bool operator==( const BitmapColor& rBitmapColor ) const;
119 inline bool operator!=( const BitmapColor& rBitmapColor ) const;
120 inline BitmapColor& operator=( const BitmapColor& rBitmapColor );
122 inline bool IsIndex() const;
124 inline sal_uInt8 GetRed() const;
125 inline void SetRed( sal_uInt8 cRed );
127 inline sal_uInt8 GetGreen() const;
128 inline void SetGreen( sal_uInt8 cGreen );
130 inline sal_uInt8 GetBlue() const;
131 inline void SetBlue( sal_uInt8 cBlue );
133 inline sal_uInt8 GetIndex() const;
134 inline void SetIndex( sal_uInt8 cIndex );
136 operator Color() const;
138 inline sal_uInt8 GetBlueOrIndex() const;
140 inline BitmapColor& Invert();
142 inline sal_uInt8 GetLuminance() const;
143 inline BitmapColor& IncreaseLuminance( sal_uInt8 cGreyInc );
144 inline BitmapColor& DecreaseLuminance( sal_uInt8 cGreyDec );
146 inline BitmapColor& Merge( const BitmapColor& rColor, sal_uInt8 cTransparency );
147 inline BitmapColor& Merge( sal_uInt8 cR, sal_uInt8 cG, sal_uInt8 cB, sal_uInt8 cTransparency );
149 inline sal_uLong GetColorError( const BitmapColor& rBitmapColor ) const;
152 // - BitmapPalette -
153 class Palette;
155 class VCL_DLLPUBLIC BitmapPalette
157 friend class SalBitmap;
158 friend class BitmapAccess;
160 private:
162 BitmapColor* mpBitmapColor;
163 sal_uInt16 mnCount;
165 public:
167 SAL_DLLPRIVATE inline BitmapColor* ImplGetColorBuffer() const;
170 public:
172 inline BitmapPalette();
173 inline BitmapPalette( const BitmapPalette& rBitmapPalette );
174 inline BitmapPalette( sal_uInt16 nCount );
175 inline ~BitmapPalette();
177 inline BitmapPalette& operator=( const BitmapPalette& rBitmapPalette );
178 inline bool operator==( const BitmapPalette& rBitmapPalette ) const;
179 inline bool operator!=( const BitmapPalette& rBitmapPalette ) const;
180 inline bool operator!();
182 inline sal_uInt16 GetEntryCount() const;
183 inline void SetEntryCount( sal_uInt16 nCount );
185 inline const BitmapColor& operator[]( sal_uInt16 nIndex ) const;
186 inline BitmapColor& operator[]( sal_uInt16 nIndex );
188 inline sal_uInt16 GetBestIndex( const BitmapColor& rCol ) const;
189 bool IsGreyPalette() const;
192 // - ColorMask -
193 class VCL_DLLPUBLIC ColorMask
195 sal_uLong mnRMask;
196 sal_uLong mnGMask;
197 sal_uLong mnBMask;
198 long mnRShift;
199 long mnGShift;
200 long mnBShift;
201 sal_uLong mnROrShift;
202 sal_uLong mnGOrShift;
203 sal_uLong mnBOrShift;
204 sal_uLong mnROr;
205 sal_uLong mnGOr;
206 sal_uLong mnBOr;
207 sal_uLong mnAlphaChannel;
209 SAL_DLLPRIVATE inline long ImplCalcMaskShift( sal_uLong nMask, sal_uLong& rOr, sal_uLong& rOrShift ) const;
211 public:
213 inline ColorMask( sal_uLong nRedMask = 0UL,
214 sal_uLong nGreenMask = 0UL,
215 sal_uLong nBlueMask = 0UL,
216 sal_uLong nAlphaChannel = 0UL );
217 inline ~ColorMask() {}
219 inline sal_uLong GetRedMask() const;
220 inline sal_uLong GetGreenMask() const;
221 inline sal_uLong GetBlueMask() const;
223 inline void GetColorFor8Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
224 inline void SetColorFor8Bit( const BitmapColor& rColor, HPBYTE pPixel ) const;
226 inline void GetColorFor16BitMSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
227 inline void SetColorFor16BitMSB( const BitmapColor& rColor, HPBYTE pPixel ) const;
228 inline void GetColorFor16BitLSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
229 inline void SetColorFor16BitLSB( const BitmapColor& rColor, HPBYTE pPixel ) const;
231 inline void GetColorFor24Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
232 inline void SetColorFor24Bit( const BitmapColor& rColor, HPBYTE pPixel ) const;
234 inline void GetColorFor32Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
235 inline void GetColorAndAlphaFor32Bit( BitmapColor& rColor, sal_uInt8& rAlpha, ConstHPBYTE pPixel ) const;
236 inline void SetColorFor32Bit( const BitmapColor& rColor, HPBYTE pPixel ) const;
239 // - BitmapBuffer -
240 struct VCL_DLLPUBLIC BitmapBuffer
242 sal_uLong mnFormat;
243 long mnWidth;
244 long mnHeight;
245 long mnScanlineSize;
246 sal_uInt16 mnBitCount;
247 ColorMask maColorMask;
248 BitmapPalette maPalette;
249 sal_uInt8* mpBits;
251 BitmapBuffer(){}
252 ~BitmapBuffer() {}
255 // - Access modes -
256 typedef enum
258 BITMAP_INFO_ACCESS,
259 BITMAP_READ_ACCESS,
260 BITMAP_WRITE_ACCESS
262 BitmapAccessMode;
264 // - StretchAndConvert -
265 VCL_DLLPUBLIC BitmapBuffer* StretchAndConvert(
266 const BitmapBuffer& rSrcBuffer, const SalTwoRect& rTwoRect,
267 sal_uLong nDstBitmapFormat, const BitmapPalette* pDstPal = NULL, const ColorMask* pDstMask = NULL );
269 inline BitmapColor::BitmapColor() :
270 mcBlueOrIndex ( 0 ),
271 mcGreen ( 0 ),
272 mcRed ( 0 ),
273 mbIndex ( sal_uInt8(false) )
277 inline BitmapColor::BitmapColor( sal_uInt8 cRed, sal_uInt8 cGreen, sal_uInt8 cBlue ) :
278 mcBlueOrIndex ( cBlue ),
279 mcGreen ( cGreen ),
280 mcRed ( cRed ),
281 mbIndex ( sal_uInt8(false) )
285 inline BitmapColor::BitmapColor( const BitmapColor& rBitmapColor ) :
286 mcBlueOrIndex ( rBitmapColor.mcBlueOrIndex ),
287 mcGreen ( rBitmapColor.mcGreen ),
288 mcRed ( rBitmapColor.mcRed ),
289 mbIndex ( rBitmapColor.mbIndex )
293 inline BitmapColor::BitmapColor( const Color& rColor ) :
294 mcBlueOrIndex ( rColor.GetBlue() ),
295 mcGreen ( rColor.GetGreen() ),
296 mcRed ( rColor.GetRed() ),
297 mbIndex ( sal_uInt8(false) )
301 inline BitmapColor::BitmapColor( sal_uInt8 cIndex ) :
302 mcBlueOrIndex ( cIndex ),
303 mcGreen ( 0 ),
304 mcRed ( 0 ),
305 mbIndex ( sal_uInt8(true) )
309 inline bool BitmapColor::operator==( const BitmapColor& rBitmapColor ) const
311 return( ( mcBlueOrIndex == rBitmapColor.mcBlueOrIndex ) &&
312 ( mbIndex ? bool(rBitmapColor.mbIndex) :
313 ( mcGreen == rBitmapColor.mcGreen && mcRed == rBitmapColor.mcRed ) ) );
316 inline bool BitmapColor::operator!=( const BitmapColor& rBitmapColor ) const
318 return !( *this == rBitmapColor );
321 inline BitmapColor& BitmapColor::operator=( const BitmapColor& rBitmapColor )
323 mcBlueOrIndex = rBitmapColor.mcBlueOrIndex;
324 mcGreen = rBitmapColor.mcGreen;
325 mcRed = rBitmapColor.mcRed;
326 mbIndex = rBitmapColor.mbIndex;
328 return *this;
331 inline bool BitmapColor::IsIndex() const
333 return mbIndex;
336 inline sal_uInt8 BitmapColor::GetRed() const
338 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
339 return mcRed;
342 inline void BitmapColor::SetRed( sal_uInt8 cRed )
344 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
345 mcRed = cRed;
348 inline sal_uInt8 BitmapColor::GetGreen() const
350 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
351 return mcGreen;
354 inline void BitmapColor::SetGreen( sal_uInt8 cGreen )
356 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
357 mcGreen = cGreen;
360 inline sal_uInt8 BitmapColor::GetBlue() const
362 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
363 return mcBlueOrIndex;
366 inline void BitmapColor::SetBlue( sal_uInt8 cBlue )
368 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
369 mcBlueOrIndex = cBlue;
372 inline sal_uInt8 BitmapColor::GetIndex() const
374 DBG_ASSERT( mbIndex, "Pixel represents color values!" );
375 return mcBlueOrIndex;
378 inline void BitmapColor::SetIndex( sal_uInt8 cIndex )
380 DBG_ASSERT( mbIndex, "Pixel represents color values!" );
381 mcBlueOrIndex = cIndex;
384 inline BitmapColor::operator Color() const
386 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
387 return Color( mcRed, mcGreen, mcBlueOrIndex );
390 inline sal_uInt8 BitmapColor::GetBlueOrIndex() const
392 // #i47518# Yield a value regardless of mbIndex
393 return mcBlueOrIndex;
396 inline BitmapColor& BitmapColor::Invert()
398 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
399 mcBlueOrIndex = ~mcBlueOrIndex, mcGreen = ~mcGreen, mcRed = ~mcRed;
401 return *this;
404 inline sal_uInt8 BitmapColor::GetLuminance() const
406 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
407 return (static_cast<unsigned long>(mcBlueOrIndex) * 28UL + static_cast<unsigned long>(mcGreen) * 151UL + static_cast<unsigned long>(mcRed) * 77UL) >> 8;
410 inline BitmapColor& BitmapColor::IncreaseLuminance( sal_uInt8 cGreyInc )
412 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
413 mcBlueOrIndex = (sal_uInt8) MinMax( (long) mcBlueOrIndex + cGreyInc, 0L, 255L );
414 mcGreen = (sal_uInt8) MinMax( (long) mcGreen + cGreyInc, 0L, 255L );
415 mcRed = (sal_uInt8) MinMax( (long) mcRed + cGreyInc, 0L, 255L );
417 return *this;
420 inline BitmapColor& BitmapColor::DecreaseLuminance( sal_uInt8 cGreyDec )
422 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
423 mcBlueOrIndex = (sal_uInt8) MinMax( (long) mcBlueOrIndex - cGreyDec, 0L, 255L );
424 mcGreen = (sal_uInt8) MinMax( (long) mcGreen - cGreyDec, 0L, 255L );
425 mcRed = (sal_uInt8) MinMax( (long) mcRed - cGreyDec, 0L, 255L );
427 return *this;
430 inline BitmapColor& BitmapColor::Merge( const BitmapColor& rBitmapColor, sal_uInt8 cTransparency )
432 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
433 DBG_ASSERT( !rBitmapColor.mbIndex, "Pixel represents index into colortable!" );
434 mcBlueOrIndex = COLOR_CHANNEL_MERGE( mcBlueOrIndex, rBitmapColor.mcBlueOrIndex, cTransparency );
435 mcGreen = COLOR_CHANNEL_MERGE( mcGreen, rBitmapColor.mcGreen, cTransparency );
436 mcRed = COLOR_CHANNEL_MERGE( mcRed, rBitmapColor.mcRed, cTransparency );
438 return *this;
441 inline BitmapColor& BitmapColor::Merge( sal_uInt8 cR, sal_uInt8 cG, sal_uInt8 cB, sal_uInt8 cTransparency )
443 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
444 mcBlueOrIndex = COLOR_CHANNEL_MERGE( mcBlueOrIndex, cB, cTransparency );
445 mcGreen = COLOR_CHANNEL_MERGE( mcGreen, cG, cTransparency );
446 mcRed = COLOR_CHANNEL_MERGE( mcRed, cR, cTransparency );
448 return *this;
451 inline sal_uLong BitmapColor::GetColorError( const BitmapColor& rBitmapColor ) const
453 DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
454 DBG_ASSERT( !rBitmapColor.mbIndex, "Pixel represents index into colortable!" );
455 return( (sal_uLong) ( labs( mcBlueOrIndex - rBitmapColor.mcBlueOrIndex ) +
456 labs( mcGreen - rBitmapColor.mcGreen ) +
457 labs( mcRed - rBitmapColor.mcRed ) ) );
460 inline BitmapPalette::BitmapPalette() :
461 mpBitmapColor ( NULL ),
462 mnCount ( 0 )
466 inline BitmapPalette::BitmapPalette( const BitmapPalette& rBitmapPalette ) :
467 mnCount( rBitmapPalette.mnCount )
469 if( mnCount )
471 const sal_uLong nSize = mnCount * sizeof( BitmapColor );
472 mpBitmapColor = reinterpret_cast<BitmapColor*>(new sal_uInt8[ nSize ]);
473 memcpy( mpBitmapColor, rBitmapPalette.mpBitmapColor, nSize );
475 else
476 mpBitmapColor = NULL;
479 inline BitmapPalette::BitmapPalette( sal_uInt16 nCount ) :
480 mnCount( nCount )
482 if( mnCount )
484 const sal_uLong nSize = mnCount * sizeof( BitmapColor );
485 mpBitmapColor = reinterpret_cast<BitmapColor*>(new sal_uInt8[ nSize ]);
486 memset( mpBitmapColor, 0, nSize );
488 else
489 mpBitmapColor = NULL;
492 inline BitmapPalette::~BitmapPalette()
494 delete[] reinterpret_cast<sal_uInt8*>(mpBitmapColor);
497 inline BitmapPalette& BitmapPalette::operator=( const BitmapPalette& rBitmapPalette )
499 delete[] reinterpret_cast<sal_uInt8*>(mpBitmapColor);
500 mnCount = rBitmapPalette.mnCount;
502 if( mnCount )
504 const sal_uLong nSize = mnCount * sizeof( BitmapColor );
505 mpBitmapColor = reinterpret_cast<BitmapColor*>(new sal_uInt8[ nSize ]);
506 memcpy( mpBitmapColor, rBitmapPalette.mpBitmapColor, nSize );
508 else
509 mpBitmapColor = NULL;
511 return *this;
514 inline bool BitmapPalette::operator==( const BitmapPalette& rBitmapPalette ) const
516 bool bRet = false;
518 if( rBitmapPalette.mnCount == mnCount )
520 bRet = true;
522 for( sal_uInt16 i = 0; i < mnCount; i++ )
524 if( mpBitmapColor[ i ] != rBitmapPalette.mpBitmapColor[ i ] )
526 bRet = false;
527 break;
532 return bRet;
535 inline bool BitmapPalette::operator!=( const BitmapPalette& rBitmapPalette ) const
537 return !( *this == rBitmapPalette );
540 inline bool BitmapPalette::operator!()
542 return( !mnCount || !mpBitmapColor );
545 inline sal_uInt16 BitmapPalette::GetEntryCount() const
547 return mnCount;
550 inline void BitmapPalette::SetEntryCount( sal_uInt16 nCount )
552 if( !nCount )
554 delete[] reinterpret_cast<sal_uInt8*>(mpBitmapColor);
555 mpBitmapColor = NULL;
556 mnCount = 0;
558 else if( nCount != mnCount )
560 const sal_uLong nNewSize = nCount * sizeof( BitmapColor );
561 const sal_uLong nMinSize = std::min( mnCount, nCount ) * sizeof( BitmapColor );
562 sal_uInt8* pNewColor = new sal_uInt8[ nNewSize ];
564 if ( nMinSize && mpBitmapColor )
565 memcpy( pNewColor, mpBitmapColor, nMinSize );
566 delete[] reinterpret_cast<sal_uInt8*>(mpBitmapColor);
567 memset( pNewColor + nMinSize, 0, nNewSize - nMinSize );
568 mpBitmapColor = reinterpret_cast<BitmapColor*>(pNewColor);
569 mnCount = nCount;
573 inline const BitmapColor& BitmapPalette::operator[]( sal_uInt16 nIndex ) const
575 DBG_ASSERT( nIndex < mnCount, "Palette index is out of range!" );
576 return mpBitmapColor[ nIndex ];
579 inline BitmapColor& BitmapPalette::operator[]( sal_uInt16 nIndex )
581 DBG_ASSERT( nIndex < mnCount, "Palette index is out of range!" );
582 return mpBitmapColor[ nIndex ];
585 inline BitmapColor* BitmapPalette::ImplGetColorBuffer() const
587 DBG_ASSERT( mpBitmapColor, "No color buffer available!" );
588 return mpBitmapColor;
591 inline sal_uInt16 BitmapPalette::GetBestIndex( const BitmapColor& rCol ) const
593 sal_uInt16 nRetIndex = 0;
595 if( mpBitmapColor && mnCount )
597 bool bFound = false;
599 for( long j = 0L; ( j < mnCount ) && !bFound; j++ )
600 if( rCol == mpBitmapColor[ j ] )
601 nRetIndex = ( (sal_uInt16) j ), bFound = true;
603 if( !bFound )
605 long nActErr, nLastErr = rCol.GetColorError( mpBitmapColor[ nRetIndex = mnCount - 1 ] );
607 for( long i = nRetIndex - 1; i >= 0L; i-- )
608 if ( ( nActErr = rCol.GetColorError( mpBitmapColor[ i ] ) ) < nLastErr )
609 nLastErr = nActErr, nRetIndex = (sal_uInt16) i;
613 return nRetIndex;
616 inline ColorMask::ColorMask( sal_uLong nRedMask,
617 sal_uLong nGreenMask,
618 sal_uLong nBlueMask,
619 sal_uLong nAlphaChannel ) :
620 mnRMask( nRedMask ),
621 mnGMask( nGreenMask ),
622 mnBMask( nBlueMask ),
623 mnROrShift( 0L ),
624 mnGOrShift( 0L ),
625 mnBOrShift( 0L ),
626 mnROr( 0L ),
627 mnGOr( 0L ),
628 mnBOr( 0L ),
629 mnAlphaChannel( nAlphaChannel )
631 mnRShift = ( mnRMask ? ImplCalcMaskShift( mnRMask, mnROr, mnROrShift ) : 0L );
632 mnGShift = ( mnGMask ? ImplCalcMaskShift( mnGMask, mnGOr, mnGOrShift ) : 0L );
633 mnBShift = ( mnBMask ? ImplCalcMaskShift( mnBMask, mnBOr, mnBOrShift ) : 0L );
636 inline long ColorMask::ImplCalcMaskShift( sal_uLong nMask, sal_uLong& rOr, sal_uLong& rOrShift ) const
638 long nShift;
639 long nRet;
640 sal_uLong nLen = 0UL;
642 // from which bit starts the mask?
643 for( nShift = 31L; ( nShift >= 0L ) && !( nMask & ( (sal_uLong)1 << (sal_uLong) nShift ) ); nShift-- )
646 nRet = nShift;
648 // XXX determine number of bits set => walk right until null
649 while( ( nShift >= 0L ) && ( nMask & ( (sal_uLong)1 << (sal_uLong) nShift ) ) )
651 nShift--;
652 nLen++;
655 assert( nLen <= 8 ); // mask length must be 8 bits or less
656 rOrShift = 8UL - nLen;
657 rOr = (sal_uInt8) ( ( 0xffUL >> nLen ) << rOrShift );
659 return( nRet -= 7 );
662 inline sal_uLong ColorMask::GetRedMask() const
664 return mnRMask;
667 inline sal_uLong ColorMask::GetGreenMask() const
669 return mnGMask;
672 inline sal_uLong ColorMask::GetBlueMask() const
674 return mnBMask;
677 inline void ColorMask::GetColorFor8Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const
679 const sal_uInt32 nVal = *pPixel;
680 MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
683 inline void ColorMask::SetColorFor8Bit( const BitmapColor& rColor, HPBYTE pPixel ) const
685 *pPixel = (sal_uInt8) COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, mnAlphaChannel );
688 inline void ColorMask::GetColorFor16BitMSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const
690 const sal_uInt32 nVal = pPixel[ 1 ] | ( (sal_uInt32) pPixel[ 0 ] << 8UL );
692 MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
695 inline void ColorMask::SetColorFor16BitMSB( const BitmapColor& rColor, HPBYTE pPixel ) const
697 const sal_uInt16 nVal = (sal_uInt16)COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, mnAlphaChannel );
699 pPixel[ 0 ] = (sal_uInt8)(nVal >> 8U);
700 pPixel[ 1 ] = (sal_uInt8) nVal;
703 inline void ColorMask::GetColorFor16BitLSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const
705 const sal_uInt32 nVal = pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL );
707 MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
710 inline void ColorMask::SetColorFor16BitLSB( const BitmapColor& rColor, HPBYTE pPixel ) const
712 const sal_uInt16 nVal = (sal_uInt16)COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, mnAlphaChannel );
714 pPixel[ 0 ] = (sal_uInt8) nVal;
715 pPixel[ 1 ] = (sal_uInt8)(nVal >> 8U);
718 inline void ColorMask::GetColorFor24Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const
720 const sal_uInt32 nVal = pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL ) | ( (sal_uInt32) pPixel[ 2 ] << 16UL );
721 MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
724 inline void ColorMask::SetColorFor24Bit( const BitmapColor& rColor, HPBYTE pPixel ) const
726 const sal_uInt32 nVal = COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, mnAlphaChannel );
727 pPixel[ 0 ] = (sal_uInt8) nVal; pPixel[ 1 ] = (sal_uInt8) ( nVal >> 8UL ); pPixel[ 2 ] = (sal_uInt8) ( nVal >> 16UL );
730 inline void ColorMask::GetColorFor32Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const
732 const sal_uInt32 nVal = (sal_uInt32) pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL ) |
733 ( (sal_uInt32) pPixel[ 2 ] << 16UL ) | ( (sal_uInt32) pPixel[ 3 ] << 24UL );
735 MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
738 inline void ColorMask::GetColorAndAlphaFor32Bit( BitmapColor& rColor, sal_uInt8& rAlpha, ConstHPBYTE pPixel ) const
740 const sal_uInt32 nVal = (sal_uInt32) pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL ) |
741 ( (sal_uInt32) pPixel[ 2 ] << 16UL ) | ( (sal_uInt32) pPixel[ 3 ] << 24UL );
742 rAlpha = (sal_uInt8)(nVal >> 24);
744 MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
747 inline void ColorMask::SetColorFor32Bit( const BitmapColor& rColor, HPBYTE pPixel ) const
749 const sal_uInt32 nVal = COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, mnAlphaChannel );
750 pPixel[ 0 ] = (sal_uInt8) nVal; pPixel[ 1 ] = (sal_uInt8) ( nVal >> 8UL );
751 pPixel[ 2 ] = (sal_uInt8) ( nVal >> 16UL ); pPixel[ 3 ] = (sal_uInt8) ( nVal >> 24UL );
754 #endif // INCLUDED_VCL_SALBTYPE_HXX
756 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */