Debian Codegraph @

cvd/image_ref.h

raw symbols SCIP
1 //-*- c++ -*-
2 //////////////////////////////////////////////////////////////////////////
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 // //
10 //////////////////////////////////////////////////////////////////////////
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
23 /// @ingroup gImage
24
25 //////////////////////////////////////
26 // CVD::ImageRef //
27 //////////////////////////////////////
28 /// An (x,y) image co-ordinate
29 class ImageRef
30 {
31 public:
32 //Construction
33 /// Construct an ImageRef initialised at (0,0)
34 constexpr ImageRef();
35 constexpr ImageRef(const ImageRef&) = default;
36
37 /// Construct an ImageRef
38 /// @param xp The x co-ordinate
39 /// @param yp The y co-ordinate
40 constexpr ImageRef(int xp, int yp);
41 /// Construct an ImageRef from a stream
42 /// @param is The stream to read
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
53 /// Step to the next co-ordinate in the image (in horizontal scanline order).
54 /// Successive calls to this function will work along each row of the image.
55 /// If the next co-ordinate is off the end of the image, it returns false.
56 /// @param max The size of the image
57 inline bool next(const ImageRef& max);
58 /// Step to the previous co-ordinate in the image (in horizontal scanline order).
59 /// Successive calls to this function will work backwards along each row of the image.
60 /// If the next co-ordinate is off the start of the image, it returns false.
61 /// @param max The size of the image
62 inline bool prev(const ImageRef& max);
63 /// Step to the next co-ordinate in the image (in horizontal scanline order),
64 /// for pixels within a rectangular window
65 /// Successive calls to this function will work backwards along each row of the image.
66 /// If the next co-ordinate is off the start of the image, it returns false.
67 /// @param min The minimum (x,y) co-ordinates in the window
68 /// @param max The minimum (x,y) co-ordinates in the window
69 inline bool next(const ImageRef& min, const ImageRef& max);
70 /// Step to the previous co-ordinate in the image (in horizontal scanline order),
71 /// for pixels within a rectangule window
72 /// Successive calls to this function will work backwards along each row of the image.
73 /// If the next co-ordinate is off the start of the image, it returns false.
74 /// @param min The minimum (x,y) co-ordinates in the window
75 /// @param max The minimum (x,y) co-ordinates in the window
76 inline bool prev(const ImageRef& min, const ImageRef& max);
77
78 /// Resets the ImageRef to (0,0)
79 inline void home();
80 /// Resets the ImageRef to the maximum co-ordinate in the image
81 /// i.e. <code>(size.x - 1, size.y - 1)</code>
82 /// @param size The size of the image
83 inline void end(const ImageRef& size);
84
85 //Operators
86
87 /// Assigment
88 /// @param ref The co-ordinate to copy
89 constexpr ImageRef& operator=(const ImageRef& ref);
90 /// Logical equals
91 /// @param ref The co-ordinate to compare with
92 constexpr bool operator==(const ImageRef& ref) const;
93 /// Logical not equals
94 /// @param ref The co-ordinate to compare with
95 constexpr bool operator!=(const ImageRef& ref) const;
96 /// Unary minus. Negates both x and y components.
97 constexpr ImageRef operator-() const;
98 /// Multiply both x and y co-ordinates by a scalar
99 /// @param scale The multiplier
100 constexpr ImageRef& operator*=(const double scale);
101 /// Divide both x and y co-ordinates by a scalar
102 /// @param scale The factor
103 constexpr ImageRef& operator/=(const double scale);
104 /// Add an offset to the co-ordinate
105 /// @param rhs The offset
106 constexpr ImageRef& operator+=(const ImageRef rhs);
107 /// Subtract an offset from the co-ordinate
108 /// @param rhs The offset
109 constexpr ImageRef& operator-=(const ImageRef rhs);
110 /// Multiply both x and y co-ordinates by a scalar
111 /// @param scale The multiplier
112 constexpr ImageRef operator*(const double scale) const;
113 /// Divide both x and y co-ordinates by a scalar
114 /// @param scale The factor
115 constexpr ImageRef operator/(const double scale) const;
116 /// Add an offset to the co-ordinate
117 /// @param rhs The offset
118 constexpr ImageRef operator+(const ImageRef rhs) const;
119 /// Subtract an offset from the co-ordinate
120 /// @param rhs The offset
121 constexpr ImageRef operator-(const ImageRef rhs) const;
122 /// Bitwise left-shift operator
123 /// @param i The amount to shift
124 constexpr ImageRef& operator<<=(int i);
125 /// Bitwise right-shift operator
126 /// @param i The amount to shift
127 constexpr ImageRef& operator>>=(int i);
128 /// Bitwise right-shift operator
129 /// @param i The amount to shift
130 constexpr ImageRef operator>>(int i) const;
131 /// Bitwise left-shift operator
132 /// @param i The amount to shift
133 constexpr ImageRef operator<<(int i) const;
134 /** An ImageRef is less than another ImageRef if it is earlier in the
135 standard horizontal scan-line order, i.e. has a smaller y-co-ordinate
136 or the same y-co-ordinate but a smaller x-co-ordinate. An ordering of
137 ImageRefs is needed to allow them to be used in STL for sets, maps,
138 multisets, multimaps etc. **/
139 constexpr bool operator<(const ImageRef& other) const;
140 /** An ImageRef is greater than another ImageRef if it is earlier in the
141 standard horizontal scan-line order, i.e. has a smaller y-co-ordinate
142 or the same y-co-ordinate but a smaller x-co-ordinate. **/
143 constexpr bool operator>(const ImageRef& other) const;
144
145 /// Magnitude-squared (x*x + y*y)
146 constexpr unsigned int mag_squared() const;
147
148 /// Area (product of x and y; signed)
149 constexpr int area() const;
150
151 /// The equivalent of doing .* in matlab
152 constexpr ImageRef dot_times(const ImageRef& ref) const;
153
154 /// Square bracket subscripts for easy loops. 0=x 1=y other=error
155 constexpr int& operator[](int i);
156
157 /// Square bracket const subscripts for easy loops. 0=x 1=y other=error
158 constexpr int operator[](int i) const;
159
160 //Why do these exist?
161 ///@overload
162 constexpr ImageRef shiftl(int i) const;
163 ///@overload
164 constexpr ImageRef shiftr(int i) const;
165
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; ///< The x co-ordinate
173 int y; ///< The y co-ordinate
174 };
175
176 /// Left-multiply an ImageRef by a scalar. Mutiplies both x and y.
177 /// @param scale The multiplier
178 /// @param ref The ImageRef to scale
179 /// @relates ImageRef
180 constexpr inline ImageRef operator*(const int scale, const ImageRef& ref);
181
182 namespace Exceptions
183 {
184 /// Exception if subscript for [] is not 0 or 1
185 struct BadSubscript : public CVD::Exceptions::All
186 {
187 BadSubscript() {};
188 };
189 }
190
191 #include <cvd/internal/image_ref_implementation.hh>
192
193 // Streams stuff for ImageRef class //
194
195 /// Write an ImageRef to a stream in the format "[x y]"
196 /// @param os The stream
197 /// @param ref The co-ordinate
198 /// @relates ImageRef
199 inline std::ostream& operator<<(std::ostream& os, const ImageRef& ref)
200 {
201 return os << "[" << ref.x << " " << ref.y << "]";
202 }
203
204 /// Read an ImageRef from a stream. Any format with two successive numbers will work
205 /// @relates ImageRef
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();
213
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
251 /// A zero ImageRef
252 /// @relates ImageRef
253 const ImageRef ImageRef_zero(0, 0);
254
255 } //namespace CVD
256
257 #endif