00001
00002 #ifndef vil_fill_h_
00003 #define vil_fill_h_
00004
00005
00006
00007
00008
00009 #include <vcl_cassert.h>
00010 #include <vil/vil_image_view.h>
00011 #include <vcl_algorithm.h>
00012
00013
00014
00015
00016 template<class T>
00017 void vil_fill(vil_image_view<T>& view, T value)
00018 {
00019
00020 if (view.is_contiguous())
00021 vcl_fill(view.begin(), view.end(), value);
00022
00023 unsigned ni = view.ni();
00024 vcl_ptrdiff_t istep=view.istep();
00025 unsigned nj = view.nj();
00026 vcl_ptrdiff_t jstep=view.jstep();
00027 unsigned np = view.nplanes();
00028 vcl_ptrdiff_t pstep = view.planestep();
00029
00030 T* plane = view.top_left_ptr();
00031 for (unsigned p=0;p<np;++p,plane += pstep)
00032 {
00033 T* row = plane;
00034 for (unsigned j=0;j<nj;++j,row += jstep)
00035 {
00036 T* pixel = row;
00037 for (unsigned i=0;i<ni;++i,pixel+=istep) *pixel=value;
00038 }
00039 }
00040 }
00041
00042
00043
00044 template<class T>
00045 void vil_fill_line(T* data, unsigned n, vcl_ptrdiff_t step, T value)
00046 {
00047 T* end_data = data + n*step;
00048 while (data!=end_data) { *data=value; data+=step; }
00049 }
00050
00051
00052
00053
00054
00055 template<class T>
00056 void vil_fill_line(vil_image_view<T> &im,
00057 int ai, int aj, int bi, int bj,
00058 T value)
00059 {
00060 if (ai == bi && aj==bj)
00061 {
00062 if (im.in_range(ai, aj)) im(ai,aj) = value;
00063 return;
00064 }
00065
00066 int d, x, y, xinc, yinc, incr1, incr2;
00067
00068 int dx = bi-ai;
00069 int dy = bj-aj;
00070 if (dy<0)
00071 {
00072 dy=-dy;
00073 yinc=-1;
00074 }
00075 else
00076 yinc=1;
00077
00078 if (dx<0)
00079 {
00080 dx=-dx;
00081 xinc=-1;
00082 }
00083 else
00084 xinc=1;
00085
00086 if (im.in_range(ai, aj)) im(ai, aj)=value;
00087 x=ai;
00088 y=aj;
00089 if (dy<=dx)
00090 {
00091 d=(dy<<1)-dx;
00092 incr1=dy<<1;
00093 incr2=-((dx-dy)<<1);
00094 while (x!=bi)
00095 {
00096 x+=xinc;
00097 if (d<0)
00098 d+=incr1;
00099 else
00100 {
00101 y+=yinc;
00102 d+=incr2;
00103 }
00104 if (im.in_range(x, y)) im(x, y)=value;
00105 }
00106 }
00107 else
00108 {
00109 d=(dx<<1)-dy;
00110 incr1=dx<<1;
00111 incr2=-((dy-dx)<<1);
00112 while (y!=bj)
00113 {
00114 y+=yinc;
00115 if (d<0)
00116 d+=incr1;
00117 else
00118 {
00119 x+=xinc;
00120 d+=incr2;
00121 }
00122 if (im.in_range(x, y)) im(x, y)=value;
00123 }
00124 }
00125 }
00126
00127
00128
00129
00130 template<class T>
00131 void vil_fill_row(vil_image_view<T>& view, unsigned j, T value)
00132 {
00133 unsigned ni = view.ni(); vcl_ptrdiff_t istep=view.istep();
00134 assert(j<view.nj()); vcl_ptrdiff_t jstep=view.jstep();
00135 unsigned np = view.nplanes(); vcl_ptrdiff_t pstep=view.planestep();
00136
00137 T* row = view.top_left_ptr() + j*jstep;
00138 for (unsigned p=0;p<np;++p,row += pstep)
00139 vil_fill_line(row,ni,istep,value);
00140 }
00141
00142
00143
00144
00145 template<class T>
00146 void vil_fill_col(vil_image_view<T>& view, unsigned i, T value)
00147 {
00148 assert(i<view.ni()); vcl_ptrdiff_t istep=view.istep();
00149 unsigned nj = view.nj(); vcl_ptrdiff_t jstep=view.jstep();
00150 unsigned np = view.nplanes(); vcl_ptrdiff_t pstep=view.planestep();
00151
00152 T* col_top = view.top_left_ptr() + i*istep;
00153 for (unsigned p=0;p<np;++p,col_top += pstep)
00154 vil_fill_line(col_top,nj,jstep,value);
00155 }
00156
00157
00158
00159
00160 template<class srcT>
00161 inline
00162 void vil_fill_mask(vil_image_view<srcT>& image,
00163 const vil_image_view<bool>& mask,
00164 srcT value, bool b=true)
00165 {
00166 unsigned ni = image.ni(), nj = image.nj(), np = image.nplanes();
00167 assert(ni==mask.ni() && nj==mask.nj());
00168 assert(mask.nplanes()==1 || mask.nplanes() ==np);
00169
00170 vcl_ptrdiff_t istepA=image.istep(),jstepA=image.jstep(),pstepA = image.planestep();
00171 vcl_ptrdiff_t istepB=mask.istep(),jstepB=mask.jstep(),pstepB = mask.planestep();
00172
00173
00174
00175 if (mask.nplanes()==1) pstepB=0;
00176
00177 srcT* planeA = image.top_left_ptr();
00178 const bool* planeB = mask.top_left_ptr();
00179 for (unsigned p=0;p<np;++p,planeA += pstepA,planeB += pstepB)
00180 {
00181 srcT* rowA = planeA;
00182 const bool* rowB = planeB;
00183 for (unsigned j=0;j<nj;++j,rowA += jstepA,rowB += jstepB)
00184 {
00185 srcT* pixelA = rowA;
00186 const bool* pixelB = rowB;
00187 for (unsigned i=0;i<ni;++i,pixelA+=istepA,pixelB+=istepB)
00188 if (*pixelB==b) *pixelA=value;
00189 }
00190 }
00191 }
00192
00193
00194
00195 template<class T>
00196 inline
00197 void vil_fill_disk(vil_image_view<T>& image, double ci, double cj, double r, T value)
00198 {
00199 unsigned ilo = vcl_max(0,int(ci-r));
00200 unsigned ihi = vcl_max(0,vcl_min(int(image.ni()-1),int(ci+r+1.0)));
00201 unsigned jlo = vcl_max(0,int(cj-r));
00202 unsigned jhi = vcl_max(0,vcl_min(int(image.nj()-1),int(cj+r+1.0)));
00203
00204 double r2 = r*r;
00205 for (unsigned j=jlo;j<=jhi;++j)
00206 {
00207 double t2 = r2 - (j-cj)*(j-cj);
00208 for (unsigned i=ilo;i<=ihi;++i)
00209 {
00210 if ((i-ci)*(i-ci)<t2)
00211 {
00212 for (unsigned k=0;k<image.nplanes();++k) image(i,j,k)=value;
00213 }
00214 }
00215 }
00216 }
00217
00218 #endif // vil_fill_h_