Compressed or not, is it a problem? - TGA

Started by
1 comment, last by HalfLucifer 22 years, 10 months ago
I''m wondering that what''s the main difference between uncompressed and compressed TGA images? I mean, if I''d like to load compressed TGA images, then what shall I modify in TGA loading code to get things right? Will it depend on the compressed technique that used? I got a TGA image that cannot be loaded correctly by Nehe''s TGA base code. However, while I removed this line below: if (memcmp(TGAheader, TGAcompare, sizeof(TGAheader))!=0) return false; Everything''s gonna be alright. TGA image is loaded correctly. Thus, I am thinking if comparing the image header bits (GLubyte TGAheader[12] = {0,0,2,0,0,0,0,0,0,0,0,0} is necessary? And does the usage of compressed and uncompressed TGA is the same or similar or what?
Advertisement
NeHe''s code cannot load compressed TGA''s, but the memcmp is a little restrictive (it cannot load lots of TGA''s that his code is compeletly funtional with). Loading compressed TGA''s is really easy though, and can reduce the file size a lot with many images. Here''s the code I have (based off of NeHe''s) that does either compressed or uncompressed 24/32 bit TGA''s (of course, you''ll need to modify it a bit to get it to work for you):
  riBool riTexture::LoadTarga(FILE *File) {  riByte TGAHeader[12], Header[6], Chunk[4], PacketHead, *At, *Data;  riUInt BPP, ImageSize, Temp, Type = GL_RGBA;  riCoord2Di Size;  if(fread(TGAHeader,1,sizeof(TGAHeader),File) != sizeof(TGAHeader) ||     fread(Header,1,sizeof(Header),File) != sizeof(Header)) {    fclose(File);    return RI_FALSE;  }  Size.X = Header[1] * 256 + Header[0];  Size.Y = Header[3] * 256 + Header[2];  if(Size.X <= 0 || Size.Y <= 0 || (Header[4]!=24 && Header[4]!=32)) {    fclose(File);    return RI_FALSE;  }  BPP = Header[4]/8;  ImageSize = Size.X * Size.Y * BPP;  Data = new riByte[ImageSize];  if(Data == NULL) {    fclose(File);    return RI_FALSE;  }  if(TGAHeader[2] == 2) {    if(fread(Data, 1, ImageSize, File) != ImageSize) {      fclose(File);      return RI_FALSE;    }  } else if(TGAHeader[2] == 10) {    At = Data;    for(riUInt BytesDone=0; BytesDone<ImageSize; ) {      PacketHead = (riByte) fgetc(File);      if(PacketHead & 0x80) {        PacketHead &= 0x7F;        PacketHead++;        fread(Chunk,BPP,1,File);        for(riByte b=0; b<PacketHead; b++) {          memcpy(At,Chunk,BPP);          BytesDone += BPP;          At += BPP;        }      } else {        PacketHead &= 0x7F;        PacketHead++;        fread(At, BPP, PacketHead, File);        BytesDone += PacketHead * BPP;        At += PacketHead * BPP;      }    }  } else {    delete [] Data;    return RI_FALSE;  }  for(riUInt i=0; i<ImageSize; i+=BPP) {    Temp = Data[i];    Data[i] = Data[i+2];    Data[i+2] = Temp;  }  glGenTextures(1,&TexID);  glBindTexture(GL_TEXTURE_2D, TexID);  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  if(Header[4]==24) {    Type = GL_RGB;  }  glTexImage2D(GL_TEXTURE_2D, 0, Type, Size.X, Size.Y, 0,               Type, GL_UNSIGNED_BYTE, Data);  delete [] Data;  return RI_TRUE;}  


[Resist Windows XP''s Invasive Production Activation Technology!]
Also, I should have taken the lines with "fclose(File);" out of there, since the function that calls LoadTarga passes and closes the FILE pointer.

[Resist Windows XP''s Invasive Production Activation Technology!]

This topic is closed to new replies.

Advertisement