1 // This is mul/vil3d/vil3d_resample_simple.hxx
2 #ifndef vil3d_resample_simple_hxx_
3 #define vil3d_resample_simple_hxx_
4 //:
5 // \file
6 // \brief Resample a 3D image by a different factor in each dimension
7 // \author Kevin de Souza
8 
9 #include "vil3d_resample_simple.h"
10 
11 //: Resample a 3D image by a different factor in each dimension.
12 //  dst_image resized by factors dx, dy, dz.
13 //  dst_image(i, j, k, p) is sampled from src_image(i/dx, j/dy, k/dz, p).
14 //  No interpolation is performed.
15 template <class T >
vil3d_resample_simple(const vil3d_image_view<T> & src_image,vil3d_image_view<T> & dst_image,const double dx,const double dy,const double dz)16 void vil3d_resample_simple(const vil3d_image_view< T >& src_image,
17                            vil3d_image_view< T >& dst_image,
18                            const double dx,
19                            const double dy,
20                            const double dz)
21 {
22   // Assume planes are the same for both images
23   const unsigned np = src_image.nplanes();
24 
25   const unsigned sni = src_image.ni();
26   const unsigned snj = src_image.nj();
27   const unsigned snk = src_image.nk();
28 
29   const unsigned dni = static_cast<unsigned>(sni*dx);
30   const unsigned dnj = static_cast<unsigned>(snj*dy);
31   const unsigned dnk = static_cast<unsigned>(snk*dz);
32 
33   dst_image.set_size(dni, dnj, dnk, np);
34   const std::ptrdiff_t d_istep = dst_image.istep();
35   const std::ptrdiff_t d_jstep = dst_image.jstep();
36   const std::ptrdiff_t d_kstep = dst_image.kstep();
37   const std::ptrdiff_t d_pstep = dst_image.planestep();
38   T* d_plane = dst_image.origin_ptr();
39 
40   // Loop over all voxels in the destination image and
41   // sample from the corresponding point in the source image
42   for (unsigned p=0; p<np; ++p, d_plane+=d_pstep)
43   {
44     T* d_slice = d_plane;
45     for (unsigned k=0; k<dnk; ++k, d_slice+=d_kstep)
46     {
47       T* d_row = d_slice;
48       for (unsigned j=0; j<dnj; ++j, d_row+=d_jstep)
49       {
50         T* d_pix = d_row;
51         for (unsigned i=0; i<dni; ++i, d_pix+=d_istep)
52         {
53           *d_pix = src_image(static_cast<unsigned>(i/dx),
54                              static_cast<unsigned>(j/dy),
55                              static_cast<unsigned>(k/dz),
56                              p);
57         }
58       }
59     }
60   }
61 }
62 
63 
64 #define VIL3D_RESAMPLE_SIMPLE_INSTANTIATE( T ) \
65 template void vil3d_resample_simple(const vil3d_image_view< T >& src_image, \
66                                     vil3d_image_view< T >& dst_image, \
67                                     const double dx, \
68                                     const double dy, \
69                                     const double dz)
70 
71 
72 #endif // vil3d_resample_simple_hxx_
73