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://opensource.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 ChannelBitSizes,  // MPL integral vector defining the number of bits for each channel. For example, for 565RGB, vector_c<int,5,6,5>
00119           typename Layout, 
00120           bool IsMutable>
00121 struct bit_aligned_pixel_reference {
00122     BOOST_STATIC_CONSTANT(int, bit_size = (mpl::accumulate<ChannelBitSizes, mpl::int_<0>, mpl::plus<mpl::_1, mpl::_2> >::type::value));
00123     typedef bit_range<bit_size,IsMutable>                                           bit_range_t;
00124     typedef typename detail::min_fast_uint<bit_size>::type                          bitfield_t;  
00125     typedef typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type data_ptr_t;
00126 
00127     typedef Layout layout_t;
00128 
00129     typedef typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type value_type;
00130     typedef const bit_aligned_pixel_reference                                    reference;
00131     typedef const bit_aligned_pixel_reference<ChannelBitSizes,Layout,false>      const_reference;
00132 
00133     BOOST_STATIC_CONSTANT(bool, is_mutable = IsMutable);
00134 
00135     bit_aligned_pixel_reference(){}
00136     bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset)   : _bit_range(data_ptr, bit_offset) {}
00137     explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
00138     template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
00139 
00140     // Grayscale references can be constructed from the channel reference
00141     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()) {
00142         BOOST_STATIC_ASSERT((num_channels<bit_aligned_pixel_reference>::value==1));
00143     }
00144 
00145     // Construct from another compatible pixel type
00146     bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {}
00147     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()) {
00148         check_compatible<packed_pixel<BF,CR,Layout> >();
00149     }
00150 
00151     template <typename P> const bit_aligned_pixel_reference& operator=(const P& p)    const { check_compatible<P>(); static_copy(p,*this); return *this; } 
00152     const bit_aligned_pixel_reference& operator=(const bit_aligned_pixel_reference& p) const { static_copy(p,*this); return *this; }
00153 
00154     template <typename P> bool operator==(const P& p) const { check_compatible<P>(); return static_equal(*this,p); }
00155     template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
00156 
00157     const bit_aligned_pixel_reference* operator->()    const { return this; }
00158 
00159     const bit_range_t& bit_range() const { return _bit_range; }
00160 private:
00161     mutable bit_range_t _bit_range;
00162     template <typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
00163 
00164     template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
00165 };
00166 
00168 //  ColorBasedConcept
00170 
00171 template <typename ChannelBitSizes, typename L, bool IsMutable, int K>  
00172 struct kth_element_type<bit_aligned_pixel_reference<ChannelBitSizes,L,IsMutable>, K> {
00173 private:
00174     typedef typename bit_aligned_pixel_reference<ChannelBitSizes,L,IsMutable>::bitfield_t bitfield_t;
00175 public:
00176     typedef const packed_dynamic_channel_reference<bitfield_t, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> type;
00177 };
00178 
00179 template <typename C, typename L, bool M, int K>  
00180 struct kth_element_reference_type<bit_aligned_pixel_reference<C,L,M>, K>
00181     : public kth_element_type<bit_aligned_pixel_reference<C,L,M>, K> {};
00182 
00183 template <typename C, typename L, bool M, int K>  
00184 struct kth_element_const_reference_type<bit_aligned_pixel_reference<C,L,M>, K>
00185     : public kth_element_type<bit_aligned_pixel_reference<C,L,M>, K> {};
00186 
00187 
00188 namespace detail {
00189     // returns sum of IntegralVector[0] ... IntegralVector[K-1]
00190     template <typename IntegralVector, int K> 
00191     struct sum_k : public mpl::plus<sum_k<IntegralVector,K-1>, typename mpl::at_c<IntegralVector,K-1>::type > {};
00192 
00193     template <typename IntegralVector> struct sum_k<IntegralVector,0> : public mpl::int_<0> {};
00194 }
00195 
00196 // at_c required by MutableColorBaseConcept
00197 template <int K, typename ChannelBitSizes, typename L, bool Mutable> inline
00198 typename kth_element_reference_type<bit_aligned_pixel_reference<ChannelBitSizes,L,Mutable>,K>::type
00199 at_c(const bit_aligned_pixel_reference<ChannelBitSizes,L,Mutable>& p) { 
00200     typedef bit_aligned_pixel_reference<ChannelBitSizes,L,Mutable> pixel_t;
00201     typedef typename kth_element_reference_type<pixel_t,K>::type channel_t;
00202     typedef typename pixel_t::bit_range_t bit_range_t;
00203 
00204     bit_range_t bit_range(p.bit_range());
00205     bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
00206 
00207     return channel_t(bit_range.current_byte(), bit_range.bit_offset()); 
00208 }
00209 
00211 //  PixelConcept
00213 
00215 template <typename C, typename L, bool M>  
00216 struct is_pixel<bit_aligned_pixel_reference<C,L,M> > : public mpl::true_{};
00217 
00219 //  PixelBasedConcept
00221 
00222 template <typename C, typename L, bool M>
00223 struct color_space_type<bit_aligned_pixel_reference<C,L,M> > {
00224     typedef typename L::color_space_t type;
00225 }; 
00226 
00227 template <typename C, typename L, bool M>
00228 struct channel_mapping_type<bit_aligned_pixel_reference<C,L,M> > {
00229     typedef typename L::channel_mapping_t type;
00230 }; 
00231 
00232 template <typename C, typename L, bool M>
00233 struct is_planar<bit_aligned_pixel_reference<C,L,M> > : mpl::false_ {}; 
00234 
00236 //  pixel_reference_type
00238 
00239 namespace detail {
00240     // returns a vector containing K copies of the type T
00241     template <unsigned K, typename T> struct k_copies;
00242     template <typename T> struct k_copies<0,T> {
00243         typedef mpl::vector0<> type;
00244     };
00245     template <unsigned K, typename T> struct k_copies : public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
00246 }
00247 
00248 // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
00249 // Note: BitField must be the same type as pixel_reference_type<...>::type::bitfield_t, but it is too complicated to ensure this
00250 template <typename BitField, int NumBits, typename Layout> 
00251 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false> {
00252 private:
00253     typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
00254     typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
00255 public:
00256     typedef bit_aligned_pixel_reference<channel_bit_sizes_t, Layout, false> type;
00257 };
00258 
00259 // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity
00260 // Note: BitField must be the same type as pixel_reference_type<...>::type::bitfield_t, but it is too complicated to ensure this
00261 template <typename BitField, int NumBits, typename Layout> 
00262 struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true> {
00263 private:
00264     typedef typename mpl::size<typename Layout::color_space_t>::type size_t;
00265     typedef typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type channel_bit_sizes_t;
00266 public:
00267     typedef bit_aligned_pixel_reference<channel_bit_sizes_t, Layout, true> type;
00268 };
00269 
00270 } }  // namespace boost::gil
00271 
00272 namespace std {
00273 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
00274 // swap with 'left bias': 
00275 // - swap between proxy and anything
00276 // - swap between value type and proxy
00277 // - swap between proxy and proxy
00278 // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
00279 
00280 template <typename C, typename L, typename R> inline
00281 void swap(boost::gil::bit_aligned_pixel_reference<C,L,true> x, R& y) { 
00282     boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<C,L,true>::value_type>(x,y); 
00283 }
00284 
00285 
00286 template <typename C, typename L> inline
00287 void swap(typename boost::gil::bit_aligned_pixel_reference<C,L,true>::value_type& x, boost::gil::bit_aligned_pixel_reference<C,L,true> y) { 
00288     boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<C,L,true>::value_type>(x,y); 
00289 }
00290 
00291 
00292 template <typename C, typename L> inline
00293 void swap(boost::gil::bit_aligned_pixel_reference<C,L,true> x, boost::gil::bit_aligned_pixel_reference<C,L,true> y) { 
00294     boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<C,L,true>::value_type>(x,y); 
00295 }
00296 }   // namespace std
00297 #endif

Generated on Thu Nov 8 21:53:16 2007 for Generic Image Library by  doxygen 1.4.4