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/dynamic_step.hpp>
13 #include <boost/gil/gray.hpp>
14 #include <boost/gil/metafunctions.hpp>
15 #include <boost/gil/point.hpp>
16 
17 #include <boost/assert.hpp>
18 
19 #include <cstddef>
20 #include <type_traits>
21 
25 
29 
33 
34 namespace boost { namespace gil {
35 struct default_color_converter;
36 
37 template <typename T> struct transposed_type;
38 
41 template <typename View>
42 struct dynamic_xy_step_type : public dynamic_y_step_type<typename dynamic_x_step_type<View>::type> {};
43 
46 template <typename View>
47 struct dynamic_xy_step_transposed_type : public dynamic_xy_step_type<typename transposed_type<View>::type> {};
48 
49 
52 template <typename Iterator>
54 interleaved_view(std::size_t width, std::size_t height,
55  Iterator pixels, std::ptrdiff_t rowsize_in_bytes) {
56  using RView = typename type_from_x_iterator<Iterator>::view_t;
57  return RView(width, height, typename RView::locator(pixels, rowsize_in_bytes));
58 }
59 
62 template <typename Iterator>
63 auto interleaved_view(point<std::size_t> dim, Iterator pixels,
64  std::ptrdiff_t rowsize_in_bytes)
66 {
67  using RView = typename type_from_x_iterator<Iterator>::view_t;
68  return RView(dim, typename RView::locator(pixels, rowsize_in_bytes));
69 }
70 
72 // interleaved_view_get_raw_data, planar_view_get_raw_data - return pointers to the raw data (the channels) of a basic homogeneous view.
74 
75 namespace detail {
76  template <typename View, bool IsMutable> struct channel_pointer_type_impl;
77 
78  template <typename View> struct channel_pointer_type_impl<View, true> {
79  using type = typename channel_type<View>::type *;
80  };
81  template <typename View> struct channel_pointer_type_impl<View, false> {
82  using type = const typename channel_type<View>::type *;
83  };
84 
85  template <typename View> struct channel_pointer_type
86  : public channel_pointer_type_impl<View, view_is_mutable<View>::value> {};
87 } // namespace detail
88 
91 template <typename HomogeneousView>
92 typename detail::channel_pointer_type<HomogeneousView>::type interleaved_view_get_raw_data(const HomogeneousView& view) {
93  static_assert(!is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value, "");
94  static_assert(std::is_pointer<typename HomogeneousView::x_iterator>::value, "");
95 
96  return &gil::at_c<0>(view(0,0));
97 }
98 
101 template <typename HomogeneousView>
102 typename detail::channel_pointer_type<HomogeneousView>::type planar_view_get_raw_data(const HomogeneousView& view, int plane_index) {
103  static_assert(is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value, "");
104  return dynamic_at_c(view.row_begin(0),plane_index);
105 }
106 
107 
111 
116 template <typename SrcConstRefP, typename DstP, typename CC=default_color_converter > // const_reference to the source pixel and destination pixel value
117 class color_convert_deref_fn : public deref_base<color_convert_deref_fn<SrcConstRefP,DstP,CC>, DstP, DstP, const DstP&, SrcConstRefP, DstP, false> {
118 private:
119  CC _cc; // color-converter
120 public:
122  color_convert_deref_fn(CC cc_in) : _cc(cc_in) {}
123 
124  DstP operator()(SrcConstRefP srcP) const {
125  DstP dstP;
126  _cc(srcP,dstP);
127  return dstP;
128  }
129 };
130 
131 namespace detail {
132  // Add color converter upon dereferencing
133  template <typename SrcView, typename CC, typename DstP, typename SrcP>
134  struct _color_converted_view_type {
135  private:
137  using add_ref_t = typename SrcView::template add_deref<deref_t>;
138  public:
139  using type = typename add_ref_t::type;
140  static type make(const SrcView& sv,CC cc) {return add_ref_t::make(sv,deref_t(cc));}
141  };
142 
143  // If the Src view has the same pixel type as the target, there is no need for color conversion
144  template <typename SrcView, typename CC, typename DstP>
145  struct _color_converted_view_type<SrcView,CC,DstP,DstP> {
146  using type = SrcView;
147  static type make(const SrcView& sv,CC) {return sv;}
148  };
149 } // namespace detail
150 
151 
154 template <typename SrcView, typename DstP, typename CC=default_color_converter>
155 struct color_converted_view_type : public detail::_color_converted_view_type<SrcView,
156  CC,
157  DstP,
158  typename SrcView::value_type> {
159  GIL_CLASS_REQUIRE(DstP, boost::gil, MutablePixelConcept)//why does it have to be mutable???
160 };
161 
162 
165 template <typename DstP, typename View, typename CC>
168 }
169 
172 template <typename DstP, typename View>
173 inline typename color_converted_view_type<View,DstP>::type
174 color_converted_view(const View& src) {
175  return color_converted_view<DstP>(src,default_color_converter());
176 }
177 
181 
183 template <typename View>
184 inline typename dynamic_y_step_type<View>::type flipped_up_down_view(const View& src) {
185  using RView = typename dynamic_y_step_type<View>::type;
186  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1));
187 }
188 
192 
194 template <typename View>
195 inline typename dynamic_x_step_type<View>::type flipped_left_right_view(const View& src) {
196  using RView = typename dynamic_x_step_type<View>::type;
197  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,0),-1,1));
198 }
199 
203 
205 template <typename View>
206 inline typename dynamic_xy_step_transposed_type<View>::type transposed_view(const View& src) {
207  using RView = typename dynamic_xy_step_transposed_type<View>::type;
208  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,0),1,1,true));
209 }
210 
214 
216 template <typename View>
217 inline typename dynamic_xy_step_transposed_type<View>::type rotated90cw_view(const View& src) {
218  using RView = typename dynamic_xy_step_transposed_type<View>::type;
219  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1,1,true));
220 }
221 
225 
227 template <typename View>
228 inline typename dynamic_xy_step_transposed_type<View>::type rotated90ccw_view(const View& src) {
229  using RView = typename dynamic_xy_step_transposed_type<View>::type;
230  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(src.width()-1,0),1,-1,true));
231 }
232 
236 
238 template <typename View>
239 inline typename dynamic_xy_step_type<View>::type rotated180_view(const View& src) {
240  using RView = typename dynamic_xy_step_type<View>::type;
241  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,src.height()-1),-1,-1));
242 }
243 
247 
249 template <typename View>
250 inline View subimage_view(const View& src, const typename View::point_t& topleft, const typename View::point_t& dimensions) {
251  return View(dimensions,src.xy_at(topleft));
252 }
253 
255 template <typename View>
256 inline View subimage_view(const View& src, int xMin, int yMin, int width, int height) {
257  return View(width,height,src.xy_at(xMin,yMin));
258 }
259 
263 
265 template <typename View>
266 inline typename dynamic_xy_step_type<View>::type subsampled_view(const View& src, typename View::coord_t xStep, typename View::coord_t yStep)
267 {
268  BOOST_ASSERT(xStep > 0 && yStep > 0);
269  using RView = typename dynamic_xy_step_type<View>::type;
270  return RView((src.width()+(xStep-1))/xStep,(src.height()+(yStep-1))/yStep,
271  typename RView::xy_locator(src.xy_at(0,0),xStep,yStep));
272 }
273 
275 template <typename View>
276 inline typename dynamic_xy_step_type<View>::type subsampled_view(const View& src, const typename View::point_t& step) {
277  return subsampled_view(src,step.x,step.y);
278 }
279 
283 
284 namespace detail {
285  template <typename View, bool AreChannelsTogether> struct __nth_channel_view_basic;
286 
287  // nth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
288  // or images with a step
289  template <typename View>
290  struct __nth_channel_view_basic<View,false> {
292 
293  static type make(const View& src, int n) {
294  using locator_t = typename type::xy_locator;
295  using x_iterator_t = typename type::x_iterator;
296  using x_iterator_base_t = typename iterator_adaptor_get_base<x_iterator_t>::type;
297  x_iterator_t sit(x_iterator_base_t(&(src(0,0)[n])),src.pixels().pixel_size());
298  return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
299  }
300  };
301 
302  // nth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
303  template <typename View>
304  struct __nth_channel_view_basic<View,true> {
306  static type make(const View& src, int n) {
307  using x_iterator_t = typename type::x_iterator;
308  return interleaved_view(src.width(),src.height(),(x_iterator_t)&(src(0,0)[n]), src.pixels().row_size());
309  }
310  };
311 
312  template <typename View, bool IsBasic> struct __nth_channel_view;
313 
314  // For basic (memory-based) views dispatch to __nth_channel_view_basic
315  template <typename View>
316  struct __nth_channel_view<View,true>
317  {
318  private:
319  using src_x_iterator = typename View::x_iterator;
320 
321  // Determines whether the channels of a given pixel iterator are adjacent in memory.
322  // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
323  static constexpr bool adjacent =
325  (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
326 
327  public:
328  using type = typename __nth_channel_view_basic<View,adjacent>::type;
329 
330  static type make(const View& src, int n) {
331  return __nth_channel_view_basic<View,adjacent>::make(src,n);
332  }
333  };
334 
339  template <typename SrcP> // SrcP is a reference to PixelConcept (could be pixel value or const/non-const reference)
340  // Examples: pixel<T,L>, pixel<T,L>&, const pixel<T,L>&, planar_pixel_reference<T&,L>, planar_pixel_reference<const T&,L>
342  {
343  static constexpr bool is_mutable =
345  private:
346  using src_pixel_t = typename remove_reference<SrcP>::type;
347  using channel_t = typename channel_type<src_pixel_t>::type;
348  using const_ref_t = typename src_pixel_t::const_reference;
350  public:
352  using value_type = typename pixel_value_type<channel_t,gray_layout_t>::type;
354  using argument_type = SrcP;
355  using reference = typename mpl::if_c<is_mutable, ref_t, value_type>::type;
356  using result_type = reference;
357 
358  nth_channel_deref_fn(int n=0) : _n(n) {}
359  template <typename P> nth_channel_deref_fn(const nth_channel_deref_fn<P>& d) : _n(d._n) {}
360 
361  int _n; // the channel to use
362 
363  result_type operator()(argument_type srcP) const {
364  return result_type(srcP[_n]);
365  }
366  };
367 
368  template <typename View> struct __nth_channel_view<View,false> {
369  private:
371  using AD = typename View::template add_deref<deref_t>;
372  public:
373  using type = typename AD::type;
374  static type make(const View& src, int n) {
375  return AD::make(src, deref_t(n));
376  }
377  };
378 } // namespace detail
379 
386 template <typename View>
388 private:
389  GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
390  using VB = detail::__nth_channel_view<View,view_is_basic<View>::value>;
391 public:
392  using type = typename VB::type;
393  static type make(const View& src, int n) { return VB::make(src,n); }
394 };
395 
396 
398 template <typename View>
399 typename nth_channel_view_type<View>::type nth_channel_view(const View& src, int n) {
400  return nth_channel_view_type<View>::make(src,n);
401 }
402 
403 
404 
405 
406 
407 
408 
412 
413 namespace detail {
414  template <int K, typename View, bool AreChannelsTogether> struct __kth_channel_view_basic;
415 
416  // kth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
417  // or images with a step
418  template <int K, typename View>
419  struct __kth_channel_view_basic<K,View,false> {
420  private:
421  using channel_t = typename kth_element_type<typename View::value_type,K>::type;
422  public:
424 
425  static type make(const View& src) {
426  using locator_t = typename type::xy_locator;
427  using x_iterator_t = typename type::x_iterator;
428  using x_iterator_base_t = typename iterator_adaptor_get_base<x_iterator_t>::type;
429  x_iterator_t sit(x_iterator_base_t(&gil::at_c<K>(src(0,0))),src.pixels().pixel_size());
430  return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
431  }
432  };
433 
434  // kth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
435  template <int K, typename View>
436  struct __kth_channel_view_basic<K,View,true> {
437  private:
438  using channel_t = typename kth_element_type<typename View::value_type, K>::type;
439  public:
441  static type make(const View& src) {
442  using x_iterator_t = typename type::x_iterator;
443  return interleaved_view(src.width(),src.height(),(x_iterator_t)&gil::at_c<K>(src(0,0)), src.pixels().row_size());
444  }
445  };
446 
447  template <int K, typename View, bool IsBasic> struct __kth_channel_view;
448 
449  // For basic (memory-based) views dispatch to __kth_channel_view_basic
450  template <int K, typename View> struct __kth_channel_view<K,View,true>
451  {
452  private:
453  using src_x_iterator = typename View::x_iterator;
454 
455  // Determines whether the channels of a given pixel iterator are adjacent in memory.
456  // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
457  static constexpr bool adjacent =
459  (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
460 
461  public:
462  using type = typename __kth_channel_view_basic<K,View,adjacent>::type;
463 
464  static type make(const View& src) {
465  return __kth_channel_view_basic<K,View,adjacent>::make(src);
466  }
467  };
468 
475  template <int K, typename SrcP>
477  {
478  static constexpr bool is_mutable =
480 
481  private:
482  using src_pixel_t = typename remove_reference<SrcP>::type;
483  using channel_t = typename kth_element_type<src_pixel_t, K>::type;
484  using const_ref_t = typename src_pixel_t::const_reference;
486 
487  public:
489  using value_type = typename pixel_value_type<channel_t,gray_layout_t>::type;
491  using argument_type = SrcP;
492  using reference = typename mpl::if_c<is_mutable, ref_t, value_type>::type;
493  using result_type = reference;
494 
496  template <typename P> kth_channel_deref_fn(const kth_channel_deref_fn<K,P>&) {}
497 
498  result_type operator()(argument_type srcP) const {
499  return result_type(gil::at_c<K>(srcP));
500  }
501  };
502 
503  template <int K, typename View> struct __kth_channel_view<K,View,false> {
504  private:
506  using AD = typename View::template add_deref<deref_t>;
507  public:
508  using type = typename AD::type;
509  static type make(const View& src) {
510  return AD::make(src, deref_t());
511  }
512  };
513 } // namespace detail
514 
521 template <int K, typename View>
523 private:
524  GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
525  using VB = detail::__kth_channel_view<K,View,view_is_basic<View>::value>;
526 public:
527  using type = typename VB::type;
528  static type make(const View& src) { return VB::make(src); }
529 };
530 
532 template <int K, typename View>
533 typename kth_channel_view_type<K,View>::type kth_channel_view(const View& src) {
535 }
536 
537 } } // namespace boost::gil
538 
539 #endif
Definition: algorithm.hpp:30
Returns the type of a transposed view that has a dynamic step along both X and Y. ...
Definition: image_view_factory.hpp:47
Returns the type of a homogeneous view given the channel type, layout, whether it operates on planar ...
Definition: metafunctions.hpp:414
Definition: algorithm.hpp:30
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:51
returns the base iterator for a given iterator adaptor. Provide an specialization when introducing ne...
Definition: metafunctions.hpp:43
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept.
Definition: metafunctions.hpp:31
Function object that given a source pixel, returns it converted to a given color space and channel de...
Definition: image_view_factory.hpp:117
Returns the type of a view that has a dynamic step along both X and Y.
Definition: dynamic_step.hpp:27
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:387
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:476
Returns the type of a view that does color conversion upon dereferencing its pixels.
Definition: image_view_factory.hpp:155
Determines if the given pixel reference is mutable (i.e. its channels can be changed) ...
Definition: metafunctions.hpp:171
Represents a color space and ordering of channels in memory.
Definition: utilities.hpp:247
Definition: image_view_factory.hpp:37
Function object that returns a grayscale reference of the N-th channel of a given reference...
Definition: image_view_factory.hpp:341
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:102
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:63
Pixel concept that allows for changing its channels.
Definition: concepts/pixel.hpp:102
Returns the type of a homogeneous pixel reference given the channel type, layout, whether it operates...
Definition: metafunctions.hpp:204
Base template for types that model HasDynamicYStepTypeConcept.
Definition: dynamic_step.hpp:21
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:92
Determines if the given view is mutable (i.e. its pixels can be changed)
Definition: metafunctions.hpp:180
Definition: color_convert.hpp:30
GIL&#39;s 2-dimensional view over immutable GIL pixels.
Definition: concepts/image_view.hpp:373
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:174
Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pi...
Definition: metafunctions.hpp:161
class for color-converting one pixel to another
Definition: color_convert.hpp:282
Returns the number of channels of a pixel-based GIL construct.
Definition: locator.hpp:38
Basic views must be over basic locators.
Definition: metafunctions.hpp:101
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:522
Determines if the given iterator has a step that could be set dynamically.
Definition: metafunctions.hpp:114
2D point both axes of which have the same dimension typeModels: Point2DConcept
Definition: locator.hpp:28
Base template for types that model HasDynamicXStepTypeConcept.
Definition: dynamic_step.hpp:17