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_COLORMASK_HXX
21 #define INCLUDED_VCL_COLORMASK_HXX
23 #include <vcl/dllapi.h>
24 #include <vcl/BitmapColor.hxx>
26 #define MASK_TO_COLOR( d_nVal, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS, d_Col ) \
27 const sal_uInt8 _def_cR = static_cast<sal_uInt8>( d_RS < 0 ? ( (d_nVal) & d_RM ) << -d_RS : ( (d_nVal) & d_RM ) >> d_RS ); \
28 const sal_uInt8 _def_cG = static_cast<sal_uInt8>( d_GS < 0 ? ( (d_nVal) & d_GM ) << -d_GS : ( (d_nVal) & d_GM ) >> d_GS ); \
29 const sal_uInt8 _def_cB = static_cast<sal_uInt8>( d_BS < 0 ? ( (d_nVal) & d_BM ) << -d_BS : ( (d_nVal) & d_BM ) >> d_BS ); \
30 d_Col = BitmapColor( static_cast<sal_uInt8>( _def_cR | ( ( _def_cR & maR.mnOr ) >> maR.mnOrShift ) ), \
31 static_cast<sal_uInt8>( _def_cG | ( ( _def_cG & maG.mnOr ) >> maG.mnOrShift ) ), \
32 static_cast<sal_uInt8>( _def_cB | ( ( _def_cB & maB.mnOr ) >> maB.mnOrShift ) ) );
35 #define COLOR_TO_MASK( d_rCol, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS, d_ALPHA ) \
36 ( ( ( ( d_RS < 0 ) ? ( static_cast<sal_uInt32>((d_rCol).GetRed()) >> -d_RS ) : \
37 ( static_cast<sal_uInt32>((d_rCol).GetRed()) << d_RS ) ) & d_RM ) | \
38 ( ( ( d_GS < 0 ) ? ( static_cast<sal_uInt32>((d_rCol).GetGreen()) >> -d_GS ) : \
39 ( static_cast<sal_uInt32>((d_rCol).GetGreen()) << d_GS ) ) & d_GM ) | \
40 ( ( ( d_BS < 0 ) ? ( static_cast<sal_uInt32>((d_rCol).GetBlue()) >> -d_BS ) : \
41 ( static_cast<sal_uInt32>((d_rCol).GetBlue()) << d_BS ) ) & d_BM ) | \
45 struct VCL_DLLPUBLIC ColorMaskElement
51 explicit ColorMaskElement(sal_uInt32 nMask
= 0)
63 // from which bit starts the mask?
66 while( nShift
>= 0 && !( mnMask
& ( 1 << nShift
) ) )
72 // XXX determine number of bits set => walk right until null
73 while( nShift
>= 0 && ( mnMask
& ( 1 << nShift
) ) )
79 if (nLen
> 8) // mask length must be 8 bits or less
83 mnOr
= static_cast<sal_uInt8
>( ( 0xFF >> nLen
) << mnOrShift
);
89 class VCL_DLLPUBLIC ColorMask
97 ColorMask(const ColorMaskElement
& rRedMask
= ColorMaskElement(),
98 const ColorMaskElement
& rGreenMask
= ColorMaskElement(),
99 const ColorMaskElement
& rBlueMask
= ColorMaskElement())
106 inline sal_uInt32
GetRedMask() const;
107 inline sal_uInt32
GetGreenMask() const;
108 inline sal_uInt32
GetBlueMask() const;
110 inline void GetColorFor16BitMSB( BitmapColor
& rColor
, const sal_uInt8
* pPixel
) const;
111 inline void SetColorFor16BitMSB( const BitmapColor
& rColor
, sal_uInt8
* pPixel
) const;
112 inline void GetColorFor16BitLSB( BitmapColor
& rColor
, const sal_uInt8
* pPixel
) const;
113 inline void SetColorFor16BitLSB( const BitmapColor
& rColor
, sal_uInt8
* pPixel
) const;
115 inline void GetColorFor32Bit( BitmapColor
& rColor
, const sal_uInt8
* pPixel
) const;
116 inline void GetColorAndAlphaFor32Bit( BitmapColor
& rColor
, sal_uInt8
& rAlpha
, const sal_uInt8
* pPixel
) const;
117 inline void SetColorFor32Bit( const BitmapColor
& rColor
, sal_uInt8
* pPixel
) const;
120 inline sal_uInt32
ColorMask::GetRedMask() const
125 inline sal_uInt32
ColorMask::GetGreenMask() const
130 inline sal_uInt32
ColorMask::GetBlueMask() const
135 inline void ColorMask::GetColorFor16BitMSB( BitmapColor
& rColor
, const sal_uInt8
* pPixel
) const
137 const sal_uInt32 nVal
= pPixel
[ 1 ] | ( static_cast<sal_uInt32
>(pPixel
[ 0 ]) << 8 );
139 MASK_TO_COLOR( nVal
, maR
.mnMask
, maG
.mnMask
, maB
.mnMask
, maR
.mnShift
, maG
.mnShift
, maB
.mnShift
, rColor
);
142 inline void ColorMask::SetColorFor16BitMSB( const BitmapColor
& rColor
, sal_uInt8
* pPixel
) const
144 const sal_uInt16 nVal
= static_cast<sal_uInt16
>(COLOR_TO_MASK( rColor
, maR
.mnMask
, maG
.mnMask
, maB
.mnMask
, maR
.mnShift
, maG
.mnShift
, maB
.mnShift
, 0/*nAlphaChannel*/ ));
146 pPixel
[ 0 ] = static_cast<sal_uInt8
>(nVal
>> 8);
147 pPixel
[ 1 ] = static_cast<sal_uInt8
>(nVal
);
150 inline void ColorMask::GetColorFor16BitLSB( BitmapColor
& rColor
, const sal_uInt8
* pPixel
) const
152 const sal_uInt32 nVal
= pPixel
[ 0 ] | ( static_cast<sal_uInt32
>(pPixel
[ 1 ]) << 8 );
154 MASK_TO_COLOR( nVal
, maR
.mnMask
, maG
.mnMask
, maB
.mnMask
, maR
.mnShift
, maG
.mnShift
, maB
.mnShift
, rColor
);
157 inline void ColorMask::SetColorFor16BitLSB( const BitmapColor
& rColor
, sal_uInt8
* pPixel
) const
159 const sal_uInt16 nVal
= static_cast<sal_uInt16
>(COLOR_TO_MASK( rColor
, maR
.mnMask
, maG
.mnMask
, maB
.mnMask
, maR
.mnShift
, maG
.mnShift
, maB
.mnShift
, 0/*nAlphaChannel*/ ));
161 pPixel
[ 0 ] = static_cast<sal_uInt8
>(nVal
);
162 pPixel
[ 1 ] = static_cast<sal_uInt8
>(nVal
>> 8);
165 inline void ColorMask::GetColorFor32Bit( BitmapColor
& rColor
, const sal_uInt8
* pPixel
) const
167 const sal_uInt32 nVal
= static_cast<sal_uInt32
>(pPixel
[ 0 ]) | ( static_cast<sal_uInt32
>(pPixel
[ 1 ]) << 8 ) |
168 ( static_cast<sal_uInt32
>(pPixel
[ 2 ]) << 16 ) | ( static_cast<sal_uInt32
>(pPixel
[ 3 ]) << 24 );
170 MASK_TO_COLOR( nVal
, maR
.mnMask
, maG
.mnMask
, maB
.mnMask
, maR
.mnShift
, maG
.mnShift
, maB
.mnShift
, rColor
);
173 inline void ColorMask::GetColorAndAlphaFor32Bit( BitmapColor
& rColor
, sal_uInt8
& rAlpha
, const sal_uInt8
* pPixel
) const
175 const sal_uInt32 nVal
= static_cast<sal_uInt32
>(pPixel
[ 0 ]) | ( static_cast<sal_uInt32
>(pPixel
[ 1 ]) << 8 ) |
176 ( static_cast<sal_uInt32
>(pPixel
[ 2 ]) << 16 ) | ( static_cast<sal_uInt32
>(pPixel
[ 3 ]) << 24 );
177 rAlpha
= static_cast<sal_uInt8
>(nVal
>> 24);
179 MASK_TO_COLOR( nVal
, maR
.mnMask
, maG
.mnMask
, maB
.mnMask
, maR
.mnShift
, maG
.mnShift
, maB
.mnShift
, rColor
);
182 inline void ColorMask::SetColorFor32Bit( const BitmapColor
& rColor
, sal_uInt8
* pPixel
) const
184 const sal_uInt32 nVal
= COLOR_TO_MASK( rColor
, maR
.mnMask
, maG
.mnMask
, maB
.mnMask
, maR
.mnShift
, maG
.mnShift
, maB
.mnShift
, 0/*nAlphaChannel*/ );
185 pPixel
[ 0 ] = static_cast<sal_uInt8
>(nVal
);
186 pPixel
[ 1 ] = static_cast<sal_uInt8
>( nVal
>> 8 );
187 pPixel
[ 2 ] = static_cast<sal_uInt8
>( nVal
>> 16 );
188 pPixel
[ 3 ] = static_cast<sal_uInt8
>( nVal
>> 24 );
191 #endif // INCLUDED_VCL_COLORMASK_HXX
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */