LibreOffice Module vcl (master)  1
ColorMask.hxx
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 #ifndef INCLUDED_VCL_COLORMASK_HXX
21 #define INCLUDED_VCL_COLORMASK_HXX
22 
23 #include <vcl/dllapi.h>
24 #include <vcl/BitmapColor.hxx>
25 
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 ) ) );
33 
34 
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 ) | \
42  d_ALPHA )
43 
44 
46 {
47  sal_uInt32 mnMask;
48  int mnShift;
49  int mnOrShift;
51  explicit ColorMaskElement(sal_uInt32 nMask = 0)
52  : mnMask(nMask)
53  , mnShift(0)
54  , mnOrShift(0)
55  , mnOr(0)
56  {
57  }
59  {
60  if (mnMask == 0)
61  return true;
62 
63  // from which bit starts the mask?
64  int nShift = 31;
65 
66  while( nShift >= 0 && !( mnMask & ( 1 << nShift ) ) )
67  --nShift;
68 
69  mnShift = nShift - 7;
70  int nLen = 0;
71 
72  // XXX determine number of bits set => walk right until null
73  while( nShift >= 0 && ( mnMask & ( 1 << nShift ) ) )
74  {
75  nShift--;
76  nLen++;
77  }
78 
79  if (nLen > 8) // mask length must be 8 bits or less
80  return false;
81 
82  mnOrShift = 8 - nLen;
83  mnOr = static_cast<sal_uInt8>( ( 0xFF >> nLen ) << mnOrShift );
84 
85  return true;
86  }
87 };
88 
90 {
94 
95 public:
96 
98  const ColorMaskElement& rGreenMask = ColorMaskElement(),
99  const ColorMaskElement& rBlueMask = ColorMaskElement())
100  : maR(rRedMask)
101  , maG(rGreenMask)
102  , maB(rBlueMask)
103  {
104  }
105 
106  inline sal_uInt32 GetRedMask() const;
107  inline sal_uInt32 GetGreenMask() const;
108  inline sal_uInt32 GetBlueMask() const;
109 
110  inline void GetColorFor8Bit( BitmapColor& rColor, const sal_uInt8* pPixel ) const;
111  inline void SetColorFor8Bit( const BitmapColor& rColor, sal_uInt8* pPixel ) const;
112 
113  inline void GetColorFor16BitMSB( BitmapColor& rColor, const sal_uInt8* pPixel ) const;
114  inline void SetColorFor16BitMSB( const BitmapColor& rColor, sal_uInt8* pPixel ) const;
115  inline void GetColorFor16BitLSB( BitmapColor& rColor, const sal_uInt8* pPixel ) const;
116  inline void SetColorFor16BitLSB( const BitmapColor& rColor, sal_uInt8* pPixel ) const;
117 
118  inline void GetColorFor32Bit( BitmapColor& rColor, const sal_uInt8* pPixel ) const;
119  inline void GetColorAndAlphaFor32Bit( BitmapColor& rColor, sal_uInt8& rAlpha, const sal_uInt8* pPixel ) const;
120  inline void SetColorFor32Bit( const BitmapColor& rColor, sal_uInt8* pPixel ) const;
121 };
122 
123 inline sal_uInt32 ColorMask::GetRedMask() const
124 {
125  return maR.mnMask;
126 }
127 
128 inline sal_uInt32 ColorMask::GetGreenMask() const
129 {
130  return maG.mnMask;
131 }
132 
133 inline sal_uInt32 ColorMask::GetBlueMask() const
134 {
135  return maB.mnMask;
136 }
137 
138 inline void ColorMask::GetColorFor8Bit( BitmapColor& rColor, const sal_uInt8* pPixel ) const
139 {
140  const sal_uInt32 nVal = *pPixel;
141  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maR.mnShift, rColor );
142 }
143 
144 inline void ColorMask::SetColorFor8Bit( const BitmapColor& rColor, sal_uInt8* pPixel ) const
145 {
146  *pPixel = static_cast<sal_uInt8>(COLOR_TO_MASK( rColor, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, 0/*nAlphaChannel*/ ));
147 }
148 
149 inline void ColorMask::GetColorFor16BitMSB( BitmapColor& rColor, const sal_uInt8* pPixel ) const
150 {
151  const sal_uInt32 nVal = pPixel[ 1 ] | ( static_cast<sal_uInt32>(pPixel[ 0 ]) << 8 );
152 
153  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
154 }
155 
156 inline void ColorMask::SetColorFor16BitMSB( const BitmapColor& rColor, sal_uInt8* pPixel ) const
157 {
158  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*/ ));
159 
160  pPixel[ 0 ] = static_cast<sal_uInt8>(nVal >> 8);
161  pPixel[ 1 ] = static_cast<sal_uInt8>(nVal);
162 }
163 
164 inline void ColorMask::GetColorFor16BitLSB( BitmapColor& rColor, const sal_uInt8* pPixel ) const
165 {
166  const sal_uInt32 nVal = pPixel[ 0 ] | ( static_cast<sal_uInt32>(pPixel[ 1 ]) << 8 );
167 
168  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
169 }
170 
171 inline void ColorMask::SetColorFor16BitLSB( const BitmapColor& rColor, sal_uInt8* pPixel ) const
172 {
173  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*/ ));
174 
175  pPixel[ 0 ] = static_cast<sal_uInt8>(nVal);
176  pPixel[ 1 ] = static_cast<sal_uInt8>(nVal >> 8);
177 }
178 
179 inline void ColorMask::GetColorFor32Bit( BitmapColor& rColor, const sal_uInt8* pPixel ) const
180 {
181  const sal_uInt32 nVal = static_cast<sal_uInt32>(pPixel[ 0 ]) | ( static_cast<sal_uInt32>(pPixel[ 1 ]) << 8 ) |
182  ( static_cast<sal_uInt32>(pPixel[ 2 ]) << 16 ) | ( static_cast<sal_uInt32>(pPixel[ 3 ]) << 24 );
183 
184  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
185 }
186 
187 inline void ColorMask::GetColorAndAlphaFor32Bit( BitmapColor& rColor, sal_uInt8& rAlpha, const sal_uInt8* pPixel ) const
188 {
189  const sal_uInt32 nVal = static_cast<sal_uInt32>(pPixel[ 0 ]) | ( static_cast<sal_uInt32>(pPixel[ 1 ]) << 8 ) |
190  ( static_cast<sal_uInt32>(pPixel[ 2 ]) << 16 ) | ( static_cast<sal_uInt32>(pPixel[ 3 ]) << 24 );
191  rAlpha = static_cast<sal_uInt8>(nVal >> 24);
192 
193  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
194 }
195 
196 inline void ColorMask::SetColorFor32Bit( const BitmapColor& rColor, sal_uInt8* pPixel ) const
197 {
198  const sal_uInt32 nVal = COLOR_TO_MASK( rColor, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, 0/*nAlphaChannel*/ );
199  pPixel[ 0 ] = static_cast<sal_uInt8>(nVal);
200  pPixel[ 1 ] = static_cast<sal_uInt8>( nVal >> 8 );
201  pPixel[ 2 ] = static_cast<sal_uInt8>( nVal >> 16 );
202  pPixel[ 3 ] = static_cast<sal_uInt8>( nVal >> 24 );
203 }
204 
205 #endif // INCLUDED_VCL_COLORMASK_HXX
206 
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void GetColorAndAlphaFor32Bit(BitmapColor &rColor, sal_uInt8 &rAlpha, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:187
void GetColorFor16BitMSB(BitmapColor &rColor, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:149
sal_uInt32 GetRedMask() const
Definition: ColorMask.hxx:123
ColorMaskElement maR
Definition: ColorMask.hxx:91
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
void GetColorFor8Bit(BitmapColor &rColor, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:138
void SetColorFor32Bit(const BitmapColor &rColor, sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:196
ColorMaskElement maG
Definition: ColorMask.hxx:92
#define MASK_TO_COLOR(d_nVal, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS, d_Col)
Definition: ColorMask.hxx:26
void GetColorFor16BitLSB(BitmapColor &rColor, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:164
ColorMaskElement maB
Definition: ColorMask.hxx:93
bool CalcMaskShift()
Definition: ColorMask.hxx:58
void SetColorFor16BitMSB(const BitmapColor &rColor, sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:156
void SetColorFor16BitLSB(const BitmapColor &rColor, sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:171
void SetColorFor8Bit(const BitmapColor &rColor, sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:144
unsigned char sal_uInt8
sal_uInt8 mnOr
Definition: ColorMask.hxx:50
ColorMaskElement(sal_uInt32 nMask=0)
Definition: ColorMask.hxx:51
sal_uInt32 GetBlueMask() const
Definition: ColorMask.hxx:133
void GetColorFor32Bit(BitmapColor &rColor, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:179
sal_uInt32 GetGreenMask() const
Definition: ColorMask.hxx:128
ColorMask(const ColorMaskElement &rRedMask=ColorMaskElement(), const ColorMaskElement &rGreenMask=ColorMaskElement(), const ColorMaskElement &rBlueMask=ColorMaskElement())
Definition: ColorMask.hxx:97
sal_uInt32 mnMask
Definition: ColorMask.hxx:47
#define COLOR_TO_MASK(d_rCol, d_RM, d_GM, d_BM, d_RS, d_GS, d_BS, d_ALPHA)
Definition: ColorMask.hxx:35