Commit 8de2497a8fcfc659e9d0927aadd44ac4ee1f0d18

Authored by Connor Osborn
1 parent b27be3ed

Improve --optimize-images to find images nested within XObjects

Showing 1 changed file with 24 additions and 25 deletions
libqpdf/QPDFJob.cc
@@ -2349,31 +2349,30 @@ QPDFJob::handleTransformations(QPDF& pdf) @@ -2349,31 +2349,30 @@ QPDFJob::handleTransformations(QPDF& pdf)
2349 int pageno = 0; 2349 int pageno = 0;
2350 for (auto& ph: dh.getAllPages()) { 2350 for (auto& ph: dh.getAllPages()) {
2351 ++pageno; 2351 ++pageno;
2352 - QPDFObjectHandle page = ph.getObjectHandle();  
2353 - for (auto& iter2: ph.getImages()) {  
2354 - std::string name = iter2.first;  
2355 - QPDFObjectHandle& image = iter2.second;  
2356 - ImageOptimizer* io = new ImageOptimizer(  
2357 - *this,  
2358 - m->oi_min_width,  
2359 - m->oi_min_height,  
2360 - m->oi_min_area,  
2361 - image);  
2362 - std::shared_ptr<QPDFObjectHandle::StreamDataProvider> sdp(io);  
2363 - if (io->evaluate(  
2364 - "image " + name + " on page " +  
2365 - std::to_string(pageno))) {  
2366 - QPDFObjectHandle new_image = pdf.newStream();  
2367 - new_image.replaceDict(image.getDict().shallowCopy());  
2368 - new_image.replaceStreamData(  
2369 - sdp,  
2370 - QPDFObjectHandle::newName("/DCTDecode"),  
2371 - QPDFObjectHandle::newNull());  
2372 - ph.getAttribute("/Resources", true)  
2373 - .getKey("/XObject")  
2374 - .replaceKey(name, new_image);  
2375 - }  
2376 - } 2352 + ph.forEachImage(
  2353 + true,
  2354 + [this, pageno, &pdf](
  2355 + QPDFObjectHandle& obj,
  2356 + QPDFObjectHandle& xobj_dict,
  2357 + std::string const& key) {
  2358 + auto io = std::make_unique<ImageOptimizer>(
  2359 + *this,
  2360 + m->oi_min_width,
  2361 + m->oi_min_height,
  2362 + m->oi_min_area,
  2363 + obj);
  2364 + if (io->evaluate(
  2365 + "image " + key + " on page " +
  2366 + std::to_string(pageno))) {
  2367 + QPDFObjectHandle new_image = pdf.newStream();
  2368 + new_image.replaceDict(obj.getDict().shallowCopy());
  2369 + new_image.replaceStreamData(
  2370 + std::move(io),
  2371 + QPDFObjectHandle::newName("/DCTDecode"),
  2372 + QPDFObjectHandle::newNull());
  2373 + xobj_dict.replaceKey(key, new_image);
  2374 + }
  2375 + });
2377 } 2376 }
2378 } 2377 }
2379 if (m->generate_appearances) { 2378 if (m->generate_appearances) {