SMIL  0.9.1
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'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
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 {
40 
46  template <class T>
47  class ImageViewer;
48 
49  template <class T>
50  class ResImage;
51 
52  template <class T>
53  class SharedImage;
54 
60  template <class T>
61  class Image : public BaseImage
62  {
63  typedef BaseImage parentClass;
64  public:
65 
67  Image();
69  Image(size_t w, size_t h, size_t d = 1);
71  Image(const char *fileName);
72 
73  virtual ~Image();
74 
75  // Provide explicit copy constructor and assignment operator
77  Image(const Image<T> & rhs, bool cloneData=false);
78  template <class T2>
79  Image(const Image<T2> &rhs, bool cloneData=false);
80  Image(const ResImage<T> & rhs, bool cloneData=true);
81 
82  // Assignment operator
83  Image<T>& operator = (const Image<T> &rhs)
84  {
85  this->clone(rhs);
86  return *this;
87  }
88 
89  Image(BaseImage *_im, bool stealIdentity=false);
90 
91  // SWIG-Python doesn't handle abstract classes.
92  // Usefull when creating an image from createFromFile (kind of cast).
94  void drain(Image<T> *im, bool deleteSrc=false);
95 
98  virtual const char* getTypeAsString()
99  {
100  T *dum = NULL;
101  return getDataTypeAsString<T>(dum);
102  }
103  typedef typename ImDtTypes<T>::pixelType pixelType;
104  typedef typename ImDtTypes<T>::lineType lineType;
105  typedef typename ImDtTypes<T>::restrictLineType restrictLineType;
106  typedef typename ImDtTypes<T>::sliceType sliceType;
107  typedef typename ImDtTypes<T>::volType volType;
108 
110  lineType getPixels() const {
111  return pixels;
112  }
114  sliceType getLines() const {
115  return lines;
116  }
118  volType getSlices() const {
119  return slices;
120  }
121 
124  SharedImage<T> getSlice(size_t sliceNum) const;
125 
127  inline T getPixel(size_t x, size_t y, size_t z=0) const
128  {
129  ASSERT((x<width && y<height && z<depth), "Coords out of image range", T(0));
130  return pixels[z*width*height+y*width+x];
131  }
133  inline T getPixel(size_t offset) const
134  {
135  ASSERT((offset < pixelCount), "Offset out of image range", T(0));
136  return pixels[offset];
137  }
138 
139  inline T getPixelNoCheck(size_t offset) const
140  {
141  return pixels[offset];
142  }
143 
145  inline RES_T setPixel(size_t x, size_t y, size_t z, const T &value)
146  {
147  ASSERT((x<width && y<height && z<depth), "Coords out of image range", RES_ERR);
148  pixels[z*width*height+y*width+x] = value;
149  modified();
150  return RES_OK;
151  }
152 
154  inline RES_T setPixel(size_t x, size_t y, const T &value)
155  {
156  return setPixel(x, y, 0, value);
157  }
158 
160  inline RES_T setPixel(size_t offset, const T &value)
161  {
162  ASSERT((offset < pixelCount), "Offset out of image range", RES_ERR);
163  pixels[offset] = value;
164  modified();
165  return RES_OK;
166  }
167 
168  inline void setPixelNoCheck(size_t offset, const T &value)
169  {
170  pixels[offset] = value;
171  }
172 
174  void toArray(T outArray[]);
176  void fromArray(const T inArray[]);
177 
179  void toCharArray(signed char outArray[]);
180  char *toCharArray() { return (char *)pixels; }
182  void fromCharArray(const signed char inArray[]);
183 
185  void toIntArray(int outArray[]);
187  void fromIntArray(const int inArray[]);
188 
190  vector<int> toIntVector();
192  void fromIntVector(const vector<int> inVector);
193 
195  string toString();
197  void fromString(string pixVals);
198 
200  virtual ImageViewer<T> *getViewer();
201 
204  virtual bool isVisible();
205 
206  virtual void init();
208  virtual void clone(const Image<T> &rhs);
209  template <class T2>
210  void clone(const Image<T2> &rhs);
212  virtual Image<T> clone(bool cloneData=true)
213  {
214  Image<T> im(*this, cloneData);
215  return im;
216  }
218  virtual RES_T setSize(size_t w, size_t h, size_t d = 1, bool doAllocate = true);
220  virtual RES_T setSize(size_t s[3], bool doAllocate = true)
221  {
222  return setSize(s[0], s[1], s[2], doAllocate);
223  }
225  virtual RES_T setSize(const BaseImage &rhs, bool doAllocate = true)
226  {
227  return setSize(rhs.getWidth(), rhs.getHeight(), rhs.getDepth(), doAllocate);
228  }
230  virtual RES_T setSize(const vector<UINT> s, bool doAllocate = true)
231  {
232  if (s.size()==3)
233  return setSize(s[0], s[1], s[2], doAllocate);
234  else return setSize(s[0], s[1], 1, doAllocate);
235  }
237  virtual RES_T allocate();
239  virtual RES_T deallocate();
240 
248  void printSelf(ostream &os, bool displayPixVals, bool hexaGrid=false, string indent="") const;
249  virtual void printSelf(ostream &os=std::cout, string indent="") const
250  {
251  printSelf(os, false, false, indent);
252  }
253  void printSelf(bool displayPixVals, bool hexaGrid=false)
254  {
255  printSelf(std::cout, displayPixVals, hexaGrid);
256  }
258  virtual const char *getInfoString(const char *indent = "") const
259  {
260  stringstream s;
261  this->printSelf(s, indent);
262  return s.str().c_str();
263  }
264 
266  virtual void* getVoidPointer(void)
267  {
268  return pixels;
269  }
270 
271  virtual RES_T load(const char *fileName);
272  virtual RES_T save(const char *fileName);
273 
274  #if defined SWIGPYTHON && defined USE_NUMPY
275 
283  PyObject * getNumArray(bool c_contigous=false);
284 
286  void fromNumArray(PyObject *obj);
287  #endif // defined SWIGPYTHON && defined USE_NUMPY
288 
290  virtual void modified();
291 
292  static T getDataTypeMin() { return ImDtTypes<T>::min(); }
293  static T getDataTypeMax() { return ImDtTypes<T>::max(); }
294 
295  inline T &operator [] (size_t i)
296  {
297  if (i<pixelCount)
298  return this->pixels[i];
299  ERR_MSG("Offset out of range.");
300  return this->dumPixel;
301  }
302 
304  Image<T>& operator << (const Image<T> &rhs);
306  Image<T>& operator << (const T &value);
308  ResImage<T>operator ~ () const;
309  ResImage<T>operator - () const;
311  ResImage<T> operator + (const Image<T> &rhs);
313  ResImage<T> operator + (const T &value);
315  Image<T>& operator += (const Image<T> &rhs);
317  Image<T>& operator += (const T &value);
319  ResImage<T>operator - (const Image<T> &rhs);
321  ResImage<T>operator - (const T &value);
323  Image<T>& operator -= (const Image<T> &rhs);
325  Image<T>& operator -= (const T &value);
327  ResImage<T>operator * (const Image<T> &rhs);
329  ResImage<T>operator * (const T &value);
331  Image<T>& operator *= (const Image<T> &rhs);
333  Image<T>& operator *= (const T &value);
335  ResImage<T>operator / (const Image<T> &rhs);
337  ResImage<T>operator / (const T &value);
339  Image<T>& operator /= (const Image<T> &rhs);
341  Image<T>& operator /= (const T &value);
343  ResImage<T>operator == (const Image<T> &rhs);
345  ResImage<T>operator != (const Image<T> &rhs);
347  ResImage<T>operator < (const Image<T> &rhs);
349  ResImage<T>operator < (const T &value);
351  ResImage<T>operator <= (const Image<T> &rhs);
353  ResImage<T>operator <= (const T &value);
355  ResImage<T>operator > (const Image<T> &rhs);
357  ResImage<T>operator > (const T &value);
359  ResImage<T>operator >= (const Image<T> &rhs);
361  ResImage<T>operator >= (const T &value);
362 
363  ResImage<T>operator | (const Image<T> &rhs);
364  ResImage<T>operator | (const T &value);
365  Image<T>& operator |= (const Image<T> &rhs);
366  Image<T>& operator |= (const T &value);
367  ResImage<T>operator & (const Image<T> &rhs);
368  ResImage<T>operator & (const T &value);
369  Image<T>& operator &= (const Image<T> &rhs);
370  Image<T>& operator &= (const T &value);
371 
375  operator bool();
376 
378  Image<T>& operator << (const lineType &tab);
380  Image<T>& operator << (vector<T> &vect);
382  Image<T>& operator >> (vector<T> &vect);
383 
384 #ifndef SWIG
385  Image<T>& operator << (const char *s);
386 #endif // SWIG
387  inline Image<T>& operator << (const string s) { return this->operator<<(s.c_str()); }
388  Image<T>& operator >> (const char *s);
389  inline Image<T>& operator >> (const string s) { return this->operator>>(s.c_str()); }
390  protected:
391 
392  lineType pixels;
393  sliceType lines;
394  volType slices;
395 
396  RES_T restruct(void);
397 
398  ImageViewer<T> *viewer;
399  void createViewer();
400 
401  T dumPixel;
402  // Specify if the viewer has been created internally
403  // ImageViewerWidget *viewer;
404 
405  public:
407  virtual void setName(const char *_name);
409  virtual void show(const char *_name=NULL, bool labelImage=false);
411  virtual void showLabel(const char *_name=NULL);
412  virtual void showNormal(const char *_name=NULL);
414  virtual void hide();
415 
416  };
417 
418  template <class T>
419  class ResImage : public Image<T>
420  {
421  public:
422  ResImage(const Image<T> &rhs)
423  : Image<T>(rhs, false)
424  {
425  }
426  // Warning ! This will empty the image rhs !
427  // Allows to avoid multiple copy with SwigValueWrapper operations...
428  ResImage(const ResImage<T> &rhs)
429  : Image<T>()
430  {
431  Image<T>::drain(const_cast<ResImage<T>*>(&rhs));
432  }
433  ~ResImage()
434  {
435  }
436  };
437 
438  template <class T>
439  Image<T> *createImage(const T)
440  {
441  return new Image<T>();
442  }
443 
444  template <class T>
445  Image<T> *castBaseImage(BaseImage *img, const T &)
446  {
447  ASSERT(strcmp(getDataTypeAsString<T>(), img->getTypeAsString())==0, "Bad type for cast", NULL);
448  return reinterpret_cast< Image<T>* >(img);
449  }
450 
452  template <class T>
453  RES_T drawOverlay(const Image<T> &imToDraw, Image<T> &imOut)
454  {
455  imOut.getViewer()->drawOverlay(imToDraw);
456  return RES_OK;
457  }
458 
461 } // namespace smil
462 
463 
464 #endif // _DIMAGE_HPP
void toArray(T outArray[])
Copy pixel values to a given array.
ResImage< T > operator>=(const Image< T > &rhs)
Greater or equal boolean operator (see grt())
Definition: DImage.hxx:805
virtual bool isVisible()
Check if the image is visible.
Definition: DImage.hxx:261
virtual RES_T deallocate()
Deallocate image.
Definition: DImage.hxx:395
ResImage< T > operator<=(const Image< T > &rhs)
Lower or equal boolean operator (see lowOrEqu())
Definition: DImage.hxx:773
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:154
Definition: DColorConvert.h:38
void fromString(string pixVals)
Import pixel values from string.
Definition: DImage.hxx:492
sliceType getLines() const
Get an array containing the start offset of each line.
Definition: DImage.hpp:114
virtual RES_T setSize(const vector< UINT > s, bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:230
virtual const char * getTypeAsString()
Get the image type.
Definition: DImage.hpp:98
Definition: DImage.hpp:50
ResImage< T > operator<(const Image< T > &rhs)
Lower boolean operator (see low())
Definition: DImage.hxx:757
virtual const char * getInfoString(const char *indent="") const
Get the description of the image as a string.
Definition: DImage.hpp:258
void toIntArray(int outArray[])
Copy pixel values to a given int array.
ResImage< T > operator>(const Image< T > &rhs)
Greater boolean operator (see grt())
Definition: DImage.hxx:789
void fromIntArray(const int inArray[])
Copy pixel values from a given int array.
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:110
virtual RES_T setSize(const BaseImage &rhs, bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:225
SharedImage< T > getSlice(size_t sliceNum) const
Get a 2D slice of a 3D image. It doesn&#39;t create an image, but returns a 2D SharedImage using the same...
Definition: DImage.hxx:558
Image< T > & operator>>(vector< T > &vect)
Export image data to a vector.
Definition: DImage.hxx:912
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
Definition: DImage.hxx:319
void fromIntVector(const vector< int > inVector)
Copy pixel values from a given int vector.
Definition: DImage.hxx:474
string toString()
Export pixel values to a string.
Definition: DImage.hxx:483
void printSelf(ostream &os, bool displayPixVals, bool hexaGrid=false, string indent="") const
Print a description of the image.
Definition: DImage.hxx:501
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:127
void fromCharArray(const signed char inArray[])
Copy pixel values from a given char array.
virtual RES_T setSize(size_t s[3], bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:220
virtual RES_T allocate()
Allocate image.
Definition: DImage.hxx:347
virtual const char * getTypeAsString()=0
Get the type of the image as a string ("UINT8",...)
virtual Image< T > clone(bool cloneData=true)
Create a clone of the image (with same size and content )
Definition: DImage.hpp:212
void fromArray(const T inArray[])
Copy pixel values from a given array.
vector< int > toIntVector()
Copy pixel values to a given int vector.
Definition: DImage.hxx:465
ResImage< T > operator~() const
Negate image.
Definition: DImage.hxx:605
virtual void hide()
Hide image.
Definition: DImage.hxx:267
Image that uses an existing (1D) data pointer.
Definition: DImage.hpp:53
RES_T setPixel(size_t offset, const T &value)
Set the value of the pixel at a given offset.
Definition: DImage.hpp:160
virtual void * getVoidPointer(void)
Get pixels as a void pointer.
Definition: DImage.hpp:266
ResImage< T > operator==(const Image< T > &rhs)
Equal boolean operator (see equ()).
Definition: DImage.hxx:741
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:91
Base Smil Object.
Definition: DBaseObject.h:53
virtual void modified()
Trigger modified event (allows to force display update)
Definition: DImage.hxx:223
ResImage< T > operator/(const Image< T > &rhs)
Divide by image.
Definition: DImage.hxx:711
ResImage< T > operator+(const Image< T > &rhs)
Add image.
Definition: DImage.hxx:621
virtual void setName(const char *_name)
Set the name of the image.
Definition: DImage.hxx:235
void drain(Image< T > *im, bool deleteSrc=false)
Replace container. Drain memory from image im to this.
Definition: DImage.hxx:124
Base image viewer.
Definition: DBaseImage.h:45
ResImage< T > operator!=(const Image< T > &rhs)
Diff boolean operator (see equ()).
Definition: DImage.hxx:749
virtual RES_T save(const char *fileName)
Save to file.
Definition: DImage.hxx:217
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:87
Base Image class.
Definition: DBaseImage.h:50
Image()
Default constructor.
Definition: DImage.hxx:55
virtual void show(const char *_name=NULL, bool labelImage=false)
Show the default viewer associated with the image.
Definition: DImage.hxx:274
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:145
Definition: DTypes.hpp:78
Main Image class.
Definition: DQVtkViewer.hpp:44
virtual RES_T load(const char *fileName)
Load from file.
Definition: DImage.hxx:211
ResImage< T > operator*(const Image< T > &rhs)
Multiply by image.
Definition: DImage.hxx:681
Image< T > & operator*=(const Image< T > &rhs)
Image multiplication assignment.
Definition: DImage.hxx:697
T getPixel(size_t offset) const
Return the value of the pixel at a given offset.
Definition: DImage.hpp:133
virtual ImageViewer< T > * getViewer()
Get the image viewer (create one if needed)
Definition: DImage.hxx:254
volType getSlices() const
Get an array containing the start offset of each slice.
Definition: DImage.hpp:118
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:95
Image< T > & operator-=(const Image< T > &rhs)
Image subtraction assignment.
Definition: DImage.hxx:667
Image< T > & operator+=(const Image< T > &rhs)
Image addition assignment.
Definition: DImage.hxx:637
virtual void clone(const Image< T > &rhs)
Clone from a given image (set same size and copy content)
Definition: DImage.hxx:155
Image< T > & operator<<(const Image< T > &rhs)
Copy image.
Definition: DImage.hxx:591
Image< T > & operator/=(const Image< T > &rhs)
Image division assignment.
Definition: DImage.hxx:727
virtual void showLabel(const char *_name=NULL)
Show the default viewer associated with the image using a color lookup table.
Definition: DImage.hxx:303