pixel.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_CONCEPTS_PIXEL_HPP
  9. #define BOOST_GIL_CONCEPTS_PIXEL_HPP
  10. #include <boost/gil/concepts/basic.hpp>
  11. #include <boost/gil/concepts/channel.hpp>
  12. #include <boost/gil/concepts/color.hpp>
  13. #include <boost/gil/concepts/color_base.hpp>
  14. #include <boost/gil/concepts/concept_check.hpp>
  15. #include <boost/gil/concepts/fwd.hpp>
  16. #include <boost/gil/concepts/pixel_based.hpp>
  17. #include <boost/gil/concepts/detail/type_traits.hpp>
  18. #include <boost/gil/detail/mp11.hpp>
  19. #include <cstddef>
  20. #include <type_traits>
  21. #if defined(BOOST_CLANG)
  22. #pragma clang diagnostic push
  23. #pragma clang diagnostic ignored "-Wunused-local-typedefs"
  24. #endif
  25. #if defined(BOOST_GCC) && (BOOST_GCC >= 40900)
  26. #pragma GCC diagnostic push
  27. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  28. #endif
  29. namespace boost { namespace gil {
  30. /// \brief Pixel concept - A color base whose elements are channels
  31. /// \ingroup PixelConcept
  32. /// \code
  33. /// concept PixelConcept<typename P> : ColorBaseConcept<P>, PixelBasedConcept<P>
  34. /// {
  35. /// where is_pixel<P>::value == true;
  36. /// // where for each K [0..size<P>::value - 1]:
  37. /// // ChannelConcept<kth_element_type<P, K>>;
  38. ///
  39. /// typename P::value_type;
  40. /// where PixelValueConcept<value_type>;
  41. /// typename P::reference;
  42. /// where PixelConcept<reference>;
  43. /// typename P::const_reference;
  44. /// where PixelConcept<const_reference>;
  45. /// static const bool P::is_mutable;
  46. ///
  47. /// template <PixelConcept P2> where { PixelConcept<P, P2> }
  48. /// P::P(P2);
  49. /// template <PixelConcept P2> where { PixelConcept<P, P2> }
  50. /// bool operator==(const P&, const P2&);
  51. /// template <PixelConcept P2> where { PixelConcept<P, P2> }
  52. /// bool operator!=(const P&, const P2&);
  53. /// };
  54. /// \endcode
  55. template <typename P>
  56. struct PixelConcept
  57. {
  58. void constraints()
  59. {
  60. gil_function_requires<ColorBaseConcept<P>>();
  61. gil_function_requires<PixelBasedConcept<P>>();
  62. static_assert(is_pixel<P>::value, "");
  63. static const bool is_mutable = P::is_mutable;
  64. ignore_unused_variable_warning(is_mutable);
  65. using value_type = typename P::value_type;
  66. // TODO: Is the cyclic dependency intentional? --mloskot
  67. // gil_function_requires<PixelValueConcept<value_type>>();
  68. using reference = typename P::reference;
  69. gil_function_requires<PixelConcept
  70. <
  71. typename detail::remove_const_and_reference<reference>::type
  72. >>();
  73. using const_reference = typename P::const_reference;
  74. gil_function_requires<PixelConcept
  75. <
  76. typename detail::remove_const_and_reference<const_reference>::type
  77. >>();
  78. }
  79. };
  80. /// \brief Pixel concept that allows for changing its channels
  81. /// \ingroup PixelConcept
  82. /// \code
  83. /// concept MutablePixelConcept<PixelConcept P> : MutableColorBaseConcept<P>
  84. /// {
  85. /// where is_mutable==true;
  86. /// };
  87. /// \endcode
  88. template <typename P>
  89. struct MutablePixelConcept
  90. {
  91. void constraints()
  92. {
  93. gil_function_requires<PixelConcept<P>>();
  94. static_assert(P::is_mutable, "");
  95. }
  96. };
  97. /// \brief Homogeneous pixel concept
  98. /// \ingroup PixelConcept
  99. /// \code
  100. /// concept HomogeneousPixelConcept<PixelConcept P>
  101. /// : HomogeneousColorBaseConcept<P>, HomogeneousPixelBasedConcept<P>
  102. /// {
  103. /// P::template element_const_reference_type<P>::type operator[](P p, std::size_t i) const
  104. /// {
  105. /// return dynamic_at_c(p,i);
  106. /// }
  107. /// };
  108. /// \endcode
  109. template <typename P>
  110. struct HomogeneousPixelConcept
  111. {
  112. void constraints()
  113. {
  114. gil_function_requires<PixelConcept<P>>();
  115. gil_function_requires<HomogeneousColorBaseConcept<P>>();
  116. gil_function_requires<HomogeneousPixelBasedConcept<P>>();
  117. p[0];
  118. }
  119. P p;
  120. };
  121. /// \brief Homogeneous pixel concept that allows for changing its channels
  122. /// \ingroup PixelConcept
  123. /// \code
  124. /// concept MutableHomogeneousPixelConcept<HomogeneousPixelConcept P>
  125. /// : MutableHomogeneousColorBaseConcept<P>
  126. /// {
  127. /// P::template element_reference_type<P>::type operator[](P p, std::size_t i)
  128. /// {
  129. /// return dynamic_at_c(p, i);
  130. /// }
  131. /// };
  132. /// \endcode
  133. template <typename P>
  134. struct MutableHomogeneousPixelConcept
  135. {
  136. void constraints()
  137. {
  138. gil_function_requires<HomogeneousPixelConcept<P>>();
  139. gil_function_requires<MutableHomogeneousColorBaseConcept<P>>();
  140. p[0] = v;
  141. v = p[0];
  142. }
  143. typename P::template element_type<P>::type v;
  144. P p;
  145. };
  146. /// \brief Pixel concept that is a Regular type
  147. /// \ingroup PixelConcept
  148. /// \code
  149. /// concept PixelValueConcept<PixelConcept P> : Regular<P>
  150. /// {
  151. /// where SameType<value_type,P>;
  152. /// };
  153. /// \endcode
  154. template <typename P>
  155. struct PixelValueConcept
  156. {
  157. void constraints()
  158. {
  159. gil_function_requires<PixelConcept<P>>();
  160. gil_function_requires<Regular<P>>();
  161. }
  162. };
  163. /// \brief Homogeneous pixel concept that is a Regular type
  164. /// \ingroup PixelConcept
  165. /// \code
  166. /// concept HomogeneousPixelValueConcept<HomogeneousPixelConcept P> : Regular<P>
  167. /// {
  168. /// where SameType<value_type,P>;
  169. /// };
  170. /// \endcode
  171. template <typename P>
  172. struct HomogeneousPixelValueConcept
  173. {
  174. void constraints()
  175. {
  176. gil_function_requires<HomogeneousPixelConcept<P>>();
  177. gil_function_requires<Regular<P>>();
  178. static_assert(std::is_same<P, typename P::value_type>::value, "");
  179. }
  180. };
  181. namespace detail {
  182. template <typename P1, typename P2, int K>
  183. struct channels_are_pairwise_compatible
  184. : mp11::mp_and
  185. <
  186. channels_are_pairwise_compatible<P1, P2, K - 1>,
  187. channels_are_compatible
  188. <
  189. typename kth_semantic_element_reference_type<P1, K>::type,
  190. typename kth_semantic_element_reference_type<P2, K>::type
  191. >
  192. >
  193. {
  194. };
  195. template <typename P1, typename P2>
  196. struct channels_are_pairwise_compatible<P1, P2, -1> : std::true_type {};
  197. } // namespace detail
  198. /// \ingroup PixelAlgorithm
  199. /// \brief Returns whether two pixels are compatible
  200. /// Pixels are compatible if their channels and color space types are compatible.
  201. /// Compatible pixels can be assigned and copy constructed from one another.
  202. /// \tparam P1 Models PixelConcept
  203. /// \tparam P2 Models PixelConcept
  204. template <typename P1, typename P2>
  205. struct pixels_are_compatible
  206. : mp11::mp_and
  207. <
  208. typename color_spaces_are_compatible
  209. <
  210. typename color_space_type<P1>::type,
  211. typename color_space_type<P2>::type
  212. >::type,
  213. detail::channels_are_pairwise_compatible
  214. <
  215. P1, P2, num_channels<P1>::value - 1
  216. >
  217. >
  218. {
  219. };
  220. /// \ingroup PixelConcept
  221. /// \brief Concept for pixel compatibility
  222. /// Pixels are compatible if their channels and color space types are compatible.
  223. /// Compatible pixels can be assigned and copy constructed from one another.
  224. /// \tparam P1 Models PixelConcept
  225. /// \tparam P2 Models PixelConcept
  226. /// \code
  227. /// concept PixelsCompatibleConcept<PixelConcept P1, PixelConcept P2>
  228. /// : ColorBasesCompatibleConcept<P1,P2> {
  229. /// // where for each K [0..size<P1>::value):
  230. /// // ChannelsCompatibleConcept<kth_semantic_element_type<P1,K>::type, kth_semantic_element_type<P2,K>::type>;
  231. /// };
  232. /// \endcode
  233. template <typename P1, typename P2>
  234. struct PixelsCompatibleConcept
  235. {
  236. void constraints()
  237. {
  238. static_assert(pixels_are_compatible<P1, P2>::value, "");
  239. }
  240. };
  241. /// \ingroup PixelConcept
  242. /// \brief Pixel convertible concept
  243. /// Convertibility is non-symmetric and implies that one pixel
  244. /// can be converted to another, approximating the color.
  245. /// Conversion is explicit and sometimes lossy.
  246. /// \code
  247. /// template <PixelConcept SrcPixel, MutablePixelConcept DstPixel>
  248. /// concept PixelConvertibleConcept
  249. /// {
  250. /// void color_convert(const SrcPixel&, DstPixel&);
  251. /// };
  252. /// \endcode
  253. template <typename SrcP, typename DstP>
  254. struct PixelConvertibleConcept
  255. {
  256. void constraints()
  257. {
  258. gil_function_requires<PixelConcept<SrcP>>();
  259. gil_function_requires<MutablePixelConcept<DstP>>();
  260. color_convert(src, dst);
  261. }
  262. SrcP src;
  263. DstP dst;
  264. };
  265. }} // namespace boost::gil
  266. #if defined(BOOST_CLANG)
  267. #pragma clang diagnostic pop
  268. #endif
  269. #if defined(BOOST_GCC) && (BOOST_GCC >= 40900)
  270. #pragma GCC diagnostic pop
  271. #endif
  272. #endif