Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mithoric

Read, Edit and save .tga file

This topic is 5493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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..
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.

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!