contrib/tbl/vipl/vipl_gradient_dir.txx
Go to the documentation of this file.
00001 #ifndef vipl_gradient_dir_txx_
00002 #define vipl_gradient_dir_txx_
00003 
00004 #include "vipl_gradient_dir.h"
00005 #include <vcl_cmath.h> // for atan2()
00006 
00007 template <class ImgIn,class ImgOut,class DataIn,class DataOut,class PixelItr>
00008 bool vipl_gradient_dir <ImgIn,ImgOut,DataIn,DataOut,PixelItr> :: section_applyop()
00009 {
00010   const ImgIn &in = this->in_data(0);
00011   ImgOut &out = *this->out_data_ptr();
00012 
00013   int startx = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::start(this->X_Axis());
00014   int starty = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::start(this->Y_Axis());
00015   int stopx  = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::stop(this->X_Axis());
00016   int stopy  = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::stop(this->Y_Axis());
00017   for (int j = starty; j < stopy; ++j)
00018     for (int i = startx; i < stopx; ++i) {
00019       register double dx = fgetpixel(in, i, j, DataIn(0)) - getpixel(in, i-1, j, DataIn(0));
00020       register double dy = fgetpixel(in, i, j, DataIn(0)) - getpixel(in, i, j-1, DataIn(0));
00021       if (dx==0 && dy==0) dx=1.0; // to avoid an atan2() domain error
00022       dx = (vcl_atan2( dy, dx ) + shift()) * scale();
00023       fsetpixel(out, i, j, DataOut(dx));
00024     }
00025   return true;
00026 }
00027 
00028 #endif // vipl_gradient_dir_txx_