Jump to content
  • Advertisement
Sign in to follow this  
Kazade

Flipping Image Data

This topic is 4861 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 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.

Share this post


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

Share this post


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

Share this post


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

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!