contrib/mul/vil3d/vil3d_resample_simple.txx
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_resample_simple.txx
00002 #ifndef vil3d_resample_simple_txx_
00003 #define vil3d_resample_simple_txx_
00004 //:
00005 // \file
00006 // \brief Resample a 3D image by a different factor in each dimension
00007 // \author Kevin de Souza
00008 
00009 #include "vil3d_resample_simple.h"
00010 
00011 //: Resample a 3D image by a different factor in each dimension.
00012 //  dst_image resized by factors dx, dy, dz.
00013 //  dst_image(i, j, k, p) is sampled from src_image(i/dx, j/dy, k/dz, p).
00014 //  No interpolation is performed.
00015 template <class T >
00016 void vil3d_resample_simple(const vil3d_image_view< T >& src_image,
00017                            vil3d_image_view< T >& dst_image,
00018                            const double dx,
00019                            const double dy,
00020                            const double dz)
00021 {
00022   // Assume planes are the same for both images
00023   const unsigned np = src_image.nplanes();
00024 
00025   const unsigned sni = src_image.ni();
00026   const unsigned snj = src_image.nj();
00027   const unsigned snk = src_image.nk();
00028 
00029   const unsigned dni = static_cast<unsigned>(sni*dx);
00030   const unsigned dnj = static_cast<unsigned>(snj*dy);
00031   const unsigned dnk = static_cast<unsigned>(snk*dz);
00032 
00033   dst_image.set_size(dni, dnj, dnk, np);
00034   const vcl_ptrdiff_t d_istep = dst_image.istep();
00035   const vcl_ptrdiff_t d_jstep = dst_image.jstep();
00036   const vcl_ptrdiff_t d_kstep = dst_image.kstep();
00037   const vcl_ptrdiff_t d_pstep = dst_image.planestep();
00038   T* d_plane = dst_image.origin_ptr();
00039 
00040   // Loop over all voxels in the destination image and
00041   // sample from the corresponding point in the source image
00042   for (unsigned p=0; p<np; ++p, d_plane+=d_pstep)
00043   {
00044     T* d_slice = d_plane;
00045     for (unsigned k=0; k<dnk; ++k, d_slice+=d_kstep)
00046     {
00047       T* d_row = d_slice;
00048       for (unsigned j=0; j<dnj; ++j, d_row+=d_jstep)
00049       {
00050         T* d_pix = d_row;
00051         for (unsigned i=0; i<dni; ++i, d_pix+=d_istep)
00052         {
00053           *d_pix = src_image(static_cast<unsigned>(i/dx),
00054                              static_cast<unsigned>(j/dy),
00055                              static_cast<unsigned>(k/dz),
00056                              p);
00057         }
00058       }
00059     }
00060   }
00061 }
00062 
00063 
00064 #define VIL3D_RESAMPLE_SIMPLE_INSTANTIATE( T ) \
00065 template void vil3d_resample_simple(const vil3d_image_view< T >& src_image, \
00066                                     vil3d_image_view< T >& dst_image, \
00067                                     const double dx, \
00068                                     const double dy, \
00069                                     const double dz)
00070 
00071 
00072 #endif // vil3d_resample_simple_txx_