SMIL  1.0.4
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 
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
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 
332  static T getDataTypeMin()
333  {
334  return ImDtTypes<T>::min();
335  }
336 
338  static T getDataTypeMax()
339  {
340  return ImDtTypes<T>::max();
341  }
342 
343  inline T &operator[](size_t i)
344  {
345  if (i < pixelCount)
346  return this->pixels[i];
347  ERR_MSG("Offset out of range.");
348  return this->dumPixel;
349  }
350 
352  Image<T> &operator<<(const Image<T> &rhs);
354  Image<T> &operator<<(const T &value);
357  ResImage<T> operator-() const;
361  ResImage<T> operator+(const T &value);
365  Image<T> &operator+=(const T &value);
369  ResImage<T> operator-(const T &value);
373  Image<T> &operator-=(const T &value);
377  ResImage<T> operator*(const T &value);
381  Image<T> &operator*=(const T &value);
385  ResImage<T> operator/(const T &value);
389  Image<T> &operator/=(const T &value);
395  ResImage<T> operator<(const Image<T> &rhs);
397  ResImage<T> operator<(const T &value);
399  ResImage<T> operator<=(const Image<T> &rhs);
401  ResImage<T> operator<=(const T &value);
405  ResImage<T> operator>(const T &value);
409  ResImage<T> operator>=(const T &value);
410 
411  ResImage<T> operator|(const Image<T> &rhs);
412  ResImage<T> operator|(const T &value);
413  Image<T> &operator|=(const Image<T> &rhs);
414  Image<T> &operator|=(const T &value);
415 
419  ResImage<T> operator&(const T &value);
423  Image<T> &operator&=(const T &value);
424 
429  operator bool();
430 
432  Image<T> &operator<<(const lineType &tab);
434  Image<T> &operator<<(vector<T> &vect);
436  Image<T> &operator>>(vector<T> &vect);
437 
438 #ifndef SWIG
439  Image<T> &operator<<(const char *s);
440 #endif // SWIG
441  inline Image<T> &operator<<(const string s)
442  {
443  return this->operator<<(s.c_str());
444  }
445 
446  Image<T> &operator>>(const char *s);
447  inline Image<T> &operator>>(const string s)
448  {
449  return this->operator>>(s.c_str());
450  }
451 
452  protected:
453  lineType pixels;
454  sliceType lines;
455  volType slices;
456 
457  RES_T restruct(void);
458 
459  ImageViewer<T> *viewer;
460  void createViewer();
461 
462  T dumPixel;
463  // Specify if the viewer has been created internally
464  // ImageViewerWidget *viewer;
465 
466  public:
468  virtual void setName(const char *_name);
469 
471  virtual void show(const char *_name = NULL, bool labelImage = false);
472 
475  virtual void showLabel(const char *_name = NULL);
476 
477  virtual void showNormal(const char *_name = NULL);
478 
480  virtual void hide();
481  };
482 
483  template <class T> class ResImage : public Image<T>
484  {
485  public:
486  ResImage(const Image<T> &rhs) : Image<T>(rhs, false)
487  {
488  }
489 
490  // Warning ! This will empty the image rhs !
491  // Allows to avoid multiple copy with SwigValueWrapper operations...
492  ResImage(const ResImage<T> &rhs) : Image<T>()
493  {
494  Image<T>::drain(const_cast<ResImage<T> *>(&rhs));
495  }
496 
497  ~ResImage()
498  {
499  }
500  };
501 
502  template <class T> Image<T> *createImage(const T)
503  {
504  return new Image<T>();
505  }
506 
507  template <class T> Image<T> *castBaseImage(BaseImage *img, const T &)
508  {
509  ASSERT(strcmp(getDataTypeAsString<T>(), img->getTypeAsString()) == 0,
510  "Bad type for cast", NULL);
511  return reinterpret_cast<Image<T> *>(img);
512  }
513 
515  template <class T>
516  RES_T drawOverlay(const Image<T> &imToDraw, Image<T> &imOut)
517  {
518  imOut.getViewer()->drawOverlay(imToDraw);
519  return RES_OK;
520  }
521 
524 } // namespace smil
525 
526 #endif // _DIMAGE_HPP
Base Image class.
Definition: DBaseImage.h:50
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:90
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:80
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:85
Base Smil Object.
Definition: DBaseObject.h:52
Main Image class.
Definition: DImage.hpp:57
ResImage< T > operator+(const T &value)
Add value.
virtual RES_T save(const char *fileName)
Save to file.
virtual void * getVoidPointer(void)
Get pixels as a void pointer.
Definition: DImage.hpp:291
void fromArray(const T inArray[])
Copy pixel values from a given array.
ResImage< T > operator!=(const Image< T > &rhs)
Diff boolean operator (see equ()).
virtual void clone(const Image< T > &rhs)
Clone from a given image (set same size and copy content)
T getPixel(size_t offset) const
Return the value of the pixel at a given offset.
Definition: DImage.hpp:136
ResImage< T > operator==(const Image< T > &rhs)
Equal boolean operator (see equ()).
virtual Image< T > clone(bool cloneData=true)
Create a clone of the image (with same size and content )
Definition: DImage.hpp:222
virtual const char * getInfoString(const char *indent="") const
Get the description of the image as a string.
Definition: DImage.hpp:283
virtual RES_T load(const char *fileName)
Load from file.
static T getDataTypeMin()
Get Maximum value of image data type.
Definition: DImage.hpp:332
ResImage< T > operator*(const T &value)
Multiply by value.
ResImage< T > operator<(const Image< T > &rhs)
Lower boolean operator (see low())
void fromCharArray(const signed char inArray[])
Copy pixel values from a given char array.
void drain(Image< T > *im, bool deleteSrc=false)
Replace container. Drain memory from image im to this.
string toString()
Export pixel values to a string.
Image< T > & operator*=(const Image< T > &rhs)
Image multiplication assignment.
virtual RES_T setSize(const BaseImage &rhs, bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:237
Image< T > & operator+=(const Image< T > &rhs)
Image addition assignment.
ResImage< T > operator/(const T &value)
Divide by value.
virtual void modified()
Trigger modified event (allows to force display update)
void toArray(T outArray[])
Copy pixel values to a given array.
RES_T setPixel(size_t offset, const T &value)
Set the value of the pixel at a given offset.
Definition: DImage.hpp:164
Image< T > & operator-=(const T &value)
Value subtraction assignment.
ResImage< T > operator<=(const Image< T > &rhs)
Lower or equal boolean operator (see lowOrEqu())
static T getDataTypeMax()
Get Maximum value of image data type.
Definition: DImage.hpp:338
volType getSlices() const
Get an array containing the start offset of each slice.
Definition: DImage.hpp:117
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:105
sliceType getLines() const
Get an array containing the start offset of each line.
Definition: DImage.hpp:111
ResImage< T > operator&(const Image< T > &rhs)
Bitwise and operator.
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
Image()
Default constructor.
Image< T > & operator&=(const Image< T > &rhs)
Bitwise and assignement.
ResImage< T > operator~() const
Negate image.
ResImage< T > operator>(const T &value)
Greater boolean operator (see grt())
virtual RES_T setSize(const vector< UINT > s, bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:243
ResImage< T > operator+(const Image< T > &rhs)
Add image.
Image< T > & operator+=(const T &value)
Value addition assignment.
Image(const Image< T > &rhs, bool cloneData=false)
Copy constructor.
ResImage< T > operator>=(const T &value)
Greater or equal boolean operator (see grt())
virtual void show(const char *_name=NULL, bool labelImage=false)
Show the default viewer associated with the image.
ResImage< T > operator>(const Image< T > &rhs)
Greater boolean operator (see grt())
void fromNumArray(PyObject *array)
fromNumArray() -
void toIntArray(int outArray[])
Copy pixel values to a given int array.
virtual RES_T allocate()
Allocate image.
void toCharArray(signed char outArray[])
Copy pixel values to a given char array.
virtual const char * getTypeAsString()
Get the image type.
Definition: DImage.hpp:93
Image(size_t w, size_t h, size_t d=1)
Contruction with a given size (automatic allocation)
virtual RES_T deallocate()
Deallocate image.
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
virtual void showLabel(const char *_name=NULL)
Show the default viewer associated with the image using a color lookup table.
ResImage< T > operator-(const T &value)
Sub value.
ResImage< T > operator-(const Image< T > &rhs)
Sub image.
Image< T > & operator/=(const T &value)
Value division assignment.
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
void fromString(string pixVals)
Import pixel values from string.
ResImage< T > operator&(const T &value)
Bitwise and operator.
Image< T > & operator-=(const Image< T > &rhs)
Image subtraction assignment.
virtual ImageViewer< T > * getViewer()
Get the image viewer (create one if needed)
virtual bool isVisible()
Check if the image is visible.
void fromIntArray(const int inArray[])
Copy pixel values from a given int array.
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...
virtual void hide()
Hide image.
virtual void setName(const char *_name)
Set the name of the image.
Image(const char *fileName)
Contruction from a file.
void printSelf(ostream &os, bool displayPixVals, bool hexaGrid=false, string indent="") const
Print a description of the image.
ResImage< T > operator*(const Image< T > &rhs)
Multiply by image.
vector< int > toIntVector()
Copy pixel values to a given int vector.
PyObject * getNumArray(bool c_contigous=false)
getNumArray() -
Image< T > & operator<<(const Image< T > &rhs)
Copy image.
ResImage< T > operator>=(const Image< T > &rhs)
Greater or equal boolean operator (see grt())
Image< T > & operator*=(const T &value)
Value multiplication assignment.
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
Image< T > & operator/=(const Image< T > &rhs)
Image division assignment.
Image< T > & operator&=(const T &value)
Bitwise and assignement.
ResImage< T > operator/(const Image< T > &rhs)
Divide by image.
Image< T > & operator>>(vector< T > &vect)
Export image data to a vector.
virtual RES_T setSize(size_t s[3], bool doAllocate=true)
Set the size of image.
Definition: DImage.hpp:232
void fromIntVector(const vector< int > inVector)
Copy pixel values from a given int vector.
Base image viewer.
Definition: DImageViewer.hpp:52
Definition: DImage.hpp:484
Image that uses an existing (1D) data pointer.
Definition: DSharedImage.hpp:51
RES_T drawOverlay(const Image< T > &imToDraw, Image< T > &imOut)
Draw overlay.
Definition: DImage.hpp:516
Definition: DTypes.hpp:88