Public Types | Public Member Functions | Static Private Member Functions | Private Attributes

RgbImage Class Reference

RgbImage class. More...

#include <RgbImage.h>

Collaboration diagram for RgbImage:
Collaboration graph
[legend]

List of all members.

Public Types

enum  {
  NoError = 0, OpenError = 1, FileFormatError = 2, MemoryError = 3,
  ReadError = 4, WriteError = 5
}

Public Member Functions

 RgbImage ()
 RgbImage (const char *filename)
 RgbImage (int numRows, int numCols)
 ~RgbImage ()
bool LoadBmpFile (const char *filename)
bool WriteBmpFile (const char *filename)
bool LoadFromOpenglBuffer ()
long GetNumRows () const
long GetNumCols () const
long GetNumBytesPerRow () const
const void * ImageData () const
const unsigned char * GetRgbPixel (long row, long col) const
unsigned char * GetRgbPixel (long row, long col)
void GetRgbPixel (long row, long col, float *red, float *green, float *blue) const
void GetRgbPixel (long row, long col, double *red, double *green, double *blue) const
void SetRgbPixelf (long row, long col, double red, double green, double blue)
void SetRgbPixelc (long row, long col, unsigned char red, unsigned char green, unsigned char blue)
int GetErrorCode () const
bool ImageLoaded () const
void Reset ()

Static Private Member Functions

static short readShort (FILE *infile)
static long readLong (FILE *infile)
static void skipChars (FILE *infile, int numChars)
static void writeLong (long data, FILE *outfile)
static void writeShort (short data, FILE *outfile)
static unsigned char doubleToUnsignedChar (double x)

Private Attributes

unsigned char * ImagePtr
long NumRows
long NumCols
int ErrorCode

Detailed Description

RgbImage class.

24 bit bmp image class.

Definition at line 20 of file RgbImage.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
NoError 
OpenError 
FileFormatError 
MemoryError 
ReadError 
WriteError 

Definition at line 124 of file RgbImage.h.

             {
                NoError = 0,
                OpenError = 1,                  // Unable to open file for reading
                FileFormatError = 2,    // Not recognized as a 24 bit BMP file
                MemoryError = 3,                // Unable to allocate memory for image data
                ReadError = 4,                  // End of file reached prematurely
                WriteError = 5                  // Unable to write out data (or no date to write out)
        };


Constructor & Destructor Documentation

RgbImage::RgbImage (  )  [inline]

RgbImage constructor

Definition at line 188 of file RgbImage.h.

{ 
        NumRows = 0;
        NumCols = 0;
        ImagePtr = 0;
        ErrorCode = 0;
}

RgbImage::RgbImage ( const char *  filename  )  [inline]

RgbImage constructor

Parameters:
filename the input file nmae

Definition at line 198 of file RgbImage.h.

{
        NumRows = 0;
        NumCols = 0;
        ImagePtr = 0;
        ErrorCode = 0;
        LoadBmpFile( filename );
}

Here is the call graph for this function:

RgbImage::RgbImage ( int  numRows,
int  numCols 
)

RgbImage constructor

Parameters:
numRows number of rows
numCols number of cols

Definition at line 19 of file RgbImage.cpp.

{
        NumRows = numRows;
        NumCols = numCols;
        ImagePtr = new unsigned char[NumRows*GetNumBytesPerRow()];
        if ( !ImagePtr ) {
                fprintf(stderr, "Unable to allocate memory for %ld x %ld bitmap.\n", 
                                NumRows, NumCols);
                Reset();
                ErrorCode = MemoryError;
        }
        // Zero out the image
        unsigned char* c = ImagePtr;
        int rowLen = GetNumBytesPerRow();
        for ( int i=0; i<NumRows; i++ ) 
        {
                for ( int j=0; j<rowLen; j++ ) 
                {
                        *(c++) = 0;
                }
        }
}

Here is the call graph for this function:

RgbImage::~RgbImage (  )  [inline]

RgbImage destructor

Definition at line 209 of file RgbImage.h.

{ 
        delete[] ImagePtr;
}


Member Function Documentation

unsigned char RgbImage::doubleToUnsignedChar ( double  x  )  [static, private]

convert a double to unsigned char

Parameters:
x input double number
Returns:
output unsigned char

Definition at line 309 of file RgbImage.cpp.

{
        if ( x>=1.0 ) {
                return (unsigned char)255;
        }
        else if ( x<=0.0 ) {
                return (unsigned char)0;
        }
        else {
                return (unsigned char)(x*255.0);                // Rounds down
        }
}

Here is the caller graph for this function:

int RgbImage::GetErrorCode (  )  const [inline]

get the error code

Definition at line 123 of file RgbImage.h.

{ return ErrorCode; }

long RgbImage::GetNumBytesPerRow (  )  const [inline]

Get number of bytes per row

Definition at line 68 of file RgbImage.h.

{ return ((3*NumCols+3)>>2)<<2; }       

Here is the caller graph for this function:

long RgbImage::GetNumCols (  )  const [inline]

number of columns

Definition at line 64 of file RgbImage.h.

{ return NumCols; }

long RgbImage::GetNumRows (  )  const [inline]

number of rows

Definition at line 61 of file RgbImage.h.

{ return NumRows; }

const unsigned char * RgbImage::GetRgbPixel ( long  row,
long  col 
) const [inline]

Get the pixel at (row, col)

Parameters:
row row position
col column position
Returns:
array of unsigned char for the pixel color

Definition at line 220 of file RgbImage.h.

{
        assert ( row<NumRows && col<NumCols );
        const unsigned char* ret = ImagePtr;
        long i = row*GetNumBytesPerRow() + 3*col;
        ret += i;
        return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char * RgbImage::GetRgbPixel ( long  row,
long  col 
) [inline]

Get the pixel at (row, col)

Parameters:
row row position
col column position
Returns:
array of unsigned char for the pixel color

Definition at line 233 of file RgbImage.h.

{
        assert ( row<NumRows && col<NumCols );
        unsigned char* ret = ImagePtr;
        long i = row*GetNumBytesPerRow() + 3*col;
        ret += i;
        return ret;
}

Here is the call graph for this function:

void RgbImage::GetRgbPixel ( long  row,
long  col,
double *  red,
double *  green,
double *  blue 
) const [inline]

Get the pixel at (row, col)

Parameters:
row row position
col column position
red pointer to the red color
green pointer to the green color
blue pointer to the blue color

Definition at line 264 of file RgbImage.h.

{
        assert ( row<NumRows && col<NumCols );
        const unsigned char* thePixel = GetRgbPixel( row, col );
        const double f = 1.0/255.0;
        *red = f*(double)(*(thePixel++));
        *green = f*(double)(*(thePixel++));
        *blue = f*(double)(*thePixel);
}

Here is the call graph for this function:

void RgbImage::GetRgbPixel ( long  row,
long  col,
float *  red,
float *  green,
float *  blue 
) const [inline]

Get the pixel at (row, col)

Parameters:
row row position
col column position
red pointer to the red color
green pointer to the green color
blue pointer to the blue color

Definition at line 248 of file RgbImage.h.

{
        assert ( row<NumRows && col<NumCols );
        const unsigned char* thePixel = GetRgbPixel( row, col );
        const float f = 1.0f/255.0f;
        *red = f*(float)(*(thePixel++));
        *green = f*(float)(*(thePixel++));
        *blue = f*(float)(*thePixel);
}

Here is the call graph for this function:

const void* RgbImage::ImageData (  )  const [inline]

Get image data buffer

Definition at line 71 of file RgbImage.h.

{ return (void*)ImagePtr; }

bool RgbImage::ImageLoaded (  )  const [inline]

Definition at line 132 of file RgbImage.h.

{ return (ImagePtr!=0); }  // Is an image loaded?

bool RgbImage::LoadBmpFile ( const char *  filename  ) 

LoadBmpFile

Parameters:
filename input file name
Returns:
whether the loading is successful

Definition at line 54 of file RgbImage.cpp.

{  
        Reset();
        FILE* infile = fopen( filename, "rb" );         // Open for reading binary data
        if ( !infile ) {
                fprintf(stderr, "Unable to open file: %s\n", filename);
                ErrorCode = OpenError;
                return false;
        }

        bool fileFormatOK = false;
        int bChar = fgetc( infile );
        int mChar = fgetc( infile );
        if ( bChar=='B' && mChar=='M' ) {                       // If starts with "BM" for "BitMap"
                skipChars( infile, 4+2+2+4+4 );                 // Skip 4 fields we don't care about
                NumCols = readLong( infile );
                NumRows = readLong( infile );
                skipChars( infile, 2 );                                 // Skip one field
                int bitsPerPixel = readShort( infile );
                skipChars( infile, 4+4+4+4+4+4 );               // Skip 6 more fields

                if ( NumCols>0 && NumCols<=100000 && NumRows>0 && NumRows<=100000  
                        && bitsPerPixel==24 && !feof(infile) ) {
                        fileFormatOK = true;
                }
        }
        if ( !fileFormatOK ) {
                Reset();
                ErrorCode = FileFormatError;
                fprintf(stderr, "Not a valid 24-bit bitmap file: %s.\n", filename);
                fclose ( infile );
                return false;
        }

        // Allocate memory
        ImagePtr = new unsigned char[NumRows*GetNumBytesPerRow()];
        if ( !ImagePtr ) {
                fprintf(stderr, "Unable to allocate memory for %ld x %ld bitmap: %s.\n", 
                                NumRows, NumCols, filename);
                Reset();
                ErrorCode = MemoryError;
                fclose ( infile );
                return false;
        }

        unsigned char* cPtr = ImagePtr;
        for ( int i=0; i<NumRows; i++ ) {
                int j;
                for ( j=0; j<NumCols; j++ ) {
                        *(cPtr+2) = fgetc( infile );    // Blue color value
                        *(cPtr+1) = fgetc( infile );    // Green color value
                        *cPtr = fgetc( infile );                // Red color value
                        cPtr += 3;
                }
                int k=3*NumCols;                                        // Num bytes already read
                for ( ; k<GetNumBytesPerRow(); k++ ) {
                        fgetc( infile );                                // Read and ignore padding;
                        *(cPtr++) = 0;
                }
        }
        if ( feof( infile ) ) {
                fprintf( stderr, "Premature end of file: %s.\n", filename );
                Reset();
                ErrorCode = ReadError;
                fclose ( infile );
                return false;
        }
        fclose( infile );       // Close the file
        return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool RgbImage::LoadFromOpenglBuffer (  ) 

Load the Bmp Image from OpenGL buffer

Returns:
whether the loading is successful

Load the Bmp Image from OpenGL buffer

Definition at line 347 of file RgbImage.cpp.

{
        int viewportData[4];
        glGetIntegerv( GL_VIEWPORT, viewportData );
        int& vWidth = viewportData[2];
        int& vHeight = viewportData[3];
        
        if ( ImagePtr==0 ) { // If no memory allocated
                NumRows = vHeight;
                NumCols = vWidth;
                ImagePtr = new unsigned char[NumRows*GetNumBytesPerRow()];
                if ( !ImagePtr ) {
                        fprintf(stderr, "Unable to allocate memory for %ld x %ld buffer.\n", 
                                        NumRows, NumCols);
                        Reset();
                        ErrorCode = MemoryError;
                        return false;
                }
        }
        assert ( vWidth>=NumCols && vHeight>=NumRows );
        int oldGlRowLen;
        if ( vWidth>=NumCols ) {
                glGetIntegerv( GL_UNPACK_ROW_LENGTH, &oldGlRowLen );
                glPixelStorei( GL_UNPACK_ROW_LENGTH, NumCols );
        }
        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

        // Get the frame buffer data.
        glReadPixels( 0, 0, NumCols, NumRows, GL_RGB, GL_UNSIGNED_BYTE, ImagePtr);

        // Restore the row length in glPixelStorei  (really ought to restore alignment too).
        if ( vWidth>=NumCols ) {
                glPixelStorei( GL_UNPACK_ROW_LENGTH, oldGlRowLen );
        }       
        return true;
}

Here is the call graph for this function:

long RgbImage::readLong ( FILE *  infile  )  [static, private]

read a long integer form a file

Parameters:
infile input file
Returns:
long integer just read

Definition at line 145 of file RgbImage.cpp.

{  
        // Read in 32 bit integer
        unsigned char byte0, byte1, byte2, byte3;
        byte0 = fgetc(infile);                  // Read bytes, low order to high order
        byte1 = fgetc(infile);
        byte2 = fgetc(infile);
        byte3 = fgetc(infile);

        // Pack together
        long ret = byte3;
        ret <<= 8;
        ret |= byte2;
        ret <<= 8;
        ret |= byte1;
        ret <<= 8;
        ret |= byte0;
        return ret;
}

Here is the caller graph for this function:

short RgbImage::readShort ( FILE *  infile  )  [static, private]

read a short integer form a file

Parameters:
infile input file
Returns:
short integer just read

Definition at line 128 of file RgbImage.cpp.

{
        // read a 16 bit integer
        unsigned char lowByte, hiByte;
        lowByte = fgetc(infile);                        // Read the low order byte (little endian form)
        hiByte = fgetc(infile);                 // Read the high order byte

        // Pack together
        short ret = hiByte;
        ret <<= 8;
        ret |= lowByte;
        return ret;
}

Here is the caller graph for this function:

void RgbImage::Reset (  )  [inline]

reset the RgbImage object

Definition at line 275 of file RgbImage.h.

{
        NumRows = 0;
        NumCols = 0;
        delete[] ImagePtr;
        ImagePtr = 0;
        ErrorCode = 0;
}

Here is the caller graph for this function:

void RgbImage::SetRgbPixelc ( long  row,
long  col,
unsigned char  red,
unsigned char  green,
unsigned char  blue 
)

set the pixel at (row, col)

Parameters:
row row position
col column position
red the red color unsigned char
green the green color unsigned char
blue the blue color unsigned char

Definition at line 294 of file RgbImage.cpp.

{
        assert ( row<NumRows && col<NumCols );
        unsigned char* thePixel = GetRgbPixel( row, col );
        *(thePixel++) = red;
        *(thePixel++) = green;
        *(thePixel) = blue;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void RgbImage::SetRgbPixelf ( long  row,
long  col,
double  red,
double  green,
double  blue 
)

set the pixel at (row, col)

Parameters:
row row position
col column position
red the red color floating point
green the green color floating point
blue the blue color floating point

set the pixel at (row, col)

Parameters:
row row position
col column position
red the red color double value
green the green color double value
blue the blue color double value

Definition at line 281 of file RgbImage.cpp.

Here is the call graph for this function:

void RgbImage::skipChars ( FILE *  infile,
int  numChars 
) [static, private]

skip several chars in the file

Parameters:
infile input file name
numChars the number of chars to be skipped

skip several chars in the file

Parameters:
numChars the number of chars to be skipped

Definition at line 168 of file RgbImage.cpp.

{
        for ( int i=0; i<numChars; i++ ) {
                fgetc( infile );
        }
}

Here is the caller graph for this function:

bool RgbImage::WriteBmpFile ( const char *  filename  ) 

WriteBmpFile

Parameters:
filename output file name
Returns:
whether the writing is successful

Definition at line 187 of file RgbImage.cpp.

{
        FILE* outfile = fopen( filename, "wb" );                // Open for reading binary data
        if ( !outfile ) {
                fprintf(stderr, "Unable to open file: %s\n", filename);
                ErrorCode = OpenError;
                return false;
        }

        fputc('B',outfile);
        fputc('M',outfile);
        int rowLen = GetNumBytesPerRow();
        writeLong( 40+14+NumRows*rowLen, outfile );     // Length of file
        writeShort( 0, outfile );                                       // Reserved for future use
        writeShort( 0, outfile );
        writeLong( 40+14, outfile );                            // Offset to pixel data
        writeLong( 40, outfile );                                       // header length
        writeLong( NumCols, outfile );                          // width in pixels
        writeLong( NumRows, outfile );                          // height in pixels (pos for bottom up)
        writeShort( 1, outfile );               // number of planes
        writeShort( 24, outfile );              // bits per pixel
        writeLong( 0, outfile );                // no compression
        writeLong( 0, outfile );                // not used if no compression
        writeLong( 0, outfile );                // Pixels per meter
        writeLong( 0, outfile );                // Pixels per meter
        writeLong( 0, outfile );                // unused for 24 bits/pixel
        writeLong( 0, outfile );                // unused for 24 bits/pixel

        // Now write out the pixel data:
        unsigned char* cPtr = ImagePtr;
        for ( int i=0; i<NumRows; i++ ) {
                // Write out i-th row's data
                int j;
                for ( j=0; j<NumCols; j++ ) {
                        fputc( *(cPtr+2), outfile);             // Blue color value
                        fputc( *(cPtr+1), outfile);             // Blue color value
                        fputc( *(cPtr+0), outfile);             // Blue color value
                        cPtr+=3;
                }
                // Pad row to word boundary
                int k=3*NumCols;                                        // Num bytes already read
                for ( ; k<GetNumBytesPerRow(); k++ ) {
                        fputc( 0, outfile );                            // Read and ignore padding;
                        cPtr++;
                }
        }

        fclose( outfile );      // Close the file
        return true;
}

Here is the call graph for this function:

void RgbImage::writeLong ( long  data,
FILE *  outfile 
) [static, private]

write a long integer to the file

Parameters:
data the long integter to write
outfile output file

Definition at line 241 of file RgbImage.cpp.

{  
        // Read in 32 bit integer
        unsigned char byte0, byte1, byte2, byte3;
        byte0 = (unsigned char)(data&0x000000ff);               // Write bytes, low order to high order
        byte1 = (unsigned char)((data>>8)&0x000000ff);
        byte2 = (unsigned char)((data>>16)&0x000000ff);
        byte3 = (unsigned char)((data>>24)&0x000000ff);

        fputc( byte0, outfile );
        fputc( byte1, outfile );
        fputc( byte2, outfile );
        fputc( byte3, outfile );
}

Here is the caller graph for this function:

void RgbImage::writeShort ( short  data,
FILE *  outfile 
) [static, private]

write a short integer to the file

Parameters:
data the short integter to write
outfile output file

Definition at line 259 of file RgbImage.cpp.

{  
        // Read in 32 bit integer
        unsigned char byte0, byte1;
        byte0 = data&0x000000ff;                // Write bytes, low order to high order
        byte1 = (data>>8)&0x000000ff;

        fputc( byte0, outfile );
        fputc( byte1, outfile );
}

Here is the caller graph for this function:


Member Data Documentation

int RgbImage::ErrorCode [private]

current error code

Definition at line 149 of file RgbImage.h.

unsigned char* RgbImage::ImagePtr [private]

pixel buffer

Definition at line 140 of file RgbImage.h.

long RgbImage::NumCols [private]

number of columns

Definition at line 146 of file RgbImage.h.

long RgbImage::NumRows [private]

number of rows

Definition at line 143 of file RgbImage.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Defines