Opengl读取TGA图片信息代码

9 篇文章 0 订阅

首先是了解opengl里AUX_RGBImageRec的定义,它包含图片宽高和数据

typedef struct _AUX_RGBImageRec {
    GLint sizeX,sizeY;
    unsignedchar *data;
} AUX_RGBImageRec;

然后就是从TAG图片里提取出这些信息放入AUX_RGBImageRec的对象

想从TAG图片里提取出这些信息得了解TAG图片文件格式,至于格式读者请自行了解如第12到16字节保存了图像宽和高第18字节以后保存了图像数据域

AUX_RGBImageRec* CMap::LoadTGA(char *filename)
{
 AUX_RGBImageRec* TGAImage=newAUX_RGBImageRec;
 GLubyte  TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; //Uncompressed TGA Header,图像类型码 置2
 GLubyte  TGAcompare[12];        //Used To Compare TGA Header
 GLubyte  header[6];         //First 6 Useful Bytes From The Header
 GLuint  bytesPerPixel;        //Holds Number Of Bytes Per Pixel Used In The TGA File
 GLuint  imageSize;         //Used To Store The Image Size When Setting Aside Ram
 GLuint  temp;          //Temporary Variable
 GLuint  type=GL_RGBA;        //Set The Default GL Mode To RBGA (32 BPP)

 FILE *file = fopen(filename,"rb");      //Open The TGA File

 if( file==NULL||          //Does File Even Exist?
  fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare)|| // Are There 12 Bytes To Read?
  memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0    || //Does The Header Match What We Want?
  fread(header,1,sizeof(header),file)!=sizeof(header))    //If So Read Next 6 Header Bytes
 {
  if (file ==NULL)         //Did The File Even Exist? *Added Jim Strong*
   returnfalse;         //Return False
  else
  {
   fclose(file);         //If Anything Failed, Close The File
   returnfalse;         //Return False
  }
 }

 TGAImage->sizeX = header[1] * 256 +header[0];   // 宽和高以16位字保存,所以要读高位和地位Width (highbyte*256+lowbyte)
 TGAImage->sizeY = header[3] * 256+header[2];   //Determine The TGAHeight (highbyte*256+lowbyte)
                                                          

  if( TGAImage->sizeX <=0 ||        //Is The Width Less Than Or Equal To Zero
  TGAImage->sizeY <=0 ||        //Is The Height Less Than Or Equal To Zero
  (header[4]!=24&&header[4]!=32))     //Is The TGA 24 or 32 Bit? RGB或RGBA
 {
  fclose(file);          //If Anything Failed, Close The File
  returnfalse;          //Return False
 }

 bytesPerPixel =header[4]/8;      //Divide By 8 To Get The Bytes Per Pixel
 imageSize  =TGAImage->sizeX*TGAImage->sizeY*bytesPerPixel; //Calculate The Memory Required For The TGA Data

 TGAImage->data=(GLubyte*)malloc(imageSize);  // ReserveMemory To Hold The TGA Data

 if( TGAImage->data==NULL||       //Does The Storage Memory Exist?
  fread(TGAImage->data,1, imageSize, file)!=imageSize) // Does The ImageSize Match The Memory Reserved?
 {
  if(TGAImage->data!=NULL)      //Was Image Data Loaded
   free(TGAImage->data);      //If So, Release The Image Data

  fclose(file);          //Close The File
  returnfalse;          //Return False
 }

 for(GLuint i=0;i<int(imageSize);i+=bytesPerPixel)  // LoopThrough The Image Data
              //Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
  temp=TGAImage->data[i];       //Temporarily Store The Value At Image Data 'i'
  TGAImage->data[i]= TGAImage->data[i + 2]; // Set The1st Byte To The Value Of The 3rd Byte
  TGAImage->data[i+ 2] =temp;     //Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
 }

 fclose(file);           //Close The File

 return TGAImage;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值