core/vil/vil_sample_profile_bicub.txx
Go to the documentation of this file.
00001 // This is core/vil/vil_sample_profile_bicub.txx
00002 #ifndef vil_sample_profile_bicub_txx_
00003 #define vil_sample_profile_bicub_txx_
00004 //:
00005 // \file
00006 // \brief Bicubic profile sampling functions for 2D images
00007 //
00008 // The vil bicub source files were derived from the corresponding
00009 // vil bilin files, thus the vil bilin/bicub source files are very
00010 // similar.  If you modify something in this file, there is a
00011 // corresponding bilin file that would likely also benefit from
00012 // the same change.
00013 
00014 #include "vil_sample_profile_bicub.h"
00015 #include <vil/vil_bicub_interp.h>
00016 
00017 inline bool vil_profile_in_image(double x0, double y0,
00018                                  double x1, double y1,
00019                                  const vil_image_view_base& image)
00020 {
00021   return x0 >= 2
00022       && y0 >= 2
00023       && x1 >= 2
00024       && y1 >= 2
00025       && x0+3 <= image.ni()
00026       && y0+3 <= image.nj()
00027       && x1+3 <= image.ni()
00028       && y1+3 <= image.nj();
00029 }
00030 
00031 //: Sample along profile, using safe bicubic interpolation
00032 //  Profile points are along the line between p0 and p1 (in image co-ordinates).
00033 //  Vector v is resized to n*np elements, where np=image.n_planes().
00034 //  v[0]..v[np-1] are the values from point p
00035 //  Points outside image return zero.
00036 template <class imType, class vecType>
00037 void vil_sample_profile_bicub(vecType* v,
00038                               const vil_image_view<imType>& image,
00039                               double x0, double y0, double dx, double dy,
00040                               int n)
00041 {
00042   bool all_in_image = vil_profile_in_image(x0,y0,x0+(n-1)*dx,y0+(n-1)*dy,image);
00043 
00044   const unsigned ni = image.ni();
00045   const unsigned nj = image.nj();
00046   const unsigned np = image.nplanes();
00047   const vcl_ptrdiff_t istep = image.istep();
00048   const vcl_ptrdiff_t jstep = image.jstep();
00049   const vcl_ptrdiff_t pstep = image.planestep();
00050   double x=x0;
00051   double y=y0;
00052   const imType* plane0 = image.top_left_ptr();
00053 
00054   if (all_in_image)
00055   {
00056     if (np==1)
00057     {
00058       for (int k=0;k<n;++k,x+=dx,y+=dy)
00059       v[k] = vil_bicub_interp(x,y,plane0,ni,nj,istep,jstep);
00060     }
00061     else
00062     {
00063       for (int k=0;k<n;++k,x+=dx,y+=dy)
00064       {
00065         for (unsigned int p=0;p<np;++p,++v)
00066           *v = vil_bicub_interp(x,y,plane0+p*pstep,ni,nj,istep,jstep);
00067       }
00068     }
00069   }
00070   else
00071   {
00072     // Use safe interpolation
00073     if (np==1)
00074     {
00075       for (int k=0;k<n;++k,x+=dx,y+=dy)
00076       v[k] = vil_bicub_interp_safe(x,y,plane0,ni,nj,istep,jstep);
00077     }
00078     else
00079     {
00080       for (int k=0;k<n;++k,x+=dx,y+=dy)
00081       {
00082         for (unsigned int p=0;p<np;++p,++v)
00083           *v = vil_bicub_interp_safe(x,y,plane0+p*pstep,ni,nj,istep,jstep);
00084       }
00085     }
00086   }
00087 }
00088 
00089 #define VIL_SAMPLE_PROFILE_BICUB_INSTANTIATE( imType, vecType ) \
00090 template void vil_sample_profile_bicub(vecType* v, \
00091                                        const vil_image_view<imType >& image, \
00092                                        double x0, double y0, \
00093                                        double dx, double dy, \
00094                                        int n)
00095 
00096 #endif // vil_sample_profile_bicub_txx_