Skip to content

Commit 9984c53

Browse files
authored
Default to using gifsave for libvips >= 8.12.0 (#269)
* Use gifsave instead of magicksave for saving GIF files * Add optional CGIF arguments and add version detection around gifsave call
1 parent ae9c84e commit 9984c53

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

vips/foreign.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@ int set_magicksave_options(VipsOperation *operation, SaveParams *params) {
327327
return ret;
328328
}
329329

330+
// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-gifsave-buffer
331+
int set_gifsave_options(VipsOperation *operation, SaveParams *params) {
332+
int ret = 0;
333+
// See for argument values: https://www.libvips.org/API/current/VipsForeignSave.html#vips-gifsave
334+
if (params->gifDither > 0.0 && params->gifDither <= 1.0) {
335+
ret = vips_object_set(VIPS_OBJECT(operation), "dither", params->gifDither, NULL);
336+
}
337+
if (params->gifEffort >= 1 && params->gifEffort <= 10) {
338+
ret = vips_object_set(VIPS_OBJECT(operation), "effort", params->gifEffort, NULL);
339+
}
340+
if (params->gifBitdepth >= 1 && params->gifBitdepth <= 8) {
341+
ret = vips_object_set(VIPS_OBJECT(operation), "bitdepth", params->gifBitdepth, NULL);
342+
}
343+
return ret;
344+
}
345+
330346
int set_avifsave_options(VipsOperation *operation, SaveParams *params) {
331347
int ret = vips_object_set(
332348
VIPS_OBJECT(operation), "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
@@ -406,7 +422,11 @@ int save_to_buffer(SaveParams *params) {
406422
case TIFF:
407423
return save_buffer("tiffsave_buffer", params, set_tiffsave_options);
408424
case GIF:
425+
#if (VIPS_MAJOR_VERSION >= 8) && (VIPS_MINOR_VERSION >= 12)
426+
return save_buffer("gifsave_buffer", params, set_gifsave_options);
427+
#else
409428
return save_buffer("magicksave_buffer", params, set_magicksave_options);
429+
#endif
410430
case AVIF:
411431
return save_buffer("heifsave_buffer", params, set_avifsave_options);
412432
case JP2K:
@@ -460,6 +480,10 @@ static SaveParams defaultSaveParams = {
460480
.pngDither = 0,
461481
.pngFilter = VIPS_FOREIGN_PNG_FILTER_NONE,
462482

483+
.gifDither = 0.0,
484+
.gifEffort = 0,
485+
.gifBitdepth = 0,
486+
463487
.webpLossless = FALSE,
464488
.webpNearLossless = FALSE,
465489
.webpReductionEffort = 4,

vips/foreign.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ func vipsSaveGIFToBuffer(in *C.VipsImage, params GifExportParams) ([]byte, error
428428
p := C.create_save_params(C.GIF)
429429
p.inputImage = in
430430
p.quality = C.int(params.Quality)
431+
p.gifDither = C.double(params.Dither)
432+
p.gifEffort = C.int(params.Effort)
433+
p.gifBitdepth = C.int(params.Bitdepth)
431434

432435
return vipsSaveToBuffer(p)
433436
}

vips/foreign.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ typedef struct SaveParams {
9696
double pngDither;
9797
int pngBitdepth;
9898

99+
// GIF (with CGIF)
100+
double gifDither;
101+
int gifEffort;
102+
int gifBitdepth;
103+
99104
// WEBP
100105
BOOL webpLossless;
101106
BOOL webpNearLossless;

vips/image.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,17 @@ func NewTiffExportParams() *TiffExportParams {
321321
type GifExportParams struct {
322322
StripMetadata bool
323323
Quality int
324+
Dither float64
325+
Effort int
326+
Bitdepth int
324327
}
325328

326329
// NewGifExportParams creates default values for an export of a GIF image.
327330
func NewGifExportParams() *GifExportParams {
328331
return &GifExportParams{
329-
Quality: 75,
332+
Quality: 75,
333+
Effort: 7,
334+
Bitdepth: 8,
330335
}
331336
}
332337

0 commit comments

Comments
 (0)