Code:
void
ScaleImage(float img_w, float img_h, float w, float h,
float &scale_w, float &scale_h)
{
if(img_w > w || img_h > h) { // scale down below natural size
const float r_w = img_w / w;
const float r_h = img_h / h;
const float r = (r_w < r_h) ? r_w : r_h;
scale_w = r / r_h;
scale_h = r / r_w;
} else { // scale (down) to natural size
scale_w = img_w / w;
scale_h = img_h / h;
}
}
This is voodoo math, I wrote it and for some reason it works, and works well, don't know why. I guess I have to thank myself.
Bookmarks