From f85acd7773449fa363fae85c6cee2e0884a5524f Mon Sep 17 00:00:00 2001 From: Sven Lafebre Date: Mon, 16 Nov 2026 10:58:57 +0100 Subject: [PATCH] Palette PNG indices: a band of rows per step on web, a cancel check between bands, and a fallible row buffer --- crates/io/src/flat.rs | 197 ++++++++++++++++++++------------ crates/io/src/png_steps.rs | 54 +++++++-- crates/io/tests/it/png_steps.rs | 16 +++ 3 files changed, 187 insertions(+), 80 deletions(-) diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index e4521f3288..1c74f645e7 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -127,9 +127,9 @@ fn import_flat_impl_with_options( } }; apply_flat_format(&mut r, bytes, format, img.as_ref())?; - // A palette PNG whose entries share a colour is read again there for its indices (#14342), - // a pass that doesn't watch for a cancel: one raised meanwhile still fails the open. - ctl.check().map_err(|_| IoError::Cancelled)?; + if format == Some(Format::Png) { + read_png_indices(&mut r, bytes, ctl)?; + } Ok(r) } @@ -241,7 +241,8 @@ impl FlatBuilder { /// The document, after copying and pruning whatever is left: the one-shot result and the last /// step of a stepped one. Like the one-shot import, it records how many images the file holds - /// when the decoder read only the first, so Save doesn't write over the rest (#5733). + /// when the decoder read only the first, so Save doesn't write over the rest (#5733). A palette + /// PNG's indices are left to [`PngIndices`]. pub(crate) fn finish(mut self, bytes: &[u8], format: Option) -> Result { let copied = self.copy_done(); while !self.copy_done() { @@ -261,82 +262,135 @@ impl FlatBuilder { } } -/// Each pixel's palette index in the palette PNG `bytes`, opened as `doc`, so Color Table can -/// tell entries of one colour apart (#14342). `None`, and Color Table falls back to the nearest -/// entry, when the document's one layer doesn't show each pixel's entry at the file's place for -/// it (an EXIF orientation turned the pixels, which colours alone can't show when entries repeat, -/// or a colour conversion changed them), or when the file is interlaced (not read here). One row -/// at a time, into tiles one band of rows at a time: next to the layer, only the indices -/// themselves (a byte per pixel) and one band are held. `None` too when memory can't hold them: -/// the open's own memory check didn't count them. -fn png_color_indices(bytes: &[u8], doc: &Document) -> Option { - let table = doc.color_table.as_ref()?; - // Only entries that share a colour need the file. When every entry has its own colour, each - // pixel's colour names its entry and Color Table's nearest entry finds it. Reading the file - // again is one pass over all its rows that neither a stepped import (the web build) splits - // up nor a cancel stops, so it is skipped when it can't add anything. - let mut colors = table.colors.clone(); - colors.sort_unstable(); - if !colors.windows(2).any(|w| w[0] == w[1]) { - return None; - } - let [layer] = doc.layers.as_slice() else { return None }; - let surface = layer.surface()?; - let mut decoder = png::Decoder::new(std::io::Cursor::new(bytes)); - decoder.set_transformations(png::Transformations::IDENTITY); - decoder.set_ignore_text_chunk(true); - let mut reader = decoder.read_info().ok()?; - let info = reader.info(); - let (w, h, bits) = (info.width, info.height, info.bit_depth as usize); - if info.color_type != png::ColorType::Indexed || info.interlaced || !matches!(bits, 1 | 2 | 4 | 8) || (w, h) != (doc.size.width, doc.size.height) { - return None; - } - let width = usize::try_from(w).ok()?; - let rows = usize::try_from(TILE_SIZE).ok()?; - let band_len = width.checked_mul(rows)?; - // The index surface's whole tiles, the band, and the row of the layer read beside it. - let needed = tile_bytes(w, h, 1).saturating_add(u64::from(w).saturating_mul(rows as u64 + 4)); - crate::memory::check_fits(w, h, needed, crate::memory::available()).ok()?; - let mut band = Vec::new(); - band.try_reserve_exact(band_len).ok()?; - let mut map = Surface::new(PixelFormat::GRAY8); - let mut shown = vec![[0u8; 4]; width]; - let (mut y, mut band_y) = (0i32, 0i32); - let height = i32::try_from(h).ok()?; - while let Some(row) = reader.next_row().ok()? { - if y >= height { +/// Each pixel's palette index in the palette PNG `bytes`, opened as `doc`, read again a band of +/// rows per [`PngIndices::step`], so Color Table can tell entries of one colour apart (#14342). +/// No indices, and Color Table falls back to the nearest entry, when the document's one layer +/// doesn't show each pixel's entry at the file's place for it (an EXIF orientation turned the +/// pixels, which colours alone can't show when entries repeat, or a colour conversion changed +/// them), or when the file is interlaced (not read here). One row at a time, into tiles one band +/// of rows at a time: next to the layer, only the indices themselves (a byte per pixel) and one +/// band are held. None either when memory can't hold them: the open's own memory check didn't +/// count them. +pub(crate) struct PngIndices> { + reader: png::Reader>, + table: photocraft_doc::ColorTable, + layer: photocraft_doc::LayerId, + surface: Surface, + map: Surface, + band: Vec, + shown: Vec<[u8; 4]>, + bits: usize, + width: u32, + height: i32, + y: i32, +} + +impl> PngIndices { + /// Starts reading `bytes` for `doc`'s indices; `None` when they can't be recorded, or needn't + /// be. + pub(crate) fn new(bytes: B, doc: &Document) -> Option { + let table = doc.color_table.as_ref()?; + // Only entries that share a colour need the file. When every entry has its own colour, + // each pixel's colour names its entry and Color Table's nearest entry finds it, so the + // pass over all the file's rows is skipped. + let mut colors = table.colors.clone(); + colors.sort_unstable(); + if !colors.windows(2).any(|w| w[0] == w[1]) { return None; } - surface.read_rgba8_into(Rect::from_xywh(0, y, w, 1), &mut shown); - for (x, px) in shown.iter().enumerate() { - let bit = x * bits; - let byte = *row.data().get(bit / 8)?; - let i = (byte >> (8 - bits - bit % 8)) & (0xff >> (8 - bits)); - let entry = table.colors.get(usize::from(i))?; - let ok = if px[3] == 0 { table.transparent == Some(i) } else { px[..3] == entry[..] }; - if !ok { - return None; + let [layer] = doc.layers.as_slice() else { return None }; + let surface = layer.surface()?; + let mut decoder = png::Decoder::new(std::io::Cursor::new(bytes)); + decoder.set_transformations(png::Transformations::IDENTITY); + decoder.set_ignore_text_chunk(true); + let reader = decoder.read_info().ok()?; + let info = reader.info(); + let (w, h, bits) = (info.width, info.height, info.bit_depth as usize); + if info.color_type != png::ColorType::Indexed || info.interlaced || !matches!(bits, 1 | 2 | 4 | 8) || (w, h) != (doc.size.width, doc.size.height) { + return None; + } + let width = usize::try_from(w).ok()?; + let band_len = width.checked_mul(usize::try_from(TILE_SIZE).ok()?)?; + // The index surface's whole tiles, the band, and the row of the layer read beside it. + let needed = tile_bytes(w, h, 1).saturating_add(u64::from(w).saturating_mul(TILE_SIZE as u64 + 4)); + crate::memory::check_fits(w, h, needed, crate::memory::available()).ok()?; + let mut band = Vec::new(); + band.try_reserve_exact(band_len).ok()?; + let mut shown = Vec::new(); + shown.try_reserve_exact(width).ok()?; + shown.resize(width, [0u8; 4]); + Some(Self { + reader, + table: table.clone(), + layer: layer.id, + surface: surface.clone(), + map: Surface::new(PixelFormat::GRAY8), + band, + shown, + bits, + width: w, + height: i32::try_from(h).ok()?, + y: 0, + }) + } + + /// Reads the next band of rows into the indices: `Some(true)` once every row is read (then + /// [`Self::finish`]), `Some(false)` while rows are left, `None` when no indices can be recorded. + pub(crate) fn step(&mut self) -> Option { + let Self { reader, table, surface, map, band, shown, bits, width, height, y, .. } = self; + let (bits, w, band_y) = (*bits, *width, *y); + while *y < *height && *y - band_y < TILE_SIZE { + let row = reader.next_row().ok()??; + surface.read_rgba8_into(Rect::from_xywh(0, *y, w, 1), shown); + for (x, px) in shown.iter().enumerate() { + let bit = x * bits; + let byte = *row.data().get(bit / 8)?; + let i = (byte >> (8 - bits - bit % 8)) & (0xff >> (8 - bits)); + let entry = table.colors.get(usize::from(i))?; + let ok = if px[3] == 0 { table.transparent == Some(i) } else { px[..3] == entry[..] }; + if !ok { + return None; + } + band.push(i); } - band.push(i); + *y += 1; } - y += 1; - if band.len() >= band_len || y == height { - map.try_write_interleaved(Rect::from_xywh(0, band_y, w, u32::try_from(y - band_y).ok()?), &band).ok()?; - band.clear(); - band_y = y; + map.try_write_interleaved(Rect::from_xywh(0, band_y, w, u32::try_from(*y - band_y).ok()?), band).ok()?; + band.clear(); + Some(*y == *height) + } + + /// The indices, once [`Self::step`] has read every row. + pub(crate) fn finish(mut self) -> Option { + // An eXIf after the pixels counts too. + self.reader.finish().ok()?; + if self.reader.info().exif_metadata.as_deref().is_some_and(|e| codecs::exif_orientation(e) != 1) || self.y != self.height { + return None; + } + Some(ColorIndices { layer: self.layer, pixels: self.surface, indices: self.map }) + } +} + +/// The indices of a palette PNG opened as `r` (see [`PngIndices`]), read with a cancel check +/// between bands of rows. +fn read_png_indices(r: &mut ImportResult, bytes: &[u8], ctl: &Interrupt) -> Result<(), IoError> { + let Some(mut read) = PngIndices::new(bytes, &r.document) else { return Ok(()) }; + loop { + ctl.check().map_err(|_| IoError::Cancelled)?; + match read.step() { + Some(false) => {} + Some(true) => break, + None => return Ok(()), } } - // An eXIf after the pixels counts too. - reader.finish().ok()?; - if reader.info().exif_metadata.as_deref().is_some_and(|e| codecs::exif_orientation(e) != 1) || y != height { - return None; - } - Some(ColorIndices { layer: layer.id, pixels: surface.clone(), indices: map }) + r.document.color_indices = read.finish(); + Ok(()) } /// What every flat format does to the document of its pixels: the palette mode, the save /// settings, a Targa's alpha channel and the profile an untagged image needs. `img` is the -/// decoded image, when the document was built from one (a streamed PNG has none). +/// decoded image, when the document was built from one (a streamed PNG has none). A palette +/// PNG's indices are read after it, by [`PngIndices`], which a stepped import can step. pub(crate) fn apply_flat_format(r: &mut ImportResult, bytes: &[u8], format: Option, img: Option<&Image>) -> Result<(), IoError> { // A palette BMP, PNG or GIF opens in Indexed Color mode with its colour table, as in // Photoshop (#3802, #4804, #7886). A GIF is always palette-based: the first frame's table. @@ -351,9 +405,6 @@ pub(crate) fn apply_flat_format(r: &mut ImportResult, bytes: &[u8], format: Opti { r.document.mode = ColorMode::Indexed; r.document.color_table = Some(photocraft_doc::ColorTable { colors, transparent }); - if format == Some(Format::Png) { - r.document.color_indices = png_color_indices(bytes, &r.document); - } } // The decoder's count of frames or pages left out (set above) stays alongside the encoding. let encoding = file_encoding_as(format, bytes); diff --git a/crates/io/src/png_steps.rs b/crates/io/src/png_steps.rs index b5a5f34914..c9c0127246 100644 --- a/crates/io/src/png_steps.rs +++ b/crates/io/src/png_steps.rs @@ -13,7 +13,7 @@ use photocraft_codecs::{self as codecs, DecodeOptions, Format, Limits, OrientSte use photocraft_geom::TILE_SIZE; use photocraft_raster::{Interrupt, PruneCursor}; -use crate::flat::{FlatBuilder, PngStream, apply_flat_format}; +use crate::flat::{FlatBuilder, PngIndices, PngStream, apply_flat_format}; use crate::{ImportResult, IoError, Route}; /// The rows of a file being streamed into the tiles of its layer. @@ -29,6 +29,15 @@ struct Pruning { extras: PngExtras, } +/// A palette PNG's document, its file being read again for the indices of entries that share a +/// colour (#14342). +struct Indexing { + result: ImportResult, + read: PngIndices, + /// Whether the layer was streamed. + streamed: bool, +} + enum Stage { /// The rows becoming tiles as they are decoded. Stream(Box), @@ -40,6 +49,8 @@ enum Stage { Orient(Box), /// The decoded image becoming the document, a band of rows and a batch of tiles at a time. Build(Box), + /// The palette indices, a band of rows at a time. + Indices(Box), Spent, } @@ -85,7 +96,11 @@ impl PngImport { /// image never exists whole. False for a file whose rows can't come in final order, and from /// the moment a late `eXIf` sends a streamed one back to a whole decode. pub fn streams(&self) -> bool { - matches!(self.stage, Stage::Stream(_) | Stage::Prune(_)) + match &self.stage { + Stage::Stream(_) | Stage::Prune(_) => true, + Stage::Indices(i) => i.streamed, + _ => false, + } } /// How much of the import is done, `0.0..=1.0`. @@ -94,13 +109,14 @@ impl PngImport { Stage::Stream(s) => s.rows.progress(), Stage::Rows(rows) => rows.progress(), Stage::Orient(steps) => steps.progress(), - Stage::Prune(_) | Stage::Build(_) | Stage::Spent => 1.0, + Stage::Prune(_) | Stage::Build(_) | Stage::Indices(_) | Stage::Spent => 1.0, } } /// Works while `more()` returns true (at least one bounded piece per call): rows (into tiles - /// as they come, or into the whole image), the turn upright, the copy into tiles, then the - /// prune scan. `Some` once the document is built, after which the import is spent. + /// as they come, or into the whole image), the turn upright, the copy into tiles, the prune + /// scan, then a palette's indices. `Some` once the document is built, after which the import + /// is spent. pub fn step(&mut self, more: &mut dyn FnMut() -> bool) -> Result, IoError> { match std::mem::replace(&mut self.stage, Stage::Spent) { Stage::Stream(mut s) => { @@ -131,7 +147,7 @@ impl PngImport { let Pruning { stream, extras, .. } = *p; let mut r = stream.into_document(&self.name, extras)?; apply_flat_format(&mut r, &self.bytes, Some(Format::Png), None)?; - return Ok(Some(r)); + return self.indices(r, true, more); } if !more() { self.stage = Stage::Prune(p); @@ -175,9 +191,33 @@ impl PngImport { } continue; } - return build.finish(self.bytes.as_slice(), Some(Format::Png)).map(Some); + let r = build.finish(self.bytes.as_slice(), Some(Format::Png))?; + return self.indices(r, false, more); + }, + Stage::Indices(mut i) => loop { + match i.read.step() { + Some(false) if more() => {} + Some(false) => { + self.stage = Stage::Indices(i); + return Ok(None); + } + Some(true) => { + let Indexing { mut result, read, .. } = *i; + result.document.color_indices = read.finish(); + return Ok(Some(result)); + } + None => return Ok(Some(i.result)), + } }, Stage::Spent => Err(IoError::Unsupported("the PNG was already imported".into())), } } + + /// The built document `result`, or, for a palette whose entries share a colour, the stage that + /// reads its indices, entered in this call while `more()` allows. + fn indices(&mut self, result: ImportResult, streamed: bool, more: &mut dyn FnMut() -> bool) -> Result, IoError> { + let Some(read) = PngIndices::new(SharedBytes(Arc::clone(&self.bytes)), &result.document) else { return Ok(Some(result)) }; + self.stage = Stage::Indices(Box::new(Indexing { result, read, streamed })); + if more() { self.step(more) } else { Ok(None) } + } } diff --git a/crates/io/tests/it/png_steps.rs b/crates/io/tests/it/png_steps.rs index f62e3bc57a..cd01899299 100644 --- a/crates/io/tests/it/png_steps.rs +++ b/crates/io/tests/it/png_steps.rs @@ -88,6 +88,22 @@ fn stepped_png_import_keeps_a_palette_as_indexed_colour() { assert_stepped_equals_import("p.png", &bytes, "palette"); } +/// A palette whose entries share a colour is read again for its indices a band of rows per step, +/// and gives the indices the one-shot import gives (#14342). +#[test] +fn a_palette_png_is_read_for_its_indices_a_band_of_rows_per_step() { + let (w, h) = (3, photocraft_geom::TILE_SIZE as u32 * 3 + 5); + let idx: Vec = (0..w * h).map(|i| (i % 2) as u8).collect(); + let shared = photocraft_codecs::encode_png_indexed(w, h, &idx, &[[255; 3], [255; 3]], None, None, None).unwrap(); + let distinct = photocraft_codecs::encode_png_indexed(w, h, &idx, &[[255; 3], [0; 3]], None, None, None).unwrap(); + assert!(whole("p.png", &shared).document.color_indices.is_some(), "the one-shot import records them"); + assert_stepped_equals_import("p.png", &shared, "entries sharing a colour"); + let (_, with) = stepped_zero_budget("p.png", &shared); + let (_, without) = stepped_zero_budget("p.png", &distinct); + // Four bands of rows, each a call of its own with an empty budget. + assert_eq!(with, without + 4); +} + #[test] fn stepped_png_import_takes_a_step_per_row_and_bounded_steps_to_build_the_document() { let bytes = encode(&noise(8, 20, ChannelLayout::Rgba, SampleType::U8), Format::Png, &EncodeOptions::default()).unwrap();