Boost GIL


bit_aligned_pixel_reference.hpp
Go to the documentation of this file.
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 
24 
25 #include <functional>
26 
27 #include <boost/config.hpp>
28 #include <boost/mpl/accumulate.hpp>
29 #include <boost/mpl/at.hpp>
30 #include <boost/mpl/bool.hpp>
31 #include <boost/mpl/if.hpp>
32 #include <boost/mpl/plus.hpp>
33 #include <boost/mpl/push_back.hpp>
34 #include <boost/mpl/vector.hpp>
35 
36 #include "gil_config.hpp"
37 #include "pixel.hpp"
38 #include "channel.hpp"
39 
40 namespace boost { namespace gil {
41 
43 // bit_range
44 //
45 // 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.
47 
48 template <int RangeSize, bool Mutable>
49 class bit_range {
50 public:
51  typedef typename mpl::if_c<Mutable,unsigned char,const unsigned char>::type byte_t;
52  typedef std::ptrdiff_t difference_type;
53  template <int RS, bool M> friend class bit_range;
54 private:
55  byte_t* _current_byte; // the starting byte of the bit range
56  int _bit_offset; // offset from the beginning of the current byte. 0<=_bit_offset<=7
57 
58 public:
59  bit_range() : _current_byte(NULL), _bit_offset(0) {}
60  bit_range(byte_t* current_byte, int bit_offset) : _current_byte(current_byte), _bit_offset(bit_offset) { assert(bit_offset>=0 && bit_offset<8); }
61 
62  bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
63  template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
64 
65  bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
66  bool operator==(const bit_range& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
67 
68  bit_range& operator++() {
69  _current_byte += (_bit_offset+RangeSize) / 8;
70  _bit_offset = (_bit_offset+RangeSize) % 8;
71  return *this;
72  }
73  bit_range& operator--() { bit_advance(-RangeSize); return *this; }
74 
75  void bit_advance(difference_type num_bits) {
76  int new_offset = int(_bit_offset+num_bits);
77  _current_byte += new_offset / 8;
78  _bit_offset = new_offset % 8;
79  if (_bit_offset<0) {
80  _bit_offset+=8;
81  --_current_byte;
82  }
83  }
84  difference_type bit_distance_to(const bit_range& b) const {
85  return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
86  }
87  byte_t* current_byte() const { return _current_byte; }
88  int bit_offset() const { return _bit_offset; }
89 };
90 
91 
95 
119 template <typename BitField,
122  typename ChannelBitSizes, // MPL integral vector defining the number of bits for each channel. For example, for 565RGB, vector_c<int,5,6,5>
123  typename Layout,
124  bool IsMutable>
126  BOOST_STATIC_CONSTANT(int, bit_size = (mpl::accumulate<ChannelBitSizes, mpl::int_<0>, mpl::plus<mpl::_1, mpl::_2> >::type::value));
127  typedef boost::gil::bit_range<bit_size,IsMutable> bit_range_t;
128  typedef BitField bitfield_t;
129  typedef typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type data_ptr_t;
130 
131  typedef Layout layout_t;
132 
136 
137  BOOST_STATIC_CONSTANT(bool, is_mutable = IsMutable);
138 
140  bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {}
141  explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
142  template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
143 
144  // Grayscale references can be constructed from the channel reference
145  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()) {
146  BOOST_STATIC_ASSERT((num_channels<bit_aligned_pixel_reference>::value==1));
147  }
148 
149  // Construct from another compatible pixel type
150  bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {}
151  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()) {
152  check_compatible<packed_pixel<BF,CR,Layout> >();
153  }
154 
155  const bit_aligned_pixel_reference& operator=(const bit_aligned_pixel_reference& p) const { static_copy(p,*this); return *this; }
156  template <typename P> const bit_aligned_pixel_reference& operator=(const P& p) const { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
157 
158  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
159  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
160 
161  const bit_aligned_pixel_reference* operator->() const { return this; }
162 
163  const bit_range_t& bit_range() const { return _bit_range; }
164 private:
165  mutable bit_range_t _bit_range;
166  template <typename B, typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
167 
168  template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
169 
170  template <typename Pixel> void assign(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); static_copy(p,*this); }
171  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
172 
173 private:
174  static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
175  template <typename Channel> void assign(const Channel& chan, mpl::false_) const { check_gray(); gil::at_c<0>(*this)=chan; }
176  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
177 };
178 
180 // ColorBasedConcept
182 
183 template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K>
184 struct kth_element_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,IsMutable>, K> {
185 public:
186  typedef const packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> type;
187 };
188 
189 template <typename B, typename C, typename L, bool M, int K>
190 struct kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
191  : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
192 
193 template <typename B, typename C, typename L, bool M, int K>
194 struct kth_element_const_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
195  : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
196 
197 
198 namespace detail {
199  // returns sum of IntegralVector[0] ... IntegralVector[K-1]
200  template <typename IntegralVector, int K>
201  struct sum_k : public mpl::plus<sum_k<IntegralVector,K-1>, typename mpl::at_c<IntegralVector,K-1>::type > {};
202 
203  template <typename IntegralVector> struct sum_k<IntegralVector,0> : public mpl::int_<0> {};
204 }
205 
206 // at_c required by MutableColorBaseConcept
207 template <int K, typename BitField, typename ChannelBitSizes, typename L, bool Mutable> inline
208 typename kth_element_reference_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>,K>::type
209 at_c(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>& p) {
210  typedef bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable> pixel_t;
211  typedef typename kth_element_reference_type<pixel_t,K>::type channel_t;
212  typedef typename pixel_t::bit_range_t bit_range_t;
213 
214  bit_range_t bit_range(p.bit_range());
215  bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
216 
217  return channel_t(bit_range.current_byte(), bit_range.bit_offset());
218 }
219 
221 // PixelConcept
223 
225 template <typename B, typename C, typename L, bool M>
226 struct is_pixel<bit_aligned_pixel_reference<B,C,L,M> > : public mpl::true_{};
227 
229 // PixelBasedConcept
231 
232 template <typename B, typename C, typename L, bool M>
233 struct color_space_type<bit_aligned_pixel_reference<B,C,L,M> > {
234  typedef typename L::color_space_t type;
235 };
236 
237 template <typename B, typename C, typename L, bool M>
238 struct channel_mapping_type<bit_aligned_pixel_reference<B,C,L,M> > {
239  typedef typename L::channel_mapping_t type;
240 };
241 
242 template <typename B, typename C, typename L, bool M>
243 struct is_planar<bit_aligned_pixel_reference<B,C,L,M> > : mpl::false_ {};
244 
246 // pixel_reference_type
248 
249 namespace detail {
250  // returns a vector containing K copies of the type T
251  template <unsigned K, typename T> struct k_copies;
252  template <typename T> struct k_copies<0,T> {
253  typedef mpl::vector0<> type;
254  };
255  template <unsigned K, typename T> struct k_copies : public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
256 }
257 
258 // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
259 template <typename BitField, int NumBits, typename Layout>
260 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false> {
261 private:
262  typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
263  typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
264 public:
265  typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false> type;
266 };
267 
268 // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity
269 template <typename BitField, int NumBits, typename Layout>
270 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true> {
271 private:
272  typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
273  typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
274 public:
275  typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true> type;
276 };
277 
278 } } // namespace boost::gil
279 
280 namespace std {
281 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
282 // swap with 'left bias':
283 // - swap between proxy and anything
284 // - swap between value type and proxy
285 // - swap between proxy and proxy
286 // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
287 
288 template <typename B, typename C, typename L, typename R> inline
289 void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, R& y) {
290  boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
291 }
292 
293 
294 template <typename B, typename C, typename L> inline
296  boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
297 }
298 
299 
300 template <typename B, typename C, typename L> inline
302  boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
303 }
304 } // namespace std
305 #endif
Channel utilities.
Heterogeneous pixel reference corresponding to non-byte-aligned bit range. Models ColorBaseConcept...
Definition: bit_aligned_pixel_reference.hpp:125
BOOST_FORCEINLINE bool equal(boost::gil::iterator_from_2d< Loc1 > first, boost::gil::iterator_from_2d< Loc1 > last, boost::gil::iterator_from_2d< Loc2 > first2)
std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:934
GIL configuration file.
Heterogeneous pixel value whose channel references can be constructed from the pixel bitfield and the...
Definition: gil_concept.hpp:83
pixel class and related utilities
Returns the number of channels of a pixel-based GIL construct.
Definition: gil_concept.hpp:66