實例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
風輕云淡 閱讀(2492)
評論(1) 編輯 收藏 引用 所屬分類:
圖像讀取