From 7e4ae9d0cee10a3415ccb084cc9bae3f524f3e36 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Tue, 30 Mar 2021 21:54:15 +0600 Subject: [PATCH] Fix PNG quantization with vips 8.10+ --- vips.c | 22 ++++++++++++++++++++-- vips.go | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/vips.c b/vips.c index d58aa013..e8205305 100644 --- a/vips.c +++ b/vips.c @@ -1,5 +1,6 @@ #include "vips.h" #include +#include #define VIPS_SUPPORT_SMARTCROP \ (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5)) @@ -43,6 +44,9 @@ #define VIPS_SUPPORT_FIND_TRIM \ (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 6)) +#define VIPS_SUPPORT_PNG_BITDEPTH \ + (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10)) + #define EXIF_ORIENTATION "exif-ifd0-Orientation" #if (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8)) @@ -642,15 +646,29 @@ vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interl int vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) { + if (!quantize) + return vips_pngsave_buffer( + in, buf, len, + "filter", VIPS_FOREIGN_PNG_FILTER_NONE, + "interlace", interlace, + NULL + ); + + int bitdepth = ceil(log2(colors)); + return vips_pngsave_buffer( in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, -#if VIPS_SUPPORT_PNG_QUANTIZATION +#if VIPS_SUPPORT_PNG_BITDEPTH + "palette", quantize, + "bitdepth", bitdepth, +#elif VIPS_SUPPORT_PNG_QUANTIZATION // VIPS_SUPPORT_PNG_BITDEPTH "palette", quantize, "colours", colors, #endif // VIPS_SUPPORT_PNG_QUANTIZATION - NULL); + NULL + ); } int diff --git a/vips.go b/vips.go index 5ab1cd74..b16faaa2 100644 --- a/vips.go +++ b/vips.go @@ -2,7 +2,7 @@ package main /* #cgo pkg-config: vips -#cgo LDFLAGS: -s -w +#cgo LDFLAGS: -s -w -lm #cgo CFLAGS: -O3 #include "vips.h" */