1 // This is core/vil1/vil1_warp.hxx
2 #ifndef vil1_warp_hxx_
3 #define vil1_warp_hxx_
4 //:
5 // \file
6 // \brief See vil1_warp.h for a description of this file
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date   23 Jan 99
9 //
10 // \verbatim
11 //  Modifications
12 //   990123 AWF Initial version.
13 // \endverbatim
14 //
15 //-----------------------------------------------------------------------------
16 
17 #include "vil1_warp.h"
18 
19 #include "vil1_interpolate.h"
20 #include "vil1_memory_image_of.h"
21 
22 static inline bool
vil1_warp_inrange_window(vil1_image const & in,int x,int y,int window_size)23 vil1_warp_inrange_window(vil1_image const& in, int x, int y, int window_size)
24 {
25   return x >= window_size && x < in.width() - window_size &&
26     y >= window_size && y < in.height() - window_size;
27 }
28 
29 // See vil1_warp.h
30 template <class PixelType, class Mapper>
vil1_warp_output_driven(vil1_memory_image_of<PixelType> const & in,vil1_memory_image_of<PixelType> & out,Mapper const & map,vil1_warp_interpolation_type interpolation)31 void vil1_warp_output_driven(vil1_memory_image_of<PixelType> const& in,
32                              vil1_memory_image_of<PixelType>& out,
33                              Mapper const& map,
34                              vil1_warp_interpolation_type interpolation)
35 {
36   //int w = in.width();
37   //int h = in.height();
38 
39   int out_w = out.width();
40   int out_h = out.height();
41 
42   for (int oy = 0; oy < out_h; ++oy)
43     for (int ox = 0; ox < out_w; ++ox) {
44       // *** Find (ix, iy) from (ox,oy)
45       double ixd, iyd;
46       map.inverse_map(double(ox), double(oy), &ixd, &iyd);
47 
48       switch (interpolation) {
49         case vil1_warp_interpolation_nearest_neighbour: {
50           // nearest neighbour
51           int ix = int(ixd + 0.5);
52           int iy = int(iyd + 0.5);
53           if (vil1_warp_inrange_window(in, ix, iy, 0))
54             out(ox, oy) = in(ix,iy);
55           break;
56         }
57         case vil1_warp_interpolation_bilinear: {
58           // bilinear
59           vil1_interpolate_bilinear(in, ixd, iyd, &out(ox,oy));
60           break;
61         }
62         case vil1_warp_interpolation_bicubic: {
63           vil1_interpolate_bicubic(in, ixd, iyd, &out(ox,oy));
64           break;
65         }
66         default:
67           break;
68       }
69     }
70 }
71 
72 #define VIL1_WARP_INSTANTIATE(PixelType, Mapper) \
73 template void vil1_warp_output_driven(vil1_memory_image_of<PixelType > const&, \
74                                       vil1_memory_image_of<PixelType >&,\
75                                       Mapper const&,\
76                                       vil1_warp_interpolation_type)
77 
78 #endif // vil1_warp_hxx_
79