Skip to content

Isolated the calculation added a test and added Rounding - #242

Merged
Dynom merged 4 commits into
h2non:masterfrom
Dynom:RoundingDimensionsCalculation-238
Jan 25, 2019
Merged

Dynom merged 4 commits into
h2non:masterfrom
Dynom:RoundingDimensionsCalculation-238

Conversation

@Dynom

@Dynom Dynom commented Jan 21, 2019 •

Copy link
Copy Markdown
Collaborator

This issue partially offers 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 ¯\_(ツ)_/¯

@Dynom
Dynom requested a review from h2non January 21, 2019 16:55
@Dynom

Dynom commented Jan 21, 2019

Copy link
Copy Markdown
Collaborator Author

@lastonoga any feedback ? -- #238

Comment thread image.go
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)))

@yuecelm yuecelm Jan 21, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in bimg I saw using int(math.Floor(f + 0.5)) instead of int(math.Round(f)), not sure why...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still use math.Round for better readability

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the sanity check! I think math.Round() is better as well. Performance in this section is hardly a bottleneck.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Image credits: James Ree, original: https://unsplash.com/photos/nM7djMZfSuo

@Dynom
Dynom merged commit 7190e85 into h2non:master Jan 25, 2019
@Dynom

Dynom commented Jan 25, 2019

Copy link
Copy Markdown
Collaborator Author

Seems that it solves the problem that inspired this PR, merging it back!

@Dynom
Dynom deleted the RoundingDimensionsCalculation-238 branch January 25, 2019 11:02
@Dynom Dynom added this to the 1.0.18 milestone Jan 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ 换网址