1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX
21 #define INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX
23 #include <basebmp/colortraits.hxx>
24 #include <basebmp/accessortraits.hxx>
26 #include <vigra/numerictraits.hxx>
27 #include <vigra/metaprogramming.hxx>
35 /** Access pixel data via palette indirection
38 Raw accessor, to be used to actually access the pixel values
41 The color value type to use - e.g. the palette is an array of that
44 template< class Accessor
, typename ColorType
> class PaletteImageAccessor
47 typedef typename
Accessor::value_type data_type
;
48 typedef ColorType value_type
;
50 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
51 // making all members public, if no member template friends
53 template<class A
, typename C
> friend class PaletteImageAccessor
;
57 const value_type
* mpPalette
;
58 std::size_t mnNumEntries
;
61 PaletteImageAccessor() :
67 template< class A
> explicit
68 PaletteImageAccessor( PaletteImageAccessor
<A
,ColorType
> const& rSrc
) :
69 maAccessor( rSrc
.maAccessor
),
70 mpPalette( rSrc
.mpPalette
),
71 mnNumEntries( rSrc
.mnNumEntries
)
74 PaletteImageAccessor( const value_type
* pPalette
,
75 std::size_t numEntries
) :
78 mnNumEntries(numEntries
)
81 template< class T
> PaletteImageAccessor( T accessor
,
82 const value_type
* pPalette
,
83 std::size_t numEntries
) :
86 mnNumEntries(numEntries
)
89 // -------------------------------------------------------
91 Accessor
const& getWrappedAccessor() const { return maAccessor
; }
92 Accessor
& getWrappedAccessor() { return maAccessor
; }
94 // -------------------------------------------------------
96 data_type
lookup(value_type
const& v
) const
98 // TODO(P3): use table-based/octree approach here!
99 const value_type
* best_entry
;
100 const value_type
* palette_end( mpPalette
+mnNumEntries
);
101 if( (best_entry
=std::find( mpPalette
, palette_end
, v
)) != palette_end
)
102 return best_entry
-mpPalette
;
104 const value_type
* curr_entry( mpPalette
);
105 best_entry
= curr_entry
;
106 while( curr_entry
!= palette_end
)
108 if( ColorTraits
<value_type
>::distance(*curr_entry
,
110 > ColorTraits
<value_type
>::distance(*curr_entry
,
113 best_entry
= curr_entry
;
119 return best_entry
-mpPalette
;
122 // -------------------------------------------------------
124 template< class Iterator
>
125 value_type
operator()(Iterator
const& i
) const
127 return mpPalette
[ maAccessor(i
) ];
130 template< class Iterator
, class Difference
>
131 value_type
operator()(Iterator
const& i
, Difference
const& diff
) const
133 return mpPalette
[ maAccessor(i
,diff
) ];
136 // -------------------------------------------------------
138 template< typename V
, class Iterator
>
139 void set(V
const& value
, Iterator
const& i
) const
143 vigra::detail::RequiresExplicitCast
<value_type
>::cast(value
) ),
147 template< typename V
, class Iterator
, class Difference
>
148 void set(V
const& value
, Iterator
const& i
, Difference
const& diff
) const
152 vigra::detail::RequiresExplicitCast
<value_type
>::cast(value
) ),
158 } // namespace basebmp
160 #endif /* INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX */
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */