SMIL  0.9.1
DSharedImage.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 _D_SHARED_IMAGE_HPP
31 #define _D_SHARED_IMAGE_HPP
32 
33 #include "Core/include/private/DImage.hpp"
34 #include "Core/include/DErrors.h"
35 
36 
37 
38 namespace smil
39 {
49  template <class T>
50  class SharedImage : public Image<T>
51  {
52  public:
53  typedef Image<T> parentClass;
54  typedef typename Image<T>::lineType lineType;
55 
58  : Image<T>()
59  {
60  this->className = "SharedImage";
61  attached = false;
62  }
63 
64  SharedImage(const Image<T> &img)
65  : Image<T>()
66  {
67  this->className = "SharedImage";
68  attached = false;
69 
70  this->attach(img);
71  }
72 
73  SharedImage(lineType dataPtr, size_t width, size_t height, size_t depth=1)
74  : Image<T>()
75  {
76  this->className = "SharedImage";
77  attached = false;
78 
79  this->attach(dataPtr, width, height, depth);
80  }
81 
82  SharedImage(const SharedImage<T> &img)
83  : Image<T>()
84  {
85  this->className = "SharedImage";
86  attached = false;
87 
88  this->clone(img);
89  }
90 
91  virtual ~SharedImage()
92  {
93  this->detach();
94  }
95 
96  SharedImage<T>& operator = (const SharedImage<T> &rhs)
97  {
98  attached = false;
99 
100  this->clone(rhs);
101  return *this;
102  }
103 
104  virtual RES_T attach(lineType dataPtr, size_t width, size_t height, size_t depth=1)
105  {
106  if (dataPtr==NULL)
107  {
108  ERR_MSG("Source image isn't allocated");
109  return RES_ERR;
110  }
111  else if (dataPtr==this->pixels && width==this->width
112  && height==this->height
113  && depth==this->depth)
114  {
115  return RES_OK;
116  }
117  else
118  {
119  if (attached)
120  detach();
121 
122  this->pixels = dataPtr;
123  this->setSize(width, height, depth);
124 
125  this->restruct();
126 
127  this->attached = true;
128  this->allocated = true;
129 
130  return RES_OK;
131  }
132  }
133 
134  virtual RES_T attach(const Image<T> &im)
135  {
136  return attach(im.getPixels(), im.getWidth(), im.getHeight(), im.getDepth());
137  }
138 
139  virtual RES_T attach(lineType dataPtr)
140  {
141  return attach(dataPtr, this->width, this->height, this->depth);
142  }
143 
144  virtual RES_T detach()
145  {
146  if (!attached)
147  return RES_OK;
148 
149  if (this->slices)
150  delete[] this->slices;
151  if (this->lines)
152  delete[] this->lines;
153 
154  this->slices = NULL;
155  this->lines = NULL;
156  this->pixels = NULL;
157 
158  this->width = 0;
159  this->height = 0;
160  this->depth = 0;
161 
162  this->attached = false;
163  this->allocated = false;
164 
165  return RES_OK;
166  }
167 
168  using Image<T>::clone;
169  virtual RES_T clone(const SharedImage<T> &rhs)
170  {
171  return attach(rhs.getPixels(), rhs.getWidth(), rhs.getHeight(), rhs.getDepth());
172  }
173 
174 
175  protected:
176  bool attached;
177 
178  virtual RES_T setSize(size_t w, size_t h, size_t d = 1, bool /*doAllocate*/ = true)
179  {
180  this->width = w;
181  this->height = h;
182  this->depth = d;
183 
184  this->sliceCount = d;
185  this->lineCount = d * h;
186  this->pixelCount = this->lineCount * w;
187 
188  if (this->viewer)
189  this->viewer->setImage(*this);
190 
191  this->modified();
192 
193  return RES_OK;
194  }
195 
196  virtual RES_T allocate()
197  {
198  return RES_ERR_BAD_ALLOCATION;
199  }
200 
201  virtual RES_T deallocate()
202  {
203  return RES_ERR_BAD_ALLOCATION;
204  }
205  };
206 
209 } // namespace smil
210 
211 
212 #endif // _D_SHARED_IMAGE_HPP
Definition: DColorConvert.h:38
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:110
virtual RES_T allocate()
Allocate image.
Definition: DSharedImage.hpp:196
Image that uses an existing (1D) data pointer.
Definition: DImage.hpp:53
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:91
virtual void modified()
Trigger modified event (allows to force display update)
Definition: DImage.hxx:223
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool=true)
Set the size of image.
Definition: DSharedImage.hpp:178
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:87
SharedImage()
Default constructor.
Definition: DSharedImage.hpp:57
virtual RES_T deallocate()
Deallocate image.
Definition: DSharedImage.hpp:201
Main Image class.
Definition: DQVtkViewer.hpp:44
size_t getDepth() const
Get image depth (Z)
Definition: DBaseImage.h:95