typedef struct
{
BYTE Header[12]; // TGA File Header
} TGAHeader;
typedef struct
{
BYTE header[6]; // First 6 Useful Bytes From The Header
UINT bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File
UINT imageSize; // Used To Store The Image Size When Setting Aside Ram
UINT temp; // Temporary Variable
UINT type;
UINT Height; //Height of Image
UINT Width; //Width ofImage
UINT Bpp; // Bits Per Pixel
BYTE *imageData; // Image data
} TGA;
int Pow2Size(char *filePath)
{
TGAHeader tgaheader;
TGA tga;
FILE *fTGA = fopen(filePath,"rb");
if(fTGA == NULL)
return -1;
if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) > 0)
{
rewind(fTGA);
fread(tga.header, sizeof(tga.header), 1, fTGA);
}
tga.Width = tga.header[1] * 256 + tga.header[0];
tga.Height = tga.header[3] * 256 + tga.header[2];
tga.Bpp = tga.header[4];
tga.bytesPerPixel = tga.Bpp / 8;
tga.imageSize = tga.bytesPerPixel * tga.Width * tga.Height;
tga.imageData = (BYTE*)malloc(tga.imageSize);
fread(tga.imageData,1,tga.imageSize,fTGA);
fclose(fTGA);
int newHeight=1024,newWidth=1024;
bool go=true;
while(go)
{
if(tga.Height >= newHeight)
{
tga.Height = newHeight;
break;
}else{
newHeight = newHeight/2;
}
if(newHeight < 2)
go = false;
}
go=true;
while(go)
{
if(tga.Width >= newWidth)
{
tga.Width = newWidth;
break;
}else{
newWidth = newWidth/2;
}
if(newWidth < 2)
go = false;
}
fTGA = fopen(filePath,"w");
tga.header[1] = newWidth;
tga.header[3] = newHeight;
tga.header[4] = tga.Bpp;
fwrite(&tgaheader,1,sizeof(TGAHeader),fTGA);
fwrite(&tga.header,1,sizeof(tga.header),fTGA);
fwrite(&tga.imageData,1,sizeof(tga.imageSize),fTGA);
fclose(fTGA);
return 0;
}
Sort of pieced the code together from nehe''s tga tutorial.
Thanks in advance.
Read, Edit and save .tga file
Ok, I''m trying to read a tga file, reduce it''s dimensions to a power of 2 size depending on initial size and then resave it.. I think I screwed up the saving bit though..
First, you should always call free if you call malloc .
Second, doing fwrite(&tga.imageData,1,sizeof(tga.imageSize),fTGA); is probaly not what you wanted to do . That sizeof will return 4, since that's the size of an integer. I'm assuming you just meant:
fwrite(&tga.imageData,1,tga.imageSize,fTGA);
--- Edit ---
Also, I noticed you modify the size of the TGA, yet you doing re-scale the data before writing it out, this will not work properly if the width/height values change, you would have to go through the entire image, and scale it down.
[edited by - Ready4Dis on November 28, 2003 9:36:58 AM]
Second, doing fwrite(&tga.imageData,1,sizeof(tga.imageSize),fTGA); is probaly not what you wanted to do . That sizeof will return 4, since that's the size of an integer. I'm assuming you just meant:
fwrite(&tga.imageData,1,tga.imageSize,fTGA);
--- Edit ---
Also, I noticed you modify the size of the TGA, yet you doing re-scale the data before writing it out, this will not work properly if the width/height values change, you would have to go through the entire image, and scale it down.
[edited by - Ready4Dis on November 28, 2003 9:36:58 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement