core/vil/vil_sample_grid_bicub.txx
Go to the documentation of this file.
00001 // This is core/vil/vil_sample_grid_bicub.txx
00002 #ifndef vil_sample_grid_bicub_txx_
00003 #define vil_sample_grid_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_grid_bicub.h"
00015 #include <vil/vil_bicub_interp.h>
00016 
00017 inline bool vil_grid_corner_in_image(double x0, double y0,
00018                                      const vil_image_view_base& image)
00019 {
00020   return x0 >= 2
00021       && y0 >= 2
00022       && x0+3 <= image.ni()
00023       && y0+3 <= image.nj();
00024 }
00025 
00026 //: Sample along profile, using safe bicubic interpolation
00027 //  Profile points are along the line between p0 and p1 (in image co-ordinates).
00028 //  Vector v is resized to n*np elements, where np=image.n_planes().
00029 //  v[0]..v[np-1] are the values from point p
00030 //  Points outside image return zero.
00031 template <class imType, class vecType>
00032 void vil_sample_grid_bicub(vecType* v,
00033                            const vil_image_view<imType>& image,
00034                            double x0, double y0, double dx1, double dy1,
00035                            double dx2, double dy2, int n1, int n2)
00036 {
00037   bool all_in_image =    vil_grid_corner_in_image(x0,y0,image)
00038                       && vil_grid_corner_in_image(x0+(n1-1)*dx1,y0+(n1-1)*dy1,image)
00039                       && vil_grid_corner_in_image(x0+(n2-1)*dx2,y0+(n2-1)*dy2,image)
00040                       && vil_grid_corner_in_image(x0+(n1-1)*dx1+(n2-1)*dx2,
00041                                                   y0+(n1-1)*dy1+(n2-1)*dy2,image);
00042 
00043   const unsigned ni = image.ni();
00044   const unsigned nj = image.nj();
00045   const unsigned np = image.nplanes();
00046   const vcl_ptrdiff_t istep = image.istep();
00047   const vcl_ptrdiff_t jstep = image.jstep();
00048   const vcl_ptrdiff_t pstep = image.planestep();
00049   double x1=x0;
00050   double y1=y0;
00051   const imType* plane0 = image.top_left_ptr();
00052 
00053   if (all_in_image)
00054   {
00055     if (np==1)
00056     {
00057       for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
00058       {
00059         double x=x1, y=y1;  // Start of j-th row
00060         for (int j=0;j<n2;++j,x+=dx2,y+=dy2,++v)
00061           *v = (vecType) vil_bicub_interp(x,y,plane0,ni,nj,istep,jstep);
00062       }
00063     }
00064     else
00065     {
00066       for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
00067       {
00068         double x=x1, y=y1; // Start of j-th row
00069         for (int j=0;j<n2;++j,x+=dx2,y+=dy2)
00070         {
00071           for (unsigned p=0;p<np;++p,++v)
00072             *v = (vecType) vil_bicub_interp(x,y,plane0+p*pstep,ni,nj,istep,jstep);
00073         }
00074       }
00075     }
00076   }
00077   else
00078   {
00079     // Use safe interpolation
00080     if (np==1)
00081     {
00082       for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
00083       {
00084         double x=x1, y=y1;  // Start of j-th row
00085         for (int j=0;j<n2;++j,x+=dx2,y+=dy2,++v)
00086           *v = (vecType) vil_bicub_interp_safe(x,y,plane0,ni,nj,istep,jstep);
00087       }
00088     }
00089     else
00090     {
00091       for (int i=0;i<n1;++i,x1+=dx1,y1+=dy1)
00092       {
00093         double x=x1, y=y1; // Start of j-th row
00094         for (int j=0;j<n2;++j,x+=dx2,y+=dy2)
00095         {
00096           for (unsigned p=0;p<np;++p,++v)
00097             *v = (vecType) vil_bicub_interp_safe(x,y,plane0+p*pstep,ni,nj,istep,jstep);
00098         }
00099       }
00100     }
00101   }
00102 }
00103 
00104 #define VIL_SAMPLE_GRID_BICUB_INSTANTIATE( imType, vecType ) \
00105 template void vil_sample_grid_bicub(vecType* v, \
00106                                     const vil_image_view<imType >& image, \
00107                                     double x0, double y0, double dx1, double dy1, \
00108                                     double dx2, double dy2, int n1, int n2)
00109 
00110 #endif // vil_sample_grid_bicub_txx_