bit_aligned_pixel_reference.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003    
00004     Use, modification and distribution are subject to the Boost Software License,
00005     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
00006     http://www.boost.org/LICENSE_1_0.txt).
00007 
00008     See http://stlab.adobe.com/gil for most recent version including documentation.
00009 */
00010 
00011 /*************************************************************************************************/
00012 
00013 #ifndef GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
00014 #define GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
00015 
00024 
00025 #include <functional>
00026 #include <boost/mpl/accumulate.hpp>
00027 #include <boost/mpl/at.hpp>
00028 #include <boost/mpl/bool.hpp>
00029 #include <boost/mpl/if.hpp>
00030 #include <boost/mpl/plus.hpp>
00031 #include <boost/mpl/push_back.hpp>
00032 #include <boost/mpl/vector.hpp>
00033 #include "gil_config.hpp"
00034 #include "pixel.hpp"
00035 #include "channel.hpp"
00036 
00037 namespace boost { namespace gil {
00038 
00040 //  bit_range
00041 //
00042 //  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.
00044  
00045 template <int RangeSize, bool Mutable>
00046 class bit_range {
00047 public:
00048     typedef typename mpl::if_c<Mutable,unsigned char,const unsigned char>::type byte_t;
00049     typedef std::ptrdiff_t difference_type;
00050     template <int RS, bool M> friend class bit_range;
00051 private:
00052     byte_t* _current_byte;   // the starting byte of the bit range
00053     int     _bit_offset;     // offset from the beginning of the current byte. 0<=_bit_offset<=7
00054 
00055 public:
00056     bit_range() : _current_byte(NULL), _bit_offset(0) {}
00057     bit_range(byte_t* current_byte, int bit_offset) : _current_byte(current_byte), _bit_offset(bit_offset) { assert(bit_offset>=0 && bit_offset<8); } 
00058 
00059     bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
00060     template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
00061 
00062     bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
00063     bool operator==(const bit_range& br) const { return  _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
00064 
00065     bit_range& operator++() {
00066         _current_byte += (_bit_offset+RangeSize) / 8;
00067         _bit_offset    = (_bit_offset+RangeSize) % 8;
00068         return *this;
00069     }
00070     bit_range& operator--() { bit_advance(-RangeSize); return *this; }
00071 
00072     void bit_advance(difference_type num_bits) {
00073         int new_offset = int(_bit_offset+num_bits);
00074         _current_byte += new_offset / 8;
00075         _bit_offset    = new_offset % 8;
00076         if (_bit_offset<0) {
00077             _bit_offset+=8;
00078             --_current_byte;
00079         }
00080     }
00081     difference_type bit_distance_to(const bit_range& b) const {
00082         return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
00083     }
00084     byte_t* current_byte() const { return _current_byte; }
00085     int     bit_offset()   const { return _bit_offset; }
00086 };
00087 
00088 
00092 
00116 
00117 
00118 template <typename BitField,
00119           typename ChannelBitSizes,  // MPL integral vector defining the number of bits for each channel. For example, for 565RGB, vector_c<int,5,6,5>
00120           typename Layout, 
00121           bool IsMutable>
00122 struct bit_aligned_pixel_reference {
00123     BOOST_STATIC_CONSTANT(int, bit_size = (mpl::accumulate<ChannelBitSizes, mpl::int_<0>, mpl::plus<mpl::_1, mpl::_2> >::type::value));
00124     typedef boost::gil::bit_range<bit_size,IsMutable>                                           bit_range_t;
00125     typedef BitField                                                                bitfield_t;  
00126     typedef typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type data_ptr_t;
00127 
00128     typedef Layout layout_t;
00129 
00130     typedef typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type       value_type;
00131     typedef const bit_aligned_pixel_reference                                         reference;
00132     typedef const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false>  const_reference;
00133 
00134     BOOST_STATIC_CONSTANT(bool, is_mutable = IsMutable);
00135 
00136     bit_aligned_pixel_reference(){}
00137     bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset)   : _bit_range(data_ptr, bit_offset) {}
00138     explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
00139     template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
00140 
00141     // Grayscale references can be constructed from the channel reference
00142     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()) {
00143         BOOST_STATIC_ASSERT((num_channels<bit_aligned_pixel_reference>::value==1));
00144     }
00145 
00146     // Construct from another compatible pixel type
00147     bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {}
00148     template <typename BF, typename CR> bit_aligned_pixel_reference(packed_pixel<BF,CR,Layout>& p) : _bit_range(static_cast<data_ptr_t>(&at_c<0>(p)), at_c<0>(p).first_bit()) {
00149         check_compatible<packed_pixel<BF,CR,Layout> >();
00150     }
00151 
00152     const bit_aligned_pixel_reference& operator=(const bit_aligned_pixel_reference& p) const { static_copy(p,*this); return *this; }
00153     template <typename P> const bit_aligned_pixel_reference& operator=(const P& p) const { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; } 
00154 
00155     template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); } 
00156     template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
00157 
00158     const bit_aligned_pixel_reference* operator->()    const { return this; }
00159 
00160     const bit_range_t& bit_range() const { return _bit_range; }
00161 private:
00162     mutable bit_range_t _bit_range;
00163     template <typename B, typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
00164 
00165     template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
00166 
00167     template <typename Pixel> void assign(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); static_copy(p,*this); } 
00168     template <typename Pixel> bool  equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); } 
00169 
00170 private:
00171     static void check_gray() {  BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
00172     template <typename Channel> void assign(const Channel& chan, mpl::false_) const { check_gray(); at_c<0>(*this)=chan; }
00173     template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return at_c<0>(*this)==chan; }
00174 };
00175 
00177 //  ColorBasedConcept
00179 
00180 template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K>  
00181 struct kth_element_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,IsMutable>, K> {
00182 public:
00183     typedef const packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> type;
00184 };
00185 
00186 template <typename B, typename C, typename L, bool M, int K>  
00187 struct kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
00188     : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
00189 
00190 template <typename B, typename C, typename L, bool M, int K>  
00191 struct kth_element_const_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
00192     : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
00193 
00194 
00195 namespace detail {
00196     // returns sum of IntegralVector[0] ... IntegralVector[K-1]
00197     template <typename IntegralVector, int K> 
00198     struct sum_k : public mpl::plus<sum_k<IntegralVector,K-1>, typename mpl::at_c<IntegralVector,K-1>::type > {};
00199 
00200     template <typename IntegralVector> struct sum_k<IntegralVector,0> : public mpl::int_<0> {};
00201 }
00202 
00203 // at_c required by MutableColorBaseConcept
00204 template <int K, typename BitField, typename ChannelBitSizes, typename L, bool Mutable> inline
00205 typename kth_element_reference_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>,K>::type
00206 at_c(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>& p) { 
00207     typedef bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable> pixel_t;
00208     typedef typename kth_element_reference_type<pixel_t,K>::type channel_t;
00209     typedef typename pixel_t::bit_range_t bit_range_t;
00210 
00211     bit_range_t bit_range(p.bit_range());
00212     bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
00213 
00214     return channel_t(bit_range.current_byte(), bit_range.bit_offset()); 
00215 }
00216 
00218 //  PixelConcept
00220 
00222 template <typename B, typename C, typename L, bool M>  
00223 struct is_pixel<bit_aligned_pixel_reference<B,C,L,M> > : public mpl::true_{};
00224 
00226 //  PixelBasedConcept
00228 
00229 template <typename B, typename C, typename L, bool M>
00230 struct color_space_type<bit_aligned_pixel_reference<B,C,L,M> > {
00231     typedef typename L::color_space_t type;
00232 }; 
00233 
00234 template <typename B, typename C, typename L, bool M>
00235 struct channel_mapping_type<bit_aligned_pixel_reference<B,C,L,M> > {
00236     typedef typename L::channel_mapping_t type;
00237 }; 
00238 
00239 template <typename B, typename C, typename L, bool M>
00240 struct is_planar<bit_aligned_pixel_reference<B,C,L,M> > : mpl::false_ {}; 
00241 
00243 //  pixel_reference_type
00245 
00246 namespace detail {
00247     // returns a vector containing K copies of the type T
00248     template <unsigned K, typename T> struct k_copies;
00249     template <typename T> struct k_copies<0,T> {
00250         typedef mpl::vector0<> type;
00251     };
00252     template <unsigned K, typename T> struct k_copies : public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
00253 }
00254 
00255 // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
00256 template <typename BitField, int NumBits, typename Layout> 
00257 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false> {
00258 private:
00259     typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
00260     typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
00261 public:
00262     typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false> type;
00263 };
00264 
00265 // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity
00266 template <typename BitField, int NumBits, typename Layout> 
00267 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true> {
00268 private:
00269     typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
00270     typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
00271 public:
00272     typedef bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true> type;
00273 };
00274 
00275 } }  // namespace boost::gil
00276 
00277 namespace std {
00278 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
00279 // swap with 'left bias': 
00280 // - swap between proxy and anything
00281 // - swap between value type and proxy
00282 // - swap between proxy and proxy
00283 // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
00284 
00285 template <typename B, typename C, typename L, typename R> inline
00286 void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, R& y) { 
00287     boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y); 
00288 }
00289 
00290 
00291 template <typename B, typename C, typename L> inline
00292 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) { 
00293     boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y); 
00294 }
00295 
00296 
00297 template <typename B, typename C, typename L> inline
00298 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) { 
00299     boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y); 
00300 }
00301 }   // namespace std
00302 #endif

Generated on Sat May 2 13:50:13 2009 for Generic Image Library by  doxygen 1.5.6