pixel.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_PIXEL_H
  10. #define GIL_PIXEL_H
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief pixel class and related utilities
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated on September 28, 2006
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include <functional>
  20. #include <boost/core/ignore_unused.hpp>
  21. #include <boost/utility/enable_if.hpp>
  22. #include <boost/mpl/bool.hpp>
  23. #include <boost/mpl/front.hpp>
  24. #include <boost/type_traits.hpp>
  25. #include "gil_config.hpp"
  26. #include "color_base.hpp"
  27. #include "gil_concept.hpp"
  28. #include "channel.hpp"
  29. #include "metafunctions.hpp"
  30. #include "utilities.hpp"
  31. #include "color_base_algorithm.hpp"
  32. namespace boost { namespace gil {
  33. // Forward-declare gray_t
  34. struct gray_color_t;
  35. typedef mpl::vector1<gray_color_t> gray_t;
  36. template <typename PixelBased> struct color_space_type;
  37. template <typename PixelBased> struct channel_mapping_type;
  38. template <typename PixelBased> struct channel_type;
  39. template <typename PixelBased> struct is_planar;
  40. template <typename PixelBased> struct color_space_type<const PixelBased> : public color_space_type<PixelBased> {};
  41. template <typename PixelBased> struct channel_mapping_type<const PixelBased> : public channel_mapping_type<PixelBased> {};
  42. template <typename PixelBased> struct channel_type<const PixelBased> : public channel_type<PixelBased> {};
  43. template <typename PixelBased> struct is_planar : mpl::false_ {};
  44. template <typename PixelBased> struct is_planar<const PixelBased> : public is_planar<PixelBased> {};
  45. template <typename T> struct is_pixel : public mpl::false_{};
  46. template <typename T> struct is_pixel<const T> : public is_pixel<T> {};
  47. /// \ingroup PixelBasedAlgorithm
  48. /// \brief Returns the number of channels of a pixel-based GIL construct
  49. template <typename PixelBased>
  50. struct num_channels : public mpl::size<typename color_space_type<PixelBased>::type> {};
  51. /**
  52. \addtogroup PixelBasedAlgorithm
  53. Example:
  54. \code
  55. BOOST_STATIC_ASSERT((num_channels<rgb8_view_t>::value==3));
  56. BOOST_STATIC_ASSERT((num_channels<cmyk16_planar_ptr_t>::value==4));
  57. BOOST_STATIC_ASSERT((is_planar<rgb16_planar_image_t>::value));
  58. BOOST_STATIC_ASSERT((is_same<color_space_type<rgb8_planar_ref_t>::type, rgb_t>::value));
  59. BOOST_STATIC_ASSERT((is_same<channel_mapping_type<cmyk8_pixel_t>::type,
  60. channel_mapping_type<rgba8_pixel_t>::type>::value));
  61. BOOST_STATIC_ASSERT((is_same<channel_type<bgr8_pixel_t>::type, uint8_t>::value));
  62. \endcode
  63. */
  64. /// \defgroup ColorBaseModelPixel pixel
  65. /// \ingroup ColorBaseModel
  66. /// \brief A homogeneous color base whose element is a channel value. Models HomogeneousColorBaseValueConcept
  67. /// \defgroup PixelModelPixel pixel
  68. /// \ingroup PixelModel
  69. /// \brief A homogeneous pixel value. Models HomogeneousPixelValueConcept
  70. /// \ingroup PixelModelPixel ColorBaseModelPixel PixelBasedModel
  71. /// \brief Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept
  72. ///
  73. /// A pixel is a set of channels defining the color at a given point in an image. Conceptually, a pixel is little more than a color base whose elements
  74. /// model \p ChannelConcept. The class \p pixel defines a simple, homogeneous pixel value. It is used to store
  75. /// the value of a color. The built-in C++ references to \p pixel, \p pixel& and \p const \p pixel& are used to represent a reference to a pixel
  76. /// inside an interleaved image view (a view in which all channels are together in memory). Similarly, built-in pointer types \p pixel* and \p const \p pixel*
  77. /// are used as the standard iterator over a row of interleaved homogeneous pixels.
  78. ///
  79. /// Since \p pixel inherits the properties of color base, assigning, equality comparison and copy-construcion are allowed between compatible pixels.
  80. /// This means that an 8-bit RGB pixel may be assigned to an 8-bit BGR pixel, or to an 8-bit planar reference. The channels are properly paired semantically.
  81. ///
  82. /// The single-channel (grayscale) instantiation of the class pixel, (i.e. \p pixel<T,gray_layout_t>) is also convertible to/from a channel value.
  83. /// This allows grayscale pixels to be used in simpler expressions like *gray_pix1 = *gray_pix2 instead of more complicated at_c<0>(gray_pix1) = at_c<0>(gray_pix2)
  84. /// or get_color<gray_color_t>(gray_pix1) = get_color<gray_color_t>(gray_pix2)
  85. template <typename ChannelValue, typename Layout> // = mpl::range_c<int,0,ColorSpace::size> >
  86. struct pixel : public detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> {
  87. private:
  88. typedef ChannelValue channel_t;
  89. typedef detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> parent_t;
  90. public:
  91. typedef pixel value_type;
  92. typedef value_type& reference;
  93. typedef const value_type& const_reference;
  94. BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits<channel_t>::is_mutable);
  95. pixel(){}
  96. explicit pixel(channel_t v) : parent_t(v) {} // sets all channels to v
  97. pixel(channel_t v0, channel_t v1) : parent_t(v0,v1) {}
  98. pixel(channel_t v0, channel_t v1, channel_t v2) : parent_t(v0,v1,v2) {}
  99. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3) : parent_t(v0,v1,v2,v3) {}
  100. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4) : parent_t(v0,v1,v2,v3,v4) {}
  101. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4, channel_t v5) : parent_t(v0,v1,v2,v3,v4,v5) {}
  102. pixel(const pixel& p) : parent_t(p) {}
  103. pixel& operator=(const pixel& p) { static_copy(p,*this); return *this; }
  104. // Construct from another compatible pixel type
  105. template <typename Pixel> pixel(const Pixel& p, typename enable_if_c<is_pixel<Pixel>::value>::type* dummy = 0) : parent_t(p) {
  106. check_compatible<Pixel>();
  107. boost::ignore_unused(dummy);
  108. }
  109. template <typename P> pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
  110. template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
  111. template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
  112. // homogeneous pixels have operator[]
  113. typename channel_traits<channel_t>::reference operator[](std::size_t i) { return dynamic_at_c(*this,i); }
  114. typename channel_traits<channel_t>::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); }
  115. private:
  116. template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
  117. template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
  118. template <typename Pixel> void check_compatible() const { gil_function_requires<PixelsCompatibleConcept<Pixel,pixel> >(); }
  119. // Support for assignment/equality comparison of a channel with a grayscale pixel
  120. private:
  121. static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
  122. template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
  123. template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
  124. public:
  125. pixel& operator= (channel_t chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
  126. bool operator==(channel_t chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
  127. };
  128. /////////////////////////////
  129. // ColorBasedConcept
  130. /////////////////////////////
  131. template <typename ChannelValue, typename Layout, int K>
  132. struct kth_element_type<pixel<ChannelValue,Layout>, K> {
  133. typedef ChannelValue type;
  134. };
  135. template <typename ChannelValue, typename Layout, int K>
  136. struct kth_element_reference_type<pixel<ChannelValue,Layout>, K> {
  137. typedef typename channel_traits<ChannelValue>::reference type;
  138. };
  139. template <typename ChannelValue, typename Layout, int K>
  140. struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K> {
  141. typedef typename channel_traits<ChannelValue>::const_reference type;
  142. };
  143. template <typename ChannelValue, typename Layout, int K>
  144. struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K> {
  145. typedef typename channel_traits<ChannelValue>::const_reference type;
  146. };
  147. /////////////////////////////
  148. // PixelConcept
  149. /////////////////////////////
  150. template <typename ChannelValue, typename Layout>
  151. struct is_pixel<pixel<ChannelValue,Layout> > : public mpl::true_{};
  152. /////////////////////////////
  153. // HomogeneousPixelBasedConcept
  154. /////////////////////////////
  155. template <typename ChannelValue, typename Layout>
  156. struct color_space_type<pixel<ChannelValue,Layout> > {
  157. typedef typename Layout::color_space_t type;
  158. };
  159. template <typename ChannelValue, typename Layout>
  160. struct channel_mapping_type<pixel<ChannelValue,Layout> > {
  161. typedef typename Layout::channel_mapping_t type;
  162. };
  163. template <typename ChannelValue, typename Layout>
  164. struct is_planar<pixel<ChannelValue,Layout> > : public mpl::false_ {};
  165. template <typename ChannelValue, typename Layout>
  166. struct channel_type<pixel<ChannelValue,Layout> > {
  167. typedef ChannelValue type;
  168. };
  169. } } // namespace boost::gil
  170. namespace boost {
  171. template <typename ChannelValue, typename Layout>
  172. struct has_trivial_constructor<gil::pixel<ChannelValue,Layout> > : public has_trivial_constructor<ChannelValue> {};
  173. }
  174. #endif