fix: setting width and height to Contain
is not preserving aspect ratio of tall images
#6554
+15
−36
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fixes #3708, fixes #4407
The scaling for
"Contain"
uses the wrong direct proportion formula for tall images. This is due to the image'saspect
beingwidth / height
. The original formulascaled_width = scaled_height / aspect
expands towhich inversely scaled the width of tall images.
The formula is now based on two scaling factors based on the image's dimension relative to the window's. Now, only one computation is needed to cover for all scenarios of computing width instead of having to go through if else branches. Same goes for height.
These were derived from the principle of direct proportion
k=x/y
wherek
is a scaling factor, andx
y
are the widths of the image and window for example. The largerk
is, the larger the window is relative to the image on the width side. Same goes for the height.Contain needs the minimum scaling factor to ensure both dimensions fit within the window. Cover needs the maximum scaling factor to ensure the image's smaller dimension fits in the window, allowing the larger dimension to crop, as expected. Both of which are the behavior described in the docs.
Example
config used
"Contain"
"Cover"
(no changes)art by @AZMC15