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