packed_pixel.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_PACKED_PIXEL_HPP
  9. #define BOOST_GIL_PACKED_PIXEL_HPP
  10. #include <boost/gil/pixel.hpp>
  11. #include <boost/gil/detail/mp11.hpp>
  12. #include <functional>
  13. #include <type_traits>
  14. namespace boost { namespace gil {
  15. /// A model of a heterogeneous pixel whose channels are bit ranges.
  16. /// For example 16-bit RGB in '565' format.
  17. /// \defgroup ColorBaseModelPackedPixel packed_pixel
  18. /// \ingroup ColorBaseModel
  19. /// \brief A heterogeneous color base whose elements are reference proxies to channels in a pixel. Models ColorBaseValueConcept. This class is used to model packed pixels, such as 16-bit packed RGB.
  20. /// \defgroup PixelModelPackedPixel packed_pixel
  21. /// \ingroup PixelModel
  22. /// \brief A heterogeneous pixel used to represent packed pixels with non-byte-aligned channels. Models PixelValueConcept
  23. ///
  24. /// Example:
  25. /// \code
  26. /// using rgb565_pixel_t = packed_pixel_type<uint16_t, mp11::mp_list-c<unsigned,5,6,5>, rgb_layout_t>::type;
  27. /// static_assert(sizeof(rgb565_pixel_t) == 2, "");
  28. ///
  29. /// rgb565_pixel_t r565;
  30. /// get_color(r565,red_t()) = 31;
  31. /// get_color(r565,green_t()) = 63;
  32. /// get_color(r565,blue_t()) = 31;
  33. /// assert(r565 == rgb565_pixel_t((uint16_t)0xFFFF));
  34. /// \endcode
  35. /// \ingroup ColorBaseModelPackedPixel PixelModelPackedPixel PixelBasedModel
  36. /// \brief Heterogeneous pixel value whose channel references can be constructed from the pixel bitfield and their index. Models ColorBaseValueConcept, PixelValueConcept, PixelBasedConcept
  37. /// Typical use for this is a model of a packed pixel (like 565 RGB)
  38. /// \tparam BitField Type that holds the bits of the pixel. Typically an integral type, like std::uint16_t.
  39. /// \tparam ChannelRefs MP11 list whose elements are packed channels. They must be constructible from BitField. GIL uses packed_channel_reference
  40. /// \tparam Layout defining the color space and ordering of the channels. Example value: rgb_layout_t
  41. template <typename BitField, typename ChannelRefs, typename Layout>
  42. struct packed_pixel
  43. {
  44. BitField _bitfield{0}; // TODO: Make private
  45. using layout_t = Layout;
  46. using value_type = packed_pixel<BitField, ChannelRefs, Layout>;
  47. using reference = value_type&;
  48. using const_reference = value_type const&;
  49. static constexpr bool is_mutable =
  50. channel_traits<mp11::mp_front<ChannelRefs>>::is_mutable;
  51. packed_pixel() = default;
  52. explicit packed_pixel(const BitField& bitfield) : _bitfield(bitfield) {}
  53. // Construct from another compatible pixel type
  54. packed_pixel(const packed_pixel& p) : _bitfield(p._bitfield) {}
  55. template <typename Pixel>
  56. packed_pixel(Pixel const& p,
  57. typename std::enable_if<is_pixel<Pixel>::value>::type* /*dummy*/ = nullptr)
  58. {
  59. check_compatible<Pixel>();
  60. static_copy(p, *this);
  61. }
  62. packed_pixel(int chan0, int chan1)
  63. : _bitfield(0)
  64. {
  65. static_assert(num_channels<packed_pixel>::value == 2, "");
  66. gil::at_c<0>(*this) = chan0; gil::at_c<1>(*this) = chan1;
  67. }
  68. packed_pixel(int chan0, int chan1, int chan2)
  69. : _bitfield(0)
  70. {
  71. static_assert(num_channels<packed_pixel>::value == 3, "");
  72. gil::at_c<0>(*this) = chan0;
  73. gil::at_c<1>(*this) = chan1;
  74. gil::at_c<2>(*this) = chan2;
  75. }
  76. packed_pixel(int chan0, int chan1, int chan2, int chan3)
  77. : _bitfield(0)
  78. {
  79. static_assert(num_channels<packed_pixel>::value == 4, "");
  80. gil::at_c<0>(*this) = chan0;
  81. gil::at_c<1>(*this) = chan1;
  82. gil::at_c<2>(*this) = chan2;
  83. gil::at_c<3>(*this) = chan3;
  84. }
  85. packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4)
  86. : _bitfield(0)
  87. {
  88. static_assert(num_channels<packed_pixel>::value == 5, "");
  89. gil::at_c<0>(*this) = chan0;
  90. gil::at_c<1>(*this) = chan1;
  91. gil::at_c<2>(*this) = chan2;
  92. gil::at_c<3>(*this) = chan3;
  93. gil::at_c<4>(*this) = chan4;
  94. }
  95. auto operator=(packed_pixel const& p) -> packed_pixel&
  96. {
  97. _bitfield = p._bitfield;
  98. return *this;
  99. }
  100. template <typename Pixel>
  101. auto operator=(Pixel const& p) -> packed_pixel&
  102. {
  103. assign(p, is_pixel<Pixel>());
  104. return *this;
  105. }
  106. template <typename Pixel>
  107. bool operator==(Pixel const& p) const
  108. {
  109. return equal(p, is_pixel<Pixel>());
  110. }
  111. template <typename Pixel>
  112. bool operator!=(Pixel const& p) const { return !(*this==p); }
  113. private:
  114. template <typename Pixel>
  115. static void check_compatible()
  116. {
  117. gil_function_requires<PixelsCompatibleConcept<Pixel, packed_pixel>>();
  118. }
  119. template <typename Pixel>
  120. void assign(Pixel const& p, std::true_type)
  121. {
  122. check_compatible<Pixel>();
  123. static_copy(p, *this);
  124. }
  125. template <typename Pixel>
  126. bool equal(Pixel const& p, std::true_type) const
  127. {
  128. check_compatible<Pixel>();
  129. return static_equal(*this, p);
  130. }
  131. // Support for assignment/equality comparison of a channel with a grayscale pixel
  132. static void check_gray()
  133. {
  134. static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
  135. }
  136. template <typename Channel>
  137. void assign(Channel const& channel, std::false_type)
  138. {
  139. check_gray();
  140. gil::at_c<0>(*this) = channel;
  141. }
  142. template <typename Channel>
  143. bool equal (Channel const& channel, std::false_type) const
  144. {
  145. check_gray();
  146. return gil::at_c<0>(*this) == channel;
  147. }
  148. public:
  149. auto operator=(int channel) -> packed_pixel&
  150. {
  151. check_gray();
  152. gil::at_c<0>(*this) = channel;
  153. return *this;
  154. }
  155. bool operator==(int channel) const
  156. {
  157. check_gray();
  158. return gil::at_c<0>(*this) == channel;
  159. }
  160. };
  161. /////////////////////////////
  162. // ColorBasedConcept
  163. /////////////////////////////
  164. template <typename BitField, typename ChannelRefs, typename Layout, int K>
  165. struct kth_element_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
  166. {
  167. using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::value_type;
  168. };
  169. template <typename BitField, typename ChannelRefs, typename Layout, int K>
  170. struct kth_element_reference_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
  171. {
  172. using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::reference;
  173. };
  174. template <typename BitField, typename ChannelRefs, typename Layout, int K>
  175. struct kth_element_const_reference_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
  176. {
  177. using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::const_reference;
  178. };
  179. template <int K, typename P, typename C, typename L>
  180. inline
  181. auto at_c(packed_pixel<P, C, L>& p)
  182. -> typename kth_element_reference_type<packed_pixel<P, C, L>, K>::type
  183. {
  184. return typename kth_element_reference_type
  185. <
  186. packed_pixel<P, C, L>,
  187. K
  188. >::type{&p._bitfield};
  189. }
  190. template <int K, typename P, typename C, typename L>
  191. inline
  192. auto at_c(const packed_pixel<P, C, L>& p)
  193. -> typename kth_element_const_reference_type<packed_pixel<P, C, L>, K>::type
  194. {
  195. return typename kth_element_const_reference_type
  196. <
  197. packed_pixel<P, C, L>,
  198. K>::type{&p._bitfield};
  199. }
  200. /////////////////////////////
  201. // PixelConcept
  202. /////////////////////////////
  203. // Metafunction predicate that flags packed_pixel as a model of PixelConcept. Required by PixelConcept
  204. template <typename BitField, typename ChannelRefs, typename Layout>
  205. struct is_pixel<packed_pixel<BitField, ChannelRefs, Layout>> : std::true_type {};
  206. /////////////////////////////
  207. // PixelBasedConcept
  208. /////////////////////////////
  209. template <typename P, typename C, typename Layout>
  210. struct color_space_type<packed_pixel<P, C, Layout>>
  211. {
  212. using type = typename Layout::color_space_t;
  213. };
  214. template <typename P, typename C, typename Layout>
  215. struct channel_mapping_type<packed_pixel<P, C, Layout>>
  216. {
  217. using type = typename Layout::channel_mapping_t;
  218. };
  219. template <typename P, typename C, typename Layout>
  220. struct is_planar<packed_pixel<P, C, Layout>> : std::false_type {};
  221. ////////////////////////////////////////////////////////////////////////////////
  222. /// Support for interleaved iterators over packed pixel
  223. ////////////////////////////////////////////////////////////////////////////////
  224. /// \defgroup PixelIteratorModelPackedInterleavedPtr Pointer to packed_pixel<P,CR,Layout>
  225. /// \ingroup PixelIteratorModel
  226. /// \brief Iterators over interleaved pixels.
  227. /// The pointer packed_pixel<P,CR,Layout>* is used as an iterator over interleaved pixels of packed format. Models PixelIteratorConcept, HasDynamicXStepTypeConcept, MemoryBasedIteratorConcept
  228. template <typename P, typename C, typename L>
  229. struct iterator_is_mutable<packed_pixel<P, C, L>*>
  230. : std::integral_constant<bool, packed_pixel<P, C, L>::is_mutable>
  231. {};
  232. template <typename P, typename C, typename L>
  233. struct iterator_is_mutable<const packed_pixel<P, C, L>*> : std::false_type {};
  234. }} // namespace boost::gil
  235. #endif