CMultiImage.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Sebastien Grange
40  \version 3.2.0 $Rev: 2147 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CMultiImageH
46 #define CMultiImageH
47 //------------------------------------------------------------------------------
48 #include "graphics/CImage.h"
49 //------------------------------------------------------------------------------
50 #include <string>
51 #include <vector>
52 //------------------------------------------------------------------------------
53 namespace chai3d {
54 //------------------------------------------------------------------------------
55 
56 //==============================================================================
63 //==============================================================================
64 
65 //------------------------------------------------------------------------------
67 typedef std::shared_ptr<cMultiImage> cMultiImagePtr;
68 //------------------------------------------------------------------------------
69 
70 //==============================================================================
87 //==============================================================================
88 class cMultiImage : public cImage
89 {
90  //--------------------------------------------------------------------------
91  // CONSTRUCTOR & DESTRUCTOR:
92  //--------------------------------------------------------------------------
93 
94 public:
95 
97  cMultiImage();
98 
100  virtual ~cMultiImage();
101 
103  static cMultiImagePtr create() { return (std::make_shared<cMultiImage>()); }
104 
105 
106  //--------------------------------------------------------------------------
107  // PUBLIC METHODS - GENERAL COMMANDS:
108  //--------------------------------------------------------------------------
109 
110 public:
111 
113  cMultiImagePtr copy();
114 
116  bool allocate(const unsigned int a_width,
117  const unsigned int a_height,
118  const unsigned int a_slices,
119  const GLenum a_format = GL_RGB,
120  const GLenum a_type = GL_UNSIGNED_BYTE);
121 
123  void erase() { cleanup(); }
124 
126  virtual unsigned int getImageCount() const { return (unsigned int)(m_imageCount); }
127 
129  bool convert(const unsigned int a_newFormat);
130 
131 
132  //--------------------------------------------------------------------------
133  // PUBLIC METHODS - MULTIMAGE SELECTION:
134  //--------------------------------------------------------------------------
135 
136 public:
137 
139  virtual unsigned long getCurrentIndex() { return m_currentIndex; }
140 
142  virtual bool selectImage(unsigned long a_index);
143 
145  bool addImage(cImage &a_image, unsigned long a_index=-1);
146 
148  bool removeImage(unsigned long a_index);
149 
150 
151  //--------------------------------------------------------------------------
152  // PUBLIC METHODS - MANIPULATING PIXELS:
153  //--------------------------------------------------------------------------
154 
155 public:
156 
158  virtual void setTransparentColor(const cColorb &a_color,
159  const unsigned char a_transparencyLevel);
160 
162  virtual void setTransparentColor(const unsigned char a_r,
163  const unsigned char a_g,
164  const unsigned char a_b,
165  const unsigned char a_transparencyLevel) { cColorb color(a_r, a_g, a_b); setTransparentColor(color, a_transparencyLevel); }
166 
168  virtual void setTransparency(const unsigned char a_transparencyLevel);
169 
171  virtual void flipHorizontal();
172 
173 
174  //--------------------------------------------------------------------------
175  // PUBLIC METHODS - MANIPULATING VOXELS:
176  //--------------------------------------------------------------------------
177 
178 public:
179 
181  virtual void getVoxelLocation(const cVector3d& a_texCoord, int& a_voxelX, int& a_voxelY, int& a_voxelZ, bool a_clampToImageSize = true) const;
182 
184  virtual void getVoxelLocationInterpolated(const cVector3d& a_texCoord, double& a_voxelX, double& a_voxelY, double& a_voxelZ, bool a_clampToImageSize = true) const;
185 
187  virtual unsigned char* getVoxelData(const unsigned int a_x,
188  const unsigned int a_y,
189  const unsigned int a_z) const;
190 
192  virtual bool getVoxelColor(const unsigned int a_x,
193  const unsigned int a_y,
194  const unsigned int a_z,
195  cColorb& a_color) const;
196 
198  virtual bool getVoxelColor(const unsigned int a_x,
199  const unsigned int a_y,
200  const unsigned int a_z,
201  cColorf& a_color) const;
202 
204  virtual bool getVoxelColorInterpolated(const double a_x,
205  const double a_y,
206  const double a_z,
207  cColorb& a_color) const;
208 
210  virtual bool getVoxelColorInterpolated(const double a_x,
211  const double a_y,
212  const double a_z,
213  cColorf& a_color) const;
214 
216  virtual void setVoxelColor(const unsigned int a_x,
217  const unsigned int a_y,
218  const unsigned int a_z,
219  const cColorb& a_color);
220 
222  virtual void setVoxelColor(const unsigned int a_x,
223  const unsigned int a_y,
224  const unsigned int a_z,
225  const unsigned char a_r,
226  const unsigned char a_g,
227  const unsigned char a_b) { cColorb color(a_r, a_g, a_b); setVoxelColor(a_x, a_y, a_z, color); }
228 
230  virtual void setVoxelColor(const unsigned int a_x,
231  const unsigned int a_y,
232  const unsigned int a_z,
233  const unsigned char a_grayLevel);
234 
235 
236  //--------------------------------------------------------------------------
237  // PUBLIC METHODS - MEMORY DATA:
238  //--------------------------------------------------------------------------
239 
240 public:
241 
243  inline unsigned char* getArray() { return (m_array); }
244 
245 
246  //--------------------------------------------------------------------------
247  // PUBLIC METHODS - FILES:
248  //--------------------------------------------------------------------------
249 
250 public:
251 
253  virtual bool loadFromFile(const std::string& a_filename);
254 
256  virtual int loadFromFiles(const std::vector<std::string>& a_filename);
257 
259  virtual int loadFromFiles(const std::string& a_basename, const std::string& a_extension, unsigned long a_max = 9999);
260 
262  virtual bool addFromFile(const std::string& a_filename, unsigned long a_index=-1);
263 
265  virtual bool saveToFiles(const std::string& a_basename, const std::string& a_extension);
266 
267 
268  //--------------------------------------------------------------------------
269  // PROTECTED METHODS:
270  //--------------------------------------------------------------------------
271 
272 protected:
273 
275  void defaults();
276 
278  void cleanup();
279 
281  virtual bool addFromFilePrealloc(const std::string& a_filename, unsigned long a_index);
282 
284  bool addImagePrealloc(cImage &a_image, unsigned long a_index);
285 
286 
287  //--------------------------------------------------------------------------
288  // PROTECTED MEMBERS:
289  //--------------------------------------------------------------------------
290 
291 protected:
292 
294  unsigned char* m_array;
295 
297  unsigned long m_imageCount;
298 
300  unsigned long m_currentIndex;
301 };
302 
303 //------------------------------------------------------------------------------
304 } // namespace chai3d
305 //------------------------------------------------------------------------------
306 
307 //------------------------------------------------------------------------------
308 #endif
309 //------------------------------------------------------------------------------
This class implements a 3D vector.
Definition: CVector3d.h:88
unsigned long m_currentIndex
Index of the currently selected image.
Definition: CMultiImage.h:300
virtual bool saveToFiles(const std::string &a_basename, const std::string &a_extension)
This method saves all images to a set of files.
Definition: CMultiImage.cpp:1263
virtual void flipHorizontal()
This method flips all images horizontally.
Definition: CMultiImage.cpp:1032
virtual void setTransparentColor(const unsigned char a_r, const unsigned char a_g, const unsigned char a_b, const unsigned char a_transparencyLevel)
This method defines a pixel color to be transparent for all images.
Definition: CMultiImage.h:162
virtual unsigned char * getVoxelData(const unsigned int a_x, const unsigned int a_y, const unsigned int a_z) const
This method returns a pointer to voxel memory data.
Definition: CMultiImage.cpp:989
virtual void setVoxelColor(const unsigned int a_x, const unsigned int a_y, const unsigned int a_z, const cColorb &a_color)
This method sets the color of an image voxel at location (x,y,z).
Definition: CMultiImage.cpp:828
This class implements an array of 2D image structures.
Definition: CMultiImage.h:88
cMultiImage()
Default constructor of cMultiImage.
Definition: CMultiImage.cpp:63
cMultiImagePtr copy()
This method creates a copy itself.
Definition: CMultiImage.cpp:205
unsigned long m_imageCount
Number of images contained in the structure.
Definition: CMultiImage.h:297
virtual unsigned int getImageCount() const
This method returns the number of images stored.
Definition: CMultiImage.h:126
bool addImage(cImage &a_image, unsigned long a_index=-1)
This method adds an image to the set if size and format are compatible.
Definition: CMultiImage.cpp:341
virtual bool addFromFilePrealloc(const std::string &a_filename, unsigned long a_index)
Add an image file to a preallocated set if size and format are compatible.
Definition: CMultiImage.cpp:1232
void erase()
This method deletes all image data from memory.
Definition: CMultiImage.h:123
std::shared_ptr< cMultiImage > cMultiImagePtr
Definition: CMultiImage.h:66
virtual void setTransparentColor(const cColorb &a_color, const unsigned char a_transparencyLevel)
This method defines a pixel color for all images to be transparent.
Definition: CMultiImage.cpp:930
virtual void getVoxelLocationInterpolated(const cVector3d &a_texCoord, double &a_voxelX, double &a_voxelY, double &a_voxelZ, bool a_clampToImageSize=true) const
This method retrieves the voxel location from a texture coordinate.
Definition: CMultiImage.cpp:550
unsigned char * m_array
The image array data that holds all images contiguously.
Definition: CMultiImage.h:294
void cleanup()
This method deletes memory and rid ourselves of any image previously stored.
Definition: CMultiImage.cpp:103
virtual bool loadFromFile(const std::string &a_filename)
This method loads an image file.
Definition: CMultiImage.cpp:1055
bool convert(const unsigned int a_newFormat)
This method converts the image to a new format passed as argument.
Definition: CMultiImage.cpp:231
virtual void setVoxelColor(const unsigned int a_x, const unsigned int a_y, const unsigned int a_z, const unsigned char a_r, const unsigned char a_g, const unsigned char a_b)
This method sets the color of an image voxel at location (x,y,z).
Definition: CMultiImage.h:222
virtual bool getVoxelColor(const unsigned int a_x, const unsigned int a_y, const unsigned int a_z, cColorb &a_color) const
This method returns the color of an image voxel at location (x,y,z).
Definition: CMultiImage.cpp:583
virtual bool addFromFile(const std::string &a_filename, unsigned long a_index=-1)
This method adds an image file to the set if size and format are compatible.
Definition: CMultiImage.cpp:1202
This class defines a color using a GLubyte representation for each component.
Definition: CColor.h:1184
virtual ~cMultiImage()
Destructor of cMultiImage.
Definition: CMultiImage.cpp:75
static cMultiImagePtr create()
Shared cMultiImage allocator.
Definition: CMultiImage.h:103
bool removeImage(unsigned long a_index)
This method removes an image from the set.
Definition: CMultiImage.cpp:470
virtual int loadFromFiles(const std::vector< std::string > &a_filename)
This method loads an image set from a set of files.
Definition: CMultiImage.cpp:1145
This class defines a color using a GLfloat representation for each component.
Definition: CColor.h:138
bool allocate(const unsigned int a_width, const unsigned int a_height, const unsigned int a_slices, const GLenum a_format=GL_RGB, const GLenum a_type=GL_UNSIGNED_BYTE)
This method allocates a new multi image array by defining its size, pixel format and pixel type...
Definition: CMultiImage.cpp:132
virtual bool selectImage(unsigned long a_index)
This method sets the current image.
Definition: CMultiImage.cpp:311
Definition: CAudioBuffer.cpp:56
unsigned char * getArray()
This method returns a pointer to the actual image data array. Use with care!
Definition: CMultiImage.h:243
virtual bool getVoxelColorInterpolated(const double a_x, const double a_y, const double a_z, cColorb &a_color) const
This method returns the interpolated color of an image voxel at location (x,y,z). ...
Definition: CMultiImage.cpp:671
bool addImagePrealloc(cImage &a_image, unsigned long a_index)
Add an image to a preallocated set if size and format are compatible.
Definition: CMultiImage.cpp:427
Implements a 2D image data structure.
This class implements a 2D image data structure.
Definition: CImage.h:83
virtual unsigned long getCurrentIndex()
This method returns the index number of the current image.
Definition: CMultiImage.h:139
virtual void setTransparency(const unsigned char a_transparencyLevel)
This method defines a transparent level to all pixels of image set.
Definition: CMultiImage.cpp:959
void defaults()
This method initializes all member variables.
Definition: CMultiImage.cpp:87
virtual void getVoxelLocation(const cVector3d &a_texCoord, int &a_voxelX, int &a_voxelY, int &a_voxelZ, bool a_clampToImageSize=true) const
This method retrieves the nearest voxel location from a texture coordinate.
Definition: CMultiImage.cpp:517