13 #ifndef GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
14 #define GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
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>
40 namespace boost {
namespace gil {
48 template <
int RangeSize,
bool Mutable>
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;
55 byte_t* _current_byte;
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); }
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) {}
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; }
68 bit_range& operator++() {
69 _current_byte += (_bit_offset+RangeSize) / 8;
70 _bit_offset = (_bit_offset+RangeSize) % 8;
73 bit_range& operator--() { bit_advance(-RangeSize);
return *
this; }
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;
84 difference_type bit_distance_to(
const bit_range& b)
const {
85 return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
87 byte_t* current_byte()
const {
return _current_byte; }
88 int bit_offset()
const {
return _bit_offset; }
119 template <
typename BitField,
122 typename ChannelBitSizes,
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;
131 typedef Layout layout_t;
137 BOOST_STATIC_CONSTANT(
bool, is_mutable = IsMutable);
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()) {
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> >();
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; }
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); }
161 const bit_aligned_pixel_reference* operator->()
const {
return this; }
163 const bit_range_t& bit_range()
const {
return _bit_range; }
165 mutable bit_range_t _bit_range;
166 template <
typename B,
typename C,
typename L,
bool M>
friend struct bit_aligned_pixel_reference;
168 template <
typename Pixel>
static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
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); }
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; }
183 template <
typename BitField,
typename ChannelBitSizes,
typename L,
bool IsMutable,
int K>
186 typedef const packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> type;
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> {};
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> {};
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 > {};
203 template <
typename IntegralVector>
struct sum_k<IntegralVector,0> :
public mpl::int_<0> {};
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;
214 bit_range_t bit_range(p.bit_range());
215 bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
217 return channel_t(bit_range.current_byte(), bit_range.bit_offset());
225 template <
typename B,
typename C,
typename L,
bool M>
232 template <
typename B,
typename C,
typename L,
bool M>
234 typedef typename L::color_space_t type;
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;
242 template <
typename B,
typename C,
typename L,
bool M>
243 struct is_planar<bit_aligned_pixel_reference<B,C,L,M> > : mpl::false_ {};
251 template <
unsigned K,
typename T>
struct k_copies;
252 template <
typename T>
struct k_copies<0,T> {
253 typedef mpl::vector0<> type;
255 template <
unsigned K,
typename T>
struct k_copies :
public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
259 template <
typename BitField,
int NumBits,
typename Layout>
260 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false> {
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;
265 typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false> type;
269 template <
typename BitField,
int NumBits,
typename Layout>
270 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true> {
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;
275 typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true> type;
288 template <
typename B,
typename C,
typename L,
typename R>
inline
290 boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
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);
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);
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
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