fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / include / basebmp / paletteimageaccessor.hxx
blob6f0164e64af6559f1b791bf4c869ba7dd6a1ec82
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
29 #include <algorithm>
30 #include <functional>
32 namespace basebmp
35 /** Access pixel data via palette indirection
37 @tpl Accessor
38 Raw accessor, to be used to actually access the pixel values
40 @tpl ColorType
41 The color value type to use - e.g. the palette is an array of that
42 type
44 template< class Accessor, typename ColorType > class PaletteImageAccessor
46 public:
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
52 private:
53 template<class A, typename C> friend class PaletteImageAccessor;
54 #endif
56 Accessor maAccessor;
57 const value_type* mpPalette;
58 std::size_t mnNumEntries;
60 public:
61 PaletteImageAccessor() :
62 maAccessor(),
63 mpPalette(0),
64 mnNumEntries(0)
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 ) :
76 maAccessor(),
77 mpPalette(pPalette),
78 mnNumEntries(numEntries)
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)
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,
109 *best_entry)
110 > ColorTraits<value_type>::distance(*curr_entry,
111 v) )
113 best_entry = curr_entry;
116 ++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
141 maAccessor.set(
142 lookup(
143 vigra::detail::RequiresExplicitCast<value_type>::cast(value) ),
144 i );
147 template< typename V, class Iterator, class Difference >
148 void set(V const& value, Iterator const& i, Difference const& diff) const
150 maAccessor.set(
151 lookup(
152 vigra::detail::RequiresExplicitCast<value_type>::cast(value) ),
154 diff );
158 } // namespace basebmp
160 #endif /* INCLUDED_BASEBMP_PALETTEIMAGEACCESSOR_HXX */
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */