mirror of
https://github.com/Cykooz/fast_image_resize.git
synced 2026-11-29 04:01:35 +00:00
Fixed temp image size calculation during resize (#63).
This commit is contained in:
1 parent
292d3a898d
commit
30a46b5dab
5 files changed
+80
-11
No files matched your search
@@ -16,6 +16,7 @@
|
||||
### Fixed
|
||||
|
||||
- Fixed integer overflow in `check_crop_box()` function.
|
||||
- Fixed temp image size calculation during resize ([#63](https://github.com/Cykooz/fast_image_resize/issues/63)).
|
||||
|
||||
## [6.1.0] - 2026-07-21
|
||||
|
||||
|
||||
+61
-1
@@ -60,6 +60,8 @@ pub(crate) struct Coefficients {
|
||||
pub values: Vec<f64>,
|
||||
pub window_size: usize,
|
||||
pub bounds: Vec<Bound>,
|
||||
min_bound_start: u32,
|
||||
max_bound_end: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@@ -83,6 +85,14 @@ impl Coefficients {
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
pub fn min_bound_start(&self) -> u32 {
|
||||
self.min_bound_start
|
||||
}
|
||||
|
||||
pub fn max_bound_end(&self) -> u32 {
|
||||
self.max_bound_end
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn precompute_coefficients(
|
||||
@@ -118,6 +128,8 @@ pub(crate) fn precompute_coefficients(
|
||||
let count_of_coeffs = window_size * out_size as usize;
|
||||
let mut coeffs: Vec<f64> = Vec::with_capacity(count_of_coeffs);
|
||||
let mut bounds: Vec<Bound> = Vec::with_capacity(out_size as usize);
|
||||
let mut min_bound_start = u32::MAX;
|
||||
let mut max_bound_end = 0;
|
||||
|
||||
for out_x in 0..out_size {
|
||||
// Find the point in the input image corresponding to the center
|
||||
@@ -161,7 +173,7 @@ pub(crate) fn precompute_coefficients(
|
||||
}
|
||||
|
||||
if ww != 0.0 {
|
||||
// Normalise values of weights.
|
||||
// Normalize values of weights.
|
||||
// The sum of weights must be equal to 1.0.
|
||||
coeffs[cur_index..].iter_mut().for_each(|w| *w /= ww);
|
||||
}
|
||||
@@ -171,11 +183,59 @@ pub(crate) fn precompute_coefficients(
|
||||
start: bound_start,
|
||||
size: bound_end - bound_start,
|
||||
});
|
||||
min_bound_start = min_bound_start.min(bound_start);
|
||||
max_bound_end = max_bound_end.max(bound_end);
|
||||
}
|
||||
|
||||
if bounds.is_empty() {
|
||||
min_bound_start = 0;
|
||||
}
|
||||
|
||||
Coefficients {
|
||||
values: coeffs,
|
||||
window_size,
|
||||
bounds,
|
||||
min_bound_start,
|
||||
max_bound_end,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::CropBox;
|
||||
|
||||
#[test]
|
||||
fn test_min_bound_start_and_max_bound_end() {
|
||||
// https://github.com/Cykooz/fast_image_resize/issues/63
|
||||
let crop_box = CropBox {
|
||||
left: 10.0 + 1.0 / 6.0,
|
||||
top: 0.0,
|
||||
width: 20.0,
|
||||
height: 8.0,
|
||||
};
|
||||
let (filter_fn, filter_support) = get_filter_func(FilterType::CatmullRom);
|
||||
let coeffs = precompute_coefficients(
|
||||
100,
|
||||
crop_box.left,
|
||||
crop_box.left + crop_box.width,
|
||||
30,
|
||||
filter_fn,
|
||||
filter_support,
|
||||
true,
|
||||
);
|
||||
assert_eq!(
|
||||
coeffs.min_bound_start,
|
||||
coeffs.bounds.iter().map(|b| b.start).min().unwrap_or(0)
|
||||
);
|
||||
assert_eq!(
|
||||
coeffs.max_bound_end,
|
||||
coeffs
|
||||
.bounds
|
||||
.iter()
|
||||
.map(|b| b.start + b.size)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -256,6 +256,8 @@ mod tests {
|
||||
values: vec![value],
|
||||
window_size: 1,
|
||||
bounds: vec![Bound { start: 0, size: 1 }],
|
||||
min_bound_start: 0,
|
||||
max_bound_end: 1,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-10
@@ -410,11 +410,8 @@ impl Resizer {
|
||||
if P::components_is_u8() {
|
||||
// For u8-based images, it is faster to do the vertical pass first
|
||||
// instead of the horizontal.
|
||||
let x_first = horiz_coeffs.bounds[0].start;
|
||||
// Last used col in the source image
|
||||
let last_x_bound = horiz_coeffs.bounds.last().unwrap();
|
||||
let x_last = last_x_bound.start + last_x_bound.size;
|
||||
let temp_width = x_last - x_first;
|
||||
let x_first = horiz_coeffs.min_bound_start();
|
||||
let temp_width = horiz_coeffs.max_bound_end() - x_first;
|
||||
let mut temp_image = get_temp_image_from_buffer(
|
||||
&mut self.convolution_buffer,
|
||||
temp_width,
|
||||
@@ -443,11 +440,8 @@ impl Resizer {
|
||||
self.cpu_extensions,
|
||||
);
|
||||
} else {
|
||||
let y_first = vert_coeffs.bounds[0].start;
|
||||
// Last used row in the source image
|
||||
let last_y_bound = vert_coeffs.bounds.last().unwrap();
|
||||
let y_last = last_y_bound.start + last_y_bound.size;
|
||||
let temp_height = y_last - y_first;
|
||||
let y_first = vert_coeffs.min_bound_start();
|
||||
let temp_height = vert_coeffs.max_bound_end() - y_first;
|
||||
let mut temp_image = get_temp_image_from_buffer(
|
||||
&mut self.convolution_buffer,
|
||||
dst_width,
|
||||
|
||||
@@ -255,6 +255,18 @@ fn resize_to_same_width_or_height_after_cropping() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_temp_image_size_calculation_during_resize() {
|
||||
// https://github.com/Cykooz/fast_image_resize/issues/63
|
||||
let src = Image::new(100, 8, PixelType::U8x4);
|
||||
let mut dst = Image::new(30, 9, PixelType::U8x4);
|
||||
let options = ResizeOptions::new()
|
||||
.resize_alg(ResizeAlg::Convolution(FilterType::CatmullRom))
|
||||
.crop(10.0 + 1.0 / 6.0, 0.0, 20.0, 8.0)
|
||||
.use_alpha(false);
|
||||
assert!(Resizer::new().resize(&src, &mut dst, &options).is_ok());
|
||||
}
|
||||
|
||||
trait ResizeTest<const CC: usize> {
|
||||
fn downscale_test(resize_alg: ResizeAlg, cpu_extensions: CpuExtensions, checksum: [u64; CC]);
|
||||
fn upscale_test(resize_alg: ResizeAlg, cpu_extensions: CpuExtensions, checksum: [u64; CC]);
|
||||
|
||||
Reference in new issue
Block a user