diff options
author | Lode <lvandeve@gmail.com> | 2020-03-24 14:15:17 +0100 |
---|---|---|
committer | Lode <lvandeve@gmail.com> | 2020-03-24 14:15:17 +0100 |
commit | 7113f4e96bd26df27c46d590df95e517b966f10d (patch) | |
tree | 7b65d6c3b4e56dd4977e17216b2b63cb79055848 | |
parent | 00e21c32d17a380e2da4bd75e6847b0d9ae8d91c (diff) | |
download | zopfli-7113f4e96bd26df27c46d590df95e517b966f10d.tar.gz |
add optional flag to keep original colortype
-rw-r--r-- | src/zopflipng/zopflipng_bin.cc | 9 | ||||
-rw-r--r-- | src/zopflipng/zopflipng_lib.cc | 13 | ||||
-rw-r--r-- | src/zopflipng/zopflipng_lib.h | 10 |
3 files changed, 27 insertions, 5 deletions
diff --git a/src/zopflipng/zopflipng_bin.cc b/src/zopflipng/zopflipng_bin.cc index fc2ddfa..ea764b9 100644 --- a/src/zopflipng/zopflipng_bin.cc +++ b/src/zopflipng/zopflipng_bin.cc @@ -125,6 +125,13 @@ void ShowHelp() { " web images because web browsers do not use these chunks. By default" " ZopfliPNG only keeps (and losslessly modifies) the following chunks" " because they are essential: IHDR, PLTE, tRNS, IDAT and IEND.\n" + "--keepcolortype: Keep original color type (RGB, RGBA, gray," + " gray+alpha or palette) and bit depth of the PNG.\n" + " This results in a loss of compression opportunities, e.g. it will no" + " longer convert a 4-channel RGBA image to 2-channel gray+alpha if the" + " image only had translucent gray pixels.\n" + " May be useful if a device does not support decoding PNGs of a" + " particular color type.\n" "\n" "Usage examples:\n" "Optimize a file and overwrite if smaller: zopflipng infile.png" @@ -239,6 +246,8 @@ int main(int argc, char *argv[]) { " --keepchunks=gAMA,cHRM,sRGB,iCCP\n"); return 0; } + } else if (name == "--keepcolortype") { + png_options.keep_colortype = true; } else if (name == "--prefix") { use_prefix = true; if (!value.empty()) prefix = value; diff --git a/src/zopflipng/zopflipng_lib.cc b/src/zopflipng/zopflipng_lib.cc index bff53a0..15188db 100644 --- a/src/zopflipng/zopflipng_lib.cc +++ b/src/zopflipng/zopflipng_lib.cc @@ -35,6 +35,7 @@ ZopfliPNGOptions::ZopfliPNGOptions() , lossy_transparent(false) , lossy_8bit(false) , auto_filter_strategy(true) + , keep_colortype(false) , use_zopfli(true) , num_iterations(15) , num_iterations_large(5) @@ -379,7 +380,7 @@ int ZopfliPNGOptimize(const std::vector<unsigned char>& origpng, lodepng::State inputstate; error = lodepng::decode(image, w, h, inputstate, origpng); - bool keep_colortype = false; + bool keep_colortype = png_options.keep_colortype; if (!png_options.keepchunks.empty()) { // If the user wants to keep the non-essential chunks bKGD or sBIT, the @@ -391,10 +392,12 @@ int ZopfliPNGOptimize(const std::vector<unsigned char>& origpng, // possible. std::set<std::string> keepchunks; ChunksToKeep(origpng, png_options.keepchunks, &keepchunks); - keep_colortype = keepchunks.count("bKGD") || keepchunks.count("sBIT"); - if (keep_colortype && verbose) { - printf("Forced to keep original color type due to keeping bKGD or sBIT" - " chunk.\n"); + if (keepchunks.count("bKGD") || keepchunks.count("sBIT")) { + if (!keep_colortype && verbose) { + printf("Forced to keep original color type due to keeping bKGD or sBIT" + " chunk.\n"); + } + keep_colortype = true; } } diff --git a/src/zopflipng/zopflipng_lib.h b/src/zopflipng/zopflipng_lib.h index 7fdeeca..b2bcd12 100644 --- a/src/zopflipng/zopflipng_lib.h +++ b/src/zopflipng/zopflipng_lib.h @@ -105,6 +105,16 @@ struct ZopfliPNGOptions { // Automatically choose filter strategy using less good compression bool auto_filter_strategy; + // Keep original color type (RGB, RGBA, gray, gray+alpha or palette) and bit + // depth of the PNG. + // This results in a loss of compression opportunities, e.g. it will no + // longer convert a 4-channel RGBA image to 2-channel gray+alpha if the image + // only had translucent gray pixels. + // May be useful if a device does not support decoding PNGs of a particular + // color type. + // Default value: false. + bool keep_colortype; + // PNG chunks to keep // chunks to literally copy over from the original PNG to the resulting one std::vector<std::string> keepchunks; |