mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 18:25:34 +01:00
Images: a selected image's color mode is read from its file header (#10167)
This commit is contained in:
1 parent
c767960328
commit
4f95a30c98
2 files changed
+74
-6
No files matched your search
@@ -1231,12 +1231,24 @@ fn queue(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
// ---------- 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",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<u8> = (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<u8> = (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::<Vec<_>>(), ["CMYK", "Grayscale", "RGB"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_needs_exactly_one_selected_object() {
|
||||
let mut s = session();
|
||||
|
||||
Reference in new issue
Block a user