Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpImage_operators.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Image handling.
32 */
33
34#ifndef VP_IMAGE_OPERATOR_H
35#define VP_IMAGE_OPERATOR_H
36
37// Warning: this file shouldn't be included by the user. Internal usage only to reduce length of vpImage.h
38
39template <class Type> std::ostream &operator<<(std::ostream &s, const vpImage<Type> &I)
40{
41 if (I.bitmap == nullptr) {
42 return s;
43 }
44
45 unsigned int i_height = I.getHeight();
46 unsigned int i_width = I.getWidth();
47 for (unsigned int i = 0; i < i_height; ++i) {
48 for (unsigned int j = 0; j < (i_width - 1); ++j) {
49 s << I[i][j] << " ";
50 }
51
52 // We don't add " " after the last column element
53 s << I[i][i_width - 1];
54
55 // We don't add a \n character at the end of the last row line
56 if (i < (i_height - 1)) {
57 s << std::endl;
58 }
59 }
60
61 return s;
62}
63
64inline std::ostream &operator<<(std::ostream &s, const vpImage<unsigned char> &I)
65{
66 if (I.bitmap == nullptr) {
67 return s;
68 }
69
70 std::ios_base::fmtflags original_flags = s.flags();
71 const unsigned int magic_3 = 3;
72
73 unsigned int i_height = I.getHeight();
74 unsigned int i_width = I.getWidth();
75 for (unsigned int i = 0; i < i_height; ++i) {
76 for (unsigned int j = 0; j < (i_width - 1); ++j) {
77 s << std::setw(magic_3) << static_cast<unsigned>(I[i][j]) << " ";
78 }
79
80 // We don't add " " after the last column element
81 s << std::setw(magic_3) << static_cast<unsigned>(I[i][I.getWidth() - 1]);
82
83 // We don't add a \n character at the end of the last row line
84 if (i < (i_height - 1)) {
85 s << std::endl;
86 }
87 }
88
89 s.flags(original_flags); // restore s to standard state
90 return s;
91}
92
93inline std::ostream &operator<<(std::ostream &s, const vpImage<char> &I)
94{
95 if (I.bitmap == nullptr) {
96 return s;
97 }
98
99 std::ios_base::fmtflags original_flags = s.flags();
100 const unsigned int magic_4 = 4;
101
102 unsigned int i_height = I.getHeight();
103 unsigned int i_width = I.getWidth();
104 for (unsigned int i = 0; i < i_height; ++i) {
105 for (unsigned int j = 0; j < (i_width - 1); ++j) {
106 s << std::setw(magic_4) << static_cast<int>(I[i][j]) << " ";
107 }
108
109 // We don't add " " after the last column element
110 s << std::setw(magic_4) << static_cast<int>(I[i][i_width - 1]);
111
112 // We don't add a \n character at the end of the last row line
113 if (i < (i_height - 1)) {
114 s << std::endl;
115 }
116 }
117
118 s.flags(original_flags); // restore s to standard state
119 return s;
120}
121
122inline std::ostream &operator<<(std::ostream &s, const vpImage<float> &I)
123{
124 if (I.bitmap == nullptr) {
125 return s;
126 }
127
128 std::ios_base::fmtflags original_flags = s.flags();
129 const unsigned int magic_9 = 9;
130 s.precision(magic_9); // http://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10
131
132 unsigned int i_height = I.getHeight();
133 unsigned int i_width = I.getWidth();
134 for (unsigned int i = 0; i < i_height; ++i) {
135 for (unsigned int j = 0; j < (i_width - 1); ++j) {
136 s << I[i][j] << " ";
137 }
138
139 // We don't add " " after the last column element
140 s << I[i][i_width - 1];
141
142 // We don't add a \n character at the end of the last row line
143 if (i < (i_height - 1)) {
144 s << std::endl;
145 }
146 }
147
148 s.flags(original_flags); // restore s to standard state
149 return s;
150}
151
152inline std::ostream &operator<<(std::ostream &s, const vpImage<double> &I)
153{
154 if (I.bitmap == nullptr) {
155 return s;
156 }
157
158 std::ios_base::fmtflags original_flags = s.flags();
159 const unsigned int magic_17 = 17;
160 s.precision(magic_17); // http://en.cppreference.com/w/cpp/types/numeric_limits/max_digits10
161
162 unsigned int i_height = I.getHeight();
163 unsigned int i_width = I.getWidth();
164 for (unsigned int i = 0; i < i_height; ++i) {
165 for (unsigned int j = 0; j < (i_width - 1); ++j) {
166 s << I[i][j] << " ";
167 }
168
169 // We don't add " " after the last column element
170 s << I[i][i_width - 1];
171
172 // We don't add a \n character at the end of the last row line
173 if (i < (i_height - 1)) {
174 s << std::endl;
175 }
176 }
177
178 s.flags(original_flags); // restore s to standard state
179 return s;
180}
181
190template <class Type> vpImage<Type> &vpImage<Type>::operator=(const vpImage<Type> &other)
191{
192 if (display != nullptr) {
193 if ((height != other.height) || (width != other.width)) {
195 "Error in vpImage::operator=() where the display is initialised but the image size is different"));
196 }
197 }
198 resize(other.height, other.width);
199 memcpy(static_cast<void *>(bitmap), static_cast<void *>(other.bitmap), other.npixels * sizeof(Type));
200
201 return *this;
202}
203
204#if ((__cplusplus >= 201103L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))) // Check if cxx11 or higher
214{
215 if (row != nullptr) {
216 delete[] row;
217 }
218 row = other.row;
219 if (bitmap != nullptr && hasOwnership) {
220 delete[] bitmap;
221 }
222 bitmap = other.bitmap;
223
224 if (display != nullptr) {
225 if ((height != other.height) || (width != other.width)) {
227 "Error in vpImage::operator=(&) where the display is initialised but the image size is different"));
228 }
229 }
230 if (other.display != nullptr) {
232 "Error in vpImage::operator=(&&) where the display of the image to move is initialised"));
233 }
234 height = other.height;
235 width = other.width;
236 npixels = other.npixels;
237 hasOwnership = other.hasOwnership;
238
239 other.bitmap = nullptr;
240 other.display = nullptr;
241 other.npixels = 0;
242 other.width = 0;
243 other.height = 0;
244 other.row = nullptr;
245 other.hasOwnership = false;
246
247 return *this;
248}
249#endif
250
257template <class Type> vpImage<Type> &vpImage<Type>::operator=(const Type &v)
258{
259 for (unsigned int i = 0; i < npixels; ++i) {
260 bitmap[i] = v;
261 }
262
263 return *this;
264}
265
271template <class Type> bool vpImage<Type>::operator==(const vpImage<Type> &I) const
272{
273 if (this->width != I.getWidth()) {
274 return false;
275 }
276 if (this->height != I.getHeight()) {
277 return false;
278 }
279
280 /*
281 // printf("wxh: %dx%d bitmap: %p I.bitmap %p\n", width, height, bitmap,
282 // I.bitmap);
283 */
284 for (unsigned int i = 0; i < npixels; ++i) {
285 if (bitmap[i] != I.bitmap[i]) {
286 /*
287 // std::cout << "differ for pixel " << i << " (" << i%this->height
288 // << ", " << i - i%this->height << ")" << std::endl;
289 */
290 return false;
291 }
292 }
293 return true;
294}
295
300template <class Type> bool vpImage<Type>::operator!=(const vpImage<Type> &I) const { return !(*this == I); }
301
314 vpImage<unsigned char> A(288, 384);
315 vpImage<unsigned char> B(288, 384);
316 vpImage<unsigned char> C;
318 A = 128;
319 B = 120;
320
321 // operator-() : C = A - B
322 C = A - B;
323
324 return 0;
326 \endcode
327
328 \sa sub(const vpImage<Type> &, const vpImage<Type> &, vpImage<Type> &) to
329 avoid matrix allocation for each use.
330*/
331template <class Type> vpImage<Type> vpImage<Type>::operator-(const vpImage<Type> &B) const
332{
334 sub(*this, B, C);
335 return C;
336}
337
338#endif
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ dimensionError
Bad dimension.
Definition vpException.h:71
@ fatalError
Fatal error.
Definition vpException.h:72
vpImage< Type > & operator=(const vpImage< Type > &other)
Copy operator.
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition vpImage.h:544
friend std::ostream & operator<<(std::ostream &s, const vpImage< Type > &I)
bool operator==(const vpImage< Type > &I) const
vpImage< Type > operator-(const vpImage< Type > &B) const
Type * bitmap
points toward the bitmap
Definition vpImage.h:135
void sub(const vpImage< Type > &B, vpImage< Type > &C) const
Definition vpImage.h:915
vpDisplay * display
Definition vpImage.h:136
vpImage()
constructor
Definition vpImage.h:521
bool operator!=(const vpImage< Type > &I) const