]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/gil/bit_aligned_pixel_reference.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / gil / bit_aligned_pixel_reference.hpp
1 /*
2 Copyright 2005-2007 Adobe Systems Incorporated
3
4 Use, modification and distribution are subject to the Boost Software License,
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt).
7
8 See http://stlab.adobe.com/gil for most recent version including documentation.
9 */
10
11 /*************************************************************************************************/
12
13 #ifndef GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
14 #define GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
15
16 ////////////////////////////////////////////////////////////////////////////////////////
17 /// \file
18 /// \brief A model of a heterogeneous pixel that is not byte aligned. Examples are bitmap (1-bit pixels) or 6-bit RGB (222)
19 /// \author Lubomir Bourdev and Hailin Jin \n
20 /// Adobe Systems Incorporated
21 /// \date 2005-2007 \n Last updated on September 28, 2006
22 ///
23 ////////////////////////////////////////////////////////////////////////////////////////
24
25 #include <functional>
26 #include <boost/mpl/accumulate.hpp>
27 #include <boost/mpl/at.hpp>
28 #include <boost/mpl/bool.hpp>
29 #include <boost/mpl/if.hpp>
30 #include <boost/mpl/plus.hpp>
31 #include <boost/mpl/push_back.hpp>
32 #include <boost/mpl/vector.hpp>
33 #include "gil_config.hpp"
34 #include "pixel.hpp"
35 #include "channel.hpp"
36
37 namespace boost { namespace gil {
38
39 /////////////////////////////
40 // bit_range
41 //
42 // Represents a range of bits that can span multiple consecutive bytes. The range has a size fixed at compile time, but the offset is specified at run time.
43 /////////////////////////////
44
45 template <int RangeSize, bool Mutable>
46 class bit_range {
47 public:
48 typedef typename mpl::if_c<Mutable,unsigned char,const unsigned char>::type byte_t;
49 typedef std::ptrdiff_t difference_type;
50 template <int RS, bool M> friend class bit_range;
51 private:
52 byte_t* _current_byte; // the starting byte of the bit range
53 int _bit_offset; // offset from the beginning of the current byte. 0<=_bit_offset<=7
54
55 public:
56 bit_range() : _current_byte(NULL), _bit_offset(0) {}
57 bit_range(byte_t* current_byte, int bit_offset) : _current_byte(current_byte), _bit_offset(bit_offset) { assert(bit_offset>=0 && bit_offset<8); }
58
59 bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
60 template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
61
62 bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
63 bool operator==(const bit_range& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
64
65 bit_range& operator++() {
66 _current_byte += (_bit_offset+RangeSize) / 8;
67 _bit_offset = (_bit_offset+RangeSize) % 8;
68 return *this;
69 }
70 bit_range& operator--() { bit_advance(-RangeSize); return *this; }
71
72 void bit_advance(difference_type num_bits) {
73 int new_offset = int(_bit_offset+num_bits);
74 _current_byte += new_offset / 8;
75 _bit_offset = new_offset % 8;
76 if (_bit_offset<0) {
77 _bit_offset+=8;
78 --_current_byte;
79 }
80 }
81 difference_type bit_distance_to(const bit_range& b) const {
82 return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
83 }
84 byte_t* current_byte() const { return _current_byte; }
85 int bit_offset() const { return _bit_offset; }
86 };
87
88
89 /// \defgroup ColorBaseModelNonAlignedPixel bit_aligned_pixel_reference
90 /// \ingroup ColorBaseModel
91 /// \brief A heterogeneous color base representing pixel that may not be byte aligned, i.e. it may correspond to a bit range that does not start/end at a byte boundary. Models ColorBaseConcept.
92
93 /**
94 \defgroup PixelModelNonAlignedPixel bit_aligned_pixel_reference
95 \ingroup PixelModel
96 \brief A heterogeneous pixel reference used to represent non-byte-aligned pixels. Models PixelConcept
97
98 Example:
99 \code
100 unsigned char data=0;
101
102 // A mutable reference to a 6-bit BGR pixel in "123" format (1 bit for red, 2 bits for green, 3 bits for blue)
103 typedef const bit_aligned_pixel_reference<unsigned char, mpl::vector3_c<int,1,2,3>, rgb_layout_t, true> rgb123_ref_t;
104
105 // create the pixel reference at bit offset 2
106 // (i.e. red = [2], green = [3,4], blue = [5,6,7] bits)
107 rgb123_ref_t ref(&data, 2);
108 get_color(ref, red_t()) = 1;
109 assert(data == 0x04);
110 get_color(ref, green_t()) = 3;
111 assert(data == 0x1C);
112 get_color(ref, blue_t()) = 7;
113 assert(data == 0xFC);
114 \endcode
115 */
116 /// \ingroup ColorBaseModelNonAlignedPixel PixelModelNonAlignedPixel PixelBasedModel
117 /// \brief Heterogeneous pixel reference corresponding to non-byte-aligned bit range. Models ColorBaseConcept, PixelConcept, PixelBasedConcept
118 template <typename BitField,
119 typename ChannelBitSizes, // MPL integral vector defining the number of bits for each channel. For example, for 565RGB, vector_c<int,5,6,5>
120 typename Layout,
121 bool IsMutable>
122 struct bit_aligned_pixel_reference {
123 BOOST_STATIC_CONSTANT(int, bit_size = (mpl::accumulate<ChannelBitSizes, mpl::int_<0>, mpl::plus<mpl::_1, mpl::_2> >::type::value));
124 typedef boost::gil::bit_range<bit_size,IsMutable> bit_range_t;
125 typedef BitField bitfield_t;
126 typedef typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type data_ptr_t;
127
128 typedef Layout layout_t;
129
130 typedef typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type value_type;
131 typedef const bit_aligned_pixel_reference reference;
132 typedef const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false> const_reference;
133
134 BOOST_STATIC_CONSTANT(bool, is_mutable = IsMutable);
135
136 bit_aligned_pixel_reference(){}
137 bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {}
138 explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
139 template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
140
141 // Grayscale references can be constructed from the channel reference
142 explicit bit_aligned_pixel_reference(const typename kth_element_type<bit_aligned_pixel_reference,0>::type channel0) : _bit_range(static_cast<data_ptr_t>(&channel0), channel0.first_bit()) {
143 BOOST_STATIC_ASSERT((num_channels<bit_aligned_pixel_reference>::value==1));
144 }
145
146 // Construct from another compatible pixel type
147 bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {}
148 template <typename BF, typename CR> bit_aligned_pixel_reference(packed_pixel<BF,CR,Layout>& p) : _bit_range(static_cast<data_ptr_t>(&gil::at_c<0>(p)), gil::at_c<0>(p).first_bit()) {
149 check_compatible<packed_pixel<BF,CR,Layout> >();
150 }
151
152 const bit_aligned_pixel_reference& operator=(const bit_aligned_pixel_reference& p) const { static_copy(p,*this); return *this; }
153 template <typename P> const bit_aligned_pixel_reference& operator=(const P& p) const { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
154
155 template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
156 template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
157
158 const bit_aligned_pixel_reference* operator->() const { return this; }
159
160 const bit_range_t& bit_range() const { return _bit_range; }
161 private:
162 mutable bit_range_t _bit_range;
163 template <typename B, typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
164
165 template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
166
167 template <typename Pixel> void assign(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); static_copy(p,*this); }
168 template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
169
170 private:
171 static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
172 template <typename Channel> void assign(const Channel& chan, mpl::false_) const { check_gray(); gil::at_c<0>(*this)=chan; }
173 template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
174 };
175
176 /////////////////////////////
177 // ColorBasedConcept
178 /////////////////////////////
179
180 template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K>
181 struct kth_element_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,IsMutable>, K> {
182 public:
183 typedef const packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> type;
184 };
185
186 template <typename B, typename C, typename L, bool M, int K>
187 struct kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
188 : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
189
190 template <typename B, typename C, typename L, bool M, int K>
191 struct kth_element_const_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
192 : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
193
194
195 namespace detail {
196 // returns sum of IntegralVector[0] ... IntegralVector[K-1]
197 template <typename IntegralVector, int K>
198 struct sum_k : public mpl::plus<sum_k<IntegralVector,K-1>, typename mpl::at_c<IntegralVector,K-1>::type > {};
199
200 template <typename IntegralVector> struct sum_k<IntegralVector,0> : public mpl::int_<0> {};
201 }
202
203 // at_c required by MutableColorBaseConcept
204 template <int K, typename BitField, typename ChannelBitSizes, typename L, bool Mutable> inline
205 typename kth_element_reference_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>,K>::type
206 at_c(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>& p) {
207 typedef bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable> pixel_t;
208 typedef typename kth_element_reference_type<pixel_t,K>::type channel_t;
209 typedef typename pixel_t::bit_range_t bit_range_t;
210
211 bit_range_t bit_range(p.bit_range());
212 bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
213
214 return channel_t(bit_range.current_byte(), bit_range.bit_offset());
215 }
216
217 /////////////////////////////
218 // PixelConcept
219 /////////////////////////////
220
221 /// Metafunction predicate that flags bit_aligned_pixel_reference as a model of PixelConcept. Required by PixelConcept
222 template <typename B, typename C, typename L, bool M>
223 struct is_pixel<bit_aligned_pixel_reference<B,C,L,M> > : public mpl::true_{};
224
225 /////////////////////////////
226 // PixelBasedConcept
227 /////////////////////////////
228
229 template <typename B, typename C, typename L, bool M>
230 struct color_space_type<bit_aligned_pixel_reference<B,C,L,M> > {
231 typedef typename L::color_space_t type;
232 };
233
234 template <typename B, typename C, typename L, bool M>
235 struct channel_mapping_type<bit_aligned_pixel_reference<B,C,L,M> > {
236 typedef typename L::channel_mapping_t type;
237 };
238
239 template <typename B, typename C, typename L, bool M>
240 struct is_planar<bit_aligned_pixel_reference<B,C,L,M> > : mpl::false_ {};
241
242 /////////////////////////////
243 // pixel_reference_type
244 /////////////////////////////
245
246 namespace detail {
247 // returns a vector containing K copies of the type T
248 template <unsigned K, typename T> struct k_copies;
249 template <typename T> struct k_copies<0,T> {
250 typedef mpl::vector0<> type;
251 };
252 template <unsigned K, typename T> struct k_copies : public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
253 }
254
255 // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
256 template <typename BitField, int NumBits, typename Layout>
257 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false> {
258 private:
259 typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
260 typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
261 public:
262 typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false> type;
263 };
264
265 // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity
266 template <typename BitField, int NumBits, typename Layout>
267 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true> {
268 private:
269 typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
270 typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
271 public:
272 typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true> type;
273 };
274
275 } } // namespace boost::gil
276
277 namespace std {
278 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
279 // swap with 'left bias':
280 // - swap between proxy and anything
281 // - swap between value type and proxy
282 // - swap between proxy and proxy
283 // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
284
285 template <typename B, typename C, typename L, typename R> inline
286 void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, R& y) {
287 boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
288 }
289
290
291 template <typename B, typename C, typename L> inline
292 void swap(typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type& x, const boost::gil::bit_aligned_pixel_reference<B,C,L,true> y) {
293 boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
294 }
295
296
297 template <typename B, typename C, typename L> inline
298 void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, const boost::gil::bit_aligned_pixel_reference<B,C,L,true> y) {
299 boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
300 }
301 } // namespace std
302 #endif