#include <RgbImage.h>
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 |
RgbImage class.
24 bit bmp image class.
Definition at line 20 of file RgbImage.h.
anonymous enum |
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) };
RgbImage::RgbImage | ( | ) | [inline] |
RgbImage::RgbImage | ( | const char * | filename | ) | [inline] |
RgbImage constructor
filename | the input file nmae |
Definition at line 198 of file RgbImage.h.
{ NumRows = 0; NumCols = 0; ImagePtr = 0; ErrorCode = 0; LoadBmpFile( filename ); }
RgbImage::RgbImage | ( | int | numRows, | |
int | numCols | |||
) |
RgbImage constructor
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; } } }
RgbImage::~RgbImage | ( | ) | [inline] |
unsigned char RgbImage::doubleToUnsignedChar | ( | double | x | ) | [static, private] |
convert a double to unsigned char
x | input double number |
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 } }
int RgbImage::GetErrorCode | ( | ) | const [inline] |
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; }
long RgbImage::GetNumCols | ( | ) | const [inline] |
long RgbImage::GetNumRows | ( | ) | const [inline] |
const unsigned char * RgbImage::GetRgbPixel | ( | long | row, | |
long | col | |||
) | const [inline] |
Get the pixel at (row, col)
row | row position | |
col | column position |
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; }
unsigned char * RgbImage::GetRgbPixel | ( | long | row, | |
long | col | |||
) | [inline] |
Get the pixel at (row, col)
row | row position | |
col | column position |
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; }
void RgbImage::GetRgbPixel | ( | long | row, | |
long | col, | |||
double * | red, | |||
double * | green, | |||
double * | blue | |||
) | const [inline] |
Get the pixel at (row, col)
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); }
void RgbImage::GetRgbPixel | ( | long | row, | |
long | col, | |||
float * | red, | |||
float * | green, | |||
float * | blue | |||
) | const [inline] |
Get the pixel at (row, col)
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); }
const void* RgbImage::ImageData | ( | ) | const [inline] |
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
filename | input file name |
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; }
bool RgbImage::LoadFromOpenglBuffer | ( | ) |
Load the Bmp Image from OpenGL buffer
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; }
long RgbImage::readLong | ( | FILE * | infile | ) | [static, private] |
read a long integer form a file
infile | input file |
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; }
short RgbImage::readShort | ( | FILE * | infile | ) | [static, private] |
read a short integer form a file
infile | input file |
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; }
void RgbImage::Reset | ( | ) | [inline] |
void RgbImage::SetRgbPixelc | ( | long | row, | |
long | col, | |||
unsigned char | red, | |||
unsigned char | green, | |||
unsigned char | blue | |||
) |
set the pixel at (row, col)
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; }
void RgbImage::SetRgbPixelf | ( | long | row, | |
long | col, | |||
double | red, | |||
double | green, | |||
double | blue | |||
) |
set the pixel at (row, col)
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)
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.
{ SetRgbPixelc( row, col, doubleToUnsignedChar(red), doubleToUnsignedChar(green), doubleToUnsignedChar(blue) ); }
void RgbImage::skipChars | ( | FILE * | infile, | |
int | numChars | |||
) | [static, private] |
skip several chars in the file
infile | input file name | |
numChars | the number of chars to be skipped |
skip several chars in the file
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 ); } }
bool RgbImage::WriteBmpFile | ( | const char * | filename | ) |
WriteBmpFile
filename | output file name |
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; }
void RgbImage::writeLong | ( | long | data, | |
FILE * | outfile | |||
) | [static, private] |
write a long integer to the file
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 ); }
void RgbImage::writeShort | ( | short | data, | |
FILE * | outfile | |||
) | [static, private] |
write a short integer to the file
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 ); }
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.