update dev300-m58
[ooovba.git] / basebmp / inc / basebmp / paletteimageaccessor.hxx
blobaf63ce0a5aa2091a0322ea7d3288a32d6b9c5517
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: paletteimageaccessor.hxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX
32 #define INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX
34 #include <basebmp/colortraits.hxx>
35 #include <basebmp/accessortraits.hxx>
37 #include <vigra/numerictraits.hxx>
38 #include <vigra/metaprogramming.hxx>
40 #include <algorithm>
41 #include <functional>
43 namespace basebmp
46 /** Access pixel data via palette indirection
48 @tpl Accessor
49 Raw accessor, to be used to actually access the pixel values
51 @tpl ColorType
52 The color value type to use - e.g. the palette is an array of that
53 type
55 template< class Accessor, typename ColorType > class PaletteImageAccessor
57 public:
58 typedef typename Accessor::value_type data_type;
59 typedef ColorType value_type;
61 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
62 // making all members public, if no member template friends
63 private:
64 template<class A, typename C> friend class PaletteImageAccessor;
65 #endif
67 Accessor maAccessor;
68 const value_type* mpPalette;
69 std::size_t mnNumEntries;
71 public:
72 PaletteImageAccessor() :
73 maAccessor(),
74 mpPalette(0),
75 mnNumEntries(0)
78 template< class A > explicit
79 PaletteImageAccessor( PaletteImageAccessor<A,ColorType> const& rSrc ) :
80 maAccessor( rSrc.maAccessor ),
81 mpPalette( rSrc.mpPalette ),
82 mnNumEntries( rSrc.mnNumEntries )
85 PaletteImageAccessor( const value_type* pPalette,
86 std::size_t numEntries ) :
87 maAccessor(),
88 mpPalette(pPalette),
89 mnNumEntries(numEntries)
92 template< class T > PaletteImageAccessor( T accessor,
93 const value_type* pPalette,
94 std::size_t numEntries ) :
95 maAccessor(accessor),
96 mpPalette(pPalette),
97 mnNumEntries(numEntries)
100 // -------------------------------------------------------
102 Accessor const& getWrappedAccessor() const { return maAccessor; }
103 Accessor& getWrappedAccessor() { return maAccessor; }
105 // -------------------------------------------------------
107 data_type lookup(value_type const& v) const
109 // TODO(P3): use table-based/octree approach here!
110 const value_type* best_entry;
111 const value_type* palette_end( mpPalette+mnNumEntries );
112 if( (best_entry=std::find( mpPalette, palette_end, v)) != palette_end )
113 return best_entry-mpPalette;
115 const value_type* curr_entry( mpPalette );
116 best_entry = curr_entry;
117 while( curr_entry != palette_end )
119 if( ColorTraits<value_type>::distance(*curr_entry,
120 *best_entry)
121 > ColorTraits<value_type>::distance(*curr_entry,
122 v) )
124 best_entry = curr_entry;
127 ++curr_entry;
130 return best_entry-mpPalette;
133 // -------------------------------------------------------
135 template< class Iterator >
136 value_type operator()(Iterator const& i) const
138 return mpPalette[ maAccessor(i) ];
141 template< class Iterator, class Difference >
142 value_type operator()(Iterator const& i, Difference const& diff) const
144 return mpPalette[ maAccessor(i,diff) ];
147 // -------------------------------------------------------
149 template< typename V, class Iterator >
150 void set(V const& value, Iterator const& i) const
152 maAccessor.set(
153 lookup(
154 vigra::detail::RequiresExplicitCast<value_type>::cast(value) ),
155 i );
158 template< typename V, class Iterator, class Difference >
159 void set(V const& value, Iterator const& i, Difference const& diff) const
161 maAccessor.set(
162 lookup(
163 vigra::detail::RequiresExplicitCast<value_type>::cast(value) ),
165 diff );
169 } // namespace basebmp
171 #endif /* INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX */