Go to the documentation of this file.00001
00007 #ifndef RGBIMAGE_H
00008 #define RGBIMAGE_H
00009
00010 #include <stdio.h>
00011 #include <assert.h>
00012
00013
00014
00020 class RgbImage
00021 {
00022 public:
00025 RgbImage();
00029 RgbImage( const char* filename );
00034 RgbImage( int numRows, int numCols );
00038 ~RgbImage();
00039
00045 bool LoadBmpFile( const char *filename );
00051 bool WriteBmpFile( const char* filename );
00052 #ifndef RGBIMAGE_DONT_USE_OPENGL
00053
00056 bool LoadFromOpenglBuffer();
00057 #endif
00058
00061 long GetNumRows() const { return NumRows; }
00064 long GetNumCols() const { return NumCols; }
00065
00068 long GetNumBytesPerRow() const { return ((3*NumCols+3)>>2)<<2; }
00071 const void* ImageData() const { return (void*)ImagePtr; }
00077 const unsigned char* GetRgbPixel( long row, long col ) const;
00083 unsigned char* GetRgbPixel( long row, long col );
00091 void GetRgbPixel( long row, long col, float* red, float* green, float* blue ) const;
00099 void GetRgbPixel( long row, long col, double* red, double* green, double* blue ) const;
00100
00108 void SetRgbPixelf( long row, long col, double red, double green, double blue );
00109
00117 void SetRgbPixelc( long row, long col,
00118 unsigned char red, unsigned char green, unsigned char blue );
00122
00123 int GetErrorCode() const { return ErrorCode; }
00124 enum {
00125 NoError = 0,
00126 OpenError = 1,
00127 FileFormatError = 2,
00128 MemoryError = 3,
00129 ReadError = 4,
00130 WriteError = 5
00131 };
00132 bool ImageLoaded() const { return (ImagePtr!=0); }
00133
00134 void Reset();
00135
00136 private:
00140 unsigned char* ImagePtr;
00143 long NumRows;
00146 long NumCols;
00149 int ErrorCode;
00150
00155 static short readShort( FILE* infile );
00160 static long readLong( FILE* infile );
00166 static void skipChars( FILE* infile, int numChars );
00171 static void RgbImage::writeLong( long data, FILE* outfile );
00176 static void RgbImage::writeShort( short data, FILE* outfile );
00177
00182 static unsigned char doubleToUnsignedChar( double x );
00183
00184 };
00185
00188 inline RgbImage::RgbImage()
00189 {
00190 NumRows = 0;
00191 NumCols = 0;
00192 ImagePtr = 0;
00193 ErrorCode = 0;
00194 }
00198 inline RgbImage::RgbImage( const char* filename )
00199 {
00200 NumRows = 0;
00201 NumCols = 0;
00202 ImagePtr = 0;
00203 ErrorCode = 0;
00204 LoadBmpFile( filename );
00205 }
00209 inline RgbImage::~RgbImage()
00210 {
00211 delete[] ImagePtr;
00212 }
00213
00214
00220 inline const unsigned char* RgbImage::GetRgbPixel( long row, long col ) const
00221 {
00222 assert ( row<NumRows && col<NumCols );
00223 const unsigned char* ret = ImagePtr;
00224 long i = row*GetNumBytesPerRow() + 3*col;
00225 ret += i;
00226 return ret;
00227 }
00233 inline unsigned char* RgbImage::GetRgbPixel( long row, long col )
00234 {
00235 assert ( row<NumRows && col<NumCols );
00236 unsigned char* ret = ImagePtr;
00237 long i = row*GetNumBytesPerRow() + 3*col;
00238 ret += i;
00239 return ret;
00240 }
00248 inline void RgbImage::GetRgbPixel( long row, long col, float* red, float* green, float* blue ) const
00249 {
00250 assert ( row<NumRows && col<NumCols );
00251 const unsigned char* thePixel = GetRgbPixel( row, col );
00252 const float f = 1.0f/255.0f;
00253 *red = f*(float)(*(thePixel++));
00254 *green = f*(float)(*(thePixel++));
00255 *blue = f*(float)(*thePixel);
00256 }
00264 inline void RgbImage::GetRgbPixel( long row, long col, double* red, double* green, double* blue ) const
00265 {
00266 assert ( row<NumRows && col<NumCols );
00267 const unsigned char* thePixel = GetRgbPixel( row, col );
00268 const double f = 1.0/255.0;
00269 *red = f*(double)(*(thePixel++));
00270 *green = f*(double)(*(thePixel++));
00271 *blue = f*(double)(*thePixel);
00272 }
00275 inline void RgbImage::Reset()
00276 {
00277 NumRows = 0;
00278 NumCols = 0;
00279 delete[] ImagePtr;
00280 ImagePtr = 0;
00281 ErrorCode = 0;
00282 }
00283
00284
00285 #endif // RGBIMAGE_H