Skip to content

Commit

Permalink
Expose reoptimise palette option for GIF output
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Jul 12, 2022
1 parent d247c02 commit 6288c7b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/api-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,14 @@ Use these GIF options for the output image.

The first entry in the palette is reserved for transparency.

The palette of the input image will be re-used if possible.

### Parameters

* `options` **[Object][6]?** output options

* `options.reoptimise` **[boolean][10]** always generate new palettes (slow), re-use existing by default (optional, default `false`)
* `options.reoptimize` **[boolean][10]** alternative spelling of `options.reoptimise` (optional, default `false`)
* `options.colours` **[number][12]** maximum number of palette entries, including transparency, between 2 and 256 (optional, default `256`)
* `options.colors` **[number][12]** alternative spelling of `options.colours` (optional, default `256`)
* `options.effort` **[number][12]** CPU effort, between 1 (fastest) and 10 (slowest) (optional, default `7`)
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Requires libvips v8.13.0

* Drop support for Node.js 12, now requires Node.js >= 14.15.0.

* GIF output now re-uses input palette if possible. Use `reoptimise` option to generate a new palette.

* Add WebP `minSize` and `mixed` options for greater control over animation frames.

* Use combined bounding box of alpha and non-alpha channels for `trim` operation.
Expand Down
1 change: 1 addition & 0 deletions lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ const Sharp = function (input, options) {
gifBitdepth: 8,
gifEffort: 7,
gifDither: 1,
gifReoptimise: false,
tiffQuality: 80,
tiffCompression: 'jpeg',
tiffPredictor: 'horizontal',
Expand Down
9 changes: 9 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ function webp (options) {
*
* The first entry in the palette is reserved for transparency.
*
* The palette of the input image will be re-used if possible.
*
* @since 0.30.0
*
* @example
Expand All @@ -545,6 +547,8 @@ function webp (options) {
* .toBuffer();
*
* @param {Object} [options] - output options
* @param {boolean} [options.reoptimise=false] - always generate new palettes (slow), re-use existing by default
* @param {boolean} [options.reoptimize=false] - alternative spelling of `options.reoptimise`
* @param {number} [options.colours=256] - maximum number of palette entries, including transparency, between 2 and 256
* @param {number} [options.colors=256] - alternative spelling of `options.colours`
* @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 10 (slowest)
Expand All @@ -557,6 +561,11 @@ function webp (options) {
*/
function gif (options) {
if (is.object(options)) {
if (is.defined(options.reoptimise)) {
this._setBooleanOption('gifReoptimise', options.reoptimise);
} else if (is.defined(options.reoptimize)) {
this._setBooleanOption('gifReoptimise', options.reoptimize);
}
const colours = options.colours || options.colors;
if (is.defined(colours)) {
if (is.integer(colours) && is.inRange(colours, 2, 256)) {
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ class PipelineWorker : public Napi::AsyncWorker {
->set("strip", !baton->withMetadata)
->set("bitdepth", baton->gifBitdepth)
->set("effort", baton->gifEffort)
->set("reoptimise", baton->gifReoptimise)
->set("dither", baton->gifDither)));
baton->bufferOut = static_cast<char*>(area->data);
baton->bufferOutLength = area->length;
Expand Down Expand Up @@ -1053,6 +1054,7 @@ class PipelineWorker : public Napi::AsyncWorker {
->set("strip", !baton->withMetadata)
->set("bitdepth", baton->gifBitdepth)
->set("effort", baton->gifEffort)
->set("reoptimise", baton->gifReoptimise)
->set("dither", baton->gifDither));
baton->formatOut = "gif";
} else if (baton->formatOut == "tiff" || (mightMatchInput && isTiff) ||
Expand Down Expand Up @@ -1537,6 +1539,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->gifBitdepth = sharp::AttrAsUint32(options, "gifBitdepth");
baton->gifEffort = sharp::AttrAsUint32(options, "gifEffort");
baton->gifDither = sharp::AttrAsDouble(options, "gifDither");
baton->gifReoptimise = sharp::AttrAsBool(options, "gifReoptimise");
baton->tiffQuality = sharp::AttrAsUint32(options, "tiffQuality");
baton->tiffPyramid = sharp::AttrAsBool(options, "tiffPyramid");
baton->tiffBitdepth = sharp::AttrAsUint32(options, "tiffBitdepth");
Expand Down
5 changes: 5 additions & 0 deletions src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct PipelineBaton {
int gifBitdepth;
int gifEffort;
double gifDither;
bool gifReoptimise;
int tiffQuality;
VipsForeignTiffCompression tiffCompression;
VipsForeignTiffPredictor tiffPredictor;
Expand Down Expand Up @@ -306,6 +307,10 @@ struct PipelineBaton {
webpEffort(4),
webpMinSize(false),
webpMixed(false),
gifBitdepth(8),
gifEffort(7),
gifDither(1.0),
gifReoptimise(false),
tiffQuality(80),
tiffCompression(VIPS_FOREIGN_TIFF_COMPRESSION_JPEG),
tiffPredictor(VIPS_FOREIGN_TIFF_PREDICTOR_HORIZONTAL),
Expand Down
16 changes: 16 additions & 0 deletions test/unit/gif.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ describe('GIF input', () => {
assert.strictEqual(true, reduced.length < original.length);
});

it('valid optimise', () => {
assert.doesNotThrow(() => sharp().gif({ reoptimise: true }));
assert.doesNotThrow(() => sharp().gif({ reoptimize: true }));
});

it('invalid reoptimise throws', () => {
assert.throws(
() => sharp().gif({ reoptimise: -1 }),
/Expected boolean for gifReoptimise but received -1 of type number/
);
assert.throws(
() => sharp().gif({ reoptimize: 'fail' }),
/Expected boolean for gifReoptimise but received fail of type string/
);
});

it('invalid loop throws', () => {
assert.throws(() => {
sharp().gif({ loop: -1 });
Expand Down

0 comments on commit 6288c7b

Please sign in to comment.