SMIL  1.0.4
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''
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 _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> class ImageViewer;
45 
49  class BaseImage : public BaseObject
50  {
51  typedef BaseObject parentClass;
52 
53  public:
54  BaseImage(const char *_className = "BaseImage")
55  : BaseObject(_className), updatesEnabled(true), width(0), height(0),
56  depth(0), pixelCount(0), lineCount(0), sliceCount(0),
57  allocated(false), allocatedSize(0)
58  {
59  onModified = Signal(this);
60  onShow = Signal(this);
61  }
62 
63  BaseImage(const BaseImage &rhs)
64  : BaseObject(rhs), updatesEnabled(true), width(0), height(0), depth(0),
65  pixelCount(0), lineCount(0), sliceCount(0), allocated(false),
66  allocatedSize(0)
67  {
68  onModified = Signal(this);
69  onShow = Signal(this);
70  }
71 
72  virtual ~BaseImage();
73 
74  // Forbid implicit assignment operator
75  BaseImage &operator=(const BaseImage &rhs);
76 
77  public:
78  virtual void init();
80  inline size_t getWidth() const
81  {
82  return width;
83  }
85  inline size_t getHeight() const
86  {
87  return height;
88  }
90  inline size_t getDepth() const
91  {
92  return depth;
93  }
94 
96  virtual size_t getAllocatedSize() const
97  {
98  return allocatedSize;
99  }
100 
102  inline UINT getDimension() const
103  {
104  if (depth > 1)
105  return 3;
106  else if (height > 1)
107  return 2;
108  else
109  return 1;
110  }
111 
114  virtual RES_T setSize(size_t w, size_t h, size_t d = 1,
115  bool doAllocate = true) = 0;
116 
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
127  inline void getSize(int *w, int *h, int *d) const
128  {
129  *w = this->width;
130  *h = this->height;
131  *d = this->depth;
132  }
133 #endif // SWIGPYTHON
134 
136  inline void getSize(size_t s[3]) const
137  {
138  s[0] = this->width;
139  s[1] = this->height;
140  s[2] = this->depth;
141  }
142 
144  inline void getSize(off_t s[3]) const
145  {
146  s[0] = this->width;
147  s[1] = this->height;
148  s[2] = this->depth;
149  }
150 
152  inline void getSize(int s[3]) const
153  {
154  s[0] = this->width;
155  s[1] = this->height;
156  s[2] = this->depth;
157  }
158 
160  inline size_t getPixelCount() const
161  {
162  return this->pixelCount;
163  }
165  inline size_t getLineCount() const
166  {
167  return this->lineCount;
168  }
170  inline size_t getSliceCount() const
171  {
172  return this->sliceCount;
173  }
174 
176  inline bool isAllocated() const
177  {
178  return this->allocated;
179  }
180 
182  virtual void *getVoidPointer() = 0;
184  virtual void modified() = 0;
185 
192  inline bool areCoordsInImage(const off_t x, const off_t y, const off_t z = 0) const
193  {
194  if (x < 0 || y < 0 || z < 0)
195  return false;
196  if (x >= off_t(this->width) || y >= off_t(this->height) || z >= off_t(this->depth))
197  return false;
198  return true;
199  }
200 
209  inline bool areCoordsInImage(const size_t x, const size_t y, const size_t z = 0) const
210  {
211  if (x >= size_t(this->width) || y >= size_t(this->height) || z >= size_t(this->depth))
212  return false;
213  return true;
214  }
215 
224  inline bool isPointInImage(const IntPoint &p) const
225  {
226  if (p.x < 0 || p.y < 0 || p.z < 0)
227  return false;
228  if (p.x >= int(width) || p.y >= int(height) || p.z >= int(depth))
229  return false;
230  return true;
231  }
232 
233 
240  inline bool isOffsetInImage(const off_t offset) const
241  {
242  if (offset < 0)
243  return false;
244  if (offset > off_t(getPixelCount()))
245  return false;
246  return true;
247  }
248 
255  inline bool isOffsetInImage(const size_t offset) const
256  {
257  if (offset > getPixelCount())
258  return false;
259  return true;
260  }
261 
263  inline size_t getOffsetFromCoords(size_t x, size_t y, size_t z = 0) const
264  {
265  size_t vmax = numeric_limits<size_t>::max();
266  if (x >= this->width)
267  return vmax;
268  if (y >= this->height)
269  return vmax;
270  if (z >= this->depth)
271  return vmax;
272  return z * this->width * this->height + y * this->width + x;
273  }
274 
276  inline size_t getOffsetFromPoint(IntPoint &p) const
277  {
278  size_t vmax = numeric_limits<size_t>::max();
279  if (p.x < 0 || p.y < 0 || p.z < 0)
280  return vmax;
281  if (p.x >= int(width))
282  return vmax;
283  if (p.y >= int(height))
284  return vmax;
285  if (p.z >= int(depth))
286  return vmax;
287  return p.z * width * height + p.y * width + p.x;
288  }
289 
291  inline void getCoordsFromOffset(size_t off, size_t &x, size_t &y,
292  size_t &z) const
293  {
294  z = off / (this->width * this->height);
295  y = (off % (this->width * this->height)) / this->width;
296  x = off % this->width;
297  }
298 
300  inline void getCoordsFromOffset(off_t off, off_t &x, off_t &y,
301  off_t &z) const
302  {
303  z = off / (this->width * this->height);
304  y = (off % (this->width * this->height)) / this->width;
305  x = off % this->width;
306  }
307 
309  inline vector<size_t> getCoordsFromOffset(size_t off) const
310  {
311  vector<size_t> coords(3);
312 
313  coords[2] = off / (this->width * this->height);
314  coords[1] = (off % (this->width * this->height)) / this->width;
315  coords[0] = off % this->width;
316  return coords;
317  }
318 
319  inline IntPoint getPointFromOffset(size_t off) const
320  {
321  IntPoint pt;
322 
323  pt.z = off / (this->width * this->height);
324  pt.y = (off % (this->width * this->height)) / this->width;
325  pt.x = off % this->width;
326  return pt;
327  }
328 
330  virtual const char *getInfoString(const char * = "") const
331  {
332  return NULL;
333  }
335  virtual const char *getTypeAsString() = 0;
336 
338  virtual bool isVisible()
339  {
340  return false;
341  }
343  virtual void show(const char * = NULL, bool = false);
345  virtual void showLabel(const char * = NULL);
347  virtual void hide() = 0;
348 
350  virtual RES_T load(const char * /*fileName*/)
351  {
352  return RES_ERR_NOT_IMPLEMENTED;
353  }
355  virtual RES_T save(const char * /*fileName*/)
356  {
357  return RES_ERR_NOT_IMPLEMENTED;
358  }
359 
360 #ifndef SWIG
362  virtual BaseImageViewer *getViewer() = 0;
363 #endif // SWIG
364 
365  bool updatesEnabled;
366  Signal onModified;
367  Signal onShow;
368 
369  protected:
370  size_t dataTypeSize;
371 
372  size_t width;
373  size_t height;
374  size_t depth;
375 
376  size_t pixelCount;
377  size_t lineCount;
378  size_t sliceCount;
379 
380  bool allocated;
381 
382  size_t allocatedSize;
383  };
384 
386  {
387  public:
388  ImageFreezer(BaseImage &im, bool updateOnDelete = true)
389  : image(&im), update(updateOnDelete)
390  {
391  imState = im.updatesEnabled;
392  im.updatesEnabled = false;
393  }
394  ~ImageFreezer()
395  {
396  image->updatesEnabled = imState;
397  if (update)
398  image->modified();
399  }
400 
401  protected:
402  BaseImage *image;
403  bool imState;
404  bool update;
405  };
406 
407 #ifndef SWIG
408 
413  inline bool haveSameSize(const BaseImage *im, ...)
414  {
415  va_list vargs;
416 
417  va_start(vargs, im);
418  if (!im->isAllocated())
419  return false;
420  size_t w = im->getWidth();
421  size_t h = im->getHeight();
422  size_t d = im->getDepth();
423 
424  BaseImage *obj;
425  while ((obj = va_arg(vargs, BaseImage *))) {
426  if (!obj->isAllocated())
427  return false;
428  if (obj->getWidth() != w)
429  return false;
430  if (obj->getHeight() != h)
431  return false;
432  if (obj->getDepth() != d)
433  return false;
434  }
435  va_end(vargs);
436  return true;
437  }
438 
443  inline bool setSameSize(const BaseImage *im, ...)
444  {
445  if (!im->isAllocated())
446  return false;
447 
448  va_list vargs;
449 
450  va_start(vargs, im);
451  size_t w = im->getWidth();
452  size_t h = im->getHeight();
453  size_t d = im->getDepth();
454 
455  BaseImage *obj;
456 
457  while ((obj = va_arg(vargs, BaseImage *))) {
458  if (obj->getWidth() != w || obj->getHeight() != h || obj->getDepth() != d)
459  if (obj->setSize(w, h, d) != RES_OK)
460  return false;
461  }
462  return true;
463  }
464 
469  inline bool areAllocated(const BaseImage *im, ...)
470  {
471  va_list vargs;
472 
473  va_start(vargs, im);
474  if (!im->isAllocated())
475  return false;
476 
477  const BaseImage *obj;
478  while ((obj = va_arg(vargs, const BaseImage *)))
479  if (!obj->isAllocated())
480  return false;
481  va_end(vargs);
482  return true;
483  }
484 
485 #define CHECK_ALLOCATED(...) (areAllocated(__VA_ARGS__, NULL))
486 #define ASSERT_ALLOCATED(...) \
487  ASSERT(CHECK_ALLOCATED(__VA_ARGS__), RES_ERR_BAD_ALLOCATION)
488 
489 #define CHECK_SAME_SIZE(...) \
490  (Core::getInstance()->autoResizeImages ? setSameSize(__VA_ARGS__, NULL) \
491  : haveSameSize(__VA_ARGS__, NULL))
492 #define ASSERT_SAME_SIZE(...) \
493  ASSERT(CHECK_SAME_SIZE(__VA_ARGS__), RES_ERR_BAD_SIZE)
494 
495 #endif // SWIG
496 
497 } // namespace smil
498 
499 #endif // _DBASE_IMAGE_H
Base Image class.
Definition: DBaseImage.h:50
void getSize(off_t s[3]) const
Get image size.
Definition: DBaseImage.h:144
size_t getOffsetFromCoords(size_t x, size_t y, size_t z=0) const
Get an offset for given x,y(,z) coordinates.
Definition: DBaseImage.h:263
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:90
virtual const char * getInfoString(const char *="") const
Get the description of the image.
Definition: DBaseImage.h:330
virtual size_t getAllocatedSize() const
Get memory size (bytes)
Definition: DBaseImage.h:96
bool isOffsetInImage(const off_t offset) const
isOffsetInImage() - checks if a buffer offset in inside the image bounds.
Definition: DBaseImage.h:240
void getCoordsFromOffset(off_t off, off_t &x, off_t &y, off_t &z) const
Get x,y(,z) coordinates for a given offset.
Definition: DBaseImage.h:300
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:291
virtual void * getVoidPointer()=0
Get the void* data array.
virtual void hide()=0
Hide the image (viewer)
bool isAllocated() const
Check if the image is allocated.
Definition: DBaseImage.h:176
void getSize(size_t s[3]) const
Get image size.
Definition: DBaseImage.h:136
void getSize(size_t *w, size_t *h, size_t *d) const
Get image size.
Definition: DBaseImage.h:118
bool areCoordsInImage(const off_t x, const off_t y, const off_t z=0) const
areCoordsInImage() - checks if the triplet (x, y, z) in inside the image bounds.
Definition: DBaseImage.h:192
bool areCoordsInImage(const size_t x, const size_t y, const size_t z=0) const
areCoordsInImage() - checks if the triplet (x, y, z) in inside the image bounds.
Definition: DBaseImage.h:209
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 getSliceCount() const
Get the number of slices(for 3D images)
Definition: DBaseImage.h:170
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:80
virtual RES_T save(const char *)
Save to file.
Definition: DBaseImage.h:355
virtual void show(const char *=NULL, bool=false)
Show the image (viewer)
Definition: DBaseImage.cpp:56
size_t getLineCount() const
Get the number of lines.
Definition: DBaseImage.h:165
virtual void modified()=0
Trigger modified event.
virtual bool isVisible()
Check if the image (viewer) is visible.
Definition: DBaseImage.h:338
virtual BaseImageViewer * getViewer()=0
Get the viewer associated to the image.
virtual void showLabel(const char *=NULL)
Show the image (viewer) as false colors.
Definition: DBaseImage.cpp:62
size_t getPixelCount() const
Get the number of pixels.
Definition: DBaseImage.h:160
bool isOffsetInImage(const size_t offset) const
isOffsetInImage() - checks if a buffer offset in inside the image bounds.
Definition: DBaseImage.h:255
UINT getDimension() const
Get dimension (2D or 3D)
Definition: DBaseImage.h:102
virtual const char * getTypeAsString()=0
Get the type of the image as a string ("UINT8",...)
vector< size_t > getCoordsFromOffset(size_t off) const
Get x,y(,z) coordinates for a given offset.
Definition: DBaseImage.h:309
size_t getOffsetFromPoint(IntPoint &p) const
Get an offset for given x,y(,z) coordinates.
Definition: DBaseImage.h:276
void getSize(int s[3]) const
Get image size.
Definition: DBaseImage.h:152
virtual RES_T load(const char *)
Load from file.
Definition: DBaseImage.h:350
bool isPointInImage(const IntPoint &p) const
isPointInImage() - checks if a Point is in inside the image bounds.
Definition: DBaseImage.h:224
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:85
Definition: DBaseImageViewer.h:47
Base Smil Object.
Definition: DBaseObject.h:52
Definition: DBaseImage.h:386
Definition: DSignal.h:56