diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 2461386d..b8b25fa0 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -2544,14 +2544,20 @@ QPDFWriter::writeLinearized() { // Optimize file and enqueue objects in order - auto skip_stream_parameters = [this](QPDFObjectHandle& stream) { - bool compress_stream; - bool is_metadata; - if (willFilterStream(stream, compress_stream, is_metadata, nullptr)) { - return 2; - } else { - return 1; + std::map stream_cache; + + auto skip_stream_parameters = [this, &stream_cache](QPDFObjectHandle& stream) { + auto& result = stream_cache[stream.getObjectID()]; + if (result == 0) { + bool compress_stream; + bool is_metadata; + if (willFilterStream(stream, compress_stream, is_metadata, nullptr)) { + result = 2; + } else { + result = 1; + } } + return result; }; QPDF::Writer::optimize(m->pdf, m->obj, skip_stream_parameters);