Skip to content

Instantly share code, notes, and snippets.

@IUnknown68
Created March 12, 2015 13:00
Show Gist options
  • Save IUnknown68/b32bc4c7b351b9d579f4 to your computer and use it in GitHub Desktop.
Save IUnknown68/b32bc4c7b351b9d579f4 to your computer and use it in GitHub Desktop.
Constrain image size
bool ConstrainImageSize(
int &aWidth, // in/out - actual image width
int &aHeight, // in/out - actual image height
const int aMaxWidth, // in - max width
const int aMaxHeight // in - max height
) const
{
float ratioNeed = std::max( (float)aWidth / (float)aMaxWidth, (float)aHeight / (float)aMaxHeight );
if( ratioNeed > 1.0 ) {
// actually needs downscaling at all
aWidth = (int)((float)aWidth / ratioNeed + 0.5);
aHeight = (int)((float)aHeight /ratioNeed + 0.5);
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment