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_PALETTEIMAGEACCESSOR_HXX
21 : #define INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX
22 :
23 : #include <basebmp/colortraits.hxx>
24 : #include <basebmp/accessortraits.hxx>
25 :
26 : #include <vigra/numerictraits.hxx>
27 : #include <vigra/metaprogramming.hxx>
28 :
29 : #include <algorithm>
30 : #include <functional>
31 :
32 : namespace basebmp
33 : {
34 :
35 : /** Access pixel data via palette indirection
36 :
37 : @tpl Accessor
38 : Raw accessor, to be used to actually access the pixel values
39 :
40 : @tpl ColorType
41 : The color value type to use - e.g. the palette is an array of that
42 : type
43 : */
44 : template< class Accessor, typename ColorType > class PaletteImageAccessor
45 : {
46 : public:
47 : typedef typename Accessor::value_type data_type;
48 : typedef ColorType value_type;
49 :
50 : #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
51 : // making all members public, if no member template friends
52 : private:
53 : template<class A, typename C> friend class PaletteImageAccessor;
54 : #endif
55 :
56 : Accessor maAccessor;
57 : const value_type* mpPalette;
58 : std::size_t mnNumEntries;
59 :
60 : public:
61 : PaletteImageAccessor() :
62 : maAccessor(),
63 : mpPalette(0),
64 : mnNumEntries(0)
65 : {}
66 :
67 : template< class A > explicit
68 551586 : PaletteImageAccessor( PaletteImageAccessor<A,ColorType> const& rSrc ) :
69 : maAccessor( rSrc.maAccessor ),
70 : mpPalette( rSrc.mpPalette ),
71 551586 : mnNumEntries( rSrc.mnNumEntries )
72 551586 : {}
73 :
74 183862 : PaletteImageAccessor( const value_type* pPalette,
75 : std::size_t numEntries ) :
76 : maAccessor(),
77 : mpPalette(pPalette),
78 183862 : mnNumEntries(numEntries)
79 183862 : {}
80 :
81 : template< class T > PaletteImageAccessor( T accessor,
82 : const value_type* pPalette,
83 : std::size_t numEntries ) :
84 : maAccessor(accessor),
85 : mpPalette(pPalette),
86 : mnNumEntries(numEntries)
87 : {}
88 :
89 :
90 :
91 : Accessor const& getWrappedAccessor() const { return maAccessor; }
92 : Accessor& getWrappedAccessor() { return maAccessor; }
93 :
94 :
95 :
96 22288 : data_type lookup(value_type const& v) const
97 : {
98 : // TODO(P3): use table-based/octree approach here!
99 : const value_type* best_entry;
100 22288 : const value_type* palette_end( mpPalette+mnNumEntries );
101 22288 : if( (best_entry=std::find( mpPalette, palette_end, v)) != palette_end )
102 22121 : return best_entry-mpPalette;
103 :
104 167 : const value_type* curr_entry( mpPalette );
105 167 : best_entry = curr_entry;
106 668 : while( curr_entry != palette_end )
107 : {
108 334 : if( ColorTraits<value_type>::distance(*curr_entry,
109 334 : *best_entry)
110 : > ColorTraits<value_type>::distance(*curr_entry,
111 334 : v) )
112 : {
113 167 : best_entry = curr_entry;
114 : }
115 :
116 334 : ++curr_entry;
117 : }
118 :
119 167 : return best_entry-mpPalette;
120 : }
121 :
122 :
123 :
124 : template< class Iterator >
125 65584552 : value_type operator()(Iterator const& i) const
126 : {
127 65584552 : return mpPalette[ maAccessor(i) ];
128 : }
129 :
130 : template< class Iterator, class Difference >
131 : value_type operator()(Iterator const& i, Difference const& diff) const
132 : {
133 : return mpPalette[ maAccessor(i,diff) ];
134 : }
135 :
136 :
137 :
138 : template< typename V, class Iterator >
139 476 : void set(V const& value, Iterator const& i) const
140 : {
141 476 : maAccessor.set(
142 : lookup(
143 476 : vigra::detail::RequiresExplicitCast<value_type>::cast(value) ),
144 : i );
145 476 : }
146 :
147 : template< typename V, class Iterator, class Difference >
148 : void set(V const& value, Iterator const& i, Difference const& diff) const
149 : {
150 : maAccessor.set(
151 : lookup(
152 : vigra::detail::RequiresExplicitCast<value_type>::cast(value) ),
153 : i,
154 : diff );
155 : }
156 : };
157 :
158 : } // namespace basebmp
159 :
160 : #endif /* INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX */
161 :
162 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|