vil_border.h
Go to the documentation of this file.
1 // This is core/vil/vil_border.h
2 #ifndef vil_border_h_
3 #define vil_border_h_
4 //:
5 // \file
6 // \brief Border handling for images.
7 // \author Nicolas Burrus - Paris
8 
9 #include <limits>
10 #include <cassert>
11 // not used? #include <vcl_compiler.h>
12 
13 //: Vil border modes.
15 {
16  //: Border pixels are constant
18 
19  //: Border pixels take the value of the closest image point.
20  // This ensures object continuity.
21  // ex: im(im.ni()+2, -1) == im(im.ni()-1, 0)
23 
24  //: Border pixels take the value of the image point which is its symmetric w.r.t. the closest image edge.
25  // ex: im(im.ni()+3, 0) = im(im.ni()-4, 0)
27 
28  //: The image is seen as periodic, after the right (respectively bottom) edge comes the left (respectively top).
29  // ex: im(im.ni()+2,0) == im(2,0)
30  // ex: im(im.ni()+3, im.nj()+2) == im(3,2)
32 };
33 
34 //: Border class. Makes pixel access outside image range transparent and configurable.
35 // Note that a border is not assigned to a specific image, but only holds border properties.
36 template <class imT>
38 {
39  public:
40  typedef typename imT::pixel_type pixel_type;
41 
42  public:
43  //: Default constructor, creates a constant border.
46  , constant_value_()
47  {}
48 
49  //: Set the border kind.
50  void set_kind (vil_border_mode brdr_kind) { border_kind_ = brdr_kind; }
51  //: Get the current border kind.
52  vil_border_mode kind() const { return border_kind_; }
53 
54  //: True if border values are constant.
55  inline bool is_constant() const
56  { return border_kind_ == vil_border_constant; }
57 
58  //: If border kind is vil_border_constant, returns the border value.
59  inline const pixel_type& constant_value() const { return constant_value_; }
60 
61  //: Set the border value if the border kind is vil_border_constant.
62  void set_constant_value (const pixel_type& val) { constant_value_ = val; }
63 
64  //: Return read-only reference to pixel at (i,j,p) on the given image.
65  // If the pixel falls out of the image range, the border value is returned.
66  inline const pixel_type&
67  operator()(const imT& im, int i, int j, int p = 0) const
68  {
69  if (im.in_range(i,j,p))
70  return im(i,j,p);
71 
72  return in_border(im, i, j, p);
73  }
74 
75  protected:
76  inline const pixel_type&
77  in_border(const imT& im, int i, int j, int p = 0) const
78  {
79  switch (border_kind_)
80  {
82  return constant_value_;
84  if (i < 0) i = 0; else if (i >= (int)im.ni()) i = im.ni()-1;
85  if (j < 0) j = 0; else if (j >= (int)im.nj()) j = im.nj()-1;
86  if (p < 0) p = 0; else if (p >= (int)im.nplanes()) p = im.nplanes()-1;
87  return im(i,j,p);
88  case vil_border_reflect:
89  if (i < 0) i = -i-1; else if (i >= (int)im.ni()) i = 2*im.ni()-i-1;
90  if (j < 0) j = -j-1; else if (j >= (int)im.nj()) j = 2*im.nj()-j-1;
91  if (p < 0) p = -p-1; else if (p >= (int)im.nplanes()) p = 2*im.nplanes()-p-1;
92  return im(i,j,p);
94  if (i < 0) i = im.ni()-((-i)%im.ni());
95  else i = i%im.ni();
96 
97  if (j < 0) j = im.nj()-((-j)%im.nj());
98  else j = j%im.nj();
99 
100  if (p < 0) p = im.nplanes()-((-p)%im.nplanes());
101  else p = p%im.nplanes();
102 
103  return im(i,j,p);
104  default:
105  assert(false);
106  return constant_value_; // To avoid warnings
107  };
108  }
109 
110  private:
113 };
114 
115 //: Provides a pixel accessor which is syntax-compatible with vil_image_view.
116 // It acts like a proxy to the underlying image,
117 // transparently providing border pixel values if required.
118 template <class imT>
120 {
121  public:
122  typedef typename imT::pixel_type pixel_type;
123 
124  public:
125  //: Constructor.
126  vil_border_accessor(const imT& img, const vil_border<imT>& brdr)
127  : im(img), border(brdr)
128  {}
129 
130  //: Returns a const reference on the pixel (i,j,p).
131  // If the pixel falls out of the image range, a border value is returned.
132  inline const pixel_type&
133  operator()(int i, int j, int p = 0) const
134  { return border(im, i, j, p); }
135 
136  private:
137  const imT& im;
139 };
140 
141 //: Instantiates a border accessor, provided for convenience.
142 template <class imT>
144 vil_border_create_accessor(const imT& im, const vil_border<imT>& border)
145 {
146  return vil_border_accessor<imT>(im, border);
147 }
148 
149 //: Instantiate a constant border whose type is derived from imT.
150 template <class imT>
151 inline vil_border<imT>
152 vil_border_create_constant(const imT&, typename imT::pixel_type constant_val = 0)
153 {
154  vil_border<imT> border;
156  border.set_constant_value(constant_val);
157  return border;
158 }
159 
160 //: Instantiate a geodesic border whose type is derived from imT.
161 template <class imT>
162 inline vil_border<imT>
164 {
165  vil_border<imT> border;
167  return border;
168 }
169 
170 //: Instantiate a reflect border whose type is derived from imT.
171 template <class imT>
172 inline vil_border<imT>
174 {
175  vil_border<imT> border;
177  return border;
178 }
179 
180 //: Instantiate a reflect border whose type is derived from imT.
181 template <class imT>
182 inline vil_border<imT>
184 {
185  vil_border<imT> border;
187  return border;
188 }
189 
190 #endif // vil_border_h_
Border pixels take the value of the closest image point.
Definition: vil_border.h:22
const imT & im
Definition: vil_border.h:137
const pixel_type & constant_value() const
If border kind is vil_border_constant, returns the border value.
Definition: vil_border.h:59
const pixel_type & operator()(int i, int j, int p=0) const
Returns a const reference on the pixel (i,j,p).
Definition: vil_border.h:133
Border pixels take the value of the image point which is its symmetric w.r.t. the closest image edge.
Definition: vil_border.h:26
Border class. Makes pixel access outside image range transparent and configurable.
Definition: vil_border.h:37
imT::pixel_type pixel_type
Definition: vil_border.h:122
The image is seen as periodic, after the right (respectively bottom) edge comes the left (respectivel...
Definition: vil_border.h:31
vil_border< imT > vil_border_create_periodic(const imT &)
Instantiate a reflect border whose type is derived from imT.
Definition: vil_border.h:183
imT::pixel_type pixel_type
Definition: vil_border.h:40
vil_border_mode
Vil border modes.
Definition: vil_border.h:14
Provides a pixel accessor which is syntax-compatible with vil_image_view.
Definition: vil_border.h:119
vil_border< imT > border
Definition: vil_border.h:138
Border pixels are constant.
Definition: vil_border.h:17
bool is_constant() const
True if border values are constant.
Definition: vil_border.h:55
vil_border< imT > vil_border_create_geodesic(const imT &)
Instantiate a geodesic border whose type is derived from imT.
Definition: vil_border.h:163
vil_border()
Default constructor, creates a constant border.
Definition: vil_border.h:44
vil_border_mode kind() const
Get the current border kind.
Definition: vil_border.h:52
vil_border< imT > vil_border_create_reflect(const imT &)
Instantiate a reflect border whose type is derived from imT.
Definition: vil_border.h:173
vil_border_accessor< imT > vil_border_create_accessor(const imT &im, const vil_border< imT > &border)
Instantiates a border accessor, provided for convenience.
Definition: vil_border.h:144
const pixel_type & operator()(const imT &im, int i, int j, int p=0) const
Return read-only reference to pixel at (i,j,p) on the given image.
Definition: vil_border.h:67
vil_border_mode border_kind_
Definition: vil_border.h:111
vil_border< imT > vil_border_create_constant(const imT &, typename imT::pixel_type constant_val=0)
Instantiate a constant border whose type is derived from imT.
Definition: vil_border.h:152
vil_border_accessor(const imT &img, const vil_border< imT > &brdr)
Constructor.
Definition: vil_border.h:126
pixel_type constant_value_
Definition: vil_border.h:112
void set_kind(vil_border_mode brdr_kind)
Set the border kind.
Definition: vil_border.h:50
void set_constant_value(const pixel_type &val)
Set the border value if the border kind is vil_border_constant.
Definition: vil_border.h:62
const pixel_type & in_border(const imT &im, int i, int j, int p=0) const
Definition: vil_border.h:77