pixel.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_PIXEL_HPP
  9. #define BOOST_GIL_PIXEL_HPP
  10. #include <boost/gil/channel.hpp>
  11. #include <boost/gil/color_base.hpp>
  12. #include <boost/gil/color_base_algorithm.hpp>
  13. #include <boost/gil/concepts.hpp>
  14. #include <boost/gil/metafunctions.hpp>
  15. #include <boost/gil/utilities.hpp>
  16. #include <boost/core/ignore_unused.hpp>
  17. #include <boost/mpl/bool.hpp>
  18. #include <boost/mpl/front.hpp>
  19. #include <boost/type_traits.hpp>
  20. #include <functional>
  21. #include <type_traits>
  22. namespace boost { namespace gil {
  23. // Forward-declare gray_t
  24. struct gray_color_t;
  25. using gray_t = mpl::vector1<gray_color_t>;
  26. template <typename PixelBased> struct color_space_type;
  27. template <typename PixelBased> struct channel_mapping_type;
  28. template <typename PixelBased> struct channel_type;
  29. template <typename PixelBased> struct is_planar;
  30. template <typename PixelBased> struct color_space_type<const PixelBased> : public color_space_type<PixelBased> {};
  31. template <typename PixelBased> struct channel_mapping_type<const PixelBased> : public channel_mapping_type<PixelBased> {};
  32. template <typename PixelBased> struct channel_type<const PixelBased> : public channel_type<PixelBased> {};
  33. template <typename PixelBased> struct is_planar : mpl::false_ {};
  34. template <typename PixelBased> struct is_planar<const PixelBased> : public is_planar<PixelBased> {};
  35. template <typename T> struct is_pixel : public mpl::false_{};
  36. template <typename T> struct is_pixel<const T> : public is_pixel<T> {};
  37. /// \ingroup PixelBasedAlgorithm
  38. /// \brief Returns the number of channels of a pixel-based GIL construct
  39. template <typename PixelBased>
  40. struct num_channels : public mpl::size<typename color_space_type<PixelBased>::type> {};
  41. /**
  42. \addtogroup PixelBasedAlgorithm
  43. Example:
  44. \code
  45. static_assert(num_channels<rgb8_view_t>::value == 3, "");
  46. static_assert(num_channels<cmyk16_planar_ptr_t>::value == 4, "");
  47. static_assert(is_planar<rgb16_planar_image_t>::value));
  48. static_assert(is_same<color_space_type<rgb8_planar_ref_t>::type, rgb_t>::value, "");
  49. static_assert(is_same<channel_mapping_type<cmyk8_pixel_t>::type,
  50. channel_mapping_type<rgba8_pixel_t>::type>::value, "");
  51. static_assert(is_same<channel_type<bgr8_pixel_t>::type, uint8_t>::value, "");
  52. \endcode
  53. */
  54. /// \defgroup ColorBaseModelPixel pixel
  55. /// \ingroup ColorBaseModel
  56. /// \brief A homogeneous color base whose element is a channel value. Models HomogeneousColorBaseValueConcept
  57. /// \defgroup PixelModelPixel pixel
  58. /// \ingroup PixelModel
  59. /// \brief A homogeneous pixel value. Models HomogeneousPixelValueConcept
  60. /// \ingroup PixelModelPixel ColorBaseModelPixel PixelBasedModel
  61. /// \brief Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept
  62. ///
  63. /// 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
  64. /// model \p ChannelConcept. The class \p pixel defines a simple, homogeneous pixel value. It is used to store
  65. /// 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
  66. /// 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*
  67. /// are used as the standard iterator over a row of interleaved homogeneous pixels.
  68. ///
  69. /// Since \p pixel inherits the properties of color base, assigning, equality comparison and copy-construcion are allowed between compatible pixels.
  70. /// 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.
  71. ///
  72. /// 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.
  73. /// 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)
  74. /// or get_color<gray_color_t>(gray_pix1) = get_color<gray_color_t>(gray_pix2)
  75. template <typename ChannelValue, typename Layout> // = mpl::range_c<int,0,ColorSpace::size> >
  76. struct pixel : public detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> {
  77. private:
  78. using channel_t = ChannelValue;
  79. using parent_t = detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value>;
  80. public:
  81. using value_type = pixel<ChannelValue, Layout>;
  82. using reference = value_type&;
  83. using const_reference = value_type const&;
  84. static constexpr bool is_mutable = channel_traits<channel_t>::is_mutable;
  85. pixel(){}
  86. explicit pixel(channel_t v) : parent_t(v) {} // sets all channels to v
  87. pixel(channel_t v0, channel_t v1) : parent_t(v0,v1) {}
  88. pixel(channel_t v0, channel_t v1, channel_t v2) : parent_t(v0,v1,v2) {}
  89. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3) : parent_t(v0,v1,v2,v3) {}
  90. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4) : parent_t(v0,v1,v2,v3,v4) {}
  91. 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) {}
  92. pixel(const pixel& p) : parent_t(p) {}
  93. pixel& operator=(const pixel& p) { static_copy(p,*this); return *this; }
  94. // Construct from another compatible pixel type
  95. template <typename Pixel>
  96. pixel(Pixel const& p,
  97. typename std::enable_if<is_pixel<Pixel>::value>::type* /*dummy*/ = nullptr)
  98. : parent_t(p)
  99. {
  100. check_compatible<Pixel>();
  101. }
  102. template <typename P> pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
  103. template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
  104. template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
  105. // homogeneous pixels have operator[]
  106. typename channel_traits<channel_t>::reference operator[](std::size_t i) { return dynamic_at_c(*this,i); }
  107. typename channel_traits<channel_t>::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); }
  108. private:
  109. template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
  110. template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
  111. template <typename Pixel> void check_compatible() const { gil_function_requires<PixelsCompatibleConcept<Pixel,pixel> >(); }
  112. // Support for assignment/equality comparison of a channel with a grayscale pixel
  113. private:
  114. static void check_gray()
  115. {
  116. static_assert(is_same<typename Layout::color_space_t, gray_t>::value, "");
  117. }
  118. template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
  119. template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
  120. public:
  121. pixel& operator= (channel_t chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
  122. bool operator==(channel_t chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
  123. };
  124. /////////////////////////////
  125. // ColorBasedConcept
  126. /////////////////////////////
  127. template <typename ChannelValue, typename Layout, int K>
  128. struct kth_element_type<pixel<ChannelValue,Layout>, K> {
  129. using type = ChannelValue;
  130. };
  131. template <typename ChannelValue, typename Layout, int K>
  132. struct kth_element_reference_type<pixel<ChannelValue,Layout>, K> {
  133. using type = typename channel_traits<ChannelValue>::reference;
  134. };
  135. template <typename ChannelValue, typename Layout, int K>
  136. struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K> {
  137. using type = typename channel_traits<ChannelValue>::const_reference;
  138. };
  139. template <typename ChannelValue, typename Layout, int K>
  140. struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K> {
  141. using type = typename channel_traits<ChannelValue>::const_reference;
  142. };
  143. /////////////////////////////
  144. // PixelConcept
  145. /////////////////////////////
  146. template <typename ChannelValue, typename Layout>
  147. struct is_pixel<pixel<ChannelValue,Layout> > : public mpl::true_{};
  148. /////////////////////////////
  149. // HomogeneousPixelBasedConcept
  150. /////////////////////////////
  151. template <typename ChannelValue, typename Layout>
  152. struct color_space_type<pixel<ChannelValue,Layout> > {
  153. using type = typename Layout::color_space_t;
  154. };
  155. template <typename ChannelValue, typename Layout>
  156. struct channel_mapping_type<pixel<ChannelValue,Layout> > {
  157. using type = typename Layout::channel_mapping_t;
  158. };
  159. template <typename ChannelValue, typename Layout>
  160. struct is_planar<pixel<ChannelValue,Layout> > : public mpl::false_ {};
  161. template <typename ChannelValue, typename Layout>
  162. struct channel_type<pixel<ChannelValue,Layout> > {
  163. using type = ChannelValue;
  164. };
  165. }} // namespace boost::gil
  166. namespace boost {
  167. template <typename ChannelValue, typename Layout>
  168. struct has_trivial_constructor<gil::pixel<ChannelValue,Layout> > : public has_trivial_constructor<ChannelValue> {};
  169. }
  170. #endif