pixel.hpp 8.5 KB

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