CImage.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 Francois Conti
40  \version 3.1.1 $Rev: 1869 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CImageH
46 #define CImageH
47 //------------------------------------------------------------------------------
48 #include "graphics/CColor.h"
49 //------------------------------------------------------------------------------
50 namespace chai3d {
51 //------------------------------------------------------------------------------
52 
53 //==============================================================================
60 //==============================================================================
61 
62 //------------------------------------------------------------------------------
63 class cImage;
64 typedef std::shared_ptr<cImage> cImagePtr;
65 //------------------------------------------------------------------------------
66 
67 //==============================================================================
82 //==============================================================================
83 class cImage
84 {
85  //--------------------------------------------------------------------------
86  // CONSTRUCTOR & DESTRUCTOR:
87  //--------------------------------------------------------------------------
88 
89 public:
90 
92  cImage();
93 
95  cImage(const unsigned int a_width,
96  const unsigned int a_height,
97  const GLenum a_format = GL_RGB,
98  const GLenum a_type = GL_UNSIGNED_BYTE);
99 
101  virtual ~cImage();
102 
104  static cImagePtr create() { return (std::make_shared<cImage>()); }
105 
106 
107  //--------------------------------------------------------------------------
108  // PUBLIC METHODS - GENERAL COMMANDS:
109  //--------------------------------------------------------------------------
110 
111 public:
112 
114  cImagePtr copy();
115 
117  bool allocate(const unsigned int a_width,
118  const unsigned int a_height,
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 (1); }
127 
129  inline bool isInitialized() const { return (m_allocated); }
130 
132  void setSize(const unsigned int a_width, const unsigned int a_height);
133 
135  inline unsigned int getWidth() const { return (m_width); }
136 
138  inline unsigned int getHeight() const { return (m_height); }
139 
141  inline GLenum getFormat() const { return (m_format); }
142 
144  inline GLenum getType() const { return (m_type); }
145 
147  inline unsigned int getBitsPerPixel() const { return (8 * m_bytesPerPixel); }
148 
150  inline unsigned int getBytesPerPixel() const { return (m_bytesPerPixel); }
151 
153  inline unsigned int getSizeInBytes() const { return (m_bytesPerPixel * m_width * m_height); }
154 
156  bool convert(const unsigned int a_newFormat);
157 
159  static int queryBytesPerPixel(const GLenum a_format,
160  const GLenum a_type);
161 
162 
163  //--------------------------------------------------------------------------
164  // PUBLIC METHODS - MEMORY DATA:
165  //--------------------------------------------------------------------------
166 
167 public:
168 
170  cImage* getImage() { return (this); }
171 
173  virtual unsigned char* getData() { return (m_data); }
174 
176  virtual void setData(unsigned char* a_data,
177  const unsigned int a_dataSizeInBytes,
178  const bool a_dealloc = false);
179 
181  bool setProperties(const unsigned int a_width,
182  const unsigned int a_height,
183  const GLenum a_format,
184  const GLenum a_type);
185 
186 
187  //--------------------------------------------------------------------------
188  // PUBLIC METHODS - MANIPULATING PIXELS:
189  //--------------------------------------------------------------------------
190 
191 public:
192 
194  virtual void clear();
195 
197  virtual void clear(const cColorb& a_color);
198 
200  virtual void clear(const unsigned char a_r,
201  const unsigned char a_g,
202  const unsigned char a_b,
203  const unsigned char a_a = 0xff) { clear(cColorb(a_r, a_g, a_b, a_a)); }
204 
206  virtual void clear(const unsigned char a_grayLevel) { clear(cColorb(a_grayLevel, a_grayLevel, a_grayLevel)); }
207 
209  virtual void getPixelLocation(const cVector3d a_texCoord, int& a_pixelX, int& a_pixelY) const;
210 
212  virtual bool getPixelColor(const unsigned int a_x,
213  const unsigned int a_y,
214  cColorb& a_color) const;
215 
217  virtual void setPixelColor(const unsigned int a_x,
218  const unsigned int a_y,
219  const cColorb& a_color);
220 
222  virtual void setPixelColor(const unsigned int a_x,
223  const unsigned int a_y,
224  const unsigned char a_r,
225  const unsigned char a_g,
226  const unsigned char a_b) { cColorb color(a_r, a_g, a_b); setPixelColor(a_x, a_y, color); }
227 
229  virtual void setPixelColor(const unsigned int a_x,
230  const unsigned int a_y,
231  const unsigned char a_grayLevel);
232 
234  virtual void setTransparentColor(const cColorb &a_color,
235  const unsigned char a_transparencyLevel);
236 
238  virtual void setTransparentColor(const unsigned char a_r,
239  const unsigned char a_g,
240  const unsigned char a_b,
241  const unsigned char a_transparencyLevel);
242 
244  virtual void setTransparency(const unsigned char a_transparencyLevel);
245 
247  virtual void flipHorizontal();
248 
249 
250  //--------------------------------------------------------------------------
251  // PUBLIC METHODS - MANIPULATING VOXELS:
252  //--------------------------------------------------------------------------
253 
254 public:
255 
257  virtual void getVoxelLocation(const cVector3d a_texCoord, int& a_voxelX, int& a_voxelY, int& a_voxelZ)
258  {
259  getPixelLocation(a_texCoord, a_voxelX, a_voxelY);
260  }
261 
263  virtual bool getVoxelColor(const unsigned int a_x,
264  const unsigned int a_y,
265  const unsigned int a_z,
266  cColorb& a_color) const
267  {
268  return (getPixelColor(a_x, a_y, a_color));
269  }
270 
272  virtual void setVoxelColor(const unsigned int a_x,
273  const unsigned int a_y,
274  const unsigned int a_z,
275  const cColorb& a_color)
276  {
277  setPixelColor(a_x, a_y, a_color);
278  }
279 
281  virtual void setVoxelColor(const unsigned int a_x,
282  const unsigned int a_y,
283  const unsigned int a_z,
284  const unsigned char a_r,
285  const unsigned char a_g,
286  const unsigned char a_b)
287  {
288  cColorb color(a_r, a_g, a_b);
289  setVoxelColor(a_x, a_y, a_z, color);
290  }
291 
293  virtual void setVoxelColor(const unsigned int a_x,
294  const unsigned int a_y,
295  const unsigned int a_z,
296  const unsigned char a_grayLevel)
297  {
298  setPixelColor(a_x, a_y, a_grayLevel);
299  }
300 
301 
302  //--------------------------------------------------------------------------
303  // PUBLIC METHODS - COPYING DATA:
304  //--------------------------------------------------------------------------
305 
306 public:
307 
309  void copyTo(const unsigned int a_sourcePosX,
310  const unsigned int a_sourcePosY,
311  const unsigned int a_sourceSizeX,
312  const unsigned int a_sourceSizeY,
313  cImagePtr a_destImage,
314  const unsigned int a_destPosX = 0,
315  const unsigned int a_destPosY = 0);
316 
318  void copyTo(cImagePtr a_destImage,
319  const unsigned int a_destPosX = 0,
320  const unsigned int a_destPosY = 0);
321 
322 
323  //--------------------------------------------------------------------------
324  // PUBLIC METHODS - MULTIMAGE SELECTION:
325  //--------------------------------------------------------------------------
326 
327 public:
328 
330  virtual unsigned long getCurrentIndex()
331  {
332  return (0);
333  }
334 
336  virtual bool selectImage(unsigned long a_index)
337  {
338  if (a_index == 0)
339  {
340  return(C_SUCCESS);
341  }
342  else
343  {
344  return (C_ERROR);
345  }
346  }
347 
348 
349  //--------------------------------------------------------------------------
350  // PUBLIC METHODS - FILES:
351  //--------------------------------------------------------------------------
352 
353 public:
354 
356  virtual bool loadFromFile(const std::string& a_filename);
357 
359  virtual bool saveToFile(const std::string& a_filename);
360 
362  std::string getFilename() const { return (m_filename); }
363 
364 
365  //--------------------------------------------------------------------------
366  // PUBLIC MEMBERS:
367  //--------------------------------------------------------------------------
368 
369 public:
370 
373 
374 
375  //--------------------------------------------------------------------------
376  // PROTECTED METHODS:
377  //--------------------------------------------------------------------------
378 
379 protected:
380 
382  void defaults();
383 
385  void cleanup();
386 
387 
388  //--------------------------------------------------------------------------
389  // PROTECTED MEMBERS:
390  //--------------------------------------------------------------------------
391 
392 
393 protected:
394 
396  std::string m_filename;
397 
399  unsigned int m_width;
400 
402  unsigned int m_height;
403 
405  GLenum m_format;
406 
408  GLenum m_type;
409 
411  unsigned int m_bytesPerPixel;
412 
414  unsigned char* m_data;
415 
417  unsigned int m_memorySize;
418 
421 
424 };
425 
426 //------------------------------------------------------------------------------
427 } // namespace chai3d
428 //------------------------------------------------------------------------------
429 
430 //------------------------------------------------------------------------------
431 #endif
432 //------------------------------------------------------------------------------
This class implements a 3D vector.
Definition: CVector3d.h:88
virtual void clear()
This method clears all image pixels with a black color.
Definition: CImage.cpp:865
bool setProperties(const unsigned int a_width, const unsigned int a_height, const GLenum a_format, const GLenum a_type)
This method overrides the properties of the image. Use with care!
Definition: CImage.cpp:407
bool convert(const unsigned int a_newFormat)
This method converts the image to a new format passed as argument.
Definition: CImage.cpp:357
GLenum getType() const
This method returns the pixel data type. (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT for instance).
Definition: CImage.h:144
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: CImage.h:263
cImage * getImage()
This method returns a pointer to this cImage object.
Definition: CImage.h:170
virtual void getPixelLocation(const cVector3d a_texCoord, int &a_pixelX, int &a_pixelY) const
This method retrieves a pixel location from a texture coordinate.
Definition: CImage.cpp:938
virtual bool saveToFile(const std::string &a_filename)
This method saves an image file by passing a filename as argument.
Definition: CImage.cpp:1330
unsigned int getBitsPerPixel() const
This method returns the number of bits per pixel used to store this image.
Definition: CImage.h:147
bool isInitialized() const
This method returns true if the image has been allocated in memory, false otherwise.
Definition: CImage.h:129
virtual void setTransparentColor(const cColorb &a_color, const unsigned char a_transparencyLevel)
This method defines a specific pixel color to be transparent.
Definition: CImage.cpp:1117
void cleanup()
This method deletes memory and rid ourselves of any image previously stored.
Definition: CImage.cpp:150
unsigned int getSizeInBytes() const
This method returns the size in bytes of the current image.
Definition: CImage.h:153
bool allocate(const unsigned int a_width, const unsigned int a_height, const GLenum a_format=GL_RGB, const GLenum a_type=GL_UNSIGNED_BYTE)
This method allocates a new image by defining its size, pixel format and pixel type.
Definition: CImage.cpp:174
virtual bool loadFromFile(const std::string &a_filename)
This method loads an image file by passing a filename as argument.
Definition: CImage.cpp:1250
unsigned int getWidth() const
This method returns the width of the image.
Definition: CImage.h:135
static cImagePtr create()
Shared cImage allocator.
Definition: CImage.h:104
const bool C_SUCCESS
Function returns successfully.
Definition: CConstants.h:77
void defaults()
This method initializes all member variables.
Definition: CImage.cpp:114
virtual void setTransparency(const unsigned char a_transparencyLevel)
This method defines a transparency value to be applied to all image pixels.
Definition: CImage.cpp:1162
virtual unsigned int getImageCount() const
This method returns the number of images stored. (1 only for class cImage).
Definition: CImage.h:126
virtual void setVoxelColor(const unsigned int a_x, const unsigned int a_y, const unsigned int a_z, const unsigned char a_grayLevel)
This method sets the gray level of an image voxel at location (x,y,z).
Definition: CImage.h:293
std::string getFilename() const
This method returns the filename from which this image was last loaded or saved.
Definition: CImage.h:362
virtual bool getPixelColor(const unsigned int a_x, const unsigned int a_y, cColorb &a_color) const
This method returns the color of a pixel at location (x,y).
Definition: CImage.cpp:959
bool m_allocated
If true, then the image has been allocated in memory, false otherwise.
Definition: CImage.h:420
unsigned int m_bytesPerPixel
Number of bytes per pixel.
Definition: CImage.h:411
virtual void setData(unsigned char *a_data, const unsigned int a_dataSizeInBytes, const bool a_dealloc=false)
This method modifies the pointer to the actual image data. Use with care!
Definition: CImage.cpp:840
unsigned char * m_data
The image data itself.
Definition: CImage.h:414
unsigned int m_height
Height in pixels of the current image.
Definition: CImage.h:402
static int queryBytesPerPixel(const GLenum a_format, const GLenum a_type)
This method queries the number of bytes per pixel for a given format.
Definition: CImage.cpp:274
unsigned int m_width
Width in pixels of the current image.
Definition: CImage.h:399
const bool C_ERROR
Function returns with an error.
Definition: CConstants.h:74
This class defines a color using a GLubyte representation for each component.
Definition: CColor.h:1184
virtual void setPixelColor(const unsigned int a_x, const unsigned int a_y, const cColorb &a_color)
This method sets the color of a pixel at location (x,y).
Definition: CImage.cpp:1018
std::string m_filename
Image filename.
Definition: CImage.h:396
unsigned int getHeight() const
This method returns the height of the image.
Definition: CImage.h:138
virtual void getVoxelLocation(const cVector3d a_texCoord, int &a_voxelX, int &a_voxelY, int &a_voxelZ)
This method retrieves a voxel location from a texture coordinate.
Definition: CImage.h:257
virtual bool selectImage(unsigned long a_index)
This method sets the current image. For cImage objects, this value is always 0.
Definition: CImage.h:336
GLenum m_format
Pixel format of the image (GL_RGB, GL_RGBA, GL_LUMINANCE).
Definition: CImage.h:405
virtual ~cImage()
Destructor of cImage.
Definition: CImage.cpp:102
virtual void clear(const unsigned char a_r, const unsigned char a_g, const unsigned char a_b, const unsigned char a_a=0xff)
This method clears all image pixels with a color passed as argument.
Definition: CImage.h:200
cColorb m_borderColor
Returned color when accessing pixels located outside of the image.
Definition: CImage.h:372
Implements color properties.
std::shared_ptr< cImage > cImagePtr
Definition: CImage.h:63
GLenum m_type
Pixel data type. (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT).
Definition: CImage.h:408
bool m_responsibleForMemoryAllocation
If true, then this object actually performed the memory allocation for this object.
Definition: CImage.h:423
unsigned int m_memorySize
Size of current image in bytes.
Definition: CImage.h:417
virtual unsigned long getCurrentIndex()
This method returns the index number of the current image. For cImage objects, the value is always 0...
Definition: CImage.h:330
void erase()
This method deletes all image data from memory.
Definition: CImage.h:123
virtual void clear(const unsigned char a_grayLevel)
This method clears all image pixels with a level of gray passed as argument.
Definition: CImage.h:206
Definition: CAudioBuffer.cpp:56
void setSize(const unsigned int a_width, const unsigned int a_height)
This method sets and allocates the size of the image by defining its width and height.
Definition: CImage.cpp:234
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: CImage.h:272
virtual void setPixelColor(const unsigned int a_x, const unsigned int a_y, const unsigned char a_r, const unsigned char a_g, const unsigned char a_b)
This method sets the color of a pixel at location (x,y).
Definition: CImage.h:222
GLenum getFormat() const
This method returns the pixel format of the image (GL_RGB, GL_RGBA, GL_LUMINANCE for instance)...
Definition: CImage.h:141
This class implements a 2D image data structure.
Definition: CImage.h:83
virtual void flipHorizontal()
This method flips this image horizontally.
Definition: CImage.cpp:1195
cImagePtr copy()
This method creates a copy itself.
Definition: CImage.cpp:250
virtual unsigned char * getData()
This method returns a pointer to the actual image data. Use with care!
Definition: CImage.h:173
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: CImage.h:281
cImage()
Default constructor of cImage.
Definition: CImage.cpp:66
void copyTo(const unsigned int a_sourcePosX, const unsigned int a_sourcePosY, const unsigned int a_sourceSizeX, const unsigned int a_sourceSizeY, cImagePtr a_destImage, const unsigned int a_destPosX=0, const unsigned int a_destPosY=0)
This method copies a section of this current image to a destination image.
Definition: CImage.cpp:450
unsigned int getBytesPerPixel() const
This method returns the number of bytes per pixel used to store this image.
Definition: CImage.h:150