Skip to content

A fast, helpful, and open-source document parser

fix(wasm): PNG-encode OCR callback image data

Logan Markewichf97cd55d3bc4
diff --git a/Cargo.lock b/Cargo.lock
index 8a25d67..a64fcc2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2016,7 +2016,7 @@ checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0"
[[package]]
name = "liteparse"
-version = "2.14.0"
+version = "2.14.1"
dependencies = [
"blake3",
"clap",
@@ -2048,7 +2048,7 @@ dependencies = [
[[package]]
name = "liteparse-napi"
-version = "2.14.0"
+version = "2.14.1"
dependencies = [
"image",
"liteparse",
@@ -2064,7 +2064,7 @@ dependencies = [
[[package]]
name = "liteparse-pdfium"
-version = "1.7.0"
+version = "1.8.0"
dependencies = [
"blake3",
"liteparse-pdfium-sys",
@@ -2072,7 +2072,7 @@ dependencies = [
[[package]]
name = "liteparse-pdfium-sys"
-version = "1.7.0"
+version = "1.8.0"
dependencies = [
"bindgen",
"flate2",
@@ -2083,7 +2083,7 @@ dependencies = [
[[package]]
name = "liteparse-python"
-version = "2.14.0"
+version = "2.14.1"
dependencies = [
"anyhow",
"clap",
@@ -2099,7 +2099,7 @@ dependencies = [
[[package]]
name = "liteparse-wasm"
-version = "2.14.0"
+version = "2.14.1"
dependencies = [
"console_error_panic_hook",
"js-sys",
diff --git a/crates/liteparse-wasm/src/lib.rs b/crates/liteparse-wasm/src/lib.rs
index 1341a02..a3d743e 100644
--- a/crates/liteparse-wasm/src/lib.rs
+++ b/crates/liteparse-wasm/src/lib.rs
@@ -934,7 +934,8 @@ pub struct ImageRect {
// ---------------------------------------------------------------------------
/// Wraps a JS object that exposes an async `recognize(imageData, width, height, language)`
-/// method, returning `Promise<Array<{text, bbox, confidence}>>`.
+/// method, returning `Promise<Array<{text, bbox, confidence}>>`. `imageData`
+/// is PNG-encoded (the documented contract), not the raw pixel buffer.
///
/// `JsValue` is `!Send`, but on `wasm32` (single-threaded) the trait does not
/// require `Send + Sync`, so this works.
@@ -970,13 +971,18 @@ impl OcrEngine for JsOcrEngine {
> + '_,
>,
> {
- // Copy bytes into a JS Uint8Array up-front (must happen on the
- // current thread anyway in wasm).
- let arr = Uint8Array::new_with_length(image_data.len() as u32);
- arr.copy_from(image_data);
+ // The documented JS contract is PNG bytes (packages/wasm/README.md):
+ // browser OCR libraries (tesseract.js, remote services) consume
+ // encoded images, not raw pixel buffers. `image_data` is the render
+ // pipeline's tightly-packed grayscale or RGB buffer.
+ let png = liteparse::extract::encode_pixels_png(image_data, width, height)
+ .map_err(|e| format!("failed to PNG-encode page for ocrEngine.recognize: {e}"));
let language = options.language.clone();
Box::pin(async move {
+ let png = png?;
+ let arr = Uint8Array::new_with_length(png.len() as u32);
+ arr.copy_from(&png);
let recognize: JsValue = Reflect::get(&self.obj, &JsValue::from_str("recognize"))
.map_err(|e| format!("ocrEngine.recognize lookup failed: {:?}", e))?;
let recognize: Function = recognize
@@ -1037,6 +1043,7 @@ pub struct OcrResult {
#[wasm_bindgen(typescript_custom_section)]
const TS_EXTRA: &'static str = r#"
export interface OcrEngine {
+ /** `imageData` is a PNG-encoded render of the page, `width`/`height` its pixel dimensions. */
recognize(imageData: Uint8Array, width: number, height: number, language: string): Promise<OcrResult[]>;
}
diff --git a/crates/liteparse/src/extract.rs b/crates/liteparse/src/extract.rs
index bebc23d..f354cfa 100644
--- a/crates/liteparse/src/extract.rs
+++ b/crates/liteparse/src/extract.rs
@@ -917,6 +917,34 @@ pub(crate) fn encode_png(rgba: &[u8], width: u32, height: u32) -> Result<Vec<u8>
Ok(png_buf)
}
+/// Encode tightly-packed pixel bytes to PNG, inferring the color type from the
+/// buffer length: 1 byte/px (grayscale), 3 (RGB), or 4 (RGBA). The OCR render
+/// pipeline produces the first two (`RenderedPage::pixels`); the wasm OCR
+/// bridge uses this to hand PNG bytes to the JS `recognize` callback.
+pub fn encode_pixels_png(
+ pixels: &[u8],
+ width: u32,
+ height: u32,
+) -> Result<Vec<u8>, LiteParseError> {
+ let px = width as usize * height as usize;
+ let color = if px > 0 && pixels.len() == px {
+ image::ExtendedColorType::L8
+ } else if px > 0 && pixels.len() == px * 3 {
+ image::ExtendedColorType::Rgb8
+ } else if px > 0 && pixels.len() == px * 4 {
+ image::ExtendedColorType::Rgba8
+ } else {
+ return Err(LiteParseError::Other(format!(
+ "pixel buffer length {} does not match {width}x{height} at 1/3/4 bytes per pixel",
+ pixels.len()
+ )));
+ };
+ let mut png_buf = Vec::new();
+ let encoder = image::codecs::png::PngEncoder::new(&mut png_buf);
+ encoder.write_image(pixels, width, height, color)?;
+ Ok(png_buf)
+}
+
/// Walk image objects on a page and return a stable per-page `ImageRef` for
/// each one. `obj_index` is the index among image-typed page objects (not all
/// page objects), so a later embed pass can pull pixel bytes via
@@ -2909,6 +2937,29 @@ mod tests {
use super::*;
use std::f32::consts::PI;
+ #[test]
+ fn encode_pixels_png_infers_color_type() {
+ for (bpp, expected_color) in [
+ (1, image::ColorType::L8),
+ (3, image::ColorType::Rgb8),
+ (4, image::ColorType::Rgba8),
+ ] {
+ let pixels = vec![0x7Fu8; 2 * 3 * bpp];
+ let png = encode_pixels_png(&pixels, 2, 3).unwrap();
+ assert_eq!(&png[..8], b"\x89PNG\r\n\x1a\n");
+ let decoded = image::load_from_memory(&png).unwrap();
+ assert_eq!((decoded.width(), decoded.height()), (2, 3));
+ assert_eq!(decoded.color(), expected_color);
+ }
+ }
+
+ #[test]
+ fn encode_pixels_png_rejects_mismatched_length() {
+ assert!(encode_pixels_png(&[0u8; 5], 2, 3).is_err());
+ assert!(encode_pixels_png(&[], 2, 3).is_err());
+ assert!(encode_pixels_png(&[0u8; 3], 0, 0).is_err());
+ }
+
fn rotated_text_pdf() -> Vec<u8> {
let content =
b"BT /F1 10 Tf 20 40 Td (FIRSTMARK) Tj ET\nBT /F1 10 Tf 20 250 Td (SECONDMARK) Tj ET";
diff --git a/scripts/browser-compat/wasm-test.html b/scripts/browser-compat/wasm-test.html
index e16137d..2d286fd 100644
--- a/scripts/browser-compat/wasm-test.html
+++ b/scripts/browser-compat/wasm-test.html
@@ -82,6 +82,10 @@
if (imageData.length === 0) {
throw new Error("OCR input is empty");
}
+ const pngSignature = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
+ if (!pngSignature.every((byte, index) => imageData[index] === byte)) {
+ throw new Error("OCR input is not PNG-encoded");
+ }
if (width <= 0 || height <= 0) {
throw new Error(`Invalid OCR dimensions: ${width}x${height}`);
}