Visual Servoing Platform  version 3.6.1 under development (2024-10-18)
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 
39 template <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 
64 inline 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 
93 inline 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 
122 inline 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 
152 inline 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 
190 template <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 template <class Type> vpImage<Type> &vpImage<Type>::operator=(vpImage<Type> &&other)
215 {
216  if (row != nullptr) {
217  delete[] row;
218  }
219  row = other.row;
220  if (bitmap != nullptr && hasOwnership) {
221  delete[] bitmap;
222  }
223  bitmap = other.bitmap;
224 
225  if (display != nullptr) {
226  if ((height != other.height) || (width != other.width)) {
228  "Error in vpImage::operator=(&) where the display is initialised but the image size is different"));
229  }
230  }
231  if (other.display != nullptr) {
233  "Error in vpImage::operator=(&&) where the display of the image to move is initialised"));
234  }
235  height = other.height;
236  width = other.width;
237  npixels = other.npixels;
238  hasOwnership = other.hasOwnership;
239 
240  other.bitmap = nullptr;
241  other.display = nullptr;
242  other.npixels = 0;
243  other.width = 0;
244  other.height = 0;
245  other.row = nullptr;
246  other.hasOwnership = false;
247 
248  return *this;
249 }
250 #endif
251 
258 template <class Type> vpImage<Type> &vpImage<Type>::operator=(const Type &v)
259 {
260  for (unsigned int i = 0; i < npixels; ++i) {
261  bitmap[i] = v;
262  }
263 
264  return *this;
265 }
266 
272 template <class Type> bool vpImage<Type>::operator==(const vpImage<Type> &I) const
273 {
274  if (this->width != I.getWidth()) {
275  return false;
276  }
277  if (this->height != I.getHeight()) {
278  return false;
279  }
280 
281  /*
282  // printf("wxh: %dx%d bitmap: %p I.bitmap %p\n", width, height, bitmap,
283  // I.bitmap);
284  */
285  for (unsigned int i = 0; i < npixels; ++i) {
286  if (bitmap[i] != I.bitmap[i]) {
287  /*
288  // std::cout << "differ for pixel " << i << " (" << i%this->height
289  // << ", " << i - i%this->height << ")" << std::endl;
290  */
291  return false;
292  }
293  }
294  return true;
295 }
301 template <class Type> bool vpImage<Type>::operator!=(const vpImage<Type> &I) const { return !(*this == I); }
302 
332 template <class Type> vpImage<Type> vpImage<Type>::operator-(const vpImage<Type> &B) const
333 {
334  vpImage<Type> C;
335  sub(*this, B, C);
336  return C;
337 }
338 
339 #endif
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:611
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
Definition of the vpImage class member functions.
Definition: vpImage.h:131
vpImage< Type > & operator=(const vpImage< Type > &other)
Copy operator.
unsigned int getWidth() const
Definition: vpImage.h:242
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
unsigned int getHeight() const
Definition: vpImage.h:181
bool operator!=(const vpImage< Type > &I) const