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/image_view.hpp>
15 #include <boost/gil/metafunctions.hpp>
16 #include <boost/gil/point.hpp>
17 #include <boost/gil/detail/mp11.hpp>
18 
19 #include <boost/assert.hpp>
20 
21 #include <cstddef>
22 #include <type_traits>
23 
27 
31 
35 
36 namespace boost { namespace gil {
37 
38 struct default_color_converter;
39 
40 template <typename T> struct transposed_type;
41 
44 template <typename View>
46  : dynamic_y_step_type<typename dynamic_x_step_type<View>::type> {};
47 
50 template <typename View>
52  : dynamic_xy_step_type<typename transposed_type<View>::type> {};
53 
56 template <typename Iterator>
58  std::size_t width, std::size_t height,
59  Iterator pixels, std::ptrdiff_t rowsize_in_bytes)
61 {
62  using RView = typename type_from_x_iterator<Iterator>::view_t;
63  return RView(width, height, typename RView::locator(pixels, rowsize_in_bytes));
64 }
65 
68 template <typename Iterator>
70  point<std::ptrdiff_t> dim, Iterator pixels,
71  std::ptrdiff_t rowsize_in_bytes)
73 {
74  using RView = typename type_from_x_iterator<Iterator>::view_t;
75  return RView(dim, typename RView::locator(pixels, rowsize_in_bytes));
76 }
77 
79 // interleaved_view_get_raw_data, planar_view_get_raw_data - return pointers to the raw data (the channels) of a basic homogeneous view.
81 
82 namespace detail {
83  template <typename View, bool IsMutable>
84  struct channel_pointer_type_impl;
85 
86  template <typename View>
87  struct channel_pointer_type_impl<View, true>
88  {
89  using type = typename channel_type<View>::type*;
90  };
91 
92  template <typename View>
93  struct channel_pointer_type_impl<View, false>
94  {
95  using type = const typename channel_type<View>::type*;
96  };
97 
98  template <typename View>
99  struct channel_pointer_type
100  : public channel_pointer_type_impl<View, view_is_mutable<View>::value> {};
101 } // namespace detail
102 
105 template <typename HomogeneousView>
106 auto interleaved_view_get_raw_data(HomogeneousView const& view)
107  -> typename detail::channel_pointer_type<HomogeneousView>::type
108 {
109  static_assert(!is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value, "");
110  static_assert(std::is_pointer<typename HomogeneousView::x_iterator>::value, "");
111 
112  return &gil::at_c<0>(view(0,0));
113 }
114 
117 template <typename HomogeneousView>
118 auto planar_view_get_raw_data(HomogeneousView const& view, int plane_index)
119  -> typename detail::channel_pointer_type<HomogeneousView>::type
120 {
121  static_assert(is_planar<HomogeneousView>::value && view_is_basic<HomogeneousView>::value, "");
122  return dynamic_at_c(view.row_begin(0),plane_index);
123 }
124 
125 
129 
134 template <typename SrcConstRefP, typename DstP, typename CC=default_color_converter > // const_reference to the source pixel and destination pixel value
135 class color_convert_deref_fn : public deref_base<color_convert_deref_fn<SrcConstRefP,DstP,CC>, DstP, DstP, const DstP&, SrcConstRefP, DstP, false> {
136 private:
137  CC _cc; // color-converter
138 public:
140  color_convert_deref_fn(CC cc_in) : _cc(cc_in) {}
141 
142  DstP operator()(SrcConstRefP srcP) const {
143  DstP dstP;
144  _cc(srcP,dstP);
145  return dstP;
146  }
147 };
148 
149 namespace detail {
150  // Add color converter upon dereferencing
151  template <typename SrcView, typename CC, typename DstP, typename SrcP>
152  struct _color_converted_view_type {
153  private:
155  using add_ref_t = typename SrcView::template add_deref<deref_t>;
156  public:
157  using type = typename add_ref_t::type;
158  static type make(const SrcView& sv,CC cc) {return add_ref_t::make(sv,deref_t(cc));}
159  };
160 
161  // If the Src view has the same pixel type as the target, there is no need for color conversion
162  template <typename SrcView, typename CC, typename DstP>
163  struct _color_converted_view_type<SrcView,CC,DstP,DstP> {
164  using type = SrcView;
165  static type make(const SrcView& sv,CC) {return sv;}
166  };
167 } // namespace detail
168 
169 
172 template <typename SrcView, typename DstP, typename CC=default_color_converter>
173 struct color_converted_view_type : public detail::_color_converted_view_type<SrcView,
174  CC,
175  DstP,
176  typename SrcView::value_type> {
177  BOOST_GIL_CLASS_REQUIRE(DstP, boost::gil, MutablePixelConcept)//why does it have to be mutable???
178 };
179 
180 
183 template <typename DstP, typename View, typename CC>
184 inline auto color_converted_view(View const& src,CC cc)
185  -> typename color_converted_view_type<View,DstP,CC>::type
186 {
188 }
189 
192 template <typename DstP, typename View>
193 inline auto color_converted_view(View const& src)
194  -> typename color_converted_view_type<View,DstP>::type
195 {
196  return color_converted_view<DstP>(src,default_color_converter());
197 }
198 
202 
204 template <typename View>
205 inline auto flipped_up_down_view(View const& src)
206  -> typename dynamic_y_step_type<View>::type
207 {
208  using RView = typename dynamic_y_step_type<View>::type;
209  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1));
210 }
211 
215 
217 template <typename View>
218 inline auto flipped_left_right_view(View const& src)
219  -> typename dynamic_x_step_type<View>::type
220 {
221  using RView = typename dynamic_x_step_type<View>::type;
222  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,0),-1,1));
223 }
224 
228 
230 template <typename View>
231 inline auto transposed_view(View const& src)
232  -> typename dynamic_xy_step_transposed_type<View>::type
233 {
234  using RView = typename dynamic_xy_step_transposed_type<View>::type;
235  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,0),1,1,true));
236 }
237 
241 
243 template <typename View>
244 inline auto rotated90cw_view(View const& src)
245  -> typename dynamic_xy_step_transposed_type<View>::type
246 {
247  using RView = typename dynamic_xy_step_transposed_type<View>::type;
248  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(0,src.height()-1),-1,1,true));
249 }
250 
254 
256 template <typename View>
257 inline auto rotated90ccw_view(View const& src)
258  -> typename dynamic_xy_step_transposed_type<View>::type
259 {
260  using RView = typename dynamic_xy_step_transposed_type<View>::type;
261  return RView(src.height(),src.width(),typename RView::xy_locator(src.xy_at(src.width()-1,0),1,-1,true));
262 }
263 
267 
269 template <typename View>
270 inline auto rotated180_view(View const& src)
271  -> typename dynamic_xy_step_type<View>::type
272 {
273  using RView = typename dynamic_xy_step_type<View>::type;
274  return RView(src.dimensions(),typename RView::xy_locator(src.xy_at(src.width()-1,src.height()-1),-1,-1));
275 }
276 
280 
282 template <typename View>
283 inline View subimage_view(
284  View const& src,
285  typename View::point_t const& topleft,
286  typename View::point_t const& dimensions)
287 {
288  return View(dimensions, src.xy_at(topleft));
289 }
290 
292 template <typename View>
293 inline View subimage_view(View const& src,
294  typename View::coord_t x_min,
295  typename View::coord_t y_min,
296  typename View::coord_t width,
297  typename View::coord_t height)
298 {
299  return View(width, height, src.xy_at(x_min, y_min));
300 }
301 
305 
307 template <typename View>
308 inline
309 auto subsampled_view(View const& src, typename View::coord_t x_step, typename View::coord_t y_step)
310  -> typename dynamic_xy_step_type<View>::type
311 {
312  BOOST_ASSERT(x_step > 0 && y_step > 0);
313  using view_t =typename dynamic_xy_step_type<View>::type;
314  return view_t(
315  (src.width() + (x_step - 1)) / x_step,
316  (src.height() + (y_step - 1)) / y_step,
317  typename view_t::xy_locator(src.xy_at(0,0), x_step, y_step));
318 }
319 
321 template <typename View>
322 inline auto subsampled_view(View const& src, typename View::point_t const& step)
323  -> typename dynamic_xy_step_type<View>::type
324 {
325  return subsampled_view(src, step.x, step.y);
326 }
327 
331 
332 namespace detail {
333  template <typename View, bool AreChannelsTogether> struct __nth_channel_view_basic;
334 
335  // nth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
336  // or images with a step
337  template <typename View>
338  struct __nth_channel_view_basic<View,false> {
339  using type = typename view_type<typename channel_type<View>::type, gray_layout_t, false, true, view_is_mutable<View>::value>::type;
340 
341  static type make(View const& src, int n) {
342  using locator_t = typename type::xy_locator;
343  using x_iterator_t = typename type::x_iterator;
344  using x_iterator_base_t = typename iterator_adaptor_get_base<x_iterator_t>::type;
345  x_iterator_t sit(x_iterator_base_t(&(src(0,0)[n])),src.pixels().pixel_size());
346  return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
347  }
348  };
349 
350  // nth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
351  template <typename View>
352  struct __nth_channel_view_basic<View,true> {
353  using type = typename view_type<typename channel_type<View>::type, gray_layout_t, false, false, view_is_mutable<View>::value>::type;
354  static type make(View const& src, int n) {
355  using x_iterator_t = typename type::x_iterator;
356  return interleaved_view(src.width(),src.height(),(x_iterator_t)&(src(0,0)[n]), src.pixels().row_size());
357  }
358  };
359 
360  template <typename View, bool IsBasic> struct __nth_channel_view;
361 
362  // For basic (memory-based) views dispatch to __nth_channel_view_basic
363  template <typename View>
364  struct __nth_channel_view<View,true>
365  {
366  private:
367  using src_x_iterator = typename View::x_iterator;
368 
369  // Determines whether the channels of a given pixel iterator are adjacent in memory.
370  // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
371  static constexpr bool adjacent =
372  !iterator_is_step<src_x_iterator>::value &&
373  (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
374 
375  public:
376  using type = typename __nth_channel_view_basic<View,adjacent>::type;
377 
378  static type make(View const& src, int n) {
379  return __nth_channel_view_basic<View,adjacent>::make(src,n);
380  }
381  };
382 
387  template <typename SrcP> // SrcP is a reference to PixelConcept (could be pixel value or const/non-const reference)
388  // Examples: pixel<T,L>, pixel<T,L>&, const pixel<T,L>&, planar_pixel_reference<T&,L>, planar_pixel_reference<const T&,L>
390  {
391  static constexpr bool is_mutable =
393  private:
394  using src_pixel_t = typename std::remove_reference<SrcP>::type;
395  using channel_t = typename channel_type<src_pixel_t>::type;
396  using const_ref_t = typename src_pixel_t::const_reference;
398  public:
400  using value_type = typename pixel_value_type<channel_t,gray_layout_t>::type;
402  using argument_type = SrcP;
403  using reference = mp11::mp_if_c<is_mutable, ref_t, value_type>;
404  using result_type = reference;
405 
406  nth_channel_deref_fn(int n=0) : _n(n) {}
407  template <typename P>
408  nth_channel_deref_fn(const nth_channel_deref_fn<P>& d) : _n(d._n) {}
409 
410  int _n; // the channel to use
411 
412  auto operator()(argument_type srcP) const -> result_type
413  {
414  return result_type(srcP[_n]);
415  }
416  };
417 
418  template <typename View> struct __nth_channel_view<View,false> {
419  private:
421  using AD = typename View::template add_deref<deref_t>;
422  public:
423  using type = typename AD::type;
424  static type make(View const& src, int n) {
425  return AD::make(src, deref_t(n));
426  }
427  };
428 } // namespace detail
429 
436 template <typename View>
438 private:
439  BOOST_GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
440  using VB = detail::__nth_channel_view<View,view_is_basic<View>::value>;
441 public:
442  using type = typename VB::type;
443  static type make(View const& src, int n) { return VB::make(src,n); }
444 };
445 
447 template <typename View>
448 typename nth_channel_view_type<View>::type nth_channel_view(View const& src, int n) {
449  return nth_channel_view_type<View>::make(src,n);
450 }
451 
455 
456 namespace detail {
457  template <int K, typename View, bool AreChannelsTogether> struct __kth_channel_view_basic;
458 
459  // kth_channel_view when the channels are not adjacent in memory. This can happen for multi-channel interleaved images
460  // or images with a step
461  template <int K, typename View>
462  struct __kth_channel_view_basic<K,View,false> {
463  private:
464  using channel_t = typename kth_element_type<typename View::value_type,K>::type;
465  public:
466  using type = typename view_type<channel_t, gray_layout_t, false, true, view_is_mutable<View>::value>::type;
467 
468  static type make(View const& src) {
469  using locator_t = typename type::xy_locator;
470  using x_iterator_t = typename type::x_iterator;
471  using x_iterator_base_t = typename iterator_adaptor_get_base<x_iterator_t>::type;
472  x_iterator_t sit(x_iterator_base_t(&gil::at_c<K>(src(0,0))),src.pixels().pixel_size());
473  return type(src.dimensions(),locator_t(sit, src.pixels().row_size()));
474  }
475  };
476 
477  // kth_channel_view when the channels are together in memory (true for simple grayscale or planar images)
478  template <int K, typename View>
479  struct __kth_channel_view_basic<K,View,true> {
480  private:
481  using channel_t = typename kth_element_type<typename View::value_type, K>::type;
482  public:
483  using type = typename view_type<channel_t, gray_layout_t, false, false, view_is_mutable<View>::value>::type;
484  static type make(View const& src) {
485  using x_iterator_t = typename type::x_iterator;
486  return interleaved_view(src.width(),src.height(),(x_iterator_t)&gil::at_c<K>(src(0,0)), src.pixels().row_size());
487  }
488  };
489 
490  template <int K, typename View, bool IsBasic> struct __kth_channel_view;
491 
492  // For basic (memory-based) views dispatch to __kth_channel_view_basic
493  template <int K, typename View> struct __kth_channel_view<K,View,true>
494  {
495  private:
496  using src_x_iterator = typename View::x_iterator;
497 
498  // Determines whether the channels of a given pixel iterator are adjacent in memory.
499  // Planar and grayscale iterators have channels adjacent in memory, whereas multi-channel interleaved and iterators with non-fundamental step do not.
500  static constexpr bool adjacent =
501  !iterator_is_step<src_x_iterator>::value &&
502  (is_planar<src_x_iterator>::value || num_channels<View>::value == 1);
503 
504  public:
505  using type = typename __kth_channel_view_basic<K,View,adjacent>::type;
506 
507  static type make(View const& src) {
508  return __kth_channel_view_basic<K,View,adjacent>::make(src);
509  }
510  };
511 
518  template <int K, typename SrcP>
520  {
521  static constexpr bool is_mutable =
523 
524  private:
525  using src_pixel_t = typename std::remove_reference<SrcP>::type;
526  using channel_t = typename kth_element_type<src_pixel_t, K>::type;
527  using const_ref_t = typename src_pixel_t::const_reference;
529 
530  public:
532  using value_type = typename pixel_value_type<channel_t,gray_layout_t>::type;
534  using argument_type = SrcP;
535  using reference = mp11::mp_if_c<is_mutable, ref_t, value_type>;
536  using result_type = reference;
537 
539  template <typename P>
541 
542  result_type operator()(argument_type srcP) const {
543  return result_type(gil::at_c<K>(srcP));
544  }
545  };
546 
547  template <int K, typename View> struct __kth_channel_view<K,View,false> {
548  private:
550  using AD = typename View::template add_deref<deref_t>;
551  public:
552  using type = typename AD::type;
553  static type make(View const& src) {
554  return AD::make(src, deref_t());
555  }
556  };
557 } // namespace detail
558 
565 template <int K, typename View>
567 private:
568  BOOST_GIL_CLASS_REQUIRE(View, boost::gil, ImageViewConcept)
569  using VB = detail::__kth_channel_view<K,View,view_is_basic<View>::value>;
570 public:
571  using type = typename VB::type;
572  static type make(View const& src) { return VB::make(src); }
573 };
574 
576 template <int K, typename View>
577 auto kth_channel_view(View const& src)
578  -> typename kth_channel_view_type<K,View>::type
579 {
581 }
582 
583 } } // namespace boost::gil
584 
585 #endif
Function object that given a source pixel, returns it converted to a given color space and channel de...
Definition: image_view_factory.hpp:135
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,...
Definition: image_view.hpp:54
auto view(image< Pixel, IsPlanar, Alloc > &img) -> typename image< Pixel, IsPlanar, Alloc >::view_t const &
Returns the non-constant-pixel view of an image.
Definition: image.hpp:565
auto interleaved_view_get_raw_data(HomogeneousView const &view) -> typename detail::channel_pointer_type< HomogeneousView >::type
Returns C pointer to the the channels of an interleaved homogeneous view.
Definition: image_view_factory.hpp:106
auto planar_view_get_raw_data(HomogeneousView const &view, int plane_index) -> typename detail::channel_pointer_type< HomogeneousView >::type
Returns C pointer to the the channels of a given color plane of a planar homogeneous view.
Definition: image_view_factory.hpp:118
auto interleaved_view(point< std::ptrdiff_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:69
auto color_converted_view(View const &src) -> typename color_converted_view_type< View, DstP >::type
overload of generic color_converted_view with the default color-converter
Definition: image_view_factory.hpp:193
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
GIL's 2-dimensional view over immutable GIL pixels.
Definition: concepts/image_view.hpp:376
Pixel concept that allows for changing its channels.
Definition: concepts/pixel.hpp:102
Definition: color_convert.hpp:31
Returns the type of a view that does color conversion upon dereferencing its pixels.
Definition: image_view_factory.hpp:176
class for color-converting one pixel to another
Definition: color_convert.hpp:328
Helper base class for pixel dereference adaptors.
Definition: utilities.hpp:107
Function object that returns a grayscale reference of the K-th channel (specified as a template param...
Definition: image_view_factory.hpp:520
Function object that returns a grayscale reference of the N-th channel of a given reference....
Definition: image_view_factory.hpp:390
Returns the type of a transposed view that has a dynamic step along both X and Y.
Definition: image_view_factory.hpp:52
Returns the type of a view that has a dynamic step along both X and Y.
Definition: image_view_factory.hpp:46
Base template for types that model HasDynamicYStepTypeConcept.
Definition: dynamic_step.hpp:21
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:566
Given a source image view type View, returns the type of an image view over a single channel of View.
Definition: image_view_factory.hpp:437
Given a model of a pixel, determines whether the model represents a pixel reference (as opposed to pi...
Definition: metafunctions.hpp:218
Determines if the given pixel reference is mutable (i.e. its channels can be changed)
Definition: metafunctions.hpp:231
Returns the type of a homogeneous pixel reference given the channel type, layout, whether it operates...
Definition: metafunctions.hpp:267
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept,...
Definition: pixel.hpp:106
Basic views must be over basic locators.
Definition: metafunctions.hpp:130