pixel.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
  4. //
  5. // Distributed under the Boost Software License, Version 1.0
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt
  8. //
  9. #ifndef BOOST_GIL_PIXEL_HPP
  10. #define BOOST_GIL_PIXEL_HPP
  11. #include <boost/gil/channel.hpp>
  12. #include <boost/gil/color_base.hpp>
  13. #include <boost/gil/color_base_algorithm.hpp>
  14. #include <boost/gil/concepts.hpp>
  15. #include <boost/gil/metafunctions.hpp>
  16. #include <boost/gil/utilities.hpp>
  17. #include <boost/gil/detail/mp11.hpp>
  18. #include <functional>
  19. #include <type_traits>
  20. namespace boost { namespace gil {
  21. // Forward-declare gray_t
  22. struct gray_color_t;
  23. using gray_t = mp11::mp_list<gray_color_t>;
  24. template <typename PixelBased> struct color_space_type;
  25. template <typename PixelBased> struct channel_mapping_type;
  26. template <typename PixelBased> struct channel_type;
  27. template <typename PixelBased> struct is_planar;
  28. template <typename PixelBased>
  29. struct color_space_type<PixelBased const> : color_space_type<PixelBased> {};
  30. template <typename PixelBased>
  31. struct channel_mapping_type<PixelBased const> : channel_mapping_type<PixelBased> {};
  32. template <typename PixelBased>
  33. struct channel_type<PixelBased const> : channel_type<PixelBased> {};
  34. template <typename PixelBased>
  35. struct is_planar : std::false_type {};
  36. template <typename PixelBased>
  37. struct is_planar<PixelBased const> : is_planar<PixelBased> {};
  38. template <typename T> struct is_pixel : std::false_type {};
  39. template <typename T> struct is_pixel<T const> : is_pixel<T> {};
  40. /// \ingroup PixelBasedAlgorithm
  41. /// \brief Returns the number of channels of a pixel-based GIL construct
  42. template <typename PixelBased>
  43. struct num_channels : mp11::mp_size<typename color_space_type<PixelBased>::type>::type {};
  44. /**
  45. \addtogroup PixelBasedAlgorithm
  46. Example:
  47. \code
  48. static_assert(num_channels<rgb8_view_t>::value == 3, "");
  49. static_assert(num_channels<cmyk16_planar_ptr_t>::value == 4, "");
  50. static_assert(is_planar<rgb16_planar_image_t>::value));
  51. static_assert(std::is_same<color_space_type<rgb8_planar_ref_t>::type, rgb_t>::value, "");
  52. static_assert(std::is_same<channel_mapping_type<cmyk8_pixel_t>::type,
  53. channel_mapping_type<rgba8_pixel_t>::type>::value, "");
  54. static_assert(std::is_same<channel_type<bgr8_pixel_t>::type, uint8_t>::value, "");
  55. \endcode
  56. */
  57. /// \defgroup ColorBaseModelPixel pixel
  58. /// \ingroup ColorBaseModel
  59. /// \brief A homogeneous color base whose element is a channel value. Models HomogeneousColorBaseValueConcept
  60. /// \defgroup PixelModelPixel pixel
  61. /// \ingroup PixelModel
  62. /// \brief A homogeneous pixel value. Models HomogeneousPixelValueConcept
  63. /// \ingroup PixelModelPixel ColorBaseModelPixel PixelBasedModel
  64. /// \brief Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept
  65. ///
  66. /// 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
  67. /// model \p ChannelConcept. The class \p pixel defines a simple, homogeneous pixel value. It is used to store
  68. /// 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
  69. /// 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*
  70. /// are used as the standard iterator over a row of interleaved homogeneous pixels.
  71. ///
  72. /// Since \p pixel inherits the properties of color base, assigning, equality comparison and copy-construcion are allowed between compatible pixels.
  73. /// 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.
  74. ///
  75. /// 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.
  76. /// 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)
  77. /// or get_color<gray_color_t>(gray_pix1) = get_color<gray_color_t>(gray_pix2)
  78. ///
  79. /// \tparam ChannelValue TODO
  80. /// \tparam Layout mp11::make_integer_sequence<int, ColorSpace::size>
  81. template <typename ChannelValue, typename Layout>
  82. struct pixel :
  83. detail::homogeneous_color_base
  84. <
  85. ChannelValue,
  86. Layout,
  87. mp11::mp_size<typename Layout::color_space_t>::value
  88. >
  89. {
  90. private:
  91. using channel_t = ChannelValue;
  92. using parent_t = detail::homogeneous_color_base
  93. <
  94. ChannelValue,
  95. Layout,
  96. mp11::mp_size<typename Layout::color_space_t>::value
  97. >;
  98. public:
  99. using value_type = pixel<ChannelValue, Layout>;
  100. using reference = value_type&;
  101. using const_reference = value_type const&;
  102. static constexpr bool is_mutable = channel_traits<channel_t>::is_mutable;
  103. pixel() = default;
  104. explicit pixel(channel_t v) : parent_t(v) {} // sets all channels to v
  105. pixel(channel_t v0, channel_t v1) : parent_t(v0, v1) {}
  106. pixel(channel_t v0, channel_t v1, channel_t v2) : parent_t(v0, v1, v2) {}
  107. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3)
  108. : parent_t(v0, v1, v2, v3)
  109. {}
  110. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4)
  111. : parent_t(v0, v1, v2, v3, v4)
  112. {}
  113. pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4, channel_t v5)
  114. : parent_t(v0, v1, v2, v3, v4, v5)
  115. {}
  116. pixel(const pixel& p) : parent_t(p) {}
  117. pixel& operator=(pixel const& p)
  118. {
  119. static_copy(p,*this);
  120. return *this;
  121. }
  122. // Construct from another compatible pixel type
  123. template <typename Pixel>
  124. pixel(Pixel const& p,
  125. typename std::enable_if<is_pixel<Pixel>::value>::type* /*dummy*/ = nullptr)
  126. : parent_t(p)
  127. {
  128. check_compatible<Pixel>();
  129. }
  130. template <typename Pixel>
  131. pixel& operator=(Pixel const& p)
  132. {
  133. assign(p, is_pixel<Pixel>());
  134. return *this;
  135. }
  136. template <typename Pixel>
  137. bool operator==(Pixel const& p) const { return equal(p, is_pixel<Pixel>()); }
  138. template <typename Pixel>
  139. bool operator!=(Pixel const& p) const { return !(*this == p); }
  140. // homogeneous pixels have operator[]
  141. auto operator[](std::size_t index)
  142. -> typename channel_traits<channel_t>::reference
  143. {
  144. return dynamic_at_c(*this, index);
  145. }
  146. auto operator[](std::size_t index) const
  147. -> typename channel_traits<channel_t>::const_reference
  148. {
  149. return dynamic_at_c(*this, index);
  150. }
  151. private:
  152. template <typename Pixel>
  153. void assign(Pixel const& p, std::true_type)
  154. {
  155. check_compatible<Pixel>();
  156. static_copy(p, *this);
  157. }
  158. template <typename Pixel>
  159. bool equal(Pixel const& p, std::true_type) const
  160. {
  161. check_compatible<Pixel>();
  162. return static_equal(*this, p);
  163. }
  164. template <typename Pixel>
  165. void check_compatible() const
  166. {
  167. gil_function_requires<PixelsCompatibleConcept<Pixel, pixel>>();
  168. }
  169. // Support for assignment/equality comparison of a channel with a grayscale pixel
  170. private:
  171. static void check_gray()
  172. {
  173. static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
  174. }
  175. template <typename Channel>
  176. void assign(Channel const& channel, std::false_type)
  177. {
  178. check_gray();
  179. gil::at_c<0>(*this) = channel;
  180. }
  181. template <typename Channel>
  182. bool equal (Channel const& channel, std::false_type) const
  183. {
  184. check_gray();
  185. return gil::at_c<0>(*this) == channel;
  186. }
  187. public:
  188. pixel& operator= (channel_t channel)
  189. {
  190. check_gray();
  191. gil::at_c<0>(*this) = channel;
  192. return *this;
  193. }
  194. bool operator==(channel_t channel) const
  195. {
  196. check_gray();
  197. return gil::at_c<0>(*this) == channel;
  198. }
  199. };
  200. /////////////////////////////
  201. // ColorBasedConcept
  202. /////////////////////////////
  203. template <typename ChannelValue, typename Layout, int K>
  204. struct kth_element_type<pixel<ChannelValue,Layout>, K>
  205. {
  206. using type = ChannelValue;
  207. };
  208. template <typename ChannelValue, typename Layout, int K>
  209. struct kth_element_reference_type<pixel<ChannelValue,Layout>, K>
  210. {
  211. using type = typename channel_traits<ChannelValue>::reference;
  212. };
  213. template <typename ChannelValue, typename Layout, int K>
  214. struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K>
  215. {
  216. using type = typename channel_traits<ChannelValue>::const_reference;
  217. };
  218. template <typename ChannelValue, typename Layout, int K>
  219. struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K>
  220. {
  221. using type = typename channel_traits<ChannelValue>::const_reference;
  222. };
  223. /////////////////////////////
  224. // PixelConcept
  225. /////////////////////////////
  226. template <typename ChannelValue, typename Layout>
  227. struct is_pixel<pixel<ChannelValue,Layout>> : std::true_type {};
  228. /////////////////////////////
  229. // HomogeneousPixelBasedConcept
  230. /////////////////////////////
  231. template <typename ChannelValue, typename Layout>
  232. struct color_space_type<pixel<ChannelValue, Layout>>
  233. {
  234. using type = typename Layout::color_space_t;
  235. };
  236. template <typename ChannelValue, typename Layout>
  237. struct channel_mapping_type<pixel<ChannelValue, Layout>>
  238. {
  239. using type = typename Layout::channel_mapping_t;
  240. };
  241. template <typename ChannelValue, typename Layout>
  242. struct is_planar<pixel<ChannelValue, Layout>> : std::false_type {};
  243. template <typename ChannelValue, typename Layout>
  244. struct channel_type<pixel<ChannelValue, Layout>>
  245. {
  246. using type = ChannelValue;
  247. };
  248. }} // namespace boost::gil
  249. #endif