实例LoadTGA.rar下载Image.h
#ifndef _IMAGE_H_
#define _IMAGE_H_
#pragma once
// 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)
#pragma pack( push, 1 ) //禁止字节自动对齐
typedef struct
{
GLubyte descLen;
GLubyte cmapType;
GLubyte imageType;
GLshort cmapStart;
GLushort cmapEntries;
GLubyte cmapBits;
GLushort xOffset;
GLushort yOffset;
}Header;
typedef struct
{
Header head;
GLushort width;
GLushort height;
GLubyte bpp;
GLubyte attrib;
}TGAHeader;
typedef struct _tTexImage
{
GLubyte* imageData;
GLuint width;
GLuint height;
GLuint bpp; //Image color depth in bits per pixel
GLuint texID;
GLuint imageType;
GLboolean bCompressed; //Compressed or Uncompressed
_tTexImage():imageData(NULL){}
}TexImage;
#pragma pack( pop )
class Image
{
public:
Image(void);
~Image(void);
public:
/**//** 加载TGA图片 **/
BOOL loadTGA( TexImage* texture, LPCSTR filename );
BOOL release( TexImage* texture );
/**//** 生成纹理 **/
void generateTexture( TexImage* texture, BOOL bMipmap = TRUE );
protected:
BOOL loadUncompressedTGA( TexImage* texture, FILE* file );
BOOL loadCompressedTGA( TexImage* texture, FILE* file );
};
#endif
Image.cpp
/**//*
说说异或运算^和他的一个常用作用。
异或的运算方法是一个二进制运算:
1^1=0
0^0=0
1^0=1
0^1=1
两者相等为0,不等为1.
这样我们发现交换两个整数的值时可以不用第三个参数。
如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了。
举一个运用, 按一个按钮交换两个mc的位置可以这样。
mybt.onPress=function()
{
mc1._x=mc1._x^mc2._x;
mc2._x=mc2._x^mc1._x;
mc1._x=mc1._x^mc2._x;
mc1._y=mc1._y^mc2._y;
mc2._y=mc2._y^mc1._y;
mc1._y=mc1._y^mc2._y;
}
这样就可以不通过监时变量来传递了。
最后要声明:只能用于整数。
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"
Image::Image(void)
{
}
Image::~Image(void)
{
}
//-----------------------------------------------------------------------
// 函数名 : 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 )
{
if ( filename == NULL )
return FALSE;
Header uTGAcompare = { 0,0,2,0,0,0,0,0}; //2为非压缩RGB格式 3 - 未压缩的,黑白图像
Header cTGAcompare = { 0,0,10,0,0,0,0,0}; //10为压缩RGB格式
TGAHeader header;
FILE* file = fopen( filename, "rb" );
if ( !file ){
TRACE("Openf file %s failed!\n", filename );
return FALSE;
}
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;
if ( header.bpp == 32 )
texture->imageType = GL_RGBA;
else if ( header.bpp = 24 )
texture->imageType = GL_RGB;
else{
TRACE("Image type error!\n");
return FALSE;
}
if ( memcmp( &uTGAcompare, &header.head, sizeof(header.head) )== 0 ){ //未压缩TGA
texture->bCompressed = FALSE;
if ( !loadUncompressedTGA( texture, file ) ){
TRACE("Load uncompressed TGA failed!\n");
return FALSE;
}
}else if ( memcmp( &cTGAcompare, &header.head ,sizeof(header.head) ) == 0 ){ //压缩TGA
texture->bCompressed = TRUE;
if ( !loadCompressedTGA( texture, file ) ){
TRACE("Load compressed TGA failed!\n");
return FALSE;
}
}else{
TRACE("Error TGA type!\n");
return FALSE;
}
return TRUE;
}
//-----------------------------------------------------------------------
// 函数名 : Image::generateTexture
// 说明 : 生成一张纹理
// 返回 : void
// 参数 : TexImage* texture
// 参数 : BOOL bMipmap /*= TRUE*/
// 作者 : Teng
// 创建时间 : 2010-5-25 17:06:42
// 最后修改 : 2010-5-25
//-----------------------------------------------------------------------
void Image::generateTexture( TexImage* texture, BOOL bMipmap /**//*= TRUE*/ )
{
//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);
glBindTexture( GL_TEXTURE_2D, texture->texID );
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);
}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 );
}
}
//-----------------------------------------------------------------------
// 函数名 : 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 )
{
ASSERT( file != NULL && texture!=NULL );
GLuint bytePerPixel = texture->bpp/8;
GLuint imgSize = texture->width*texture->height*bytePerPixel; //图像总字节数
texture->imageData = new GLubyte[ imgSize ];
if ( fread( texture->imageData, 1, imgSize, file ) != imgSize )
{
TRACE("Read texture imagedata failed!\n");
return FALSE;
}
//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)
for( int i = 0; i < (int)imgSize; i+=bytePerPixel ){
/**//*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分量
}
::fclose( file );
return TRUE;
}
//-----------------------------------------------------------------------
// 函数名 : 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 )
{
ASSERT( file != NULL && texture!=NULL );
GLuint bytePerPixel = texture->bpp/8;
GLuint imgSize = texture->width*texture->height*bytePerPixel;
texture->imageData = new GLubyte[ imgSize ];
GLuint pixelcount = texture->width * texture->height;
GLuint currentPixel = 0; //当前正在读取的像素
GLuint currentByte = 0; //当前正在向图像中写入的像素
GLubyte *colorbuffer = (GLubyte *)malloc( bytePerPixel ); // 一个像素的存储空间s
do
{
GLubyte chunkHeader = 0; //存储ID块值的变量
if ( !fread( &chunkHeader,1, sizeof( GLubyte ), file ) ){
return FALSE;
}
if ( chunkHeader < 128 ) //RAW块
{
chunkHeader++; // 变量值加1以获取RAW像素的总数
for( int i = 0; i < chunkHeader; i++ ){
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];
currentPixel++;
currentByte += bytePerPixel;
}
}
//下一段处理描述RLE段的“块”头。首先我们将chunkheader减去127来得到获取下一个颜色重复的次数。
else
{
chunkHeader -= 127; //减去127获得ID bit 的rid 开始循环拷贝我们多次读到内存中的像素,这由RLE头中的值规定。
if ( fread( colorbuffer,1, sizeof( bytePerPixel ), file ) != sizeof( bytePerPixel ) ){
TRACE("Read pixel failed!\n");
return FALSE;
}
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”字节
currentPixel++;
currentByte += bytePerPixel;
}
}
}while( currentPixel < pixelcount );
free( colorbuffer );
::fclose( file );
return TRUE;
}
//-----------------------------------------------------------------------
// 函数名 : Image::release
// 说明 : 释放资源
// 返回 : BOOL
// 参数 : TexImage* texture
// 作者 : Teng
// 创建时间 : 2010-5-25 17:42:59
// 最后修改 : 2010-5-25
//-----------------------------------------------------------------------
BOOL Image::release( TexImage* texture )
{
if ( !texture )
return FALSE;
if ( texture->imageData )
delete[] texture->imageData;
glDeleteTextures( 1, &texture->texID );
return TRUE;
}
posted on 2010-05-26 10:05
风轻云淡 阅读(2469)
评论(1) 编辑 收藏 引用 所属分类:
图像读取