fix: preserve XML and raster import semantics after codec cutover

This commit is contained in:
Dylan M. Taylor
2026-11-14 23:25:19 +00:00
parent 1e1100a9ed
commit 5122a1f9d9
10 changed files with 249 additions and 57 deletions
+11 -2
View File
@@ -144,9 +144,18 @@ fn unpack(mut src: &[u8], out: &mut Vec<u8>) {
}
}
/// The composite as a reader sees it.
/// The stored composite: RGB stays white-matted, with its original alpha.
fn merged(f: &[u8]) -> Vec<u8> {
craft_psd::PsdFile::from_bytes(f).expect("a PSD a reader takes").composite_rgba8().expect("composite").data
let psd = craft_psd::PsdFile::from_bytes(f).expect("a PSD a reader takes");
if psd.header.color_mode != craft_psd::ColorMode::Rgb {
return psd.composite_rgba8().expect("composite").data;
}
assert_eq!(psd.header.depth, 8);
let samples = psd.decode_merged().expect("stored composite planes");
let count = psd.header.width as usize * psd.header.height as usize;
(0..count)
.flat_map(|i| [samples[i], samples[count + i], samples[2 * count + i], if psd.merged_has_alpha() { samples[3 * count + i] } else { 255 }])
.collect()
}
fn pixel(px: &[u8], width: usize, x: usize, y: usize) -> [u8; 4] {
+5
View File
@@ -172,6 +172,11 @@ fn packbits_and_bilevel_images_decode() {
let dec = craft_image_codecs::decode_as(craft_image_codecs::Format::Tiff, &file).unwrap();
assert_eq!(dec.width(), 10);
assert_eq!(dec.height(), 3);
assert_eq!(
dec.data(),
[0, 255, 0, 255, 0, 255, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
"WhiteIsZero samples are inverted and row padding is ignored"
);
// A buffer that doesn't match the size is refused, not written.
let short = Image { data: &rows[..5], ..image };
assert!(t::write(&short, &Layout { byte_order: ByteOrder::Little, compression: Compression::None, ppi: 72.0, icc: None }).is_err());
+80 -40
View File
@@ -330,52 +330,65 @@ fn reduced_pixmap(bytes: &[u8], r: &Reduction) -> Result<vello_cpu::Pixmap, Unde
/// `bytes` read and shrunk as [`reduced_pixmap`] says.
fn shrunk(bytes: &[u8], r: &Reduction) -> Option<vello_cpu::Pixmap> {
shrunk_with_png_limits(bytes, r, &craft_image_codecs::Limits::default())
}
fn shrunk_with_png_limits(bytes: &[u8], r: &Reduction, png_limits: &craft_image_codecs::Limits) -> Option<vello_cpu::Pixmap> {
let mut shrink = Shrink::new(r.size, r.to);
if r.rows {
use craft_image_codecs::{ChannelLayout, CodecError, Format, Limits, PngDecodeRows, SampleType};
let mut dec = PngDecodeRows::new_streaming(bytes, &Limits::default()).ok()?;
use craft_image_codecs::{ChannelLayout, CodecError, Format, PngDecodeRows, SampleType};
let mut dec = PngDecodeRows::new_streaming(bytes, png_limits).ok()?;
if !dec.streamable() {
return None;
}
let hdr = dec.header();
dec.step_stream::<CodecError>(1, &mut || true, &mut |_y, row| {
match (hdr.layout, hdr.sample) {
(ChannelLayout::Gray, SampleType::U8) => shrink.row(row.iter().map(|&g| [g, g, g, 255])),
(ChannelLayout::GrayA, SampleType::U8) => shrink.row(row.as_chunks::<2>().0.iter().map(|&[g, a]| [g, g, g, a])),
(ChannelLayout::Rgb, SampleType::U8) => shrink.row(row.as_chunks::<3>().0.iter().map(|&[r, g, b]| [r, g, b, 255])),
(ChannelLayout::Rgba, SampleType::U8) => shrink.row(row.as_chunks::<4>().0.iter().copied()),
(ChannelLayout::Gray, SampleType::U16) => shrink.row(row.as_chunks::<2>().0.iter().map(|&g| {
let v = (u16::from_ne_bytes(g) >> 8) as u8;
[v, v, v, 255]
})),
(ChannelLayout::GrayA, SampleType::U16) => shrink.row(row.as_chunks::<4>().0.iter().map(|&[g0, g1, a0, a1]| {
let g = (u16::from_ne_bytes([g0, g1]) >> 8) as u8;
let a = (u16::from_ne_bytes([a0, a1]) >> 8) as u8;
[g, g, g, a]
})),
(ChannelLayout::Rgb, SampleType::U16) => shrink.row(row.as_chunks::<6>().0.iter().map(|&[r0, r1, g0, g1, b0, b1]| {
[
(u16::from_ne_bytes([r0, r1]) >> 8) as u8,
(u16::from_ne_bytes([g0, g1]) >> 8) as u8,
(u16::from_ne_bytes([b0, b1]) >> 8) as u8,
255,
]
})),
(ChannelLayout::Rgba, SampleType::U16) => shrink.row(row.as_chunks::<8>().0.iter().map(|&[r0, r1, g0, g1, b0, b1, a0, a1]| {
[
(u16::from_ne_bytes([r0, r1]) >> 8) as u8,
(u16::from_ne_bytes([g0, g1]) >> 8) as u8,
(u16::from_ne_bytes([b0, b1]) >> 8) as u8,
(u16::from_ne_bytes([a0, a1]) >> 8) as u8,
]
})),
_ => {
return Err(CodecError::Unsupported { format: Format::Png, reason: "unsupported PNG channel layout".into() });
}
}
Ok(())
})
.ok()??;
// Yield after each row and stop once all pixels have arrived. The pinned decoder's
// first tail batch only locates a text chunk; later batches inflate/retain its value.
// Never run those batches: the renderer needs pixels, not ancillary text. Its underlying
// PNG reader also ignores text chunks before IDAT, so neither location retains text.
for _ in 0..hdr.height {
let _extras = dec
.step_stream::<CodecError>(1, &mut || false, &mut |_y, row| {
match (hdr.layout, hdr.sample) {
(ChannelLayout::Gray, SampleType::U8) => shrink.row(row.iter().map(|&g| [g, g, g, 255])),
(ChannelLayout::GrayA, SampleType::U8) => shrink.row(row.as_chunks::<2>().0.iter().map(|&[g, a]| [g, g, g, a])),
(ChannelLayout::Rgb, SampleType::U8) => shrink.row(row.as_chunks::<3>().0.iter().map(|&[r, g, b]| [r, g, b, 255])),
(ChannelLayout::Rgba, SampleType::U8) => shrink.row(row.as_chunks::<4>().0.iter().copied()),
(ChannelLayout::Gray, SampleType::U16) => shrink.row(row.as_chunks::<2>().0.iter().map(|&g| {
let v = (u16::from_ne_bytes(g) >> 8) as u8;
[v, v, v, 255]
})),
(ChannelLayout::GrayA, SampleType::U16) => shrink.row(row.as_chunks::<4>().0.iter().map(|&[g0, g1, a0, a1]| {
let g = (u16::from_ne_bytes([g0, g1]) >> 8) as u8;
let a = (u16::from_ne_bytes([a0, a1]) >> 8) as u8;
[g, g, g, a]
})),
(ChannelLayout::Rgb, SampleType::U16) => shrink.row(row.as_chunks::<6>().0.iter().map(|&[r0, r1, g0, g1, b0, b1]| {
[
(u16::from_ne_bytes([r0, r1]) >> 8) as u8,
(u16::from_ne_bytes([g0, g1]) >> 8) as u8,
(u16::from_ne_bytes([b0, b1]) >> 8) as u8,
255,
]
})),
(ChannelLayout::Rgba, SampleType::U16) => {
shrink.row(row.as_chunks::<8>().0.iter().map(|&[r0, r1, g0, g1, b0, b1, a0, a1]| {
[
(u16::from_ne_bytes([r0, r1]) >> 8) as u8,
(u16::from_ne_bytes([g0, g1]) >> 8) as u8,
(u16::from_ne_bytes([b0, b1]) >> 8) as u8,
(u16::from_ne_bytes([a0, a1]) >> 8) as u8,
]
}))
}
_ => {
return Err(CodecError::Unsupported { format: Format::Png, reason: "unsupported PNG channel layout".into() });
}
}
Ok(())
})
.ok()?;
}
} else {
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?;
let mut limits = image::Limits::default();
@@ -616,6 +629,33 @@ mod tests {
assert_eq!(room_for(MAX_DECODE_ALLOC, MAX_DECODE_ALLOC), Ok(()));
}
#[test]
fn streaming_reduction_does_not_retain_text_after_pixels() {
let mut bytes = Vec::new();
striped(8).write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Png).unwrap();
let mut chunk = b"tEXtComment\0".to_vec();
chunk.extend(std::iter::repeat_n(b'x', 1 << 20));
// PNG's IEEE CRC over type and payload, kept local to this fixture.
let mut crc = u32::MAX;
for &byte in &chunk {
crc ^= u32::from(byte);
for _ in 0..8 {
crc = (crc >> 1) ^ (0xedb88320 & 0u32.wrapping_sub(crc & 1));
}
}
let mut tail = ((chunk.len() - 4) as u32).to_be_bytes().to_vec();
tail.extend(chunk);
tail.extend((!crc).to_be_bytes());
bytes.splice(bytes.len() - 12..bytes.len() - 12, tail);
let limits = craft_image_codecs::Limits { max_alloc: 1 << 19, ..Default::default() };
let mut full = craft_image_codecs::PngDecodeRows::new_streaming(&bytes, &limits).unwrap();
assert!(full.step_stream::<craft_image_codecs::CodecError>(1, &mut || true, &mut |_, _| Ok(())).is_err());
// The renderer stops after the pixels instead of retaining ancillary text.
let reduced = shrunk_with_png_limits(&bytes, &Reduction { size: (8, 1), to: (4, 1), rows: true, scratch: 0 }, &limits).unwrap();
assert_eq!((reduced.width(), reduced.height()), (4, 1));
assert!(red_at_its_coverage(&reduced));
}
/// Opaque red beside clear black, every other column of a `w` × 1 image.
fn striped(w: u32) -> image::RgbaImage {
image::RgbaImage::from_fn(w, 1, |x, _| if x % 2 == 0 { image::Rgba([200, 30, 30, 255]) } else { image::Rgba([0, 0, 0, 0]) })