UsTK : Ultrasound ToolKit  version 2.0.1 under development (2024-12-17)
usImage3D.h
1 /****************************************************************************
2  *
3  * This file is part of the ustk software.
4  * Copyright (C) 2016 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 ustk 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  * This software was developed at:
17  * Inria Rennes - Bretagne Atlantique
18  * Campus Universitaire de Beaulieu
19  * 35042 Rennes Cedex
20  * France
21  *
22  * If you have questions regarding the use of this file, please contact
23  * Inria at ustk@inria.fr
24  *
25  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * Authors:
29  * Pierre Chatelain
30  * Marc Pouliquen
31  *
32  *****************************************************************************/
33 
41 #ifndef __usImage3D_h_
42 #define __usImage3D_h_
43 
44 #include <algorithm>
45 #include <cstring>
46 #include <iostream>
47 #include <vector>
48 
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/core/vpException.h>
51 #include <visp3/core/vpImage.h>
52 
60 template <class Type> class usImage3D
61 {
62 public:
67 
74  usImage3D(unsigned int height, unsigned int width, unsigned int frameNumber);
75 
83  usImage3D(unsigned int height, unsigned int width, unsigned int frameNumber, Type initialValue);
84 
90  usImage3D(const usImage3D<Type> &image, const bool copy = true);
91 
95  virtual ~usImage3D();
96 
99 
104  Type *getConstData() const { return bitmap; }
105 
110  Type *getData() { return bitmap; }
111 
119  Type *getData(unsigned int i, unsigned int j, unsigned int k) { return framPointer[k] + m_width * i + j; }
120 
125  unsigned int getWidth() const { return m_width; }
126 
131  unsigned int getHeight() const { return m_height; }
132 
137  unsigned int getNumberOfFrames() const { return m_numberOfFrames; }
138 
143  unsigned int getSize() const { return m_size; }
144 
149  void initData(Type value);
150 
156 
161  bool operator==(const usImage3D<Type> &other);
162 
169  inline Type operator()(unsigned int i, unsigned int j, unsigned int k) const
170  {
171  return framPointer[k][m_width * i + j];
172  }
173 
181  inline void operator()(unsigned int i, unsigned int j, unsigned int k, Type value)
182  {
183  framPointer[k][m_width * i + j] = value;
184  }
185 
192  void resize(unsigned int height, unsigned int width, unsigned int numberOfFrames);
193 
199  void setData(Type *data, int numberOfVoxels);
200 
202 
203 protected:
204 private:
211  void init(unsigned int height, unsigned int width, unsigned int numberOfFrames);
212 
213  unsigned int m_width;
214  unsigned int m_height;
215  unsigned int m_numberOfFrames;
216  unsigned int m_size;
218  Type *bitmap;
219  Type **framPointer;
220 };
221 
222 /****************************************************************************
223 * Template implementations.
224 ****************************************************************************/
242 template <class Type>
243 inline void usImage3D<Type>::init(unsigned int height, unsigned int width, unsigned int numberOfFrames)
244 {
245  if ((width != this->m_width) || (height != this->m_height) || (numberOfFrames != this->m_numberOfFrames)) {
246  if (bitmap != NULL) {
247  vpDEBUG_TRACE(10, "Destruction bitmap[]");
248  delete[] bitmap;
249  bitmap = NULL;
250  }
251  if (framPointer != NULL) {
252  vpDEBUG_TRACE(10, "Destruction framPointer[]");
253  delete[] framPointer;
254  framPointer = NULL;
255  }
256  }
257 
258  m_width = width;
259  m_height = height;
260  m_numberOfFrames = numberOfFrames;
261 
262  m_size = m_width * m_height * m_numberOfFrames;
263 
264  if (bitmap == NULL)
265  bitmap = new Type[m_size];
266 
267  if (framPointer == NULL)
268  framPointer = new Type *[numberOfFrames];
269 
270  for (unsigned int k = 0; k < numberOfFrames; k++)
271  framPointer[k] = bitmap + m_width * m_height * k;
272 
273  // vpERROR_TRACE("Allocate bitmap %p",bitmap) ;
274  if (bitmap == NULL) {
275  vpERROR_TRACE("cannot allocate bitmap ");
276  throw(vpException(vpException::memoryAllocationError, "cannot allocate bitmap "));
277  }
278 }
279 
280 template <class Type>
281 usImage3D<Type>::usImage3D() : m_width(0), m_height(0), m_numberOfFrames(0), m_size(0), bitmap(NULL), framPointer(NULL)
282 {
283 }
284 
285 template <class Type>
286 usImage3D<Type>::usImage3D(unsigned int height, unsigned int width, unsigned int frameNumber)
287  : m_width(0), m_height(0), m_numberOfFrames(0), m_size(0), bitmap(NULL), framPointer(NULL)
288 {
289  this->init(height, width, frameNumber);
290  this->initData(Type());
291 }
292 
293 template <class Type>
294 usImage3D<Type>::usImage3D(unsigned int height, unsigned int width, unsigned int frameNumber, Type initialValue)
295  : m_width(0), m_height(0), m_numberOfFrames(0), m_size(0), bitmap(NULL), framPointer(NULL)
296 {
297  this->init(height, width, frameNumber);
298  this->initData(initialValue);
299 }
300 
301 template <class Type>
302 usImage3D<Type>::usImage3D(const usImage3D<Type> &volume, const bool copy)
303  : m_width(0), m_height(0), m_numberOfFrames(0), m_size(0), bitmap(NULL), framPointer(NULL)
304 {
305  this->init(volume.getHeight(), volume.getWidth(), volume.getNumberOfFrames());
306 
307  // deep copy
308  if (copy)
309  memcpy(bitmap, volume.bitmap, m_size * sizeof(Type));
310 }
311 
312 template <class Type> usImage3D<Type>::~usImage3D()
313 {
314  if (bitmap) {
315  delete[] bitmap;
316  bitmap = NULL;
317  }
318  if (framPointer) {
319  delete[] framPointer;
320  framPointer = NULL;
321  }
322 }
323 
324 template <class Type> usImage3D<Type> &usImage3D<Type>::operator=(const usImage3D<Type> &other)
325 {
326  this->init(other.m_height, other.m_width, other.m_numberOfFrames);
327 
328  memcpy(bitmap, other.bitmap, m_size * sizeof(Type));
329  return *this;
330 }
331 
332 template <class Type> bool usImage3D<Type>::operator==(const usImage3D<Type> &other)
333 {
334  bool settingsOk = this->getWidth() == other.getWidth() && this->getHeight() == other.getHeight() &&
335  this->getNumberOfFrames() == other.getNumberOfFrames();
336 
337  if (settingsOk) {
338  for (unsigned int i = 0; i < m_size; i++) {
339  if (bitmap[i] != other.getConstData()[i])
340  return false;
341  }
342  } else
343  return false;
344  return true;
345 }
346 
347 template <class Type> std::ostream &operator<<(std::ostream &out, const usImage3D<Type> &image)
348 {
349  return out << "width : " << image.getWidth() << std::endl
350  << "height : " << image.getHeight() << std::endl
351  << "frames in volume : " << image.getNumberOfFrames() << std::endl;
352 }
353 
354 template <class Type> void usImage3D<Type>::setData(Type *data, int numberOfVoxels)
355 {
356  try {
357  if (m_size != numberOfVoxels) {
358  throw(vpException(vpException::fatalError, "usImage3D::setData() error, bitmap dimensions mismatch."));
359  }
360  memcpy(bitmap, data, m_size * sizeof(Type));
361  } catch (std::exception e) {
362  std::cout << "usImage3D::setData(), error when trying to copy the data :" << std::endl;
363  std::cout << e.what() << std::endl;
364  }
365 }
366 
367 template <class Type> void usImage3D<Type>::initData(Type value)
368 {
369  try {
370  std::fill_n(bitmap, m_size, value);
371  } catch (const std::exception &e) {
372  std::cout << e.what() << std::endl;
373  }
374 }
375 
376 template <class Type> void usImage3D<Type>::resize(unsigned int height, unsigned int width, unsigned int numberOfFrames)
377 {
378  this->init(height, width, numberOfFrames);
379 }
380 #endif // __usImage3D_h_
Representation of a physical image volume.
Definition: usImage3D.h:61
usImage3D(const usImage3D< Type > &image, const bool copy=true)
Definition: usImage3D.h:302
Type * getData(unsigned int i, unsigned int j, unsigned int k)
Definition: usImage3D.h:119
unsigned int getNumberOfFrames() const
Definition: usImage3D.h:137
bool operator==(const usImage3D< Type > &other)
Definition: usImage3D.h:332
usImage3D(unsigned int height, unsigned int width, unsigned int frameNumber, Type initialValue)
Definition: usImage3D.h:294
unsigned int getSize() const
Definition: usImage3D.h:143
Type * getData()
Definition: usImage3D.h:110
void resize(unsigned int height, unsigned int width, unsigned int numberOfFrames)
Definition: usImage3D.h:376
Type * getConstData() const
Definition: usImage3D.h:104
void initData(Type value)
Definition: usImage3D.h:367
void setData(Type *data, int numberOfVoxels)
Definition: usImage3D.h:354
unsigned int getHeight() const
Definition: usImage3D.h:131
unsigned int getWidth() const
Definition: usImage3D.h:125
usImage3D(unsigned int height, unsigned int width, unsigned int frameNumber)
Definition: usImage3D.h:286
Type operator()(unsigned int i, unsigned int j, unsigned int k) const
Definition: usImage3D.h:169
void operator()(unsigned int i, unsigned int j, unsigned int k, Type value)
Definition: usImage3D.h:181
virtual ~usImage3D()
Definition: usImage3D.h:312
usImage3D< Type > & operator=(const usImage3D< Type > &other)
Definition: usImage3D.h:324