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