image_view_factory.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_EXTENSION_DYNAMIC_IMAGE_IMAGE_VIEW_FACTORY_HPP
  9. #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_IMAGE_VIEW_FACTORY_HPP
  10. #include <boost/gil/extension/dynamic_image/any_image_view.hpp>
  11. #include <boost/gil/dynamic_step.hpp>
  12. #include <boost/gil/image_view_factory.hpp>
  13. #include <boost/gil/point.hpp>
  14. #include <boost/gil/detail/mp11.hpp>
  15. #include <cstdint>
  16. namespace boost { namespace gil {
  17. // Methods for constructing any image views from other any image views
  18. // Extends image view factory to runtime type-specified views (any_image_view)
  19. namespace detail {
  20. template <typename ResultView>
  21. struct flipped_up_down_view_fn
  22. {
  23. using result_type = ResultView;
  24. template <typename View>
  25. auto operator()(View const& src) const -> result_type
  26. {
  27. return result_type{flipped_up_down_view(src)};
  28. }
  29. };
  30. template <typename ResultView>
  31. struct flipped_left_right_view_fn
  32. {
  33. using result_type = ResultView;
  34. template <typename View>
  35. auto operator()(View const& src) const -> result_type
  36. {
  37. return result_type{flipped_left_right_view(src)};
  38. }
  39. };
  40. template <typename ResultView>
  41. struct rotated90cw_view_fn
  42. {
  43. using result_type = ResultView;
  44. template <typename View>
  45. auto operator()(View const& src) const -> result_type
  46. {
  47. return result_type{rotated90cw_view(src)};
  48. }
  49. };
  50. template <typename ResultView>
  51. struct rotated90ccw_view_fn
  52. {
  53. using result_type = ResultView;
  54. template <typename View>
  55. auto operator()(View const& src) const -> result_type
  56. {
  57. return result_type{rotated90ccw_view(src)};
  58. }
  59. };
  60. template <typename ResultView>
  61. struct tranposed_view_fn
  62. {
  63. using result_type = ResultView;
  64. template <typename View>
  65. auto operator()(View const& src) const -> result_type
  66. {
  67. return result_type{tranposed_view(src)};
  68. }
  69. };
  70. template <typename ResultView>
  71. struct rotated180_view_fn
  72. {
  73. using result_type = ResultView;
  74. template <typename View>
  75. auto operator()(View const& src) const -> result_type
  76. {
  77. return result_type{rotated180_view(src)};
  78. }
  79. };
  80. template <typename ResultView>
  81. struct subimage_view_fn
  82. {
  83. using result_type = ResultView;
  84. subimage_view_fn(point_t const& topleft, point_t const& dimensions)
  85. : _topleft(topleft), _size2(dimensions)
  86. {}
  87. template <typename View>
  88. auto operator()(View const& src) const -> result_type
  89. {
  90. return result_type{subimage_view(src,_topleft,_size2)};
  91. }
  92. point_t _topleft;
  93. point_t _size2;
  94. };
  95. template <typename ResultView>
  96. struct subsampled_view_fn
  97. {
  98. using result_type = ResultView;
  99. subsampled_view_fn(point_t const& step) : _step(step) {}
  100. template <typename View>
  101. auto operator()(View const& src) const -> result_type
  102. {
  103. return result_type{subsampled_view(src,_step)};
  104. }
  105. point_t _step;
  106. };
  107. template <typename ResultView>
  108. struct nth_channel_view_fn
  109. {
  110. using result_type = ResultView;
  111. nth_channel_view_fn(int n) : _n(n) {}
  112. template <typename View>
  113. auto operator()(View const& src) const -> result_type
  114. {
  115. return result_type(nth_channel_view(src,_n));
  116. }
  117. int _n;
  118. };
  119. template <typename DstP, typename ResultView, typename CC = default_color_converter>
  120. struct color_converted_view_fn
  121. {
  122. using result_type = ResultView;
  123. color_converted_view_fn(CC cc = CC()): _cc(cc) {}
  124. template <typename View>
  125. auto operator()(View const& src) const -> result_type
  126. {
  127. return result_type{color_converted_view<DstP>(src, _cc)};
  128. }
  129. private:
  130. CC _cc;
  131. };
  132. } // namespace detail
  133. /// \ingroup ImageViewTransformationsFlipUD
  134. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  135. template <typename ...Views>
  136. inline
  137. auto flipped_up_down_view(any_image_view<Views...> const& src)
  138. -> typename dynamic_y_step_type<any_image_view<Views...>>::type
  139. {
  140. using result_view_t = typename dynamic_y_step_type<any_image_view<Views...>>::type;
  141. return variant2::visit(detail::flipped_up_down_view_fn<result_view_t>(), src);
  142. }
  143. /// \ingroup ImageViewTransformationsFlipLR
  144. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  145. template <typename ...Views>
  146. inline
  147. auto flipped_left_right_view(any_image_view<Views...> const& src)
  148. -> typename dynamic_x_step_type<any_image_view<Views...>>::type
  149. {
  150. using result_view_t = typename dynamic_x_step_type<any_image_view<Views...>>::type;
  151. return variant2::visit(detail::flipped_left_right_view_fn<result_view_t>(), src);
  152. }
  153. /// \ingroup ImageViewTransformationsTransposed
  154. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  155. template <typename ...Views>
  156. inline
  157. auto transposed_view(any_image_view<Views...> const& src)
  158. -> typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type
  159. {
  160. using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type;
  161. return variant2::visit(detail::tranposed_view_fn<result_view_t>(), src);
  162. }
  163. /// \ingroup ImageViewTransformations90CW
  164. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  165. template <typename ...Views>
  166. inline
  167. auto rotated90cw_view(any_image_view<Views...> const& src)
  168. -> typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type
  169. {
  170. using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type;
  171. return variant2::visit(detail::rotated90cw_view_fn<result_view_t>(), src);
  172. }
  173. /// \ingroup ImageViewTransformations90CCW
  174. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  175. template <typename ...Views>
  176. inline
  177. auto rotated90ccw_view(any_image_view<Views...> const& src)
  178. -> typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type
  179. {
  180. using result_view_t = typename dynamic_xy_step_transposed_type<any_image_view<Views...>>::type;
  181. return variant2::visit(detail::rotated90ccw_view_fn<result_view_t>(), src);
  182. }
  183. /// \ingroup ImageViewTransformations180
  184. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  185. template <typename ...Views>
  186. inline
  187. auto rotated180_view(any_image_view<Views...> const& src)
  188. -> typename dynamic_xy_step_type<any_image_view<Views...>>::type
  189. {
  190. using result_view_t = typename dynamic_xy_step_type<any_image_view<Views...>>::type;
  191. return variant2::visit(detail::rotated180_view_fn<result_view_t>(), src);
  192. }
  193. /// \ingroup ImageViewTransformationsSubimage
  194. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  195. template <typename ...Views>
  196. inline
  197. auto subimage_view(
  198. any_image_view<Views...> const& src,
  199. point_t const& topleft,
  200. point_t const& dimensions)
  201. -> any_image_view<Views...>
  202. {
  203. using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views...>>;
  204. return variant2::visit(subimage_view_fn(topleft, dimensions), src);
  205. }
  206. /// \ingroup ImageViewTransformationsSubimage
  207. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  208. template <typename ...Views>
  209. inline
  210. auto subimage_view(
  211. any_image_view<Views...> const& src,
  212. std::ptrdiff_t x_min, std::ptrdiff_t y_min, std::ptrdiff_t width, std::ptrdiff_t height)
  213. -> any_image_view<Views...>
  214. {
  215. using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views...>>;
  216. return variant2::visit(subimage_view_fn(point_t(x_min, y_min),point_t(width, height)), src);
  217. }
  218. /// \ingroup ImageViewTransformationsSubsampled
  219. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  220. template <typename ...Views>
  221. inline
  222. auto subsampled_view(any_image_view<Views...> const& src, point_t const& step)
  223. -> typename dynamic_xy_step_type<any_image_view<Views...>>::type
  224. {
  225. using step_type = typename dynamic_xy_step_type<any_image_view<Views...>>::type;
  226. using subsampled_view = detail::subsampled_view_fn<step_type>;
  227. return variant2::visit(subsampled_view(step), src);
  228. }
  229. /// \ingroup ImageViewTransformationsSubsampled
  230. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  231. template <typename ...Views>
  232. inline
  233. auto subsampled_view(any_image_view<Views...> const& src, std::ptrdiff_t x_step, std::ptrdiff_t y_step)
  234. -> typename dynamic_xy_step_type<any_image_view<Views...>>::type
  235. {
  236. using step_type = typename dynamic_xy_step_type<any_image_view<Views...>>::type;
  237. using subsampled_view_fn = detail::subsampled_view_fn<step_type>;
  238. return variant2::visit(subsampled_view_fn(point_t(x_step, y_step)), src);
  239. }
  240. namespace detail {
  241. template <typename View>
  242. struct get_nthchannel_type { using type = typename nth_channel_view_type<View>::type; };
  243. template <typename Views>
  244. struct views_get_nthchannel_type : mp11::mp_transform<get_nthchannel_type, Views> {};
  245. } // namespace detail
  246. /// \ingroup ImageViewTransformationsNthChannel
  247. /// \brief Given a runtime source image view, returns the type of a runtime image view over a single channel of the source view
  248. template <typename ...Views>
  249. struct nth_channel_view_type<any_image_view<Views...>>
  250. {
  251. using type = typename detail::views_get_nthchannel_type<any_image_view<Views...>>;
  252. };
  253. /// \ingroup ImageViewTransformationsNthChannel
  254. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  255. template <typename ...Views>
  256. inline
  257. auto nth_channel_view(any_image_view<Views...> const& src, int n)
  258. -> typename nth_channel_view_type<any_image_view<Views...>>::type
  259. {
  260. using result_view_t = typename nth_channel_view_type<any_image_view<Views...>>::type;
  261. return variant2::visit(detail::nth_channel_view_fn<result_view_t>(n), src);
  262. }
  263. namespace detail {
  264. template <typename Views, typename DstP, typename CC>
  265. struct views_get_ccv_type
  266. {
  267. private:
  268. template <typename T>
  269. using ccvt = typename color_converted_view_type<T, DstP, CC>::type;
  270. public:
  271. using type = mp11::mp_transform<ccvt, Views>;
  272. };
  273. } // namespace detail
  274. /// \ingroup ImageViewTransformationsColorConvert
  275. /// \brief Returns the type of a runtime-specified view, color-converted to a given pixel type with user specified color converter
  276. template <typename ...Views, typename DstP, typename CC>
  277. struct color_converted_view_type<any_image_view<Views...>,DstP,CC>
  278. {
  279. //using type = any_image_view<typename detail::views_get_ccv_type<Views, DstP, CC>::type>;
  280. using type = typename detail::views_get_ccv_type<any_image_view<Views...>, DstP, CC>::type;
  281. };
  282. /// \ingroup ImageViewTransformationsColorConvert
  283. /// \brief overload of generic color_converted_view with user defined color-converter
  284. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  285. template <typename DstP, typename ...Views, typename CC>
  286. inline
  287. auto color_converted_view(any_image_view<Views...> const& src, CC cc)
  288. -> typename color_converted_view_type<any_image_view<Views...>, DstP, CC>::type
  289. {
  290. using cc_view_t = typename color_converted_view_type<any_image_view<Views...>, DstP, CC>::type;
  291. return variant2::visit(detail::color_converted_view_fn<DstP, cc_view_t, CC>(cc), src);
  292. }
  293. /// \ingroup ImageViewTransformationsColorConvert
  294. /// \brief Returns the type of a runtime-specified view, color-converted to a given pixel type with the default coor converter
  295. template <typename ...Views, typename DstP>
  296. struct color_converted_view_type<any_image_view<Views...>,DstP>
  297. {
  298. using type = typename detail::views_get_ccv_type<any_image_view<Views...>, DstP, default_color_converter>::type;
  299. };
  300. /// \ingroup ImageViewTransformationsColorConvert
  301. /// \brief overload of generic color_converted_view with the default color-converter
  302. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  303. template <typename DstP, typename ...Views>
  304. inline
  305. auto color_converted_view(any_image_view<Views...> const& src)
  306. -> typename color_converted_view_type<any_image_view<Views...>, DstP>::type
  307. {
  308. using cc_view_t = typename color_converted_view_type<any_image_view<Views...>, DstP>::type;
  309. return variant2::visit(detail::color_converted_view_fn<DstP, cc_view_t>(), src);
  310. }
  311. /// \ingroup ImageViewTransformationsColorConvert
  312. /// \brief overload of generic color_converted_view with user defined color-converter
  313. /// These are workarounds for GCC 3.4, which thinks color_converted_view is ambiguous with the same method for templated views (in gil/image_view_factory.hpp)
  314. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  315. template <typename DstP, typename ...Views, typename CC>
  316. [[deprecated("Use color_converted_view(const any_image_view<Views...>& src, CC) instead.")]]
  317. inline
  318. auto any_color_converted_view(const any_image_view<Views...>& src, CC cc)
  319. -> typename color_converted_view_type<any_image_view<Views...>, DstP, CC>::type
  320. {
  321. return color_converted_view(src, cc);
  322. }
  323. /// \ingroup ImageViewTransformationsColorConvert
  324. /// \brief overload of generic color_converted_view with the default color-converter
  325. /// These are workarounds for GCC 3.4, which thinks color_converted_view is ambiguous with the same method for templated views (in gil/image_view_factory.hpp)
  326. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  327. template <typename DstP, typename ...Views>
  328. [[deprecated("Use color_converted_view(any_image_view<Views...> const& src) instead.")]]
  329. inline
  330. auto any_color_converted_view(const any_image_view<Views...>& src)
  331. -> typename color_converted_view_type<any_image_view<Views...>, DstP>::type
  332. {
  333. return color_converted_view(src);
  334. }
  335. /// \}
  336. }} // namespace boost::gil
  337. #endif