Boost GIL


image_view_factory.hpp
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 
11 #include <boost/gil/color_convert.hpp>
12 #include <boost/gil/gray.hpp>
13 #include <boost/gil/metafunctions.hpp>
14 #include <boost/gil/point.hpp>
15 
16 #include <cassert>
17 #include <cstddef>
18 
22 
26 
30 
31 namespace boost { namespace gil {
32 struct default_color_converter;
33 
34 template <typename T> struct dynamic_x_step_type;
35 template <typename T> struct dynamic_y_step_type;
36 template <typename T> struct transposed_type;
37 
40 template <typename View>
41 struct dynamic_xy_step_type : public dynamic_y_step_type<typename dynamic_x_step_type<View>::type> {};
42 
45 template <typename View>
46 struct dynamic_xy_step_transposed_type : public dynamic_xy_step_type<typename transposed_type<View>::type> {};
47 
48 
51 template <typename Iterator>
53 interleaved_view(std::size_t width, std::size_t height,
54  Iterator pixels, std::ptrdiff_t rowsize_in_bytes) {
55  typedef typename type_from_x_iterator<Iterator>::view_t RView;
56  return RView(width, height, typename RView::locator(pixels, rowsize_in_bytes));
57 }
58 
61 template <typename Iterator>
62 auto interleaved_view(point<std::size_t> dim, Iterator pixels,
63  std::ptrdiff_t rowsize_in_bytes)
65 {
66  using RView = typename type_from_x_iterator<Iterator>::view_t;
67  return RView(dim, typename RView::locator(pixels, rowsize_in_bytes));
68 }
69 
71 // interleaved_view_get_raw_data, planar_view_get_raw_data - return pointers to the raw data (the channels) of a basic homogeneous view.
73 
74 namespace detail {
75  template <typename View, bool IsMutable> struct channel_pointer_type_impl;
76 
77  template <typename View> struct channel_pointer_type_impl<View, true> {
78  typedef typename channel_type<View>::type* type;
79  };
80  template <typename View> struct channel_pointer_type_impl<View, false> {
81  typedef const typename channel_type<View>::type* type;
82  };
83 
84  template <typename View> struct channel_pointer_type
85  : public channel_pointer_type_impl<View, view_is_mutable<View>::value> {};
86 } // namespace detail
87 
90 template <typename HomogeneousView>
91 typename detail::channel_pointer_type<HomogeneousView>::type interleaved_view_get_raw_data(const HomogeneousView& view) {
92  BOOST_STATIC_ASSERT((!is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value));
93  BOOST_STATIC_ASSERT((boost::is_pointer<typename HomogeneousView::x_iterator>::value));
94 
95  return &gil::at_c<0>(view(0,0));
96 }
97 
100 template <typename HomogeneousView>
101 typename detail::channel_pointer_type<HomogeneousView>::type planar_view_get_raw_data(const HomogeneousView& view, int plane_index) {
102  BOOST_STATIC_ASSERT((is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value));
103  return dynamic_at_c(view.row_begin(0),plane_index);
104 }
105 
106 
110 
115 template <typename SrcConstRefP, typename DstP, typename CC=default_color_converter > // const_reference to the source pixel and destination pixel value
116 class color_convert_deref_fn : public deref_base<color_convert_deref_fn<SrcConstRefP,DstP,CC>, DstP, DstP, const DstP&, SrcConstRefP, DstP, false> {
117 private:
118  CC _cc; // color-converter
119 public:
121  color_convert_deref_fn(CC cc_in) : _cc(cc_in) {}
122 
123  DstP operator()(SrcConstRefP srcP) const {
124  DstP dstP;
125  _cc(srcP,dstP);
126  return dstP;
127  }
128 };
129 
130 namespace detail {
131  // Add color converter upon dereferencing
132  template <typename SrcView, typename CC, typename DstP, typename SrcP>
133  struct _color_converted_view_type {
134  private:
136  typedef typename SrcView::template add_deref<deref_t> add_ref_t;
137  public:
138  typedef typename add_ref_t::type type;
139  static type make(const SrcView& sv,CC cc) {return add_ref_t::make(sv,deref_t(cc));}
140  };
141 
142  // If the Src view has the same pixel type as the target, there is no need for color conversion
143  template <typename SrcView, typename CC, typename DstP>
144  struct _color_converted_view_type<SrcView,CC,DstP,DstP> {
145  typedef SrcView type;
146  static type make(const SrcView& sv,CC) {return sv;}
147  };
148 } // namespace detail
149 
150 
153 template <typename SrcView, typename DstP, typename CC=default_color_converter>
154 struct color_converted_view_type : public detail::_color_converted_view_type<SrcView,
155  CC,
156  DstP,
157  typename SrcView::value_type> {
158  GIL_CLASS_REQUIRE(DstP, boost::gil, MutablePixelConcept)//why does it have to be mutable???
159 };
160 
161 
164 template <typename DstP, typename View, typename CC>
167 }
168 
171 template <typename DstP, typename View>
172 inline typename color_converted_view_type<View,DstP>::type
173 color_converted_view(const View& src) {
174  return color_converted_view<DstP>(src,default_color_converter());
175 }
176 
180 
182 template <typename View>
183 inline typename dynamic_y_step_type<View>::type flipped_up_down_view(const View& src) {
184  typedef typename dynamic_y_step_type<View>::type RView;
185  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1));
186 }
187 
191 
193 template <typename View>
194 inline typename dynamic_x_step_type<View>::type flipped_left_right_view(const View& src) {
195  typedef typename dynamic_x_step_type<View>::type RView;
196  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,0),-1,1));
197 }
198 
202 
204 template <typename View>
205 inline typename dynamic_xy_step_transposed_type<View>::type transposed_view(const View& src) {
206  typedef typename dynamic_xy_step_transposed_type<View>::type RView;
207  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,0),1,1,true));
208 }
209 
213 
215 template <typename View>
216 inline typename dynamic_xy_step_transposed_type<View>::type rotated90cw_view(const View& src) {
217  typedef typename dynamic_xy_step_transposed_type<View>::type RView;
218  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1,1,true));
219 }
220 
224 
226 template <typename View>
227 inline typename dynamic_xy_step_transposed_type<View>::type rotated90ccw_view(const View& src) {
228  typedef typename dynamic_xy_step_transposed_type<View>::type RView;
229  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(src.width()-1,0),1,-1,true));
230 }
231 
235 
237 template <typename View>
238 inline typename dynamic_xy_step_type<View>::type rotated180_view(const View& src) {
239  typedef typename dynamic_xy_step_type<View>::type RView;
240  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,src.height()-1),-1,-1));
241 }
242 
246 
248 template <typename View>
249 inline View subimage_view(const View& src, const typename View::point_t& topleft, const typename View::point_t& dimensions) {
250  return View(dimensions,src.xy_at(topleft));
251 }
252 
254 template <typename View>
255 inline View subimage_view(const View& src, int xMin, int yMin, int width, int height) {
256  return View(width,height,src.xy_at(xMin,yMin));
257 }
258 
262 
264 template <typename View>
265 inline typename dynamic_xy_step_type<View>::type subsampled_view(const View& src, typename View::coord_t xStep, typename View::coord_t yStep) {
266  assert(xStep>0 && yStep>0);
267  typedef typename dynamic_xy_step_type<View>::type RView;
268  return RView((src.width()+(xStep-1))/xStep,(src.height()+(yStep-1))/yStep,
269  typename RView::xy_locator(src.xy_at(0,0),xStep,yStep));
270 }
271 
273 template <typename View>
274 inline typename dynamic_xy_step_type<View>::type subsampled_view(const View& src, const typename View::point_t& step) {
275  return subsampled_view(src,step.x,step.y);
276 }
277 
281 
282 namespace detail {
283  template <typename View, bool AreChannelsTogether> struct __nth_channel_view_basic;
284 
285  // nth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
286  // or images with a step
287  template <typename View>
288  struct __nth_channel_view_basic<View,false> {
289  typedef typename view_type<typename channel_type<View>::type, gray_layout_t, false, true, view_is_mutable<View>::value>::type type;
290 
291  static type make(const View& src, int n) {
292  typedef typename type::xy_locator locator_t;
293  typedef typename type::x_iterator x_iterator_t;
294  typedef typename iterator_adaptor_get_base<x_iterator_t>::type x_iterator_base_t;
295  x_iterator_t sit(x_iterator_base_t(&(src(0,0)[n])),src.pixels().pixel_size());
296  return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
297  }
298  };
299 
300  // nth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
301  template <typename View>
302  struct __nth_channel_view_basic<View,true> {
303  typedef typename view_type<typename channel_type<View>::type, gray_layout_t, false, false, view_is_mutable<View>::value>::type type;
304  static type make(const View& src, int n) {
305  typedef typename type::x_iterator x_iterator_t;
306  return interleaved_view(src.width(),src.height(),(x_iterator_t)&(src(0,0)[n]), src.pixels().row_size());
307  }
308  };
309 
310  template <typename View, bool IsBasic> struct __nth_channel_view;
311 
312  // For basic (memory-based) views dispatch to __nth_channel_view_basic
313  template <typename View> struct __nth_channel_view<View,true> {
314  private:
315  typedef typename View::x_iterator src_x_iterator;
316 
317  // Determines whether the channels of a given pixel iterator are adjacent in memory.
318  // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
319  BOOST_STATIC_CONSTANT(bool, adjacent=
320  !iterator_is_step<src_x_iterator>::value &&
321  (is_planar<src_x_iterator>::value ||
322  num_channels<View>::value==1));
323  public:
324  typedef typename __nth_channel_view_basic<View,adjacent>::type type;
325 
326  static type make(const View& src, int n) {
327  return __nth_channel_view_basic<View,adjacent>::make(src,n);
328  }
329  };
330 
335  template <typename SrcP> // SrcP is a reference to PixelConcept (could be pixel value or const/non-const reference)
336  // Examples: pixel<T,L>, pixel<T,L>&, const pixel<T,L>&, planar_pixel_reference<T&,L>, planar_pixel_reference<const T&,L>
338  BOOST_STATIC_CONSTANT(bool, is_mutable=pixel_is_reference<SrcP>::value && pixel_reference_is_mutable<SrcP>::value);
339  private:
340  typedef typename remove_reference<SrcP>::type src_pixel_t;
341  typedef typename channel_type<src_pixel_t>::type channel_t;
342  typedef typename src_pixel_t::const_reference const_ref_t;
344  public:
348  typedef SrcP argument_type;
349  typedef typename mpl::if_c<is_mutable, ref_t, value_type>::type reference;
350  typedef reference result_type;
351 
352  nth_channel_deref_fn(int n=0) : _n(n) {}
353  template <typename P> nth_channel_deref_fn(const nth_channel_deref_fn<P>& d) : _n(d._n) {}
354 
355  int _n; // the channel to use
356 
357  result_type operator()(argument_type srcP) const {
358  return result_type(srcP[_n]);
359  }
360  };
361 
362  template <typename View> struct __nth_channel_view<View,false> {
363  private:
365  typedef typename View::template add_deref<deref_t> AD;
366  public:
367  typedef typename AD::type type;
368  static type make(const View& src, int n) {
369  return AD::make(src, deref_t(n));
370  }
371  };
372 } // namespace detail
373 
380 template <typename View>
382 private:
383  GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
384  typedef detail::__nth_channel_view<View,view_is_basic<View>::value> VB;
385 public:
386  typedef typename VB::type type;
387  static type make(const View& src, int n) { return VB::make(src,n); }
388 };
389 
390 
392 template <typename View>
393 typename nth_channel_view_type<View>::type nth_channel_view(const View& src, int n) {
394  return nth_channel_view_type<View>::make(src,n);
395 }
396 
397 
398 
399 
400 
401 
402 
406 
407 namespace detail {
408  template <int K, typename View, bool AreChannelsTogether> struct __kth_channel_view_basic;
409 
410  // kth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
411  // or images with a step
412  template <int K, typename View>
413  struct __kth_channel_view_basic<K,View,false> {
414  private:
415  typedef typename kth_element_type<typename View::value_type,K>::type channel_t;
416  public:
417  typedef typename view_type<channel_t, gray_layout_t, false, true, view_is_mutable<View>::value>::type type;
418 
419  static type make(const View& src) {
420  typedef typename type::xy_locator locator_t;
421  typedef typename type::x_iterator x_iterator_t;
422  typedef typename iterator_adaptor_get_base<x_iterator_t>::type x_iterator_base_t;
423  x_iterator_t sit(x_iterator_base_t(&gil::at_c<K>(src(0,0))),src.pixels().pixel_size());
424  return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
425  }
426  };
427 
428  // kth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
429  template <int K, typename View>
430  struct __kth_channel_view_basic<K,View,true> {
431  private:
432  typedef typename kth_element_type<typename View::value_type, K>::type channel_t;
433  public:
434  typedef typename view_type<channel_t, gray_layout_t, false, false, view_is_mutable<View>::value>::type type;
435  static type make(const View& src) {
436  typedef typename type::x_iterator x_iterator_t;
437  return interleaved_view(src.width(),src.height(),(x_iterator_t)&gil::at_c<K>(src(0,0)), src.pixels().row_size());
438  }
439  };
440 
441  template <int K, typename View, bool IsBasic> struct __kth_channel_view;
442 
443  // For basic (memory-based) views dispatch to __kth_channel_view_basic
444  template <int K, typename View> struct __kth_channel_view<K,View,true> {
445  private:
446  typedef typename View::x_iterator src_x_iterator;
447 
448  // Determines whether the channels of a given pixel iterator are adjacent in memory.
449  // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
450  BOOST_STATIC_CONSTANT(bool, adjacent=
451  !iterator_is_step<src_x_iterator>::value &&
452  (is_planar<src_x_iterator>::value ||
453  num_channels<View>::value==1));
454  public:
455  typedef typename __kth_channel_view_basic<K,View,adjacent>::type type;
456 
457  static type make(const View& src) {
458  return __kth_channel_view_basic<K,View,adjacent>::make(src);
459  }
460  };
461 
466  template <int K, typename SrcP> // SrcP is a reference to PixelConcept (could be pixel value or const/non-const reference)
467  // Examples: pixel<T,L>, pixel<T,L>&, const pixel<T,L>&, planar_pixel_reference<T&,L>, planar_pixel_reference<const T&,L>
469  BOOST_STATIC_CONSTANT(bool, is_mutable=pixel_is_reference<SrcP>::value && pixel_reference_is_mutable<SrcP>::value);
470  private:
471  typedef typename remove_reference<SrcP>::type src_pixel_t;
472  typedef typename kth_element_type<src_pixel_t, K>::type channel_t;
473  typedef typename src_pixel_t::const_reference const_ref_t;
475  public:
479  typedef SrcP argument_type;
480  typedef typename mpl::if_c<is_mutable, ref_t, value_type>::type reference;
481  typedef reference result_type;
482 
484  template <typename P> kth_channel_deref_fn(const kth_channel_deref_fn<K,P>&) {}
485 
486  result_type operator()(argument_type srcP) const {
487  return result_type(gil::at_c<K>(srcP));
488  }
489  };
490 
491  template <int K, typename View> struct __kth_channel_view<K,View,false> {
492  private:
494  typedef typename View::template add_deref<deref_t> AD;
495  public:
496  typedef typename AD::type type;
497  static type make(const View& src) {
498  return AD::make(src, deref_t());
499  }
500  };
501 } // namespace detail
502 
509 template <int K, typename View>
511 private:
512  GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
513  typedef detail::__kth_channel_view<K,View,view_is_basic<View>::value> VB;
514 public:
515  typedef typename VB::type type;
516  static type make(const View& src) { return VB::make(src); }
517 };
518 
520 template <int K, typename View>
521 typename kth_channel_view_type<K,View>::type kth_channel_view(const View& src) {
523 }
524 
525 } } // namespace boost::gil
526 
527 #endif
Returns the type of a transposed view that has a dynamic step along both X and Y. ...
Definition: image_view_factory.hpp:46
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:50
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept.
Definition: metafunctions.hpp:30
Function object that given a source pixel, returns it converted to a given color space and channel de...
Definition: image_view_factory.hpp:116
Returns the type of a view that has a dynamic step along both X and Y.
Definition: image_view_factory.hpp:41
Given a source image view type View, returns the type of an image view over a single channel of ViewI...
Definition: image_view_factory.hpp:381
Helper base class for pixel dereference adaptors.
Definition: utilities.hpp:90
Function object that returns a grayscale reference of the K-th channel (specified as a template param...
Definition: image_view_factory.hpp:468
Returns the type of a view that does color conversion upon dereferencing its pixels.
Definition: image_view_factory.hpp:154
Determines if the given pixel reference is mutable (i.e. its channels can be changed) ...
Definition: metafunctions.hpp:170
Function object that returns a grayscale reference of the N-th channel of a given reference...
Definition: image_view_factory.hpp:337
detail::channel_pointer_type< HomogeneousView >::type planar_view_get_raw_data(const HomogeneousView &view, int plane_index)
Returns C pointer to the the channels of a given color plane of a planar homogeneous view...
Definition: image_view_factory.hpp:101
auto interleaved_view(point< std::size_t > dim, Iterator pixels, std::ptrdiff_t rowsize_in_bytes) -> typename type_from_x_iterator< Iterator >::view_t
Constructing image views from raw interleaved pixel data.
Definition: image_view_factory.hpp:62
Pixel concept that allows for changing its channels.
Definition: concepts.hpp:911
Returns the type of a homogeneous pixel reference given the channel type, layout, whether it operates...
Definition: metafunctions.hpp:203
detail::channel_pointer_type< HomogeneousView >::type interleaved_view_get_raw_data(const HomogeneousView &view)
Returns C pointer to the the channels of an interleaved homogeneous view.
Definition: image_view_factory.hpp:91
Definition: color_convert.hpp:30
GIL's 2-dimensional view over immutable GIL pixels.
Definition: concepts.hpp:2020
const image< Pixel, IsPlanar, Alloc >::view_t & view(image< Pixel, IsPlanar, Alloc > &img)
Returns the non-constant-pixel view of an image.
Definition: image.hpp:460
color_converted_view_type< View, DstP >::type color_converted_view(const View &src)
overload of generic color_converted_view with the default color-converter
Definition: image_view_factory.hpp:173
Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pi...
Definition: metafunctions.hpp:160
class for color-converting one pixel to another
Definition: color_convert.hpp:282
Basic views must be over basic locators.
Definition: metafunctions.hpp:100
Given a source image view type View, returns the type of an image view over a given channel of View...
Definition: image_view_factory.hpp:510
2D point both axes of which have the same dimension typeModels: Point2DConcept
Definition: concepts.hpp:42