My Project
Loading...
Searching...
No Matches
CImage.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, 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.3.0
41*/
42//==============================================================================
43
44//------------------------------------------------------------------------------
45#ifndef CImageH
46#define CImageH
47//------------------------------------------------------------------------------
48#include "graphics/CColor.h"
49//------------------------------------------------------------------------------
50namespace chai3d {
51//------------------------------------------------------------------------------
52
53//==============================================================================
60//==============================================================================
61
62//------------------------------------------------------------------------------
63class cImage;
64typedef std::shared_ptr<cImage> cImagePtr;
65//------------------------------------------------------------------------------
66
67//==============================================================================
82//==============================================================================
83class cImage
84{
85 //--------------------------------------------------------------------------
86 // CONSTRUCTOR & DESTRUCTOR:
87 //--------------------------------------------------------------------------
88
89public:
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
111public:
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
167public:
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
191public:
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, bool a_clampToImageSize = true) const;
210
212 virtual void getPixelLocationInterpolated(const cVector3d& a_texCoord, double& a_pixelX, double& a_pixelY, bool a_clampToImageSize = true) const;
213
215 virtual bool getPixelColor(const unsigned int a_x,
216 const unsigned int a_y,
217 cColorb& a_color) const;
218
220 virtual bool getPixelColor(const unsigned int a_x,
221 const unsigned int a_y,
222 cColorf& a_color) const;
223
225 virtual bool getPixelColorInterpolated(const double a_x,
226 const double a_y,
227 cColorb& a_color) const;
228
230 virtual bool getPixelColorInterpolated(const double a_x,
231 const double a_y,
232 cColorf& a_color) const;
233
235 virtual void setPixelColor(const unsigned int a_x,
236 const unsigned int a_y,
237 const cColorb& a_color);
238
240 virtual void setPixelColor(const unsigned int a_x,
241 const unsigned int a_y,
242 const unsigned char a_r,
243 const unsigned char a_g,
244 const unsigned char a_b) { cColorb color(a_r, a_g, a_b); setPixelColor(a_x, a_y, color); }
245
247 virtual void setPixelColor(const unsigned int a_x,
248 const unsigned int a_y,
249 const unsigned char a_grayLevel);
250
252 virtual void setTransparentColor(const cColorb &a_color,
253 const unsigned char a_transparencyLevel);
254
256 virtual void setTransparentColor(const unsigned char a_r,
257 const unsigned char a_g,
258 const unsigned char a_b,
259 const unsigned char a_transparencyLevel);
260
262 virtual void setTransparency(const unsigned char a_transparencyLevel);
263
265 virtual void flipHorizontal();
266
267
268 //--------------------------------------------------------------------------
269 // PUBLIC METHODS - MANIPULATING VOXELS:
270 //--------------------------------------------------------------------------
271
272public:
273
275 virtual void getVoxelLocation(const cVector3d& a_texCoord, int& a_voxelX, int& a_voxelY, int& a_voxelZ, bool a_clampToImageSize = true) const
276 {
277 getPixelLocation(a_texCoord, a_voxelX, a_voxelY, a_clampToImageSize);
278 a_voxelZ = 0;
279 }
280
282 virtual void getVoxelLocationInterpolated(const cVector3d& a_texCoord, double& a_voxelX, double& a_voxelY, double& a_voxelZ, bool a_clampToImageSize = true) const
283 {
284 getPixelLocationInterpolated(a_texCoord, a_voxelX, a_voxelY, a_clampToImageSize);
285 a_voxelZ = 0.0;
286 }
287
289 virtual bool getVoxelColor(const unsigned int a_x,
290 const unsigned int a_y,
291 const unsigned int a_z,
292 cColorb& a_color) const
293 {
294 return (getPixelColor(a_x, a_y, a_color));
295 }
296
298 virtual bool getVoxelColorInterpolated(const double a_x,
299 const double a_y,
300 const double a_z,
301 cColorb& a_color) const
302 {
303 return (getPixelColorInterpolated(a_x, a_y, a_color));
304 }
305
307 virtual bool getVoxelColorInterpolated(const double a_x,
308 const double a_y,
309 const double a_z,
310 cColorf& a_color) const
311 {
312 return (getPixelColorInterpolated(a_x, a_y, a_color));
313 }
314
316 virtual void setVoxelColor(const unsigned int a_x,
317 const unsigned int a_y,
318 const unsigned int a_z,
319 const cColorb& a_color)
320 {
321 setPixelColor(a_x, a_y, a_color);
322 }
323
325 virtual void setVoxelColor(const unsigned int a_x,
326 const unsigned int a_y,
327 const unsigned int a_z,
328 const unsigned char a_r,
329 const unsigned char a_g,
330 const unsigned char a_b)
331 {
332 cColorb color(a_r, a_g, a_b);
333 setVoxelColor(a_x, a_y, a_z, color);
334 }
335
337 virtual void setVoxelColor(const unsigned int a_x,
338 const unsigned int a_y,
339 const unsigned int a_z,
340 const unsigned char a_grayLevel)
341 {
342 setPixelColor(a_x, a_y, a_grayLevel);
343 }
344
345
346 //--------------------------------------------------------------------------
347 // PUBLIC METHODS - COPYING DATA:
348 //--------------------------------------------------------------------------
349
350public:
351
353 void copyTo(const unsigned int a_sourcePosX,
354 const unsigned int a_sourcePosY,
355 const unsigned int a_sourceSizeX,
356 const unsigned int a_sourceSizeY,
357 cImagePtr a_destImage,
358 const unsigned int a_destPosX = 0,
359 const unsigned int a_destPosY = 0);
360
362 void copyTo(cImagePtr a_destImage,
363 const unsigned int a_destPosX = 0,
364 const unsigned int a_destPosY = 0);
365
366
367 //--------------------------------------------------------------------------
368 // PUBLIC METHODS - MULTIMAGE SELECTION:
369 //--------------------------------------------------------------------------
370
371public:
372
374 virtual unsigned long getCurrentIndex()
375 {
376 return (0);
377 }
378
380 virtual bool selectImage(unsigned long a_index)
381 {
382 if (a_index == 0)
383 {
384 return(C_SUCCESS);
385 }
386 else
387 {
388 return (C_ERROR);
389 }
390 }
391
392
393 //--------------------------------------------------------------------------
394 // PUBLIC METHODS - FILES:
395 //--------------------------------------------------------------------------
396
397public:
398
400 virtual bool loadFromFile(const std::string& a_filename);
401
403 virtual bool saveToFile(const std::string& a_filename);
404
406 std::string getFilename() const { return (m_filename); }
407
408
409 //--------------------------------------------------------------------------
410 // PUBLIC MEMBERS:
411 //--------------------------------------------------------------------------
412
413public:
414
417
418
419 //--------------------------------------------------------------------------
420 // PROTECTED METHODS:
421 //--------------------------------------------------------------------------
422
423protected:
424
426 void defaults();
427
429 void cleanup();
430
431
432 //--------------------------------------------------------------------------
433 // PROTECTED MEMBERS:
434 //--------------------------------------------------------------------------
435
436
437protected:
438
440 std::string m_filename;
441
443 unsigned int m_width;
444
446 unsigned int m_height;
447
449 GLenum m_format;
450
452 GLenum m_type;
453
455 unsigned int m_bytesPerPixel;
456
458 unsigned char* m_data;
459
461 unsigned int m_memorySize;
462
465
468};
469
470//------------------------------------------------------------------------------
471} // namespace chai3d
472//------------------------------------------------------------------------------
473
474//------------------------------------------------------------------------------
475#endif
476//------------------------------------------------------------------------------
Implements color properties.
This class implements a 2D image data structure.
Definition CImage.h:84
bool m_allocated
If true, then the image has been allocated in memory, false otherwise.
Definition CImage.h:464
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:841
virtual bool getPixelColorInterpolated(const double a_x, const double a_y, cColorb &a_color) const
This method returns the interpolated color of an image pixel at location (x,y).
Definition CImage.cpp:1086
cImage * getImage()
This method returns a pointer to this cImage object.
Definition CImage.h:170
virtual unsigned char * getData()
This method returns a pointer to the actual image data. Use with care!
Definition CImage.h:173
virtual void flipHorizontal()
This method flips this image horizontally.
Definition CImage.cpp:1370
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:325
unsigned int getBitsPerPixel() const
This method returns the number of bits per pixel used to store this image.
Definition CImage.h:147
GLenum getType() const
This method returns the pixel data type. (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT for instance).
Definition CImage.h:144
virtual bool selectImage(unsigned long a_index)
This method sets the current image. For cImage objects, this value is always 0.
Definition CImage.h:380
virtual bool loadFromFile(const std::string &a_filename)
This method loads an image file by passing a filename as argument.
Definition CImage.cpp:1425
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:1193
void cleanup()
This method deletes memory and rid ourselves of any image previously stored.
Definition CImage.cpp:151
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:275
virtual void setTransparency(const unsigned char a_transparencyLevel)
This method defines a transparency value to be applied to all image pixels.
Definition CImage.cpp:1337
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:240
unsigned int getBytesPerPixel() const
This method returns the number of bytes per pixel used to store this image.
Definition CImage.h:150
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:374
std::string getFilename() const
This method returns the filename from which this image was last loaded or saved.
Definition CImage.h:406
virtual ~cImage()
Destructor of cImage.
Definition CImage.cpp:103
unsigned char * m_data
The image data itself.
Definition CImage.h:458
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
virtual unsigned int getImageCount() const
This method returns the number of images stored. (1 only for class cImage).
Definition CImage.h:126
unsigned int getWidth() const
This method returns the width of the image.
Definition CImage.h:135
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 CImage.h:298
unsigned int getSizeInBytes() const
This method returns the size in bytes of the current image.
Definition CImage.h:153
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:316
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:175
GLenum m_type
Pixel data type. (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT).
Definition CImage.h:452
cColorb m_borderColor
Returned color when accessing pixels located outside of the image.
Definition CImage.h:416
cImage()
Default constructor of cImage.
Definition CImage.cpp:67
bool m_responsibleForMemoryAllocation
If true, then this object actually performed the memory allocation for this object.
Definition CImage.h:467
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:408
bool convert(const unsigned int a_newFormat)
This method converts the image to a new format passed as argument.
Definition CImage.cpp:358
std::string m_filename
Image filename.
Definition CImage.h:440
GLenum m_format
Pixel format of the image (GL_RGB, GL_RGBA, GL_LUMINANCE).
Definition CImage.h:449
virtual void getPixelLocationInterpolated(const cVector3d &a_texCoord, double &a_pixelX, double &a_pixelY, bool a_clampToImageSize=true) const
This method retrieves a pixel location from a texture coordinate.
Definition CImage.cpp:972
virtual bool saveToFile(const std::string &a_filename)
This method saves an image file by passing a filename as argument.
Definition CImage.cpp:1525
GLenum getFormat() const
This method returns the pixel format of the image (GL_RGB, GL_RGBA, GL_LUMINANCE for instance).
Definition CImage.h:141
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:337
cImagePtr copy()
This method creates a copy itself.
Definition CImage.cpp:251
static cImagePtr create()
Shared cImage allocator.
Definition CImage.h:104
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:1004
unsigned int m_width
Width in pixels of the current image.
Definition CImage.h:443
unsigned int m_memorySize
Size of current image in bytes.
Definition CImage.h:461
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
bool isInitialized() const
This method returns true if the image has been allocated in memory, false otherwise.
Definition CImage.h:129
void erase()
This method deletes all image data from memory.
Definition CImage.h:123
virtual void clear()
This method clears all image pixels with a black color.
Definition CImage.cpp:866
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:289
unsigned int m_bytesPerPixel
Number of bytes per pixel.
Definition CImage.h:455
virtual bool getVoxelColorInterpolated(const double a_x, const double a_y, const double a_z, cColorf &a_color) const
This method returns the interpolated color of an image voxel at location (x,y,z).
Definition CImage.h:307
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:451
virtual void getPixelLocation(const cVector3d &a_texCoord, int &a_pixelX, int &a_pixelY, bool a_clampToImageSize=true) const
This method retrieves the nearest pixel location from a texture coordinate.
Definition CImage.cpp:940
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 CImage.h:282
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 CImage.h:275
unsigned int getHeight() const
This method returns the height of the image.
Definition CImage.h:138
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:235
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:1292
unsigned int m_height
Height in pixels of the current image.
Definition CImage.h:446
void defaults()
This method initializes all member variables.
Definition CImage.cpp:115
Definition CAudioBuffer.cpp:57
const bool C_ERROR
Function returns with an error.
Definition CConstants.h:74
std::shared_ptr< cImage > cImagePtr
Definition CImage.h:64
const bool C_SUCCESS
Function returns successfully.
Definition CConstants.h:77
This class defines a color using a GLubyte representation for each component.
Definition CColor.h:1185
This class defines a color using a GLfloat representation for each component.
Definition CColor.h:139
This class implements a 3D vector.
Definition CVector3d.h:89