I've found this quite a tricky issue. Here's my current function (which uses Round):
Code: Select all
;shrink image to fit rectangle (maintain aspect ratio)
JEE_MathsImageFitRectShrink(vImgW, vImgH, vLimW, vLimH, ByRef vImgW3, ByRef vImgH3)
{
vImgW3 := vImgH3 := ""
if (vImgW <= vLimW) && (vImgH <= vLimH)
{
vImgW3 := vImgW, vImgH3 := vImgH
return
}
vRatioW := vLimW / vImgW ;to fit the image width-wise (although might be too high)
vRatioH := vLimH / vImgH ;to fit image height-wise (although might be too wide)
vImgH1 := Round(vImgH * vRatioW) ;vImgW1 := vLimW
if (vImgH1 <= vLimH)
{
vImgW3 := vLimW, vImgH3 := vImgH1
if (vRatioW > vRatioH)
return
}
vImgW2 := Round(vImgW * vRatioH) ;vImgH2 := vLimH
if (vImgW2 <= vLimW)
vImgW3 := vImgW2, vImgH3 := vLimH
;diagnostic:
if (vImgW3 = "")
MsgBox, % "error: " A_ThisFunc "`r`n" "img: " vImgW " " vImgH "`r`n" "lim: " vLimW " " vLimH "`r`n" vImgW1 " " vImgH1 "`r`n" vImgW2 " " vImgH2
}
Code: Select all
q:: ;confirm that JEE_MathsImageFitRectShrink never returns blanks
vOutput := ""
vNum := 51
Loop, % vNum
{
vLimH := A_Index-1
Loop, % vNum
{
vLimW := A_Index-1
Loop, % vNum
{
vImgH := A_Index-1
Loop, % vNum
{
vImgW := A_Index-1
;if the function fails, then:
;a MsgBox within the function should trigger
JEE_MathsImageFitRectShrink(vImgW, vImgH, vLimW, vLimH, vImgW3, vImgH3)
}
}
}
}
Clipboard := vOutput
MsgBox, % "done"
return