1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: paletteimageaccessor.hxx,v $
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>
46 /** Access pixel data via palette indirection
49 Raw accessor, to be used to actually access the pixel values
52 The color value type to use - e.g. the palette is an array of that
55 template< class Accessor
, typename ColorType
> class PaletteImageAccessor
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
64 template<class A
, typename C
> friend class PaletteImageAccessor
;
68 const value_type
* mpPalette
;
69 std::size_t mnNumEntries
;
72 PaletteImageAccessor() :
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
) :
89 mnNumEntries(numEntries
)
92 template< class T
> PaletteImageAccessor( T accessor
,
93 const value_type
* pPalette
,
94 std::size_t numEntries
) :
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
,
121 > ColorTraits
<value_type
>::distance(*curr_entry
,
124 best_entry
= 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
154 vigra::detail::RequiresExplicitCast
<value_type
>::cast(value
) ),
158 template< typename V
, class Iterator
, class Difference
>
159 void set(V
const& value
, Iterator
const& i
, Difference
const& diff
) const
163 vigra::detail::RequiresExplicitCast
<value_type
>::cast(value
) ),
169 } // namespace basebmp
171 #endif /* INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX */