lvandeve / lodepng

PNG encoder and decoder in C and C++.
zlib License
2.06k stars 421 forks source link

How To Change Deflate (not really an issue) #53

Closed MrJohnBarker closed 7 years ago

MrJohnBarker commented 7 years ago

Hi Lode, It's probably out of the scope noob question and maybe not an real issue about the lodepng itself. I'm trying to add This library as deflate compressor in ZopfliPNG instead of Zopfli. I don't even know if I'm doing this in the right way:

   unsigned CustomPNGDeflate(unsigned char** out, size_t* outsize,
   const unsigned char* in, size_t insize,
   const LodePNGCompressSettings* settings) {

   int compression_level = 12;
   struct libdeflate_compressor* compressor;
   compressor = libdeflate_alloc_compressor(compression_level);
   libdeflate_deflate_compress(compressor, in, insize, out, *outsize);
   libdeflate_free_compressor(compressor);
   return 0;
}

This compile well, but ZopfliPNG is corrupting all images (full black pixels) probably because it's not used as it should be. Any help will be appreciated !

fhanau commented 7 years ago

This should work:

 unsigned CustomPNGDeflate(unsigned char** out, size_t* outsize,
   const unsigned char* in, size_t insize,
   const LodePNGCompressSettings* settings) {

  int compression_level = 12;
  struct libdeflate_compressor* compressor;
  compressor = libdeflate_alloc_compressor(compression_level);
  size_t allocsize = libdeflate_deflate_compress_bound(compressor, insize);
  (*out) = (unsigned char*)malloc(allocsize);
  (*outsize) = libdeflate_deflate_compress(compressor, in, insize, *out, allocsize);
  libdeflate_free_compressor(compressor);
  return 0;
}

By the way, if you're interested in achieving better/faster image compression than with lodepng, you should check out my image optimizer fhanau/Efficient-Compression-Tool

MrJohnBarker commented 7 years ago

Thank you very much @fhanau I was happy to add your code to ZopfliPNG but finally I tried your fhanau/Efficient-Compression-Tool first. That's exactly what I was looking for actually, it's faster+better than ZopfliPNG. Very impressive!