xberg 1.2.2

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! End-to-end integration tests for `ImageExtractionConfig.output_format`.
//!
//! Drives real extractors through `extract_uri_document_blocking` and asserts that the
//! chosen `ImageOutputFormat` variant propagates correctly to
//! `ExtractedDocument.images[*].format` and raw byte magic-number signatures.
//!
//! Fixtures used:
//!   - `pdf/embedded_images_tables.pdf`  — PDF with embedded JPEG images
//!   - `pdf/multipage_marketing.pdf`     — multi-page PDF with embedded images
//!   - `docx/word_image_anchors.docx`    — DOCX with anchored embedded images
//!   - `images/example.jpg`              — standalone JPEG file
//!   - `images/test_hello_world.png`     — PNG with OCR-able text
//!
//! Each test is `#[cfg(feature = "image-encode")]` because the re-encode pass
//! is only present when that feature is compiled in. The full feature set
//! (`--features full`) and the minimal combo (`--features pdf,image-encode`)
//! both satisfy this guard.

#![allow(clippy::print_stdout, clippy::print_stderr, clippy::dbg_macro)] // ~keep: test/bench binaries print by design; org logging policy exempts tests
#![cfg(feature = "image-encode")]

mod helpers;
use helpers::extract_uri_document_blocking;

use xberg::core::config::extraction::ImageOutputFormat;
use xberg::core::config::{ExtractionConfig, ImageExtractionConfig, OutputFormat};

/// PNG magic bytes: `\x89PNG\r\n\x1a\n`
#[cfg(any(feature = "pdf", feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
const PNG_MAGIC: &[u8] = b"\x89PNG\r\n\x1a\n";

/// JPEG SOI marker: `\xFF\xD8\xFF`
#[cfg(any(feature = "pdf", all(feature = "ocr", feature = "image-encode")))]
const JPEG_MAGIC: &[u8] = b"\xFF\xD8\xFF";

/// WebP container: `RIFF` at offset 0, `WEBP` at offset 8
#[cfg(feature = "pdf")]
fn is_webp(data: &[u8]) -> bool {
    data.len() >= 12 && data.starts_with(b"RIFF") && &data[8..12] == b"WEBP"
}

fn test_documents_dir() -> std::path::PathBuf {
    std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("test_documents")
}

fn fixture_path(relative: &str) -> std::path::PathBuf {
    test_documents_dir().join(relative)
}

/// Build an `ExtractionConfig` that enables image extraction with the given
/// `ImageOutputFormat`.
fn config_with_output_format(output_format: ImageOutputFormat) -> ExtractionConfig {
    ExtractionConfig {
        output_format: OutputFormat::Markdown,
        images: Some(ImageExtractionConfig {
            extract_images: true,
            output_format,
            ..Default::default()
        }),
        use_cache: false,
        ..Default::default()
    }
}

/// Native passthrough: without specifying an output format (or by explicitly
/// choosing `Native`), the images returned reflect whatever format the extractor
/// produced from the PDF. No re-encode pass is applied, so the format field must
/// be consistent with a raw PDF embedded image (typically "jpeg" or "png").
///
/// Verify this is stable by extracting twice — once with `Native` (explicit) and
/// once without an `ImageExtractionConfig.output_format` field — and confirming
/// both runs produce the same set of format strings.
#[cfg(feature = "pdf")]
#[test]
fn pdf_native_passthrough() {
    let path = fixture_path("pdf/embedded_images_tables.pdf");
    if !path.exists() {
        eprintln!("skipped: pdf/embedded_images_tables.pdf not present");
        return;
    }

    let config_explicit_native = config_with_output_format(ImageOutputFormat::Native);

    let config_default = ExtractionConfig {
        output_format: OutputFormat::Markdown,
        images: Some(ImageExtractionConfig {
            extract_images: true,
            ..Default::default()
        }),
        use_cache: false,
        ..Default::default()
    };

    let result_native =
        extract_uri_document_blocking(&path, None, &config_explicit_native).expect("native extraction must succeed");
    let result_default =
        extract_uri_document_blocking(&path, None, &config_default).expect("default extraction must succeed");

    let images_native = result_native
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");
    let images_default = result_default
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    assert!(
        !images_native.is_empty(),
        "embedded_images_tables.pdf must yield at least one image with Native format"
    );
    assert_eq!(
        images_native.len(),
        images_default.len(),
        "Native explicit and Default must extract the same number of images"
    );

    let formats_native: Vec<&str> = images_native.iter().map(|i| i.format.as_ref()).collect();
    let formats_default: Vec<&str> = images_default.iter().map(|i| i.format.as_ref()).collect();
    assert_eq!(
        formats_native, formats_default,
        "Native explicit and Default must produce identical format strings per image"
    );
}

/// Force PNG: every extracted image must report `format == "png"` and its raw
/// bytes must start with the 8-byte PNG signature.
#[cfg(feature = "pdf")]
#[test]
fn pdf_force_png() {
    let path = fixture_path("pdf/embedded_images_tables.pdf");
    if !path.exists() {
        eprintln!("skipped: pdf/embedded_images_tables.pdf not present");
        return;
    }

    let config = config_with_output_format(ImageOutputFormat::Png);
    let result = extract_uri_document_blocking(&path, None, &config).expect("PNG extraction must succeed");

    let images = result
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    assert!(
        !images.is_empty(),
        "embedded_images_tables.pdf must yield at least one image"
    );

    for img in images {
        assert_eq!(
            img.format.as_ref(),
            "png",
            "image at index {} must report format=\"png\" after PNG re-encode, got \"{}\"",
            img.image_index,
            img.format
        );
        assert!(
            img.data.starts_with(PNG_MAGIC),
            "image at index {} has format=\"png\" but bytes do not start with PNG magic \
             (first 8 bytes: {:02x?})",
            img.image_index,
            &img.data[..8.min(img.data.len())]
        );
    }
}

/// Force JPEG: every extracted image must report `format == "jpeg"` and its raw
/// bytes must start with the JPEG SOI marker `\xFF\xD8\xFF`.
#[cfg(feature = "pdf")]
#[test]
fn pdf_force_jpeg() {
    let path = fixture_path("pdf/embedded_images_tables.pdf");
    if !path.exists() {
        eprintln!("skipped: pdf/embedded_images_tables.pdf not present");
        return;
    }

    let config = config_with_output_format(ImageOutputFormat::Jpeg { quality: 85 });
    let result = extract_uri_document_blocking(&path, None, &config).expect("JPEG extraction must succeed");

    let images = result
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    assert!(
        !images.is_empty(),
        "embedded_images_tables.pdf must yield at least one image"
    );

    for img in images {
        assert_eq!(
            img.format.as_ref(),
            "jpeg",
            "image at index {} must report format=\"jpeg\" after JPEG re-encode, got \"{}\"",
            img.image_index,
            img.format
        );
        assert!(
            img.data.starts_with(JPEG_MAGIC),
            "image at index {} has format=\"jpeg\" but bytes do not start with JPEG SOI \
             (first 3 bytes: {:02x?})",
            img.image_index,
            &img.data[..3.min(img.data.len())]
        );
    }
}

/// Force WebP: every extracted image must report `format == "webp"` and its raw
/// bytes must match the RIFF/WEBP container signature.
#[cfg(feature = "pdf")]
#[test]
fn pdf_force_webp() {
    let path = fixture_path("pdf/embedded_images_tables.pdf");
    if !path.exists() {
        eprintln!("skipped: pdf/embedded_images_tables.pdf not present");
        return;
    }

    let config = config_with_output_format(ImageOutputFormat::Webp { quality: 80 });
    let result = extract_uri_document_blocking(&path, None, &config).expect("WebP extraction must succeed");

    let images = result
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    assert!(
        !images.is_empty(),
        "embedded_images_tables.pdf must yield at least one image"
    );

    for img in images {
        assert_eq!(
            img.format.as_ref(),
            "webp",
            "image at index {} must report format=\"webp\" after WebP re-encode, got \"{}\"",
            img.image_index,
            img.format
        );
        assert!(
            is_webp(&img.data),
            "image at index {} has format=\"webp\" but bytes do not match RIFF/WEBP signature \
             (first 12 bytes: {:02x?})",
            img.image_index,
            &img.data[..12.min(img.data.len())]
        );
    }
}

/// Office DOCX mixed-to-PNG: extract a DOCX with embedded images and force all
/// output to PNG. Every image must report `format == "png"`.
///
/// `word_image_anchors.docx` contains anchored images of various formats.
#[cfg(feature = "office")]
#[test]
fn office_mixed_to_png() {
    let path = fixture_path("docx/word_image_anchors.docx");
    if !path.exists() {
        eprintln!("skipped: docx/word_image_anchors.docx not present");
        return;
    }

    let config = config_with_output_format(ImageOutputFormat::Png);
    let result = extract_uri_document_blocking(&path, None, &config).expect("DOCX PNG extraction must succeed");

    let images = match result.images.as_ref() {
        Some(v) if !v.is_empty() => v,
        _ => {
            eprintln!("skipped: word_image_anchors.docx yielded no extracted images");
            return;
        }
    };

    for img in images {
        let undecodable = result
            .processing_warnings
            .iter()
            .any(|w| w.source.as_ref() == "image_encoder" && w.message.contains(&format!("image_{}", img.image_index)));
        if undecodable {
            continue;
        }

        assert_eq!(
            img.format.as_ref(),
            "png",
            "image at index {} must report format=\"png\" after PNG re-encode; \
             got \"{}\". Warnings: {:?}",
            img.image_index,
            img.format,
            result
                .processing_warnings
                .iter()
                .map(|w| (w.source.as_ref(), w.message.as_ref()))
                .collect::<Vec<_>>()
        );
        assert!(
            img.data.starts_with(PNG_MAGIC),
            "image at index {} must start with PNG magic bytes",
            img.image_index
        );
    }
}

/// Standalone JPEG to PNG: extract a raw JPEG file with `output_format = Png`
/// and verify the single returned image has `format == "png"` with matching
/// PNG magic bytes.
///
/// Also verifies that image dimensions are preserved after re-encoding: the
/// decoded PNG must report the same (width, height) as the source JPEG.
///
/// Requires `ocr-pipeline` (or `ocr`/`ocr-wasm`) because the `ImageExtractor`
/// that handles `image/jpeg` standalone files is only registered under those
/// features.
#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
#[test]
fn image_extractor_jpeg_to_png() {
    let path = fixture_path("images/example.jpg");
    if !path.exists() {
        eprintln!("skipped: images/example.jpg not present");
        return;
    }

    let native_config = ExtractionConfig {
        images: Some(ImageExtractionConfig {
            extract_images: true,
            output_format: ImageOutputFormat::Native,
            ..Default::default()
        }),
        use_cache: false,
        ..Default::default()
    };
    let native_result =
        extract_uri_document_blocking(&path, None, &native_config).expect("native JPEG extraction must succeed");

    let png_config = config_with_output_format(ImageOutputFormat::Png);
    let png_result = extract_uri_document_blocking(&path, None, &png_config).expect("PNG JPEG extraction must succeed");

    let images = png_result
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    assert!(
        !images.is_empty(),
        "example.jpg must yield at least one extracted image after PNG re-encode"
    );

    let img = &images[0];
    assert_eq!(
        img.format.as_ref(),
        "png",
        "standalone JPEG re-encoded to PNG must report format=\"png\", got \"{}\"",
        img.format
    );
    assert!(
        img.data.starts_with(PNG_MAGIC),
        "PNG-re-encoded image must start with PNG magic bytes (got {:02x?})",
        &img.data[..8.min(img.data.len())]
    );

    if let Some(native_images) = native_result.images.as_ref()
        && !native_images.is_empty()
    {
        let native_img = &native_images[0];
        if let (Some(src_w), Some(src_h), Some(dst_w), Some(dst_h)) =
            (native_img.width, native_img.height, img.width, img.height)
        {
            assert_eq!(
                (src_w, src_h),
                (dst_w, dst_h),
                "JPEG→PNG re-encode must preserve dimensions; \
                 source ({src_w}×{src_h}) != re-encoded ({dst_w}×{dst_h})"
            );
        }
    }
}

/// OCR before re-encode: extract an image containing recognizable text with
/// OCR enabled and `output_format = Jpeg`.
///
/// Verifies that:
/// 1. `result.content` is non-empty (OCR ran and extracted text).
/// 2. The final image has `format == "jpeg"` (re-encode ran after OCR).
///
/// If OCR had run *after* re-encode, both assertions might still pass, but
/// the test documents the expected pipeline stage order and will catch any
/// accidental reordering that breaks the OCR path.
#[cfg(all(feature = "ocr", feature = "image-encode"))]
#[test]
fn ocr_runs_before_reencode() {
    use xberg::core::config::OcrConfig;

    let path = fixture_path("images/test_hello_world.png");
    if !path.exists() {
        eprintln!("skipped: images/test_hello_world.png not present");
        return;
    }

    let config = ExtractionConfig {
        ocr: Some(OcrConfig {
            backend: "tesseract".to_string(),
            language: vec!["eng".to_string()],
            ..Default::default()
        }),
        force_ocr: true,
        images: Some(ImageExtractionConfig {
            extract_images: true,
            output_format: ImageOutputFormat::Jpeg { quality: 85 },
            ..Default::default()
        }),
        use_cache: false,
        ..Default::default()
    };

    let result = extract_uri_document_blocking(&path, None, &config).expect("OCR + JPEG re-encode must succeed");

    assert!(
        !result.content.trim().is_empty(),
        "OCR must populate result.content even when output_format=Jpeg; got empty content"
    );

    let images = result
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    assert!(
        !images.is_empty(),
        "test_hello_world.png must yield at least one image entry"
    );

    let jpeg_images: Vec<_> = images.iter().filter(|i| i.format.as_ref() == "jpeg").collect();
    assert!(
        !jpeg_images.is_empty(),
        "output_format=Jpeg must re-encode at least one image to JPEG; \
         got formats: {:?}",
        images.iter().map(|i| i.format.as_ref()).collect::<Vec<_>>()
    );

    for img in &jpeg_images {
        assert!(
            img.data.starts_with(JPEG_MAGIC),
            "JPEG-re-encoded image at index {} must start with JPEG SOI bytes",
            img.image_index
        );
    }
}

/// Quality affects byte size: extract the same PDF twice — once at high quality
/// (JPEG q=95) and once at low quality (JPEG q=30). For at least one image the
/// low-quality output must be smaller than the high-quality output.
///
/// This proves that the `quality` field flows all the way through to the encoder
/// and is not silently ignored.
#[cfg(feature = "pdf")]
#[test]
fn quality_change_alters_byte_size() {
    let path = fixture_path("pdf/multipage_marketing.pdf");
    if !path.exists() {
        eprintln!("skipped: pdf/multipage_marketing.pdf not present");
        return;
    }

    let config_high = config_with_output_format(ImageOutputFormat::Jpeg { quality: 95 });
    let config_low = config_with_output_format(ImageOutputFormat::Jpeg { quality: 30 });

    let result_high =
        extract_uri_document_blocking(&path, None, &config_high).expect("high-quality JPEG extraction must succeed");
    let result_low =
        extract_uri_document_blocking(&path, None, &config_low).expect("low-quality JPEG extraction must succeed");

    let images_high = result_high
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");
    let images_low = result_low
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    if images_high.is_empty() {
        eprintln!("skipped: multipage_marketing.pdf yielded no extractable images");
        return;
    }

    assert_eq!(
        images_high.len(),
        images_low.len(),
        "high-quality and low-quality runs must extract the same number of images"
    );

    let any_smaller = images_high
        .iter()
        .zip(images_low.iter())
        .any(|(high, low)| low.data.len() < high.data.len());

    assert!(
        any_smaller,
        "JPEG quality=30 must produce at least one image smaller than quality=95; \
         sizes at q=95: {:?}, sizes at q=30: {:?}",
        images_high.iter().map(|i| i.data.len()).collect::<Vec<_>>(),
        images_low.iter().map(|i| i.data.len()).collect::<Vec<_>>()
    );
}

/// Unsupported source format: when a fixture produces a vector/metafile image
/// (SVG, EMF, WMF) and `output_format != Native`, the re-encode pass must leave
/// that image's bytes untouched and emit a `ProcessingWarning` whose `source ==
/// "image_encoder"`.
///
/// This test looks for such images in `word_image_anchors.docx`. If none are
/// found (because the fixture has no EMF/WMF embedded images), it logs a message
/// and returns without failing — the pipeline smoke test in
/// `core/pipeline/tests.rs` already covers the svg path directly.
#[cfg(feature = "office")]
#[test]
fn unsupported_source_format_warns_and_preserves() {
    let path = fixture_path("docx/word_image_anchors.docx");
    if !path.exists() {
        eprintln!("skipped: docx/word_image_anchors.docx not present");
        return;
    }

    let config = config_with_output_format(ImageOutputFormat::Png);
    let result = extract_uri_document_blocking(&path, None, &config).expect("DOCX extraction must not error out");

    let encoder_warnings: Vec<_> = result
        .processing_warnings
        .iter()
        .filter(|w| w.source.as_ref() == "image_encoder")
        .collect();

    if encoder_warnings.is_empty() {
        eprintln!(
            "word_image_anchors.docx produced no image_encoder warnings — \
             no undecodable vector images present in this fixture; \
             the pipeline smoke test covers svg/emf directly"
        );
        return;
    }

    for warning in &encoder_warnings {
        assert!(
            !warning.message.is_empty(),
            "image_encoder warning must have a non-empty message; got: {:?}",
            warning
        );
    }

    if let Some(images) = result.images.as_ref() {
        for img in images {
            assert!(
                !img.data.is_empty(),
                "image at index {} must retain non-empty data even when re-encode was skipped",
                img.image_index
            );
        }
    }
}

/// HEIF output: extract a PDF with embedded JPEG, force `output_format = Heif`,
/// and verify that every returned image has `format == "heif"`.
///
/// Requires the `heic` Cargo feature. Skipped automatically when not compiled in.
#[cfg(all(feature = "heic", feature = "pdf"))]
#[test]
fn heif_output() {
    use xberg::core::config::extraction::ImageOutputFormat;

    let path = fixture_path("pdf/embedded_images_tables.pdf");
    if !path.exists() {
        eprintln!("skipped: pdf/embedded_images_tables.pdf not present");
        return;
    }

    let config = config_with_output_format(ImageOutputFormat::Heif { quality: 80 });
    let result = extract_uri_document_blocking(&path, None, &config).expect("HEIF extraction must succeed");

    let images = result
        .images
        .as_ref()
        .expect("images must be Some when extract_images=true");

    assert!(
        !images.is_empty(),
        "embedded_images_tables.pdf must yield at least one image after HEIF re-encode"
    );

    for img in images {
        assert_eq!(
            img.format.as_ref(),
            "heif",
            "image at index {} must report format=\"heif\", got \"{}\"",
            img.image_index,
            img.format
        );
        assert!(
            !img.data.is_empty(),
            "HEIF image at index {} must have non-empty data",
            img.image_index
        );

        let parse_result = xberg_libheif::HeifContext::read_from_bytes(&img.data);
        assert!(
            parse_result.is_ok(),
            "image at index {} has format=\"heif\" but failed xberg_libheif round-trip: {:?}",
            img.image_index,
            parse_result.err()
        );
    }
}