![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsTargaImageHandler.hpp 00003 00004 Targa Image Handler 00005 Adapted from OpenGL Superbible (3rd Edition) (2005) 00006 00007 SUKITTI PUNAK (10/27/2004) 00008 UPDATE (09/10/2010) 00009 ******************************************************************************/ 00010 #ifndef TAPs_TARGA_IMAGE_HANDLER_HPP 00011 #define TAPs_TARGA_IMAGE_HANDLER_HPP 00012 00013 #include <iostream> 00014 #include <GL/glext.h> 00015 00016 #ifndef _WINDOWS_ 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 TARGAIMAGE_HPP_DEBUG_MODE 00027 #ifdef TARGAIMAGE_HPP_DEBUG_MODE 00028 #define TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( s ) { printf( "DEBUG MESSAGE: " ); printf s; } 00029 #else 00030 #define TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( s ) {} 00031 #endif 00032 00033 BEGIN_NAMESPACE_TAPs 00034 //============================================================================= 00035 // Targa File Header 00036 #pragma pack(1) 00037 typedef struct tagTARGAHEADER 00038 { 00039 unsigned char identsize; // Size of ID field that follows header (0) 00040 unsigned char colorMapType; // 0 = None, 1 = paletted 00041 unsigned char imageType; // 0 = none, 1 = indexed, 2 = rgb, 3 = grey, +8=rle 00042 unsigned short colorMapStart; // First colour map entry 00043 unsigned short colorMapLength; // Number of colors 00044 unsigned char colorMapBits; // bits per palette entry 00045 unsigned short xstart; // image x origin 00046 unsigned short ystart; // image y origin 00047 unsigned short width; // width in pixels 00048 unsigned short height; // height in pixels 00049 unsigned char bits; // bits per pixel (8 16, 24, 32) 00050 unsigned char descriptor; // image descriptor 00051 00052 } TARGAHEADER; 00053 #pragma pack(8) 00054 //============================================================================= 00055 // CLASS: Targa Image 00056 // DESC: Handle a .TGA image file. 00057 class TargaImage { 00058 //****************************************************************************** 00059 // Member Functions 00060 public: 00061 //------------------------------------------------------------------------- 00062 // Constructor 00063 TargaImage() 00064 : m_pucImage( NULL ), m_uiWidth( 0 ), m_uiHeight( 0 ), 00065 m_uiComponents( GL_BGR_EXT ), m_uiFormat( GL_RGB8 ) 00066 {} // END: Constructor 00067 //------------------------------------------------------------------------- 00068 // Destructor 00069 ~TargaImage() 00070 { 00071 // reclaim the allocated memory if any 00072 if ( m_pucImage != NULL ) { 00073 delete [] m_pucImage; 00074 m_pucImage = NULL; 00075 } 00076 } // END: Destructor 00077 //------------------------------------------------------------------------- 00078 // MEMBER FUNCION: LoadFile() 00079 // DESC: 00080 bool LoadFile( char *pcFileName ) 00081 { 00082 FILE *filePtr; // the file pointer 00083 TARGAHEADER targaHeader; // targa header 00084 unsigned long lImageSize; // size in bytes of image 00085 short sDepth; // pixel depth 00086 00087 // Reclaim the allocated memory if any 00088 if ( m_pucImage != NULL ) { 00089 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Erase an old TGA image data\n") ); 00090 delete [] m_pucImage; 00091 m_pucImage = NULL; 00092 } 00093 else { 00094 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("the TGA image pointer is new\n") ); 00095 } 00096 00097 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Try open the file\n") ); 00098 00099 std::cout << "Name: " << pcFileName << std::endl; 00100 // Try to open filename in "read binary" mode 00101 if ( ( filePtr = fopen( pcFileName, "rb" ) ) == NULL ) { 00102 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Unsucceed opening the TGA image file\n") ); 00103 return false; 00104 } 00105 else { 00106 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Succeed opening the TGA image file\n") ); 00107 } 00108 00109 // Read in header (binary) 00110 fread( &targaHeader, 18/*sizeof( TARGAHEADER )*/, 1, filePtr ); 00111 00112 // Check for supporting only 8, 24, or 32 bit targas 00113 if ( static_cast<int>(targaHeader.bits) != 8 00114 && static_cast<int>(targaHeader.bits) != 24 00115 && static_cast<int>(targaHeader.bits) != 32 ) { 00116 std::cout << static_cast<int>(targaHeader.bits) << std::endl; 00117 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Unsupported format (not 8, 24, or 32 bits)\n") ); 00118 fclose( filePtr ); 00119 return false; 00120 } 00121 else { 00122 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("The format is supported (8, 24, or 32 bits)\n") ); 00123 } 00124 00125 // Get width, height, and depth of image 00126 m_uiWidth = targaHeader.width; 00127 m_uiHeight = targaHeader.height; 00128 sDepth = targaHeader.bits / 8; 00129 std::cout << "width: " << m_uiWidth << std::endl; 00130 std::cout << "height: " << m_uiHeight << std::endl; 00131 std::cout << "depth: " << sDepth << std::endl; 00132 00133 // Calculate the size of image buffer 00134 lImageSize = targaHeader.width * targaHeader.height * sDepth; 00135 00136 // Allocate memory and check for success 00137 m_pucImage = new unsigned char[lImageSize]; 00138 if ( !m_pucImage ) { 00139 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Cannot allocate memory for the image\n") ); 00140 fclose( filePtr ); 00141 return false; 00142 } 00143 else { 00144 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Memory is allocated for the image\n") ); 00145 } 00146 00147 // Read in the image data 00148 // Check for read error. 00149 // Should catch for RLE (compression) or other unsupported formats 00150 if ( fread( m_pucImage, lImageSize, 1, filePtr ) != 1 ) { 00151 delete [] m_pucImage; 00152 m_pucImage = NULL; 00153 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Cannot read the image data correctly\n") ); 00154 fclose( filePtr ); 00155 return false; 00156 } 00157 else { 00158 TARGAIMAGE_HPP_DEBUG_MODE_BY_PRINTF( ("Image data is read successfully\n") ); 00159 } 00160 00161 // Set OpenGL format expected 00162 switch (sDepth) { 00163 case 3: // most likely case 00164 std::cout << "GL_RGB8" << std::endl; 00165 m_uiFormat = GL_BGR_EXT; 00166 m_uiComponents = GL_RGB8; 00167 break; 00168 case 4: // with alpha channel 00169 std::cout << "GL_RGBA8" << std::endl; 00170 m_uiFormat = GL_BGRA_EXT; 00171 m_uiComponents = GL_RGBA8; 00172 break; 00173 case 1: 00174 std::cout << "GL_LUMINANCE8" << std::endl; 00175 m_uiFormat = GL_LUMINANCE; 00176 m_uiComponents = GL_LUMINANCE8; 00177 break; 00178 }; 00179 00180 // Done with file 00181 fclose( filePtr ); 00182 return true; 00183 } // END: LoadFile() 00184 00185 00186 //------------------------------------------------------------------------- 00187 // MEMBER FUNCTION: getImageSize() 00188 // DESC: 00189 unsigned int GetWidth() const { 00190 return m_uiWidth; 00191 //return m_ucWidth; 00192 } // END: getWidth() 00193 //------------------------------------------------------------------------- 00194 // MEMBER FUNCTION: getImageSize() 00195 // DESC: 00196 unsigned int GetHeight() const { 00197 //return m_ucHeight; 00198 return m_uiHeight; 00199 } // END: getHeight() 00200 //------------------------------------------------------------------------- 00201 // MEMBER FUNCTION: getImageSize() 00202 // DESC: 00203 unsigned int GetImageSize() const { 00204 return m_uiWidth * m_uiHeight; 00205 } // END: getImageSize() 00206 //------------------------------------------------------------------------- 00207 // MEMBER FUNCTION: getConstPtrToImageData() 00208 // DESC: 00209 unsigned char * const GetConstPtrToImageData() const { 00210 return m_pucImage; 00211 } // END: getConstPtrToImageData() 00212 //------------------------------------------------------------------------- 00213 // MEMBER FUNCTION: Available() 00214 // DESC: 00215 bool Available() const { 00216 return m_pucImage != NULL ? true : false; 00217 } // END: Available() 00218 //------------------------------------------------------------------------- 00219 00220 //****************************************************************************** 00221 // Data Members 00222 private: 00223 static const WORD SM_BITMAP_ID; 00224 private: 00225 unsigned char * m_pucImage; // a pointer to the image data 00226 unsigned int m_uiWidth; // image width 00227 unsigned int m_uiHeight; // image height 00228 unsigned int m_uiComponents; // component; GL_RGB8, GL_RGBA8, GL_LUMINANCE 00229 unsigned int m_uiFormat; // targa format; 8, 24, or 32 bits 00230 }; // END CLASS TargaImage 00231 //----------------------------------------------------------------------------- 00232 //============================================================================= 00233 END_NAMESPACE_TAPs 00234 //----------------------------------------------------------------------------- 00235 #endif 00236 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00237 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8