contrib/tbl/vipl/vipl_add_random_noise.txx
Go to the documentation of this file.
00001 #ifndef vipl_add_random_noise_txx_
00002 #define vipl_add_random_noise_txx_
00003 
00004 #include "vipl_add_random_noise.h"
00005 #include <vnl/vnl_numeric_traits.h>
00006 
00007 template <class ImgIn,class ImgOut,class DataIn,class DataOut,class PixelItr>
00008 bool vipl_add_random_noise <ImgIn,ImgOut,DataIn,DataOut,PixelItr> :: section_applyop()
00009 {
00010   int startx = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::start(this->X_Axis());
00011   int starty = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::start(this->Y_Axis());
00012   int stopx = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::stop(this->X_Axis());
00013   int stopy = vipl_filter<ImgIn,ImgOut,DataIn,DataOut,2,PixelItr>::stop(this->Y_Axis());
00014   for (register int j = starty; j < stopy; ++j)
00015     for (register int i = startx; i < stopx; ++i)
00016     {
00017       double rnd = type_ == UNIFORM_NOISE ? vnl_sample_uniform(-maxdev_,maxdev_)
00018                  : type_ == GAUSSIAN_NOISE ? vnl_sample_normal(0,maxdev_)
00019                  : vnl_sample_uniform(-maxdev_,maxdev_); // TODO - implement exponential noise
00020       DataOut p = fgetpixel(this->in_data(),i,j,DataIn(0)), q = DataOut(rnd);
00021       if (!clipping_ && rnd+p < 0)
00022         setpixel(this->out_data(), i, j, (DataOut)0);
00023       else if (!clipping_ && rnd+p > vnl_numeric_traits<DataOut>::maxval)
00024         setpixel(this->out_data(), i, j, vnl_numeric_traits<DataOut>::maxval);
00025       else
00026         setpixel(this->out_data(), i, j, DataOut(p+q));
00027   }
00028   return true;
00029 }
00030 
00031 #endif // vipl_add_random_noise_txx_