image_view_factory.hpp 25 KB

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