mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 12:03:31 +00:00
Crop Image: refuse a resample too large for the raster allowance
object.cropImage with ppi now checks that the source, its 16-bit
weighted copy, the resampler's intermediate rows and the results fit in
the raster allowance (2 GB, 512 MB in the web app) before it resamples,
and refuses with "an image of {w} × {h} pixels is too large to resample"
otherwise. A crop that is drawn first frees the drawing's PNG before its
pixels are resampled.
This commit is contained in:
@@ -1031,8 +1031,10 @@ fn check_crop_size(w: f64, h: f64) -> Result<()> {
|
||||
|
||||
/// `img` resampled to `w` × `h` with its colors weighted by their alpha, so the color of a fully
|
||||
/// transparent pixel does not show at the edges of what is opaque. The weighted colors are held in
|
||||
/// 16 bits a channel until the end, so a nearly transparent pixel keeps its color.
|
||||
fn resize_premultiplied(img: &image::RgbaImage, w: u32, h: u32) -> image::RgbaImage {
|
||||
/// 16 bits a channel until the end, so a nearly transparent pixel keeps its color. An error when
|
||||
/// the working copies would not fit ([`check_resample_size`]).
|
||||
fn resize_premultiplied(img: &image::RgbaImage, w: u32, h: u32) -> Result<image::RgbaImage> {
|
||||
check_resample_size(img.width(), img.height(), w, h)?;
|
||||
let mut pre = image::ImageBuffer::<image::Rgba<u16>, Vec<u16>>::new(img.width(), img.height());
|
||||
for (d, s) in pre.pixels_mut().zip(img.pixels()) {
|
||||
let a = u32::from(s[3]);
|
||||
@@ -1052,7 +1054,19 @@ fn resize_premultiplied(img: &image::RgbaImage, w: u32, h: u32) -> image::RgbaIm
|
||||
}
|
||||
d.0[3] = ((a + 128) / 257).min(255) as u8;
|
||||
}
|
||||
res
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Whether resampling a `sw` × `sh` image to `w` × `h` fits in [`MAX_RASTER_ALLOC`]: the source and
|
||||
/// its weighted 16-bit copy (12 bytes a pixel), the resampler's float rows between its two passes
|
||||
/// (16 bytes a pixel, `sw` × `h`) and the 16-bit and 8-bit results (12 bytes a pixel of `w` × `h`).
|
||||
pub(crate) fn check_resample_size(sw: u32, sh: u32, w: u32, h: u32) -> Result<()> {
|
||||
let (sw, sh, w, h) = (u64::from(sw), u64::from(sh), u64::from(w), u64::from(h));
|
||||
let bytes = (sw * sh).saturating_mul(12).saturating_add((sw * h).saturating_mul(16)).saturating_add((w * h).saturating_mul(12));
|
||||
if bytes > crate::cmd::fileio::MAX_RASTER_ALLOC {
|
||||
return Err(bad("object.cropImage", format!("an image of {sw} × {sh} pixels is too large to resample")));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `img` as PNG bytes.
|
||||
@@ -1118,7 +1132,7 @@ fn crop_image(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Some(t) => {
|
||||
let (cw, ch) = cut.dimensions();
|
||||
let (w, h) = ((r.width() * t).round().max(1.0) as u32, (r.height() * t).round().max(1.0) as u32);
|
||||
let resized = resize_premultiplied(&cut, w, h);
|
||||
let resized = resize_premultiplied(&cut, w, h)?;
|
||||
(resized, xf * Affine::scale_non_uniform(f64::from(cw) / f64::from(w), f64::from(ch) / f64::from(h)))
|
||||
}
|
||||
None => (cut, xf),
|
||||
@@ -1167,9 +1181,11 @@ fn crop_image(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// fitted to the box, so the image keeps its size.
|
||||
let (png, w, h, xf) = match target {
|
||||
Some(t) => {
|
||||
let drawn = image::load_from_memory(&png).map_err(|e| bad(C, e.to_string()))?.to_rgba8();
|
||||
let drawn = image::load_from_memory(&png).map_err(|e| bad(C, e.to_string()))?.into_rgba8();
|
||||
// Only the decoded pixels are held while they are resampled.
|
||||
drop(png);
|
||||
let (tw, th) = ((r.width() * t).round().max(1.0) as u32, (r.height() * t).round().max(1.0) as u32);
|
||||
let resized = resize_premultiplied(&drawn, tw, th);
|
||||
let resized = resize_premultiplied(&drawn, tw, th)?;
|
||||
let xf = Affine::translate(r.origin().to_vec2()) * Affine::scale_non_uniform(r.width() / f64::from(tw), r.height() / f64::from(th));
|
||||
(encode_png(&resized)?, tw, th, xf)
|
||||
}
|
||||
|
||||
@@ -5541,6 +5541,22 @@ fn crop_image_trim_checks_the_rgba_size_before_decoding() {
|
||||
assert!(within.contains("the image's pixels can't be read"), "{within}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crop_image_resampling_is_held_to_the_raster_allowance() {
|
||||
use crate::cmd::menucmds::check_resample_size;
|
||||
let max = crate::cmd::fileio::MAX_RASTER_ALLOC;
|
||||
// 40 bytes a pixel resampled to the same size (12 + 16 + 12).
|
||||
let side = ((max / 40) as f64).sqrt() as u32;
|
||||
assert!(check_resample_size(side, side, side, side).is_ok());
|
||||
let e = check_resample_size(side + 1, side + 1, side + 1, side + 1).unwrap_err().to_string();
|
||||
assert!(e.contains(&format!("an image of {0} × {0} pixels is too large to resample", side + 1)), "{e}");
|
||||
// A large source counts even when the result is small.
|
||||
let wide = ((max / 12) as f64).sqrt() as u32 + 1;
|
||||
assert!(check_resample_size(wide, wide, 1, 1).is_err());
|
||||
assert!(check_resample_size(u32::MAX, u32::MAX, u32::MAX, u32::MAX).is_err());
|
||||
assert!(check_resample_size(100, 100, 50, 50).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crop_image_trim_leaves_a_cmyk_image_alone() {
|
||||
// CMYK images keep their ink amounts and have no alpha: nothing to cut, and not an error.
|
||||
|
||||
+3
-1
@@ -1925,7 +1925,9 @@ An image scaled differently across and down is resampled on both sides, so its p
|
||||
The most it takes is the image's own resolution (along the denser side of its pixel grid, however it is turned), or
|
||||
300 ppi for a linked image; above that it is refused with `ppi can't be more than {max} for this image`. A value
|
||||
outside 1 to 2400 is refused with `ppi must be between 1 and 2400`, and a value that is not a positive number with
|
||||
`ppi must be a positive number, not {ppi}`. A null `ppi` keeps the image's own, as a missing one does, and `ppi` with
|
||||
`ppi must be a positive number, not {ppi}`. A resample whose working copies would pass the raster allowance (2 GB,
|
||||
512 MB in the web app) is refused with `an image of {w} × {h} pixels is too large to resample`, naming the size
|
||||
of the pixels being resampled. A null `ppi` keeps the image's own, as a missing one does, and `ppi` with
|
||||
`trim` is refused. While the Crop Image tool is chosen, `tool.setOption` sets `ppi` (the resolution Apply sends),
|
||||
`reference` (0 to 8, left to right and top to bottom: the point of the box that the Control bar's X and Y name and
|
||||
that its W and H resize about; 4, the center, by default) and `linked` (W and H keep the box's proportions; false by
|
||||
|
||||
Reference in New Issue
Block a user