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