SMIL  1.0.4
DImage_RGB.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 _IMAGE_RGB_H
31 #define _IMAGE_RGB_H
32 
33 
34 #include "Core/include/private/DImage.hpp"
35 #include "Base/include/private/DImageArith.hpp"
36 #include "Core/include/DTypes.h"
37 #include "Base/include/private/DImageTransform.hpp"
38 #include "Base/include/private/DMeasures.hpp"
39 
40 namespace smil
41 {
42 
43  template <>
44  inline void Image<RGB>::init()
45  {
46  className = "Image";
47 
48  slices = NULL;
49  lines = NULL;
50  // pixels = NULL;
51 
52  dataTypeSize = sizeof(pixelType);
53 
54  allocatedSize = 0;
55 
56  viewer = NULL;
57  name = "";
58 
59  updatesEnabled = true;
60 
61  parentClass::init();
62  }
63 
64  template <>
65  inline RES_T Image< RGB >::restruct(void)
66  {
67  if (this->slices)
68  delete[] this->slices;
69  if (this->lines)
70  delete[] this->lines;
71 
72  this->lines = new lineType[lineCount];
73  this->slices = new sliceType[sliceCount];
74 
75  sliceType cur_line = this->lines;
76  volType cur_slice = this->slices;
77 
78  size_t pixelsPerSlice = this->width * this->height;
79 
80  for (size_t k=0; k<depth; k++, cur_slice++)
81  {
82  *cur_slice = cur_line;
83 
84  for (size_t j=0; j<height; j++, cur_line++)
85  {
86  for (UINT n=0;n<3;n++)
87  (*cur_line).arrays[n] = pixels.arrays[n] + k*pixelsPerSlice + j*width;
88  }
89  }
90 
91 
92  return RES_OK;
93  }
94 
95 
96 // template <>
97 // inline const char* Image<RGB>::getTypeAsString()
98 // {
99 // if (this->allocated)
100 // return this->pixels.subtypeName.c_str();
101 // else return "RGB";
102 // }
103 
104  template <>
106  {
107  return &pixels;
108  }
109 
110  template <>
111  inline RES_T Image<RGB>::allocate()
112  {
113  if (this->allocated)
114  return RES_ERR_BAD_ALLOCATION;
115 
116 // this->pixels = ImDtTypes<RGB>::createLine(pixelCount);
117  // pixels = new pixelType[pixelCount];
118  this->pixels.createArrays(this->pixelCount);
119 // this->pixels.subtypeName = "RGB";
120 
121  ASSERT((this->pixels.isAllocated()), "Can't allocate image", RES_ERR_BAD_ALLOCATION);
122 
123  this->allocated = true;
124  this->allocatedSize = 3*this->pixelCount*sizeof(UINT8);
125 
126  this->restruct();
127 
128  return RES_OK;
129  }
130 
131  template <>
132  inline RES_T Image<RGB>::deallocate()
133  {
134  if (!this->allocated)
135  return RES_OK;
136 
137  if (this->slices)
138  delete[] this->slices;
139  if (this->lines)
140  delete[] this->lines;
141  if (this->pixels.isAllocated())
142  this->pixels.deleteArrays();
143 // ImDtTypes<RGB>::deleteLine(pixels);
144 
145  this->slices = NULL;
146  this->lines = NULL;
147  for (UINT n=0;n<3;n++)
148  this->pixels.arrays[n] = NULL;
149 
150  this->allocated = false;
151  this->allocatedSize = 0;
152 
153  return RES_OK;
154  }
155 
156  template <>
157  inline double vol(const Image<RGB> &imIn)
158  {
159  if (!imIn.isAllocated())
160  return RES_ERR_BAD_ALLOCATION;
161 
162  int npix = imIn.getPixelCount();
163  ImDtTypes<UINT8>::lineType pixels;
164  double vol = 0;
165 
166  for (UINT n=0;n<3;n++)
167  {
168  pixels = imIn.getPixels().arrays[n];
169  for (int i=0;i<npix;i++)
170  vol += double(pixels[i]);
171  }
172 
173  return vol;
174  }
175 
176  template <>
177  inline Image<RGB>::operator bool()
178  {
179  return vol(*this)==255 * pixelCount * 3;
180  }
181 
182 // template <>
183 // inline std::map<RGB, UINT> histogram(const Image<RGB> &imIn)
184 // {
185 // map<T, UINT> h;
186 // for (T i=ImDtTypes<T>::min();;i++)
187 // {
188 // h.insert(pair<T,UINT>(i, 0));
189 // if (i==ImDtTypes<T>::max())
190 // break;
191 // }
192 //
193 // typename Image<T>::lineType pixels = imIn.getPixels();
194 // for (size_t i=0;i<imIn.getPixelCount();i++)
195 // h[pixels[i]]++;
196 //
197 // return h;
198 // }
199 
200 // template <>
201 // inline void Image<RGB>::printSelf(ostream &os, bool displayPixVals, bool hexaGrid, string indent) const
202 // {
203 // }
204 
205 #ifndef SWIGPYTHON
206  template <>
207  inline char *Image<RGB>::toCharArray()
208  {
209  cout << "Not implemented for RGB images" << endl;
210  return NULL;
211  }
212 #endif // SWIGPYTHON
213 
214 
215 #ifdef USE_QWT
216 // #include "Gui/Qt/DQtImageViewer.hpp"
217 #endif // USE_QWT
218 
219 
220 } // namespace smil
221 
222 #endif // _IMAGE_RGB_H
virtual void * getVoidPointer(void)
Get pixels as a void pointer.
Definition: DImage.hpp:291
virtual RES_T allocate()
Allocate image.
virtual RES_T deallocate()
Deallocate image.
double vol(const Image< T > &imIn)
vol() - Volume of an image
Definition: DMeasures.hpp:129