-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
682538d
commit eabd650
Showing
4 changed files
with
89 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod iter_ext; | |
pub mod path_ext; | ||
pub mod string_ext; | ||
pub mod range_ext; | ||
pub mod image_ext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use image::DynamicImage; | ||
use image::imageops::FilterType; | ||
|
||
pub trait Resizing { | ||
fn resize(&self, nwidth: u32, nheight: u32, fill: bool, filter: FilterType) -> Self; | ||
} | ||
|
||
impl Resizing for DynamicImage { | ||
fn resize(&self, to_width: u32, to_height: u32, fill: bool, filter: FilterType) -> Self { | ||
let (width, height) = resize_dimensions(self.width(), self.height(), to_width, to_height, fill); | ||
return self.resize_exact(width, height, filter); | ||
} | ||
} | ||
|
||
// image::math::utils::resize_dimensions() | ||
fn resize_dimensions( | ||
width: u32, | ||
height: u32, | ||
nwidth: u32, | ||
nheight: u32, | ||
fill: bool, | ||
) -> (u32, u32) { | ||
let wratio = nwidth as f64 / width as f64; | ||
let hratio = nheight as f64 / height as f64; | ||
|
||
let ratio = if fill { | ||
f64::max(wratio, hratio) | ||
} else { | ||
f64::min(wratio, hratio) | ||
}; | ||
|
||
let nw = 1.max((width as f64 * ratio).round() as u64); | ||
let nh = 1.max((height as f64 * ratio).round() as u64); | ||
|
||
if nw > u64::from(u32::MAX) { | ||
let ratio = u32::MAX as f64 / width as f64; | ||
(u32::MAX, 1.max((height as f64 * ratio).round() as u32)) | ||
} else if nh > u64::from(u32::MAX) { | ||
let ratio = u32::MAX as f64 / height as f64; | ||
(1.max((width as f64 * ratio).round() as u32), u32::MAX) | ||
} else { | ||
(nw as u32, nh as u32) | ||
} | ||
} |