Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider making math::resize_dimensions function public #2394

Open
phil-opp opened this issue Dec 16, 2024 · 1 comment
Open

Consider making math::resize_dimensions function public #2394

phil-opp opened this issue Dec 16, 2024 · 1 comment

Comments

@phil-opp
Copy link

The resize_dimensions would be quite useful when working with imageops::resize and imageops::thumbnail. Unfortunately it's not part of the public API, so downstream crates cannot use it.

This was already requested in #1354 a few years ago. The issue was closed without a comment.

Draft

It would be nice if we could make the function public.

@fintelia
Copy link
Contributor

fintelia commented Dec 16, 2024

Looks like there was also an abandoned PR: #1356. For reference (since I didn't remember off hand what it did), this is the implementation of resize_dimensions...

image/src/math/utils.rs

Lines 5 to 40 in 2125965

/// Calculates the width and height an image should be resized to.
/// This preserves aspect ratio, and based on the `fill` parameter
/// will either fill the dimensions to fit inside the smaller constraint
/// (will overflow the specified bounds on one axis to preserve
/// aspect ratio), or will shrink so that both dimensions are
/// completely contained within the given `width` and `height`,
/// with empty space on one axis.
pub(crate) fn resize_dimensions(
width: u32,
height: u32,
nwidth: u32,
nheight: u32,
fill: bool,
) -> (u32, u32) {
let wratio = f64::from(nwidth) / f64::from(width);
let hratio = f64::from(nheight) / f64::from(height);
let ratio = if fill {
f64::max(wratio, hratio)
} else {
f64::min(wratio, hratio)
};
let nw = max((f64::from(width) * ratio).round() as u64, 1);
let nh = max((f64::from(height) * ratio).round() as u64, 1);
if nw > u64::from(u32::MAX) {
let ratio = f64::from(u32::MAX) / f64::from(width);
(u32::MAX, max((f64::from(height) * ratio).round() as u32, 1))
} else if nh > u64::from(u32::MAX) {
let ratio = f64::from(u32::MAX) / f64::from(height);
(max((f64::from(width) * ratio).round() as u32, 1), u32::MAX)
} else {
(nw as u32, nh as u32)
}
}

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

No branches or pull requests

2 participants