Commit e62f1e4e4457487b3ae7b729b5fb7a78081b0478
1 parent
0f85e452
Add Pl_DCT::make_compress_config
Showing
5 changed files
with
43 additions
and
3 deletions
include/qpdf/Pl_DCT.hh
| ... | ... | @@ -24,6 +24,7 @@ |
| 24 | 24 | |
| 25 | 25 | #include <qpdf/Pl_Buffer.hh> |
| 26 | 26 | #include <cstddef> |
| 27 | +#include <functional> | |
| 27 | 28 | |
| 28 | 29 | // jpeglib.h must be included after cstddef or else it messes up the definition of size_t. |
| 29 | 30 | #include <jpeglib.h> |
| ... | ... | @@ -62,6 +63,10 @@ class QPDF_DLL_CLASS Pl_DCT: public Pipeline |
| 62 | 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 | 70 | // Constructor for compressing image data |
| 66 | 71 | QPDF_DLL |
| 67 | 72 | Pl_DCT( | ... | ... |
libqpdf/Pl_DCT.cc
| 1 | 1 | #include <qpdf/Pl_DCT.hh> |
| 2 | 2 | |
| 3 | -#include "qpdf/QPDFLogger.hh" | |
| 4 | 3 | #include <qpdf/QIntC.hh> |
| 5 | 4 | #include <qpdf/QTC.hh> |
| 6 | 5 | |
| ... | ... | @@ -14,6 +13,23 @@ |
| 14 | 13 | |
| 15 | 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 | 33 | struct qpdf_jpeg_error_mgr |
| 18 | 34 | { |
| 19 | 35 | struct jpeg_error_mgr pub; |
| ... | ... | @@ -407,3 +423,9 @@ Pl_DCT::decompress(void* cinfo_p, Buffer* b) |
| 407 | 423 | (void)jpeg_finish_decompress(cinfo); |
| 408 | 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 | 57 | size_t oi_min_height; |
| 58 | 58 | size_t oi_min_area; |
| 59 | 59 | QPDFObjectHandle image; |
| 60 | + std::shared_ptr<Pl_DCT::CompressConfig> config; | |
| 60 | 61 | }; |
| 61 | 62 | |
| 62 | 63 | class DiscardContents: public QPDFObjectHandle::ParserCallbacks |
| ... | ... | @@ -115,6 +116,7 @@ ImageOptimizer::ImageOptimizer( |
| 115 | 116 | oi_min_area(oi_min_area), |
| 116 | 117 | image(image) |
| 117 | 118 | { |
| 119 | + config = Pl_DCT::make_compress_config([](jpeg_compress_struct* config) {}); | |
| 118 | 120 | } |
| 119 | 121 | |
| 120 | 122 | std::shared_ptr<Pipeline> |
| ... | ... | @@ -195,7 +197,7 @@ ImageOptimizer::makePipeline(std::string const& description, Pipeline* next) |
| 195 | 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 | 201 | return result; |
| 200 | 202 | } |
| 201 | 203 | ... | ... |
libtests/dct_compress.cc
| 1 | 1 | #include <qpdf/Pl_DCT.hh> |
| 2 | 2 | #include <qpdf/Pl_StdioFile.hh> |
| 3 | 3 | #include <qpdf/QUtil.hh> |
| 4 | +#include <qpdf/assert_test.h> | |
| 4 | 5 | |
| 5 | 6 | #include <cstdio> |
| 6 | 7 | #include <cstdlib> |
| ... | ... | @@ -50,12 +51,16 @@ main(int argc, char* argv[]) |
| 50 | 51 | FILE* infile = QUtil::safe_fopen(infilename, "rb"); |
| 51 | 52 | FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); |
| 52 | 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 | 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 | 59 | while (size_t len = fread(buf, 1, sizeof(buf), infile)) { |
| 56 | 60 | dct.write(buf, len); |
| 57 | 61 | } |
| 58 | 62 | dct.finish(); |
| 63 | + assert(called); | |
| 59 | 64 | |
| 60 | 65 | fclose(infile); |
| 61 | 66 | fclose(outfile); | ... | ... |
manual/release-notes.rst
| ... | ... | @@ -38,6 +38,12 @@ more detail. |
| 38 | 38 | unencrypted. qpdf has always incorrectly handled all |
| 39 | 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 | 47 | - CLI Enhancements |
| 42 | 48 | |
| 43 | 49 | - New :qpdf:ref:`--remove-structure` option to exclude the document | ... | ... |