contrib/mul/vil3d/vil3d_scan_image.h
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_scan_image.h
00002 #ifndef vil3d_scan_image_h_
00003 #define vil3d_scan_image_h_
00004 //:
00005 // \file
00006 // \brief Read all voxels in an image (or pair) and feed into a functor
00007 // \author Tim Cootes
00008 
00009 #include <vcl_cassert.h>
00010 #include <vil3d/vil3d_image_view.h>
00011 
00012 //: Call f(v) for every voxel in the image
00013 template <class T, class Fn >
00014 inline void vil3d_scan_image(const vil3d_image_view<T >& im, Fn& f)
00015 {
00016   const T* plane = im.origin_ptr();
00017 
00018   unsigned ni = im.ni();
00019   unsigned nj = im.nj();
00020   unsigned nk = im.nk();
00021   unsigned np = im.nplanes();
00022   vcl_ptrdiff_t istep = im.istep(), jstep=im.jstep(), kstep=im.kstep();
00023 
00024   for (unsigned int p=0;p<np;++p, plane += im.planestep())
00025   {
00026     const T* slice = plane;
00027     for (unsigned int k=0;k<nk;++k, slice += kstep)
00028     {
00029       const T* row = slice;
00030       const T* row_end = row + nj*jstep;
00031       for (;row!=row_end;row += jstep)
00032       {
00033         const T* pixel = row;
00034         const T* pixel_end = row + ni*istep;
00035         for (;pixel!=pixel_end;pixel+=istep) f(*pixel);
00036       }
00037     }
00038   }
00039 }
00040 
00041 //: Call f(im1(i,j,k,p),im2(i,j,k,p)) for every voxel in the images
00042 template <class T1, class T2, class Fn >
00043 inline void vil3d_scan_image(const vil3d_image_view<T1 >& im1, 
00044                              const vil3d_image_view<T2 >& im2, Fn& f)
00045 {
00046   unsigned ni = im1.ni();
00047   unsigned nj = im1.nj();
00048   unsigned nk = im1.nk();
00049   unsigned np = im1.nplanes();
00050   assert(im1.ni()==ni && im2.nj()==nj && im2.nk()==nk && im2.nplanes()==np);
00051 
00052   const T1* plane1 = im1.origin_ptr();
00053   const T2* plane2 = im2.origin_ptr();
00054 
00055   vcl_ptrdiff_t istep1 = im1.istep(), jstep1=im1.jstep(), kstep1=im1.kstep();
00056   vcl_ptrdiff_t pstep1=im1.planestep();
00057   vcl_ptrdiff_t istep2 = im2.istep(), jstep2=im2.jstep(), kstep2=im2.kstep();
00058   vcl_ptrdiff_t pstep2=im2.planestep();
00059 
00060   for (unsigned int p=0;p<np;++p, plane1 += pstep1,plane2 += pstep2)
00061   {
00062     const T1* slice1 = plane1;
00063     const T2* slice2 = plane2;
00064     for (unsigned int k=0;k<nk;++k, slice1 += kstep1, slice2 += kstep2)
00065     {
00066       const T1* row1 = slice1;
00067       const T1* row1_end = row1 + nj*jstep1;
00068       const T2* row2 = slice2;
00069       for (;row1!=row1_end;row1 += jstep1,row2 += jstep2)
00070       {
00071         const T1* pixel1 = row1;
00072         const T1* pixel1_end = row1 + ni*istep1;
00073         const T2* pixel2 = row2;
00074         for (;pixel1!=pixel1_end;pixel1+=istep1,pixel2+=istep2)
00075           f(*pixel1,*pixel2);
00076       }
00077     }
00078   }
00079 
00080 }
00081 
00082 #endif