Opengl读取TGA图片信息代码

首先是了解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  {              //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;
}

你可能感兴趣的:(OpenGL,C++)