Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_BASEBMP_GREYLEVELFORMATS_HXX
21 : #define INCLUDED_BASEBMP_GREYLEVELFORMATS_HXX
22 :
23 : #include <basebmp/color.hxx>
24 : #include <basebmp/colortraits.hxx>
25 : #include <basebmp/accessor.hxx>
26 : #include <basebmp/pixeliterator.hxx>
27 : #include <basebmp/packedpixeliterator.hxx>
28 : #include <basebmp/pixelformatadapters.hxx>
29 : #include <basebmp/metafunctions.hxx>
30 :
31 : #include <vigra/numerictraits.hxx>
32 : #include <vigra/metaprogramming.hxx>
33 :
34 : #include <functional>
35 :
36 : namespace basebmp
37 : {
38 :
39 : template< typename PixelType,
40 : typename ColorType,
41 : int UsedRange > struct GreylevelGetter :
42 : public std::unary_function<PixelType, ColorType>
43 : {
44 3 : ColorType operator()( PixelType const& c ) const
45 : {
46 : return ColorTraits<ColorType>::fromGreyscale(
47 3 : vigra::NumericTraits<PixelType>::toPromote(c) *
48 3 : vigra::NumericTraits<PixelType>::maxConst / UsedRange );
49 : }
50 : };
51 :
52 : template< typename PixelType,
53 : typename ColorType,
54 : int UsedRange > struct GreylevelSetter :
55 : public std::unary_function<ColorType, PixelType>
56 : {
57 60494 : PixelType operator()( ColorType const& c ) const
58 : {
59 : return vigra::NumericTraits<PixelType>::toPromote(
60 60494 : ColorTraits<ColorType>::toGreyscale(c)) *
61 : UsedRange /
62 60494 : vigra::NumericTraits<PixelType>::maxConst;
63 : }
64 : };
65 :
66 :
67 :
68 : template< class Iterator,
69 : class Accessor,
70 : int UsedRange > struct PixelFormatTraitsTemplate_Greylevel
71 : {
72 : typedef typename Iterator::value_type pixel_type;
73 :
74 : typedef GreylevelGetter<pixel_type,
75 : Color,
76 : UsedRange> getter_type;
77 : typedef GreylevelSetter<pixel_type,
78 : Color,
79 : UsedRange> setter_type;
80 :
81 : typedef Iterator iterator_type;
82 : typedef Accessor raw_accessor_type;
83 : typedef AccessorSelector<
84 : getter_type,
85 : setter_type > accessor_selector;
86 : };
87 :
88 : template< int BitsPerPixel,
89 : bool MsbFirst > struct PixelFormatTraitsTemplate_PackedGreylevel :
90 : public PixelFormatTraitsTemplate_Greylevel<
91 : PackedPixelIterator< sal_uInt8,
92 : BitsPerPixel,
93 : true >,
94 : NonStandardAccessor< sal_uInt8 >,
95 : (1UL << BitsPerPixel)-1 >
96 : {};
97 :
98 :
99 :
100 : // 1bpp MSB
101 : typedef PixelFormatTraitsTemplate_PackedGreylevel<1, true> PixelFormatTraits_GREY1_MSB;
102 :
103 : // 1bpp LSB
104 : typedef PixelFormatTraitsTemplate_PackedGreylevel<1, false> PixelFormatTraits_GREY1_LSB;
105 : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_GREY1_MSB::getter_type,
106 : PixelFormatTraits_GREY1_MSB::setter_type);
107 :
108 :
109 : // 4bpp MSB
110 : typedef PixelFormatTraitsTemplate_PackedGreylevel<4, true> PixelFormatTraits_GREY4_MSB;
111 :
112 : // 4bpp LSB
113 : typedef PixelFormatTraitsTemplate_PackedGreylevel<4, false> PixelFormatTraits_GREY4_LSB;
114 : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_GREY4_MSB::getter_type,
115 : PixelFormatTraits_GREY4_MSB::setter_type);
116 :
117 : // 8bpp
118 : typedef PixelFormatTraitsTemplate_Greylevel<
119 : PixelIterator< sal_uInt8 >,
120 : StandardAccessor< sal_uInt8 >,
121 : 255 > PixelFormatTraits_GREY8;
122 : BASEBMP_SPECIALIZE_ACCESSORTRAITS(PixelFormatTraits_GREY8::getter_type,
123 : PixelFormatTraits_GREY8::setter_type);
124 :
125 : } // namespace basebmp
126 :
127 : #endif /* INCLUDED_BASEBMP_GREYLEVELFORMATS_HXX */
128 :
129 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|