CUGL  2.0
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
cugl::PixelMap Class Reference

#include <cugl.h>

Public Member Functions

 PixelMap ()
 
 PixelMap (const char *bmpFileName)
 
 PixelMap (GLint x, GLint y, GLsizei w, GLsizei h)
 
 ~PixelMap ()
 
void read (const char *bmpFileName)
 
void read (GLint x, GLint y, GLsizei w, GLsizei h, GLenum mode=GL_FRONT)
 
void write (const char *bmpFileName)
 
bool badSize ()
 
void rescale ()
 
void draw ()
 
void setTexture (GLuint name)
 
void setMipmaps (GLuint name)
 
void select (const PixelMap &src, int xp, int yp, int width, int height)
 
unsigned long getRows () const
 
unsigned long getColumns () const
 
unsigned long getSize () const
 
char * getName () const
 
bool ready () const
 

Private Member Functions

 PixelMap (const PixelMap &pm)
 
bool allocate (unsigned long newSize)
 

Private Attributes

unsigned long numRows
 
unsigned long numCols
 
unsigned long size
 
char * fileName
 
unsigned char * pixels
 

Friends

bool compatible (const PixelMap &m1, const PixelMap &m2)
 
void mix (const PixelMap &m1, const PixelMap &m2, PixelMap &res, double prop)
 
std::ostream & operator<< (std::ostream &os, const PixelMap &pm)
 

Detailed Description

An instance is an RGB pixel array. An instance of PixelMap holds an array that OpenGL can use as a texture or to display directly.

This class is implemented so that it does not depend on Windows. In principle, it should even work with Linux and other operating systems, although this has not been tested thoroughly.

If you want to use a BMP image as a texture, the width and height must be powers of two. Thus a 256 x 512 BMP image is acceptable but a 640 x 400 image is not. This restriction does not apply to images that are displayed as bitmaps.

Since an instance of PixelMap contains pointers, there are memory management issues. The destructor deletes pixel and other data associated with a pixel map. The copy constructor is declared privately and not implemented: this prevents instances being passed by value, although they may be passed by reference. Since there is a default constructor, you can construct arrays of PixelMap's or pointers to PixelMap.

Definition at line 1958 of file cugl.h.

Constructor & Destructor Documentation

cugl::PixelMap::PixelMap ( )

Construct an empty pixel map.

cugl::PixelMap::PixelMap ( const char *  bmpFileName)

Read a pixel map from a file.

Parameters
bmpFileNameis a path to a BMP file.
cugl::PixelMap::PixelMap ( GLint  x,
GLint  y,
GLsizei  w,
GLsizei  h 
)

Construct a pixel map from a region of the pixel buffer.

Parameters
xis the X coordinate of the lower left corner of the region.
yis the Y coordinate of the lower left corner of the region.
wis the width of the region in pixels.
his the height of the region in pixels.
Note
See also PixelMap::read(x, y, w, h).
cugl::PixelMap::~PixelMap ( )

Delete a pixel map.

cugl::PixelMap::PixelMap ( const PixelMap pm)
private

The copy constructor is private and unimplemented. This prevents pixel maps from being copied by mistake.

Member Function Documentation

bool cugl::PixelMap::allocate ( unsigned long  newSize)
private

Allocate memory for a new pixel map if necessary.

bool cugl::PixelMap::badSize ( )

Check the dimensions of the bit map. If the dimensions are not powers of 2, return true. If the dimensions of the bitmap are not powers of 2, OpenGL cannot use it as a texture. You should call PixelMap::rescale() to resize the bit map.

void cugl::PixelMap::draw ( )

Draw the pixel map at the current raster position. Error NO_PIX_MAP if there is no pixel map avaialble.

unsigned long cugl::PixelMap::getColumns ( ) const
inline

Return number of columns in pixel map.

Definition at line 2113 of file cugl.h.

References numCols.

char* cugl::PixelMap::getName ( ) const
inline

Return name of BMP file.

Definition at line 2129 of file cugl.h.

References fileName.

unsigned long cugl::PixelMap::getRows ( ) const
inline

Return number of rows in pixel map.

Definition at line 2105 of file cugl.h.

References numRows.

unsigned long cugl::PixelMap::getSize ( ) const
inline

Return bytes of memory used by pixel map.

Definition at line 2121 of file cugl.h.

References size.

void cugl::PixelMap::read ( const char *  bmpFileName)

Read a BMP file and convert it to a pixel map. Previous data associated with this object will be deleted. The BMP file must satisfy several criteria if it is to be converted successfully. Conversion failure is indicated by the following CUGL errors.

  • OPEN_FAILED: the file could not be opened
  • NOT_BMP_FILE: the file is not a BMP file
  • NOT_24_BITS: the format is not 24 bits/pixel
  • COMPRESSED_BMP_FILE: the file is compressed
  • NOT_ENOUGH_MEMORY: there is not enough memory to store the data

OpenGL cannot use a bitmap as a texture if its dimensions are not powers of 2. To check whether the bitmap has acceptable dimensions, call PixelMap::badSize(). To convert the bitmap dimensions to acceptable values, call PixelMap::rescale().

Parameters
bmpFileNameis the name of the file to be read, with the extension '.bmp'.
void cugl::PixelMap::read ( GLint  x,
GLint  y,
GLsizei  w,
GLsizei  h,
GLenum  mode = GL_FRONT 
)

Read a pixel map from a region of the framebuffer. This function is similar to the constructor with the same parameters, but allocates new memory only if necessary.

Parameters
xis the X coordinate of the lower left corner of the region.
yis the Y coordinate of the lower left corner of the region.
wis the width of the region in pixels.
his the height of the region in pixels.
modeis passed to glReadBuffer before reading the pixels.
Note
See also the constructor PixelMap(x, y, w, h).
bool cugl::PixelMap::ready ( ) const
inline

Return true if a pixel map has been read successfully.

Definition at line 2137 of file cugl.h.

References pixels.

void cugl::PixelMap::rescale ( )

Rescale a bit map whose dimensions are not powers of 2. The new image will be distorted; the amount of distortion depends on how much the dimensions have to be altered. Use PixelMap::badSize() to determine whether the dimensions are powers of 2.

void cugl::PixelMap::select ( const PixelMap src,
int  xp,
int  yp,
int  width,
int  height 
)

Select a region from a pixel map.

Parameters
srcis the pixel map from which the data is extracted.
xpdefines the left side of the selected region.
ypdefines the right side of the selected region.
widthis the width of the selected region.
heightis the height of the selected region.
void cugl::PixelMap::setMipmaps ( GLuint  name)

Set texture parameters for the pixel mipmaps. Construct a family of mipmaps for texturing.

Parameters
nameis an OpenGL index for the texture parameters provided by the caller.
Note
This function sets GL_TEXTURE_MIN_FILTER to GL_LINEAR_MIPMAP_NEAREST. Call glTexParameter() to change this setting.
When this function returns, OpenGL has copied the pixel map into its own memory space. It is therefore safe to delete the PixelMap instance after calling setMipmaps().
Call this function once only for each texture you need in your program. Use glBindTexture to select textures in the display function.
void cugl::PixelMap::setTexture ( GLuint  name)

Set texture parameters for the pixel map.

Parameters
nameis an OpenGL index for the texture parameters provided by the caller.
Note
This function sets GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER to GL_NEAREST. Call glTexParameter() to change these settings.
When this function returns, OpenGL has copied the pixel map into its own memory space. It is therefore safe to delete the PixelMap instance after calling setTexture().
void cugl::PixelMap::write ( const char *  bmpFileName)

Write a pixel map to an output stream as a BMP file.

Parameters
bmpFileNameis the name of the file to be written, with the extension '.bmp'.

Friends And Related Function Documentation

bool compatible ( const PixelMap m1,
const PixelMap m2 
)
friend

Check that two pixel maps are compatible for combining.

void mix ( const PixelMap m1,
const PixelMap m2,
PixelMap res,
double  prop 
)
friend

Combine two pixel maps.

Parameters
m1is the first map to be combined.
m2is the second map to be combined.
resis the resulting pixel map. The caller is responsible for constructing this map; it is not constructed by the function.
propis the mixing proportion: 0 gives m1, 0.5 gives half of each, and 1 gives m2.
std::ostream& operator<< ( std::ostream &  os,
const PixelMap pm 
)
friend

Write a description of the pixel map to the output stream.

Member Data Documentation

char* cugl::PixelMap::fileName
private

Name of the file used to store the pixel map.

Definition at line 2168 of file cugl.h.

Referenced by getName().

unsigned long cugl::PixelMap::numCols
private

Number of columns in the pixel map.

Definition at line 2162 of file cugl.h.

Referenced by getColumns().

unsigned long cugl::PixelMap::numRows
private

Number of rows in the pixel map.

Definition at line 2159 of file cugl.h.

Referenced by getRows().

unsigned char* cugl::PixelMap::pixels
private

Pointer to the pixels of the map.

Definition at line 2171 of file cugl.h.

Referenced by ready().

unsigned long cugl::PixelMap::size
private

Size, in bytes, of the pixel map.

Definition at line 2165 of file cugl.h.

Referenced by getSize().


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