image_view_factory.hpp 24 KB

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