Sign in to follow this  
MadsGustaf

Retaining aspect ratio when resizing images

Recommended Posts

This probably belongs in the "For beginners" forum, as it probaly has a rather obvious answer, however i cannot seem to get this right, so i am calling for help at GameDev :p If a image is larger than i allow, i would want to resize the image (setting the axis that is too big to the maximum for that axis) and calculate the new width/height for the second axis while still retaining the aspect ratio between the width and height. Here is the code i am using. 500 is the maximum width, and 600 is the maximum height that i am allowing.
[source langg = "cpp"]
if(TextureImage[0]->sizeX > 500 && TextureImage[0]->sizeX >= TextureImage[0]->sizeY )
		{
		 double newheight= 500/TextureImage[0]->sizeX * TextureImage[0]->sizeY ;
		 vector[0].Set(500,newheight);
		}
		else if(TextureImage[0]->sizeY > 600 && TextureImage[0]->sizeY >= TextureImage[0]->sizeX)
		{
		 double newwidth= 600/TextureImage[0]->sizeY * TextureImage[0]->sizeX ;
		 vector[0].Set(newwidth,600);
		}
		else
		{
		 vector[0].Set(TextureImage[0]->sizeX, TextureImage[0]->sizeY);
		}

the vector array simply holds the width and height for the individual images.

Share this post


Link to post
Share on other sites
So what's your problem? Runtime error? compiler error? Integer truncation when doing division? Computer turning pink and emitting sound waves just beyond human hearing range?

Share this post


Link to post
Share on other sites
Well, the problem is nothing is being drawn, when either the image's width is bigger 500 or the images height is greater than 600.
I highly doubt it is related, but i am drawing the following way:

[source langg = "cpp"]
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(WIDTH/2 - vector[0].width/2, vector[0].height, 0); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( WIDTH/2 + vector[0].width/2, vector[0].height, 0); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( WIDTH/2 + vector[0].width/2, 0, 0); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(WIDTH/2 - vector[0].width/2, 0, 0); //top left of the texture and quad
glEnd();
return TRUE;


I can draw just fine if i call this function:
vector[0].Set(TextureImage[0]->sizeX, TextureImage[0]->sizeY);
But then i am not resizing, So my problem isn't related to drawing to the screen. It is the 'if' sentences above, that there is something wrong with.

Share this post


Link to post
Share on other sites
Then the problem is probably integer truncation while doing division. Assuming that your image sizes are specified with integer types, then lines like:

double newheight= 500/TextureImage[0]->sizeX * TextureImage[0]->sizeY ;

Divide an integer value 500, by another integer value TextureImage[0]->sizeX, which is strictly greater than 500, which results in a value below 1, which truncates to 0. So the multiplication by sizeY turns into a 0. Cast one or both of the division arguments to a floating point type before doing the division.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this