\
biSizeImage=(biWidth*biBitCount+31)/32*4*biHeight
inline void GetColor( unsigned int x, unsigned int y, unsigned char* ucpRed, unsigned char* ucpGreen, unsigned char* ucpBlue )//============================================================== //============================================================== //= image.h ==================================================== //= Original coders: Trent Polack ([email protected]) and = //- Evan Pipho ([email protected]) = //============================================================== //= Image/texture loadings ops = //============================================================== //============================================================== #ifndef __IMAGE_H__ #define __IMAGE_H__ //-------------------------------------------------------------- //-------------------------------------------------------------- //- HEADERS AND LIBRARIES -------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> //-------------------------------------------------------------- //-------------------------------------------------------------- //- CONSTANTS -------------------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- #define BITMAP_ID 0x4D42 //-------------------------------------------------------------- //-------------------------------------------------------------- //- STRUCTURES ------------------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- struct TGAInformationHeader { unsigned char m_ucHeader[6]; unsigned int m_uiBytesPerPixel; unsigned int m_uiImageSize; unsigned int m_uiTemp; unsigned int m_uiType; unsigned int m_uiHeight; unsigned int m_uiWidth; unsigned int m_uiBPP; }; //设置1字节对齐,入栈push1 #pragma pack( push, 1 ) struct BMPFileHeader { unsigned short usType; //2位 unsigned int uiSize; //4位 unsigned short usReserved1;//2位 unsigned short usReserved2; //2位 unsigned int uiOffBits; //4位 }; //取消1字节对齐,pop 1..出栈 #pragma pack( pop ) struct BMPInfoHeader { unsigned int uiSize; long lWidth; long lHeight; unsigned short usPlanes; unsigned short usBitCount; unsigned int uiCompression; unsigned int uiSizeImage; long lXPelsPerMeter; long lYPelsPerMeter; unsigned int uiClrUsed; unsigned int uiClrImportant; }; //-------------------------------------------------------------- //-------------------------------------------------------------- //- GLOBALS ---------------------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- extern unsigned char g_ucUTGAcompare[12]; extern unsigned char g_ucCTGAcompare[12]; //-------------------------------------------------------------- //-------------------------------------------------------------- //- CLASS ------------------------------------------------------ //-------------------------------------------------------------- //-------------------------------------------------------------- class CIMAGE { private: unsigned char* m_ucpData; unsigned int m_uiWidth; unsigned int m_uiHeight; unsigned int m_uiBPP; unsigned int m_ID; bool m_bIsLoaded; bool LoadBMP( void ); bool SaveBMP( char* szFilename ); bool LoadCompressedTGA( void ); bool LoadUncompressedTGA( void ); public: bool Create( unsigned int uiWidth, unsigned int uiHeight, unsigned int uiBPP ); bool LoadData( char* a_szFilename ); bool Load( char* a_szFilename, float a_fMinFilter, float a_fMaxFilter, bool a_bMipmap= false ); void Unload( void ); bool Save( char* szFilename ); //-------------------------------------------------------------- // Name: CIMAGE::GetColor - public // Description: Get the color (RGB triplet) from a texture pixel // Arguments: -x, y: position to get color from // -ucpRed, ucpGreen, ucpBlue: place to store the RGB // values that are extracted from the texture // Return Value: None //-------------------------------------------------------------- inline void GetColor( unsigned int x, unsigned int y, unsigned char* ucpRed, unsigned char* ucpGreen, unsigned char* ucpBlue ) { unsigned int uiBPP= m_uiBPP/8; if( ( x<m_uiWidth ) && ( y<m_uiHeight ) ) { *ucpRed = m_ucpData[( ( y*m_uiHeight )+x )*uiBPP]; *ucpGreen= m_ucpData[( ( y*m_uiHeight )+x )*uiBPP+1]; *ucpBlue = m_ucpData[( ( y*m_uiHeight )+x )*uiBPP+2]; } } //-------------------------------------------------------------- // Name: CIMAGE::SetColor - public // Description: Set the color (RGB triplet) for a texture pixel // Arguments: -x, y: position to set color at // -ucpRed, ucpGreen, ucpBlue: color to set at pixel // Return Value: None //-------------------------------------------------------------- inline void SetColor( unsigned int x, unsigned int y, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue ) { unsigned int uiBPP= m_uiBPP/8; if( ( x<m_uiWidth ) && ( y<m_uiHeight ) ) { m_ucpData[( ( y*m_uiHeight )+x )*uiBPP] = ucRed; m_ucpData[( ( y*m_uiHeight )+x )*uiBPP+1]= ucGreen; m_ucpData[( ( y*m_uiHeight )+x )*uiBPP+2]= ucBlue; } } //-------------------------------------------------------------- // Name: CIMAGE::GetData - public // Description: Get a pointer to the image's data buffer // Arguments: None // Return Value: An unsigned char buffer (the texture's data) //-------------------------------------------------------------- inline unsigned char* GetData( void ) { return m_ucpData; } //-------------------------------------------------------------- // Name: CIMAGE::GetWidth - public // Description: Get the texture's width (size along the x-axis) // Arguments: None // Return Value: An unsigned int value: the texture's size along the x-axis //-------------------------------------------------------------- inline unsigned int GetWidth( void ) { return m_uiWidth; } //-------------------------------------------------------------- // Name: CIMAGE::GetHeight - public // Description: Get the texture's height (size along the y-axis) // Arguments: None // Return Value: An unsigned int value: the texture's size along the y-axis //-------------------------------------------------------------- inline unsigned int GetHeight( void ) { return m_uiHeight; } //-------------------------------------------------------------- // Name: CIMAGE::GetBPP - public // Description: Get the texture's number of bits per pixel // Arguments: None // Return Value: An unsigned int value: the texture's bits per pixel //-------------------------------------------------------------- inline unsigned int GetBPP( void ) { return m_uiBPP; } //-------------------------------------------------------------- // Name: CIMAGE::GetID - public // Description: Get the texture's identifying ID (use with OpenGL) // Arguments: None // Return Value: An unsigned int value: the texture's ID //-------------------------------------------------------------- inline unsigned int GetID( void ) { return m_ID; } //-------------------------------------------------------------- // Name: CIMAGE::SetID - public // Description: Set the texture's identifying ID (use with OpenGL) // Arguments: -uiID: the texture's soon-to-be ID // Return Value: None //-------------------------------------------------------------- inline void SetID( unsigned int uiID ) { m_ID= uiID; } //-------------------------------------------------------------- // Name: CIMAGE::IsLoaded - public // Description: Find out if the texture has been loaded or not // Arguments: None // Return Value: A boolean value: -true: a texture has been loaded // -false: a texture hasn't been loaded //-------------------------------------------------------------- inline bool IsLoaded( void ) { return m_bIsLoaded; } }; #endif //__IMAGE_H__
//首先,我们关心的是它的接口:
inline bool IsLoaded( void )
inline void SetID( unsigned int uiID )
inline unsigned int GetID( void )
inline unsigned int GetBPP( void )
inline unsigned int GetHeight( void )
inline unsigned int GetWidth( void )
inline unsigned char* GetData( void )
inline void SetColor( unsigned int x, unsigned int y, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
inline void GetColor( unsigned int x, unsigned int y, unsigned char* ucpRed, unsigned char* ucpGreen, unsigned char* ucpBlue )
就这么多。。。。。。。
它对应于一个文件。。
前面n多是GetSet方法。。。。关键是如何实现的Load和LoadData;;
//创建一个新的BMP(在内存中的)『实际上是个二维数组。。。』
bool Create( unsigned int uiWidth, unsigned int uiHeight, unsigned int uiBPP );
//这个函数用指定的filename来保存此对象中的数据。
bool Save( char* szFilename );
//卸载Load进来的数据。
void Unload( void );
//这个函数用来加载BMP文件并且制作纹理,纹理ID可以通过
// inline unsigned int GetID( void ) 得到
//如果用这个函数,那么纹理ID已经设好。。。。。。。。。。。。一定不能再去SetID
bool Load( char* a_szFilename, float a_fMinFilter, float a_fMaxFilter, bool a_bMipmap= false );
//纯粹的把BMP内容加载进来
//下面这个函数没有纹理ID,因此,必须手动设置纹理ID。
bool LoadData( char* a_szFilename );