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 //: This function should not be the same in bicub and bilin
00018 inline bool vil_profile_bicub_in_image(double x0, double y0,
00019                                        double x1, double y1,
00020                                        const vil_image_view_base& image)
00021 {
00022   return x0 >= 2
00023       && y0 >= 2
00024       && x1 >= 2
00025       && y1 >= 2
00026       && x0+3 <= image.ni()
00027       && y0+3 <= image.nj()
00028       && x1+3 <= image.ni()
00029       && y1+3 <= image.nj();
00030 }
00031 
00032 //: Sample along profile, using safe bicubic interpolation
00033 //  Profile points are along the line between p0 and p1 (in image co-ordinates).
00034 //  Vector v is resized to n*np elements, where np=image.n_planes().
00035 //  v[0]..v[np-1] are the values from point p
00036 //  Points outside image return zero.
00037 template <class imType, class vecType>
00038 void vil_sample_profile_bicub(vecType* v,
00039                               const vil_image_view<imType>& image,
00040                               double x0, double y0, double dx, double dy,
00041                               int n)
00042 {
00043   bool all_in_image = vil_profile_bicub_in_image(x0,y0,x0+(n-1)*dx,y0+(n-1)*dy,image);
00044 
00045   const unsigned ni = image.ni();
00046   const unsigned nj = image.nj();
00047   const unsigned np = image.nplanes();
00048   const vcl_ptrdiff_t istep = image.istep();
00049   const vcl_ptrdiff_t jstep = image.jstep();
00050   const vcl_ptrdiff_t pstep = image.planestep();
00051   double x=x0;
00052   double y=y0;
00053   const imType* plane0 = image.top_left_ptr();
00054 
00055   if (all_in_image)
00056   {
00057     if (np==1)
00058     {
00059       for (int k=0;k<n;++k,x+=dx,y+=dy)
00060       v[k] = vil_bicub_interp(x,y,plane0,ni,nj,istep,jstep);
00061     }
00062     else
00063     {
00064       for (int k=0;k<n;++k,x+=dx,y+=dy)
00065       {
00066         for (unsigned int p=0;p<np;++p,++v)
00067           *v = vil_bicub_interp(x,y,plane0+p*pstep,ni,nj,istep,jstep);
00068       }
00069     }
00070   }
00071   else
00072   {
00073     // Use safe interpolation
00074     if (np==1)
00075     {
00076       for (int k=0;k<n;++k,x+=dx,y+=dy)
00077       v[k] = vil_bicub_interp_safe(x,y,plane0,ni,nj,istep,jstep);
00078     }
00079     else
00080     {
00081       for (int k=0;k<n;++k,x+=dx,y+=dy)
00082       {
00083         for (unsigned int p=0;p<np;++p,++v)
00084           *v = vil_bicub_interp_safe(x,y,plane0+p*pstep,ni,nj,istep,jstep);
00085       }
00086     }
00087   }
00088 }
00089 
00090 #define VIL_SAMPLE_PROFILE_BICUB_INSTANTIATE( imType, vecType ) \
00091 template void vil_sample_profile_bicub(vecType* v, \
00092                                        const vil_image_view<imType >& image, \
00093                                        double x0, double y0, \
00094                                        double dx, double dy, \
00095                                        int n)
00096 
00097 #endif // vil_sample_profile_bicub_txx_