pixel.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "-Wunknown-pragmas"
  24. #pragma clang diagnostic ignored "-Wunused-local-typedefs"
  25. #endif
  26. #if defined(BOOST_GCC) && (BOOST_GCC >= 40900)
  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(std::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. : mp11::mp_and
  186. <
  187. channels_are_pairwise_compatible<P1, P2, K - 1>,
  188. channels_are_compatible
  189. <
  190. typename kth_semantic_element_reference_type<P1, K>::type,
  191. typename kth_semantic_element_reference_type<P2, K>::type
  192. >
  193. >
  194. {
  195. };
  196. template <typename P1, typename P2>
  197. struct channels_are_pairwise_compatible<P1, P2, -1> : std::true_type {};
  198. } // namespace detail
  199. /// \ingroup PixelAlgorithm
  200. /// \brief Returns whether two pixels are compatible
  201. /// Pixels are compatible if their channels and color space types are compatible.
  202. /// Compatible pixels can be assigned and copy constructed from one another.
  203. /// \tparam P1 Models PixelConcept
  204. /// \tparam P2 Models PixelConcept
  205. template <typename P1, typename P2>
  206. struct pixels_are_compatible
  207. : mp11::mp_and
  208. <
  209. typename color_spaces_are_compatible
  210. <
  211. typename color_space_type<P1>::type,
  212. typename color_space_type<P2>::type
  213. >::type,
  214. detail::channels_are_pairwise_compatible
  215. <
  216. P1, P2, num_channels<P1>::value - 1
  217. >
  218. >
  219. {
  220. };
  221. /// \ingroup PixelConcept
  222. /// \brief Concept for pixel compatibility
  223. /// Pixels are compatible if their channels and color space types are compatible.
  224. /// Compatible pixels can be assigned and copy constructed from one another.
  225. /// \tparam P1 Models PixelConcept
  226. /// \tparam P2 Models PixelConcept
  227. /// \code
  228. /// concept PixelsCompatibleConcept<PixelConcept P1, PixelConcept P2>
  229. /// : ColorBasesCompatibleConcept<P1,P2> {
  230. /// // where for each K [0..size<P1>::value):
  231. /// // ChannelsCompatibleConcept<kth_semantic_element_type<P1,K>::type, kth_semantic_element_type<P2,K>::type>;
  232. /// };
  233. /// \endcode
  234. template <typename P1, typename P2>
  235. struct PixelsCompatibleConcept
  236. {
  237. void constraints()
  238. {
  239. static_assert(pixels_are_compatible<P1, P2>::value, "");
  240. }
  241. };
  242. /// \ingroup PixelConcept
  243. /// \brief Pixel convertible concept
  244. /// Convertibility is non-symmetric and implies that one pixel
  245. /// can be converted to another, approximating the color.
  246. /// Conversion is explicit and sometimes lossy.
  247. /// \code
  248. /// template <PixelConcept SrcPixel, MutablePixelConcept DstPixel>
  249. /// concept PixelConvertibleConcept
  250. /// {
  251. /// void color_convert(const SrcPixel&, DstPixel&);
  252. /// };
  253. /// \endcode
  254. template <typename SrcP, typename DstP>
  255. struct PixelConvertibleConcept
  256. {
  257. void constraints()
  258. {
  259. gil_function_requires<PixelConcept<SrcP>>();
  260. gil_function_requires<MutablePixelConcept<DstP>>();
  261. color_convert(src, dst);
  262. }
  263. SrcP src;
  264. DstP dst;
  265. };
  266. }} // namespace boost::gil
  267. #if defined(BOOST_CLANG)
  268. #pragma clang diagnostic pop
  269. #endif
  270. #if defined(BOOST_GCC) && (BOOST_GCC >= 40900)
  271. #pragma GCC diagnostic pop
  272. #endif
  273. #endif