Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Krita: uncompressed tiles are read as interleaved pixels (#7082) (#7083)

David Mann977cf5f6de89
diff --git a/crates/automation/tests/krita.rs b/crates/automation/tests/krita.rs
index 85bde4f5f..44de53ff2 100644
--- a/crates/automation/tests/krita.rs
+++ b/crates/automation/tests/krita.rs
@@ -4,14 +4,15 @@
use photocraft_automation::Headless;
use serde_json::json;
-/// A tiled pixel file with one raw 64x64 tile of `bgra` at (0, 0).
+/// A tiled pixel file with one raw 64x64 tile of `bgra` at (0, 0). Krita writes a raw tile
+/// (flag 0) as plain interleaved pixels; only LZF tiles are split into byte planes.
fn tiles(bgra: [u8; 4]) -> Vec<u8> {
let n = 64 * 64;
let mut out = b"VERSION 2\nTILEWIDTH 64\nTILEHEIGHT 64\nPIXELSIZE 4\nDATA 1\n".to_vec();
out.extend(format!("0,0,LZF,{}\n", 1 + n * 4).into_bytes());
out.push(0);
- for b in bgra {
- out.extend(std::iter::repeat_n(b, n));
+ for _ in 0..n {
+ out.extend(bgra);
}
out
}
diff --git a/crates/io/src/kra/tests.rs b/crates/io/src/kra/tests.rs
index 4316a74c1..92e951cdb 100644
--- a/crates/io/src/kra/tests.rs
+++ b/crates/io/src/kra/tests.rs
@@ -28,7 +28,8 @@ fn tile_file(pixel_size: usize, tiles: &[(i32, i32, Vec<u8>)]) -> Vec<u8> {
planar[k * n + p] = px[p * pixel_size + k];
}
}
- let body: Vec<u8> = if i % 2 == 1 { [vec![1], lzf_literal(&planar)].concat() } else { [vec![0], planar].concat() };
+ // Krita splits only LZF tiles into byte planes; raw tiles stay interleaved.
+ let body: Vec<u8> = if i % 2 == 1 { [vec![1], lzf_literal(&planar)].concat() } else { [&[0][..], px].concat() };
out.extend(format!("{x},{y},LZF,{}\n", body.len()).into_bytes());
out.extend(body);
}
@@ -160,6 +161,22 @@ fn a_nonzero_default_pixel_fills_untouched_areas() {
assert_eq!(s.to_interleaved(Rect::from_xywh(100, 100, 1, 1)), [0, 0, 255, 255]);
}
+#[test]
+fn raw_tiles_hold_interleaved_pixels() {
+ // Krita writes a tile uncompressed (flag 0) when LZF can't shrink it, and then the pixels
+ // stay interleaved: only LZF tiles are split into byte planes.
+ let mut body = vec![0u8];
+ body.extend((0..T * T).flat_map(|_| [10u8, 20, 30, 255]));
+ let mut file = format!("VERSION 2\nTILEWIDTH {T}\nTILEHEIGHT {T}\nPIXELSIZE 4\nDATA 1\n0,0,LZF,{}\n", body.len()).into_bytes();
+ file.extend(body);
+ let doc = maindoc("RGBA", r#"<layer nodetype="paintlayer" name="L" colorspacename="RGBA" filename="l"/>"#);
+ let r = crate::import("a.kra", &archive(&doc, &[("img/layers/l", file)])).unwrap();
+ let s = layer(&r.document, "L").surface().unwrap();
+ for (x, y) in [(0, 0), (1, 0), (3, 3)] {
+ assert_eq!(s.to_interleaved(Rect::from_xywh(x, y, 1, 1)), [30, 20, 10, 255], "pixel {x},{y}");
+ }
+}
+
fn merged_png() -> Vec<u8> {
let img = Image::from_raw(12, 8, ChannelLayout::Rgba, CSample::U8, vec![90; 12 * 8 * 4]).unwrap();
photocraft_codecs::encode(&img, Format::Png, &Default::default()).unwrap()
diff --git a/crates/io/src/kra/tiles.rs b/crates/io/src/kra/tiles.rs
index 8af6638aa..67fd02fca 100644
--- a/crates/io/src/kra/tiles.rs
+++ b/crates/io/src/kra/tiles.rs
@@ -231,19 +231,22 @@ impl<'a> TileFile<'a> {
for &(x, y, data) in &self.tiles {
ctl.check().map_err(|_| IoError::Cancelled)?;
let (flag, payload) = data.split_first().ok_or_else(|| invalid("empty tile"))?;
- let planar: std::borrow::Cow<'_, [u8]> = match flag {
- 1 => lzf_decompress(payload, expected)?.into(),
- 0 if payload.len() == expected => payload.into(),
- 0 => return Err(invalid("raw tile has the wrong size")),
- f => return Err(invalid(format!("unknown tile compression flag {f}"))),
- };
- // Byte planes: byte k of pixel i is at k * n + i.
- for (k, plane) in planar.chunks_exact(n).enumerate() {
- for (i, b) in plane.iter().enumerate() {
- if let Some(slot) = interleaved.get_mut(i * self.pixel_size + k) {
- *slot = *b;
+ match flag {
+ 1 => {
+ // LZF tiles hold byte planes: byte k of pixel i is at k * n + i.
+ let planar = lzf_decompress(payload, expected)?;
+ for (k, plane) in planar.chunks_exact(n).enumerate() {
+ for (i, b) in plane.iter().enumerate() {
+ if let Some(slot) = interleaved.get_mut(i * self.pixel_size + k) {
+ *slot = *b;
+ }
+ }
}
}
+ // Raw tiles (written when LZF doesn't shrink them) are already interleaved.
+ 0 if payload.len() == expected => interleaved.copy_from_slice(payload),
+ 0 => return Err(invalid("raw tile has the wrong size")),
+ f => return Err(invalid(format!("unknown tile compression flag {f}"))),
}
space.convert(&interleaved, fmt, &mut out);
let (x0, y0) =