1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rgbmaskpixelformats.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_BASEBMP_RGBMASKPIXELFORMATS_HXX
32 #define INCLUDED_BASEBMP_RGBMASKPIXELFORMATS_HXX
34 #include <basebmp/color.hxx>
35 #include <basebmp/colortraits.hxx>
36 #include <basebmp/accessor.hxx>
37 #include <basebmp/pixeliterator.hxx>
38 #include <basebmp/pixelformatadapters.hxx>
39 #include <basebmp/metafunctions.hxx>
40 #include <basebmp/endian.hxx>
42 #include <vigra/numerictraits.hxx>
43 #include <vigra/metaprogramming.hxx>
50 /** Base class operating on RGB truecolor mask pixel
52 Use this template, if you have an (integer) pixel type, and three
53 bitmasks denoting where the channel bits are.
56 Input pixel type to operate on
59 Underlying color type, to convert the pixel values into
62 Bitmask, to access the red bits in the data type
65 Bitmask, to access the green bits in the data type
68 Bitmask, to access the blue bits in the data type
71 When true, the final pixel values will be byte-swapped before
74 template< typename PixelType
,
77 unsigned int GreenMask
,
78 unsigned int BlueMask
,
79 bool SwapBytes
> struct RGBMaskFunctorBase
81 typedef PixelType pixel_type
;
82 typedef ColorType color_type
;
83 typedef typename make_unsigned
<pixel_type
>::type unsigned_pixel_type
;
84 typedef typename ColorTraits
<ColorType
>::component_type component_type
;
86 // calc corrective shifts for all three channels in advance
88 red_shift
= numberOfTrailingZeros
<RedMask
>::value
,
89 green_shift
= numberOfTrailingZeros
<GreenMask
>::value
,
90 blue_shift
= numberOfTrailingZeros
<BlueMask
>::value
,
92 red_bits
= bitcount
<RedMask
>::value
,
93 green_bits
= bitcount
<GreenMask
>::value
,
94 blue_bits
= bitcount
<BlueMask
>::value
98 template< typename PixelType
,
100 unsigned int RedMask
,
101 unsigned int GreenMask
,
102 unsigned int BlueMask
,
103 bool SwapBytes
> struct RGBMaskGetter
:
104 public RGBMaskFunctorBase
<PixelType
,
110 public std::unary_function
<PixelType
, ColorType
>
112 typedef RGBMaskFunctorBase
<PixelType
,
117 SwapBytes
> base_type
;
119 ColorType
operator()( PixelType v
) const
121 v
= SwapBytes
? byteSwap(v
) : v
;
123 const typename
base_type::unsigned_pixel_type
red (v
& RedMask
);
124 const typename
base_type::unsigned_pixel_type
green(v
& GreenMask
);
125 const typename
base_type::unsigned_pixel_type
blue (v
& BlueMask
);
127 // shift color nibbles to right-aligend position. ORing it
128 // channel value shifted twice the number of channel bits, to
129 // spread the value into the component_type range
130 ColorType
res( (shiftRight(red
,
131 base_type::red_shift
-8*
132 (signed)sizeof(typename
base_type::component_type
)+
133 base_type::red_bits
)) |
135 base_type::red_shift
-8*
136 (signed)sizeof(typename
base_type::component_type
)+
137 2*base_type::red_bits
)),
140 base_type::green_shift
-8*
141 (signed)sizeof(typename
base_type::component_type
)+
142 base_type::green_bits
)) |
144 base_type::green_shift
-8*
145 (signed)sizeof(typename
base_type::component_type
)+
146 2*base_type::green_bits
)),
149 base_type::blue_shift
-8*
150 (signed)sizeof(typename
base_type::component_type
)+
151 base_type::blue_bits
)) |
153 base_type::blue_shift
-8*
154 (signed)sizeof(typename
base_type::component_type
)+
155 2*base_type::blue_bits
)) );
160 template< typename PixelType
,
162 unsigned int RedMask
,
163 unsigned int GreenMask
,
164 unsigned int BlueMask
,
165 bool SwapBytes
> struct RGBMaskSetter
:
166 public RGBMaskFunctorBase
<PixelType
,
172 public std::unary_function
<ColorType
, PixelType
>
174 typedef RGBMaskFunctorBase
<PixelType
,
179 SwapBytes
> base_type
;
181 PixelType
operator()( ColorType
const& c
) const
183 const typename
base_type::unsigned_pixel_type
red (c
.getRed());
184 const typename
base_type::unsigned_pixel_type
green(c
.getGreen());
185 const typename
base_type::unsigned_pixel_type
blue (c
.getBlue());
187 typename
base_type::unsigned_pixel_type
res(
189 base_type::red_shift
-8*
190 (signed)sizeof(typename
base_type::component_type
)+
191 base_type::red_bits
) & RedMask
) |
193 base_type::green_shift
-8*
194 (signed)sizeof(typename
base_type::component_type
)+
195 base_type::green_bits
) & GreenMask
) |
197 base_type::blue_shift
-8*
198 (signed)sizeof(typename
base_type::component_type
)+
199 base_type::blue_bits
) & BlueMask
) );
201 return SwapBytes
? byteSwap(res
) : res
;
205 //-----------------------------------------------------------------------------
207 template< typename PixelType
,
208 unsigned int RedMask
,
209 unsigned int GreenMask
,
210 unsigned int BlueMask
,
211 bool SwapBytes
> struct PixelFormatTraitsTemplate_RGBMask
213 typedef PixelType pixel_type
;
215 typedef RGBMaskGetter
<pixel_type
,
220 SwapBytes
> getter_type
;
221 typedef RGBMaskSetter
<pixel_type
,
226 SwapBytes
> setter_type
;
228 typedef PixelIterator
<pixel_type
> iterator_type
;
229 typedef StandardAccessor
<pixel_type
> raw_accessor_type
;
230 typedef AccessorSelector
<
231 getter_type
, setter_type
> accessor_selector
;
234 //-----------------------------------------------------------------------------
237 # define BASEBMP_TRUECOLORMASK_LSB_SWAP false
238 # define BASEBMP_TRUECOLORMASK_MSB_SWAP true
240 # ifdef OSL_BIGENDIAN
241 # define BASEBMP_TRUECOLORMASK_LSB_SWAP true
242 # define BASEBMP_TRUECOLORMASK_MSB_SWAP false
244 # error Undetermined endianness!
248 //-----------------------------------------------------------------------------
251 typedef PixelFormatTraitsTemplate_RGBMask
<
256 BASEBMP_TRUECOLORMASK_MSB_SWAP
> PixelFormatTraits_RGB16_565_MSB
;
257 BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGB16_565_MSB::getter_type
,
258 PixelFormatTraits_RGB16_565_MSB::setter_type
);
261 typedef PixelFormatTraitsTemplate_RGBMask
<
266 BASEBMP_TRUECOLORMASK_LSB_SWAP
> PixelFormatTraits_RGB16_565_LSB
;
267 BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGB16_565_LSB::getter_type
,
268 PixelFormatTraits_RGB16_565_LSB::setter_type
);
270 // 32bpp endian-sensitive RGB
271 typedef PixelFormatTraitsTemplate_RGBMask
<
276 BASEBMP_TRUECOLORMASK_LSB_SWAP
> PixelFormatTraits_RGB32_888
;
277 BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_RGB32_888::getter_type
,
278 PixelFormatTraits_RGB32_888::setter_type
);
280 // 32bpp endian-sensitive BGR
281 typedef PixelFormatTraitsTemplate_RGBMask
<
286 BASEBMP_TRUECOLORMASK_MSB_SWAP
> PixelFormatTraits_BGR32_888
;
287 BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_BGR32_888::getter_type
,
288 PixelFormatTraits_BGR32_888::setter_type
);
290 } // namespace basebmp
292 #endif /* INCLUDED_BASEBMP_RGBMASKPIXELFORMATS_HXX */