SMIL  1.0.1-dev
DImage.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _DIMAGE_HPP
31 #define _DIMAGE_HPP
32 
33 #include <string>
34 
35 #include "Core/include/DBaseImage.h"
36 #include "Gui/include/DBaseImageViewer.h"
37 
38 namespace smil
39 {
45  template <class T> class ImageViewer;
46 
47  template <class T> class ResImage;
48 
49  template <class T> class SharedImage;
50 
56  template <class T> class Image : public BaseImage
57  {
58  typedef BaseImage parentClass;
59 
60  public:
62  Image();
64  Image(size_t w, size_t h, size_t d = 1);
66  Image(const char *fileName);
67 
68  virtual ~Image();
69 
70  // Provide explicit copy constructor and assignment operator
72  Image(const Image<T> &rhs, bool cloneData = false);
73 
74  template <class T2> Image(const Image<T2> &rhs, bool cloneData = false);
75  Image(const ResImage<T> &rhs, bool cloneData = true);
76 
77  // Assignment operator
78  Image<T> &operator=(const Image<T> &rhs)
79  {
80  this->clone(rhs);
81  return *this;
82  }
83 
84  Image(BaseImage *_im, bool stealIdentity = false);
85 
86  // SWIG-Python doesn't handle abstract classes.
87  // Usefull when creating an image from createFromFile (kind of cast).
89  void drain(Image<T> *im, bool deleteSrc = false);
90 
93  virtual const char *getTypeAsString()
94  {
95  T *dum = NULL;
96  return getDataTypeAsString<T>(dum);
97  }
98  typedef typename ImDtTypes<T>::pixelType pixelType;
99  typedef typename ImDtTypes<T>::lineType lineType;
100  typedef typename ImDtTypes<T>::restrictLineType restrictLineType;
101  typedef typename ImDtTypes<T>::sliceType sliceType;
102  typedef typename ImDtTypes<T>::volType volType;
103 
105  lineType getPixels() const
106  {
107  return pixels;
108  }
109 
111  sliceType getLines() const
112  {
113  return lines;
114  }
115 
117  volType getSlices() const
118  {
119  return slices;
120  }
121 
125  SharedImage<T> getSlice(size_t sliceNum) const;
126 
128  inline T getPixel(size_t x, size_t y, size_t z = 0) const
129  {
130  ASSERT((x < width && y < height && z < depth),
131  "Coords out of image range", T(0));
132  return pixels[z * width * height + y * width + x];
133  }
134 
136  inline T getPixel(size_t offset) const
137  {
138  ASSERT((offset < pixelCount), "Offset out of image range", T(0));
139  return pixels[offset];
140  }
141 
142  inline T getPixelNoCheck(size_t offset) const
143  {
144  return pixels[offset];
145  }
146 
148  inline RES_T setPixel(size_t x, size_t y, size_t z, const T &value)
149  {
150  ASSERT((x < width && y < height && z < depth),
151  "Coords out of image range", RES_ERR);
152  pixels[z * width * height + y * width + x] = value;
153  modified();
154  return RES_OK;
155  }
156 
158  inline RES_T setPixel(size_t x, size_t y, const T &value)
159  {
160  return setPixel(x, y, 0, value);
161  }
162 
164  inline RES_T setPixel(size_t offset, const T &value)
165  {
166  ASSERT((offset < pixelCount), "Offset out of image range", RES_ERR);
167  pixels[offset] = value;
168  modified();
169  return RES_OK;
170  }
171 
172  inline void setPixelNoCheck(size_t offset, const T &value)
173  {
174  pixels[offset] = value;
175  }
176 
178  void toArray(T outArray[]);
179 
181  void fromArray(const T inArray[]);
182 
184  void toCharArray(signed char outArray[]);
185  char *toCharArray()
186  {
187  return (char *) pixels;
188  }
190  void fromCharArray(const signed char inArray[]);
191 
193  void toIntArray(int outArray[]);
194 
196  void fromIntArray(const int inArray[]);
197 
199  vector<int> toIntVector();
200 
202  void fromIntVector(const vector<int> inVector);
203 
205  string toString();
207  void fromString(string pixVals);
208 
210  virtual ImageViewer<T> *getViewer();
211 
214  virtual bool isVisible();
215 
216  virtual void init();
217 
219  virtual void clone(const Image<T> &rhs);
220  template <class T2> void clone(const Image<T2> &rhs);
222  virtual Image<T> clone(bool cloneData = true)
223  {
224  Image<T> im(*this, cloneData);
225  return im;
226  }
227 
229  virtual RES_T setSize(size_t w, size_t h, size_t d = 1,
230  bool doAllocate = true);
232  virtual RES_T setSize(size_t s[3], bool doAllocate = true)
233  {
234  return setSize(s[0], s[1], s[2], doAllocate);
235  }
237  virtual RES_T setSize(const BaseImage &rhs, bool doAllocate = true)
238  {
239  return setSize(rhs.getWidth(), rhs.getHeight(), rhs.getDepth(),
240  doAllocate);
241  }
243  virtual RES_T setSize(const vector<UINT> s, bool doAllocate = true)
244  {
245  if (s.size() == 3)
246  return setSize(s[0], s[1], s[2], doAllocate);
247  else
248  return setSize(s[0], s[1], 1, doAllocate);
249  }
250 
252  virtual RES_T allocate();
254  virtual RES_T deallocate();
255 
264  void printSelf(ostream &os, bool displayPixVals, bool hexaGrid = false,
265  string indent = "") const;
266 
267  virtual void printSelf(ostream &os = std::cout, string indent = "") const
268  {
269  printSelf(os, false, false, indent);
270  }
271 
272  virtual void printSelf(string indent)
273  {
274  printSelf(std::cout, false, false, indent);
275  }
276 
277  void printSelf(bool displayPixVals, bool hexaGrid = false, string indent = "")
278  {
279  printSelf(std::cout, displayPixVals, hexaGrid, indent);
280  }
281 
283  virtual const char *getInfoString(const char *indent = "") const
284  {
285  stringstream s;
286  this->printSelf(s, indent);
287  return s.str().c_str();
288  }
289 
291  virtual void *getVoidPointer(void)
292  {
293  return pixels;
294  }
295 
296  virtual RES_T load(const char *fileName);
297  virtual RES_T save(const char *fileName);
298 
299 #if defined SWIGPYTHON && defined USE_NUMPY
300 
313  PyObject *getNumArray(bool c_contigous = false);
314 
325  void fromNumArray(PyObject *array);
326 #endif // defined SWIGPYTHON && defined USE_NUMPY
327 
329  virtual void modified();
330 
331  static T getDataTypeMin()
332  {
333  return ImDtTypes<T>::min();
334  }
335 
336  static T getDataTypeMax()
337  {
338  return ImDtTypes<T>::max();
339  }
340 
341  inline T &operator[](size_t i)
342  {
343  if (i < pixelCount)
344  return this->pixels[i];
345  ERR_MSG("Offset out of range.");
346  return this->dumPixel;
347  }
348 
350  Image<T> &operator<<(const Image<T> &rhs);
352  Image<T> &operator<<(const T &value);
354  ResImage<T> operator~() const;
355  ResImage<T> operator-() const;
357  ResImage<T> operator+(const Image<T> &rhs);
359  ResImage<T> operator+(const T &value);
361  Image<T> &operator+=(const Image<T> &rhs);
363  Image<T> &operator+=(const T &value);
365  ResImage<T> operator-(const Image<T> &rhs);
367  ResImage<T> operator-(const T &value);
369  Image<T> &operator-=(const Image<T> &rhs);
371  Image<T> &operator-=(const T &value);
373  ResImage<T> operator*(const Image<T> &rhs);
375  ResImage<T> operator*(const T &value);
377  Image<T> &operator*=(const Image<T> &rhs);
379  Image<T> &operator*=(const T &value);
381  ResImage<T> operator/(const Image<T> &rhs);
383  ResImage<T> operator/(const T &value);
385  Image<T> &operator/=(const Image<T> &rhs);
387  Image<T> &operator/=(const T &value);
389  ResImage<T> operator==(const Image<T> &rhs);
391  ResImage<T> operator!=(const Image<T> &rhs);
393  ResImage<T> operator<(const Image<T> &rhs);
395  ResImage<T> operator<(const T &value);
397  ResImage<T> operator<=(const Image<T> &rhs);
399  ResImage<T> operator<=(const T &value);
401  ResImage<T> operator>(const Image<T> &rhs);
403  ResImage<T> operator>(const T &value);
405  ResImage<T> operator>=(const Image<T> &rhs);
407  ResImage<T> operator>=(const T &value);
408 
409  ResImage<T> operator|(const Image<T> &rhs);
410  ResImage<T> operator|(const T &value);
411  Image<T> &operator|=(const Image<T> &rhs);
412  Image<T> &operator|=(const T &value);
413 
415  ResImage<T> operator&(const Image<T> &rhs);
417  ResImage<T> operator&(const T &value);
419  Image<T> &operator&=(const Image<T> &rhs);
421  Image<T> &operator&=(const T &value);
422 
427  operator bool();
428 
430  Image<T> &operator<<(const lineType &tab);
432  Image<T> &operator<<(vector<T> &vect);
434  Image<T> &operator>>(vector<T> &vect);
435 
436 #ifndef SWIG
437  Image<T> &operator<<(const char *s);
438 #endif // SWIG
439  inline Image<T> &operator<<(const string s)
440  {
441  return this->operator<<(s.c_str());
442  }
443 
444  Image<T> &operator>>(const char *s);
445  inline Image<T> &operator>>(const string s)
446  {
447  return this->operator>>(s.c_str());
448  }
449 
450  protected:
451  lineType pixels;
452  sliceType lines;
453  volType slices;
454 
455  RES_T restruct(void);
456 
457  ImageViewer<T> *viewer;
458  void createViewer();
459 
460  T dumPixel;
461  // Specify if the viewer has been created internally
462  // ImageViewerWidget *viewer;
463 
464  public:
466  virtual void setName(const char *_name);
467 
469  virtual void show(const char *_name = NULL, bool labelImage = false);
470 
473  virtual void showLabel(const char *_name = NULL);
474 
475  virtual void showNormal(const char *_name = NULL);
476 
478  virtual void hide();
479  };
480 
481  template <class T> class ResImage : public Image<T>
482  {
483  public:
484  ResImage(const Image<T> &rhs) : Image<T>(rhs, false)
485  {
486  }
487 
488  // Warning ! This will empty the image rhs !
489  // Allows to avoid multiple copy with SwigValueWrapper operations...
490  ResImage(const ResImage<T> &rhs) : Image<T>()
491  {
492  Image<T>::drain(const_cast<ResImage<T> *>(&rhs));
493  }
494 
495  ~ResImage()
496  {
497  }
498  };
499 
500  template <class T> Image<T> *createImage(const T)
501  {
502  return new Image<T>();
503  }
504 
505  template <class T> Image<T> *castBaseImage(BaseImage *img, const T &)
506  {
507  ASSERT(strcmp(getDataTypeAsString<T>(), img->getTypeAsString()) == 0,
508  "Bad type for cast", NULL);
509  return reinterpret_cast<Image<T> *>(img);
510  }
511 
513  template <class T>
514  RES_T drawOverlay(const Image<T> &imToDraw, Image<T> &imOut)
515  {
516  imOut.getViewer()->drawOverlay(imToDraw);
517  return RES_OK;
518  }
519 
522 } // namespace smil
523 
524 #endif // _DIMAGE_HPP
smil::Image::fromCharArray
void fromCharArray(const signed char inArray[])
Copy pixel values from a given char array.
smil::Image::getSlices
volType getSlices() const
Get an array containing the start offset of each slice.
Definition: DImage.hpp:117
smil::Image::getPixels
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:105
smil::Image::allocate
virtual RES_T allocate()
Allocate image.
smil::Image::setPixel
RES_T setPixel(size_t offset, const T &value)
Set the value of the pixel at a given offset.
Definition: DImage.hpp:164
smil::Image::isVisible
virtual bool isVisible()
Check if the image is visible.
smil::Image::operator*
ResImage< T > operator*(const Image< T > &rhs)
Multiply by image.
smil::Image::getNumArray
PyObject * getNumArray(bool c_contigous=false)
getNumArray() -
smil::Image::setPixel
RES_T setPixel(size_t x, size_t y, const T &value)
Set the value of the pixel at pos x,y.
Definition: DImage.hpp:158
smil::Image::fromString
void fromString(string pixVals)
Import pixel values from string.
smil::Image::operator>
ResImage< T > operator>(const Image< T > &rhs)
Greater boolean operator (see grt())
smil::Image::operator~
ResImage< T > operator~() const
Negate image.
smil::Image::operator+=
Image< T > & operator+=(const Image< T > &rhs)
Image addition assignment.
smil::Image::operator<
ResImage< T > operator<(const Image< T > &rhs)
Lower boolean operator (see low())
smil::Image::clone
virtual void clone(const Image< T > &rhs)
Clone from a given image (set same size and copy content)
smil::Image::hide
virtual void hide()
Hide image.
smil::Image::getTypeAsString
virtual const char * getTypeAsString()
Get the image type.
Definition: DImage.hpp:93
smil::Image::Image
Image()
Default constructor.
smil::Image::operator+
ResImage< T > operator+(const Image< T > &rhs)
Add image.
smil::Image::operator/=
Image< T > & operator/=(const Image< T > &rhs)
Image division assignment.
smil::SharedImage
Image that uses an existing (1D) data pointer.
Definition: DImage.hpp:49
smil::Image::toArray
void toArray(T outArray[])
Copy pixel values to a given array.
smil::Image::toIntVector
vector< int > toIntVector()
Copy pixel values to a given int vector.
smil::Image::getVoidPointer
virtual void * getVoidPointer(void)
Get pixels as a void pointer.
Definition: DImage.hpp:291
smil::Image::modified
virtual void modified()
Trigger modified event (allows to force display update)
smil::BaseImage
Base Image class.
Definition: DBaseImage.h:49
smil::Image::clone
virtual Image< T > clone(bool cloneData=true)
Create a clone of the image (with same size and content )
Definition: DImage.hpp:222
smil::Image::setSize
virtual RES_T setSize(const vector< UINT > s, bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:243
smil::Image::deallocate
virtual RES_T deallocate()
Deallocate image.
smil::Image::operator&=
Image< T > & operator&=(const Image< T > &rhs)
Bitwise and assignement.
smil::BaseImage::getHeight
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:85
smil::Image::operator>>
Image< T > & operator>>(vector< T > &vect)
Export image data to a vector.
smil::Image::operator<=
ResImage< T > operator<=(const Image< T > &rhs)
Lower or equal boolean operator (see lowOrEqu())
smil::Image::operator*=
Image< T > & operator*=(const Image< T > &rhs)
Image multiplication assignment.
smil::Image::toString
string toString()
Export pixel values to a string.
smil::Image::drain
void drain(Image< T > *im, bool deleteSrc=false)
Replace container. Drain memory from image im to this.
smil::Image::setSize
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
smil::Image::setSize
virtual RES_T setSize(const BaseImage &rhs, bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:237
smil::drawOverlay
RES_T drawOverlay(const Image< T > &imToDraw, Image< T > &imOut)
Draw overlay.
Definition: DImage.hpp:514
smil::Image::show
virtual void show(const char *_name=NULL, bool labelImage=false)
Show the default viewer associated with the image.
smil::Image::showLabel
virtual void showLabel(const char *_name=NULL)
Show the default viewer associated with the image using a color lookup table.
smil::Image
Main Image class.
Definition: DQVtkViewer.hpp:42
smil::Image::operator&
ResImage< T > operator&(const Image< T > &rhs)
Bitwise and operator.
smil::Image::fromIntVector
void fromIntVector(const vector< int > inVector)
Copy pixel values from a given int vector.
smil::Image::setPixel
RES_T setPixel(size_t x, size_t y, size_t z, const T &value)
Set the value of the pixel at pos x,y,z (for 3D image)
Definition: DImage.hpp:148
smil::Image::operator-=
Image< T > & operator-=(const Image< T > &rhs)
Image subtraction assignment.
smil::BaseImage::getDepth
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:90
smil::Image::operator==
ResImage< T > operator==(const Image< T > &rhs)
Equal boolean operator (see equ()).
smil::Image::getSlice
SharedImage< T > getSlice(size_t sliceNum) const
Get a 2D slice of a 3D image. It doesn't create an image, but returns a 2D SharedImage using the same...
smil::Image::operator>=
ResImage< T > operator>=(const Image< T > &rhs)
Greater or equal boolean operator (see grt())
smil::ResImage
Definition: DImage.hpp:47
smil::Image::operator/
ResImage< T > operator/(const Image< T > &rhs)
Divide by image.
smil::Image::fromIntArray
void fromIntArray(const int inArray[])
Copy pixel values from a given int array.
smil::Image::setName
virtual void setName(const char *_name)
Set the name of the image.
smil::Image::getLines
sliceType getLines() const
Get an array containing the start offset of each line.
Definition: DImage.hpp:111
smil::ImDtTypes
Definition: DTypes.hpp:88
smil::Image::printSelf
void printSelf(ostream &os, bool displayPixVals, bool hexaGrid=false, string indent="") const
Print a description of the image.
smil::Image::fromNumArray
void fromNumArray(PyObject *array)
fromNumArray() -
smil::Image::operator<<
Image< T > & operator<<(const Image< T > &rhs)
Copy image.
smil::Image::toIntArray
void toIntArray(int outArray[])
Copy pixel values to a given int array.
smil::Image::getInfoString
virtual const char * getInfoString(const char *indent="") const
Get the description of the image as a string.
Definition: DImage.hpp:283
smil::Image::operator!=
ResImage< T > operator!=(const Image< T > &rhs)
Diff boolean operator (see equ()).
smil::Image::getViewer
virtual ImageViewer< T > * getViewer()
Get the image viewer (create one if needed)
smil::Image::fromArray
void fromArray(const T inArray[])
Copy pixel values from a given array.
smil::Image::setSize
virtual RES_T setSize(size_t s[3], bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:232
smil::Image::getPixel
T getPixel(size_t offset) const
Return the value of the pixel at a given offset.
Definition: DImage.hpp:136
smil::Image::getPixel
T getPixel(size_t x, size_t y, size_t z=0) const
Return the value of the pixel at pos x,y(,z)
Definition: DImage.hpp:128
smil::BaseImage::getWidth
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:80