xref: /trunk/main/basebmp/source/intconversion.hxx (revision 883ba44fa57d8dd873f9491a44215c47fb8f7173)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef INCLUDED_BASEBMP_INTCONVERSION_HXX
23 #define INCLUDED_BASEBMP_INTCONVERSION_HXX
24 
25 #include <vigra/rgbvalue.hxx>
26 #include <functional>
27 
28 namespace basebmp
29 {
30     // metafunctions to retrieve correct POD from/to basebmp::Color
31     //------------------------------------------------------------------------
32 
33     // type-safe conversion from RgbValue to packed int32
34     template< class RgbVal > struct UInt32FromRgbValue
35     {
36         sal_uInt32 operator()( RgbVal const& c ) const
37         {
38             return (c[0] << 16) | (c[1] << 8) | c[2];
39         }
40     };
41 
42     // type-safe conversion from packed int32 to RgbValue
43     template< class RgbVal > struct RgbValueFromUInt32
44     {
45         RgbVal operator()( sal_uInt32 c ) const
46         {
47             return RgbVal((c >> 16) & 0xFF,
48                           (c >> 8) & 0xFF,
49                           c & 0xFF);
50         }
51     };
52 
53     // Get converter from given data type to sal_uInt32
54     template< typename DataType > struct uInt32Converter
55     {
56         typedef std::identity<DataType> to;
57         typedef std::identity<DataType> from;
58     };
59     template< unsigned int RedIndex,
60               unsigned int GreenIndex,
61               unsigned int BlueIndex > struct uInt32Converter<
62                   vigra::RGBValue< sal_uInt8,
63                                    RedIndex,
64                                    GreenIndex,
65                                    BlueIndex > >
66     {
67         typedef UInt32FromRgbValue<
68             vigra::RGBValue< sal_uInt8,
69                              RedIndex,
70                              GreenIndex,
71                              BlueIndex > >
72             to;
73         typedef RgbValueFromUInt32<
74             vigra::RGBValue< sal_uInt8,
75                              RedIndex,
76                              GreenIndex,
77                              BlueIndex > >
78             from;
79     };
80 }
81 
82 #endif /* INCLUDED_BASEBMP_INTCONVERSION_HXX */
83 
84 /* vim: set noet sw=4 ts=4: */
85