chai3d::cImage Class Reference

This class implements a 2D image data structure. More...

#include <CImage.h>

Inheritance diagram for chai3d::cImage:

Public Member Functions

 cImage ()
 Default constructor of cImage. More...
 
 cImage (const unsigned int a_width, const unsigned int a_height, const GLenum a_format=GL_RGB, const GLenum a_type=GL_UNSIGNED_BYTE)
 Constructor of cImage. Initializes an image by passing its width, height, pixel format and pixel type. More...
 
virtual ~cImage ()
 Destructor of cImage. More...
 
cImagePtr copy ()
 This method creates a copy itself. More...
 
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. More...
 
void erase ()
 This method deletes all image data from memory. More...
 
virtual unsigned int getImageCount () const
 This method returns the number of images stored. (1 only for class cImage). More...
 
bool isInitialized () const
 This method returns true if the image has been allocated in memory, false otherwise. More...
 
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. More...
 
unsigned int getWidth () const
 This method returns the width of the image. More...
 
unsigned int getHeight () const
 This method returns the height of the image. More...
 
GLenum getFormat () const
 This method returns the pixel format of the image (GL_RGB, GL_RGBA, GL_LUMINANCE for instance). More...
 
GLenum getType () const
 This method returns the pixel data type. (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT for instance). More...
 
unsigned int getBitsPerPixel () const
 This method returns the number of bits per pixel used to store this image. More...
 
unsigned int getBytesPerPixel () const
 This method returns the number of bytes per pixel used to store this image. More...
 
unsigned int getSizeInBytes () const
 This method returns the size in bytes of the current image. More...
 
bool convert (const unsigned int a_newFormat)
 This method converts the image to a new format passed as argument. More...
 
cImagegetImage ()
 This method returns a pointer to this cImage object. More...
 
virtual unsigned char * getData ()
 This method returns a pointer to the actual image data. Use with care! More...
 
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! More...
 
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! More...
 
virtual void clear ()
 This method clears all image pixels with a black color. More...
 
virtual void clear (const cColorb &a_color)
 This method clears all image pixels with a color passed as argument. More...
 
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. More...
 
virtual void clear (const unsigned char a_grayLevel)
 This method clears all image pixels with a level of gray passed as argument. More...
 
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. More...
 
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. More...
 
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). More...
 
virtual bool getPixelColor (const unsigned int a_x, const unsigned int a_y, cColorf &a_color) const
 This method returns the color of a pixel at location (x,y). More...
 
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). More...
 
virtual bool getPixelColorInterpolated (const double a_x, const double a_y, cColorf &a_color) const
 This method returns the interpolated color of an image pixel at location (x,y). More...
 
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). More...
 
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). More...
 
virtual void setPixelColor (const unsigned int a_x, const unsigned int a_y, const unsigned char a_grayLevel)
 This method sets the gray scale of a pixel at location (x,y). More...
 
virtual void setTransparentColor (const cColorb &a_color, const unsigned char a_transparencyLevel)
 This method defines a specific pixel color to be transparent. More...
 
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 specific pixel color to be transparent. More...
 
virtual void setTransparency (const unsigned char a_transparencyLevel)
 This method defines a transparency value to be applied to all image pixels. More...
 
virtual void flipHorizontal ()
 This method flips this image horizontally. More...
 
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. More...
 
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. More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
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). More...
 
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. More...
 
void copyTo (cImagePtr a_destImage, const unsigned int a_destPosX=0, const unsigned int a_destPosY=0)
 This method copies the entire image to a destination image. More...
 
virtual unsigned long getCurrentIndex ()
 This method returns the index number of the current image. For cImage objects, the value is always 0. More...
 
virtual bool selectImage (unsigned long a_index)
 This method sets the current image. For cImage objects, this value is always 0. More...
 
virtual bool loadFromFile (const std::string &a_filename)
 This method loads an image file by passing a filename as argument. More...
 
virtual bool saveToFile (const std::string &a_filename)
 This method saves an image file by passing a filename as argument. More...
 
std::string getFilename () const
 This method returns the filename from which this image was last loaded or saved. More...
 

Static Public Member Functions

static cImagePtr create ()
 Shared cImage allocator. More...
 
static int queryBytesPerPixel (const GLenum a_format, const GLenum a_type)
 This method queries the number of bytes per pixel for a given format. More...
 

Public Attributes

cColorb m_borderColor
 Returned color when accessing pixels located outside of the image. More...
 

Protected Member Functions

void defaults ()
 This method initializes all member variables. More...
 
void cleanup ()
 This method deletes memory and rid ourselves of any image previously stored. More...
 

Protected Attributes

std::string m_filename
 Image filename. More...
 
unsigned int m_width
 Width in pixels of the current image. More...
 
unsigned int m_height
 Height in pixels of the current image. More...
 
GLenum m_format
 Pixel format of the image (GL_RGB, GL_RGBA, GL_LUMINANCE). More...
 
GLenum m_type
 Pixel data type. (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT). More...
 
unsigned int m_bytesPerPixel
 Number of bytes per pixel. More...
 
unsigned char * m_data
 The image data itself. More...
 
unsigned int m_memorySize
 Size of current image in bytes. More...
 
bool m_allocated
 If true, then the image has been allocated in memory, false otherwise. More...
 
bool m_responsibleForMemoryAllocation
 If true, then this object actually performed the memory allocation for this object. More...
 

Detailed Description

This class provides support for 2D images of the following formats: GL_LUMINANCE, GL_RGB, and GL_RGBA.
Several file formats are also supported for loading and saving images to disk. These include bmp, gif, jpg, png, ppm, and raw.

Constructor & Destructor Documentation

chai3d::cImage::cImage ( )

Default Constructor of cImage.

chai3d::cImage::cImage ( const unsigned int  a_width,
const unsigned int  a_height,
const GLenum  a_format = GL_RGB,
const GLenum  a_type = GL_UNSIGNED_BYTE 
)

Constructor of cImage. Allocates the image by passing width, height and pixel format.

Parameters
a_widthWidth of image.
a_heightHeight of image.
a_formatPixel format. Accepted values are: GL_LUMINANCE, GL_RGB, GL_RGBA.
a_typePixel type. Accepted values are: GL_UNSIGNED_BYTE, GL_UNSIGNED_INT.
chai3d::cImage::~cImage ( )
virtual

Destructor of cImage.

Member Function Documentation

static cImagePtr chai3d::cImage::create ( )
inlinestatic
cImagePtr chai3d::cImage::copy ( )

This method creates a copy of itself.

Returns
Pointer to new object.
bool chai3d::cImage::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 width, height and pixel format.

Parameters
a_widthWidth of image
a_heightHeight of image
a_formatPixel format. Accepted values are: GL_LUMINANCE, GL_RGB, GL_RGBA
a_typePixel type. Accepted values are: GL_UNSIGNED_BYTE, GL_UNSIGNED_INT
void chai3d::cImage::erase ( )
inline
virtual unsigned int chai3d::cImage::getImageCount ( ) const
inlinevirtual

Reimplemented in chai3d::cMultiImage.

bool chai3d::cImage::isInitialized ( ) const
inline
void chai3d::cImage::setSize ( const unsigned int  a_width,
const unsigned int  a_height 
)

This method sets the size of image by defining the width and height.

Parameters
a_widthWidth of image.
a_heightHeight of image.
unsigned int chai3d::cImage::getWidth ( ) const
inline
unsigned int chai3d::cImage::getHeight ( ) const
inline
GLenum chai3d::cImage::getFormat ( ) const
inline
GLenum chai3d::cImage::getType ( ) const
inline
unsigned int chai3d::cImage::getBitsPerPixel ( ) const
inline
unsigned int chai3d::cImage::getBytesPerPixel ( ) const
inline
unsigned int chai3d::cImage::getSizeInBytes ( ) const
inline
bool chai3d::cImage::convert ( const unsigned int  a_newFormat)

This method converts this image into a new pixel format.

Parameters
a_newFormatNew desired pixel format: GL_LUMINANCE, GL_RGB, GL_RGBA.
Returns
true if operation succeeds, false otherwise.
int chai3d::cImage::queryBytesPerPixel ( const GLenum  a_format,
const GLenum  a_type 
)
static

This method queries the number of bytes per pixel for a given format.

Parameters
a_formatPixel format of image.
a_typePixel data type of image.
Returns
Number of bytes used for storing a single pixel.
cImage* chai3d::cImage::getImage ( )
inline
virtual unsigned char* chai3d::cImage::getData ( )
inlinevirtual
void chai3d::cImage::setData ( unsigned char *  a_data,
const unsigned int  a_dataSizeInBytes,
const bool  a_dealloc = false 
)
virtual

This method assigns a different memory location for the image data. Use with care!
Make sure to call function setProperties() afterwards in order to correctly set the dimension and pixel format of the image described by the new data.

Parameters
a_dataPointer to new image data.
a_dataSizeInBytesSize in byte of the image data.
a_deallocIf true then this class is responsible for deallocating image data when the object is deleted.
bool chai3d::cImage::setProperties ( const unsigned int  a_width,
const unsigned int  a_height,
const GLenum  a_format,
const GLenum  a_type 
)

This method assigns new properties including width, height and pixel format to the image. If the requested image properties do not match the exact amount of allocated data image, then the operation fails and the desired properties are ignored.

Parameters
a_widthWidth of new image.
a_heightHeight of new image.
a_formatPixel format. Accepted values are (GL_LUMINANCE, GL_RGB, GL_RGBA).
a_typePixel data type. (GL_UNSIGNED_BYTE, GL_UNSIGNED_INT for instance).
Returns
true if operation succeeds, false otherwise.
void chai3d::cImage::clear ( )
virtual

This method clears an image with defaults data.

void chai3d::cImage::clear ( const cColorb a_color)
virtual

This method clears an image with a defined color passed as argument.

Parameters
a_colorNew color of the image.
virtual void chai3d::cImage::clear ( const unsigned char  a_r,
const unsigned char  a_g,
const unsigned char  a_b,
const unsigned char  a_a = 0xff 
)
inlinevirtual
virtual void chai3d::cImage::clear ( const unsigned char  a_grayLevel)
inlinevirtual
void chai3d::cImage::getPixelLocation ( const cVector3d a_texCoord,
int &  a_pixelX,
int &  a_pixelY,
bool  a_clampToImageSize = true 
) const
virtual

This method retrieves the nearest pixel location from a texture coordinate.
Each texture coordinate is a value defined between 0.0 and 1.0.

Parameters
a_texCoordTexture coordinate.
a_pixelXReturn value for pixel coordinate X.
a_pixelYReturn value for pixel coordinate Y.
a_clampToImageSizeIf true then pixel value is clamped to image size.
void chai3d::cImage::getPixelLocationInterpolated ( const cVector3d a_texCoord,
double &  a_pixelX,
double &  a_pixelY,
bool  a_clampToImageSize = true 
) const
virtual

This method retrieves a pixel location from a texture coordinate.
Each texture coordinate is a value defined between 0.0 and 1.0.

Parameters
a_texCoordTexture coordinate.
a_pixelXReturn value for pixel coordinate X.
a_pixelYReturn value for pixel coordinate Y.
a_clampToImageSizeIf true then pixel value is clamped to image size.
bool chai3d::cImage::getPixelColor ( const unsigned int  a_x,
const unsigned int  a_y,
cColorb a_color 
) const
virtual

This method returns the color of a pixel by passing its x and y image coordinates.

Parameters
a_xX coordinate of the pixel.
a_yY coordinate of the pixel.
a_colorReturn color of the pixel.
Returns
true if operation succeeds, false otherwise.
bool chai3d::cImage::getPixelColor ( const unsigned int  a_x,
const unsigned int  a_y,
cColorf a_color 
) const
virtual

This method returns the color of a pixel by passing its x and y image coordinates.

Parameters
a_xX coordinate of the pixel.
a_yY coordinate of the pixel.
a_colorReturn color of the pixel.
Returns
true if operation succeeds, false otherwise.
bool chai3d::cImage::getPixelColorInterpolated ( const double  a_x,
const double  a_y,
cColorb a_color 
) const
virtual

This method returns the interpolated color of an image pixel at location (x,y).

Parameters
a_xX coordinate of the pixel.
a_yY coordinate of the pixel.
a_colorReturn color of the pixel.
Returns
true if operation succeeds, false otherwise.
bool chai3d::cImage::getPixelColorInterpolated ( const double  a_x,
const double  a_y,
cColorf a_color 
) const
virtual

This method returns the interpolated color of an image pixel at location (x,y).

Parameters
a_xX coordinate of the pixel.
a_yY coordinate of the pixel.
a_colorReturn color of the pixel.
Returns
true if operation succeeds, false otherwise.
void chai3d::cImage::setPixelColor ( const unsigned int  a_x,
const unsigned int  a_y,
const cColorb a_color 
)
virtual

This method sets the color of a pixel at a desired location (x,y).

Parameters
a_xX coordinate of the image pixel.
a_yY coordinate of the image pixel.
a_colorNew pixel color.
virtual void chai3d::cImage::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 
)
inlinevirtual
void chai3d::cImage::setPixelColor ( const unsigned int  a_x,
const unsigned int  a_y,
const unsigned char  a_grayLevel 
)
virtual

This method sets the gray level of a pixel at a desired location (x,y).

Parameters
a_xX coordinate of the pixel.
a_yY coordinate of the pixel.
a_grayLevelNew luminance value of the pixel.
void chai3d::cImage::setTransparentColor ( const cColorb a_color,
const unsigned char  a_transparencyLevel 
)
virtual

This method defines a color to be transparent. If the image is not in GL_RGBA format, it is first converted in order to enable pixel transparency capabilities.

Parameters
a_colorSelected pixel color.
a_transparencyLevelTransparency level.

Reimplemented in chai3d::cMultiImage.

void chai3d::cImage::setTransparentColor ( const unsigned char  a_r,
const unsigned char  a_g,
const unsigned char  a_b,
const unsigned char  a_transparencyLevel 
)
virtual

Define a pixel color to be transparent. If the image is not in GL_RGBA format, it is first converted in order to enable pixel transparency capabilities.

Parameters
a_rRed component of selected pixel color.
a_gGreen component of selected pixel color.
a_bBlue component of selected pixel color.
a_transparencyLevelTransparency level.

Reimplemented in chai3d::cMultiImage.

void chai3d::cImage::setTransparency ( const unsigned char  a_transparencyLevel)
virtual

his method assigns a transparency level to all image pixels. If the image is not in GL_RGBA format, it is first converted in order to enable pixel transparency capabilities.

Parameters
a_transparencyLevelTransparency level.

Reimplemented in chai3d::cMultiImage.

void chai3d::cImage::flipHorizontal ( )
virtual

This method flips the image horizontally.

Reimplemented in chai3d::cMultiImage.

virtual void chai3d::cImage::getVoxelLocation ( const cVector3d a_texCoord,
int &  a_voxelX,
int &  a_voxelY,
int &  a_voxelZ,
bool  a_clampToImageSize = true 
) const
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual void chai3d::cImage::getVoxelLocationInterpolated ( const cVector3d a_texCoord,
double &  a_voxelX,
double &  a_voxelY,
double &  a_voxelZ,
bool  a_clampToImageSize = true 
) const
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual bool chai3d::cImage::getVoxelColor ( const unsigned int  a_x,
const unsigned int  a_y,
const unsigned int  a_z,
cColorb a_color 
) const
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual bool chai3d::cImage::getVoxelColorInterpolated ( const double  a_x,
const double  a_y,
const double  a_z,
cColorb a_color 
) const
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual bool chai3d::cImage::getVoxelColorInterpolated ( const double  a_x,
const double  a_y,
const double  a_z,
cColorf a_color 
) const
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual void chai3d::cImage::setVoxelColor ( const unsigned int  a_x,
const unsigned int  a_y,
const unsigned int  a_z,
const cColorb a_color 
)
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual void chai3d::cImage::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 
)
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual void chai3d::cImage::setVoxelColor ( const unsigned int  a_x,
const unsigned int  a_y,
const unsigned int  a_z,
const unsigned char  a_grayLevel 
)
inlinevirtual

Reimplemented in chai3d::cMultiImage.

void chai3d::cImage::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 different location or onto a different destination image.

Parameters
a_sourcePosXX coordinate of top left pixel to copy.
a_sourcePosYY coordinate of top left pixel to copy.
a_sourceSizeXWidth of area to copy.
a_sourceSizeYHeight of area to copy.
a_destImageDestination image when area will be pasted.
a_destPosXX coordinate of top left pixel on destination image where area is pasted.
a_destPosYY coordinate of top left pixel on destination image where area is pasted.
void chai3d::cImage::copyTo ( cImagePtr  a_destImage,
const unsigned int  a_destPosX = 0,
const unsigned int  a_destPosY = 0 
)

This method copies this entire image to another destination image. If the destination images is smaller than the source, the copied area is cropped before being copied.

Parameters
a_destImageDestination image when area will be pasted.
a_destPosXX coordinate of top left pixel on destination image where area is pasted.
a_destPosYY coordinate of top left pixel on destination image where area is pasted.
virtual unsigned long chai3d::cImage::getCurrentIndex ( )
inlinevirtual

Reimplemented in chai3d::cMultiImage.

virtual bool chai3d::cImage::selectImage ( unsigned long  a_index)
inlinevirtual

Reimplemented in chai3d::cMultiImage.

bool chai3d::cImage::loadFromFile ( const std::string &  a_filename)
virtual

Loads this image from the specified file. Returns true if all goes well. Note that regardless of whether it succeeds, this over-writes any image that had previously been loaded by this object.

Parameters
a_filenameImage filename
Returns
true if file loaded successfully, false otherwise.

Reimplemented in chai3d::cMultiImage.

bool chai3d::cImage::saveToFile ( const std::string &  a_filename)
virtual

Saves this image from the specified file. Returns true if all goes well. All images are saved in RGB format.

Parameters
a_filenameImage filename.
std::string chai3d::cImage::getFilename ( ) const
inline
void chai3d::cImage::defaults ( )
protected

This method initializes internal variables.

void chai3d::cImage::cleanup ( )
protected

This method frees memory that was used for image data, and re-initialize internal variables.

Member Data Documentation

cColorb chai3d::cImage::m_borderColor
std::string chai3d::cImage::m_filename
protected
unsigned int chai3d::cImage::m_width
protected
unsigned int chai3d::cImage::m_height
protected
GLenum chai3d::cImage::m_format
protected
GLenum chai3d::cImage::m_type
protected
unsigned int chai3d::cImage::m_bytesPerPixel
protected
unsigned char* chai3d::cImage::m_data
protected
unsigned int chai3d::cImage::m_memorySize
protected
bool chai3d::cImage::m_allocated
protected
bool chai3d::cImage::m_responsibleForMemoryAllocation
protected

The documentation for this class was generated from the following files: