Jump to content
  • Advertisement
Sign in to follow this  
Deliverance

Simple question about TGA's

This topic is 4235 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

Hi, hello again! I have a little problem: I'm trying to save an image in TGA format, here are the functions I'm using:
bool CImage::WriteTGA(char *fileName, int offset)
{	
	CLogger logger("log.txt");	

	FILE* Output = fopen(fileName, "wb");	
		
	if ( !Output )
	{	
		logger.Message("Error - <WriteTGA>: Cannot write to file %s", fileName);
	}

	fseek(Output, offset, SEEK_SET);
			
	unsigned char header[18] = {0};
	header[2] = NO_RLE;	
	header[12] = width;	
	header[13] = width >> 8;
	header[14] = height;
	header[15] = height >> 8;			
	header[16] = components << 3;
														 		
	fwrite(header, 18, 1, Output);

	unsigned char *newData = new unsigned char[width * height * components];
	memcpy(newData, pixelData, width * height * components);
	
	// RGB -> BGR
	int i;
	for ( i = 0; i < width * height; i++)
	{												 
		unsigned char temp = newData[i * components + 0];
		newData[i * components + 0] = pixelData[i * components + 2];
		newData[i * components + 2] = temp;
	}

	fwrite(newData, width * height * components, 1, Output);
		
	fclose(Output);
		
	delete[] newData;
	newData = NULL;

	return true;
}

void CImage::SetPixel(int i, int j, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{	
	pixelData[i*width*components + j*components + 0] = r;	
	pixelData[i*width*components + j*components + 1] = g;
	pixelData[i*width*components + j*components + 2] = b;
	if ( components == 4 )
		pixelData[i*width*components + j*components + 3] = a;
}

tColor4ub CImage::GetPixel(int i, int j)
{	
	tColor4ub color;
	color.r = pixelData[i*width*components + j*components + 0];
	color.g = pixelData[i*width*components + j*components + 1];
	color.b = pixelData[i*width*components + j*components + 2];
	if ( components == 4 )
		color.a = pixelData[i*width*components + j*components + 3];
	
	return color;
}

void CImage::CreateEmpty(int width, int height, int components, unsigned char *pixelData)
{
	this->width = width;
	this->height = height;
	this->components = components;
		
	this->pixelData = new unsigned char[width * height * components];
	memset(this->pixelData, 0, width * height * components);
	if (pixelData)
		memcpy(this->pixelData, pixelData, width * height * components);
}

Okay I'm trying to create now, using the functions above an image that will have the top row all white. Instead of that I get an image that has the bottom wor all whitr, why? here is the code that implements this
CImage img;
img.CreateEmpty(64, 64, 3, NULL);
int collon;
for ( collon = 0; collon < 64; ++collon )
	img.SetPixel( 0, collon, 255, 255, 255, 255);
img.WriteTGA("test.tga", 0);
img.Free();

and here is the output: Image Hosted by ImageShack.us (The last line is white :) )

Share this post


Link to post
Share on other sites
Advertisement
because the coord's (x,0) are at the bottom of the image. try filling coords (x,63).....

If the height value is negative, then the image would go the other way (and your code would do as you expect). The only problem with that, is that most people don't support negative widths & heights. So basically, just remember that 0,0 is the bottom left....

Share this post


Link to post
Share on other sites
The last byte of the header normally sets the origin of the image, be it top left, top right, bottom left or bottom right. See this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!