aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-08-24 01:54:08 +0200
committerJay Berkenbilt <ejb@ql.org>2019-08-24 02:34:21 +0200
commitdac0598b94c877bec92a1edd78ae00021cfa1638 (patch)
tree70988ddf9cc7c4e9c2e7b867bb135c331e061217
parentbda5d26894c857a05f811a6ebd3595d26424d2ea (diff)
downloadqpdf-dac0598b94c877bec92a1edd78ae00021cfa1638.tar.zst
Add ability to set zlib compression level globally
-rw-r--r--ChangeLog5
-rw-r--r--include/qpdf/Pl_Flate.hh10
-rw-r--r--libqpdf/Pl_Flate.cc10
-rw-r--r--zlib-flate/qtest/1.compressed-1bin0 -> 193 bytes
-rw-r--r--zlib-flate/qtest/1.compressed-9bin0 -> 193 bytes
-rw-r--r--zlib-flate/qtest/zf.test24
-rw-r--r--zlib-flate/zlib-flate.cc17
7 files changed, 54 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index af06bf3f..cdfa3fce 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2019-08-23 Jay Berkenbilt <ejb@ql.org>
+
+ * Add option Pl_Flate::setCompressionLevel to globally set the
+ zlib compression level used by all Pl_Flate pipelines.
+
2019-08-22 Jay Berkenbilt <ejb@ql.org>
* 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 <qpdf/QUtil.hh>
#include <qpdf/QIntC.hh>
+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
{
@@ -236,6 +238,12 @@ Pl_Flate::finish()
}
void
+Pl_Flate::setCompressionLevel(int level)
+{
+ compression_level = level;
+}
+
+void
Pl_Flate::checkError(char const* prefix, int error_code)
{
z_stream& zstream = *(static_cast<z_stream*>(this->m->zdata));
diff --git a/zlib-flate/qtest/1.compressed-1 b/zlib-flate/qtest/1.compressed-1
new file mode 100644
index 00000000..11150cf3
--- /dev/null
+++ b/zlib-flate/qtest/1.compressed-1
Binary files differ
diff --git a/zlib-flate/qtest/1.compressed-9 b/zlib-flate/qtest/1.compressed-9
new file mode 100644
index 00000000..25f4647b
--- /dev/null
+++ b/zlib-flate/qtest/1.compressed-9
Binary files differ
diff --git a/zlib-flate/qtest/zf.test b/zlib-flate/qtest/zf.test
index 39271e16..2b188eb2 100644
--- a/zlib-flate/qtest/zf.test
+++ b/zlib-flate/qtest/zf.test
@@ -7,15 +7,21 @@ require TestDriver;
my $td = new TestDriver('zlib-flate');
-$td->runtest("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();