annotate DEPENDENCIES/generic/include/boost/gil/bit_aligned_pixel_reference.hpp @ 133:4acb5d8d80b6 tip

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