core/vil/vil_clamp.h
Go to the documentation of this file.
00001 // This is core/vil/vil_clamp.h
00002 #ifndef vil_clamp_h_
00003 #define vil_clamp_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Ian Scott.
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   06 May 2004 Jocelyn Marchadier - added vil_clamp_below
00014 // \endverbatim
00015 
00016 #include <vil/vil_image_resource.h>
00017 #include <vil/vil_image_view.h>
00018 #include <vcl_cassert.h>
00019 
00020 //: Clamp an image view between two values.
00021 // \relatesalso vil_image_view
00022 template <class T>
00023 inline void vil_clamp(const vil_image_view<T >&src, vil_image_view<T >&dest, T lo, T hi)
00024 {
00025   assert (hi >= lo);
00026   assert (src.nplanes() == dest.nplanes() &&
00027           src.nj() == dest.nj() &&
00028           src.ni() == dest.ni());
00029   for (unsigned p = 0; p < src.nplanes(); ++p)
00030     for (unsigned j = 0; j < src.nj(); ++j)
00031       for (unsigned i = 0; i < src.ni(); ++i)
00032       {
00033         const T v = src(i,j,p);
00034         dest(i,j,p) = v<lo?lo:(v>hi?hi:v);
00035       }
00036 }
00037 
00038 
00039 //: Clamp an image resource between two values.
00040 // \relatesalso vil_image_resource
00041 vil_image_resource_sptr vil_clamp(const vil_image_resource_sptr &src, double low, double hi);
00042 
00043 
00044 //: A generic_image adaptor that behaves like a clamped version of its input
00045 // For implementation use only - use vil_clamp() to create one.
00046 class vil_clamp_image_resource : public vil_image_resource
00047 {
00048   vil_clamp_image_resource(vil_image_resource_sptr const&, double low, double high);
00049   friend vil_image_resource_sptr vil_clamp(const vil_image_resource_sptr &src, double low, double hi);
00050  public:
00051 
00052   virtual unsigned nplanes() const { return src_->nplanes(); }
00053   virtual unsigned ni() const { return src_->ni(); }
00054   virtual unsigned nj() const { return src_->nj(); }
00055 
00056   virtual enum vil_pixel_format pixel_format() const { return src_->pixel_format(); }
00057 
00058 
00059   virtual vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
00060                                                  unsigned j0, unsigned nj) const;
00061 
00062   virtual vil_image_view_base_sptr get_view(unsigned i0, unsigned ni,
00063                                             unsigned j0, unsigned nj) const {
00064     return get_copy_view(i0, ni, j0, nj); }
00065 
00066 
00067   //: Put the data in this view back into the image source.
00068   virtual bool put_view(const vil_image_view_base& /*im*/, unsigned /*i0*/, unsigned /*j0*/) { return false; }
00069 
00070   //: Extra property information
00071   virtual bool get_property(char const* tag, void* property_value = 0) const;
00072 
00073  protected:
00074   //: Reference to underlying image source
00075   vil_image_resource_sptr src_;
00076   //: Lower clamp value
00077   double lo_;
00078   //: Upper clamp value
00079   double hi_;
00080 };
00081 
00082 //: Clamp an image view above a given value t, setting it to v if below or on t
00083 // \relatesalso vil_image_view
00084 template <class T>
00085 inline void vil_clamp_below(vil_image_view<T>& src, T t, T v)
00086 {
00087    vcl_ptrdiff_t istepA=src.istep(), jstepA=src.jstep(), pstepA=src.planestep();
00088    T* planeA = src.top_left_ptr();
00089    for (unsigned int p=0; p<src.nplanes(); ++p,planeA+=pstepA)
00090    {
00091      T* rowA = planeA;
00092      for (unsigned int j=0; j<src.nj(); ++j,rowA+=jstepA)
00093      {
00094        T* pixelA = rowA;
00095        for (unsigned int i=0; i<src.ni(); ++i,pixelA+=istepA)
00096          if (*pixelA <= t)
00097            *pixelA = v;
00098      }
00099   }
00100 }
00101 
00102 //: Clamp an image view above a given value t, setting it to this t if below t
00103 // \relatesalso vil_image_view
00104 template <class T>
00105 inline void vil_clamp_below(vil_image_view<T>& src, T t)
00106 {
00107   vil_clamp_below(src, t, t);
00108 }
00109 
00110 #endif // vil_clamp_h_