[raster] Toolbox: probe/decode with caps, convert, crop, rotate, resize, presets, compress to size, HEIC via WIC
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
//! Orientation, crop, rotate, resize, presets, compress-to-size.
|
||||
|
||||
mod common;
|
||||
|
||||
use common::*;
|
||||
use image::GenericImageView;
|
||||
use pictor_raster::{
|
||||
Fit, Format, Limits, Preset, RasterError, Rect, Rotation, apply_preset, compress_to, crop, decode, probe, resize,
|
||||
rotate,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn exif_orientation_6_rotates_on_decode() {
|
||||
// Left half red, right half blue, 40 × 20.
|
||||
let mut img = image::RgbImage::new(40, 20);
|
||||
for (x, _, px) in img.enumerate_pixels_mut() {
|
||||
px.0 = if x < 20 { [255, 0, 0] } else { [0, 0, 255] };
|
||||
}
|
||||
let jpeg = encode_with_image(&image::DynamicImage::ImageRgb8(img), image::ImageFormat::Jpeg);
|
||||
let tagged = jpeg_with_orientation(&jpeg, 6);
|
||||
let out = decode(&tagged, &Limits::default()).unwrap();
|
||||
assert_eq!(out.dimensions(), (20, 40), "width and height swap");
|
||||
// Orientation 6 means "rotate 90° clockwise to display": the left (red) half ends up on top.
|
||||
let top = out.get_pixel(10, 5).0;
|
||||
let bottom = out.get_pixel(10, 35).0;
|
||||
assert!(top[0] > 200 && top[2] < 60, "top {top:?}");
|
||||
assert!(bottom[2] > 200 && bottom[0] < 60, "bottom {bottom:?}");
|
||||
// probe reports the stored size, before orientation.
|
||||
let p = probe(&tagged, &Limits::default()).unwrap();
|
||||
assert_eq!((p.width, p.height), (40, 20));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crop_checks_bounds() {
|
||||
let img = sample_rgb(100, 50);
|
||||
let c = crop(&img, Rect { x: 10, y: 5, width: 30, height: 20 }).unwrap();
|
||||
assert_eq!(c.dimensions(), (30, 20));
|
||||
assert_eq!(c.get_pixel(0, 0), img.get_pixel(10, 5));
|
||||
for bad in [
|
||||
Rect { x: 90, y: 0, width: 20, height: 10 },
|
||||
Rect { x: 0, y: 0, width: 0, height: 10 },
|
||||
Rect { x: u32::MAX, y: 0, width: 2, height: 2 },
|
||||
] {
|
||||
assert!(matches!(crop(&img, bad), Err(RasterError::InvalidCrop)), "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotations() {
|
||||
let img = sample_rgb(30, 10);
|
||||
assert_eq!(rotate(&img, Rotation::Cw90).dimensions(), (10, 30));
|
||||
assert_eq!(rotate(&img, Rotation::Ccw90).dimensions(), (10, 30));
|
||||
assert_eq!(rotate(&img, Rotation::R180).dimensions(), (30, 10));
|
||||
assert_eq!(rotate(&img, Rotation::FlipH).get_pixel(0, 0), img.get_pixel(29, 0));
|
||||
assert_eq!(rotate(&img, Rotation::FlipV).get_pixel(0, 0), img.get_pixel(0, 9));
|
||||
assert_eq!(rotate(&img, Rotation::Cw90).get_pixel(9, 0), img.get_pixel(0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resize_fits() {
|
||||
let l = Limits::default();
|
||||
let img = sample_rgb(400, 200);
|
||||
assert_eq!(resize(&img, Fit::LongEdge(100), &l).unwrap().dimensions(), (100, 50));
|
||||
assert_eq!(resize(&img, Fit::LongEdge(1000), &l).unwrap().dimensions(), (400, 200), "never upscales");
|
||||
assert_eq!(resize(&img, Fit::Exact(50, 70), &l).unwrap().dimensions(), (50, 70));
|
||||
assert_eq!(resize(&img, Fit::Percent(50.0), &l).unwrap().dimensions(), (200, 100));
|
||||
assert_eq!(resize(&img, Fit::Percent(0.01), &l).unwrap().dimensions(), (1, 1));
|
||||
for bad in [Fit::Percent(f32::NAN), Fit::Percent(-5.0), Fit::Percent(f32::INFINITY), Fit::Exact(0, 5)] {
|
||||
assert!(matches!(resize(&img, bad, &l), Err(RasterError::InvalidSize)), "{bad:?}");
|
||||
}
|
||||
let err = resize(&img, Fit::Exact(100_000, 100_000), &l).unwrap_err();
|
||||
assert!(matches!(err, RasterError::TooManyPixels { .. }), "{err:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_preset_long_edge_1600_jpeg() {
|
||||
let big = sample_rgb(3200, 2000);
|
||||
let (bytes, format) = apply_preset(&big, Format::Png, Preset::Email).unwrap();
|
||||
assert_eq!(format, Format::Jpeg);
|
||||
let p = probe(&bytes, &Limits::default()).unwrap();
|
||||
assert_eq!((p.width, p.height), (1600, 1000));
|
||||
|
||||
let small = sample_rgb(800, 600);
|
||||
let (bytes, _) = apply_preset(&small, Format::Jpeg, Preset::Email).unwrap();
|
||||
let p = probe(&bytes, &Limits::default()).unwrap();
|
||||
assert_eq!((p.width, p.height), (800, 600), "never upscales");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_preset_keeps_alpha_as_png() {
|
||||
let rgba = sample_rgba(3000, 1000);
|
||||
let (bytes, format) = apply_preset(&rgba, Format::Png, Preset::Web).unwrap();
|
||||
assert_eq!(format, Format::Png);
|
||||
let p = probe(&bytes, &Limits::default()).unwrap();
|
||||
assert_eq!((p.width, p.height), (2048, 683));
|
||||
|
||||
let (_, format) = apply_preset(&sample_rgb(300, 200), Format::Png, Preset::Web).unwrap();
|
||||
assert_eq!(format, Format::Jpeg);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn original_preset_keeps_size_and_format() {
|
||||
let img = sample_rgb(321, 123);
|
||||
let (bytes, format) = apply_preset(&img, Format::Bmp, Preset::Original).unwrap();
|
||||
assert_eq!(format, Format::Bmp);
|
||||
assert_eq!(decode(&bytes, &Limits::default()).unwrap().dimensions(), (321, 123));
|
||||
let (_, format) = apply_preset(&img, Format::Heif, Preset::Original).unwrap();
|
||||
assert_eq!(format, Format::Jpeg, "HEIC cannot be written, so Original falls back to JPEG");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_noisy_photo_under_500_kb() {
|
||||
let img = noisy_rgb(3000, 2000, 42);
|
||||
let target = 500_000;
|
||||
let c = compress_to(&img, Format::Jpeg, target, &Limits::default()).unwrap();
|
||||
eprintln!("compressed: {} bytes, q {:?}, scale {}, {}×{}", c.bytes.len(), c.quality, c.scale, c.width, c.height);
|
||||
assert!(c.bytes.len() as u64 <= target);
|
||||
let back = decode(&c.bytes, &Limits::default()).unwrap();
|
||||
assert_eq!(back.dimensions(), (c.width, c.height));
|
||||
assert!(c.width >= 320);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_easy_target_keeps_full_size() {
|
||||
let img = sample_rgb(800, 600);
|
||||
let c = compress_to(&img, Format::Jpeg, 2_000_000, &Limits::default()).unwrap();
|
||||
assert_eq!((c.width, c.height, c.scale), (800, 600, 1.0));
|
||||
assert_eq!(c.quality, Some(92));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_impossible_target_reports_smallest() {
|
||||
let img = noisy_rgb(1200, 800, 9);
|
||||
match compress_to(&img, Format::Jpeg, 1_000, &Limits::default()) {
|
||||
Err(RasterError::CannotReachTarget { smallest_bytes }) => assert!(smallest_bytes > 1_000),
|
||||
other => panic!("expected CannotReachTarget, got {:?}", other.map(|c| c.bytes.len())),
|
||||
}
|
||||
match compress_to(&img, Format::Png, 1_000, &Limits::default()) {
|
||||
Err(RasterError::CannotReachTarget { smallest_bytes }) => assert!(smallest_bytes > 1_000),
|
||||
other => panic!("expected CannotReachTarget, got {:?}", other.map(|c| c.bytes.len())),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_png_scales_down() {
|
||||
let img = noisy_rgb(1000, 800, 3);
|
||||
let c = compress_to(&img, Format::Png, 600_000, &Limits::default()).unwrap();
|
||||
assert!(c.bytes.len() <= 600_000);
|
||||
assert!(c.scale < 1.0);
|
||||
assert_eq!(c.quality, None);
|
||||
assert_eq!(c.format, Format::Png);
|
||||
}
|
||||
Reference in New Issue
Block a user