From 157843ccb32ac4c8d365e57c2081bdde278a2b15 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Tue, 23 May 2023 19:08:35 +0300 Subject: [PATCH] Use VIPS_META_BITS_PER_SAMPLE image header instead of palette-bit-depth when available --- processing/watermark.go | 2 +- vips/vips.c | 25 +++++++++++++++++-------- vips/vips.go | 4 ++-- vips/vips.h | 2 ++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/processing/watermark.go b/processing/watermark.go index db5ba841..23845a53 100644 --- a/processing/watermark.go +++ b/processing/watermark.go @@ -69,7 +69,7 @@ func prepareWatermark(wm *vips.Image, wmData *imagedata.ImageData, opts *options } } - wm.RemoveHeader("palette-bit-depth") + wm.RemoveBitsPerSampleHeader() return nil } diff --git a/vips/vips.c b/vips/vips.c index 4178ff29..e6cce86a 100644 --- a/vips/vips.c +++ b/vips/vips.c @@ -15,6 +15,10 @@ #define VIPS_GIF_RESOLUTION_LIMITED \ (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION <= 12) +#ifndef VIPS_META_BITS_PER_SAMPLE +#define VIPS_META_BITS_PER_SAMPLE "palette-bit-depth" +#endif + int vips_initialize() { return vips_init("imgproxy"); @@ -134,17 +138,22 @@ vips_get_orientation(VipsImage *image) { } int -vips_get_palette_bit_depth(VipsImage *image) { - int palette_bit_depth; +vips_get_bits_per_sample(VipsImage *image) { + int bits_per_sample; if ( - vips_image_get_typeof(image, "palette-bit-depth") == G_TYPE_INT && - vips_image_get_int(image, "palette-bit-depth", &palette_bit_depth) == 0 - ) return palette_bit_depth; + vips_image_get_typeof(image, VIPS_META_BITS_PER_SAMPLE) == G_TYPE_INT && + vips_image_get_int(image, VIPS_META_BITS_PER_SAMPLE, &bits_per_sample) == 0 + ) return bits_per_sample; return 0; } +void +vips_remove_bits_per_sample(VipsImage *image) { + vips_image_remove(image, VIPS_META_BITS_PER_SAMPLE); +} + VipsBandFormat vips_band_format(VipsImage *in) { return in->BandFmt; @@ -610,7 +619,7 @@ vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright) { if ( (strcmp(name, VIPS_META_ICC_NAME) == 0) || - (strcmp(name, "palette-bit-depth") == 0) || + (strcmp(name, VIPS_META_BITS_PER_SAMPLE) == 0) || (strcmp(name, "width") == 0) || (strcmp(name, "height") == 0) || (strcmp(name, "bands") == 0) || @@ -664,8 +673,8 @@ vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quant else if (colors > 4) bitdepth = 4; else if (colors > 2) bitdepth = 2; } else { - bitdepth = vips_get_palette_bit_depth(in); - if (bitdepth) { + bitdepth = vips_get_bits_per_sample(in); + if (bitdepth && bitdepth <= 8) { if (bitdepth > 4) bitdepth = 8; else if (bitdepth > 2) bitdepth = 4; quantize = 1; diff --git a/vips/vips.go b/vips/vips.go index 2dee4b68..954e0abc 100644 --- a/vips/vips.go +++ b/vips/vips.go @@ -482,8 +482,8 @@ func (img *Image) SetBlob(name string, value []byte) { C.vips_image_set_blob_copy(img.VipsImage, cachedCString(name), unsafe.Pointer(&value[0]), C.size_t(len(value))) } -func (img *Image) RemoveHeader(name string) { - C.vips_image_remove(img.VipsImage, cachedCString(name)) +func (img *Image) RemoveBitsPerSampleHeader() { + C.vips_remove_bits_per_sample(img.VipsImage) } func (img *Image) CastUchar() error { diff --git a/vips/vips.h b/vips/vips.h index 7d84539a..c4071442 100644 --- a/vips/vips.h +++ b/vips/vips.h @@ -28,6 +28,8 @@ void vips_strip_meta(VipsImage *image); VipsBandFormat vips_band_format(VipsImage *in); +void vips_remove_bits_per_sample(VipsImage * image); + gboolean vips_is_animated(VipsImage * in); int vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n);