image_view_factory.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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_IMAGE_VIEW_FACTORY_HPP
  9. #define BOOST_GIL_IMAGE_VIEW_FACTORY_HPP
  10. #include <boost/gil/color_convert.hpp>
  11. #include <boost/gil/dynamic_step.hpp>
  12. #include <boost/gil/gray.hpp>
  13. #include <boost/gil/metafunctions.hpp>
  14. #include <boost/gil/point.hpp>
  15. #include <boost/assert.hpp>
  16. #include <cstddef>
  17. #include <type_traits>
  18. /// Methods for creating shallow image views from raw pixel data or from other image views -
  19. /// flipping horizontally or vertically, axis-aligned rotation, a subimage, subsampled
  20. /// or n-th channel image view. Derived image views are shallow copies and are fast to construct.
  21. /// \defgroup ImageViewConstructors Image View From Raw Data
  22. /// \ingroup ImageViewAlgorithm
  23. /// \brief Methods for constructing image views from raw data and for getting raw data from views
  24. /// \defgroup ImageViewTransformations Image View Transformations
  25. /// \ingroup ImageViewAlgorithm
  26. /// \brief Methods for constructing one image view from another
  27. namespace boost { namespace gil {
  28. struct default_color_converter;
  29. template <typename T> struct transposed_type;
  30. /// \brief Returns the type of a view that has a dynamic step along both X and Y
  31. /// \ingroup ImageViewTransformations
  32. template <typename View>
  33. struct dynamic_xy_step_type : public dynamic_y_step_type<typename dynamic_x_step_type<View>::type> {};
  34. /// \brief Returns the type of a transposed view that has a dynamic step along both X and Y
  35. /// \ingroup ImageViewTransformations
  36. template <typename View>
  37. struct dynamic_xy_step_transposed_type : public dynamic_xy_step_type<typename transposed_type<View>::type> {};
  38. /// \ingroup ImageViewConstructors
  39. /// \brief Constructing image views from raw interleaved pixel data
  40. template <typename Iterator>
  41. typename type_from_x_iterator<Iterator>::view_t
  42. interleaved_view(std::size_t width, std::size_t height,
  43. Iterator pixels, std::ptrdiff_t rowsize_in_bytes) {
  44. using RView = typename type_from_x_iterator<Iterator>::view_t;
  45. return RView(width, height, typename RView::locator(pixels, rowsize_in_bytes));
  46. }
  47. /// \ingroup ImageViewConstructors
  48. /// \brief Constructing image views from raw interleaved pixel data
  49. template <typename Iterator>
  50. auto interleaved_view(point<std::size_t> dim, Iterator pixels,
  51. std::ptrdiff_t rowsize_in_bytes)
  52. -> typename type_from_x_iterator<Iterator>::view_t
  53. {
  54. using RView = typename type_from_x_iterator<Iterator>::view_t;
  55. return RView(dim, typename RView::locator(pixels, rowsize_in_bytes));
  56. }
  57. /////////////////////////////
  58. // interleaved_view_get_raw_data, planar_view_get_raw_data - return pointers to the raw data (the channels) of a basic homogeneous view.
  59. /////////////////////////////
  60. namespace detail {
  61. template <typename View, bool IsMutable> struct channel_pointer_type_impl;
  62. template <typename View> struct channel_pointer_type_impl<View, true> {
  63. using type = typename channel_type<View>::type *;
  64. };
  65. template <typename View> struct channel_pointer_type_impl<View, false> {
  66. using type = const typename channel_type<View>::type *;
  67. };
  68. template <typename View> struct channel_pointer_type
  69. : public channel_pointer_type_impl<View, view_is_mutable<View>::value> {};
  70. } // namespace detail
  71. /// \ingroup ImageViewConstructors
  72. /// \brief Returns C pointer to the the channels of an interleaved homogeneous view.
  73. template <typename HomogeneousView>
  74. typename detail::channel_pointer_type<HomogeneousView>::type interleaved_view_get_raw_data(const HomogeneousView& view) {
  75. static_assert(!is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value, "");
  76. static_assert(std::is_pointer<typename HomogeneousView::x_iterator>::value, "");
  77. return &gil::at_c<0>(view(0,0));
  78. }
  79. /// \ingroup ImageViewConstructors
  80. /// \brief Returns C pointer to the the channels of a given color plane of a planar homogeneous view.
  81. template <typename HomogeneousView>
  82. typename detail::channel_pointer_type<HomogeneousView>::type planar_view_get_raw_data(const HomogeneousView& view, int plane_index) {
  83. static_assert(is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value, "");
  84. return dynamic_at_c(view.row_begin(0),plane_index);
  85. }
  86. /// \defgroup ImageViewTransformationsColorConvert color_converted_view
  87. /// \ingroup ImageViewTransformations
  88. /// \brief Color converted view of another view
  89. /// \ingroup ImageViewTransformationsColorConvert PixelDereferenceAdaptorModel
  90. /// \brief Function object that given a source pixel, returns it converted to a given color space and channel depth. Models: PixelDereferenceAdaptorConcept
  91. ///
  92. /// Useful in constructing a color converted view over a given image view
  93. template <typename SrcConstRefP, typename DstP, typename CC=default_color_converter > // const_reference to the source pixel and destination pixel value
  94. class color_convert_deref_fn : public deref_base<color_convert_deref_fn<SrcConstRefP,DstP,CC>, DstP, DstP, const DstP&, SrcConstRefP, DstP, false> {
  95. private:
  96. CC _cc; // color-converter
  97. public:
  98. color_convert_deref_fn() {}
  99. color_convert_deref_fn(CC cc_in) : _cc(cc_in) {}
  100. DstP operator()(SrcConstRefP srcP) const {
  101. DstP dstP;
  102. _cc(srcP,dstP);
  103. return dstP;
  104. }
  105. };
  106. namespace detail {
  107. // Add color converter upon dereferencing
  108. template <typename SrcView, typename CC, typename DstP, typename SrcP>
  109. struct _color_converted_view_type {
  110. private:
  111. using deref_t = color_convert_deref_fn<typename SrcView::const_t::reference,DstP,CC>;
  112. using add_ref_t = typename SrcView::template add_deref<deref_t>;
  113. public:
  114. using type = typename add_ref_t::type;
  115. static type make(const SrcView& sv,CC cc) {return add_ref_t::make(sv,deref_t(cc));}
  116. };
  117. // If the Src view has the same pixel type as the target, there is no need for color conversion
  118. template <typename SrcView, typename CC, typename DstP>
  119. struct _color_converted_view_type<SrcView,CC,DstP,DstP> {
  120. using type = SrcView;
  121. static type make(const SrcView& sv,CC) {return sv;}
  122. };
  123. } // namespace detail
  124. /// \brief Returns the type of a view that does color conversion upon dereferencing its pixels
  125. /// \ingroup ImageViewTransformationsColorConvert
  126. template <typename SrcView, typename DstP, typename CC=default_color_converter>
  127. struct color_converted_view_type : public detail::_color_converted_view_type<SrcView,
  128. CC,
  129. DstP,
  130. typename SrcView::value_type> {
  131. GIL_CLASS_REQUIRE(DstP, boost::gil, MutablePixelConcept)//why does it have to be mutable???
  132. };
  133. /// \ingroup ImageViewTransformationsColorConvert
  134. /// \brief view of a different color space with a user defined color-converter
  135. template <typename DstP, typename View, typename CC>
  136. inline typename color_converted_view_type<View,DstP,CC>::type color_converted_view(const View& src,CC cc) {
  137. return color_converted_view_type<View,DstP,CC>::make(src,cc);
  138. }
  139. /// \ingroup ImageViewTransformationsColorConvert
  140. /// \brief overload of generic color_converted_view with the default color-converter
  141. template <typename DstP, typename View>
  142. inline typename color_converted_view_type<View,DstP>::type
  143. color_converted_view(const View& src) {
  144. return color_converted_view<DstP>(src,default_color_converter());
  145. }
  146. /// \defgroup ImageViewTransformationsFlipUD flipped_up_down_view
  147. /// \ingroup ImageViewTransformations
  148. /// \brief view of a view flipped up-to-down
  149. /// \ingroup ImageViewTransformationsFlipUD
  150. template <typename View>
  151. inline typename dynamic_y_step_type<View>::type flipped_up_down_view(const View& src) {
  152. using RView = typename dynamic_y_step_type<View>::type;
  153. return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1));
  154. }
  155. /// \defgroup ImageViewTransformationsFlipLR flipped_left_right_view
  156. /// \ingroup ImageViewTransformations
  157. /// \brief view of a view flipped left-to-right
  158. /// \ingroup ImageViewTransformationsFlipLR
  159. template <typename View>
  160. inline typename dynamic_x_step_type<View>::type flipped_left_right_view(const View& src) {
  161. using RView = typename dynamic_x_step_type<View>::type;
  162. return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,0),-1,1));
  163. }
  164. /// \defgroup ImageViewTransformationsTransposed transposed_view
  165. /// \ingroup ImageViewTransformations
  166. /// \brief view of a view transposed
  167. /// \ingroup ImageViewTransformationsTransposed
  168. template <typename View>
  169. inline typename dynamic_xy_step_transposed_type<View>::type transposed_view(const View& src) {
  170. using RView = typename dynamic_xy_step_transposed_type<View>::type;
  171. return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,0),1,1,true));
  172. }
  173. /// \defgroup ImageViewTransformations90CW rotated90cw_view
  174. /// \ingroup ImageViewTransformations
  175. /// \brief view of a view rotated 90 degrees clockwise
  176. /// \ingroup ImageViewTransformations90CW
  177. template <typename View>
  178. inline typename dynamic_xy_step_transposed_type<View>::type rotated90cw_view(const View& src) {
  179. using RView = typename dynamic_xy_step_transposed_type<View>::type;
  180. return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1,1,true));
  181. }
  182. /// \defgroup ImageViewTransformations90CCW rotated90ccw_view
  183. /// \ingroup ImageViewTransformations
  184. /// \brief view of a view rotated 90 degrees counter-clockwise
  185. /// \ingroup ImageViewTransformations90CCW
  186. template <typename View>
  187. inline typename dynamic_xy_step_transposed_type<View>::type rotated90ccw_view(const View& src) {
  188. using RView = typename dynamic_xy_step_transposed_type<View>::type;
  189. return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(src.width()-1,0),1,-1,true));
  190. }
  191. /// \defgroup ImageViewTransformations180 rotated180_view
  192. /// \ingroup ImageViewTransformations
  193. /// \brief view of a view rotated 180 degrees
  194. /// \ingroup ImageViewTransformations180
  195. template <typename View>
  196. inline typename dynamic_xy_step_type<View>::type rotated180_view(const View& src) {
  197. using RView = typename dynamic_xy_step_type<View>::type;
  198. return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,src.height()-1),-1,-1));
  199. }
  200. /// \defgroup ImageViewTransformationsSubimage subimage_view
  201. /// \ingroup ImageViewTransformations
  202. /// \brief view of an axis-aligned rectangular area within an image_view
  203. /// \ingroup ImageViewTransformationsSubimage
  204. template <typename View>
  205. inline View subimage_view(const View& src, const typename View::point_t& topleft, const typename View::point_t& dimensions) {
  206. return View(dimensions,src.xy_at(topleft));
  207. }
  208. /// \ingroup ImageViewTransformationsSubimage
  209. template <typename View>
  210. inline View subimage_view(const View& src, int xMin, int yMin, int width, int height) {
  211. return View(width,height,src.xy_at(xMin,yMin));
  212. }
  213. /// \defgroup ImageViewTransformationsSubsampled subsampled_view
  214. /// \ingroup ImageViewTransformations
  215. /// \brief view of a subsampled version of an image_view, stepping over a number of channels in X and number of rows in Y
  216. /// \ingroup ImageViewTransformationsSubsampled
  217. template <typename View>
  218. inline typename dynamic_xy_step_type<View>::type subsampled_view(const View& src, typename View::coord_t xStep, typename View::coord_t yStep)
  219. {
  220. BOOST_ASSERT(xStep > 0 && yStep > 0);
  221. using RView = typename dynamic_xy_step_type<View>::type;
  222. return RView((src.width()+(xStep-1))/xStep,(src.height()+(yStep-1))/yStep,
  223. typename RView::xy_locator(src.xy_at(0,0),xStep,yStep));
  224. }
  225. /// \ingroup ImageViewTransformationsSubsampled
  226. template <typename View>
  227. inline typename dynamic_xy_step_type<View>::type subsampled_view(const View& src, const typename View::point_t& step) {
  228. return subsampled_view(src,step.x,step.y);
  229. }
  230. /// \defgroup ImageViewTransformationsNthChannel nth_channel_view
  231. /// \ingroup ImageViewTransformations
  232. /// \brief single-channel (grayscale) view of the N-th channel of a given image_view
  233. namespace detail {
  234. template <typename View, bool AreChannelsTogether> struct __nth_channel_view_basic;
  235. // nth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
  236. // or images with a step
  237. template <typename View>
  238. struct __nth_channel_view_basic<View,false> {
  239. using type = typename view_type<typename channel_type<View>::type, gray_layout_t, false, true, view_is_mutable<View>::value>::type;
  240. static type make(const View& src, int n) {
  241. using locator_t = typename type::xy_locator;
  242. using x_iterator_t = typename type::x_iterator;
  243. using x_iterator_base_t = typename iterator_adaptor_get_base<x_iterator_t>::type;
  244. x_iterator_t sit(x_iterator_base_t(&(src(0,0)[n])),src.pixels().pixel_size());
  245. return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
  246. }
  247. };
  248. // nth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
  249. template <typename View>
  250. struct __nth_channel_view_basic<View,true> {
  251. using type = typename view_type<typename channel_type<View>::type, gray_layout_t, false, false, view_is_mutable<View>::value>::type;
  252. static type make(const View& src, int n) {
  253. using x_iterator_t = typename type::x_iterator;
  254. return interleaved_view(src.width(),src.height(),(x_iterator_t)&(src(0,0)[n]), src.pixels().row_size());
  255. }
  256. };
  257. template <typename View, bool IsBasic> struct __nth_channel_view;
  258. // For basic (memory-based) views dispatch to __nth_channel_view_basic
  259. template <typename View>
  260. struct __nth_channel_view<View,true>
  261. {
  262. private:
  263. using src_x_iterator = typename View::x_iterator;
  264. // Determines whether the channels of a given pixel iterator are adjacent in memory.
  265. // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
  266. static constexpr bool adjacent =
  267. !iterator_is_step<src_x_iterator>::value &&
  268. (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
  269. public:
  270. using type = typename __nth_channel_view_basic<View,adjacent>::type;
  271. static type make(const View& src, int n) {
  272. return __nth_channel_view_basic<View,adjacent>::make(src,n);
  273. }
  274. };
  275. /// \brief Function object that returns a grayscale reference of the N-th channel of a given reference. Models: PixelDereferenceAdaptorConcept.
  276. /// \ingroup PixelDereferenceAdaptorModel
  277. ///
  278. /// If the input is a pixel value or constant reference, the function object is immutable. Otherwise it is mutable (and returns non-const reference to the n-th channel)
  279. template <typename SrcP> // SrcP is a reference to PixelConcept (could be pixel value or const/non-const reference)
  280. // Examples: pixel<T,L>, pixel<T,L>&, const pixel<T,L>&, planar_pixel_reference<T&,L>, planar_pixel_reference<const T&,L>
  281. struct nth_channel_deref_fn
  282. {
  283. static constexpr bool is_mutable =
  284. pixel_is_reference<SrcP>::value && pixel_reference_is_mutable<SrcP>::value;
  285. private:
  286. using src_pixel_t = typename remove_reference<SrcP>::type;
  287. using channel_t = typename channel_type<src_pixel_t>::type;
  288. using const_ref_t = typename src_pixel_t::const_reference;
  289. using ref_t = typename pixel_reference_type<channel_t,gray_layout_t,false,is_mutable>::type;
  290. public:
  291. using const_t = nth_channel_deref_fn<const_ref_t>;
  292. using value_type = typename pixel_value_type<channel_t,gray_layout_t>::type;
  293. using const_reference = typename pixel_reference_type<channel_t,gray_layout_t,false,false>::type;
  294. using argument_type = SrcP;
  295. using reference = typename mpl::if_c<is_mutable, ref_t, value_type>::type;
  296. using result_type = reference;
  297. nth_channel_deref_fn(int n=0) : _n(n) {}
  298. template <typename P> nth_channel_deref_fn(const nth_channel_deref_fn<P>& d) : _n(d._n) {}
  299. int _n; // the channel to use
  300. result_type operator()(argument_type srcP) const {
  301. return result_type(srcP[_n]);
  302. }
  303. };
  304. template <typename View> struct __nth_channel_view<View,false> {
  305. private:
  306. using deref_t = nth_channel_deref_fn<typename View::reference>;
  307. using AD = typename View::template add_deref<deref_t>;
  308. public:
  309. using type = typename AD::type;
  310. static type make(const View& src, int n) {
  311. return AD::make(src, deref_t(n));
  312. }
  313. };
  314. } // namespace detail
  315. /// \brief Given a source image view type View, returns the type of an image view over a single channel of View
  316. /// \ingroup ImageViewTransformationsNthChannel
  317. ///
  318. /// If the channels in the source view are adjacent in memory (such as planar non-step view or single-channel view) then the
  319. /// return view is a single-channel non-step view.
  320. /// If the channels are non-adjacent (interleaved and/or step view) then the return view is a single-channel step view.
  321. template <typename View>
  322. struct nth_channel_view_type {
  323. private:
  324. GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
  325. using VB = detail::__nth_channel_view<View,view_is_basic<View>::value>;
  326. public:
  327. using type = typename VB::type;
  328. static type make(const View& src, int n) { return VB::make(src,n); }
  329. };
  330. /// \ingroup ImageViewTransformationsNthChannel
  331. template <typename View>
  332. typename nth_channel_view_type<View>::type nth_channel_view(const View& src, int n) {
  333. return nth_channel_view_type<View>::make(src,n);
  334. }
  335. /// \defgroup ImageViewTransformationsKthChannel kth_channel_view
  336. /// \ingroup ImageViewTransformations
  337. /// \brief single-channel (grayscale) view of the K-th channel of a given image_view. The channel index is a template parameter
  338. namespace detail {
  339. template <int K, typename View, bool AreChannelsTogether> struct __kth_channel_view_basic;
  340. // kth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
  341. // or images with a step
  342. template <int K, typename View>
  343. struct __kth_channel_view_basic<K,View,false> {
  344. private:
  345. using channel_t = typename kth_element_type<typename View::value_type,K>::type;
  346. public:
  347. using type = typename view_type<channel_t, gray_layout_t, false, true, view_is_mutable<View>::value>::type;
  348. static type make(const View& src) {
  349. using locator_t = typename type::xy_locator;
  350. using x_iterator_t = typename type::x_iterator;
  351. using x_iterator_base_t = typename iterator_adaptor_get_base<x_iterator_t>::type;
  352. x_iterator_t sit(x_iterator_base_t(&gil::at_c<K>(src(0,0))),src.pixels().pixel_size());
  353. return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
  354. }
  355. };
  356. // kth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
  357. template <int K, typename View>
  358. struct __kth_channel_view_basic<K,View,true> {
  359. private:
  360. using channel_t = typename kth_element_type<typename View::value_type, K>::type;
  361. public:
  362. using type = typename view_type<channel_t, gray_layout_t, false, false, view_is_mutable<View>::value>::type;
  363. static type make(const View& src) {
  364. using x_iterator_t = typename type::x_iterator;
  365. return interleaved_view(src.width(),src.height(),(x_iterator_t)&gil::at_c<K>(src(0,0)), src.pixels().row_size());
  366. }
  367. };
  368. template <int K, typename View, bool IsBasic> struct __kth_channel_view;
  369. // For basic (memory-based) views dispatch to __kth_channel_view_basic
  370. template <int K, typename View> struct __kth_channel_view<K,View,true>
  371. {
  372. private:
  373. using src_x_iterator = typename View::x_iterator;
  374. // Determines whether the channels of a given pixel iterator are adjacent in memory.
  375. // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
  376. static constexpr bool adjacent =
  377. !iterator_is_step<src_x_iterator>::value &&
  378. (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
  379. public:
  380. using type = typename __kth_channel_view_basic<K,View,adjacent>::type;
  381. static type make(const View& src) {
  382. return __kth_channel_view_basic<K,View,adjacent>::make(src);
  383. }
  384. };
  385. /// \brief Function object that returns a grayscale reference of the K-th channel (specified as a template parameter) of a given reference. Models: PixelDereferenceAdaptorConcept.
  386. /// \ingroup PixelDereferenceAdaptorModel
  387. ///
  388. /// If the input is a pixel value or constant reference, the function object is immutable. Otherwise it is mutable (and returns non-const reference to the k-th channel)
  389. /// \tparam SrcP reference to PixelConcept (could be pixel value or const/non-const reference)
  390. /// Examples: pixel<T,L>, pixel<T,L>&, const pixel<T,L>&, planar_pixel_reference<T&,L>, planar_pixel_reference<const T&,L>
  391. template <int K, typename SrcP>
  392. struct kth_channel_deref_fn
  393. {
  394. static constexpr bool is_mutable =
  395. pixel_is_reference<SrcP>::value && pixel_reference_is_mutable<SrcP>::value;
  396. private:
  397. using src_pixel_t = typename remove_reference<SrcP>::type;
  398. using channel_t = typename kth_element_type<src_pixel_t, K>::type;
  399. using const_ref_t = typename src_pixel_t::const_reference;
  400. using ref_t = typename pixel_reference_type<channel_t,gray_layout_t,false,is_mutable>::type;
  401. public:
  402. using const_t = kth_channel_deref_fn<K,const_ref_t>;
  403. using value_type = typename pixel_value_type<channel_t,gray_layout_t>::type;
  404. using const_reference = typename pixel_reference_type<channel_t,gray_layout_t,false,false>::type;
  405. using argument_type = SrcP;
  406. using reference = typename mpl::if_c<is_mutable, ref_t, value_type>::type;
  407. using result_type = reference;
  408. kth_channel_deref_fn() {}
  409. template <typename P> kth_channel_deref_fn(const kth_channel_deref_fn<K,P>&) {}
  410. result_type operator()(argument_type srcP) const {
  411. return result_type(gil::at_c<K>(srcP));
  412. }
  413. };
  414. template <int K, typename View> struct __kth_channel_view<K,View,false> {
  415. private:
  416. using deref_t = kth_channel_deref_fn<K,typename View::reference>;
  417. using AD = typename View::template add_deref<deref_t>;
  418. public:
  419. using type = typename AD::type;
  420. static type make(const View& src) {
  421. return AD::make(src, deref_t());
  422. }
  423. };
  424. } // namespace detail
  425. /// \brief Given a source image view type View, returns the type of an image view over a given channel of View.
  426. /// \ingroup ImageViewTransformationsKthChannel
  427. ///
  428. /// If the channels in the source view are adjacent in memory (such as planar non-step view or single-channel view) then the
  429. /// return view is a single-channel non-step view.
  430. /// If the channels are non-adjacent (interleaved and/or step view) then the return view is a single-channel step view.
  431. template <int K, typename View>
  432. struct kth_channel_view_type {
  433. private:
  434. GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
  435. using VB = detail::__kth_channel_view<K,View,view_is_basic<View>::value>;
  436. public:
  437. using type = typename VB::type;
  438. static type make(const View& src) { return VB::make(src); }
  439. };
  440. /// \ingroup ImageViewTransformationsKthChannel
  441. template <int K, typename View>
  442. typename kth_channel_view_type<K,View>::type kth_channel_view(const View& src) {
  443. return kth_channel_view_type<K,View>::make(src);
  444. }
  445. } } // namespace boost::gil
  446. #endif