![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsBitmapImageHandler.hpp 00003 00004 Bitmap Image Handler 00005 Adapted from OpenGL Game Programming book (2001) 00006 00007 SUKITTI PUNAK (09/16/2004) 00008 UPDATE (07/17/2007) 00009 ******************************************************************************/ 00010 #ifndef TAPs_BITMAP_IMAGE_HANDLER_HPP 00011 #define TAPs_BITMAP_IMAGE_HANDLER_HPP 00012 00013 #include <iostream> 00014 using std::cerr; 00015 00016 #if !defined( _WINDOWS ) && !defined( WIN32 ) 00017 #include "TAPsWindowsDataTypeDef.hpp" 00018 using namespace TAPs::WindowsDataTypeDef; 00019 #else 00020 #include <windows.h> 00021 #endif 00022 00023 #include "../Core/TAPsDef.hpp" 00024 00025 // Uncomment the following #define line for Debug Message 00026 #define BITMAPIMAGE_H_DEBUG_MODE 00027 #ifdef BITMAPIMAGE_H_DEBUG_MODE 00028 #define BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( s ) { printf( "DEBUG MESSAGE: " ); printf s; } 00029 #else 00030 #define BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( s ) {} 00031 #endif 00032 00033 BEGIN_NAMESPACE_TAPs 00034 //============================================================================= 00035 // CLASS: BitmapImage 00036 // DESC: Handle a bitmap data. 00037 class BitmapImage { 00038 //****************************************************************************** 00039 // Bitmap File Header 00040 typedef struct tagBITMAPFILEHEADER { 00041 WORD bfType; // specifies the file type; must be BM (0x4D42) 00042 DWORD bfSize; // specifies the size in bytes of the bitmap file 00043 WORD bfReserved1; // reserved: must be zero 00044 WORD bfReserved2; // reserved: must be zero 00045 DWORD bfOffBits; // specifies the offset, in bytes, from the 00046 // BITMAPFILEHEADER structure to the bitmap bits 00047 } BITMAPFILEHEADER; 00048 //****************************************************************************** 00049 // Bitmap Information Header 00050 typedef struct tagBITMAPINFOHEADER { 00051 DWORD biSize; // specifies number of bytes required by the structure 00052 LONG biWidth; // specifies the width of the bitmap, in pixels 00053 LONG biHeight; // specifies the height of the bitmap, in pixels 00054 WORD biPlanes; // specifies the number of color planes, must be 1 00055 WORD biBitCount; // specifies the number of bits per pixel; must be 1, 4, 8, 16, 24, or 32 00056 DWORD biCompression; // specifies the type of compression 00057 DWORD biSizeImage; // size of image in bytes 00058 LONG biXPelsPerMeter; // specifies the number of pixels per meter in x axis 00059 LONG biYPelsPerMeter; // specifies the number of pixels per meter in y axis 00060 DWORD biClrUsed; // specifies the number of colors used by the bitmap 00061 DWORD biClrImportant; // specifies the number of colors that are important 00062 } BITMAPINFOHEADER; 00063 //****************************************************************************** 00064 // Member Functions 00065 public: 00066 // START: Constructor --------------------------------------------------- 00067 BitmapImage() 00068 { 00069 m_pucBitmapImage = NULL; 00070 } // END: Constructor 00071 00072 00073 // START: Destructor ---------------------------------------------------- 00074 ~BitmapImage() 00075 { 00076 // reclaim the allocated memory if any 00077 if ( m_pucBitmapImage != NULL ) { 00078 delete [] m_pucBitmapImage; 00079 m_pucBitmapImage = NULL; 00080 } 00081 } // END: Destructor 00082 00083 00084 // MEMBER FUNCION: LoadBitMapFile() -------------------------------------- 00085 // DESC: 00086 bool LoadBitmapFile( char *pcFileName ) 00087 { 00088 FILE *filePtr; // the file pointer 00089 BITMAPFILEHEADER bitmapFileHeader; // bitmap file header 00090 unsigned int uiImageIdx = 0; // image index counter 00091 unsigned char ucTempRGB; // swap variable 00092 00093 // reclaim the allocated memory if any 00094 if ( m_pucBitmapImage != NULL ) { 00095 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Erase an old BMP image data\n") ); 00096 delete [] m_pucBitmapImage; 00097 m_pucBitmapImage = NULL; 00098 } 00099 00100 // try to open filename in "read binary" mode 00101 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("sizeof(WORD) = %d\n", sizeof(WORD)) ); 00102 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("sizeof(DWORD) = %d\n", sizeof(DWORD)) ); 00103 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("sizeof(BITMAPFILEHEADER) = %d\n", sizeof(BITMAPFILEHEADER)) ); 00104 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("sizeof(BITMAPINFOHEADER) = %d\n", sizeof(BITMAPINFOHEADER)) ); 00105 00106 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("FileName = %s\n", pcFileName) ); 00107 if ( ( filePtr = fopen( pcFileName, "rb" ) ) == NULL ) { 00108 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("unsucceed opening the BMP image file\n") ); 00109 return false; 00110 } 00111 else { 00112 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("succeed opening the BMP image file\n") ); 00113 } 00114 00115 // read the bitmap file header 00116 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Read the BMP image file header\n") ); 00117 //return false; 00118 //fread( &bitmapFileHeader, sizeof( BITMAPFILEHEADER ), 1, filePtr ); 00119 fread( &bitmapFileHeader.bfType, sizeof( WORD ), 1, filePtr ); 00120 fread( &bitmapFileHeader.bfSize, sizeof( DWORD ), 1, filePtr ); 00121 fread( &bitmapFileHeader.bfReserved1, sizeof( WORD ), 1, filePtr ); 00122 fread( &bitmapFileHeader.bfReserved2, sizeof( WORD ), 1, filePtr ); 00123 fread( &bitmapFileHeader.bfOffBits, sizeof( DWORD ), 1, filePtr ); 00124 00125 // verify that this is a bitmap by checking for the universal bitmap id 00126 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Check whether the BMP image is a bitmap image\n") ); 00127 00128 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("bfType = %d and SM_BITMAP_ID = %d\n", bitmapFileHeader.bfType, SM_BITMAP_ID) ); 00129 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("bfSize = %d\n", bitmapFileHeader.bfSize) ); 00130 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("bfOffbits = %d\n", bitmapFileHeader.bfOffBits) ); 00131 if ( bitmapFileHeader.bfType != SM_BITMAP_ID ) { 00132 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("No, it's not!\n") ); 00133 fclose( filePtr ); 00134 return false; 00135 } 00136 else { 00137 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Yes, it is.\n") ); 00138 } 00139 00140 fseek( filePtr, 14, SEEK_SET ); 00141 00142 // read the bitmap information header 00143 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Read bitmap info header\n") ); 00144 00145 fread( &m_pBitmapInfoHeader_st, sizeof( BITMAPINFOHEADER ), 1, filePtr ); 00146 00147 00148 // HACK 00149 m_pBitmapInfoHeader_st.biSizeImage = m_pBitmapInfoHeader_st.biWidth * m_pBitmapInfoHeader_st.biHeight * 3; 00150 00151 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biSize = %d\n", m_pBitmapInfoHeader_st.biSize) ); 00152 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biWidth = %d\n", m_pBitmapInfoHeader_st.biWidth) ); 00153 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biHeight = %d\n", m_pBitmapInfoHeader_st.biHeight) ); 00154 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biPlanes = %d\n", m_pBitmapInfoHeader_st.biPlanes) ); 00155 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biBitCount = %d\n", m_pBitmapInfoHeader_st.biBitCount) ); 00156 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biCompression = %d\n", m_pBitmapInfoHeader_st.biCompression) ); 00157 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biSizeImage = %d\n", m_pBitmapInfoHeader_st.biSizeImage) ); 00158 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biXPelsPerMeter = %d\n", m_pBitmapInfoHeader_st.biXPelsPerMeter) ); 00159 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biYPelsPerMeter = %d\n", m_pBitmapInfoHeader_st.biYPelsPerMeter) ); 00160 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biClrUsed = %d\n", m_pBitmapInfoHeader_st.biClrUsed) ); 00161 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("biClrImportant = %d\n", m_pBitmapInfoHeader_st.biClrImportant) ); 00162 00163 // move file pointer to beginning of bitmap data 00164 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Go to the start of data\n") ); 00165 fseek( filePtr, bitmapFileHeader.bfOffBits, SEEK_SET ); 00166 00167 // allocate enough memory for the bitmap image data 00168 m_pucBitmapImage = new unsigned char[ m_pBitmapInfoHeader_st.biSizeImage ]; 00169 00170 // verify memory allocation 00171 if ( !m_pucBitmapImage ) { 00172 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Couldn't allocate memory for the texture\n") ); 00173 fclose( filePtr ); 00174 return false; 00175 } 00176 else { 00177 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("memory allocated for the texture, move on\n") ); 00178 } 00179 00180 // read in the bitmap image data 00181 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Read bitmap pixel data\n") ); 00182 fread( m_pucBitmapImage, 1, m_pBitmapInfoHeader_st.biSizeImage, filePtr ); 00183 00184 // make sure bitmap image data was read 00185 //if ( !m_pucBitmapImage ) { 00186 // fclose( filePtr ); 00187 // return false; 00188 //} 00189 00190 // swap the R and B values to get RGB since the bitmap color format is 00191 // in BGR 00192 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Change BGR to RGB\n") ); 00193 for ( uiImageIdx = 0; uiImageIdx < m_pBitmapInfoHeader_st.biSizeImage; uiImageIdx += 3 ) { 00194 ucTempRGB = m_pucBitmapImage[ uiImageIdx ]; 00195 m_pucBitmapImage[ uiImageIdx ] = m_pucBitmapImage[ uiImageIdx + 2 ]; 00196 m_pucBitmapImage[ uiImageIdx + 2 ] = ucTempRGB; 00197 } 00198 00199 // close the file 00200 fclose( filePtr ); 00201 00202 BITMAPIMAGE_H_DEBUG_MODE_BY_PRINTF( ("Done image loading\n") ); 00203 return true; // successful loading 00204 } // END: LoadBitMapFile() 00205 00206 //------------------------------------------------------------------------- 00207 // MEMBER FUNCTION: GetImageSize() 00208 // DESC: 00209 unsigned int GetWidth() const { 00210 return static_cast<unsigned int>( m_pBitmapInfoHeader_st.biHeight ); 00211 //return m_ucWidth; 00212 } // END: GetWidth() 00213 //------------------------------------------------------------------------- 00214 // MEMBER FUNCTION: GetImageSize() 00215 // DESC: 00216 unsigned int GetHeight() const { 00217 //return m_ucHeight; 00218 return static_cast<unsigned int>( m_pBitmapInfoHeader_st.biHeight ); 00219 } // END: GetHeight() 00220 //------------------------------------------------------------------------- 00221 // MEMBER FUNCTION: GetImageSize() 00222 // DESC: 00223 unsigned int GetImageSize() const { 00224 return static_cast<unsigned int>( m_pBitmapInfoHeader_st.biSizeImage ); 00225 } // END: GetImageSize() 00226 //------------------------------------------------------------------------- 00227 // MEMBER FUNCTION: GetConstPtrToImageData() 00228 // DESC: 00229 unsigned char * const GetConstPtrToImageData() const { 00230 return m_pucBitmapImage; 00231 } // END: GetConstPtrToImageData() 00232 //------------------------------------------------------------------------- 00233 // MEMBER FUNCTION: Available() 00234 // DESC: 00235 bool Available() const { 00236 return m_pucBitmapImage != NULL ? true : false; 00237 } // END: Available() 00238 //------------------------------------------------------------------------- 00239 00240 //****************************************************************************** 00241 // Data Members 00242 private: 00243 static const WORD SM_BITMAP_ID; 00244 private: 00245 BITMAPINFOHEADER m_pBitmapInfoHeader_st; // a pointer to the image info header 00246 unsigned char *m_pucBitmapImage; // a pointer to the image data 00247 //unsigned char m_ucWidth; // image width 00248 //unsigned char m_ucHeight; // image height 00249 }; // END CLASS BitmapImage 00250 //----------------------------------------------------------------------------- 00251 //============================================================================= 00252 END_NAMESPACE_TAPs 00253 //----------------------------------------------------------------------------- 00254 // Include definition if TAPs_USE_EXPORT is not defined 00255 #if !defined( TAPs_USE_EXPORT ) 00256 #include "TAPsBitmapImageHandler.cpp" 00257 #endif 00258 //----------------------------------------------------------------------------- 00259 #endif 00260 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00261 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8