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_GREYLEVELFORMATS_HXX
25 #define INCLUDED_BASEBMP_GREYLEVELFORMATS_HXX
26 
27 #include <basebmp/color.hxx>
28 #include <basebmp/colortraits.hxx>
29 #include <basebmp/accessor.hxx>
30 #include <basebmp/pixeliterator.hxx>
31 #include <basebmp/packedpixeliterator.hxx>
32 #include <basebmp/pixelformatadapters.hxx>
33 #include <basebmp/metafunctions.hxx>
34 
35 #include <vigra/numerictraits.hxx>
36 #include <vigra/metaprogramming.hxx>
37 
38 #include <functional>
39 
40 namespace basebmp
41 {
42 
43 template< typename PixelType,
44           typename ColorType,
45           int      UsedRange > struct GreylevelGetter :
46         public std::unary_function<PixelType, ColorType>
47 {
operator ()basebmp::GreylevelGetter48     ColorType operator()( PixelType const& c ) const
49     {
50         return ColorTraits<ColorType>::fromGreyscale(
51             vigra::NumericTraits<PixelType>::toPromote(c) *
52             vigra::NumericTraits<PixelType>::maxConst / UsedRange );
53     }
54 };
55 
56 template< typename PixelType,
57           typename ColorType,
58           int      UsedRange > struct GreylevelSetter :
59     public std::unary_function<ColorType, PixelType>
60 {
operator ()basebmp::GreylevelSetter61     PixelType operator()( ColorType const& c ) const
62     {
63         return vigra::NumericTraits<PixelType>::toPromote(
64             ColorTraits<ColorType>::toGreyscale(c)) *
65             UsedRange /
66             vigra::NumericTraits<PixelType>::maxConst;
67     }
68 };
69 
70 //-----------------------------------------------------------------------------
71 
72 template< class Iterator,
73           class Accessor,
74           int   UsedRange > struct PixelFormatTraitsTemplate_Greylevel
75 {
76     typedef typename Iterator::value_type       pixel_type;
77 
78     typedef GreylevelGetter<pixel_type,
79                             Color,
80                             UsedRange>          getter_type;
81     typedef GreylevelSetter<pixel_type,
82                             Color,
83                             UsedRange>          setter_type;
84 
85     typedef Iterator                            iterator_type;
86     typedef Accessor                            raw_accessor_type;
87     typedef AccessorSelector<
88         getter_type,
89         setter_type >                           accessor_selector;
90 };
91 
92 template< int BitsPerPixel,
93           bool MsbFirst > struct PixelFormatTraitsTemplate_PackedGreylevel :
94     public PixelFormatTraitsTemplate_Greylevel<
95                PackedPixelIterator< sal_uInt8,
96                                     BitsPerPixel,
97                                     true >,
98                NonStandardAccessor< sal_uInt8 >,
99                (1UL << BitsPerPixel)-1 >
100 {};
101 
102 //-----------------------------------------------------------------------------
103 
104 // 1bpp MSB
105 typedef PixelFormatTraitsTemplate_PackedGreylevel<1, true> PixelFormatTraits_GREY1_MSB;
106 
107 // 1bpp LSB
108 typedef PixelFormatTraitsTemplate_PackedGreylevel<1, false> PixelFormatTraits_GREY1_LSB;
109 BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_GREY1_MSB::getter_type,
110                                   PixelFormatTraits_GREY1_MSB::setter_type);
111 
112 
113 // 4bpp MSB
114 typedef PixelFormatTraitsTemplate_PackedGreylevel<4, true> PixelFormatTraits_GREY4_MSB;
115 
116 // 4bpp LSB
117 typedef PixelFormatTraitsTemplate_PackedGreylevel<4, false> PixelFormatTraits_GREY4_LSB;
118 BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_GREY4_MSB::getter_type,
119                                   PixelFormatTraits_GREY4_MSB::setter_type);
120 
121 // 8bpp
122 typedef PixelFormatTraitsTemplate_Greylevel<
123     PixelIterator< sal_uInt8 >,
124     StandardAccessor< sal_uInt8 >,
125     255 >                                                   PixelFormatTraits_GREY8;
126 BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_GREY8::getter_type,
127                                   PixelFormatTraits_GREY8::setter_type);
128 
129 } // namespace basebmp
130 
131 #endif /* INCLUDED_BASEBMP_GREYLEVELFORMATS_HXX */
132