vil_clamp.h
Go to the documentation of this file.
1 // This is core/vil/vil_clamp.h
2 #ifndef vil_clamp_h_
3 #define vil_clamp_h_
4 //:
5 // \file
6 // \author Ian Scott.
7 //
8 // \verbatim
9 // Modifications
10 // 06 May 2004 Jocelyn Marchadier - added vil_clamp_below
11 // \endverbatim
12 
13 #include <vil/vil_image_resource.h>
14 #include <vil/vil_image_view.h>
15 #include <cassert>
16 #ifdef _MSC_VER
17 # include <vcl_msvc_warnings.h>
18 #endif
19 
20 //: Clamp an image view between two values.
21 // \relatesalso vil_image_view
22 template <class T>
23 inline void vil_clamp(const vil_image_view<T >&src, vil_image_view<T >&dest, T lo, T hi)
24 {
25  assert (hi >= lo);
26  assert (src.nplanes() == dest.nplanes() &&
27  src.nj() == dest.nj() &&
28  src.ni() == dest.ni());
29  for (unsigned p = 0; p < src.nplanes(); ++p)
30  for (unsigned j = 0; j < src.nj(); ++j)
31  for (unsigned i = 0; i < src.ni(); ++i)
32  {
33  const T v = src(i,j,p);
34  dest(i,j,p) = v<lo?lo:(v>hi?hi:v);
35  }
36 }
37 
38 
39 //: Clamp an image resource between two values.
40 // \relatesalso vil_image_resource
41 vil_image_resource_sptr vil_clamp(const vil_image_resource_sptr &src, double low, double hi);
42 
43 
44 //: A generic_image adaptor that behaves like a clamped version of its input
45 // For implementation use only - use vil_clamp() to create one.
47 {
48  vil_clamp_image_resource(vil_image_resource_sptr const&, double low, double high);
49  friend vil_image_resource_sptr vil_clamp(const vil_image_resource_sptr &src, double low, double hi);
50  public:
51 
52  unsigned nplanes() const override { return src_->nplanes(); }
53  unsigned ni() const override { return src_->ni(); }
54  unsigned nj() const override { return src_->nj(); }
55 
56  enum vil_pixel_format pixel_format() const override { return src_->pixel_format(); }
57 
58 
59  vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned ni,
60  unsigned j0, unsigned nj) const override;
61 
62  vil_image_view_base_sptr get_view(unsigned i0, unsigned ni,
63  unsigned j0, unsigned nj) const override {
64  return get_copy_view(i0, ni, j0, nj); }
65 
66 
67  //: Put the data in this view back into the image source.
68  bool put_view(const vil_image_view_base& /*im*/, unsigned /*i0*/, unsigned /*j0*/) override { return false; }
69 
70  //: Extra property information
71  bool get_property(char const* tag, void* property_value = nullptr) const override;
72 
73  protected:
74  //: Reference to underlying image source
76  //: Lower clamp value
77  double lo_;
78  //: Upper clamp value
79  double hi_;
80 };
81 
82 //: Clamp an image view above a given value t, setting it to v if below or on t
83 // \relatesalso vil_image_view
84 template <class T>
85 inline void vil_clamp_below(vil_image_view<T>& src, T t, T v)
86 {
87  std::ptrdiff_t istepA=src.istep(), jstepA=src.jstep(), pstepA=src.planestep();
88  T* planeA = src.top_left_ptr();
89  for (unsigned int p=0; p<src.nplanes(); ++p,planeA+=pstepA)
90  {
91  T* rowA = planeA;
92  for (unsigned int j=0; j<src.nj(); ++j,rowA+=jstepA)
93  {
94  T* pixelA = rowA;
95  for (unsigned int i=0; i<src.ni(); ++i,pixelA+=istepA)
96  if (*pixelA <= t)
97  *pixelA = v;
98  }
99  }
100 }
101 
102 //: Clamp an image view below a given value t, setting it to v if above or on t
103 // \relatesalso vil_image_view
104 template <class T>
105 inline void vil_clamp_above(vil_image_view<T>& src, T t, T v)
106 {
107  std::ptrdiff_t istepA=src.istep(), jstepA=src.jstep(), pstepA=src.planestep();
108  T* planeA = src.top_left_ptr();
109  for (unsigned int p=0; p<src.nplanes(); ++p,planeA+=pstepA)
110  {
111  T* rowA = planeA;
112  for (unsigned int j=0; j<src.nj(); ++j,rowA+=jstepA)
113  {
114  T* pixelA = rowA;
115  for (unsigned int i=0; i<src.ni(); ++i,pixelA+=istepA)
116  if (*pixelA >= t)
117  *pixelA = v;
118  }
119  }
120 }
121 
122 //: Clamp an image view above a given value t, setting it to this t if below t
123 // \relatesalso vil_image_view
124 template <class T>
125 inline void vil_clamp_below(vil_image_view<T>& src, T t)
126 {
127  vil_clamp_below(src, t, t);
128 }
129 
130 //: Clamp an image view below a given value t, setting it to this t if above t
131 // \relatesalso vil_image_view
132 template <class T>
133 inline void vil_clamp_above(vil_image_view<T>& src, T t)
134 {
135  vil_clamp_above(src, t, t);
136 }
137 
138 
139 #endif // vil_clamp_h_
An abstract base class of smart pointers to actual image data in memory.
vil_pixel_format
Describes the type of the concrete data.
unsigned nplanes() const override
Dimensions: Planes x ni x nj.
Definition: vil_clamp.h:52
void vil_clamp(const vil_image_view< T > &src, vil_image_view< T > &dest, T lo, T hi)
Clamp an image view between two values.
Definition: vil_clamp.h:23
double hi_
Upper clamp value.
Definition: vil_clamp.h:79
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
unsigned ni() const override
Dimensions: Planes x ni x nj.
Definition: vil_clamp.h:53
friend vil_image_resource_sptr vil_clamp(const vil_image_resource_sptr &src, double low, double hi)
Clamp an image resource between two values.
Definition: vil_clamp.cxx:22
void vil_clamp_above(vil_image_view< T > &src, T t, T v)
Clamp an image view below a given value t, setting it to v if above or on t.
Definition: vil_clamp.h:105
A generic_image adaptor that behaves like a clamped version of its input.
Definition: vil_clamp.h:46
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
#define v
std::ptrdiff_t planestep() const
Add this to your pixel pointer to get pixel on next plane.
unsigned nj() const override
Dimensions: Planes x ni x nj.
Definition: vil_clamp.h:54
bool put_view(const vil_image_view_base &, unsigned, unsigned) override
Put the data in this view back into the image source.
Definition: vil_clamp.h:68
double lo_
Lower clamp value.
Definition: vil_clamp.h:77
Abstract representation of an image source or image destination.
vil_image_view_base_sptr get_view(unsigned i0, unsigned ni, unsigned j0, unsigned nj) const override
Create a read/write view of the data.
Definition: vil_clamp.h:62
vil_image_view_base_sptr get_copy_view() const
Create a read/write view of a copy of all the data.
A base class reference-counting view of some image data.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
vil_image_resource_sptr src_
Reference to underlying image source.
Definition: vil_clamp.h:75
unsigned nplanes() const
Number of planes.
void vil_clamp_below(vil_image_view< T > &src, T t, T v)
Clamp an image view above a given value t, setting it to v if below or on t.
Definition: vil_clamp.h:85
enum vil_pixel_format pixel_format() const override
Pixel Format.
Definition: vil_clamp.h:56
Representation of a generic image source or destination.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.
vil_clamp_image_resource(vil_image_resource_sptr const &, double low, double high)
Definition: vil_clamp.cxx:28
bool get_property(char const *tag, void *property_value=nullptr) const override
Extra property information.
Definition: vil_clamp.cxx:37