Skip to content

Commit e815b6a

Browse files
committed
add public resize_to_fit and resize_to_fill utility functions
fixes #1354
1 parent 1ec76c9 commit e815b6a

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/dynimage.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::image::{GenericImage, GenericImageView, ImageDecoder, ImageFormat, Im
3535
use crate::image::ImageEncoder;
3636
use crate::io::free_functions;
3737
use crate::imageops;
38-
use crate::math::resize_dimensions;
38+
use crate::math;
3939
use crate::traits::Pixel;
4040

4141
/// A Dynamic Image
@@ -756,7 +756,7 @@ impl DynamicImage {
756756
/// within the bounds specified by ```nwidth``` and ```nheight```.
757757
pub fn resize(&self, nwidth: u32, nheight: u32, filter: imageops::FilterType) -> DynamicImage {
758758
let (width2, height2) =
759-
resize_dimensions(self.width(), self.height(), nwidth, nheight, false);
759+
math::resize_to_fit((self.width(), self.height()), (nwidth, nheight));
760760

761761
self.resize_exact(width2, height2, filter)
762762
}
@@ -783,7 +783,7 @@ impl DynamicImage {
783783
/// May give aliasing artifacts if new size is close to old size.
784784
pub fn thumbnail(&self, nwidth: u32, nheight: u32) -> DynamicImage {
785785
let (width2, height2) =
786-
resize_dimensions(self.width(), self.height(), nwidth, nheight, false);
786+
math::resize_to_fit((self.width(), self.height()), (nwidth, nheight));
787787
self.thumbnail_exact(width2, height2)
788788
}
789789

@@ -810,7 +810,7 @@ impl DynamicImage {
810810
filter: imageops::FilterType,
811811
) -> DynamicImage {
812812
let (width2, height2) =
813-
resize_dimensions(self.width(), self.height(), nwidth, nheight, true);
813+
math::resize_to_fill((self.width(), self.height()), (nwidth, nheight));
814814

815815
let mut intermediate = self.resize_exact(width2, height2, filter);
816816
let (iwidth, iheight) = intermediate.dimensions();

src/math/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pub mod utils;
44

55
mod rect;
66
pub use self::rect::Rect;
7-
pub(crate) use self::utils::resize_dimensions;
7+
8+
pub use self::utils::{resize_to_fit, resize_to_fill};

src/math/utils.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,26 @@ where
2424
a
2525
}
2626

27+
/// Resizes the given size to fit into a new size while preserving the aspect ratio.
28+
/// The returned size can be smaller than the requested size in one or the other dimension.
29+
pub fn resize_to_fit(size: (u32, u32), new_size: (u32, u32)) -> (u32, u32) {
30+
resize_dimensions(size.0, size.1, new_size.0, new_size.1, false)
31+
}
32+
33+
/// Resizes the given size to fill a new size while preserving the aspect ratio.
34+
/// The returned size can be larger than the requested size in one or the other dimension.
35+
pub fn resize_to_fill(size: (u32, u32), new_size: (u32, u32)) -> (u32, u32) {
36+
resize_dimensions(size.0, size.1, new_size.0, new_size.1, true)
37+
}
38+
2739
/// Calculates the width and height an image should be resized to.
2840
/// This preserves aspect ratio, and based on the `fill` parameter
2941
/// will either fill the dimensions to fit inside the smaller constraint
3042
/// (will overflow the specified bounds on one axis to preserve
3143
/// aspect ratio), or will shrink so that both dimensions are
3244
/// completely contained with in the given `width` and `height`,
3345
/// with empty space on one axis.
34-
pub(crate) fn resize_dimensions(width: u32, height: u32, nwidth: u32, nheight: u32, fill: bool) -> (u32, u32) {
46+
fn resize_dimensions(width: u32, height: u32, nwidth: u32, nheight: u32, fill: bool) -> (u32, u32) {
3547
let ratio = u64::from(width) * u64::from(nheight);
3648
let nratio = u64::from(nwidth) * u64::from(height);
3749

0 commit comments

Comments
 (0)