Turi Create  4.0
resample.hpp
Go to the documentation of this file.
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 /*
7  Copyright 2005-2007 Adobe Systems Incorporated
8  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
9  or a copy at http://opensource.adobe.com/licenses.html)
10 */
11 
12 /*************************************************************************************************/
13 
14 #ifndef GIL_RESAMPLE_HPP
15 #define GIL_RESAMPLE_HPP
16 
17 #include <boost/lambda/lambda.hpp>
18 #include <boost/lambda/bind.hpp>
19 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
20 #include "affine.hpp"
21 
22 ////////////////////////////////////////////////////////////////////////////////////////
23 /// \file
24 /// \brief support for generic image resampling
25 /// NOTE: The code is for example use only. It is not optimized for performance
26 /// \author Lubomir Bourdev and Hailin Jin \n
27 /// Adobe Systems Incorporated
28 /// \date 2005-2007 \n October 30, 2006
29 ///
30 ////////////////////////////////////////////////////////////////////////////////////////
31 
32 namespace boost { namespace gil {
33 
34 ///////////////////////////////////////////////////////////////////////////
35 ////
36 //// resample_pixels: set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
37 ////
38 ///////////////////////////////////////////////////////////////////////////
39 
40 template <typename MapFn> struct mapping_traits {};
41 
42 /// \brief Set each pixel in the destination view as the result of a sampling function over the transformed coordinates of the source view
43 /// \ingroup ImageAlgorithms
44 ///
45 /// The provided implementation works for 2D image views only
46 template <typename Sampler, // Models SamplerConcept
47  typename SrcView, // Models RandomAccess2DImageViewConcept
48  typename DstView, // Models MutableRandomAccess2DImageViewConcept
49  typename MapFn> // Models MappingFunctionConcept
50 void resample_pixels(const SrcView& src_view, const DstView& dst_view, const MapFn& dst_to_src, Sampler sampler=Sampler()) {
51  typename DstView::point_t dst_dims=dst_view.dimensions();
52  typename DstView::point_t dst_p;
54 
55  for (dst_p.y=0; dst_p.y<dst_dims.y; ++dst_p.y) {
56  typename DstView::x_iterator xit = dst_view.row_begin(dst_p.y);
57  for (dst_p.x=0; dst_p.x<dst_dims.x; ++dst_p.x) {
58  sample(sampler, src_view, transform(dst_to_src, dst_p), xit[dst_p.x]);
59  }
60  }
61 }
62 
63 ///////////////////////////////////////////////////////////////////////////
64 ////
65 //// resample_pixels when one or both image views are run-time instantiated.
66 ////
67 ///////////////////////////////////////////////////////////////////////////
68 
69 namespace detail {
70  template <typename Sampler, typename MapFn>
71  struct resample_pixels_fn : public binary_operation_obj<resample_pixels_fn<Sampler,MapFn> > {
72  MapFn _dst_to_src;
73  Sampler _sampler;
74  resample_pixels_fn(const MapFn& dst_to_src, const Sampler& sampler) : _dst_to_src(dst_to_src), _sampler(sampler) {}
75 
76  template <typename SrcView, typename DstView> void apply_compatible(const SrcView& src, const DstView& dst) const {
77  resample_pixels(src, dst, _dst_to_src, _sampler);
78  }
79  };
80 }
81 
82 /// \brief resample_pixels when the source is run-time specified
83 /// If invoked on incompatible views, throws std::bad_cast()
84 /// \ingroup ImageAlgorithms
85 template <typename Sampler, typename Types1, typename V2, typename MapFn>
86 void resample_pixels(const any_image_view<Types1>& src, const V2& dst, const MapFn& dst_to_src, Sampler sampler=Sampler()) {
87  apply_operation(src,bind(detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler), _1, dst));
88 }
89 
90 /// \brief resample_pixels when the destination is run-time specified
91 /// If invoked on incompatible views, throws std::bad_cast()
92 /// \ingroup ImageAlgorithms
93 template <typename Sampler, typename V1, typename Types2, typename MapFn>
94 void resample_pixels(const V1& src, const any_image_view<Types2>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler()) {
95  apply_operation(dst,bind(detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler), src, _1));
96 }
97 
98 /// \brief resample_pixels when both the source and the destination are run-time specified
99 /// If invoked on incompatible views, throws std::bad_cast()
100 /// \ingroup ImageAlgorithms
101 template <typename Sampler, typename SrcTypes, typename DstTypes, typename MapFn>
102 void resample_pixels(const any_image_view<SrcTypes>& src, const any_image_view<DstTypes>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler()) {
103  apply_operation(src,dst,detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler));
104 }
105 
106 ///////////////////////////////////////////////////////////////////////////
107 ////
108 //// resample_subimage: copy into the destination a rotated rectangular region from the source, rescaling it to fit into the destination
109 ////
110 ///////////////////////////////////////////////////////////////////////////
111 
112 // Extract into dst the rotated bounds [src_min..src_max] rotated at 'angle' from the source view 'src'
113 // The source coordinates are in the coordinate space of the source image
114 // Note that the views could also be variants (i.e. any_image_view)
115 template <typename Sampler, typename SrcMetaView, typename DstMetaView>
116 void resample_subimage(const SrcMetaView& src, const DstMetaView& dst,
117  double src_min_x, double src_min_y,
118  double src_max_x, double src_max_y,
119  double angle, const Sampler& sampler=Sampler()) {
120  double src_width = std::max<double>(src_max_x - src_min_x - 1,1);
121  double src_height = std::max<double>(src_max_y - src_min_y - 1,1);
122  double dst_width = std::max<double>((double)(dst.width()-1),1);
123  double dst_height = std::max<double>((double)(dst.height()-1),1);
124 
125  matrix3x2<double> mat =
126  matrix3x2<double>::get_translate(-dst_width/2.0, -dst_height/2.0) *
127  matrix3x2<double>::get_scale(src_width / dst_width, src_height / dst_height)*
129  matrix3x2<double>::get_translate(src_min_x + src_width/2.0, src_min_y + src_height/2.0);
130  resample_pixels(src,dst,mat,sampler);
131 }
132 
133 ///////////////////////////////////////////////////////////////////////////
134 ////
135 //// resize_view: Copy the source view into the destination, scaling to fit
136 ////
137 ///////////////////////////////////////////////////////////////////////////
138 
139 template <typename Sampler, typename SrcMetaView, typename DstMetaView>
140 void resize_view(const SrcMetaView& src, const DstMetaView& dst, const Sampler& sampler=Sampler()) {
141  resample_subimage(src,dst,0.0,0.0,(double)src.width(),(double)src.height(),0.0,sampler);
142 }
143 
144 } } // namespace boost::gil
145 
146 #endif
support for affine transformations
void resample_pixels(const SrcView &src_view, const DstView &dst_view, const MapFn &dst_to_src, Sampler sampler=Sampler())
Set each pixel in the destination view as the result of a sampling function over the transformed coor...
Definition: resample.hpp:50
Define affine mapping that transforms the source coordinates by the affine transformation.
Definition: affine.hpp:84