Files
photocraft/crates/io/tests/it/source_images.rs
T
Michael Higginsandkayrockscreenprinting 6a676ee1be Save over a flat JPEG: JPEG Options, then in place; transparent ones offered as PNG (#2223)
Fixes #2223. Carries the in-place flat Save of #5865 (closed as superseded by this PR) and revives what #2515 (@kayrockscreenprinting, closed when its author stepped away) added beyond it.

File › Save now writes back over a flat file when that file still holds the document: one plain opaque layer the format can store, and only when the file held a single image. A JPEG (and a TIFF, Targa or DDS) asks its format options first, as Photoshop does; Cancel writes nothing. A JPEG whose lone layer has transparency it can't keep is offered as a PNG. Multi-image files (animations, multi-page TIFFs, multi-size icons, multi-part EXRs) still go through Save As; the source image count is kept through crash recovery.

Co-authored-by: kayrockscreenprinting <257643019+kayrockscreenprinting@users.noreply.github.com>
2026-11-10 20:26:48 +01:00

49 lines
2.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! `FileEncoding::source_images`: a file holding more images than the one a document opens from
//! (an animation, a multi-size icon) is recorded so File › Save doesn't write over it (#5733).
use crate::common::raster;
use photocraft_codecs::web::{GifFrame, encode_gif_animated};
use photocraft_io::import;
/// A 3×2 animated GIF of `frames` identical frames.
fn gif(frames: usize) -> Vec<u8> {
let frames: Vec<GifFrame> = (0..frames).map(|_| GifFrame { indices: vec![0; 6], palette: vec![[255, 0, 0]], transparent: None, delay_cs: 5 }).collect();
encode_gif_animated(3, 2, &frames, true).unwrap()
}
/// An icon file whose `entries` directory entries all point at one PNG image.
fn ico(entries: u16) -> Vec<u8> {
let mut d = photocraft_doc::Document::new("i", photocraft_geom::Size::new(4, 4), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8);
let fmt = d.pixel_format();
d.layers.push(raster("Layer 0", fmt, d.bounds(), 3, true));
let one = photocraft_io::export(&d, "i.ico", &Default::default()).unwrap().bytes;
// ICONDIR (6 bytes) + one 16-byte ICONDIRENTRY whose last 4 bytes are the image offset.
let (entry, image) = (&one[6..22], &one[22..]);
let offset = 6 + 16 * u32::from(entries);
let mut out = vec![0, 0, 1, 0];
out.extend(entries.to_le_bytes());
for _ in 0..entries {
out.extend(&entry[..12]);
out.extend(offset.to_le_bytes());
}
out.extend(image);
out
}
/// An animated GIF records how many frames it holds; a single frame records nothing.
#[test]
fn an_animation_records_its_frame_count() {
assert_eq!(import("a.gif", &gif(2)).unwrap().document.file_encoding.source_images, Some(2));
assert_eq!(import("a.gif", &gif(1)).unwrap().document.file_encoding.source_images, None);
}
/// A multi-size icon records its directory count and opens one size; a single-size icon
/// records nothing.
#[test]
fn a_multi_size_icon_records_its_entry_count() {
let two = import("a.ico", &ico(2)).unwrap().document;
assert_eq!(two.layers.len(), 1);
assert_eq!(two.file_encoding.source_images, Some(2));
assert_eq!(import("a.ico", &ico(1)).unwrap().document.file_encoding.source_images, None);
}