Page 1 of 1

TGA / JPG loading Bug in OpenGL! (partial fix)

Posted: Mon May 08, 2006 11:04 am
by chromdragon
when loading a .tga texture (IRR must flip the image so that is right but it does not)

when loading the some img in .jpg format the img si flip correctly!

Posted: Mon May 08, 2006 11:49 am
by hybrid
There is a tag in tga which tells the app if the image should be flipped or not. This is not honored by Irrlicht. But, there are also tools which do not set this tag correctly, so even when flipping the image it might still not work with some gfx tools. But yes, should be fixed to be standard conforming.
Is the behavior in JPG also buggy, or did you just meant that with jpg it's working correctly?

Posted: Mon May 08, 2006 12:45 pm
by chromdragon
A quick hack for now!

Code: Select all

void CImage::flip(bool yup, bool xup)
{
	c8 *cData = (c8 *) Data;
	s32 maxX = Size.Width, maxY = Size.Height;

	if (Format == video::ECF_A8R8G8B8 && yup == true)
	{
		for(s32 y=0; y<maxY/2-1; y++)
		for(s32 x=0; x<maxX; x++)
		{
				c8 oldColor[4];

				oldColor[0] = cData[Size.Width * y*4 + x*4 + 0];
				oldColor[1] = cData[Size.Width * y*4 + x*4 + 1];
				oldColor[2] = cData[Size.Width * y*4 + x*4 + 2];
				oldColor[3] = cData[Size.Width * y*4 + x*4 + 3];

				cData[Size.Width * y*4 + x*4 + 0] = cData[Size.Width * (maxY-y-1)*4 + x*4 + 0];
				cData[Size.Width * y*4 + x*4 + 1] = cData[Size.Width * (maxY-y-1)*4 + x*4 + 1];
				cData[Size.Width * y*4 + x*4 + 2] = cData[Size.Width * (maxY-y-1)*4 + x*4 + 2];
				cData[Size.Width * y*4 + x*4 + 3] = cData[Size.Width * (maxY-y-1)*4 + x*4 + 3];

				cData[Size.Width * (maxY-y-1)*4 + x*4 + 0] = oldColor[0];
				cData[Size.Width * (maxY-y-1)*4 + x*4 + 1] = oldColor[1];
				cData[Size.Width * (maxY-y-1)*4 + x*4 + 2] = oldColor[2];
				cData[Size.Width * (maxY-y-1)*4 + x*4 + 3] = oldColor[3];
		}
	}

	else if (Format == video::ECF_R8G8B8 && yup == true)
	{
		for(s32 y=0; y<maxY/2-1; y++)
		for(s32 x=0; x<maxX; x++)
		{
				c8 oldColor[3];

				oldColor[0] = cData[Size.Width * y*3 + x*3 + 0];
				oldColor[1] = cData[Size.Width * y*3 + x*3 + 1];
				oldColor[2] = cData[Size.Width * y*3 + x*3 + 2];

				cData[Size.Width * y*3 + x*3 + 0] = cData[Size.Width * (maxY-y-1)*3 + x*3 + 0];
				cData[Size.Width * y*3 + x*3 + 1] = cData[Size.Width * (maxY-y-1)*3 + x*3 + 1];
				cData[Size.Width * y*3 + x*3 + 2] = cData[Size.Width * (maxY-y-1)*3 + x*3 + 2];

				cData[Size.Width * (maxY-y-1)*3 + x*3 + 0] = oldColor[0];
				cData[Size.Width * (maxY-y-1)*3 + x*3 + 1] = oldColor[1];
				cData[Size.Width * (maxY-y-1)*3 + x*3 + 2] = oldColor[2];
		}
	}

	if (Format == video::ECF_A8R8G8B8 && xup == true)
	{
		for(s32 y=0; y<maxY; y++)
		for(s32 x=0; x<maxX/2-1; x++)
		{
				c8 oldColor[4];

				oldColor[0] = cData[Size.Width * y*4 + x*4 + 0];
				oldColor[1] = cData[Size.Width * y*4 + x*4 + 1];
				oldColor[2] = cData[Size.Width * y*4 + x*4 + 2];
				oldColor[3] = cData[Size.Width * y*4 + x*4 + 3];

				cData[Size.Width * y*4 + x*4 + 0] = cData[Size.Width * y*4 + (maxX-x-1)*4 + 0];
				cData[Size.Width * y*4 + x*4 + 1] = cData[Size.Width * y*4 + (maxX-x-1)*4 + 1];
				cData[Size.Width * y*4 + x*4 + 2] = cData[Size.Width * y*4 + (maxX-x-1)*4 + 2];
				cData[Size.Width * y*4 + x*4 + 3] = cData[Size.Width * y*4 + (maxX-x-1)*4 + 3];

				cData[Size.Width * y*4 + (maxX-x-1)*4 + 0] = oldColor[0];
				cData[Size.Width * y*4 + (maxX-x-1)*4 + 1] = oldColor[1];
				cData[Size.Width * y*4 + (maxX-x-1)*4 + 2] = oldColor[2];
				cData[Size.Width * y*4 + (maxX-x-1)*4 + 3] = oldColor[3];
		}
	}

	else if (Format == video::ECF_R8G8B8 && xup == true)
	{
		for(s32 y=0; y<maxY; y++)
		for(s32 x=0; x<maxX/2-1; x++)
		{
				c8 oldColor[3];

				oldColor[0] = cData[Size.Width * y*3 + x*3 + 0];
				oldColor[1] = cData[Size.Width * y*3 + x*3 + 1];
				oldColor[2] = cData[Size.Width * y*3 + x*3 + 2];

				cData[Size.Width * y*3 + x*3 + 0] = cData[Size.Width * y*3 + (maxX-x-1)*3 + 0];
				cData[Size.Width * y*3 + x*3 + 1] = cData[Size.Width * y*3 + (maxX-x-1)*3 + 1];
				cData[Size.Width * y*3 + x*3 + 2] = cData[Size.Width * y*3 + (maxX-x-1)*3 + 2];

				cData[Size.Width * y*3 + (maxX-x-1)*3 + 0] = oldColor[0];
				cData[Size.Width * y*3 + (maxX-x-1)*3 + 1] = oldColor[1];
				cData[Size.Width * y*3 + (maxX-x-1)*3 + 2] = oldColor[2];
		}
	}
}