8 #ifndef BOOST_GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP 9 #define BOOST_GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP 11 #include <boost/gil/pixel.hpp> 12 #include <boost/gil/channel.hpp> 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> 25 #include <type_traits> 27 namespace boost {
namespace gil {
38 template <
int RangeSize,
bool Mutable>
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;
45 byte_t* _current_byte;
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)
54 BOOST_ASSERT(bit_offset >= 0 && bit_offset < 8);
57 bit_range(
const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
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; }
63 bit_range& operator++() {
64 _current_byte += (_bit_offset+RangeSize) / 8;
65 _bit_offset = (_bit_offset+RangeSize) % 8;
68 bit_range& operator--() { bit_advance(-RangeSize);
return *
this; }
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;
79 difference_type bit_distance_to(
const bit_range& b)
const {
80 return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
82 byte_t* current_byte()
const {
return _current_byte; }
83 int bit_offset()
const {
return _bit_offset; }
119 template <
typename BitField,
typename ChannelBitSizes,
typename Layout,
bool IsMutable>
120 struct bit_aligned_pixel_reference
122 static constexpr
int bit_size =
127 mpl::plus<mpl::_1, mpl::_2>
131 using bitfield_t = BitField;
132 using data_ptr_t =
typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type;
134 using layout_t = Layout;
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;
140 static constexpr
bool is_mutable = IsMutable;
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) {}
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())
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> >();
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; }
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); }
166 const bit_aligned_pixel_reference* operator->()
const {
return this; }
168 const bit_range_t& bit_range()
const {
return _bit_range; }
170 mutable bit_range_t _bit_range;
171 template <
typename B,
typename C,
typename L,
bool M>
friend struct bit_aligned_pixel_reference;
173 template <
typename Pixel>
static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
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); }
179 static void check_gray()
181 static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value,
"");
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; }
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>
195 using type = packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable>
const;
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> {};
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> {};
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 > {};
212 template <
typename IntegralVector>
struct sum_k<IntegralVector,0> :
public mpl::int_<0> {};
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)
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;
225 bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
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_{};
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;
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;
252 template <
typename B,
typename C,
typename L,
bool M>
253 struct is_planar<bit_aligned_pixel_reference<B,C,L,M> > : mpl::false_ {};
261 template <
unsigned K,
typename T>
struct k_copies;
262 template <
typename T>
struct k_copies<0,T> {
263 using type = mpl::vector0<>;
265 template <
unsigned K,
typename T>
struct k_copies :
public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
269 template <
typename BitField,
int NumBits,
typename Layout>
270 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false>
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;
276 using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false>;
280 template <
typename BitField,
int NumBits,
typename Layout>
281 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true>
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;
287 using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true>;
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);
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);
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);
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