mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Palette PNG indices: no second full-image buffer, and a memory check
The index read kept every index in one vector and then copied it into the index surface, two bytes per pixel that the open's memory check never counted. Write the indices into the surface one band of rows at a time instead, and check that they and one band fit in free memory before reading. When they don't, no indices are recorded and Color Table falls back to the nearest entry; the open itself goes on.
This commit is contained in:
1 parent
02f6089600
commit
5c1357b46d
2 files changed
+34
-10
No files matched your search
@@ -244,6 +244,18 @@ fn a_transparent_entry_keeps_its_index() {
|
||||
assert_eq!([0, 1, 2].map(|x| surf.rgba(x, 0)), [[0.0; 4], WHITE, GREEN]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn indices_taller_than_one_band_of_rows_land_in_place() {
|
||||
// Rows past the first band (one tile high) go in with the next one, and the last band is short.
|
||||
let (w, h) = (3, photocraft_geom::TILE_SIZE as usize * 2 + 5);
|
||||
let indices: Vec<u8> = (0..w * h).map(|k| ((k / w + k % w) % 2) as u8).collect();
|
||||
let png = palette_png(w as u32, &indices, png::BitDepth::Eight, &[[255, 255, 255], [255, 255, 255]], None);
|
||||
let doc = photocraft_io::import("tall.png", &png).unwrap().document;
|
||||
let layer = &doc.layers[0];
|
||||
let known = doc.color_indices.as_ref().unwrap().known(layer.id, layer.surface().unwrap(), doc.bounds());
|
||||
assert_eq!(known.as_deref(), Some(&indices[..]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_palette_png_without_shared_colours_is_not_read_again() {
|
||||
// Every entry its own colour: the colours already name the entries, so the open skips the
|
||||
|
||||
+22
-10
@@ -265,8 +265,10 @@ impl FlatBuilder {
|
||||
/// 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:
|
||||
/// next to the layer, only the indices themselves (a byte per pixel) are held.
|
||||
/// 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<ColorIndices> {
|
||||
let table = doc.color_table.as_ref()?;
|
||||
// Only entries that share a colour need the file. When every entry has its own colour, each
|
||||
@@ -290,13 +292,18 @@ fn png_color_indices(bytes: &[u8], doc: &Document) -> Option<ColorIndices> {
|
||||
return None;
|
||||
}
|
||||
let width = usize::try_from(w).ok()?;
|
||||
let count = width.checked_mul(usize::try_from(h).ok()?)?;
|
||||
let mut indices = Vec::new();
|
||||
indices.try_reserve_exact(count).ok()?;
|
||||
let rows = usize::try_from(TILE_SIZE).ok()?;
|
||||
let band_len = width.checked_mul(rows)?;
|
||||
let needed = u64::from(w).checked_mul(u64::from(h))?.checked_add(u64::try_from(band_len).ok()?)?;
|
||||
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 = 0;
|
||||
let (mut y, mut band_y) = (0i32, 0i32);
|
||||
let height = i32::try_from(h).ok()?;
|
||||
while let Some(row) = reader.next_row().ok()? {
|
||||
if indices.len() >= count {
|
||||
if y >= height {
|
||||
return None;
|
||||
}
|
||||
surface.read_rgba8_into(Rect::from_xywh(0, y, w, 1), &mut shown);
|
||||
@@ -309,16 +316,21 @@ fn png_color_indices(bytes: &[u8], doc: &Document) -> Option<ColorIndices> {
|
||||
if !ok {
|
||||
return None;
|
||||
}
|
||||
indices.push(i);
|
||||
band.push(i);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
// 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) || indices.len() != count {
|
||||
if reader.info().exif_metadata.as_deref().is_some_and(|e| codecs::exif_orientation(e) != 1) || y != height {
|
||||
return None;
|
||||
}
|
||||
ColorIndices::new(layer.id, surface, doc.bounds(), &indices)
|
||||
Some(ColorIndices { layer: layer.id, pixels: surface.clone(), indices: map })
|
||||
}
|
||||
|
||||
/// What every flat format does to the document of its pixels: the palette mode, the save
|
||||
|
||||
Reference in new issue
Block a user