Advertisement

Flipping Image Data

Started by July 26, 2005 07:07 AM
2 comments, last by Kazade 19 years, 1 month ago
Hi everyone, I'm in the middle of writing my own texture library and im having a bit of a problem. To cut down on the code for my bitmap loader i used SDL's built in BMP loader. This works fine except it loads the bitmap upside down and mirrored. I've managed to reverse the data so it only needs flipping horizontally, but i cant figure out how. Heres my code:

	bool CBitmap::Load(std::string filename)
	{
		tempBMP = SDL_LoadBMP(filename.c_str());

		if (!tempBMP)
		{
			LoadError("Could not load bitmap!\n");
			return false;
		}

		width = tempBMP->w;
		height = tempBMP->h;
		bpp = tempBMP->format->BytesPerPixel;
		
		if (bpp == 3)
			type = GL_BGR_EXT;
		else if (bpp == 4)
			type = GL_BGRA_EXT;
		else
		{
			LoadError("Incompatible Type!\n");
			return false;
		}

		int imageSize = width * height * bpp;

		data = (GLubyte*) malloc(imageSize);
		
		GLubyte* temp = (GLubyte*) malloc(imageSize);
		memcpy(temp, tempBMP->pixels, imageSize);

		for (int i = 0; i < imageSize; i+=3)		//Flip image vertical
		{
			data = temp[imageSize - i];
			data[i+1] = temp[imageSize - i+1];
			data[i+2] = temp[imageSize - i+2];
		}
	
		free(temp);

		SDL_FreeSurface(tempBMP);

		return true;

Anyone have any ideas? I know i can just change my opengl coordinates or the texture matrix, but then it messes up my other image format loading. Cheers Luke.
Member of the NeHe team.
I don't know much about OpenGL,
but all you need I think for horizontal switching is something like the following loop:

for( y = 0; y< ysize_of_image; y++ )
for( x = 0; x < xsize_of_image; x++ )
data[ y * xsize_of_image + x ] =
temp[ y * ( xsize_of_image + 1 ) - x - 1 ];

By the way:
data = temp[imageSize - i];

this seems to be memory violation for i == 0
as you read from temp[imageSize], which is outside your allocated memory.
Advertisement
Quote: Original post by Sparhawk42

By the way:
data = temp[imageSize - i];

this seems to be memory violation for i == 0
as you read from temp[imageSize], which is outside your allocated memory.


Thanks adjusted for that now, I still cant get it to flip. Because data has 3 bytes of data per pixel, i need to keep those intact and yet switch them horizontally. I'm getting all sorts of wierd results. I think ive been staring at it too long.

Luke.
Member of the NeHe team.
I've done it, thanks for the help.

If anyone is interested heres how:

	bool CBitmap::Load(std::string filename)	{		tempBMP = SDL_LoadBMP(filename.c_str());		if (!tempBMP)		{			LoadError("Could not load bitmap!\n");			return false;		}		width = tempBMP->w;		height = tempBMP->h;		bpp = tempBMP->format->BytesPerPixel;				if (bpp == 3)			type = GL_BGR_EXT;		else if (bpp == 4)			type = GL_BGRA_EXT;		else		{			LoadError("Incompatible Type!\n");			return false;		}		int imageSize = (width * height * bpp) - bpp;		data = (GLubyte*) malloc(imageSize);				GLubyte* temp = (GLubyte*) malloc(imageSize);		memcpy(temp, tempBMP->pixels, imageSize);		for (int i = 0; i < imageSize; i+=bpp)		//Flip image vertical		{			data = temp[imageSize - i];			data[i+1] = temp[imageSize - i+1];			data[i+2] = temp[imageSize - i+2];		}		memcpy(temp, data, imageSize);		int w = 0, x = 0, y = 0;		for (int i = 0; i < imageSize; i+=bpp)		//Flip image horizontal		{			w = ((y + 1) * width*3) - (x*3) - 3;			data = temp[w];			data[i+1] = temp[w+1];			data[i+2] = temp[w+2];			x++;			if (x == width)			{				x = 0;				y++;			}		}		free(temp);		SDL_FreeSurface(tempBMP);		return true;	}
Member of the NeHe team.

This topic is closed to new replies.

Advertisement