Boost GIL


bit_aligned_pixel_reference.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
9 #define BOOST_GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
10 
11 #include <boost/gil/pixel.hpp>
12 #include <boost/gil/channel.hpp>
13 
14 #include <boost/assert.hpp>
15 #include <boost/config.hpp>
16 #include <boost/mpl/accumulate.hpp>
17 #include <boost/mpl/at.hpp>
18 #include <boost/mpl/bool.hpp>
19 #include <boost/mpl/if.hpp>
20 #include <boost/mpl/plus.hpp>
21 #include <boost/mpl/push_back.hpp>
22 #include <boost/mpl/vector.hpp>
23 
24 #include <functional>
25 #include <type_traits>
26 
27 namespace boost { namespace gil {
28 
31 
33 // bit_range
34 //
35 // 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.
37 
38 template <int RangeSize, bool Mutable>
39 class bit_range {
40 public:
41  using byte_t = typename mpl::if_c<Mutable,unsigned char,const unsigned char>::type;
42  using difference_type = std::ptrdiff_t;
43  template <int RS, bool M> friend class bit_range;
44 private:
45  byte_t* _current_byte; // the starting byte of the bit range
46  int _bit_offset; // offset from the beginning of the current byte. 0<=_bit_offset<=7
47 
48 public:
49  bit_range() : _current_byte(nullptr), _bit_offset(0) {}
50  bit_range(byte_t* current_byte, int bit_offset)
51  : _current_byte(current_byte)
52  , _bit_offset(bit_offset)
53  {
54  BOOST_ASSERT(bit_offset >= 0 && bit_offset < 8);
55  }
56 
57  bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
58  template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
59 
60  bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
61  bool operator==(const bit_range& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
62 
63  bit_range& operator++() {
64  _current_byte += (_bit_offset+RangeSize) / 8;
65  _bit_offset = (_bit_offset+RangeSize) % 8;
66  return *this;
67  }
68  bit_range& operator--() { bit_advance(-RangeSize); return *this; }
69 
70  void bit_advance(difference_type num_bits) {
71  int new_offset = int(_bit_offset+num_bits);
72  _current_byte += new_offset / 8;
73  _bit_offset = new_offset % 8;
74  if (_bit_offset<0) {
75  _bit_offset+=8;
76  --_current_byte;
77  }
78  }
79  difference_type bit_distance_to(const bit_range& b) const {
80  return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
81  }
82  byte_t* current_byte() const { return _current_byte; }
83  int bit_offset() const { return _bit_offset; }
84 };
85 
119 template <typename BitField, typename ChannelBitSizes, typename Layout, bool IsMutable>
120 struct bit_aligned_pixel_reference
121 {
122  static constexpr int bit_size =
123  mpl::accumulate
124  <
125  ChannelBitSizes,
126  mpl::int_<0>,
127  mpl::plus<mpl::_1, mpl::_2>
128  >::type::value;
129 
130  using bit_range_t = boost::gil::bit_range<bit_size,IsMutable>;
131  using bitfield_t = BitField;
132  using data_ptr_t =typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type;
133 
134  using layout_t = Layout;
135 
137  using reference = const bit_aligned_pixel_reference<BitField, ChannelBitSizes, Layout, IsMutable>;
138  using const_reference = bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false> const;
139 
140  static constexpr bool is_mutable = IsMutable;
141 
142  bit_aligned_pixel_reference(){}
143  bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {}
144  explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
145  template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
146 
147  // Grayscale references can be constructed from the channel reference
148  explicit bit_aligned_pixel_reference(typename kth_element_type<bit_aligned_pixel_reference,0>::type const channel0)
149  : _bit_range(static_cast<data_ptr_t>(&channel0), channel0.first_bit())
150  {
151  static_assert(num_channels<bit_aligned_pixel_reference>::value == 1, "");
152  }
153 
154  // Construct from another compatible pixel type
155  bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {}
156  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()) {
157  check_compatible<packed_pixel<BF,CR,Layout> >();
158  }
159 
160  const bit_aligned_pixel_reference& operator=(const bit_aligned_pixel_reference& p) const { static_copy(p,*this); return *this; }
161  template <typename P> const bit_aligned_pixel_reference& operator=(const P& p) const { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
162 
163  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
164  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
165 
166  const bit_aligned_pixel_reference* operator->() const { return this; }
167 
168  const bit_range_t& bit_range() const { return _bit_range; }
169 private:
170  mutable bit_range_t _bit_range;
171  template <typename B, typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
172 
173  template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
174 
175  template <typename Pixel> void assign(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); static_copy(p,*this); }
176  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
177 
178 private:
179  static void check_gray()
180  {
181  static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
182  }
183 
184  template <typename Channel> void assign(const Channel& chan, mpl::false_) const { check_gray(); gil::at_c<0>(*this)=chan; }
185  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
186 };
187 
189 // ColorBasedConcept
191 
192 template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K>
193 struct kth_element_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,IsMutable>, K>
194 {
195  using type = packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> const;
196 };
197 
198 template <typename B, typename C, typename L, bool M, int K>
199 struct kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
200  : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
201 
202 template <typename B, typename C, typename L, bool M, int K>
203 struct kth_element_const_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
204  : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
205 
206 
207 namespace detail {
208  // returns sum of IntegralVector[0] ... IntegralVector[K-1]
209  template <typename IntegralVector, int K>
210  struct sum_k : public mpl::plus<sum_k<IntegralVector,K-1>, typename mpl::at_c<IntegralVector,K-1>::type > {};
211 
212  template <typename IntegralVector> struct sum_k<IntegralVector,0> : public mpl::int_<0> {};
213 }
214 
215 // at_c required by MutableColorBaseConcept
216 template <int K, typename BitField, typename ChannelBitSizes, typename L, bool Mutable> inline
217 typename kth_element_reference_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>,K>::type
218 at_c(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>& p)
219 {
220  using pixel_t = bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>;
221  using channel_t = typename kth_element_reference_type<pixel_t,K>::type;
222  using bit_range_t = typename pixel_t::bit_range_t;
223 
224  bit_range_t bit_range(p.bit_range());
225  bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
226 
227  return channel_t(bit_range.current_byte(), bit_range.bit_offset());
228 }
229 
231 // PixelConcept
233 
235 template <typename B, typename C, typename L, bool M>
236 struct is_pixel<bit_aligned_pixel_reference<B,C,L,M> > : public mpl::true_{};
237 
239 // PixelBasedConcept
241 
242 template <typename B, typename C, typename L, bool M>
243 struct color_space_type<bit_aligned_pixel_reference<B,C,L,M> > {
244  using type = typename L::color_space_t;
245 };
246 
247 template <typename B, typename C, typename L, bool M>
248 struct channel_mapping_type<bit_aligned_pixel_reference<B,C,L,M> > {
249  using type = typename L::channel_mapping_t;
250 };
251 
252 template <typename B, typename C, typename L, bool M>
253 struct is_planar<bit_aligned_pixel_reference<B,C,L,M> > : mpl::false_ {};
254 
256 // pixel_reference_type
258 
259 namespace detail {
260  // returns a vector containing K copies of the type T
261  template <unsigned K, typename T> struct k_copies;
262  template <typename T> struct k_copies<0,T> {
263  using type = mpl::vector0<>;
264  };
265  template <unsigned K, typename T> struct k_copies : public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
266 }
267 
268 // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
269 template <typename BitField, int NumBits, typename Layout>
270 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false>
271 {
272 private:
273  using size_t = typename mpl::size<typename Layout::color_space_t>::type;
274  using channel_bit_sizes_t = typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type;
275 public:
276  using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false>;
277 };
278 
279 // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity
280 template <typename BitField, int NumBits, typename Layout>
281 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true>
282 {
283 private:
284  using size_t = typename mpl::size<typename Layout::color_space_t>::type;
285  using channel_bit_sizes_t = typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits>>::type;
286 public:
287  using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true>;
288 };
289 
290 } } // namespace boost::gil
291 
292 namespace std {
293 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
294 // swap with 'left bias':
295 // - swap between proxy and anything
296 // - swap between value type and proxy
297 // - swap between proxy and proxy
298 // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
299 
300 template <typename B, typename C, typename L, typename R> inline
301 void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, R& y) {
302  boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
303 }
304 
305 
306 template <typename B, typename C, typename L> inline
307 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) {
308  boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
309 }
310 
311 
312 template <typename B, typename C, typename L> inline
313 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) {
314  boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
315 }
316 } // namespace std
317 #endif
Definition: algorithm.hpp:30
void swap(const boost::gil::packed_channel_reference< BF, FB, NB, M > x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:485
add_reference< E >::type at_c(detail::homogeneous_color_base< E, L, N > &p)
Provides mutable access to the K-th element, in physical order.
Definition: color_base.hpp:387
Definition: algorithm.hpp:127
Definition: bit_aligned_pixel_reference.hpp:39
Returns the type of a homogeneous pixel reference given the channel type, layout, whether it operates...
Definition: metafunctions.hpp:204
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:929
Heterogeneous pixel value whose channel references can be constructed from the pixel bitfield and the...
Definition: metafunctions.hpp:32
Returns the number of channels of a pixel-based GIL construct.
Definition: locator.hpp:38