Skip to content

Commit

Permalink
Added ability to specify lossless WebP compression by specifying a qu…
Browse files Browse the repository at this point in the history
…ality factor of -1
  • Loading branch information
ruven committed Jan 16, 2024
1 parent 93c7213 commit 5be7927
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
16/01/2024:
- Added ability to specify lossless WebP compression by specifying a quality factor of -1


15/01/2024:
- Fix to IIIF size calculation for region requests that go beyond the boundaries of the image

Expand Down
4 changes: 2 additions & 2 deletions src/Environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ class Environment {
int quality;
if( envpara ){
quality = atoi( envpara );
if( quality > 100 ) quality = 900;
if( quality < 0 ) quality = 0;
if( quality > 100 ) quality = 100;
if( quality < -1 ) quality = -1; // Allow -1 = lossless
}
else quality = WEBP_QUALITY;

Expand Down
4 changes: 3 additions & 1 deletion src/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ int main( int argc, char *argv[] )
logfile << "Setting default PNG compression level to " << png_quality << endl;
#endif
#ifdef HAVE_WEBP
logfile << "Setting default WebP compression level to " << webp_quality << endl;
logfile << "Setting default WebP compression level to ";
if( webp_quality == -1 ) logfile << "lossless" << endl;
else logfile << webp_quality << endl;
#endif
logfile << "Setting maximum CVT size to " << max_CVT << endl;
logfile << "Setting HTTP Cache-Control header to '" << cache_control << "'" << endl;
Expand Down
26 changes: 20 additions & 6 deletions src/WebPCompressor.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* WebP Compressor Class:
Handles alpha channels, ICC profiles and XMP metadata
Copyright (C) 2022-2023 Ruven Pillay
Copyright (C) 2022-2024 Ruven Pillay
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -65,7 +65,15 @@ class WebPCompressor : public Compressor {
WebPConfigInit( &config );
config.method = 0; // Fastest encoding
config.thread_level = 1; // Enable threading
config.quality = this->Q; // WebP's quality range is 0-100

// Update our WebP config structure depending on whether lossless or lossy compression requested
if( compressionLevel == -1 ){
// -1 indicates lossless
config.lossless = 1;
config.quality = 0; // Zero means fastest for lossless
}
// WebP's lossy quality range is 0-100
else config.quality = this->Q;

// Create our muxer object
mux = WebPMuxNew();
Expand Down Expand Up @@ -138,12 +146,18 @@ class WebPCompressor : public Compressor {
// WebP quality ranges from 0 (best compression - smallest size) to 100 (worst compression - largest size)
this->Q = quality;

// WebP compression level
if( Q < 0 ) Q = 0;
// Limit to WebP quality range - negative values indicate lossless
if( this->Q < 1 ) Q = -1;
else if( Q > 100 ) Q = 100;

// Update our WebP config structure
config.quality = Q;
// Update our WebP config structure depending on whether lossless or lossy compression requested
if( this->Q == -1 ){
// -1 indicates lossless
config.lossless = 1;
config.quality = 0; // Zero means fastest for lossless
}
// WebP's lossy quality range is 0-100
else config.quality = this->Q;
}


Expand Down

0 comments on commit 5be7927

Please sign in to comment.