color_base_algorithm.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
  4. //
  5. // Distributed under the Boost Software License, Version 1.0
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt
  8. //
  9. #ifndef BOOST_GIL_COLOR_BASE_ALGORITHM_HPP
  10. #define BOOST_GIL_COLOR_BASE_ALGORITHM_HPP
  11. #include <boost/gil/concepts.hpp>
  12. #include <boost/gil/utilities.hpp>
  13. #include <boost/gil/detail/mp11.hpp>
  14. #include <boost/config.hpp>
  15. #include <algorithm>
  16. #include <type_traits>
  17. namespace boost { namespace gil {
  18. ///////////////////////////////////////
  19. /// size: Semantic channel size
  20. ///////////////////////////////////////
  21. /**
  22. \defgroup ColorBaseAlgorithmSize size
  23. \ingroup ColorBaseAlgorithm
  24. \brief Returns an integral constant type specifying the number of elements in a color base
  25. Example:
  26. \code
  27. static_assert(size<rgb8_pixel_t>::value == 3, "");
  28. static_assert(size<cmyk8_planar_ptr_t>::value == 4, "");
  29. \endcode
  30. */
  31. /// \brief Returns an integral constant type specifying the number of elements in a color base
  32. /// \ingroup ColorBaseAlgorithmSize
  33. template <typename ColorBase>
  34. struct size : public mp11::mp_size<typename ColorBase::layout_t::color_space_t> {};
  35. ///////////////////////////////////////
  36. /// semantic_at_c: Semantic channel accessors
  37. ///////////////////////////////////////
  38. /**
  39. \defgroup ColorBaseAlgorithmSemanticAtC kth_semantic_element_type, kth_semantic_element_reference_type, kth_semantic_element_const_reference_type, semantic_at_c
  40. \ingroup ColorBaseAlgorithm
  41. \brief Support for accessing the elements of a color base by semantic index
  42. The semantic index of an element is the index of its color in the color space. Semantic indexing allows for proper pairing of elements of color bases
  43. independent on their layout. For example, red is the first semantic element of a color base regardless of whether it has an RGB layout or a BGR layout.
  44. All GIL color base algorithms taking multiple color bases use semantic indexing to access their elements.
  45. Example:
  46. \code
  47. // 16-bit BGR pixel, 4 bits for the blue, 3 bits for the green, 2 bits for the red channel and 7 unused bits
  48. using bgr432_pixel_t = packed_pixel_type<uint16_t, mp11::mp_list_c<unsigned,4,3,2>, bgr_layout_t>::type;
  49. // A reference to its red channel. Although the red channel is the third, its semantic index is 0 in the RGB color space
  50. using red_channel_reference_t = kth_semantic_element_reference_type<bgr432_pixel_t, 0>::type;
  51. // Initialize the pixel to black
  52. bgr432_pixel_t red_pixel(0,0,0);
  53. // Set the red channel to 100%
  54. red_channel_reference_t red_channel = semantic_at_c<0>(red_pixel);
  55. red_channel = channel_traits<red_channel_reference_t>::max_value();
  56. \endcode
  57. */
  58. /// \brief Specifies the type of the K-th semantic element of a color base
  59. /// \ingroup ColorBaseAlgorithmSemanticAtC
  60. template <typename ColorBase, int K>
  61. struct kth_semantic_element_type
  62. {
  63. using channel_mapping_t = typename ColorBase::layout_t::channel_mapping_t;
  64. static_assert(K < mp11::mp_size<channel_mapping_t>::value,
  65. "K index should be less than size of channel_mapping_t sequence");
  66. static constexpr int semantic_index = mp11::mp_at_c<channel_mapping_t, K>::type::value;
  67. using type = typename kth_element_type<ColorBase, semantic_index>::type;
  68. };
  69. /// \brief Specifies the return type of the mutable semantic_at_c<K>(color_base);
  70. /// \ingroup ColorBaseAlgorithmSemanticAtC
  71. template <typename ColorBase, int K>
  72. struct kth_semantic_element_reference_type
  73. {
  74. using channel_mapping_t = typename ColorBase::layout_t::channel_mapping_t;
  75. static_assert(K < mp11::mp_size<channel_mapping_t>::value,
  76. "K index should be less than size of channel_mapping_t sequence");
  77. static constexpr int semantic_index = mp11::mp_at_c<channel_mapping_t, K>::type::value;
  78. using type = typename kth_element_reference_type<ColorBase, semantic_index>::type;
  79. static type get(ColorBase& cb) { return gil::at_c<semantic_index>(cb); }
  80. };
  81. /// \brief Specifies the return type of the constant semantic_at_c<K>(color_base);
  82. /// \ingroup ColorBaseAlgorithmSemanticAtC
  83. template <typename ColorBase, int K>
  84. struct kth_semantic_element_const_reference_type
  85. {
  86. using channel_mapping_t = typename ColorBase::layout_t::channel_mapping_t;
  87. static_assert(K < mp11::mp_size<channel_mapping_t>::value,
  88. "K index should be less than size of channel_mapping_t sequence");
  89. static constexpr int semantic_index = mp11::mp_at_c<channel_mapping_t, K>::type::value;
  90. using type = typename kth_element_const_reference_type<ColorBase,semantic_index>::type;
  91. static type get(const ColorBase& cb) { return gil::at_c<semantic_index>(cb); }
  92. };
  93. /// \brief A mutable accessor to the K-th semantic element of a color base
  94. /// \ingroup ColorBaseAlgorithmSemanticAtC
  95. template <int K, typename ColorBase>
  96. inline
  97. auto semantic_at_c(ColorBase& p)
  98. -> typename std::enable_if
  99. <
  100. !std::is_const<ColorBase>::value,
  101. typename kth_semantic_element_reference_type<ColorBase, K>::type
  102. >::type
  103. {
  104. return kth_semantic_element_reference_type<ColorBase, K>::get(p);
  105. }
  106. /// \brief A constant accessor to the K-th semantic element of a color base
  107. /// \ingroup ColorBaseAlgorithmSemanticAtC
  108. template <int K, typename ColorBase>
  109. inline
  110. auto semantic_at_c(ColorBase const& p)
  111. -> typename kth_semantic_element_const_reference_type<ColorBase, K>::type
  112. {
  113. return kth_semantic_element_const_reference_type<ColorBase, K>::get(p);
  114. }
  115. ///////////////////////////////////////
  116. /// get_color: Named channel accessors
  117. ///////////////////////////////////////
  118. /**
  119. \defgroup ColorBaseAlgorithmColor color_element_type, color_element_reference_type, color_element_const_reference_type, get_color, contains_color
  120. \ingroup ColorBaseAlgorithm
  121. \brief Support for accessing the elements of a color base by color name
  122. Example: A function that takes a generic pixel containing a red channel and sets it to 100%:
  123. \code
  124. template <typename Pixel>
  125. void set_red_to_max(Pixel& pixel) {
  126. boost::function_requires<MutablePixelConcept<Pixel> >();
  127. static_assert(contains_color<Pixel, red_t>::value, "");
  128. using red_channel_t = typename color_element_type<Pixel, red_t>::type;
  129. get_color(pixel, red_t()) = channel_traits<red_channel_t>::max_value();
  130. }
  131. \endcode
  132. */
  133. /// \brief A predicate metafunction determining whether a given color base contains a given color
  134. /// \ingroup ColorBaseAlgorithmColor
  135. template <typename ColorBase, typename Color>
  136. struct contains_color
  137. : mp11::mp_contains<typename ColorBase::layout_t::color_space_t, Color>
  138. {};
  139. template <typename ColorBase, typename Color>
  140. struct color_index_type : public detail::type_to_index<typename ColorBase::layout_t::color_space_t,Color> {};
  141. /// \brief Specifies the type of the element associated with a given color tag
  142. /// \ingroup ColorBaseAlgorithmColor
  143. template <typename ColorBase, typename Color>
  144. struct color_element_type : public kth_semantic_element_type<ColorBase,color_index_type<ColorBase,Color>::value> {};
  145. /// \brief Specifies the return type of the mutable element accessor by color name, get_color(color_base, Color());
  146. /// \ingroup ColorBaseAlgorithmColor
  147. template <typename ColorBase, typename Color>
  148. struct color_element_reference_type : public kth_semantic_element_reference_type<ColorBase,color_index_type<ColorBase,Color>::value> {};
  149. /// \brief Specifies the return type of the constant element accessor by color name, get_color(color_base, Color());
  150. /// \ingroup ColorBaseAlgorithmColor
  151. template <typename ColorBase, typename Color>
  152. struct color_element_const_reference_type : public kth_semantic_element_const_reference_type<ColorBase,color_index_type<ColorBase,Color>::value> {};
  153. /// \brief Mutable accessor to the element associated with a given color name
  154. /// \ingroup ColorBaseAlgorithmColor
  155. template <typename ColorBase, typename Color>
  156. typename color_element_reference_type<ColorBase,Color>::type get_color(ColorBase& cb, Color=Color()) {
  157. return color_element_reference_type<ColorBase,Color>::get(cb);
  158. }
  159. /// \brief Constant accessor to the element associated with a given color name
  160. /// \ingroup ColorBaseAlgorithmColor
  161. template <typename ColorBase, typename Color>
  162. typename color_element_const_reference_type<ColorBase,Color>::type get_color(const ColorBase& cb, Color=Color()) {
  163. return color_element_const_reference_type<ColorBase,Color>::get(cb);
  164. }
  165. ///////////////////////////////////////
  166. ///
  167. /// element_type, element_reference_type, element_const_reference_type: Support for homogeneous color bases
  168. ///
  169. ///////////////////////////////////////
  170. /**
  171. \defgroup ColorBaseAlgorithmHomogeneous element_type, element_reference_type, element_const_reference_type
  172. \ingroup ColorBaseAlgorithm
  173. \brief Types for homogeneous color bases
  174. Example:
  175. \code
  176. using element_t = element_type<rgb8c_planar_ptr_t>::type;
  177. static_assert(std::is_same<element_t, const uint8_t*>::value, "");
  178. \endcode
  179. */
  180. /// \brief Specifies the element type of a homogeneous color base
  181. /// \ingroup ColorBaseAlgorithmHomogeneous
  182. template <typename ColorBase>
  183. struct element_type : public kth_element_type<ColorBase, 0> {};
  184. /// \brief Specifies the return type of the mutable element accessor at_c of a homogeneous color base
  185. /// \ingroup ColorBaseAlgorithmHomogeneous
  186. template <typename ColorBase>
  187. struct element_reference_type : public kth_element_reference_type<ColorBase, 0> {};
  188. /// \brief Specifies the return type of the constant element accessor at_c of a homogeneous color base
  189. /// \ingroup ColorBaseAlgorithmHomogeneous
  190. template <typename ColorBase>
  191. struct element_const_reference_type : public kth_element_const_reference_type<ColorBase, 0> {};
  192. namespace detail {
  193. // compile-time recursion for per-element operations on color bases
  194. template <int N>
  195. struct element_recursion
  196. {
  197. #if defined(BOOST_GCC) && (BOOST_GCC >= 40900)
  198. #pragma GCC diagnostic push
  199. #pragma GCC diagnostic ignored "-Wconversion"
  200. #pragma GCC diagnostic ignored "-Wfloat-equal"
  201. #endif
  202. template <typename P1,typename P2>
  203. static bool static_equal(const P1& p1, const P2& p2)
  204. {
  205. return element_recursion<N-1>::static_equal(p1,p2) &&
  206. semantic_at_c<N-1>(p1)==semantic_at_c<N-1>(p2);
  207. }
  208. template <typename P1,typename P2>
  209. static void static_copy(const P1& p1, P2& p2)
  210. {
  211. element_recursion<N-1>::static_copy(p1,p2);
  212. semantic_at_c<N-1>(p2)=semantic_at_c<N-1>(p1);
  213. }
  214. template <typename P,typename T2>
  215. static void static_fill(P& p, T2 v)
  216. {
  217. element_recursion<N-1>::static_fill(p,v);
  218. semantic_at_c<N-1>(p)=v;
  219. }
  220. template <typename Dst,typename Op>
  221. static void static_generate(Dst& dst, Op op)
  222. {
  223. element_recursion<N-1>::static_generate(dst,op);
  224. semantic_at_c<N-1>(dst)=op();
  225. }
  226. #if defined(BOOST_GCC) && (BOOST_GCC >= 40900)
  227. #pragma GCC diagnostic pop
  228. #endif
  229. //static_for_each with one source
  230. template <typename P1,typename Op>
  231. static Op static_for_each(P1& p1, Op op) {
  232. Op op2(element_recursion<N-1>::static_for_each(p1,op));
  233. op2(semantic_at_c<N-1>(p1));
  234. return op2;
  235. }
  236. template <typename P1,typename Op>
  237. static Op static_for_each(const P1& p1, Op op) {
  238. Op op2(element_recursion<N-1>::static_for_each(p1,op));
  239. op2(semantic_at_c<N-1>(p1));
  240. return op2;
  241. }
  242. //static_for_each with two sources
  243. template <typename P1,typename P2,typename Op>
  244. static Op static_for_each(P1& p1, P2& p2, Op op) {
  245. Op op2(element_recursion<N-1>::static_for_each(p1,p2,op));
  246. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2));
  247. return op2;
  248. }
  249. template <typename P1,typename P2,typename Op>
  250. static Op static_for_each(P1& p1, const P2& p2, Op op) {
  251. Op op2(element_recursion<N-1>::static_for_each(p1,p2,op));
  252. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2));
  253. return op2;
  254. }
  255. template <typename P1,typename P2,typename Op>
  256. static Op static_for_each(const P1& p1, P2& p2, Op op) {
  257. Op op2(element_recursion<N-1>::static_for_each(p1,p2,op));
  258. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2));
  259. return op2;
  260. }
  261. template <typename P1,typename P2,typename Op>
  262. static Op static_for_each(const P1& p1, const P2& p2, Op op) {
  263. Op op2(element_recursion<N-1>::static_for_each(p1,p2,op));
  264. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2));
  265. return op2;
  266. }
  267. //static_for_each with three sources
  268. template <typename P1,typename P2,typename P3,typename Op>
  269. static Op static_for_each(P1& p1, P2& p2, P3& p3, Op op) {
  270. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  271. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  272. return op2;
  273. }
  274. template <typename P1,typename P2,typename P3,typename Op>
  275. static Op static_for_each(P1& p1, P2& p2, const P3& p3, Op op) {
  276. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  277. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  278. return op2;
  279. }
  280. template <typename P1,typename P2,typename P3,typename Op>
  281. static Op static_for_each(P1& p1, const P2& p2, P3& p3, Op op) {
  282. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  283. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  284. return op2;
  285. }
  286. template <typename P1,typename P2,typename P3,typename Op>
  287. static Op static_for_each(P1& p1, const P2& p2, const P3& p3, Op op) {
  288. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  289. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  290. return op2;
  291. }
  292. template <typename P1,typename P2,typename P3,typename Op>
  293. static Op static_for_each(const P1& p1, P2& p2, P3& p3, Op op) {
  294. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  295. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  296. return op2;
  297. }
  298. template <typename P1,typename P2,typename P3,typename Op>
  299. static Op static_for_each(const P1& p1, P2& p2, const P3& p3, Op op) {
  300. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  301. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  302. return op2;
  303. }
  304. template <typename P1,typename P2,typename P3,typename Op>
  305. static Op static_for_each(const P1& p1, const P2& p2, P3& p3, Op op) {
  306. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  307. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  308. return op2;
  309. }
  310. template <typename P1,typename P2,typename P3,typename Op>
  311. static Op static_for_each(const P1& p1, const P2& p2, const P3& p3, Op op) {
  312. Op op2(element_recursion<N-1>::static_for_each(p1,p2,p3,op));
  313. op2(semantic_at_c<N-1>(p1), semantic_at_c<N-1>(p2), semantic_at_c<N-1>(p3));
  314. return op2;
  315. }
  316. //static_transform with one source
  317. template <typename P1,typename Dst,typename Op>
  318. static Op static_transform(P1& src, Dst& dst, Op op) {
  319. Op op2(element_recursion<N-1>::static_transform(src,dst,op));
  320. semantic_at_c<N-1>(dst)=op2(semantic_at_c<N-1>(src));
  321. return op2;
  322. }
  323. template <typename P1,typename Dst,typename Op>
  324. static Op static_transform(const P1& src, Dst& dst, Op op) {
  325. Op op2(element_recursion<N-1>::static_transform(src,dst,op));
  326. semantic_at_c<N-1>(dst)=op2(semantic_at_c<N-1>(src));
  327. return op2;
  328. }
  329. //static_transform with two sources
  330. template <typename P1,typename P2,typename Dst,typename Op>
  331. static Op static_transform(P1& src1, P2& src2, Dst& dst, Op op) {
  332. Op op2(element_recursion<N-1>::static_transform(src1,src2,dst,op));
  333. semantic_at_c<N-1>(dst)=op2(semantic_at_c<N-1>(src1), semantic_at_c<N-1>(src2));
  334. return op2;
  335. }
  336. template <typename P1,typename P2,typename Dst,typename Op>
  337. static Op static_transform(P1& src1, const P2& src2, Dst& dst, Op op) {
  338. Op op2(element_recursion<N-1>::static_transform(src1,src2,dst,op));
  339. semantic_at_c<N-1>(dst)=op2(semantic_at_c<N-1>(src1), semantic_at_c<N-1>(src2));
  340. return op2;
  341. }
  342. template <typename P1,typename P2,typename Dst,typename Op>
  343. static Op static_transform(const P1& src1, P2& src2, Dst& dst, Op op) {
  344. Op op2(element_recursion<N-1>::static_transform(src1,src2,dst,op));
  345. semantic_at_c<N-1>(dst)=op2(semantic_at_c<N-1>(src1), semantic_at_c<N-1>(src2));
  346. return op2;
  347. }
  348. template <typename P1,typename P2,typename Dst,typename Op>
  349. static Op static_transform(const P1& src1, const P2& src2, Dst& dst, Op op) {
  350. Op op2(element_recursion<N-1>::static_transform(src1,src2,dst,op));
  351. semantic_at_c<N-1>(dst)=op2(semantic_at_c<N-1>(src1), semantic_at_c<N-1>(src2));
  352. return op2;
  353. }
  354. };
  355. // Termination condition of the compile-time recursion for element operations on a color base
  356. template<> struct element_recursion<0> {
  357. //static_equal
  358. template <typename P1,typename P2>
  359. static bool static_equal(const P1&, const P2&) { return true; }
  360. //static_copy
  361. template <typename P1,typename P2>
  362. static void static_copy(const P1&, const P2&) {}
  363. //static_fill
  364. template <typename P, typename T2>
  365. static void static_fill(const P&, T2) {}
  366. //static_generate
  367. template <typename Dst,typename Op>
  368. static void static_generate(const Dst&,Op){}
  369. //static_for_each with one source
  370. template <typename P1,typename Op>
  371. static Op static_for_each(const P1&,Op op){return op;}
  372. //static_for_each with two sources
  373. template <typename P1,typename P2,typename Op>
  374. static Op static_for_each(const P1&,const P2&,Op op){return op;}
  375. //static_for_each with three sources
  376. template <typename P1,typename P2,typename P3,typename Op>
  377. static Op static_for_each(const P1&,const P2&,const P3&,Op op){return op;}
  378. //static_transform with one source
  379. template <typename P1,typename Dst,typename Op>
  380. static Op static_transform(const P1&,const Dst&,Op op){return op;}
  381. //static_transform with two sources
  382. template <typename P1,typename P2,typename Dst,typename Op>
  383. static Op static_transform(const P1&,const P2&,const Dst&,Op op){return op;}
  384. };
  385. // std::min and std::max don't have the mutable overloads...
  386. template <typename Q> inline const Q& mutable_min(const Q& x, const Q& y) { return x<y ? x : y; }
  387. template <typename Q> inline Q& mutable_min( Q& x, Q& y) { return x<y ? x : y; }
  388. template <typename Q> inline const Q& mutable_max(const Q& x, const Q& y) { return x<y ? y : x; }
  389. template <typename Q> inline Q& mutable_max( Q& x, Q& y) { return x<y ? y : x; }
  390. // compile-time recursion for min/max element
  391. template <int N>
  392. struct min_max_recur {
  393. template <typename P> static typename element_const_reference_type<P>::type max_(const P& p) {
  394. return mutable_max(min_max_recur<N-1>::max_(p),semantic_at_c<N-1>(p));
  395. }
  396. template <typename P> static typename element_reference_type<P>::type max_( P& p) {
  397. return mutable_max(min_max_recur<N-1>::max_(p),semantic_at_c<N-1>(p));
  398. }
  399. template <typename P> static typename element_const_reference_type<P>::type min_(const P& p) {
  400. return mutable_min(min_max_recur<N-1>::min_(p),semantic_at_c<N-1>(p));
  401. }
  402. template <typename P> static typename element_reference_type<P>::type min_( P& p) {
  403. return mutable_min(min_max_recur<N-1>::min_(p),semantic_at_c<N-1>(p));
  404. }
  405. };
  406. // termination condition of the compile-time recursion for min/max element
  407. template <>
  408. struct min_max_recur<1> {
  409. template <typename P> static typename element_const_reference_type<P>::type max_(const P& p) { return semantic_at_c<0>(p); }
  410. template <typename P> static typename element_reference_type<P>::type max_( P& p) { return semantic_at_c<0>(p); }
  411. template <typename P> static typename element_const_reference_type<P>::type min_(const P& p) { return semantic_at_c<0>(p); }
  412. template <typename P> static typename element_reference_type<P>::type min_( P& p) { return semantic_at_c<0>(p); }
  413. };
  414. } // namespace detail
  415. /// \defgroup ColorBaseAlgorithmMinMax static_min, static_max
  416. /// \ingroup ColorBaseAlgorithm
  417. /// \brief Equivalents to std::min_element and std::max_element for homogeneous color bases
  418. ///
  419. /// Example:
  420. /// \code
  421. /// rgb8_pixel_t pixel(10,20,30);
  422. /// assert(pixel[2] == 30);
  423. /// static_max(pixel) = static_min(pixel);
  424. /// assert(pixel[2] == 10);
  425. /// \endcode
  426. /// \{
  427. template <typename P>
  428. BOOST_FORCEINLINE
  429. typename element_const_reference_type<P>::type static_max(const P& p) { return detail::min_max_recur<size<P>::value>::max_(p); }
  430. template <typename P>
  431. BOOST_FORCEINLINE
  432. typename element_reference_type<P>::type static_max( P& p) { return detail::min_max_recur<size<P>::value>::max_(p); }
  433. template <typename P>
  434. BOOST_FORCEINLINE
  435. typename element_const_reference_type<P>::type static_min(const P& p) { return detail::min_max_recur<size<P>::value>::min_(p); }
  436. template <typename P>
  437. BOOST_FORCEINLINE
  438. typename element_reference_type<P>::type static_min( P& p) { return detail::min_max_recur<size<P>::value>::min_(p); }
  439. /// \}
  440. /// \defgroup ColorBaseAlgorithmEqual static_equal
  441. /// \ingroup ColorBaseAlgorithm
  442. /// \brief Equivalent to std::equal. Pairs the elements semantically
  443. ///
  444. /// Example:
  445. /// \code
  446. /// rgb8_pixel_t rgb_red(255,0,0);
  447. /// bgr8_pixel_t bgr_red(0,0,255);
  448. /// assert(rgb_red[0]==255 && bgr_red[0]==0);
  449. ///
  450. /// assert(static_equal(rgb_red,bgr_red));
  451. /// assert(rgb_red==bgr_red); // operator== invokes static_equal
  452. /// \endcode
  453. /// \{
  454. template <typename P1,typename P2>
  455. BOOST_FORCEINLINE
  456. bool static_equal(const P1& p1, const P2& p2) { return detail::element_recursion<size<P1>::value>::static_equal(p1,p2); }
  457. /// \}
  458. /// \defgroup ColorBaseAlgorithmCopy static_copy
  459. /// \ingroup ColorBaseAlgorithm
  460. /// \brief Equivalent to std::copy. Pairs the elements semantically
  461. ///
  462. /// Example:
  463. /// \code
  464. /// rgb8_pixel_t rgb_red(255,0,0);
  465. /// bgr8_pixel_t bgr_red;
  466. /// static_copy(rgb_red, bgr_red); // same as bgr_red = rgb_red
  467. ///
  468. /// assert(rgb_red[0] == 255 && bgr_red[0] == 0);
  469. /// assert(rgb_red == bgr_red);
  470. /// \endcode
  471. /// \{
  472. template <typename Src,typename Dst>
  473. BOOST_FORCEINLINE
  474. void static_copy(const Src& src, Dst& dst)
  475. {
  476. detail::element_recursion<size<Dst>::value>::static_copy(src, dst);
  477. }
  478. /// \}
  479. /// \defgroup ColorBaseAlgorithmFill static_fill
  480. /// \ingroup ColorBaseAlgorithm
  481. /// \brief Equivalent to std::fill.
  482. ///
  483. /// Example:
  484. /// \code
  485. /// rgb8_pixel_t p;
  486. /// static_fill(p, 10);
  487. /// assert(p == rgb8_pixel_t(10,10,10));
  488. /// \endcode
  489. /// \{
  490. template <typename P,typename V>
  491. BOOST_FORCEINLINE
  492. void static_fill(P& p, const V& v)
  493. {
  494. detail::element_recursion<size<P>::value>::static_fill(p,v);
  495. }
  496. /// \}
  497. /// \defgroup ColorBaseAlgorithmGenerate static_generate
  498. /// \ingroup ColorBaseAlgorithm
  499. /// \brief Equivalent to std::generate.
  500. ///
  501. /// Example: Set each channel of a pixel to its semantic index. The channels must be assignable from an integer.
  502. /// \code
  503. /// struct consecutive_fn {
  504. /// int& _current;
  505. /// consecutive_fn(int& start) : _current(start) {}
  506. /// int operator()() { return _current++; }
  507. /// };
  508. /// rgb8_pixel_t p;
  509. /// int start=0;
  510. /// static_generate(p, consecutive_fn(start));
  511. /// assert(p == rgb8_pixel_t(0,1,2));
  512. /// \endcode
  513. ///
  514. /// \{
  515. template <typename P1,typename Op>
  516. BOOST_FORCEINLINE
  517. void static_generate(P1& dst,Op op) { detail::element_recursion<size<P1>::value>::static_generate(dst,op); }
  518. /// \}
  519. /// \defgroup ColorBaseAlgorithmTransform static_transform
  520. /// \ingroup ColorBaseAlgorithm
  521. /// \brief Equivalent to std::transform. Pairs the elements semantically
  522. ///
  523. /// Example: Write a generic function that adds two pixels into a homogeneous result pixel.
  524. /// \code
  525. /// template <typename Result>
  526. /// struct my_plus {
  527. /// template <typename T1, typename T2>
  528. /// Result operator()(T1 f1, T2 f2) const { return f1+f2; }
  529. /// };
  530. ///
  531. /// template <typename Pixel1, typename Pixel2, typename Pixel3>
  532. /// void sum_channels(const Pixel1& p1, const Pixel2& p2, Pixel3& result) {
  533. /// using result_channel_t = typename channel_type<Pixel3>::type;
  534. /// static_transform(p1,p2,result,my_plus<result_channel_t>());
  535. /// }
  536. ///
  537. /// rgb8_pixel_t p1(1,2,3);
  538. /// bgr8_pixel_t p2(3,2,1);
  539. /// rgb8_pixel_t result;
  540. /// sum_channels(p1,p2,result);
  541. /// assert(result == rgb8_pixel_t(2,4,6));
  542. /// \endcode
  543. /// \{
  544. //static_transform with one source
  545. template <typename Src,typename Dst,typename Op>
  546. BOOST_FORCEINLINE
  547. Op static_transform(Src& src,Dst& dst,Op op) { return detail::element_recursion<size<Dst>::value>::static_transform(src,dst,op); }
  548. template <typename Src,typename Dst,typename Op>
  549. BOOST_FORCEINLINE
  550. Op static_transform(const Src& src,Dst& dst,Op op) { return detail::element_recursion<size<Dst>::value>::static_transform(src,dst,op); }
  551. //static_transform with two sources
  552. template <typename P2,typename P3,typename Dst,typename Op>
  553. BOOST_FORCEINLINE
  554. Op static_transform(P2& p2,P3& p3,Dst& dst,Op op) { return detail::element_recursion<size<Dst>::value>::static_transform(p2,p3,dst,op); }
  555. template <typename P2,typename P3,typename Dst,typename Op>
  556. BOOST_FORCEINLINE
  557. Op static_transform(P2& p2,const P3& p3,Dst& dst,Op op) { return detail::element_recursion<size<Dst>::value>::static_transform(p2,p3,dst,op); }
  558. template <typename P2,typename P3,typename Dst,typename Op>
  559. BOOST_FORCEINLINE
  560. Op static_transform(const P2& p2,P3& p3,Dst& dst,Op op) { return detail::element_recursion<size<Dst>::value>::static_transform(p2,p3,dst,op); }
  561. template <typename P2,typename P3,typename Dst,typename Op>
  562. BOOST_FORCEINLINE
  563. Op static_transform(const P2& p2,const P3& p3,Dst& dst,Op op) { return detail::element_recursion<size<Dst>::value>::static_transform(p2,p3,dst,op); }
  564. /// \}
  565. /// \defgroup ColorBaseAlgorithmForEach static_for_each
  566. /// \ingroup ColorBaseAlgorithm
  567. /// \brief Equivalent to std::for_each. Pairs the elements semantically
  568. ///
  569. /// Example: Use static_for_each to increment a planar pixel iterator
  570. /// \code
  571. /// struct increment {
  572. /// template <typename Incrementable>
  573. /// void operator()(Incrementable& x) const { ++x; }
  574. /// };
  575. ///
  576. /// template <typename ColorBase>
  577. /// void increment_elements(ColorBase& cb) {
  578. /// static_for_each(cb, increment());
  579. /// }
  580. ///
  581. /// uint8_t red[2], green[2], blue[2];
  582. /// rgb8c_planar_ptr_t p1(red,green,blue);
  583. /// rgb8c_planar_ptr_t p2=p1;
  584. /// increment_elements(p1);
  585. /// ++p2;
  586. /// assert(p1 == p2);
  587. /// \endcode
  588. /// \{
  589. //static_for_each with one source
  590. template <typename P1,typename Op>
  591. BOOST_FORCEINLINE
  592. Op static_for_each( P1& p1, Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,op); }
  593. template <typename P1,typename Op>
  594. BOOST_FORCEINLINE
  595. Op static_for_each(const P1& p1, Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,op); }
  596. //static_for_each with two sources
  597. template <typename P1,typename P2,typename Op>
  598. BOOST_FORCEINLINE
  599. Op static_for_each(P1& p1, P2& p2, Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,op); }
  600. template <typename P1,typename P2,typename Op>
  601. BOOST_FORCEINLINE
  602. Op static_for_each(P1& p1,const P2& p2, Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,op); }
  603. template <typename P1,typename P2,typename Op>
  604. BOOST_FORCEINLINE
  605. Op static_for_each(const P1& p1, P2& p2, Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,op); }
  606. template <typename P1,typename P2,typename Op>
  607. BOOST_FORCEINLINE
  608. Op static_for_each(const P1& p1,const P2& p2, Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,op); }
  609. //static_for_each with three sources
  610. template <typename P1,typename P2,typename P3,typename Op>
  611. BOOST_FORCEINLINE
  612. Op static_for_each(P1& p1,P2& p2,P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  613. template <typename P1,typename P2,typename P3,typename Op>
  614. BOOST_FORCEINLINE
  615. Op static_for_each(P1& p1,P2& p2,const P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  616. template <typename P1,typename P2,typename P3,typename Op>
  617. BOOST_FORCEINLINE
  618. Op static_for_each(P1& p1,const P2& p2,P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  619. template <typename P1,typename P2,typename P3,typename Op>
  620. BOOST_FORCEINLINE
  621. Op static_for_each(P1& p1,const P2& p2,const P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  622. template <typename P1,typename P2,typename P3,typename Op>
  623. BOOST_FORCEINLINE
  624. Op static_for_each(const P1& p1,P2& p2,P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  625. template <typename P1,typename P2,typename P3,typename Op>
  626. BOOST_FORCEINLINE
  627. Op static_for_each(const P1& p1,P2& p2,const P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  628. template <typename P1,typename P2,typename P3,typename Op>
  629. BOOST_FORCEINLINE
  630. Op static_for_each(const P1& p1,const P2& p2,P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  631. template <typename P1,typename P2,typename P3,typename Op>
  632. BOOST_FORCEINLINE
  633. Op static_for_each(const P1& p1,const P2& p2,const P3& p3,Op op) { return detail::element_recursion<size<P1>::value>::static_for_each(p1,p2,p3,op); }
  634. ///\}
  635. } } // namespace boost::gil
  636. #endif