packed_pixel.hpp 8.8 KB

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