实例LoadTGA.rar下载Image.h
#ifndef _IMAGE_H_
#define _IMAGE_H_
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#pragma once
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// These defines are used to tell us about the type of TARGA file it is
#define TGA_RGB 2 // This tells us it's a normal RGB (really BGR) file
#define TGA_A 3 // This tells us it's a ALPHA file
#define TGA_RLE 10 // This tells us that the targa is Run-Length Encoded (RLE)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#pragma pack( push, 1 ) //禁止字节自动对齐
typedef struct
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
GLubyte descLen;
GLubyte cmapType;
GLubyte imageType;
GLshort cmapStart;
GLushort cmapEntries;
GLubyte cmapBits;
GLushort xOffset;
GLushort yOffset;
}Header;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
typedef struct
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Header head;
GLushort width;
GLushort height;
GLubyte bpp;
GLubyte attrib;
}TGAHeader;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
typedef struct _tTexImage
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
GLubyte* imageData;
GLuint width;
GLuint height;
GLuint bpp; //Image color depth in bits per pixel
GLuint texID;
GLuint imageType;
GLboolean bCompressed; //Compressed or Uncompressed
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_tTexImage():imageData(NULL)
{}
}TexImage;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#pragma pack( pop )
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class Image
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
Image(void);
~Image(void);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 加载TGA图片 **/
BOOL loadTGA( TexImage* texture, LPCSTR filename );
BOOL release( TexImage* texture );
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//** 生成纹理 **/
void generateTexture( TexImage* texture, BOOL bMipmap = TRUE );
protected:
BOOL loadUncompressedTGA( TexImage* texture, FILE* file );
BOOL loadCompressedTGA( TexImage* texture, FILE* file );
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#endif
Image.cpp
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
说说异或运算^和他的一个常用作用。
异或的运算方法是一个二进制运算:
1^1=0
0^0=0
1^0=1
0^1=1
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
两者相等为0,不等为1.
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
这样我们发现交换两个整数的值时可以不用第三个参数。
如a=11,b=9.以下是二进制
a=a^b=1011^1001=0010;
b=b^a=1001^0010=1011;
a=a^b=0010^1011=1001;
这样一来a=9,b=13了。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
举一个运用, 按一个按钮交换两个mc的位置可以这样。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
mybt.onPress=function()
{
mc1._x=mc1._x^mc2._x;
mc2._x=mc2._x^mc1._x;
mc1._x=mc1._x^mc2._x;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
mc1._y=mc1._y^mc2._y;
mc2._y=mc2._y^mc1._y;
mc1._y=mc1._y^mc2._y;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
这样就可以不通过监时变量来传递了。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
最后要声明:只能用于整数。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
vertex[0].position = vec2( -width/2, height/2 );
vertex[1].position = vec2( width/2, height/2 );
vertex[2].position = vec2( width/2, -height/2 );
vertex[3].position = vec2( -width/2, -height/2 );
*/
#include "StdAfx.h"
#include "global.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Image::Image(void)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Image::~Image(void)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//-----------------------------------------------------------------------
// 函数名 : Image::loadTGA
// 说明 : 加载TGA图片
// 返回 : BOOL
// 参数 : TexImage* texture
// 参数 : LPCSTR filename
// 作者 : Teng
// 创建时间 : 2010-4-14 15:35:32
// 最后修改 : 2010-4-14
//-----------------------------------------------------------------------
BOOL Image::loadTGA( TexImage* texture, LPCSTR filename )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if ( filename == NULL )
return FALSE;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Header uTGAcompare =
{ 0,0,2,0,0,0,0,0}; //2为非压缩RGB格式 3 - 未压缩的,黑白图像
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Header cTGAcompare =
{ 0,0,10,0,0,0,0,0}; //10为压缩RGB格式
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
TGAHeader header;
FILE* file = fopen( filename, "rb" );
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( !file )
{
TRACE("Openf file %s failed!\n", filename );
return FALSE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( fread( &header, 1, sizeof(TGAHeader), file ) != sizeof( TGAHeader ) )
{ //读取TGA整个头结构体
if ( file )
fclose( file );
TRACE("Read data failed\n");
return FALSE;
}
texture->width = header.width;
texture->height = header.height;
texture->bpp = header.bpp;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if ( header.bpp == 32 )
texture->imageType = GL_RGBA;
else if ( header.bpp = 24 )
texture->imageType = GL_RGB;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
TRACE("Image type error!\n");
return FALSE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( memcmp( &uTGAcompare, &header.head, sizeof(header.head) )== 0 )
{ //未压缩TGA
texture->bCompressed = FALSE;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( !loadUncompressedTGA( texture, file ) )
{
TRACE("Load uncompressed TGA failed!\n");
return FALSE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if ( memcmp( &cTGAcompare, &header.head ,sizeof(header.head) ) == 0 )
{ //压缩TGA
texture->bCompressed = TRUE;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( !loadCompressedTGA( texture, file ) )
{
TRACE("Load compressed TGA failed!\n");
return FALSE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
TRACE("Error TGA type!\n");
return FALSE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return TRUE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//-----------------------------------------------------------------------
// 函数名 : Image::generateTexture
// 说明 : 生成一张纹理
// 返回 : void
// 参数 : TexImage* texture
// 参数 : BOOL bMipmap /*= TRUE*/
// 作者 : Teng
// 创建时间 : 2010-5-25 17:06:42
// 最后修改 : 2010-5-25
//-----------------------------------------------------------------------
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
void Image::generateTexture( TexImage* texture, BOOL bMipmap /**//*= TRUE*/ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//Bulid a textue from the data.
glGenTextures( 1, &texture->texID );
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_texWrap);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_texWrap);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_texFilter);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_texFilter);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_texMode);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
glBindTexture( GL_TEXTURE_2D, texture->texID );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( bMipmap )
{
gluBuild2DMipmaps( GL_TEXTURE_2D, texture->bpp/8, texture->width,texture->height, texture->imageType, GL_UNSIGNED_BYTE, texture->imageData );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
glTexImage2D( GL_TEXTURE_2D, 0, texture->bpp/8, texture->width, texture->height, 0, texture->imageType, GL_UNSIGNED_BYTE, texture->imageData );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//-----------------------------------------------------------------------
// 函数名 : Image::loadUncompressedTGA
// 说明 : 加载未压缩TGA纹理
// 返回 : BOOL
// 参数 : TexImage* texture
// 参数 : FILE* file 当前file指针,指向TGA图像第一个像素地址
// 作者 : Teng
// 创建时间 : 2010-4-14 14:39:22
// 最后修改 : 2010-4-14
//-----------------------------------------------------------------------
BOOL Image::loadUncompressedTGA( TexImage* texture, FILE* file )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
ASSERT( file != NULL && texture!=NULL );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
GLuint bytePerPixel = texture->bpp/8;
GLuint imgSize = texture->width*texture->height*bytePerPixel; //图像总字节数
texture->imageData = new GLubyte[ imgSize ];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if ( fread( texture->imageData, 1, imgSize, file ) != imgSize )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TRACE("Read texture imagedata failed!\n");
return FALSE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//TGA采用了逆OpenGL的格式,要将BGR转换成为RGB
// Go through all of the pixels and swap the B and R values since TGA
// files are stored as BGR instead of RGB (or use GL_BGR_EXT verses GL_RGB)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for( int i = 0; i < (int)imgSize; i+=bytePerPixel )
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*GLushort temp = texture->imageData[i];
texture->imageData[i] = texture->imageData[i+2];
texture->imageData[i+2] = temp;*/
texture->imageData[i] ^= texture->imageData[i+2] ^= texture->imageData[i] ^= texture->imageData[ i+2 ]; //位操作提高速度,更换B,R分量
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
::fclose( file );
return TRUE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//-----------------------------------------------------------------------
// 函数名 : Image::loadCompressedTGA
// 说明 : 加载压缩TGA纹理
// 返回 : BOOL
// 参数 : TexImage* texture
// 参数 : FILE* file
// 作者 : Teng
// 创建时间 : 2010-4-14 14:38:55
// 最后修改 : 2010-4-14
//-----------------------------------------------------------------------
BOOL Image::loadCompressedTGA( TexImage* texture, FILE* file )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
ASSERT( file != NULL && texture!=NULL );
GLuint bytePerPixel = texture->bpp/8;
GLuint imgSize = texture->width*texture->height*bytePerPixel;
texture->imageData = new GLubyte[ imgSize ];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
GLuint pixelcount = texture->width * texture->height;
GLuint currentPixel = 0; //当前正在读取的像素
GLuint currentByte = 0; //当前正在向图像中写入的像素
GLubyte *colorbuffer = (GLubyte *)malloc( bytePerPixel ); // 一个像素的存储空间s
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
do
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GLubyte chunkHeader = 0; //存储ID块值的变量
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( !fread( &chunkHeader,1, sizeof( GLubyte ), file ) )
{
return FALSE;
}
if ( chunkHeader < 128 ) //RAW块
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
chunkHeader++; // 变量值加1以获取RAW像素的总数
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for( int i = 0; i < chunkHeader; i++ )
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( fread( colorbuffer, 1,sizeof( bytePerPixel ), file ) != sizeof( bytePerPixel ) )
{
TRACE("Read pixel failed!\n");
return FALSE;
}
texture->imageData[currentByte] = colorbuffer[ 2 ];
texture->imageData[currentByte+1] = colorbuffer[1];
texture->imageData[currentByte+2] = colorbuffer[0];
if ( bytePerPixel == 4 )
texture->imageData[ currentByte+3] = colorbuffer[3];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
currentPixel++;
currentByte += bytePerPixel;
}
}
//下一段处理描述RLE段的“块”头。首先我们将chunkheader减去127来得到获取下一个颜色重复的次数。
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
chunkHeader -= 127; //减去127获得ID bit 的rid 开始循环拷贝我们多次读到内存中的像素,这由RLE头中的值规定。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( fread( colorbuffer,1, sizeof( bytePerPixel ), file ) != sizeof( bytePerPixel ) )
{
TRACE("Read pixel failed!\n");
return FALSE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for( int i = 0; i < chunkHeader; i++ )
{
texture->imageData[ currentByte ] = colorbuffer[ 2 ]; // 拷贝“R”字节
texture->imageData[ currentByte +1 ] = colorbuffer[ 1 ]; // 拷贝“G”字节
texture->imageData[ currentByte + 2 ] = colorbuffer[ 0 ]; // 拷贝“B”字节
if ( bytePerPixel == 4 )
texture->imageData[ currentByte+3 ] = colorbuffer[ 3 ]; // 拷贝“A”字节
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
currentPixel++;
currentByte += bytePerPixel;
}
}
}while( currentPixel < pixelcount );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
free( colorbuffer );
::fclose( file );
return TRUE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//-----------------------------------------------------------------------
// 函数名 : Image::release
// 说明 : 释放资源
// 返回 : BOOL
// 参数 : TexImage* texture
// 作者 : Teng
// 创建时间 : 2010-5-25 17:42:59
// 最后修改 : 2010-5-25
//-----------------------------------------------------------------------
BOOL Image::release( TexImage* texture )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if ( !texture )
return FALSE;
if ( texture->imageData )
delete[] texture->imageData;
glDeleteTextures( 1, &texture->texID );
return TRUE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2010-05-26 10:05
风轻云淡 阅读(2451)
评论(1) 编辑 收藏 引用 所属分类:
图像读取