From dac0598b94c877bec92a1edd78ae00021cfa1638 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 23 Aug 2019 19:54:08 -0400 Subject: [PATCH] Add ability to set zlib compression level globally --- ChangeLog | 5 +++++ include/qpdf/Pl_Flate.hh | 10 ++++++++++ libqpdf/Pl_Flate.cc | 10 +++++++++- zlib-flate/qtest/1.compressed-1 | Bin 0 -> 193 bytes zlib-flate/qtest/1.compressed-9 | Bin 0 -> 193 bytes zlib-flate/qtest/zf.test | 24 +++++++++++++++--------- zlib-flate/zlib-flate.cc | 17 +++++++++++++++-- 7 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 zlib-flate/qtest/1.compressed-1 create mode 100644 zlib-flate/qtest/1.compressed-9 diff --git a/ChangeLog b/ChangeLog index af06bf3f..cdfa3fce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2019-08-23 Jay Berkenbilt + + * Add option Pl_Flate::setCompressionLevel to globally set the + zlib compression level used by all Pl_Flate pipelines. + 2019-08-22 Jay Berkenbilt * In QPDFObjectHandle::ParserCallbacks, in addition to diff --git a/include/qpdf/Pl_Flate.hh b/include/qpdf/Pl_Flate.hh index f25adfbb..96e09d4b 100644 --- a/include/qpdf/Pl_Flate.hh +++ b/include/qpdf/Pl_Flate.hh @@ -28,6 +28,7 @@ class Pl_Flate: public Pipeline { public: static unsigned int const def_bufsize = 65536; + static int compression_level; enum action_e { a_inflate, a_deflate }; @@ -42,6 +43,15 @@ class Pl_Flate: public Pipeline QPDF_DLL virtual void finish(); + // Globally set compression level from 1 (fastest, least + // compression) to 9 (slowest, most compression). Use -1 to set + // the default compression level. This is passed directly to zlib. + // This method returns a pointer to the current Pl_Flate object so + // you can create a pipeline with + // Pl_Flate(...)->setCompressionLevel(...) + QPDF_DLL + static void setCompressionLevel(int); + private: void handleData(unsigned char* data, size_t len, int flush); void checkError(char const* prefix, int error_code); diff --git a/libqpdf/Pl_Flate.cc b/libqpdf/Pl_Flate.cc index a782255b..1eca837e 100644 --- a/libqpdf/Pl_Flate.cc +++ b/libqpdf/Pl_Flate.cc @@ -6,6 +6,8 @@ #include #include +int Pl_Flate::compression_level = Z_DEFAULT_COMPRESSION; + Pl_Flate::Members::Members(size_t out_bufsize, action_e action) : out_bufsize(out_bufsize), @@ -120,7 +122,7 @@ Pl_Flate::handleData(unsigned char* data, size_t len, int flush) #endif if (this->m->action == a_deflate) { - err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION); + err = deflateInit(&zstream, compression_level); } else { @@ -235,6 +237,12 @@ Pl_Flate::finish() this->getNext()->finish(); } +void +Pl_Flate::setCompressionLevel(int level) +{ + compression_level = level; +} + void Pl_Flate::checkError(char const* prefix, int error_code) { diff --git a/zlib-flate/qtest/1.compressed-1 b/zlib-flate/qtest/1.compressed-1 new file mode 100644 index 0000000000000000000000000000000000000000..11150cf303b0b51084896f1816e2d7e3f0bc0644 GIT binary patch literal 193 zcmV;y06zbC0VR(ya>OtQMSGv(cWIoxMJ8pMy#OoWmDaM@2o#NPA8a=a%;&!kKQ%D= zTPg^aH-;r{jFSI2df%AwD`V&lxF_BbbF3fc`pS4q%kjz6)g`VwvkfA2++T`i0#@qA{(D-4OtQMSGv(cWIoxMJ8pMy#OoWmDaM@2o#NPA8a=a%;&!kKQ%D= zTPg^aH-;r{jFSI2df%AwD`V&lxF_BbbF3fc`pS4q%kjz6)g`VwvkfA2++T`i0#@qA{(D-4runtest("compress", - {$td->COMMAND => "zlib-flate -compress < 1.uncompressed"}, - {$td->FILE => "1.compressed", - $td->EXIT_STATUS => 0}); +foreach my $level ('', '=1', '=9') +{ + my $f = $level; + $f =~ s/=/-/; + $td->runtest("compress", + {$td->COMMAND => + "zlib-flate -compress$level < 1.uncompressed"}, + {$td->FILE => "1.compressed$f", + $td->EXIT_STATUS => 0}); -$td->runtest("uncompress", - {$td->COMMAND => "zlib-flate -uncompress < 1.compressed"}, - {$td->FILE => "1.uncompressed", - $td->EXIT_STATUS => 0}); + $td->runtest("uncompress", + {$td->COMMAND => "zlib-flate -uncompress < 1.compressed"}, + {$td->FILE => "1.uncompressed", + $td->EXIT_STATUS => 0}); +} $td->runtest("error", {$td->COMMAND => "zlib-flate -uncompress < 1.uncompressed"}, @@ -23,4 +29,4 @@ $td->runtest("error", $td->EXIT_STATUS => 2}, $td->NORMALIZE_NEWLINES); -$td->report(3); +$td->report(7); diff --git a/zlib-flate/zlib-flate.cc b/zlib-flate/zlib-flate.cc index d1c74d4d..c2613202 100644 --- a/zlib-flate/zlib-flate.cc +++ b/zlib-flate/zlib-flate.cc @@ -12,8 +12,14 @@ static char const* whoami = 0; void usage() { - std::cerr << "Usage: " << whoami << " { -uncompress | -compress }" - << std::endl; + std::cerr << "Usage: " << whoami << " { -uncompress | -compress[=n] }" + << std::endl + << "If n is specified with -compress, it is a" + << " zlib compression level from" << std::endl + << "1 to 9 where lower numbers are faster and" + << " less compressed and higher" << std::endl + << "numbers are slower and more compresed" + << std::endl; exit(2); } @@ -43,6 +49,7 @@ int main(int argc, char* argv[]) { usage(); } + // QXXXQ level Pl_Flate::action_e action = Pl_Flate::a_inflate; @@ -54,6 +61,12 @@ int main(int argc, char* argv[]) { action = Pl_Flate::a_deflate; } + else if ((strncmp(argv[1], "-compress=", 10) == 0)) + { + action = Pl_Flate::a_deflate; + int level = QUtil::string_to_int(argv[1] + 10); + Pl_Flate::setCompressionLevel(level); + } else { usage();