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 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;
114 
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;
118 };
119 
120 inline sal_uInt32 ColorMask::GetRedMask() const
121 {
122  return maR.mnMask;
123 }
124 
125 inline sal_uInt32 ColorMask::GetGreenMask() const
126 {
127  return maG.mnMask;
128 }
129 
130 inline sal_uInt32 ColorMask::GetBlueMask() const
131 {
132  return maB.mnMask;
133 }
134 
135 inline void ColorMask::GetColorFor16BitMSB( BitmapColor& rColor, const sal_uInt8* pPixel ) const
136 {
137  const sal_uInt32 nVal = pPixel[ 1 ] | ( static_cast<sal_uInt32>(pPixel[ 0 ]) << 8 );
138 
139  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
140 }
141 
142 inline void ColorMask::SetColorFor16BitMSB( const BitmapColor& rColor, sal_uInt8* pPixel ) const
143 {
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*/ ));
145 
146  pPixel[ 0 ] = static_cast<sal_uInt8>(nVal >> 8);
147  pPixel[ 1 ] = static_cast<sal_uInt8>(nVal);
148 }
149 
150 inline void ColorMask::GetColorFor16BitLSB( BitmapColor& rColor, const sal_uInt8* pPixel ) const
151 {
152  const sal_uInt32 nVal = pPixel[ 0 ] | ( static_cast<sal_uInt32>(pPixel[ 1 ]) << 8 );
153 
154  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
155 }
156 
157 inline void ColorMask::SetColorFor16BitLSB( const BitmapColor& rColor, sal_uInt8* pPixel ) const
158 {
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*/ ));
160 
161  pPixel[ 0 ] = static_cast<sal_uInt8>(nVal);
162  pPixel[ 1 ] = static_cast<sal_uInt8>(nVal >> 8);
163 }
164 
165 inline void ColorMask::GetColorFor32Bit( BitmapColor& rColor, const sal_uInt8* pPixel ) const
166 {
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 );
169 
170  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
171 }
172 
173 inline void ColorMask::GetColorAndAlphaFor32Bit( BitmapColor& rColor, sal_uInt8& rAlpha, const sal_uInt8* pPixel ) const
174 {
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);
178 
179  MASK_TO_COLOR( nVal, maR.mnMask, maG.mnMask, maB.mnMask, maR.mnShift, maG.mnShift, maB.mnShift, rColor );
180 }
181 
182 inline void ColorMask::SetColorFor32Bit( const BitmapColor& rColor, sal_uInt8* pPixel ) const
183 {
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 );
189 }
190 
191 #endif // INCLUDED_VCL_COLORMASK_HXX
192 
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void GetColorAndAlphaFor32Bit(BitmapColor &rColor, sal_uInt8 &rAlpha, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:173
void GetColorFor16BitMSB(BitmapColor &rColor, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:135
sal_uInt32 GetRedMask() const
Definition: ColorMask.hxx:120
ColorMaskElement maR
Definition: ColorMask.hxx:91
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
void SetColorFor32Bit(const BitmapColor &rColor, sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:182
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:150
ColorMaskElement maB
Definition: ColorMask.hxx:93
bool CalcMaskShift()
Definition: ColorMask.hxx:58
void SetColorFor16BitMSB(const BitmapColor &rColor, sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:142
void SetColorFor16BitLSB(const BitmapColor &rColor, sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:157
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:130
void GetColorFor32Bit(BitmapColor &rColor, const sal_uInt8 *pPixel) const
Definition: ColorMask.hxx:165
sal_uInt32 GetGreenMask() const
Definition: ColorMask.hxx:125
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