packed_pixel.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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;
  67. gil::at_c<1>(*this) = chan1;
  68. }
  69. packed_pixel(int chan0, int chan1, int chan2)
  70. : _bitfield(0)
  71. {
  72. static_assert(num_channels<packed_pixel>::value == 3, "");
  73. gil::at_c<0>(*this) = chan0;
  74. gil::at_c<1>(*this) = chan1;
  75. gil::at_c<2>(*this) = chan2;
  76. }
  77. packed_pixel(int chan0, int chan1, int chan2, int chan3)
  78. : _bitfield(0)
  79. {
  80. static_assert(num_channels<packed_pixel>::value == 4, "");
  81. gil::at_c<0>(*this) = chan0;
  82. gil::at_c<1>(*this) = chan1;
  83. gil::at_c<2>(*this) = chan2;
  84. gil::at_c<3>(*this) = chan3;
  85. }
  86. packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4)
  87. : _bitfield(0)
  88. {
  89. static_assert(num_channels<packed_pixel>::value == 5, "");
  90. gil::at_c<0>(*this) = chan0;
  91. gil::at_c<1>(*this) = chan1;
  92. gil::at_c<2>(*this) = chan2;
  93. gil::at_c<3>(*this) = chan3;
  94. gil::at_c<4>(*this) = chan4;
  95. }
  96. auto operator=(packed_pixel const& p) -> packed_pixel&
  97. {
  98. _bitfield = p._bitfield;
  99. return *this;
  100. }
  101. template <typename Pixel>
  102. auto operator=(Pixel const& p) -> packed_pixel&
  103. {
  104. assign(p, is_pixel<Pixel>());
  105. return *this;
  106. }
  107. template <typename Pixel>
  108. bool operator==(Pixel const& p) const
  109. {
  110. return equal(p, is_pixel<Pixel>());
  111. }
  112. template <typename Pixel>
  113. bool operator!=(Pixel const& p) const { return !(*this==p); }
  114. private:
  115. template <typename Pixel>
  116. static void check_compatible()
  117. {
  118. gil_function_requires<PixelsCompatibleConcept<Pixel, packed_pixel>>();
  119. }
  120. template <typename Pixel>
  121. void assign(Pixel const& p, std::true_type)
  122. {
  123. check_compatible<Pixel>();
  124. static_copy(p, *this);
  125. }
  126. template <typename Pixel>
  127. bool equal(Pixel const& p, std::true_type) const
  128. {
  129. check_compatible<Pixel>();
  130. return static_equal(*this, p);
  131. }
  132. // Support for assignment/equality comparison of a channel with a grayscale pixel
  133. static void check_gray()
  134. {
  135. static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
  136. }
  137. template <typename Channel>
  138. void assign(Channel const& channel, std::false_type)
  139. {
  140. check_gray();
  141. gil::at_c<0>(*this) = channel;
  142. }
  143. template <typename Channel>
  144. bool equal (Channel const& channel, std::false_type) const
  145. {
  146. check_gray();
  147. return gil::at_c<0>(*this) == channel;
  148. }
  149. public:
  150. auto operator=(int channel) -> packed_pixel&
  151. {
  152. check_gray();
  153. gil::at_c<0>(*this) = channel;
  154. return *this;
  155. }
  156. bool operator==(int channel) const
  157. {
  158. check_gray();
  159. return gil::at_c<0>(*this) == channel;
  160. }
  161. };
  162. /////////////////////////////
  163. // ColorBasedConcept
  164. /////////////////////////////
  165. template <typename BitField, typename ChannelRefs, typename Layout, int K>
  166. struct kth_element_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
  167. {
  168. using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::value_type;
  169. };
  170. template <typename BitField, typename ChannelRefs, typename Layout, int K>
  171. struct kth_element_reference_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
  172. {
  173. using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::reference;
  174. };
  175. template <typename BitField, typename ChannelRefs, typename Layout, int K>
  176. struct kth_element_const_reference_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
  177. {
  178. using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::const_reference;
  179. };
  180. template <int K, typename P, typename C, typename L>
  181. inline
  182. auto at_c(packed_pixel<P, C, L>& p)
  183. -> typename kth_element_reference_type<packed_pixel<P, C, L>, K>::type
  184. {
  185. return typename kth_element_reference_type
  186. <
  187. packed_pixel<P, C, L>,
  188. K
  189. >::type{&p._bitfield};
  190. }
  191. template <int K, typename P, typename C, typename L>
  192. inline
  193. auto at_c(const packed_pixel<P, C, L>& p)
  194. -> typename kth_element_const_reference_type<packed_pixel<P, C, L>, K>::type
  195. {
  196. return typename kth_element_const_reference_type
  197. <
  198. packed_pixel<P, C, L>,
  199. K>::type{&p._bitfield};
  200. }
  201. /////////////////////////////
  202. // PixelConcept
  203. /////////////////////////////
  204. // Metafunction predicate that flags packed_pixel as a model of PixelConcept.
  205. // Required by PixelConcept
  206. template <typename BitField, typename ChannelRefs, typename Layout>
  207. struct is_pixel<packed_pixel<BitField, ChannelRefs, Layout>> : std::true_type {};
  208. /////////////////////////////
  209. // PixelBasedConcept
  210. /////////////////////////////
  211. template <typename P, typename C, typename Layout>
  212. struct color_space_type<packed_pixel<P, C, Layout>>
  213. {
  214. using type = typename Layout::color_space_t;
  215. };
  216. template <typename P, typename C, typename Layout>
  217. struct channel_mapping_type<packed_pixel<P, C, Layout>>
  218. {
  219. using type = typename Layout::channel_mapping_t;
  220. };
  221. template <typename P, typename C, typename Layout>
  222. struct is_planar<packed_pixel<P, C, Layout>> : std::false_type {};
  223. ////////////////////////////////////////////////////////////////////////////////
  224. /// Support for interleaved iterators over packed pixel
  225. ////////////////////////////////////////////////////////////////////////////////
  226. /// \defgroup PixelIteratorModelPackedInterleavedPtr Pointer to packed_pixel<P,CR,Layout>
  227. /// \ingroup PixelIteratorModel
  228. /// \brief Iterators over interleaved pixels.
  229. /// The pointer packed_pixel<P,CR,Layout>* is used as an iterator over interleaved
  230. /// pixels of packed format.
  231. /// Models PixelIteratorConcept, HasDynamicXStepTypeConcept, MemoryBasedIteratorConcept
  232. template <typename P, typename C, typename L>
  233. struct iterator_is_mutable<packed_pixel<P, C, L>*>
  234. : std::integral_constant<bool, packed_pixel<P, C, L>::is_mutable>
  235. {};
  236. template <typename P, typename C, typename L>
  237. struct iterator_is_mutable<const packed_pixel<P, C, L>*> : std::false_type {};
  238. }} // namespace boost::gil
  239. #endif