xref: /trunk/main/vcl/inc/vcl/salbtype.hxx (revision 98a781ec)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SV_SALBTYPE_HXX
25 #define _SV_SALBTYPE_HXX
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <osl/endian.h>
30 #include <tools/debug.hxx>
31 #include <vcl/salgtype.hxx>
32 #include <tools/color.hxx>
33 #include <tools/gen.hxx>
34 #include <vcl/sv.h>
35 #include <vcl/dllapi.h>
36 
37 // ----------
38 // - Memory -
39 // ----------
40 
41 typedef sal_uInt8*		HPBYTE;
42 typedef HPBYTE		Scanline;
43 typedef const sal_uInt8*	ConstHPBYTE;
44 typedef ConstHPBYTE	ConstScanline;
45 
46 // ------------------
47 // - Bitmap formats -
48 // ------------------
49 
50 #define BMP_FORMAT_BOTTOM_UP						0x00000000UL
51 #define BMP_FORMAT_TOP_DOWN 						0x80000000UL
52 
53 #define BMP_FORMAT_1BIT_MSB_PAL 					0x00000001UL
54 #define BMP_FORMAT_1BIT_LSB_PAL 					0x00000002UL
55 
56 #define BMP_FORMAT_4BIT_MSN_PAL 					0x00000004UL
57 #define BMP_FORMAT_4BIT_LSN_PAL 					0x00000008UL
58 
59 #define BMP_FORMAT_8BIT_PAL 						0x00000010UL
60 #define BMP_FORMAT_8BIT_TC_MASK 					0x00000020UL
61 
62 // #define BMP_FORMAT_16BIT_TC_MASK					0x00000040UL
63 
64 #define BMP_FORMAT_24BIT_TC_BGR 					0x00000080UL
65 #define BMP_FORMAT_24BIT_TC_RGB 					0x00000100UL
66 #define BMP_FORMAT_24BIT_TC_MASK					0x00000200UL
67 
68 #define BMP_FORMAT_32BIT_TC_ABGR					0x00000400UL
69 #define BMP_FORMAT_32BIT_TC_ARGB					0x00000800UL
70 #define BMP_FORMAT_32BIT_TC_BGRA					0x00001000UL
71 #define BMP_FORMAT_32BIT_TC_RGBA					0x00002000UL
72 #define BMP_FORMAT_32BIT_TC_MASK					0x00004000UL
73 
74 #define BMP_FORMAT_16BIT_TC_MSB_MASK				0x00008000UL
75 #define BMP_FORMAT_16BIT_TC_LSB_MASK				0x00010000UL
76 
77 #define BMP_SCANLINE_ADJUSTMENT( Mac_nBmpFormat )	( (Mac_nBmpFormat) & 0x80000000UL )
78 #define BMP_SCANLINE_FORMAT( Mac_nBmpFormat )		( (Mac_nBmpFormat) & 0x7FFFFFFFUL )
79 
80 // ------------------------------------------------------------------
81 
82 #define MASK_TO_COLOR( d_nVal, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS, d_Col )							\
83 sal_uLong _def_cR = (sal_uInt8) ( d_RS < 0L ? ( (d_nVal) & d_RM ) << -d_RS : ( (d_nVal) & d_RM ) >> d_RS );	\
84 sal_uLong _def_cG = (sal_uInt8) ( d_GS < 0L ? ( (d_nVal) & d_GM ) << -d_GS : ( (d_nVal) & d_GM ) >> d_GS );	\
85 sal_uLong _def_cB = (sal_uInt8) ( d_BS < 0L ? ( (d_nVal) & d_BM ) << -d_BS : ( (d_nVal) & d_BM ) >> d_BS );	\
86 d_Col = BitmapColor( (sal_uInt8) ( _def_cR | ( ( _def_cR & mnROr ) >> mnROrShift ) ),					\
87 					 (sal_uInt8) ( _def_cG | ( ( _def_cG & mnGOr ) >> mnGOrShift ) ),					\
88 					 (sal_uInt8) ( _def_cB | ( ( _def_cB & mnBOr ) >> mnBOrShift ) ) );
89 
90 // ------------------------------------------------------------------
91 
92 #define COLOR_TO_MASK( d_rCol, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS ) \
93 ( ( ( ( d_RS < 0L ) ? ( (sal_uInt32) (d_rCol).GetRed() >> -d_RS ) : 	\
94 	( (sal_uInt32) (d_rCol).GetRed() << d_RS ) ) & d_RM ) | 			\
95   ( ( ( d_GS < 0L ) ? ( (sal_uInt32) (d_rCol).GetGreen() >> -d_GS ) :	\
96 	( (sal_uInt32) (d_rCol).GetGreen() << d_GS ) ) & d_GM ) |			\
97   ( ( ( d_BS < 0L ) ? ( (sal_uInt32) (d_rCol).GetBlue() >> -d_BS ) :	\
98 	( (sal_uInt32) (d_rCol).GetBlue() << d_BS ) ) & d_BM ) )
99 
100 // ---------------
101 // - BitmapColor -
102 // ---------------
103 
104 class Color;
105 
106 class VCL_DLLPUBLIC BitmapColor
107 {
108 private:
109 
110 // !!! Achtung:
111 // !!! da auf die Member dieser Klasse via memcpy
112 // !!! zugegriffen wird, darf diese Klasse weder
113 // !!! in der Groesse noch der Reihenfolge der
114 // !!! Member veraendert werden (KA 02.09.97)
115 	sal_uInt8				mcBlueOrIndex;
116 	sal_uInt8				mcGreen;
117 	sal_uInt8				mcRed;
118 	sal_uInt8				mbIndex;
119 
120 public:
121 
122 	inline				BitmapColor();
123 	inline				BitmapColor( const BitmapColor& rBitmapColor );
124 	inline				BitmapColor( sal_uInt8 cRed, sal_uInt8 cGreen, sal_uInt8 cBlue );
125 	inline				BitmapColor( const Color& rColor );
126 	explicit
127 	inline				BitmapColor( sal_uInt8 cIndex );
128 
~BitmapColor()129 	inline				~BitmapColor() {};
130 
131 	inline sal_Bool 		operator==( const BitmapColor& rBitmapColor ) const;
132 	inline sal_Bool 		operator!=( const BitmapColor& rBitmapColor ) const;
133 	inline BitmapColor& operator=( const BitmapColor& rBitmapColor );
134 
135 	inline sal_Bool 		IsIndex() const;
136 
137 	inline sal_uInt8 		GetRed() const;
138 	inline void 		SetRed( sal_uInt8 cRed );
139 
140 	inline sal_uInt8 		GetGreen() const;
141 	inline void 		SetGreen( sal_uInt8 cGreen );
142 
143 	inline sal_uInt8 		GetBlue() const;
144 	inline void 		SetBlue( sal_uInt8 cBlue );
145 
146 	inline sal_uInt8 		GetIndex() const;
147 	inline void 		SetIndex( sal_uInt8 cIndex );
148 
149 #ifdef BINFILTER_COMPAT
operator sal_uInt8() const150 	operator sal_uInt8() const { return mcBlueOrIndex; }
151 #endif // BINFILTER_COMPAT
152 	operator			Color() const;
153 
154     inline sal_uInt8         GetBlueOrIndex() const;
155 
156 	inline BitmapColor& Invert();
157 
158 	inline sal_uInt8 		GetLuminance() const;
159 	inline BitmapColor& IncreaseLuminance( sal_uInt8 cGreyInc );
160 	inline BitmapColor& DecreaseLuminance( sal_uInt8 cGreyDec );
161 
162 	inline BitmapColor&	Merge( const BitmapColor& rColor, sal_uInt8 cTransparency );
163 	inline BitmapColor&	Merge( sal_uInt8 cR, sal_uInt8 cG, sal_uInt8 cB, sal_uInt8 cTransparency );
164 
165 	inline sal_uLong		GetColorError( const BitmapColor& rBitmapColor ) const;
166 };
167 
168 // ---------------
169 // - BitmapPalette -
170 // ---------------
171 
172 class Palette;
173 
174 class VCL_DLLPUBLIC BitmapPalette
175 {
176 	friend class SalBitmap;
177 	friend class BitmapAccess;
178 
179 private:
180 
181 	BitmapColor*				mpBitmapColor;
182 	sal_uInt16						mnCount;
183 
184 //#if 0 // _SOLAR__PRIVATE
185 
186 public:
187 
188 	SAL_DLLPRIVATE inline BitmapColor* ImplGetColorBuffer() const;
189 
190 //#endif // __PRIVATE
191 
192 public:
193 
194 	inline						BitmapPalette();
195 	inline						BitmapPalette( const BitmapPalette& rBitmapPalette );
196 	inline						BitmapPalette( sal_uInt16 nCount );
197 	inline						~BitmapPalette();
198 
199 	inline BitmapPalette&		operator=( const BitmapPalette& rBitmapPalette );
200 	inline sal_Bool 				operator==( const BitmapPalette& rBitmapPalette ) const;
201 	inline sal_Bool 				operator!=( const BitmapPalette& rBitmapPalette ) const;
202 	inline sal_Bool 				operator!();
203 
204 	inline sal_uInt16				GetEntryCount() const;
205 	inline void 				SetEntryCount( sal_uInt16 nCount );
206 
207 	inline const BitmapColor&	operator[]( sal_uInt16 nIndex ) const;
208 	inline BitmapColor& 		operator[]( sal_uInt16 nIndex );
209 
210 	inline sal_uInt16				GetBestIndex( const BitmapColor& rCol ) const;
211 	bool						IsGreyPalette() const;
212 };
213 
214 // ---------------
215 // - ColorMask -
216 // ---------------
217 
218 class VCL_DLLPUBLIC ColorMask
219 {
220 	sal_uLong				mnRMask;
221 	sal_uLong				mnGMask;
222 	sal_uLong				mnBMask;
223 	long				mnRShift;
224 	long				mnGShift;
225 	long				mnBShift;
226 	sal_uLong				mnROrShift;
227 	sal_uLong				mnGOrShift;
228 	sal_uLong				mnBOrShift;
229 	sal_uLong				mnROr;
230 	sal_uLong				mnGOr;
231 	sal_uLong				mnBOr;
232 
233 	SAL_DLLPRIVATE inline long ImplCalcMaskShift( sal_uLong nMask, sal_uLong& rOr, sal_uLong& rOrShift ) const;
234 
235 public:
236 
237 	inline				ColorMask( sal_uLong nRedMask = 0UL, sal_uLong nGreenMask = 0UL, sal_uLong nBlueMask = 0UL );
~ColorMask()238 	inline				~ColorMask() {}
239 
240 	inline sal_uLong		GetRedMask() const;
241 	inline sal_uLong		GetGreenMask() const;
242 	inline sal_uLong		GetBlueMask() const;
243 
244 	inline void 		GetColorFor8Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
245 	inline void 		SetColorFor8Bit( const BitmapColor& rColor, HPBYTE pPixel ) const;
246 
247 	inline void 		GetColorFor16BitMSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
248 	inline void 		SetColorFor16BitMSB( const BitmapColor& rColor, HPBYTE pPixel ) const;
249 	inline void 		GetColorFor16BitLSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
250 	inline void 		SetColorFor16BitLSB( const BitmapColor& rColor, HPBYTE pPixel ) const;
251 
252 	inline void 		GetColorFor24Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
253 	inline void 		SetColorFor24Bit( const BitmapColor& rColor, HPBYTE pPixel ) const;
254 
255 	inline void 		GetColorFor32Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const;
256 	inline void 		GetColorAndAlphaFor32Bit( BitmapColor& rColor, sal_uInt8& rAlpha, ConstHPBYTE pPixel ) const;
257 	inline void 		SetColorFor32Bit( const BitmapColor& rColor, HPBYTE pPixel ) const;
258 };
259 
260 // ---------------
261 // - BitmapBuffer -
262 // ---------------
263 
264 struct VCL_DLLPUBLIC BitmapBuffer
265 {
266 	sal_uLong			mnFormat;
267 	long			mnWidth;
268 	long			mnHeight;
269 	long			mnScanlineSize;
270 	sal_uInt16			mnBitCount;
271 	ColorMask		maColorMask;
272 	BitmapPalette	maPalette;
273 	sal_uInt8*			mpBits;
274 
BitmapBufferBitmapBuffer275 					BitmapBuffer(){}
~BitmapBufferBitmapBuffer276 					~BitmapBuffer() {}
277 };
278 
279 // ---------------------
280 // - StretchAndConvert -
281 // ---------------------
282 
283 VCL_DLLPUBLIC BitmapBuffer* StretchAndConvert(
284     const BitmapBuffer& rSrcBuffer, const SalTwoRect& rTwoRect,
285     sal_uLong nDstBitmapFormat, const BitmapPalette* pDstPal = NULL, const ColorMask* pDstMask = NULL );
286 
287 // ------------------------------------------------------------------
288 
BitmapColor()289 inline BitmapColor::BitmapColor() :
290 			mcBlueOrIndex	( 0 ),
291 			mcGreen 		( 0 ),
292 			mcRed			( 0 ),
293 			mbIndex 		( sal_False )
294 {
295 }
296 
297 // ------------------------------------------------------------------
298 
BitmapColor(sal_uInt8 cRed,sal_uInt8 cGreen,sal_uInt8 cBlue)299 inline BitmapColor::BitmapColor( sal_uInt8 cRed, sal_uInt8 cGreen, sal_uInt8 cBlue ) :
300 			mcBlueOrIndex	( cBlue ),
301 			mcGreen 		( cGreen ),
302 			mcRed			( cRed ),
303 			mbIndex 		( sal_False )
304 {
305 }
306 
307 // ------------------------------------------------------------------
308 
BitmapColor(const BitmapColor & rBitmapColor)309 inline BitmapColor::BitmapColor( const BitmapColor& rBitmapColor ) :
310 			mcBlueOrIndex	( rBitmapColor.mcBlueOrIndex ),
311 			mcGreen 		( rBitmapColor.mcGreen ),
312 			mcRed			( rBitmapColor.mcRed ),
313 			mbIndex 		( rBitmapColor.mbIndex )
314 {
315 }
316 
317 // ------------------------------------------------------------------
318 
BitmapColor(const Color & rColor)319 inline BitmapColor::BitmapColor( const Color& rColor ) :
320 			mcBlueOrIndex	( rColor.GetBlue() ),
321 			mcGreen 		( rColor.GetGreen() ),
322 			mcRed			( rColor.GetRed() ),
323 			mbIndex 		( 0 )
324 {
325 }
326 
327 // ------------------------------------------------------------------
328 
BitmapColor(sal_uInt8 cIndex)329 inline BitmapColor::BitmapColor( sal_uInt8 cIndex ) :
330 			mcBlueOrIndex	( cIndex ),
331 			mcGreen 		( 0 ),
332 			mcRed			( 0 ),
333 			mbIndex 		( sal_True )
334 {
335 }
336 
337 // ------------------------------------------------------------------
338 
operator ==(const BitmapColor & rBitmapColor) const339 inline sal_Bool BitmapColor::operator==( const BitmapColor& rBitmapColor ) const
340 {
341 	return( ( mcBlueOrIndex == rBitmapColor.mcBlueOrIndex ) &&
342 			( mbIndex ? rBitmapColor.mbIndex :
343 			( mcGreen == rBitmapColor.mcGreen && mcRed == rBitmapColor.mcRed ) ) );
344 }
345 
346 // ------------------------------------------------------------------
347 
operator !=(const BitmapColor & rBitmapColor) const348 inline sal_Bool BitmapColor::operator!=( const BitmapColor& rBitmapColor ) const
349 {
350 	return !( *this == rBitmapColor );
351 }
352 
353 // ------------------------------------------------------------------
354 
operator =(const BitmapColor & rBitmapColor)355 inline BitmapColor& BitmapColor::operator=( const BitmapColor& rBitmapColor )
356 {
357 	mcBlueOrIndex = rBitmapColor.mcBlueOrIndex;
358 	mcGreen = rBitmapColor.mcGreen;
359 	mcRed = rBitmapColor.mcRed;
360 	mbIndex = rBitmapColor.mbIndex;
361 
362 	return *this;
363 }
364 
365 // ------------------------------------------------------------------
366 
IsIndex() const367 inline sal_Bool BitmapColor::IsIndex() const
368 {
369 	return mbIndex;
370 }
371 
372 // ------------------------------------------------------------------
373 
GetRed() const374 inline sal_uInt8 BitmapColor::GetRed() const
375 {
376 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
377 	return mcRed;
378 }
379 
380 // ------------------------------------------------------------------
381 
SetRed(sal_uInt8 cRed)382 inline void BitmapColor::SetRed( sal_uInt8 cRed )
383 {
384 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
385 	mcRed = cRed;
386 }
387 
388 // ------------------------------------------------------------------
389 
GetGreen() const390 inline sal_uInt8 BitmapColor::GetGreen() const
391 {
392 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
393 	return mcGreen;
394 }
395 
396 // ------------------------------------------------------------------
397 
SetGreen(sal_uInt8 cGreen)398 inline void BitmapColor::SetGreen( sal_uInt8 cGreen )
399 {
400 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
401 	mcGreen = cGreen;
402 }
403 
404 // ------------------------------------------------------------------
405 
GetBlue() const406 inline sal_uInt8 BitmapColor::GetBlue() const
407 {
408 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
409 	return mcBlueOrIndex;
410 }
411 
412 // ------------------------------------------------------------------
413 
SetBlue(sal_uInt8 cBlue)414 inline void BitmapColor::SetBlue( sal_uInt8 cBlue )
415 {
416 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
417 	mcBlueOrIndex = cBlue;
418 }
419 
420 // ------------------------------------------------------------------
421 
GetIndex() const422 inline sal_uInt8 BitmapColor::GetIndex() const
423 {
424 	DBG_ASSERT( mbIndex, "Pixel represents color values!" );
425 	return mcBlueOrIndex;
426 }
427 
428 // ------------------------------------------------------------------
429 
SetIndex(sal_uInt8 cIndex)430 inline void BitmapColor::SetIndex( sal_uInt8 cIndex )
431 {
432 	DBG_ASSERT( mbIndex, "Pixel represents color values!" );
433 	mcBlueOrIndex = cIndex;
434 }
435 
436 // ------------------------------------------------------------------
437 
operator Color() const438 inline BitmapColor::operator Color() const
439 {
440 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
441 	return Color( mcRed, mcGreen, mcBlueOrIndex );
442 }
443 
444 // ------------------------------------------------------------------
445 
GetBlueOrIndex() const446 inline sal_uInt8 BitmapColor::GetBlueOrIndex() const
447 {
448     // #i47518# Yield a value regardless of mbIndex
449 	return mcBlueOrIndex;
450 }
451 
452 // ------------------------------------------------------------------
453 
Invert()454 inline BitmapColor& BitmapColor::Invert()
455 {
456 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
457 	mcBlueOrIndex = ~mcBlueOrIndex, mcGreen = ~mcGreen, mcRed = ~mcRed;
458 
459 	return *this;
460 }
461 
462 // ------------------------------------------------------------------
463 
GetLuminance() const464 inline sal_uInt8 BitmapColor::GetLuminance() const
465 {
466 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
467 	return( (sal_uInt8) ( ( mcBlueOrIndex * 28UL + mcGreen * 151UL + mcRed * 77UL ) >> 8UL ) );
468 }
469 
470 // ------------------------------------------------------------------
471 
IncreaseLuminance(sal_uInt8 cGreyInc)472 inline BitmapColor& BitmapColor::IncreaseLuminance( sal_uInt8 cGreyInc )
473 {
474 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
475 	mcBlueOrIndex = (sal_uInt8) MinMax( (long) mcBlueOrIndex + cGreyInc, 0L, 255L );
476 	mcGreen = (sal_uInt8) MinMax( (long) mcGreen + cGreyInc, 0L, 255L );
477 	mcRed = (sal_uInt8) MinMax( (long) mcRed + cGreyInc, 0L, 255L );
478 
479 	return *this;
480 }
481 
482 // ------------------------------------------------------------------
483 
DecreaseLuminance(sal_uInt8 cGreyDec)484 inline BitmapColor& BitmapColor::DecreaseLuminance( sal_uInt8 cGreyDec )
485 {
486 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
487 	mcBlueOrIndex = (sal_uInt8) MinMax( (long) mcBlueOrIndex - cGreyDec, 0L, 255L );
488 	mcGreen = (sal_uInt8) MinMax( (long) mcGreen - cGreyDec, 0L, 255L );
489 	mcRed = (sal_uInt8) MinMax( (long) mcRed - cGreyDec, 0L, 255L );
490 
491 	return *this;
492 }
493 
494 // ------------------------------------------------------------------
495 
Merge(const BitmapColor & rBitmapColor,sal_uInt8 cTransparency)496 inline BitmapColor&	BitmapColor::Merge( const BitmapColor& rBitmapColor, sal_uInt8 cTransparency )
497 {
498 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
499 	DBG_ASSERT( !rBitmapColor.mbIndex, "Pixel represents index into colortable!" );
500 	mcBlueOrIndex = COLOR_CHANNEL_MERGE( mcBlueOrIndex, rBitmapColor.mcBlueOrIndex, cTransparency );
501 	mcGreen = COLOR_CHANNEL_MERGE( mcGreen, rBitmapColor.mcGreen, cTransparency );
502 	mcRed = COLOR_CHANNEL_MERGE( mcRed, rBitmapColor.mcRed, cTransparency );
503 
504 	return *this;
505 }
506 
507 // ------------------------------------------------------------------
508 
Merge(sal_uInt8 cR,sal_uInt8 cG,sal_uInt8 cB,sal_uInt8 cTransparency)509 inline BitmapColor&	BitmapColor::Merge( sal_uInt8 cR, sal_uInt8 cG, sal_uInt8 cB, sal_uInt8 cTransparency )
510 {
511 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
512 	mcBlueOrIndex = COLOR_CHANNEL_MERGE( mcBlueOrIndex, cB, cTransparency );
513 	mcGreen = COLOR_CHANNEL_MERGE( mcGreen, cG, cTransparency );
514 	mcRed = COLOR_CHANNEL_MERGE( mcRed, cR, cTransparency );
515 
516 	return *this;
517 }
518 
519 // ------------------------------------------------------------------
520 
GetColorError(const BitmapColor & rBitmapColor) const521 inline sal_uLong BitmapColor::GetColorError( const BitmapColor& rBitmapColor ) const
522 {
523 	DBG_ASSERT( !mbIndex, "Pixel represents index into colortable!" );
524 	DBG_ASSERT( !rBitmapColor.mbIndex, "Pixel represents index into colortable!" );
525 	return( (sal_uLong) ( labs( mcBlueOrIndex - rBitmapColor.mcBlueOrIndex ) +
526 					  labs( mcGreen - rBitmapColor.mcGreen ) +
527 					  labs( mcRed - rBitmapColor.mcRed ) ) );
528 }
529 
530 // ------------------------------------------------------------------
531 
BitmapPalette()532 inline BitmapPalette::BitmapPalette() :
533 			mpBitmapColor	( NULL ),
534 			mnCount 		( 0 )
535 {
536 }
537 
538 // ------------------------------------------------------------------
539 
BitmapPalette(const BitmapPalette & rBitmapPalette)540 inline BitmapPalette::BitmapPalette( const BitmapPalette& rBitmapPalette ) :
541 			mnCount( rBitmapPalette.mnCount )
542 {
543 	if( mnCount )
544 	{
545 		const sal_uLong nSize = mnCount * sizeof( BitmapColor );
546 		mpBitmapColor = (BitmapColor*) new sal_uInt8[ nSize ];
547 		memcpy( mpBitmapColor, rBitmapPalette.mpBitmapColor, nSize );
548 	}
549 	else
550 		mpBitmapColor = NULL;
551 }
552 
553 // ------------------------------------------------------------------
554 
BitmapPalette(sal_uInt16 nCount)555 inline BitmapPalette::BitmapPalette( sal_uInt16 nCount ) :
556 			mnCount( nCount )
557 {
558 	if( mnCount )
559 	{
560 		const sal_uLong nSize = mnCount * sizeof( BitmapColor );
561 		mpBitmapColor = (BitmapColor*) new sal_uInt8[ nSize ];
562 		memset( mpBitmapColor, 0, nSize );
563 	}
564 	else
565 		mpBitmapColor = NULL;
566 }
567 
568 // ------------------------------------------------------------------
569 
~BitmapPalette()570 inline BitmapPalette::~BitmapPalette()
571 {
572 	delete[] (sal_uInt8*) mpBitmapColor;
573 }
574 
575 // ------------------------------------------------------------------
576 
operator =(const BitmapPalette & rBitmapPalette)577 inline BitmapPalette& BitmapPalette::operator=( const BitmapPalette& rBitmapPalette )
578 {
579 	delete[] (sal_uInt8*) mpBitmapColor;
580 	mnCount = rBitmapPalette.mnCount;
581 
582 	if( mnCount )
583 	{
584 		const sal_uLong nSize = mnCount * sizeof( BitmapColor );
585 		mpBitmapColor = (BitmapColor*) new sal_uInt8[ nSize ];
586 		memcpy( mpBitmapColor, rBitmapPalette.mpBitmapColor, nSize );
587 	}
588 	else
589 		mpBitmapColor = NULL;
590 
591 	return *this;
592 }
593 
594 // ------------------------------------------------------------------
595 
operator ==(const BitmapPalette & rBitmapPalette) const596 inline sal_Bool BitmapPalette::operator==( const BitmapPalette& rBitmapPalette ) const
597 {
598 	sal_Bool bRet = sal_False;
599 
600 	if( rBitmapPalette.mnCount == mnCount )
601 	{
602 		bRet = sal_True;
603 
604 		for( sal_uInt16 i = 0; i < mnCount; i++ )
605 		{
606 			if( mpBitmapColor[ i ] != rBitmapPalette.mpBitmapColor[ i ] )
607 			{
608 				bRet = sal_False;
609 				break;
610 			}
611 		}
612 	}
613 
614 	return bRet;
615 }
616 
617 // ------------------------------------------------------------------
618 
operator !=(const BitmapPalette & rBitmapPalette) const619 inline sal_Bool BitmapPalette::operator!=( const BitmapPalette& rBitmapPalette ) const
620 {
621 	return !( *this == rBitmapPalette );
622 }
623 
624 // ------------------------------------------------------------------
625 
operator !()626 inline sal_Bool BitmapPalette::operator!()
627 {
628 	return( !mnCount || !mpBitmapColor );
629 }
630 
631 // ------------------------------------------------------------------
632 
GetEntryCount() const633 inline sal_uInt16 BitmapPalette::GetEntryCount() const
634 {
635 	return mnCount;
636 }
637 
638 // ------------------------------------------------------------------
639 
SetEntryCount(sal_uInt16 nCount)640 inline void BitmapPalette::SetEntryCount( sal_uInt16 nCount )
641 {
642 	if( !nCount )
643 	{
644 		delete[] (sal_uInt8*) mpBitmapColor;
645 		mpBitmapColor = NULL;
646 		mnCount = 0;
647 	}
648 	else if( nCount != mnCount )
649 	{
650 		const sal_uLong nNewSize = nCount * sizeof( BitmapColor );
651 		const sal_uLong nMinSize = Min( mnCount, nCount ) * sizeof( BitmapColor );
652 		sal_uInt8*		pNewColor = new sal_uInt8[ nNewSize ];
653 
654 		if ( nMinSize && mpBitmapColor )
655 			memcpy( pNewColor, mpBitmapColor, nMinSize );
656 		delete[] (sal_uInt8*) mpBitmapColor;
657 		memset( pNewColor + nMinSize, 0, nNewSize - nMinSize );
658 		mpBitmapColor = (BitmapColor*) pNewColor;
659 		mnCount = nCount;
660 	}
661 }
662 
663 // ------------------------------------------------------------------
664 
operator [](sal_uInt16 nIndex) const665 inline const BitmapColor& BitmapPalette::operator[]( sal_uInt16 nIndex ) const
666 {
667 	DBG_ASSERT( nIndex < mnCount, "Palette index is out of range!" );
668 	return mpBitmapColor[ nIndex ];
669 }
670 
671 // ------------------------------------------------------------------
672 
operator [](sal_uInt16 nIndex)673 inline BitmapColor& BitmapPalette::operator[]( sal_uInt16 nIndex )
674 {
675 	DBG_ASSERT( nIndex < mnCount, "Palette index is out of range!" );
676 	return mpBitmapColor[ nIndex ];
677 }
678 
679 // ------------------------------------------------------------------
680 
681 //#if 0 // _SOLAR__PRIVATE
ImplGetColorBuffer() const682 inline BitmapColor* BitmapPalette::ImplGetColorBuffer() const
683 {
684 	DBG_ASSERT( mpBitmapColor, "No color buffer available!" );
685 	return mpBitmapColor;
686 }
687 //#endif
688 // ------------------------------------------------------------------
689 
GetBestIndex(const BitmapColor & rCol) const690 inline sal_uInt16 BitmapPalette::GetBestIndex( const BitmapColor& rCol ) const
691 {
692 	sal_uInt16 nRetIndex = 0;
693 
694 	if( mpBitmapColor && mnCount )
695 	{
696 		sal_Bool bFound = sal_False;
697 
698 		for( long j = 0L; ( j < mnCount ) && !bFound; j++ )
699 			if( rCol == mpBitmapColor[ j ] )
700 				nRetIndex = ( (sal_uInt16) j ), bFound = sal_True;
701 
702 		if( !bFound )
703 		{
704 			long nActErr, nLastErr = rCol.GetColorError( mpBitmapColor[ nRetIndex = mnCount - 1 ] );
705 
706 			for( long i = nRetIndex - 1; i >= 0L; i-- )
707 				if ( ( nActErr = rCol.GetColorError( mpBitmapColor[ i ] ) ) < nLastErr )
708 					nLastErr = nActErr, nRetIndex = (sal_uInt16) i;
709 		}
710 	}
711 
712 	return nRetIndex;
713 }
714 
715 // ------------------------------------------------------------------
716 
ColorMask(sal_uLong nRedMask,sal_uLong nGreenMask,sal_uLong nBlueMask)717 inline ColorMask::ColorMask( sal_uLong nRedMask, sal_uLong nGreenMask, sal_uLong nBlueMask ) :
718 			mnRMask( nRedMask ),
719 			mnGMask( nGreenMask ),
720 			mnBMask( nBlueMask ),
721             mnROrShift( 0L ),
722             mnGOrShift( 0L ),
723             mnBOrShift( 0L ),
724             mnROr( 0L ),
725             mnGOr( 0L ),
726             mnBOr( 0L )
727 {
728 	mnRShift = ( mnRMask ? ImplCalcMaskShift( mnRMask, mnROr, mnROrShift ) : 0L );
729 	mnGShift = ( mnGMask ? ImplCalcMaskShift( mnGMask, mnGOr, mnGOrShift ) : 0L );
730 	mnBShift = ( mnBMask ? ImplCalcMaskShift( mnBMask, mnBOr, mnBOrShift ) : 0L );
731 }
732 
733 // ------------------------------------------------------------------
734 
ImplCalcMaskShift(sal_uLong nMask,sal_uLong & rOr,sal_uLong & rOrShift) const735 inline long ColorMask::ImplCalcMaskShift( sal_uLong nMask, sal_uLong& rOr, sal_uLong& rOrShift ) const
736 {
737 	long	nShift;
738 	long	nRet;
739 	sal_uLong	nLen = 0UL;
740 
741 	// bei welchen Bits faengt die Maske an
742 	for( nShift = 31L; ( nShift >= 0L ) && !( nMask & ( 1 << (sal_uLong) nShift ) ); nShift-- )
743 	{}
744 
745 	nRet = nShift;
746 
747 	// XXX Anzahl der gesetzten Bits ermitteln => nach rechts bis Null laufen
748 	while( ( nShift >= 0L ) && ( nMask & ( 1 << (sal_uLong) nShift ) ) )
749 	{
750 		nShift--;
751 		nLen++;
752 	}
753 
754 	rOrShift = 8L - nLen;
755 	rOr = (sal_uInt8) ( ( 0xffUL >> nLen ) << rOrShift );
756 
757 	return( nRet -= 7 );
758 }
759 
760 // ------------------------------------------------------------------
761 
GetRedMask() const762 inline sal_uLong ColorMask::GetRedMask() const
763 {
764 	return mnRMask;
765 }
766 
767 // ------------------------------------------------------------------
768 
GetGreenMask() const769 inline sal_uLong ColorMask::GetGreenMask() const
770 {
771 	return mnGMask;
772 }
773 
774 // ------------------------------------------------------------------
775 
GetBlueMask() const776 inline sal_uLong ColorMask::GetBlueMask() const
777 {
778 	return mnBMask;
779 }
780 
781 // ------------------------------------------------------------------
782 
GetColorFor8Bit(BitmapColor & rColor,ConstHPBYTE pPixel) const783 inline void ColorMask::GetColorFor8Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const
784 {
785 	const sal_uInt32 nVal = *pPixel;
786 	MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
787 }
788 
789 // ------------------------------------------------------------------
790 
SetColorFor8Bit(const BitmapColor & rColor,HPBYTE pPixel) const791 inline void ColorMask::SetColorFor8Bit( const BitmapColor& rColor, HPBYTE pPixel ) const
792 {
793 	*pPixel = (sal_uInt8) COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift );
794 }
795 
796 // ------------------------------------------------------------------
797 
GetColorFor16BitMSB(BitmapColor & rColor,ConstHPBYTE pPixel) const798 inline void ColorMask::GetColorFor16BitMSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const
799 {
800 #ifdef OSL_BIGENDIAN
801 	const sal_uInt32 nVal = *(sal_uInt16*) pPixel;
802 #else
803 	const sal_uInt32 nVal = pPixel[ 1 ] | ( (sal_uInt32) pPixel[ 0 ] << 8UL );
804 #endif
805 
806 	MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
807 }
808 
809 // ------------------------------------------------------------------
810 
SetColorFor16BitMSB(const BitmapColor & rColor,HPBYTE pPixel) const811 inline void ColorMask::SetColorFor16BitMSB( const BitmapColor& rColor, HPBYTE pPixel ) const
812 {
813     const sal_uInt16 nVal = (sal_uInt16)COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift );
814 
815 #ifdef OSL_BIGENDIAN
816     *(sal_uInt16*) pPixel = nVal;
817 #else
818     pPixel[ 0 ] = (sal_uInt8)(nVal >> 8U);
819     pPixel[ 1 ] = (sal_uInt8) nVal;
820 #endif
821 }
822 
823 // ------------------------------------------------------------------
824 
GetColorFor16BitLSB(BitmapColor & rColor,ConstHPBYTE pPixel) const825 inline void ColorMask::GetColorFor16BitLSB( BitmapColor& rColor, ConstHPBYTE pPixel ) const
826 {
827 #ifdef OSL_BIGENDIAN
828     const sal_uInt32 nVal = pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL );
829 #else
830     const sal_uInt32 nVal = *(sal_uInt16*) pPixel;
831 #endif
832 
833     MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
834 }
835 
836 // ------------------------------------------------------------------
837 
SetColorFor16BitLSB(const BitmapColor & rColor,HPBYTE pPixel) const838 inline void ColorMask::SetColorFor16BitLSB( const BitmapColor& rColor, HPBYTE pPixel ) const
839 {
840     const sal_uInt16 nVal = (sal_uInt16)COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift );
841 
842 #ifdef OSL_BIGENDIAN
843     pPixel[ 0 ] = (sal_uInt8) nVal;
844     pPixel[ 1 ] = (sal_uInt8)(nVal >> 8U);
845 #else
846     *(sal_uInt16*) pPixel = nVal;
847 #endif
848 }
849 
850 
851 // ------------------------------------------------------------------
852 
GetColorFor24Bit(BitmapColor & rColor,ConstHPBYTE pPixel) const853 inline void ColorMask::GetColorFor24Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const
854 {
855 	const sal_uInt32 nVal = pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL ) | ( (sal_uInt32) pPixel[ 2 ] << 16UL );
856 	MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
857 }
858 
859 // ------------------------------------------------------------------
860 
SetColorFor24Bit(const BitmapColor & rColor,HPBYTE pPixel) const861 inline void ColorMask::SetColorFor24Bit( const BitmapColor& rColor, HPBYTE pPixel ) const
862 {
863 	const sal_uInt32 nVal = COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift );
864 	pPixel[ 0 ] = (sal_uInt8) nVal; pPixel[ 1 ] = (sal_uInt8) ( nVal >> 8UL ); pPixel[ 2 ] = (sal_uInt8) ( nVal >> 16UL );
865 }
866 
867 // ------------------------------------------------------------------
868 
GetColorFor32Bit(BitmapColor & rColor,ConstHPBYTE pPixel) const869 inline void ColorMask::GetColorFor32Bit( BitmapColor& rColor, ConstHPBYTE pPixel ) const
870 {
871 #ifdef OSL_BIGENDIAN
872 	const sal_uInt32 nVal = (sal_uInt32) pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL ) |
873 						( (sal_uInt32) pPixel[ 2 ] << 16UL ) | ( (sal_uInt32) pPixel[ 3 ] << 24UL );
874 #else
875 	const sal_uInt32 nVal = *(sal_uInt32*) pPixel;
876 #endif
877 
878 	MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
879 }
880 
881 // ------------------------------------------------------------------
882 
GetColorAndAlphaFor32Bit(BitmapColor & rColor,sal_uInt8 & rAlpha,ConstHPBYTE pPixel) const883 inline void ColorMask::GetColorAndAlphaFor32Bit( BitmapColor& rColor, sal_uInt8& rAlpha, ConstHPBYTE pPixel ) const
884 {
885 #ifdef OSL_BIGENDIAN
886 	const sal_uInt32 nVal = (sal_uInt32) pPixel[ 0 ] | ( (sal_uInt32) pPixel[ 1 ] << 8UL ) |
887 						( (sal_uInt32) pPixel[ 2 ] << 16UL ) | ( (sal_uInt32) pPixel[ 3 ] << 24UL );
888 #else
889 	const sal_uInt32 nVal = *(sal_uInt32*) pPixel;
890 #endif
891     rAlpha = (sal_uInt8)(nVal >> 24);
892 
893 	MASK_TO_COLOR( nVal, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift, rColor );
894 }
895 
896 // ------------------------------------------------------------------
897 
SetColorFor32Bit(const BitmapColor & rColor,HPBYTE pPixel) const898 inline void ColorMask::SetColorFor32Bit( const BitmapColor& rColor, HPBYTE pPixel ) const
899 {
900 #ifdef OSL_BIGENDIAN
901 	const sal_uInt32 nVal = COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift );
902 	pPixel[ 0 ] = (sal_uInt8) nVal; pPixel[ 1 ] = (sal_uInt8) ( nVal >> 8UL );
903 	pPixel[ 2 ] = (sal_uInt8) ( nVal >> 16UL ); pPixel[ 3 ] = (sal_uInt8) ( nVal >> 24UL );
904 #else
905 	*(sal_uInt32*) pPixel = COLOR_TO_MASK( rColor, mnRMask, mnGMask, mnBMask, mnRShift, mnGShift, mnBShift );
906 #endif
907 }
908 
909 #endif // _SV_SALBTYPE_HXX
910