summaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFJob.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <jberkenbilt@users.noreply.github.com>2023-03-18 20:34:27 +0100
committerGitHub <noreply@github.com>2023-03-18 20:34:27 +0100
commitfe36ef141c7d77548175666eb952cd185f2d9fa6 (patch)
treea30e500226bc6a7b13f6e794484218e6bd93ede3 /libqpdf/QPDFJob.cc
parent1e53da74bc3cf0cbd3bd3dae2890e83ad33c3ed4 (diff)
parentf6b13fcc05d3d1367a03d54e83fe0605366e49fd (diff)
downloadqpdf-fe36ef141c7d77548175666eb952cd185f2d9fa6.tar.zst
Merge pull request #924 from cdosborn/main
Improve --optimize-images to find images nested within XObjects
Diffstat (limited to 'libqpdf/QPDFJob.cc')
-rw-r--r--libqpdf/QPDFJob.cc49
1 files changed, 24 insertions, 25 deletions
diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc
index b57a791b..4916be41 100644
--- a/libqpdf/QPDFJob.cc
+++ b/libqpdf/QPDFJob.cc
@@ -2363,31 +2363,30 @@ QPDFJob::handleTransformations(QPDF& pdf)
int pageno = 0;
for (auto& ph: dh.getAllPages()) {
++pageno;
- QPDFObjectHandle page = ph.getObjectHandle();
- for (auto& iter2: ph.getImages()) {
- std::string name = iter2.first;
- QPDFObjectHandle& image = iter2.second;
- ImageOptimizer* io = new ImageOptimizer(
- *this,
- m->oi_min_width,
- m->oi_min_height,
- m->oi_min_area,
- image);
- std::shared_ptr<QPDFObjectHandle::StreamDataProvider> sdp(io);
- if (io->evaluate(
- "image " + name + " on page " +
- std::to_string(pageno))) {
- QPDFObjectHandle new_image = pdf.newStream();
- new_image.replaceDict(image.getDict().shallowCopy());
- new_image.replaceStreamData(
- sdp,
- QPDFObjectHandle::newName("/DCTDecode"),
- QPDFObjectHandle::newNull());
- ph.getAttribute("/Resources", true)
- .getKey("/XObject")
- .replaceKey(name, new_image);
- }
- }
+ ph.forEachImage(
+ true,
+ [this, pageno, &pdf](
+ QPDFObjectHandle& obj,
+ QPDFObjectHandle& xobj_dict,
+ std::string const& key) {
+ auto io = std::make_unique<ImageOptimizer>(
+ *this,
+ m->oi_min_width,
+ m->oi_min_height,
+ m->oi_min_area,
+ obj);
+ if (io->evaluate(
+ "image " + key + " on page " +
+ std::to_string(pageno))) {
+ QPDFObjectHandle new_image = pdf.newStream();
+ new_image.replaceDict(obj.getDict().shallowCopy());
+ new_image.replaceStreamData(
+ std::move(io),
+ QPDFObjectHandle::newName("/DCTDecode"),
+ QPDFObjectHandle::newNull());
+ xobj_dict.replaceKey(key, new_image);
+ }
+ });
}
}
if (m->generate_appearances) {