From ae864894069c9d46440355698a732aa8c7ffd3dd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 10:08:08 -0500 Subject: [PATCH] render: delegate TIFF and EPS PackBits encoding to craft-stream-codecs --- Cargo.lock | 14 +++++++++++--- Cargo.toml | 2 +- crates/render/src/encode/tests_tiff.rs | 13 +++++++++++++ crates/render/src/encode/tiff.rs | 25 +------------------------ 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 783e02a9bc..d4338ac63f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1253,11 +1253,11 @@ dependencies = [ [[package]] name = "craft-stream-codecs" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd#a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" +source = "git+https://github.com/storytold/craft-libs?rev=ed850ede0f26ef0b8ff9b4d1fc08466a1cf21375#ed850ede0f26ef0b8ff9b4d1fc08466a1cf21375" dependencies = [ "fax", "hayro-ccitt", - "hayro-jbig2", + "hayro-jbig2 0.3.0 (git+https://github.com/storytold/craft-libs?rev=ed850ede0f26ef0b8ff9b4d1fc08466a1cf21375)", "thiserror 2.0.21", ] @@ -2540,6 +2540,14 @@ dependencies = [ "hayro-ccitt", ] +[[package]] +name = "hayro-jbig2" +version = "0.3.0" +source = "git+https://github.com/storytold/craft-libs?rev=ed850ede0f26ef0b8ff9b4d1fc08466a1cf21375#ed850ede0f26ef0b8ff9b4d1fc08466a1cf21375" +dependencies = [ + "hayro-ccitt", +] + [[package]] name = "hayro-jpeg2000" version = "0.3.5" @@ -2561,7 +2569,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc6 dependencies = [ "flate2", "hayro-ccitt", - "hayro-jbig2", + "hayro-jbig2 0.3.0 (git+https://github.com/storytold/craft-libs?rev=a499742a9389f47d10cdc61e4cdbf4e5f07f48cd)", "hayro-jpeg2000", "memchr", "rustc-hash 2.1.3", diff --git a/Cargo.toml b/Cargo.toml index 89d6328ea7..ae7835b750 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" } craft-tiff = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" } craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" } -craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" } +craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "ed850ede0f26ef0b8ff9b4d1fc08466a1cf21375" } craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd" } craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "a499742a9389f47d10cdc61e4cdbf4e5f07f48cd", features = ["egui"] } # internal diff --git a/crates/render/src/encode/tests_tiff.rs b/crates/render/src/encode/tests_tiff.rs index 392e4ed6d2..17dea965a0 100644 --- a/crates/render/src/encode/tests_tiff.rs +++ b/crates/render/src/encode/tests_tiff.rs @@ -232,3 +232,16 @@ fn lzw_strips_read_back_as_weezl_reads_them() { assert!(same >= weezl.len().min(4_300), "{} bytes: the same for {same}", strip.len()); } } + +#[test] +fn shared_packbits_preserves_packet_bytes_and_append_contract() { + let mut out = vec![42]; + t::packbits(b"aabbccc", &mut out); + assert_eq!(out, [42, 3, b'a', b'a', b'b', b'b', 254, b'c']); + let before = out.clone(); + t::packbits(&[], &mut out); + assert_eq!(out, before); + let mut run = vec![]; + t::packbits(&[7; 129], &mut run); + assert_eq!(run, [129, 7, 0, 7]); +} diff --git a/crates/render/src/encode/tiff.rs b/crates/render/src/encode/tiff.rs index 6a01f78109..8be67aca20 100644 --- a/crates/render/src/encode/tiff.rs +++ b/crates/render/src/encode/tiff.rs @@ -282,30 +282,7 @@ fn resolution(ppi: f64) -> Values { /// `data` run-length encoded (PackBits: what TIFF's compression 32773 and PostScript's /// `RunLengthDecode` read), without an end marker. pub fn packbits(data: &[u8], out: &mut Vec) { - let mut i = 0; - while i < data.len() { - let Some(&b) = data.get(i) else { break }; - let run = data.get(i..).map_or(1, |rest| rest.iter().take(128).take_while(|x| **x == b).count()); - if run >= 3 { - out.push((257 - run) as u8); - out.push(b); - i += run; - continue; - } - // Literal bytes up to the next run of three. - let mut j = i; - while j < data.len() && j - i < 128 { - let three = data.get(j..j + 3).is_some_and(|w| w[0] == w[1] && w[1] == w[2]); - if three { - break; - } - j += 1; - } - let j = j.max(i + 1).min(data.len()); - out.push((j - i - 1) as u8); - out.extend(data.get(i..j).unwrap_or_default()); - i = j; - } + craft_stream_codecs::runlength::encode_packbits_into(data, out); } /// The TIFF options of a raster export.