mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-31 02:48:31 +00:00
Merge pull request #924 from cdosborn/main
Improve --optimize-images to find images nested within XObjects
This commit is contained in:
commit
fe36ef141c
@ -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) {
|
||||
|
@ -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);
|
||||
|
BIN
qpdf/qtest/qpdf/nested-images.pdf
Normal file
BIN
qpdf/qtest/qpdf/nested-images.pdf
Normal file
Binary file not shown.
18
qpdf/qtest/qpdf/optimize-images-nested-images-json.out
Normal file
18
qpdf/qtest/qpdf/optimize-images-nested-images-json.out
Normal 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
|
||||
}
|
||||
]
|
||||
}
|
2
qpdf/qtest/qpdf/optimize-images-nested-images.out
Normal file
2
qpdf/qtest/qpdf/optimize-images-nested-images.out
Normal file
@ -0,0 +1,2 @@
|
||||
qpdf: image /X1 on page 1: optimizing image reduces size from 2628 to ...
|
||||
qpdf: wrote file a.pdf
|
Loading…
x
Reference in New Issue
Block a user