update dev300-m58
[ooovba.git] / basebmp / inc / basebmp / rgbmaskpixelformats.hxx
blob972a394ef5dfe64e8e6aecc0af9baa99569e97ce
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rgbmaskpixelformats.hxx,v $
10 * $Revision: 1.4 $
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>
45 #include <functional>
47 namespace basebmp
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.
55 @tpl PixelType
56 Input pixel type to operate on
58 @tpl ColorType
59 Underlying color type, to convert the pixel values into
61 @tpl RedMask
62 Bitmask, to access the red bits in the data type
64 @tpl GreenMask
65 Bitmask, to access the green bits in the data type
67 @tpl BlueMask
68 Bitmask, to access the blue bits in the data type
70 @tpl SwapBytes
71 When true, the final pixel values will be byte-swapped before
72 passed on.
73 */
74 template< typename PixelType,
75 typename ColorType,
76 unsigned int RedMask,
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
87 enum {
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,
99 typename ColorType,
100 unsigned int RedMask,
101 unsigned int GreenMask,
102 unsigned int BlueMask,
103 bool SwapBytes > struct RGBMaskGetter :
104 public RGBMaskFunctorBase<PixelType,
105 ColorType,
106 RedMask,
107 GreenMask,
108 BlueMask,
109 SwapBytes>,
110 public std::unary_function<PixelType, ColorType>
112 typedef RGBMaskFunctorBase<PixelType,
113 ColorType,
114 RedMask,
115 GreenMask,
116 BlueMask,
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)) |
134 (shiftRight(red,
135 base_type::red_shift-8*
136 (signed)sizeof(typename base_type::component_type)+
137 2*base_type::red_bits)),
139 (shiftRight(green,
140 base_type::green_shift-8*
141 (signed)sizeof(typename base_type::component_type)+
142 base_type::green_bits)) |
143 (shiftRight(green,
144 base_type::green_shift-8*
145 (signed)sizeof(typename base_type::component_type)+
146 2*base_type::green_bits)),
148 (shiftRight(blue,
149 base_type::blue_shift-8*
150 (signed)sizeof(typename base_type::component_type)+
151 base_type::blue_bits)) |
152 (shiftRight(blue,
153 base_type::blue_shift-8*
154 (signed)sizeof(typename base_type::component_type)+
155 2*base_type::blue_bits)) );
156 return res;
160 template< typename PixelType,
161 typename ColorType,
162 unsigned int RedMask,
163 unsigned int GreenMask,
164 unsigned int BlueMask,
165 bool SwapBytes > struct RGBMaskSetter :
166 public RGBMaskFunctorBase<PixelType,
167 ColorType,
168 RedMask,
169 GreenMask,
170 BlueMask,
171 SwapBytes>,
172 public std::unary_function<ColorType, PixelType>
174 typedef RGBMaskFunctorBase<PixelType,
175 ColorType,
176 RedMask,
177 GreenMask,
178 BlueMask,
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(
188 (shiftLeft(red,
189 base_type::red_shift-8*
190 (signed)sizeof(typename base_type::component_type)+
191 base_type::red_bits) & RedMask) |
192 (shiftLeft(green,
193 base_type::green_shift-8*
194 (signed)sizeof(typename base_type::component_type)+
195 base_type::green_bits) & GreenMask) |
196 (shiftLeft(blue,
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,
216 Color,
217 RedMask,
218 GreenMask,
219 BlueMask,
220 SwapBytes> getter_type;
221 typedef RGBMaskSetter<pixel_type,
222 Color,
223 RedMask,
224 GreenMask,
225 BlueMask,
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 //-----------------------------------------------------------------------------
236 #ifdef OSL_LITENDIAN
237 # define BASEBMP_TRUECOLORMASK_LSB_SWAP false
238 # define BASEBMP_TRUECOLORMASK_MSB_SWAP true
239 #else
240 # ifdef OSL_BIGENDIAN
241 # define BASEBMP_TRUECOLORMASK_LSB_SWAP true
242 # define BASEBMP_TRUECOLORMASK_MSB_SWAP false
243 # else
244 # error Undetermined endianness!
245 # endif
246 #endif
248 //-----------------------------------------------------------------------------
250 // 16bpp MSB RGB
251 typedef PixelFormatTraitsTemplate_RGBMask<
252 sal_uInt16,
253 0xF800,
254 0x07E0,
255 0x001F,
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);
260 // 16bpp LSB RGB
261 typedef PixelFormatTraitsTemplate_RGBMask<
262 sal_uInt16,
263 0xF800,
264 0x07E0,
265 0x001F,
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<
272 sal_uInt32,
273 0xFF0000,
274 0x00FF00,
275 0x0000FF,
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<
282 sal_uInt32,
283 0xFF0000,
284 0x00FF00,
285 0x0000FF,
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 */