Jump to content
  • Advertisement
Sign in to follow this  
cozzie

Vertically flip TGA data

This topic is 4870 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 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];
	}

Share this post


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

Share this post


Link to post
Share on other sites
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;
}
}


Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Hello Eric,

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


// flip upside down saved TGA's if needed

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

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

// copy tempImageData (upsidedown image) flipped into imageData

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

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.

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!