Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to using gifsave for libvips >= 8.12.0 #269

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions vips/foreign.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ int set_magicksave_options(VipsOperation *operation, SaveParams *params) {
return ret;
}

// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-gifsave-buffer
int set_gifsave_options(VipsOperation *operation, SaveParams *params) {
int ret = 0;
// See for argument values: https://www.libvips.org/API/current/VipsForeignSave.html#vips-gifsave
if (params->gifDither > 0.0 && params->gifDither <= 1.0) {
ret = vips_object_set(VIPS_OBJECT(operation), "dither", params->gifDither, NULL);
}
if (params->gifEffort >= 1 && params->gifEffort <= 10) {
ret = vips_object_set(VIPS_OBJECT(operation), "effort", params->gifEffort, NULL);
}
if (params->gifBitdepth >= 1 && params->gifBitdepth <= 8) {
ret = vips_object_set(VIPS_OBJECT(operation), "bitdepth", params->gifBitdepth, NULL);
}
return ret;
}

int set_avifsave_options(VipsOperation *operation, SaveParams *params) {
int ret = vips_object_set(
VIPS_OBJECT(operation), "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
Expand Down Expand Up @@ -406,7 +422,11 @@ int save_to_buffer(SaveParams *params) {
case TIFF:
return save_buffer("tiffsave_buffer", params, set_tiffsave_options);
case GIF:
#if (VIPS_MAJOR_VERSION >= 8) && (VIPS_MINOR_VERSION >= 12)
return save_buffer("gifsave_buffer", params, set_gifsave_options);
#else
return save_buffer("magicksave_buffer", params, set_magicksave_options);
#endif
case AVIF:
return save_buffer("heifsave_buffer", params, set_avifsave_options);
case JP2K:
Expand Down Expand Up @@ -460,6 +480,10 @@ static SaveParams defaultSaveParams = {
.pngDither = 0,
.pngFilter = VIPS_FOREIGN_PNG_FILTER_NONE,

.gifDither = 0.0,
.gifEffort = 0,
.gifBitdepth = 0,

.webpLossless = FALSE,
.webpNearLossless = FALSE,
.webpReductionEffort = 4,
Expand Down
3 changes: 3 additions & 0 deletions vips/foreign.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ func vipsSaveGIFToBuffer(in *C.VipsImage, params GifExportParams) ([]byte, error
p := C.create_save_params(C.GIF)
p.inputImage = in
p.quality = C.int(params.Quality)
p.gifDither = C.double(params.Dither)
p.gifEffort = C.int(params.Effort)
p.gifBitdepth = C.int(params.Bitdepth)

return vipsSaveToBuffer(p)
}
Expand Down
5 changes: 5 additions & 0 deletions vips/foreign.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ typedef struct SaveParams {
double pngDither;
int pngBitdepth;

// GIF (with CGIF)
double gifDither;
int gifEffort;
int gifBitdepth;

// WEBP
BOOL webpLossless;
BOOL webpNearLossless;
Expand Down
7 changes: 6 additions & 1 deletion vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,17 @@ func NewTiffExportParams() *TiffExportParams {
type GifExportParams struct {
StripMetadata bool
Quality int
Dither float64
Effort int
Bitdepth int
}

// NewGifExportParams creates default values for an export of a GIF image.
func NewGifExportParams() *GifExportParams {
return &GifExportParams{
Quality: 75,
Quality: 75,
Effort: 7,
Bitdepth: 8,
}
}

Expand Down