Merge pull request #924 from cdosborn/main

Improve --optimize-images to find images nested within XObjects
This commit is contained in:
Jay Berkenbilt 2023-03-18 15:34:27 -04:00 committed by GitHub
commit fe36ef141c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 25 deletions

View File

@ -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) {

View File

@ -33,6 +33,8 @@ my @image_opt = (
['large-inline-image', 'inline-images-keep-all', '--keep-inline-images'],
['unsupported-optimization', 'unsupported',
'--oi-min-width=0 --oi-min-height=0 --oi-min-area=0'],
['nested-images', 'nested-images',
'--oi-min-width=0 --oi-min-height=0 --oi-min-area=0']
);
my $n_tests = 2 * scalar(@image_opt);

Binary file not shown.

View File

@ -0,0 +1,18 @@
{
"version": 2,
"parameters": {
"decodelevel": "generalized"
},
"pages": [
{
"contents": [
"4 0 R"
],
"images": [],
"label": null,
"object": "3 0 R",
"outlines": [],
"pageposfrom1": 1
}
]
}

View File

@ -0,0 +1,2 @@
qpdf: image /X1 on page 1: optimizing image reduces size from 2628 to ...
qpdf: wrote file a.pdf