xref: /trunk/main/basebmp/source/intconversion.hxx (revision 48cdb363)
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 
23 
24 #ifndef INCLUDED_BASEBMP_INTCONVERSION_HXX
25 #define INCLUDED_BASEBMP_INTCONVERSION_HXX
26 
27 #include <vigra/rgbvalue.hxx>
28 #include <functional>
29 
30 namespace basebmp
31 {
32     // metafunctions to retrieve correct POD from/to basebmp::Color
33     //------------------------------------------------------------------------
34 
35     /// type-safe conversion from RgbValue to packed int32
36     template< class RgbVal > struct UInt32FromRgbValue
37     {
operator ()basebmp::UInt32FromRgbValue38         sal_uInt32 operator()( RgbVal const& c ) const
39         {
40             return (c[0] << 16) | (c[1] << 8) | c[2];
41         }
42     };
43 
44     /// type-safe conversion from packed int32 to RgbValue
45     template< class RgbVal > struct RgbValueFromUInt32
46     {
operator ()basebmp::RgbValueFromUInt3247         RgbVal operator()( sal_uInt32 c ) const
48         {
49             return RgbVal((c >> 16) & 0xFF,
50                           (c >> 8) & 0xFF,
51                           c & 0xFF);
52         }
53     };
54 
55     /// Get converter from given data type to sal_uInt32
56     template< typename DataType > struct uInt32Converter
57     {
58         typedef std::identity<DataType> to;
59         typedef std::identity<DataType> from;
60     };
61     template< unsigned int RedIndex,
62               unsigned int GreenIndex,
63               unsigned int BlueIndex > struct uInt32Converter<
64                   vigra::RGBValue< sal_uInt8,
65                                    RedIndex,
66                                    GreenIndex,
67                                    BlueIndex > >
68     {
69         typedef UInt32FromRgbValue<
70             vigra::RGBValue< sal_uInt8,
71                              RedIndex,
72                              GreenIndex,
73                              BlueIndex > >
74             to;
75         typedef RgbValueFromUInt32<
76             vigra::RGBValue< sal_uInt8,
77                              RedIndex,
78                              GreenIndex,
79                              BlueIndex > >
80             from;
81     };
82 }
83 
84 #endif /* INCLUDED_BASEBMP_INTCONVERSION_HXX */
85