render: delegate TIFF and EPS PackBits encoding to craft-stream-codecs

This commit is contained in:
Dylan M. Taylor
2026-11-16 10:08:08 -05:00
parent a6e94de555
commit ae86489406
4 changed files with 26 additions and 28 deletions
Generated
+11 -3
View File
@@ -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",
+1 -1
View File
@@ -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
+13
View File
@@ -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]);
}
+1 -24
View File
@@ -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<u8>) {
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.