1 // Copyright 2013 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Author: [email protected] (Lode Vandevenne) 16 // Author: [email protected] (Jyrki Alakuijala) 17 18 // Library to recompress and optimize PNG images. Uses Zopfli as the compression 19 // backend, chooses optimal PNG color model, and tries out several PNG filter 20 // strategies. 21 22 #ifndef ZOPFLIPNG_LIB_H_ 23 #define ZOPFLIPNG_LIB_H_ 24 25 #ifdef __cplusplus 26 27 #include <string> 28 #include <vector> 29 30 extern "C" { 31 32 #endif 33 34 #include <stdlib.h> 35 36 enum ZopfliPNGFilterStrategy { 37 kStrategyZero = 0, 38 kStrategyOne = 1, 39 kStrategyTwo = 2, 40 kStrategyThree = 3, 41 kStrategyFour = 4, 42 kStrategyMinSum, 43 kStrategyEntropy, 44 kStrategyPredefined, 45 kStrategyBruteForce, 46 kNumFilterStrategies /* Not a strategy but used for the size of this enum */ 47 }; 48 49 typedef struct CZopfliPNGOptions { 50 int lossy_transparent; 51 int lossy_8bit; 52 53 enum ZopfliPNGFilterStrategy* filter_strategies; 54 // How many strategies to try. 55 int num_filter_strategies; 56 57 int auto_filter_strategy; 58 59 char** keepchunks; 60 // How many entries in keepchunks. 61 int num_keepchunks; 62 63 int use_zopfli; 64 65 int num_iterations; 66 67 int num_iterations_large; 68 69 int block_split_strategy; 70 } CZopfliPNGOptions; 71 72 // Sets the default options 73 // Does not allocate or set keepchunks or filter_strategies 74 void CZopfliPNGSetDefaults(CZopfliPNGOptions *png_options); 75 76 // Returns 0 on success, error code otherwise 77 // The caller must free resultpng after use 78 int CZopfliPNGOptimize(const unsigned char* origpng, 79 const size_t origpng_size, 80 const CZopfliPNGOptions* png_options, 81 int verbose, 82 unsigned char** resultpng, 83 size_t* resultpng_size); 84 85 #ifdef __cplusplus 86 } // extern "C" 87 #endif 88 89 // C++ API 90 #ifdef __cplusplus 91 92 struct ZopfliPNGOptions { 93 ZopfliPNGOptions(); 94 95 bool verbose; 96 97 // Allow altering hidden colors of fully transparent pixels 98 bool lossy_transparent; 99 // Convert 16-bit per channel images to 8-bit per channel 100 bool lossy_8bit; 101 102 // Filter strategies to try 103 std::vector<ZopfliPNGFilterStrategy> filter_strategies; 104 105 // Automatically choose filter strategy using less good compression 106 bool auto_filter_strategy; 107 108 // Keep original color type (RGB, RGBA, gray, gray+alpha or palette) and bit 109 // depth of the PNG. 110 // This results in a loss of compression opportunities, e.g. it will no 111 // longer convert a 4-channel RGBA image to 2-channel gray+alpha if the image 112 // only had translucent gray pixels. 113 // May be useful if a device does not support decoding PNGs of a particular 114 // color type. 115 // Default value: false. 116 bool keep_colortype; 117 118 // PNG chunks to keep 119 // chunks to literally copy over from the original PNG to the resulting one 120 std::vector<std::string> keepchunks; 121 122 // Use Zopfli deflate compression 123 bool use_zopfli; 124 125 // Zopfli number of iterations 126 int num_iterations; 127 128 // Zopfli number of iterations on large images 129 int num_iterations_large; 130 131 // Unused, left for backwards compatiblity. 132 int block_split_strategy; 133 }; 134 135 // Returns 0 on success, error code otherwise. 136 // If verbose is true, it will print some info while working. 137 int ZopfliPNGOptimize(const std::vector<unsigned char>& origpng, 138 const ZopfliPNGOptions& png_options, 139 bool verbose, 140 std::vector<unsigned char>* resultpng); 141 142 #endif // __cplusplus 143 144 #endif // ZOPFLIPNG_LIB_H_ 145