Vertically flip TGA data

Started by
13 comments, last by cozzie 18 years, 5 months ago
Hi all, I'm trying to flip my tga file data vertically, because it's up-side-down saved in the TGA-file. I've tested texturing my faces with "tempImageData", which works correct. But after flipping the data (using the source below), the image looks exactly the same )-; Can't seem to find out what I'm doing wrong. Any idea?

//	flip upside down saved TGA's if needed

	GLubyte *tempImageData = (GLubyte*) malloc(tga.imageSize);
	memcpy(tempImageData, imageData, tga.imageSize);

	GLuint i;

	delete[] imageData;
	imageData = new GLubyte[tga.imageSize];

	for(i=0;i<(int)tga.imageSize;i+=tga.bytesPerPixel)
	{
		imageData = tempImageData[tga.imageSize-i];
		imageData[i+1] = tempImageData[tga.imageSize-i+1];
		imageData[i+2] = tempImageData[tga.imageSize-i+2];
		if(bpp==32) imageData[i+3] = tempImageData[tga.imageSize-i+3];
	}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement
How did it become upside down? Anyways an easier route for a few files would be to load them into an art program and just flip them.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

You need to flip row-by-row, not pixel-by-pixel.
You should never let your fears become the boundaries of your dreams.
Can you flip the image in place as follows. This is set up for 4-byte pixels.

typedef unsigned long Pixel;Pixel *image = (Pixel *) imageData;long h = height / 2;for (long a = 0; a < h; a++){    Pixel *top = image + a * width;    Pixel *bot = image + (height - a - 1) * width;    for (long b = 0; b < width; b++)    {        Pixel p = *top;        *top++ = *bot;        *bot++ = p;    }}

Why not just change the texture coordinates to flip it? Also isn't there a matrix that will flip it? Color matrix or something?
Keys to success: Ability, ambition and opportunity.
Quote:Original post by LilBudyWizer
Why not just change the texture coordinates to flip it? Also isn't there a matrix that will flip it? Color matrix or something?


That would be the texture matrix. It modifies texture coordinates just like the modelview matrix affects vertices, so it could be used for that for sure. I think it might be better to just flip the TGA files in some art program. It would be quicker than developing and debugging code to do it. Why reinvent the wheel except for learning purposes?


If you go through the tga spec, a part of the header specifies the x and y origin (zero coordinate) of the image. You could extract those values and see if they help you when you pass in texture coordinates or modify the texture matrix.

This happened to me too when I wrote my TGA loader too, and I agree with the others that it would be easier to flip in an art program. The GIMP has an option when you save TGA whether you want the lower left as origin (which you should change and test a little). You would need no flipping in that case, either. [smile]
Actually I was thinking if you were using draw pixels. I got confused with the color matrix which transforms the colors in the image. You can use pixel zoom to flip the image though. I'm pretty sure I've done it and it works with -1 as the zoom factor.
Keys to success: Ability, ambition and opportunity.
Lot's of options I hear ;-)

Flipping the images would do the trick, if all my textures are from the same graphics program (which I can probably manage).
It seemed cleaner to me to read from the header of the TGA if the image is upsidedown and flip it in that situation.
That way it all stays clean code, in my opinion.

I'll go for the option eric is referring too, and I'm afraid darkwing's right when I look at my code right now.

Eric: can I post up my adjusted flipping code if it doesn't work?
Thanks all for the reactions.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Hello Eric,

Unfortunately I just don't get it )-;
This is what I've got so far:

//	flip upside down saved TGA's if neededGLubyte *tempImageData = (GLubyte*) malloc(tga.imageSize);memcpy(tempImageData, imageData, tga.imageSize);delete[] imageData;imageData = new GLubyte[tga.imageSize];// copy tempImageData (upsidedown image) flipped into imageDatalong a;long h = tga.height/2;for(a=0;a<h;a++){}


The original upsidedown TGA-data is in "tempImageData" and needs to be saved flipped in "imageData". The original "tempImageData" is saved as followed:

"rgbrgbrgbrgbrgb.... etc" or "rgbargbargbargba... etc"
(tga.bpp == 24) or (tga.bpp == 32)

Probably in the second loop (which goes through the pixels per row), there should be a check "if bpp==32" then read/write the alpha value.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement