flpdf 0.10.0

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
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
//! Optimize eligible raster images during document transformations.
//!
//! qpdf correspondence: QPDFJob::ImageOptimizer and Pl_DCT image compression.
//!
//! qpdf 11.9.0 image-optimization transformation.
//!
//! The implementation follows `QPDFJob::ImageOptimizer` and the surrounding
//! `QPDFJob::handleTransformations` call order (`libqpdf/QPDFJob.cc:36-236,
//! 2137-2174`). Image data is decoded through the canonical specialized stream
//! pipeline, encoded through the qpdf-shaped `Pl_DCT` compression stage, and
//! installed as a deferred stream provider only when the encoded payload is
//! smaller than the original.

use crate::object_handle::ObjectHandle;
use crate::pipeline::count::Count;
use crate::pipeline::dct::PlDct;
use crate::pipeline::{Discard, Pipeline};
use crate::writer::DecodeLevel;
use crate::{PageDocumentHelper, PageObjectHelper, Pdf, QPDFLogger, Result, StreamDataProvider};
use std::io::{Read, Seek};
use std::rc::Rc;

const DEFAULT_OI_MIN_WIDTH: u32 = 128;
const DEFAULT_OI_MIN_HEIGHT: u32 = 128;
const DEFAULT_OI_MIN_AREA: u32 = 16_384;
const DEFAULT_II_MIN_BYTES: usize = 1_024;

/// Options controlling qpdf's `--optimize-images` transformation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImageOptimizationOptions {
    /// Minimum image width. Images at or below this width are retained.
    pub min_width: u32,
    /// Minimum image height. Images at or below this height are retained.
    pub min_height: u32,
    /// Minimum image area. Images at or below this area are retained.
    pub min_area: u32,
    /// Minimum encoded inline-image payload for externalization.
    pub inline_min_bytes: usize,
    /// Do not externalize inline images before optimizing XObjects.
    pub keep_inline_images: bool,
}

impl Default for ImageOptimizationOptions {
    fn default() -> Self {
        Self {
            min_width: DEFAULT_OI_MIN_WIDTH,
            min_height: DEFAULT_OI_MIN_HEIGHT,
            min_area: DEFAULT_OI_MIN_AREA,
            inline_min_bytes: DEFAULT_II_MIN_BYTES,
            keep_inline_images: false,
        }
    }
}

/// Apply qpdf's image transformation to every page and recursively reachable
/// Form XObject.
///
/// `logger` and `message_prefix` are the job-owned diagnostic route. The
/// transformation itself mutates only the document's canonical ObjectHandle
/// graph; image bytes remain deferred until the writer consumes the new
/// provider-backed stream, matching qpdf's `replaceStreamData` provider path.
pub fn optimize_images<R: Read + Seek + 'static>(
    pdf: &mut Pdf<R>,
    logger: &QPDFLogger,
    message_prefix: &[u8],
    verbose: bool,
    options: ImageOptimizationOptions,
) -> Result<()> {
    if !options.keep_inline_images {
        let pages = PageDocumentHelper::new(pdf).get_all_pages()?;
        for page in pages {
            PageObjectHelper::from_object_handle(page, pdf)
                .externalize_inline_images(options.inline_min_bytes, false)?;
        }
    }

    let pages = PageDocumentHelper::new(pdf).get_all_pages()?;
    for (page_index, page_handle) in pages.into_iter().enumerate() {
        let page_number = page_index + 1;
        let mut replacements = Vec::new();
        {
            let mut page = PageObjectHelper::from_object_handle(page_handle, pdf);
            page.for_each_image(true, |image, xobjects, key| {
                let description = format!(
                    "image {} on page {page_number}",
                    String::from_utf8_lossy(&key)
                );
                if !ImageOptimizer::preflight(&image)? {
                    log_skip(
                        logger,
                        message_prefix,
                        verbose,
                        &description,
                        SkipReason::UnableToDecode,
                    )?; // cov:ignore: llvm-cov attributes this successful multiline logger call to its opening expressions
                    return Ok(());
                }
                let optimizer = match ImageOptimizer::prepare(image.clone(), options)? {
                    PrepareResult::Ready(optimizer) => optimizer,
                    PrepareResult::Skip(reason) => {
                        log_skip(logger, message_prefix, verbose, &description, reason)?;
                        return Ok(());
                    }
                };

                let Some(evaluation) = optimizer.evaluate()? else {
                    return Ok(()); // cov:ignore: the preflight and evaluation use the same immutable source; only an external mutable provider can make the second pipe fail
                };
                match evaluation {
                    Evaluation::NotSmaller => {
                        log_verbose(
                            logger,
                            message_prefix,
                            verbose,
                            &description,
                            "not optimizing because DCT compression does not reduce image size"
                                .to_owned(),
                        )?; // cov:ignore: llvm-cov attributes this successful multiline logger call to its opening expressions
                    }
                    Evaluation::Smaller {
                        original_length,
                        compressed_length,
                    } => {
                        log_verbose(
                            logger,
                            message_prefix,
                            verbose,
                            &description,
                            format!(
                                "optimizing image reduces size from {original_length} to {compressed_length}"
                            ),
                        )?; // cov:ignore: llvm-cov attributes this successful multiline logger call to its opening expressions

                        let new_image = image.copy_stream()?;
                        new_image.replace_stream_data_provider(
                            Rc::new(optimizer),
                            Some(ObjectHandle::name(b"DCTDecode".to_vec())),
                            Some(ObjectHandle::null()),
                        )?; // cov:ignore: provider registration succeeds for the freshly copied indirect stream
                        replacements.push((xobjects, key, new_image));
                    }
                }
                Ok(())
            })?;
        }

        for (xobjects, key, new_image) in replacements {
            xobjects.replace_key(&key, new_image.clone())?;
        }
    }
    Ok(())
}

fn log_skip(
    logger: &QPDFLogger,
    message_prefix: &[u8],
    verbose: bool,
    description: &str,
    reason: SkipReason,
) -> Result<()> {
    log_verbose(
        logger,
        message_prefix,
        verbose,
        description,
        reason.message().to_owned(),
    )
}

fn log_verbose(
    logger: &QPDFLogger,
    message_prefix: &[u8],
    verbose: bool,
    description: &str,
    message: String,
) -> Result<()> {
    if verbose {
        let mut line = message_prefix.to_vec();
        line.extend_from_slice(b": ");
        line.extend_from_slice(description.as_bytes());
        line.extend_from_slice(b": ");
        line.extend_from_slice(message.as_bytes());
        line.push(b'\n');
        logger.info(line)?;
    }
    Ok(())
}

#[derive(Clone, Copy)]
enum SkipReason {
    MissingKeys,
    BitsPerComponent,
    Colorspace,
    TooSmall,
    UnableToDecode,
}

impl SkipReason {
    fn message(self) -> &'static str {
        match self {
            Self::MissingKeys => "not optimizing because image dictionary is missing required keys",
            Self::BitsPerComponent => {
                "not optimizing because image has other than 8 bits per component"
            }
            Self::Colorspace => {
                "not optimizing because qpdf can't optimize images with this colorspace"
            }
            Self::TooSmall => {
                "not optimizing because image is smaller than requested minimum dimensions"
            }
            Self::UnableToDecode => {
                "not optimizing because unable to decode data or data already uses DCT"
            }
        }
    }
}

enum PrepareResult {
    Ready(ImageOptimizer),
    Skip(SkipReason),
}

enum Evaluation {
    NotSmaller,
    Smaller {
        original_length: u64,
        compressed_length: u64,
    },
}

struct ImageOptimizer {
    image: ObjectHandle,
    dictionary: ObjectHandle,
    width: u32,
    height: u32,
    pixel_format: libjpeg_turbo_rs::PixelFormat,
}

impl ImageOptimizer {
    fn preflight(image: &ObjectHandle) -> Result<bool> {
        let mut discard = Discard;
        let mut filtering_attempted = false;
        let succeeded = image.pipe_stream_data(
            &mut discard,
            &mut filtering_attempted,
            0,
            DecodeLevel::Specialized,
            true,
            false,
        )?; // cov:ignore: the preflight pipeline call is the qpdf filterability probe; its successful continuation is attributed to the opening expressions
        Ok(succeeded && filtering_attempted)
    }

    fn prepare(image: ObjectHandle, options: ImageOptimizationOptions) -> Result<PrepareResult> {
        let dictionary = image.as_stream_dict().ok_or_else(|| {
            crate::Error::Internal("image XObject has no stream dictionary".into())
        })?;
        let width_value = dictionary.try_get_key(b"/Width")?;
        let height_value = dictionary.try_get_key(b"/Height")?;
        if !width_value.try_is_number()? || !height_value.try_is_number()? {
            return Ok(PrepareResult::Skip(SkipReason::MissingKeys));
        }
        let width = qpdf_dimension(&width_value)?;
        let height = qpdf_dimension(&height_value)?;

        let bits_per_component = dictionary.try_get_key(b"/BitsPerComponent")?;
        if !bits_per_component.try_is_integer()? || bits_per_component.try_get_int_value()? != 8 {
            return Ok(PrepareResult::Skip(SkipReason::BitsPerComponent));
        }

        let colorspace = dictionary.try_get_key(b"/ColorSpace")?;
        colorspace.try_dereference()?;
        let Some(colorspace) = colorspace.as_name() else {
            return Ok(PrepareResult::Skip(SkipReason::Colorspace));
        };
        let pixel_format = match colorspace.as_slice() {
            b"DeviceRGB" => libjpeg_turbo_rs::PixelFormat::Rgb,
            b"DeviceGray" => libjpeg_turbo_rs::PixelFormat::Grayscale,
            b"DeviceCMYK" => libjpeg_turbo_rs::PixelFormat::Cmyk,
            _ => return Ok(PrepareResult::Skip(SkipReason::Colorspace)),
        };

        let area = width.wrapping_mul(height);
        if (options.min_width > 0 && width <= options.min_width)
            || (options.min_height > 0 && height <= options.min_height)
            || (options.min_area > 0 && area <= options.min_area)
        {
            return Ok(PrepareResult::Skip(SkipReason::TooSmall));
        }

        Ok(PrepareResult::Ready(Self {
            image,
            dictionary,
            width,
            height,
            pixel_format,
        }))
    }

    fn evaluate(&self) -> Result<Option<Evaluation>> {
        let mut discard = Discard;
        let mut count = Count::new("count", &mut discard);
        let mut encoder = self.encoder(&mut count);
        let mut filtering_attempted = false;
        let succeeded = self.image.pipe_stream_data(
            &mut encoder,
            &mut filtering_attempted,
            0,
            DecodeLevel::Specialized,
            false,
            false,
        )?; // cov:ignore: the second pipe is over the same immutable source already accepted by preflight; only an external mutable provider can return false here
        drop(encoder);
        if !succeeded {
            return Ok(None); // cov:ignore: StreamDataProvider requires stable bytes across calls; preflight already accepted this source
        }

        let original_length = self
            .dictionary
            .try_get_key(b"/Length")?
            .try_get_int_value()?
            .max(0) as u64;
        let compressed_length = count.count();
        if compressed_length >= original_length {
            Ok(Some(Evaluation::NotSmaller))
        } else {
            Ok(Some(Evaluation::Smaller {
                original_length,
                compressed_length,
            }))
        }
    }

    fn encoder<'a>(&self, next: &'a mut dyn Pipeline) -> PlDct<'a> {
        PlDct::new_compressor(
            "jpg",
            next,
            self.width as usize,
            self.height as usize,
            self.pixel_format,
        )
    }
}

impl StreamDataProvider for ImageOptimizer {
    fn provide_stream_data_by_id(
        &self,
        _object_number: u32,
        _generation: u16,
        pipeline: &mut dyn Pipeline,
    ) -> Result<()> {
        let mut encoder = self.encoder(pipeline);
        let mut filtering_attempted = false;
        let _ = self.image.pipe_stream_data(
            &mut encoder,
            &mut filtering_attempted,
            0,
            DecodeLevel::Specialized,
            false,
            false,
        )?; // cov:ignore: provider output is consumed by the writer's already-valid downstream pipeline
        Ok(())
    }
}

fn qpdf_dimension(value: &ObjectHandle) -> Result<u32> {
    if value.try_is_integer()? {
        let value = value.try_get_int_value()?;
        return Ok(value.clamp(0, i64::from(u32::MAX)) as u32);
    }
    let value = value.try_get_numeric_value()?;
    if value.is_nan() {
        return Ok(0);
    }
    Ok(value.clamp(0.0, f64::from(u32::MAX)) as u32)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::PlString;
    use std::rc::Rc;

    fn image_dictionary(width: ObjectHandle, height: ObjectHandle) -> ObjectHandle {
        ObjectHandle::dictionary(vec![
            (b"/Width".to_vec(), width),
            (b"/Height".to_vec(), height),
            (b"/BitsPerComponent".to_vec(), ObjectHandle::integer(8)),
            (
                b"/ColorSpace".to_vec(),
                ObjectHandle::name(b"DeviceGray".to_vec()),
            ),
            (b"/Length".to_vec(), ObjectHandle::integer(0)),
        ])
    }

    fn direct_image(width: usize, height: usize, data: Vec<u8>) -> ObjectHandle {
        let dictionary = image_dictionary(
            ObjectHandle::integer(width as i64),
            ObjectHandle::integer(height as i64),
        );
        dictionary
            .replace_key(b"/Length", ObjectHandle::integer(data.len() as i64))
            .unwrap();
        ObjectHandle::stream(dictionary, Rc::new(data))
    }

    fn prepared_gray_optimizer(width: usize, height: usize, data: Vec<u8>) -> ImageOptimizer {
        let image = direct_image(width, height, data);
        let dictionary = image.as_stream_dict().expect("image dictionary");
        ImageOptimizer {
            image,
            dictionary,
            width: width as u32,
            height: height as u32,
            pixel_format: libjpeg_turbo_rs::PixelFormat::Grayscale,
        }
    }

    #[test]
    fn prepare_matches_qpdf_metadata_skip_reasons_and_numeric_dimensions() {
        assert!(matches!(
            ImageOptimizer::prepare(
                ObjectHandle::stream(
                    ObjectHandle::dictionary(vec![(
                        b"/Width".to_vec(),
                        ObjectHandle::integer(200),
                    )]),
                    Rc::new(Vec::new()),
                ),
                ImageOptimizationOptions::default(),
            )
            .unwrap(),
            PrepareResult::Skip(SkipReason::MissingKeys)
        ));

        let bad_bits = ObjectHandle::stream(
            ObjectHandle::dictionary(vec![
                (b"/Width".to_vec(), ObjectHandle::integer(200)),
                (b"/Height".to_vec(), ObjectHandle::integer(200)),
                (b"/BitsPerComponent".to_vec(), ObjectHandle::integer(1)),
            ]),
            Rc::new(Vec::new()),
        );
        assert!(matches!(
            ImageOptimizer::prepare(bad_bits, ImageOptimizationOptions::default()).unwrap(),
            PrepareResult::Skip(SkipReason::BitsPerComponent)
        ));

        let bad_colorspace = ObjectHandle::stream(
            ObjectHandle::dictionary(vec![
                (b"/Width".to_vec(), ObjectHandle::integer(200)),
                (b"/Height".to_vec(), ObjectHandle::integer(200)),
                (b"/BitsPerComponent".to_vec(), ObjectHandle::integer(8)),
                (
                    b"/ColorSpace".to_vec(),
                    ObjectHandle::name(b"Pattern".to_vec()),
                ),
            ]),
            Rc::new(Vec::new()),
        );
        assert!(matches!(
            ImageOptimizer::prepare(bad_colorspace, ImageOptimizationOptions::default()).unwrap(),
            PrepareResult::Skip(SkipReason::Colorspace)
        ));

        let small_options = ImageOptimizationOptions {
            min_width: 200,
            ..ImageOptimizationOptions::default()
        };
        let small = ObjectHandle::stream(
            image_dictionary(ObjectHandle::integer(200), ObjectHandle::integer(200)),
            Rc::new(Vec::new()),
        );
        assert!(matches!(
            ImageOptimizer::prepare(small, small_options).unwrap(),
            PrepareResult::Skip(SkipReason::TooSmall)
        ));

        assert_eq!(qpdf_dimension(&ObjectHandle::real(200.9)).unwrap(), 200);
        assert_eq!(qpdf_dimension(&ObjectHandle::real(f64::NAN)).unwrap(), 0);
        assert_eq!(qpdf_dimension(&ObjectHandle::integer(-1)).unwrap(), 0);
        assert_eq!(
            qpdf_dimension(&ObjectHandle::integer(i64::MAX)).unwrap(),
            u32::MAX
        );
    }

    #[test]
    fn prepare_rejects_a_non_stream_image_handle() {
        let result = ImageOptimizer::prepare(
            ObjectHandle::integer(1),
            ImageOptimizationOptions::default(),
        );
        assert!(
            matches!(result, Err(crate::Error::Internal(message)) if message == "image XObject has no stream dictionary")
        );
    }

    #[test]
    fn optimizer_preflight_evaluate_and_provider_share_the_same_source() {
        let image = direct_image(200, 200, vec![128; 40_000]);
        assert!(ImageOptimizer::preflight(&image).unwrap());
        let optimizer = prepared_gray_optimizer(200, 200, vec![128; 40_000]);
        assert!(matches!(
            optimizer.evaluate().unwrap(),
            Some(Evaluation::Smaller { .. })
        ));

        let mut output = Vec::new();
        let mut sink = PlString::new("sink", None, &mut output);
        optimizer
            .provide_stream_data_by_id(0, 0, &mut sink)
            .expect("provider emits the same deterministic JPEG");
        assert!(output.starts_with(&[0xff, 0xd8]));
    }

    #[test]
    fn optimizer_keeps_a_source_when_jpeg_does_not_shrink_it() {
        let optimizer = prepared_gray_optimizer(1, 1, vec![128]);
        assert!(matches!(
            optimizer.evaluate().unwrap(),
            Some(Evaluation::NotSmaller)
        ));
    }
}