Commit dac0598b94c877bec92a1edd78ae00021cfa1638
1 parent
bda5d268
Add ability to set zlib compression level globally
Showing
7 changed files
with
54 additions
and
12 deletions
ChangeLog
| 1 | +2019-08-23 Jay Berkenbilt <ejb@ql.org> | ||
| 2 | + | ||
| 3 | + * Add option Pl_Flate::setCompressionLevel to globally set the | ||
| 4 | + zlib compression level used by all Pl_Flate pipelines. | ||
| 5 | + | ||
| 1 | 2019-08-22 Jay Berkenbilt <ejb@ql.org> | 6 | 2019-08-22 Jay Berkenbilt <ejb@ql.org> |
| 2 | 7 | ||
| 3 | * In QPDFObjectHandle::ParserCallbacks, in addition to | 8 | * In QPDFObjectHandle::ParserCallbacks, in addition to |
include/qpdf/Pl_Flate.hh
| @@ -28,6 +28,7 @@ class Pl_Flate: public Pipeline | @@ -28,6 +28,7 @@ class Pl_Flate: public Pipeline | ||
| 28 | { | 28 | { |
| 29 | public: | 29 | public: |
| 30 | static unsigned int const def_bufsize = 65536; | 30 | static unsigned int const def_bufsize = 65536; |
| 31 | + static int compression_level; | ||
| 31 | 32 | ||
| 32 | enum action_e { a_inflate, a_deflate }; | 33 | enum action_e { a_inflate, a_deflate }; |
| 33 | 34 | ||
| @@ -42,6 +43,15 @@ class Pl_Flate: public Pipeline | @@ -42,6 +43,15 @@ class Pl_Flate: public Pipeline | ||
| 42 | QPDF_DLL | 43 | QPDF_DLL |
| 43 | virtual void finish(); | 44 | virtual void finish(); |
| 44 | 45 | ||
| 46 | + // Globally set compression level from 1 (fastest, least | ||
| 47 | + // compression) to 9 (slowest, most compression). Use -1 to set | ||
| 48 | + // the default compression level. This is passed directly to zlib. | ||
| 49 | + // This method returns a pointer to the current Pl_Flate object so | ||
| 50 | + // you can create a pipeline with | ||
| 51 | + // Pl_Flate(...)->setCompressionLevel(...) | ||
| 52 | + QPDF_DLL | ||
| 53 | + static void setCompressionLevel(int); | ||
| 54 | + | ||
| 45 | private: | 55 | private: |
| 46 | void handleData(unsigned char* data, size_t len, int flush); | 56 | void handleData(unsigned char* data, size_t len, int flush); |
| 47 | void checkError(char const* prefix, int error_code); | 57 | void checkError(char const* prefix, int error_code); |
libqpdf/Pl_Flate.cc
| @@ -6,6 +6,8 @@ | @@ -6,6 +6,8 @@ | ||
| 6 | #include <qpdf/QUtil.hh> | 6 | #include <qpdf/QUtil.hh> |
| 7 | #include <qpdf/QIntC.hh> | 7 | #include <qpdf/QIntC.hh> |
| 8 | 8 | ||
| 9 | +int Pl_Flate::compression_level = Z_DEFAULT_COMPRESSION; | ||
| 10 | + | ||
| 9 | Pl_Flate::Members::Members(size_t out_bufsize, | 11 | Pl_Flate::Members::Members(size_t out_bufsize, |
| 10 | action_e action) : | 12 | action_e action) : |
| 11 | out_bufsize(out_bufsize), | 13 | out_bufsize(out_bufsize), |
| @@ -120,7 +122,7 @@ Pl_Flate::handleData(unsigned char* data, size_t len, int flush) | @@ -120,7 +122,7 @@ Pl_Flate::handleData(unsigned char* data, size_t len, int flush) | ||
| 120 | #endif | 122 | #endif |
| 121 | if (this->m->action == a_deflate) | 123 | if (this->m->action == a_deflate) |
| 122 | { | 124 | { |
| 123 | - err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION); | 125 | + err = deflateInit(&zstream, compression_level); |
| 124 | } | 126 | } |
| 125 | else | 127 | else |
| 126 | { | 128 | { |
| @@ -236,6 +238,12 @@ Pl_Flate::finish() | @@ -236,6 +238,12 @@ Pl_Flate::finish() | ||
| 236 | } | 238 | } |
| 237 | 239 | ||
| 238 | void | 240 | void |
| 241 | +Pl_Flate::setCompressionLevel(int level) | ||
| 242 | +{ | ||
| 243 | + compression_level = level; | ||
| 244 | +} | ||
| 245 | + | ||
| 246 | +void | ||
| 239 | Pl_Flate::checkError(char const* prefix, int error_code) | 247 | Pl_Flate::checkError(char const* prefix, int error_code) |
| 240 | { | 248 | { |
| 241 | z_stream& zstream = *(static_cast<z_stream*>(this->m->zdata)); | 249 | z_stream& zstream = *(static_cast<z_stream*>(this->m->zdata)); |
zlib-flate/qtest/1.compressed-1
0 → 100644
No preview for this file type
zlib-flate/qtest/1.compressed-9
0 → 100644
No preview for this file type
zlib-flate/qtest/zf.test
| @@ -7,15 +7,21 @@ require TestDriver; | @@ -7,15 +7,21 @@ require TestDriver; | ||
| 7 | 7 | ||
| 8 | my $td = new TestDriver('zlib-flate'); | 8 | my $td = new TestDriver('zlib-flate'); |
| 9 | 9 | ||
| 10 | -$td->runtest("compress", | ||
| 11 | - {$td->COMMAND => "zlib-flate -compress < 1.uncompressed"}, | ||
| 12 | - {$td->FILE => "1.compressed", | ||
| 13 | - $td->EXIT_STATUS => 0}); | 10 | +foreach my $level ('', '=1', '=9') |
| 11 | +{ | ||
| 12 | + my $f = $level; | ||
| 13 | + $f =~ s/=/-/; | ||
| 14 | + $td->runtest("compress", | ||
| 15 | + {$td->COMMAND => | ||
| 16 | + "zlib-flate -compress$level < 1.uncompressed"}, | ||
| 17 | + {$td->FILE => "1.compressed$f", | ||
| 18 | + $td->EXIT_STATUS => 0}); | ||
| 14 | 19 | ||
| 15 | -$td->runtest("uncompress", | ||
| 16 | - {$td->COMMAND => "zlib-flate -uncompress < 1.compressed"}, | ||
| 17 | - {$td->FILE => "1.uncompressed", | ||
| 18 | - $td->EXIT_STATUS => 0}); | 20 | + $td->runtest("uncompress", |
| 21 | + {$td->COMMAND => "zlib-flate -uncompress < 1.compressed"}, | ||
| 22 | + {$td->FILE => "1.uncompressed", | ||
| 23 | + $td->EXIT_STATUS => 0}); | ||
| 24 | +} | ||
| 19 | 25 | ||
| 20 | $td->runtest("error", | 26 | $td->runtest("error", |
| 21 | {$td->COMMAND => "zlib-flate -uncompress < 1.uncompressed"}, | 27 | {$td->COMMAND => "zlib-flate -uncompress < 1.uncompressed"}, |
| @@ -23,4 +29,4 @@ $td->runtest("error", | @@ -23,4 +29,4 @@ $td->runtest("error", | ||
| 23 | $td->EXIT_STATUS => 2}, | 29 | $td->EXIT_STATUS => 2}, |
| 24 | $td->NORMALIZE_NEWLINES); | 30 | $td->NORMALIZE_NEWLINES); |
| 25 | 31 | ||
| 26 | -$td->report(3); | 32 | +$td->report(7); |
zlib-flate/zlib-flate.cc
| @@ -12,8 +12,14 @@ static char const* whoami = 0; | @@ -12,8 +12,14 @@ static char const* whoami = 0; | ||
| 12 | 12 | ||
| 13 | void usage() | 13 | void usage() |
| 14 | { | 14 | { |
| 15 | - std::cerr << "Usage: " << whoami << " { -uncompress | -compress }" | ||
| 16 | - << std::endl; | 15 | + std::cerr << "Usage: " << whoami << " { -uncompress | -compress[=n] }" |
| 16 | + << std::endl | ||
| 17 | + << "If n is specified with -compress, it is a" | ||
| 18 | + << " zlib compression level from" << std::endl | ||
| 19 | + << "1 to 9 where lower numbers are faster and" | ||
| 20 | + << " less compressed and higher" << std::endl | ||
| 21 | + << "numbers are slower and more compresed" | ||
| 22 | + << std::endl; | ||
| 17 | exit(2); | 23 | exit(2); |
| 18 | } | 24 | } |
| 19 | 25 | ||
| @@ -43,6 +49,7 @@ int main(int argc, char* argv[]) | @@ -43,6 +49,7 @@ int main(int argc, char* argv[]) | ||
| 43 | { | 49 | { |
| 44 | usage(); | 50 | usage(); |
| 45 | } | 51 | } |
| 52 | + // QXXXQ level | ||
| 46 | 53 | ||
| 47 | Pl_Flate::action_e action = Pl_Flate::a_inflate; | 54 | Pl_Flate::action_e action = Pl_Flate::a_inflate; |
| 48 | 55 | ||
| @@ -54,6 +61,12 @@ int main(int argc, char* argv[]) | @@ -54,6 +61,12 @@ int main(int argc, char* argv[]) | ||
| 54 | { | 61 | { |
| 55 | action = Pl_Flate::a_deflate; | 62 | action = Pl_Flate::a_deflate; |
| 56 | } | 63 | } |
| 64 | + else if ((strncmp(argv[1], "-compress=", 10) == 0)) | ||
| 65 | + { | ||
| 66 | + action = Pl_Flate::a_deflate; | ||
| 67 | + int level = QUtil::string_to_int(argv[1] + 10); | ||
| 68 | + Pl_Flate::setCompressionLevel(level); | ||
| 69 | + } | ||
| 57 | else | 70 | else |
| 58 | { | 71 | { |
| 59 | usage(); | 72 | usage(); |