Repository navigation
Isolated the calculation added a test and added Rounding - #242
Conversation
|
@lastonoga any feedback ? -- #238 |
| func calculateDestinationFitDimension(imageWidth, imageHeight, fitWidth, fitHeight int) (int, int) { | ||
| if imageWidth*fitHeight > fitWidth*imageHeight { | ||
| // constrained by width | ||
| fitHeight = int(math.Round(float64(fitWidth) * float64(imageHeight) / float64(imageWidth))) |
There was a problem hiding this comment.
in bimg I saw using int(math.Floor(f + 0.5)) instead of int(math.Round(f)), not sure why...
There was a problem hiding this comment.
found an explanation in https://golang.org/src/math/floor.go: math.Floor have (architecture-specific) implementations in assembler (e.g. in https://golang.org/src/math/floor_386.s), math.Round not. seems they prefer math.Floor because of the higher performance.
There was a problem hiding this comment.
I would still use math.Round for better readability
There was a problem hiding this comment.
Thanks for the sanity check! I think math.Round() is better as well. Performance in this section is hardly a bottleneck.
There was a problem hiding this comment.
I wanted also check if we should use math.Ceil rather than math.Round, I took a peek in libvips but I didnt come yet to a result. or we could play with vipsthumbnail if e.g. a case with 411.4 px will be rounded up (-> use math.Ceil) or rounded down (-> use math.Round)
There was a problem hiding this comment.
libvips uses Rounding, when scaling a 900x500 (a nice fractional conversion size) image to 312 outer dimensions, it rounds to 173 instead of 174.
- 312 / (900/500) = 173.3
- 313 / (900/500) = 173.8
The command
vipsthumbnail ./james-ree-1312004-unsplash.jpg -s 312x312 -o tn_%s.jpg --vips-cache-trace
produces a 312x173 size.

Image credits: James Ree, original: https://unsplash.com/photos/nM7djMZfSuo
|
Seems that it solves the problem that inspired this PR, merging it back! |
This issue
partiallyoffers a fix for #238. However this PR doesn't remove the black line, which still needs fixing.I'm not actually sure if this calculation is correct. Several authors have touched this part and no rounding happend in the previous calculations. This cautions me to be optimistic about it's correctness, although they now do seem "sensible".
I'd happily receive some feedback from anyone about the correctness of the math involved
¯\_(ツ)_/¯