image_view_factory.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 apply_operation(src, detail::flipped_up_down_view_fn<result_view_t>());
  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 apply_operation(src, detail::flipped_left_right_view_fn<result_view_t>());
  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(const any_image_view<Views>& 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 apply_operation(src, detail::tranposed_view_fn<result_view_t>());
  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(const any_image_view<Views>& 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 apply_operation(src,detail::rotated90cw_view_fn<result_view_t>());
  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(const any_image_view<Views>& src)
  178. -> typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type
  179. {
  180. return apply_operation(src,detail::rotated90ccw_view_fn<typename dynamic_xy_step_transposed_type<any_image_view<Views>>::type>());
  181. }
  182. /// \ingroup ImageViewTransformations180
  183. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  184. template <typename Views>
  185. inline
  186. auto rotated180_view(any_image_view<Views> const& src)
  187. -> typename dynamic_xy_step_type<any_image_view<Views>>::type
  188. {
  189. using step_type = typename dynamic_xy_step_type<any_image_view<Views>>::type;
  190. return apply_operation(src, detail::rotated180_view_fn<step_type>());
  191. }
  192. /// \ingroup ImageViewTransformationsSubimage
  193. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  194. template <typename Views>
  195. inline
  196. auto subimage_view(
  197. any_image_view<Views> const& src,
  198. point_t const& topleft,
  199. point_t const& dimensions)
  200. -> any_image_view<Views>
  201. {
  202. using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views>>;
  203. return apply_operation(src, subimage_view_fn(topleft, dimensions));
  204. }
  205. /// \ingroup ImageViewTransformationsSubimage
  206. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  207. template <typename Views>
  208. inline
  209. auto subimage_view(
  210. any_image_view<Views> const& src,
  211. std::ptrdiff_t x_min, std::ptrdiff_t y_min, std::ptrdiff_t width, std::ptrdiff_t height)
  212. -> any_image_view<Views>
  213. {
  214. using subimage_view_fn = detail::subimage_view_fn<any_image_view<Views>>;
  215. return apply_operation(src, subimage_view_fn(point_t(x_min, y_min),point_t(width, height)));
  216. }
  217. /// \ingroup ImageViewTransformationsSubsampled
  218. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  219. template <typename Views>
  220. inline
  221. auto subsampled_view(any_image_view<Views> const& src, point_t const& step)
  222. -> typename dynamic_xy_step_type<any_image_view<Views>>::type
  223. {
  224. using step_type = typename dynamic_xy_step_type<any_image_view<Views>>::type;
  225. using subsampled_view = detail::subsampled_view_fn<step_type>;
  226. return apply_operation(src, subsampled_view(step));
  227. }
  228. /// \ingroup ImageViewTransformationsSubsampled
  229. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  230. template <typename Views>
  231. inline
  232. auto subsampled_view(any_image_view<Views> const& src, std::ptrdiff_t x_step, std::ptrdiff_t y_step)
  233. -> typename dynamic_xy_step_type<any_image_view<Views>>::type
  234. {
  235. using step_type = typename dynamic_xy_step_type<any_image_view<Views>>::type;
  236. using subsampled_view_fn = detail::subsampled_view_fn<step_type>;
  237. return apply_operation(src, subsampled_view_fn(point_t(x_step, y_step)));
  238. }
  239. namespace detail {
  240. template <typename View>
  241. struct get_nthchannel_type { using type = typename nth_channel_view_type<View>::type; };
  242. template <typename Views>
  243. struct views_get_nthchannel_type : mp11::mp_transform<get_nthchannel_type, Views> {};
  244. } // namespace detail
  245. /// \ingroup ImageViewTransformationsNthChannel
  246. /// \brief Given a runtime source image view, returns the type of a runtime image view over a single channel of the source view
  247. template <typename Views>
  248. struct nth_channel_view_type<any_image_view<Views>>
  249. {
  250. using type = any_image_view<typename detail::views_get_nthchannel_type<Views>::type>;
  251. };
  252. /// \ingroup ImageViewTransformationsNthChannel
  253. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  254. template <typename Views>
  255. inline
  256. auto nth_channel_view(const any_image_view<Views>& src, int n)
  257. -> typename nth_channel_view_type<any_image_view<Views>>::type
  258. {
  259. using result_view_t = typename nth_channel_view_type<any_image_view<Views>>::type;
  260. return apply_operation(src,detail::nth_channel_view_fn<result_view_t>(n));
  261. }
  262. namespace detail {
  263. template <typename View, typename DstP, typename CC>
  264. struct get_ccv_type : color_converted_view_type<View, DstP, CC> {};
  265. template <typename Views, typename DstP, typename CC>
  266. struct views_get_ccv_type
  267. {
  268. private:
  269. // FIXME: Remove class name injection with detail:: qualification
  270. // Required as workaround for MP11 issue that treats unqualified metafunction
  271. // in the class definition of the same name as the specialization (Peter Dimov):
  272. // invalid template argument for template parameter 'F', expected a class template
  273. template <typename T>
  274. using ccvt = detail::get_ccv_type<T, DstP, CC>;
  275. public:
  276. using type = mp11::mp_transform<ccvt, Views>;
  277. };
  278. } // namespace detail
  279. /// \ingroup ImageViewTransformationsColorConvert
  280. /// \brief Returns the type of a runtime-specified view, color-converted to a given pixel type with user specified color converter
  281. template <typename Views, typename DstP, typename CC>
  282. struct color_converted_view_type<any_image_view<Views>,DstP,CC>
  283. {
  284. using type = any_image_view<typename detail::views_get_ccv_type<Views, DstP, CC>::type>;
  285. };
  286. /// \ingroup ImageViewTransformationsColorConvert
  287. /// \brief overload of generic color_converted_view with user defined color-converter
  288. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  289. template <typename DstP, typename Views, typename CC>
  290. inline
  291. auto color_converted_view(const any_image_view<Views>& src, CC)
  292. -> typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type
  293. {
  294. using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type;
  295. return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
  296. }
  297. /// \ingroup ImageViewTransformationsColorConvert
  298. /// \brief Returns the type of a runtime-specified view, color-converted to a given pixel type with the default coor converter
  299. template <typename Views, typename DstP>
  300. struct color_converted_view_type<any_image_view<Views>,DstP>
  301. {
  302. using type = any_image_view<typename detail::views_get_ccv_type<Views, DstP, default_color_converter>::type>;
  303. };
  304. /// \ingroup ImageViewTransformationsColorConvert
  305. /// \brief overload of generic color_converted_view with the default color-converter
  306. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  307. template <typename DstP, typename Views>
  308. inline
  309. auto color_converted_view(any_image_view<Views> const& src)
  310. -> typename color_converted_view_type<any_image_view<Views>, DstP>::type
  311. {
  312. using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP>::type;
  313. return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
  314. }
  315. /// \ingroup ImageViewTransformationsColorConvert
  316. /// \brief overload of generic color_converted_view with user defined color-converter
  317. /// 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)
  318. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  319. template <typename DstP, typename Views, typename CC>
  320. inline
  321. auto any_color_converted_view(const any_image_view<Views>& src, CC)
  322. -> typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type
  323. {
  324. using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP, CC>::type;
  325. return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
  326. }
  327. /// \ingroup ImageViewTransformationsColorConvert
  328. /// \brief overload of generic color_converted_view with the default color-converter
  329. /// 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)
  330. /// \tparam Views Models Boost.MP11-compatible list of models of ImageViewConcept
  331. template <typename DstP, typename Views>
  332. inline
  333. auto any_color_converted_view(const any_image_view<Views>& src)
  334. -> typename color_converted_view_type<any_image_view<Views>, DstP>::type
  335. {
  336. using cc_view_t = typename color_converted_view_type<any_image_view<Views>, DstP>::type;
  337. return apply_operation(src, detail::color_converted_view_fn<DstP, cc_view_t>());
  338. }
  339. /// \}
  340. }} // namespace boost::gil
  341. #endif