mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-15 23:02:22 +00:00
Use shared RunLength encoding for PDF image streams
This commit is contained in:
1 parent
1d0b311b4a
commit
75c4b01db3
5 files changed
+49
-33
No files matched your search
Generated
+27
@@ -1007,6 +1007,17 @@ name = "craft-sheet"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430955825553ab7b976f4#bb57d422d0bc842cbde430955825553ab7b976f4"
|
||||
|
||||
[[package]]
|
||||
name = "craft-stream-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bb57d422d0bc842cbde430955825553ab7b976f4#bb57d422d0bc842cbde430955825553ab7b976f4"
|
||||
dependencies = [
|
||||
"fax",
|
||||
"hayro-ccitt",
|
||||
"hayro-jbig2",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-ui"
|
||||
version = "0.1.0"
|
||||
@@ -2284,6 +2295,7 @@ dependencies = [
|
||||
name = "gridcraft-pdf"
|
||||
version = "0.19.1"
|
||||
dependencies = [
|
||||
"craft-stream-codecs",
|
||||
"harfrust",
|
||||
"sha2 0.11.1",
|
||||
"skrifa",
|
||||
@@ -2448,6 +2460,21 @@ dependencies = [
|
||||
"foldhash 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hayro-ccitt"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a"
|
||||
|
||||
[[package]]
|
||||
name = "hayro-jbig2"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69374b3668dd45aeb3d3145cda68f2c7b4f223aaa2511e67d076f1c7d741388d"
|
||||
dependencies = [
|
||||
"hayro-ccitt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
|
||||
@@ -31,6 +31,8 @@ gridcraft-math = { path = "crates/math" }
|
||||
# craft-libs revision.
|
||||
# craft-update: app-specific pretend versions apply to startup and manual checks.
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "=0.1.0", features = ["egui"] }
|
||||
# PDF image-stream RunLength encoding, at the existing shared revision.
|
||||
craft-stream-codecs = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
|
||||
# Compound files ([MS-CFB]) and password-encrypted OOXML packages ([MS-OFFCRYPTO]).
|
||||
craft-cfb = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
|
||||
craft-offcrypto = { git = "https://github.com/storytold/craft-libs", rev = "bb57d422d0bc842cbde430955825553ab7b976f4", version = "0.1.0" }
|
||||
|
||||
@@ -10,6 +10,7 @@ readme = "README.md"
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-stream-codecs.workspace = true
|
||||
harfrust = "=0.12.0"
|
||||
skrifa = { workspace = true }
|
||||
unicode-bidi = "=0.3.18"
|
||||
|
||||
+1
-33
@@ -979,39 +979,7 @@ pub struct PdfDoc {
|
||||
|
||||
/// PackBits-style run-length encoding (PDF `RunLengthDecode`).
|
||||
fn run_length(data: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(data.len() / 2 + 16);
|
||||
let mut i = 0;
|
||||
while i < data.len() {
|
||||
let b = data[i];
|
||||
let mut run = 1;
|
||||
while i + run < data.len() && run < 128 && data[i + run] == b {
|
||||
run += 1;
|
||||
}
|
||||
if run >= 2 {
|
||||
out.push((257 - run) as u8);
|
||||
out.push(b);
|
||||
i += run;
|
||||
continue;
|
||||
}
|
||||
// Literal run until the next repeat of 3+ or 128 bytes.
|
||||
let start = i;
|
||||
let mut len = 0;
|
||||
while i < data.len() && len < 128 {
|
||||
if i + 2 < data.len() && data[i] == data[i + 1] && data[i] == data[i + 2] {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
len += 1;
|
||||
}
|
||||
if len == 0 {
|
||||
// Next is a repeat; loop will handle it.
|
||||
continue;
|
||||
}
|
||||
out.push((len - 1) as u8);
|
||||
out.extend_from_slice(&data[start..start + len]);
|
||||
}
|
||||
out.push(128);
|
||||
out
|
||||
craft_stream_codecs::runlength::encode(data)
|
||||
}
|
||||
|
||||
/// Longest Info string written, in characters; longer values are cut on a character boundary.
|
||||
|
||||
@@ -1239,3 +1239,21 @@ fn symbol_text_is_never_shaped_as_arabic() -> Result<(), Box<dyn Error>> {
|
||||
assert_eq!(text_width(Font::Symbol, 10.0, "\u{03B1}ب"), f32::from(Font::Symbol.char_width('\u{03B1}') + Font::Symbol.char_width('ب')) / 100.0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_run_length_preserves_pixels_at_packet_boundaries() {
|
||||
for len in [0, 1, 2, 3, 127, 128, 129, 255, 256, 257, 1024] {
|
||||
for seed in [0u8, 127, 128, 255] {
|
||||
let repeats = vec![seed; len];
|
||||
let literals: Vec<u8> = (0..len).map(|i| seed.wrapping_add(i as u8)).collect();
|
||||
let pairs: Vec<u8> = (0..len).map(|i| (i / 2) as u8).collect();
|
||||
for pixels in [repeats, literals, pairs] {
|
||||
let encoded = run_length(&pixels);
|
||||
assert_eq!(encoded.last(), Some(&128));
|
||||
assert_eq!(rle_decode(&encoded), pixels);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Two equal bytes may now terminate a literal packet. PDF pixel values stay exact.
|
||||
assert_eq!(run_length(&[1, 2, 2, 3]), [0, 1, 255, 2, 0, 3, 128]);
|
||||
}
|
||||
Reference in new issue
Block a user