Commit e62f1e4e4457487b3ae7b729b5fb7a78081b0478

Authored by Jay Berkenbilt
1 parent 0f85e452

Add Pl_DCT::make_compress_config

include/qpdf/Pl_DCT.hh
@@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
24 24
25 #include <qpdf/Pl_Buffer.hh> 25 #include <qpdf/Pl_Buffer.hh>
26 #include <cstddef> 26 #include <cstddef>
  27 +#include <functional>
27 28
28 // jpeglib.h must be included after cstddef or else it messes up the definition of size_t. 29 // jpeglib.h must be included after cstddef or else it messes up the definition of size_t.
29 #include <jpeglib.h> 30 #include <jpeglib.h>
@@ -62,6 +63,10 @@ class QPDF_DLL_CLASS Pl_DCT: public Pipeline @@ -62,6 +63,10 @@ class QPDF_DLL_CLASS Pl_DCT: public Pipeline
62 virtual void apply(jpeg_compress_struct*) = 0; 63 virtual void apply(jpeg_compress_struct*) = 0;
63 }; 64 };
64 65
  66 + QPDF_DLL
  67 + static std::shared_ptr<CompressConfig>
  68 + make_compress_config(std::function<void(jpeg_compress_struct*)>);
  69 +
65 // Constructor for compressing image data 70 // Constructor for compressing image data
66 QPDF_DLL 71 QPDF_DLL
67 Pl_DCT( 72 Pl_DCT(
libqpdf/Pl_DCT.cc
1 #include <qpdf/Pl_DCT.hh> 1 #include <qpdf/Pl_DCT.hh>
2 2
3 -#include "qpdf/QPDFLogger.hh"  
4 #include <qpdf/QIntC.hh> 3 #include <qpdf/QIntC.hh>
5 #include <qpdf/QTC.hh> 4 #include <qpdf/QTC.hh>
6 5
@@ -14,6 +13,23 @@ @@ -14,6 +13,23 @@
14 13
15 namespace 14 namespace
16 { 15 {
  16 + class FunctionCallbackConfig: public Pl_DCT::CompressConfig
  17 + {
  18 + public:
  19 + explicit FunctionCallbackConfig(std::function<void(jpeg_compress_struct*)> f) :
  20 + f(std::move(f))
  21 + {
  22 + }
  23 + ~FunctionCallbackConfig() override = default;
  24 + void
  25 + apply(jpeg_compress_struct* config) override
  26 + {
  27 + f(config);
  28 + };
  29 +
  30 + std::function<void(jpeg_compress_struct*)> f;
  31 + };
  32 +
17 struct qpdf_jpeg_error_mgr 33 struct qpdf_jpeg_error_mgr
18 { 34 {
19 struct jpeg_error_mgr pub; 35 struct jpeg_error_mgr pub;
@@ -407,3 +423,9 @@ Pl_DCT::decompress(void* cinfo_p, Buffer* b) @@ -407,3 +423,9 @@ Pl_DCT::decompress(void* cinfo_p, Buffer* b)
407 (void)jpeg_finish_decompress(cinfo); 423 (void)jpeg_finish_decompress(cinfo);
408 next()->finish(); 424 next()->finish();
409 } 425 }
  426 +
  427 +std::shared_ptr<Pl_DCT::CompressConfig>
  428 +Pl_DCT::make_compress_config(std::function<void(jpeg_compress_struct*)> f)
  429 +{
  430 + return std::make_shared<FunctionCallbackConfig>(f);
  431 +}
libqpdf/QPDFJob.cc
@@ -57,6 +57,7 @@ namespace @@ -57,6 +57,7 @@ namespace
57 size_t oi_min_height; 57 size_t oi_min_height;
58 size_t oi_min_area; 58 size_t oi_min_area;
59 QPDFObjectHandle image; 59 QPDFObjectHandle image;
  60 + std::shared_ptr<Pl_DCT::CompressConfig> config;
60 }; 61 };
61 62
62 class DiscardContents: public QPDFObjectHandle::ParserCallbacks 63 class DiscardContents: public QPDFObjectHandle::ParserCallbacks
@@ -115,6 +116,7 @@ ImageOptimizer::ImageOptimizer( @@ -115,6 +116,7 @@ ImageOptimizer::ImageOptimizer(
115 oi_min_area(oi_min_area), 116 oi_min_area(oi_min_area),
116 image(image) 117 image(image)
117 { 118 {
  119 + config = Pl_DCT::make_compress_config([](jpeg_compress_struct* config) {});
118 } 120 }
119 121
120 std::shared_ptr<Pipeline> 122 std::shared_ptr<Pipeline>
@@ -195,7 +197,7 @@ ImageOptimizer::makePipeline(std::string const&amp; description, Pipeline* next) @@ -195,7 +197,7 @@ ImageOptimizer::makePipeline(std::string const&amp; description, Pipeline* next)
195 return result; 197 return result;
196 } 198 }
197 199
198 - result = std::make_shared<Pl_DCT>("jpg", next, w, h, components, cs); 200 + result = std::make_shared<Pl_DCT>("jpg", next, w, h, components, cs, config.get());
199 return result; 201 return result;
200 } 202 }
201 203
libtests/dct_compress.cc
1 #include <qpdf/Pl_DCT.hh> 1 #include <qpdf/Pl_DCT.hh>
2 #include <qpdf/Pl_StdioFile.hh> 2 #include <qpdf/Pl_StdioFile.hh>
3 #include <qpdf/QUtil.hh> 3 #include <qpdf/QUtil.hh>
  4 +#include <qpdf/assert_test.h>
4 5
5 #include <cstdio> 6 #include <cstdio>
6 #include <cstdlib> 7 #include <cstdlib>
@@ -50,12 +51,16 @@ main(int argc, char* argv[]) @@ -50,12 +51,16 @@ main(int argc, char* argv[])
50 FILE* infile = QUtil::safe_fopen(infilename, "rb"); 51 FILE* infile = QUtil::safe_fopen(infilename, "rb");
51 FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); 52 FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
52 Pl_StdioFile out("stdout", outfile); 53 Pl_StdioFile out("stdout", outfile);
  54 + bool called = false;
  55 + auto callback = [&called](jpeg_compress_struct*) { called = true; };
  56 + auto config = Pl_DCT::make_compress_config(callback);
53 unsigned char buf[100]; 57 unsigned char buf[100];
54 - Pl_DCT dct("dct", &out, width, height, components, cs); 58 + Pl_DCT dct("dct", &out, width, height, components, cs, config.get());
55 while (size_t len = fread(buf, 1, sizeof(buf), infile)) { 59 while (size_t len = fread(buf, 1, sizeof(buf), infile)) {
56 dct.write(buf, len); 60 dct.write(buf, len);
57 } 61 }
58 dct.finish(); 62 dct.finish();
  63 + assert(called);
59 64
60 fclose(infile); 65 fclose(infile);
61 fclose(outfile); 66 fclose(outfile);
manual/release-notes.rst
@@ -38,6 +38,12 @@ more detail. @@ -38,6 +38,12 @@ more detail.
38 unencrypted. qpdf has always incorrectly handled all 38 unencrypted. qpdf has always incorrectly handled all
39 ``/Metadata`` streams as special with cleartext metadata. 39 ``/Metadata`` streams as special with cleartext metadata.
40 40
  41 + - Library Enhancements
  42 +
  43 + - Add function ``Pl_DCT::make_compress_config`` to return a
  44 + ``Pl_DCT::CompressConfig`` shared pointer from a
  45 + ``std::function`` for a more modern configuration option.
  46 +
41 - CLI Enhancements 47 - CLI Enhancements
42 48
43 - New :qpdf:ref:`--remove-structure` option to exclude the document 49 - New :qpdf:ref:`--remove-structure` option to exclude the document