From 194de01a5240fe6742397699bec62a9bc4f87ccb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 17:01:14 -0800 Subject: [PATCH] Images: a selected image's color mode is read from its file header With one image selected, the Control bar and the Properties panel ask image.info for its details again after every change to the document, each step of a drag included. image.info decoded the whole image to find its color mode, so dragging or redrawing a large placed image (about 8000 x 4000 px) spent hundreds of milliseconds per step on the UI thread. A group holding the image did not lag. image.info and file.place.info now read the color mode from the header of a PNG, JPEG, TIFF, PSD, GIF or BMP file, and report RGB for WebP, without decoding the pixels. Other formats are still decoded. --- crates/engine/src/cmd/place.rs | 24 ++++++++++---- crates/engine/src/tests/place.rs | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/place.rs b/crates/engine/src/cmd/place.rs index 58612c744c..ba2b523f62 100644 --- a/crates/engine/src/cmd/place.rs +++ b/crates/engine/src/cmd/place.rs @@ -1231,12 +1231,24 @@ fn queue(s: &mut Session, p: &Value) -> Result { // ---------- queries ---------- -/// The colour mode an image file stores its pixels in. -fn color_mode(bytes: &[u8]) -> &'static str { - match craft_image_codecs::decode(bytes).ok().map(|img| img.layout()) { - Some(craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA) => "Grayscale", - Some(craft_image_codecs::ChannelLayout::Cmyk | craft_image_codecs::ChannelLayout::CmykA) => "CMYK", - _ => "RGB", +/// The colour mode an image file stores its pixels in. A PNG, JPEG, TIFF, PSD, GIF or BMP file's +/// header says it and a WebP file is always RGB, so their pixels are not decoded; the Control bar +/// and the Properties panel ask `image.info` again after every change, each step of a drag +/// included. A file in another format is decoded. +pub(crate) fn color_mode(bytes: &[u8]) -> &'static str { + use craft_image_codecs::{ChannelLayout as L, RasterColorType as T}; + match craft_image_codecs::inspect_raster(bytes) { + Ok(Some(i)) => match i.color_type { + T::Bitmap | T::Gray | T::GrayAlpha => "Grayscale", + T::Cmyk | T::CmykAlpha => "CMYK", + _ => "RGB", + }, + _ if craft_image_codecs::detect(bytes) == Some(craft_image_codecs::Format::WebP) => "RGB", + _ => match craft_image_codecs::decode(bytes).ok().map(|img| img.layout()) { + Some(L::Gray | L::GrayA) => "Grayscale", + Some(L::Cmyk | L::CmykA) => "CMYK", + _ => "RGB", + }, } } diff --git a/crates/engine/src/tests/place.rs b/crates/engine/src/tests/place.rs index 94da32aa01..c38dd53452 100644 --- a/crates/engine/src/tests/place.rs +++ b/crates/engine/src/tests/place.rs @@ -247,6 +247,62 @@ fn replace_keeps_the_transform_and_the_stacking_place() { assert_eq!(top_children(&s)[0].id.0, first.as_u64().unwrap(), "one undo step brings the old image back"); } +/// `image.info` reads a PNG's colour mode from its header without decoding its pixels: a grayscale +/// PNG whose compressed pixel data is unreadable still says Grayscale. The Control bar asks again +/// after every step of a drag, so a decode there redid a large image's whole decode each step. +#[test] +fn image_color_mode_reads_the_header_without_decoding_the_pixels() { + let raw: Vec = (0..64u32 * 64).map(|i| (i * 7 % 251) as u8).collect(); + let gray = craft_image_codecs::Image::from_raw(64, 64, craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, raw).unwrap(); + let mut png = craft_image_codecs::encode(&gray, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); + assert_eq!(cmd::place::color_mode(&png), "Grayscale"); + // Each IDAT chunk's data zeroed (an invalid zlib stream) under a correct CRC. + let mut at = 8; + let mut idat = 0; + while at + 12 <= png.len() { + let len = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize; + if &png[at + 4..at + 8] == b"IDAT" { + png[at + 8..at + 8 + len].fill(0); + let crc = crc32fast::hash(&png[at + 4..at + 8 + len]); + png[at + 8 + len..at + 12 + len].copy_from_slice(&crc.to_be_bytes()); + idat += 1; + } + at += 12 + len; + } + assert!(idat > 0, "the PNG has pixel data"); + assert!(craft_image_codecs::decode(&png).is_err(), "the pixels no longer decode"); + assert_eq!(cmd::place::color_mode(&png), "Grayscale", "the header alone gives the colour mode"); +} + +/// The colour mode read from a file's header is the one its decoded pixels have, in every format +/// and channel layout the codecs write. +#[test] +fn image_color_mode_from_the_header_matches_the_decoded_pixels() { + use craft_image_codecs::{ChannelLayout as L, Format as F}; + let decoded = |bytes: &[u8]| match craft_image_codecs::decode(bytes).map(|i| i.layout()) { + Ok(L::Gray | L::GrayA) => "Grayscale", + Ok(L::Cmyk | L::CmykA) => "CMYK", + Ok(_) => "RGB", + Err(e) => panic!("the written file decodes: {e}"), + }; + let mut seen = std::collections::BTreeSet::new(); + let mut checked = 0; + for format in [F::Png, F::Jpeg, F::Tiff, F::Gif, F::Bmp, F::WebP] { + for layout in [L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk] { + let n = 16 * 16 * layout.channels(); + let raw: Vec = (0..n).map(|i| (i * 13 % 256) as u8).collect(); + let img = craft_image_codecs::Image::from_raw(16, 16, layout, craft_image_codecs::SampleType::U8, raw).unwrap(); + let Ok(bytes) = craft_image_codecs::encode(&img, format, &craft_image_codecs::EncodeOptions::default()) else { continue }; + let want = decoded(&bytes); + assert_eq!(cmd::place::color_mode(&bytes), want, "{format:?} written from {layout:?}"); + seen.insert(want); + checked += 1; + } + } + assert!(checked >= 12, "{checked} files written"); + assert_eq!(seen.into_iter().collect::>(), ["CMYK", "Grayscale", "RGB"]); +} + #[test] fn replace_needs_exactly_one_selected_object() { let mut s = session();