libcvd
image_ref.h
1 //-*- c++ -*-
3 // //
4 // CVD::image.h //
5 // //
6 // Definitions for of template classes CVD::ImageRef and CVD::Image //
7 // //
8 // derived from IPRS_* developed by Tom Drummond //
9 // //
11 
12 #ifndef __CVD_IMAGE_REF_H__
13 #define __CVD_IMAGE_REF_H__
14 
15 #include <cassert>
16 #include <cctype>
17 #include <cvd/exceptions.h>
18 #include <iostream>
19 
20 namespace CVD
21 {
22 
24 
26 // CVD::ImageRef //
29 class ImageRef
30 {
31  public:
32  //Construction
34  constexpr ImageRef();
35  constexpr ImageRef(const ImageRef&) = default;
36 
40  constexpr ImageRef(int xp, int yp);
43  inline ImageRef(std::istream& is);
44 
45  //#if __cplusplus >= 201103
46  //
47  //ImageRef(const std::initializer_list<int>& init)
48  // :
49  // #endif
50 
51  //Iteration
52 
57  inline bool next(const ImageRef& max);
62  inline bool prev(const ImageRef& max);
69  inline bool next(const ImageRef& min, const ImageRef& max);
76  inline bool prev(const ImageRef& min, const ImageRef& max);
77 
79  inline void home();
83  inline void end(const ImageRef& size);
84 
85  //Operators
86 
89  constexpr ImageRef& operator=(const ImageRef& ref);
92  constexpr bool operator==(const ImageRef& ref) const;
95  constexpr bool operator!=(const ImageRef& ref) const;
97  constexpr ImageRef operator-() const;
100  constexpr ImageRef& operator*=(const double scale);
103  constexpr ImageRef& operator/=(const double scale);
106  constexpr ImageRef& operator+=(const ImageRef rhs);
109  constexpr ImageRef& operator-=(const ImageRef rhs);
112  constexpr ImageRef operator*(const double scale) const;
115  constexpr ImageRef operator/(const double scale) const;
118  constexpr ImageRef operator+(const ImageRef rhs) const;
121  constexpr ImageRef operator-(const ImageRef rhs) const;
124  constexpr ImageRef& operator<<=(int i);
127  constexpr ImageRef& operator>>=(int i);
130  constexpr ImageRef operator>>(int i) const;
133  constexpr ImageRef operator<<(int i) const;
139  constexpr bool operator<(const ImageRef& other) const;
143  constexpr bool operator>(const ImageRef& other) const;
146  constexpr unsigned int mag_squared() const;
147 
149  constexpr int area() const;
150 
152  constexpr ImageRef dot_times(const ImageRef& ref) const;
153 
155  constexpr int& operator[](int i);
156 
158  constexpr int operator[](int i) const;
159 
160  //Why do these exist?
162  constexpr ImageRef shiftl(int i) const;
164  constexpr ImageRef shiftr(int i) const;
166  constexpr ImageRef transpose() const
167  {
168  return ImageRef(y, x);
169  }
170 
171  // and now the data members (which are public!)
172  int x;
173  int y;
174 };
175 
180 constexpr inline ImageRef operator*(const int scale, const ImageRef& ref);
182 namespace Exceptions
183 {
186  {
187  BadSubscript() {};
188  };
189 }
190 
191 #include <cvd/internal/image_ref_implementation.hh>
192 
193 // Streams stuff for ImageRef class //
194 
199 inline std::ostream& operator<<(std::ostream& os, const ImageRef& ref)
200 {
201  return os << "[" << ref.x << " " << ref.y << "]";
202 }
203 
206 inline std::istream& operator>>(std::istream& is, ImageRef& ref)
207 {
208  //Full parsing for ImageRefs, to allow it to accept the
209  //output produced above, as well as the older (x,y) format
210  is >> std::ws;
211 
212  int c = is.get();
214  if(c == -1)
215  return is;
216 
217  if(c == '(')
218  {
219  is >> std::ws >> ref.x >> std::ws;
220 
221  if(is.get() != ',')
222  goto bad;
223 
224  is >> std::ws >> ref.y >> std::ws;
225 
226  if(is.get() != ')')
227  goto bad;
228  }
229  else if(c == '[')
230  {
231  is >> std::ws >> ref.x >> std::ws >> ref.y >> std::ws;
232  if(is.get() != ']')
233  goto bad;
234  }
235  else if(isdigit(c))
236  {
237  is.unget();
238  is >> ref.x >> ref.y;
239  }
240  else
241  goto bad;
242 
243  return is;
244 
245 bad:
246  is.setstate(std::ios_base::badbit);
247 
248  return is;
249 }
250 
253 const ImageRef ImageRef_zero(0, 0);
254 
255 } //namespace CVD
256 
257 #endif
constexpr ImageRef operator/(const double scale) const
Divide both x and y co-ordinates by a scalar.
Definition: image_ref.h:112
constexpr ImageRef shiftl(int i) const
Definition: image_ref.h:144
constexpr ImageRef shiftr(int i) const
Definition: image_ref.h:152
constexpr int area() const
Area (product of x and y; signed)
Definition: image_ref.h:213
All classes and functions are within the CVD namespace.
Definition: argb.h:6
constexpr ImageRef & operator*=(const double scale)
Multiply both x and y co-ordinates by a scalar.
Definition: image_ref.h:78
constexpr ImageRef operator+(const ImageRef rhs) const
Add an offset to the co-ordinate.
Definition: image_ref.h:118
constexpr ImageRef()
Construct an ImageRef initialised at (0,0)
Definition: image_ref.h:2
std::istream & operator>>(std::istream &is, ImageRef &ref)
Read an ImageRef from a stream.
Definition: image_ref.h:206
constexpr bool operator>(const ImageRef &other) const
An ImageRef is greater than another ImageRef if it is earlier in the standard horizontal scan-line or...
Definition: image_ref.h:181
constexpr ImageRef & operator<<=(int i)
Bitwise left-shift operator.
Definition: image_ref.h:130
std::ostream & operator<<(std::ostream &os, const ImageRef &ref)
Write an ImageRef to a stream in the format "[x y]".
Definition: image_ref.h:199
constexpr unsigned int mag_squared() const
Magnitude-squared (x*x + y*y)
Definition: image_ref.h:207
bool next(const ImageRef &max)
Step to the next co-ordinate in the image (in horizontal scanline order).
Definition: image_ref.h:24
constexpr ImageRef operator>>(int i) const
Bitwise right-shift operator.
Definition: image_ref.h:165
constexpr ImageRef operator*(const double scale) const
Multiply both x and y co-ordinates by a scalar.
Definition: image_ref.h:106
bool prev(const ImageRef &max)
Step to the previous co-ordinate in the image (in horizontal scanline order).
Definition: image_ref.h:34
const ImageRef ImageRef_zero(0, 0)
A zero ImageRef.
constexpr ImageRef & operator>>=(int i)
Bitwise right-shift operator.
Definition: image_ref.h:137
constexpr ImageRef & operator=(const ImageRef &ref)
Assigment.
Definition: image_ref.h:55
constexpr ImageRef & operator+=(const ImageRef rhs)
Add an offset to the co-ordinate.
Definition: image_ref.h:92
int x
The x co-ordinate.
Definition: image_ref.h:172
Base class for all CVD exceptions.
Definition: exceptions.h:15
constexpr ImageRef operator-() const
Unary minus. Negates both x and y components.
Definition: image_ref.h:72
Definition: image_ref.h:29
constexpr bool operator<(const ImageRef &other) const
An ImageRef is less than another ImageRef if it is earlier in the standard horizontal scan-line order...
Definition: image_ref.h:176
constexpr bool operator!=(const ImageRef &ref) const
Logical not equals.
Definition: image_ref.h:67
void home()
Resets the ImageRef to (0,0)
Definition: image_ref.h:44
Exception if subscript for [] is not 0 or 1.
Definition: image_ref.h:185
constexpr bool operator==(const ImageRef &ref) const
Logical equals.
Definition: image_ref.h:62
int y
The y co-ordinate.
Definition: image_ref.h:173
constexpr int & operator[](int i)
Square bracket subscripts for easy loops. 0=x 1=y other=error.
Definition: image_ref.h:187
void end(const ImageRef &size)
Resets the ImageRef to the maximum co-ordinate in the image i.e.
Definition: image_ref.h:49
constexpr ImageRef & operator-=(const ImageRef rhs)
Subtract an offset from the co-ordinate.
Definition: image_ref.h:99
constexpr ImageRef operator<<(int i) const
Bitwise left-shift operator.
Definition: image_ref.h:160
constexpr ImageRef dot_times(const ImageRef &ref) const
The equivalent of doing .* in matlab.
Definition: image_ref.h:218
constexpr ImageRef & operator/=(const double scale)
Divide both x and y co-ordinates by a scalar.
Definition: image_ref.h:85