SMIL  0.9.1
DBaseImage.h
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 _D_BASE_IMAGE_H
31 #define _D_BASE_IMAGE_H
32 
33 #include "DBaseObject.h"
34 #include "DSignal.h"
35 #include "DSlot.h"
36 #include "DCommon.h"
37 #include "DErrors.h"
38 
39 #include "Gui/include/private/DImageViewer.hpp"
40 
41 namespace smil
42 {
43  class BaseImageViewer;
44  template <class T>
45  class ImageViewer;
46 
50  class BaseImage : public BaseObject
51  {
52  typedef BaseObject parentClass;
53  public:
54  BaseImage(const char *_className="BaseImage")
55  : BaseObject(_className),
56  updatesEnabled(true),
57  width(0), height(0), depth(0),
58  pixelCount(0), lineCount(0), sliceCount(0),
59  allocated(false),
60  allocatedSize(0)
61  {
62  onModified = Signal(this);
63  onShow = Signal(this);
64  }
65 
66  BaseImage(const BaseImage &rhs)
67  : BaseObject(rhs),
68  updatesEnabled(true),
69  width(0), height(0), depth(0),
70  pixelCount(0), lineCount(0), sliceCount(0),
71  allocated(false),
72  allocatedSize(0)
73  {
74  onModified = Signal(this);
75  onShow = Signal(this);
76  }
77 
78  virtual ~BaseImage();
79 
80  // Forbid implicit assignment operator
81  BaseImage& operator=(const BaseImage &rhs);
82 
83  public:
84 
85  virtual void init();
87  inline size_t getWidth() const {
88  return width;
89  }
91  inline size_t getHeight() const {
92  return height;
93  }
95  inline size_t getDepth() const {
96  return depth;
97  }
98 
100  virtual size_t getAllocatedSize() const {
101  return allocatedSize;
102  }
103 
105  inline UINT getDimension() const
106  {
107  if (depth > 1)
108  return 3;
109  else if (height > 1)
110  return 2;
111  else return 1;
112  }
113 
116  virtual RES_T setSize(size_t w, size_t h, size_t d = 1, bool doAllocate = true) = 0;
117 
118  inline void getSize(size_t *w, size_t *h, size_t *d) const
119  {
120  *w = this->width;
121  *h = this->height;
122  *d = this->depth;
123  }
124 
125 #ifndef SWIGPYTHON
126  inline void getSize(int *w, int *h, int *d) const
127  {
128  *w = this->width;
129  *h = this->height;
130  *d = this->depth;
131  }
132 #endif // SWIGPYTHON
133 
135  inline void getSize(size_t s[3]) const
136  {
137  s[0] = this->width;
138  s[1] = this->height;
139  s[2] = this->depth;
140  }
141 
142  inline void getSize(int s[3]) const
143  {
144  s[0] = this->width;
145  s[1] = this->height;
146  s[2] = this->depth;
147  }
148 
150  inline size_t getPixelCount() const {
151  return this->pixelCount;
152  }
154  inline size_t getLineCount() const {
155  return this->lineCount;
156  }
158  inline size_t getSliceCount() const {
159  return this->sliceCount;
160  }
161 
163  inline bool isAllocated() const {
164  return this->allocated;
165  }
166 
168  virtual void* getVoidPointer() = 0;
170  virtual void modified() = 0;
171 
173  inline size_t getOffsetFromCoords(size_t x, size_t y, size_t z=1) const
174  {
175  if (x>=this->width) return -1;
176  if (y>=this->height) return -1;
177  if (z>=this->depth) return -1;
178  return z*this->width*this->height + y*this->width + x;
179  }
180 
182  inline void getCoordsFromOffset(size_t off, size_t &x, size_t &y, size_t &z) const
183  {
184  z = off / (this->width*this->height);
185  y = (off % (this->width*this->height))/this->width;
186  x = off % this->width;
187  }
188 
190  virtual const char *getInfoString(const char * = "") const { return NULL; }
192  virtual const char* getTypeAsString() = 0;
193 
195  virtual bool isVisible() { return false; }
197  virtual void show(const char* = NULL, bool = false);
199  virtual void showLabel(const char * = NULL);
201  virtual void hide() = 0;
202 
204  virtual RES_T load(const char * /*fileName*/) { return RES_ERR_NOT_IMPLEMENTED; }
206  virtual RES_T save(const char * /*fileName*/) { return RES_ERR_NOT_IMPLEMENTED; }
207 
208 #ifndef SWIG
209  virtual BaseImageViewer *getViewer() = 0;
211 #endif // SWIG
212 
213  bool updatesEnabled;
214  Signal onModified;
215  Signal onShow;
216  protected:
217  size_t dataTypeSize;
218 
219  size_t width;
220  size_t height;
221  size_t depth;
222 
223  size_t pixelCount;
224  size_t lineCount;
225  size_t sliceCount;
226 
227  bool allocated;
228 
229  size_t allocatedSize;
230 
231 
232  };
233 
234 
236  {
237  public:
238  ImageFreezer(BaseImage &im, bool updateOnDelete=true)
239  : image(&im),
240  update(updateOnDelete)
241  {
242  imState = im.updatesEnabled;
243  im.updatesEnabled = false;
244  }
245  ~ImageFreezer()
246  {
247  image->updatesEnabled = imState;
248  if (update)
249  image->modified();
250 
251  }
252  protected:
253  BaseImage *image;
254  bool imState;
255  bool update;
256  };
257 
258 #ifndef SWIG
259 
264  inline bool haveSameSize(const BaseImage *im, ...)
265  {
266  va_list vargs;
267 
268  va_start(vargs, im);
269  if (!im->isAllocated())
270  return false;
271  size_t w = im->getWidth();
272  size_t h = im->getHeight();
273  size_t d = im->getDepth();
274 
275  BaseImage *obj;
276  while ((obj = va_arg(vargs, BaseImage*)))
277  {
278  if (!obj->isAllocated()) return false;
279  if (obj->getWidth()!=w) return false;
280  if (obj->getHeight()!=h) return false;
281  if (obj->getDepth()!=d) return false;
282  }
283  va_end(vargs);
284  return true;
285  }
286 
291  inline bool setSameSize(const BaseImage *im, ...)
292  {
293  if (!im->isAllocated())
294  return false;
295 
296  va_list vargs;
297 
298  va_start(vargs, im);
299  size_t w = im->getWidth();
300  size_t h = im->getHeight();
301  size_t d = im->getDepth();
302 
303  BaseImage *obj;
304 
305  while ((obj = va_arg(vargs, BaseImage*)))
306  {
307  if (obj->getWidth()!=w || obj->getHeight()!=h || obj->getDepth()!=d)
308  if (obj->setSize(w, h, d)!=RES_OK)
309  return false;
310  }
311  return true;
312  }
313 
318  inline bool areAllocated(const BaseImage *im, ...)
319  {
320  va_list vargs;
321 
322  va_start(vargs, im);
323  if (!im->isAllocated())
324  return false;
325 
326  const BaseImage *obj;
327  while ((obj = va_arg(vargs, const BaseImage*)))
328  if (!obj->isAllocated()) return false;
329  va_end(vargs);
330  return true;
331  }
332 
333  #define CHECK_ALLOCATED(...) (areAllocated(__VA_ARGS__, NULL))
334  #define ASSERT_ALLOCATED(...) ASSERT(CHECK_ALLOCATED(__VA_ARGS__), RES_ERR_BAD_ALLOCATION)
335 
336  #define CHECK_SAME_SIZE(...) (Core::getInstance()->autoResizeImages ? setSameSize(__VA_ARGS__, NULL) : haveSameSize(__VA_ARGS__, NULL))
337  #define ASSERT_SAME_SIZE(...) ASSERT(CHECK_SAME_SIZE(__VA_ARGS__), RES_ERR_BAD_SIZE)
338 
339 
340 #endif // SWIG
341 
342 
343 } // namespace smil
344 
345 #endif // _DBASE_IMAGE_H
346 
virtual void showLabel(const char *=NULL)
Show the image (viewer) as false colors.
Definition: DBaseImage.cpp:62
virtual void * getVoidPointer()=0
Get the void* data array.
Definition: DBaseImageViewer.h:46
Definition: DColorConvert.h:38
virtual BaseImageViewer * getViewer()=0
Get the viewer associated to the image.
Definition: DBaseImage.h:235
Definition: DSignal.h:55
virtual void hide()=0
Hide the image (viewer)
virtual RES_T save(const char *)
Save to file.
Definition: DBaseImage.h:206
virtual size_t getAllocatedSize() const
Get memory size (bytes)
Definition: DBaseImage.h:100
virtual const char * getTypeAsString()=0
Get the type of the image as a string ("UINT8",...)
bool isAllocated() const
Check if the image is allocated.
Definition: DBaseImage.h:163
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)=0
Set image size Set image size and allocate it if doAllocate is true.
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:91
virtual const char * getInfoString(const char *="") const
Get the description of the image.
Definition: DBaseImage.h:190
Base Smil Object.
Definition: DBaseObject.h:53
virtual void show(const char *=NULL, bool=false)
Show the image (viewer)
Definition: DBaseImage.cpp:56
virtual bool isVisible()
Check if the image (viewer) is visible.
Definition: DBaseImage.h:195
Base image viewer.
Definition: DBaseImage.h:45
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:87
size_t getPixelCount() const
Get the number of pixels.
Definition: DBaseImage.h:150
Base Image class.
Definition: DBaseImage.h:50
virtual void modified()=0
Trigger modified event.
size_t getOffsetFromCoords(size_t x, size_t y, size_t z=1) const
Get an offset for given x,y(,z) coordinates.
Definition: DBaseImage.h:173
size_t getLineCount() const
Get the number of lines.
Definition: DBaseImage.h:154
size_t getSliceCount() const
Get the number of slices(for 3D images)
Definition: DBaseImage.h:158
void getCoordsFromOffset(size_t off, size_t &x, size_t &y, size_t &z) const
Get x,y(,z) coordinates for a given offset.
Definition: DBaseImage.h:182
UINT getDimension() const
Get dimension (2D or 3D)
Definition: DBaseImage.h:105
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:95
virtual RES_T load(const char *)
Load from file.
Definition: DBaseImage.h:204
void getSize(size_t s[3]) const
Get image size.
Definition: DBaseImage.h:135