contrib/mul/vil3d/vil3d_clamp.h
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_clamp.h
00002 #ifndef vil3d_clamp_h_
00003 #define vil3d_clamp_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Ian Scott.
00010 
00011 
00012 #include <vil3d/vil3d_image_view.h>
00013 #include <vcl_cassert.h>
00014 
00015 //: Clamp an image view between two values.
00016 // \relatesalso vil3d_image_view
00017 template <class T>
00018 inline void vil3d_clamp(vil3d_image_view<T >&src, vil3d_image_view<T >&dest, T lo, T hi)
00019 {
00020   assert (hi >= lo);
00021   assert (src.nplanes() == dest.nplanes() &&
00022           src.nk() == dest.nk() &&
00023           src.nj() == dest.nj() &&
00024           src.ni() == dest.ni());
00025   for (unsigned p = 0; p < src.nplanes(); ++p)
00026     for (unsigned k = 0; k < src.nk(); ++k)
00027       for (unsigned j = 0; j < src.nj(); ++j)
00028         for (unsigned i = 0; i < src.ni(); ++i)
00029         {
00030           const T v = src(i,j,k,p);
00031           dest(i,j,k,p) = v<lo?lo:(v>hi?hi:v);
00032         }
00033 }
00034 
00035 //: Clamp an image view above a given value t, setting it to v if below or on t
00036 // \relatesalso vil3d_image_view
00037 template <class T>
00038 inline void vil3d_clamp_below(vil3d_image_view<T>& src, T t, T v)
00039 {
00040   vcl_ptrdiff_t istepA=src.istep(), jstepA=src.jstep(),
00041     kstepA=src.kstep(), pstepA=src.planestep();
00042   T* planeA = src.origin_ptr();
00043   for (unsigned int p=0; p<src.nplanes(); ++p,planeA+=pstepA)
00044   {
00045     T* sliceA = planeA;
00046     for (unsigned int k=0; k<src.nk(); ++k,sliceA+=kstepA)
00047     {
00048       T* rowA = sliceA;
00049       for (unsigned int j=0; j<src.nj(); ++j,rowA+=jstepA)
00050       {
00051         T* voxelA = rowA;
00052         for (unsigned int i=0; i<src.ni(); ++i,voxelA+=istepA)
00053           if (*voxelA <= t)
00054             *voxelA = v;
00055       }
00056     }
00057   }
00058 }
00059 
00060 //: Clamp an image view above a given value t, setting it to this t if below t
00061 // \relatesalso vil3d_image_view
00062 template <class T>
00063 inline void vil3d_clamp_below(vil3d_image_view<T>& src, T t)
00064 {
00065   vil3d_clamp_below(src, t, t);
00066 }
00067 
00068 //: Clamp an image view below a given value t, setting it to v if above or on t
00069 // \relatesalso vil3d_image_view
00070 template <class T>
00071 inline void vil3d_clamp_above(vil3d_image_view<T>& src, T t, T v)
00072 {
00073   vcl_ptrdiff_t istepA=src.istep(), jstepA=src.jstep(),
00074     kstepA=src.kstep(), pstepA=src.planestep();
00075   T* planeA = src.origin_ptr();
00076   for (unsigned int p=0; p<src.nplanes(); ++p,planeA+=pstepA)
00077   {
00078     T* sliceA = planeA;
00079     for (unsigned int k=0; k<src.nk(); ++k,sliceA+=kstepA)
00080     {
00081       T* rowA = sliceA;
00082       for (unsigned int j=0; j<src.nj(); ++j,rowA+=jstepA)
00083       {
00084         T* voxelA = rowA;
00085         for (unsigned int i=0; i<src.ni(); ++i,voxelA+=istepA)
00086           if (*voxelA >= t)
00087             *voxelA = v;
00088       }
00089     }
00090   }
00091 }
00092 
00093 //: Clamp an image view below a given value t, setting it to this t if above t
00094 // \relatesalso vil3d_image_view
00095 template <class T>
00096 inline void vil3d_clamp_above(vil3d_image_view<T>& src, T t)
00097 {
00098   vil3d_clamp_above(src, t, t);
00099 }
00100 
00101 
00102 #endif // vil3d_clamp_h_