2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 02:49:00 +00:00

Add ability to set zlib compression level globally

This commit is contained in:
Jay Berkenbilt 2019-08-23 19:54:08 -04:00
parent bda5d26894
commit dac0598b94
7 changed files with 54 additions and 12 deletions

View File

@ -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

View File

@ -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);

View File

@ -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
{
@ -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)
{

Binary file not shown.

Binary file not shown.

View File

@ -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);

View File

@ -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();