From 3f08f6a35969f6d0c5387dcc40dd143945d132d7 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Mon, 19 Jul 2021 14:55:22 +0100 Subject: [PATCH] Add default background metadata for PNG and GIF images --- docs/api-input.md | 1 + docs/changelog.md | 2 ++ lib/input.js | 1 + src/metadata.cc | 14 ++++++++++++++ src/metadata.h | 1 + test/unit/metadata.js | 4 +++- 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/api-input.md b/docs/api-input.md index 0e6a4947..bac5cc68 100644 --- a/docs/api-input.md +++ b/docs/api-input.md @@ -22,6 +22,7 @@ A `Promise` is returned when `callback` is not provided. * `pagePrimary`: Number of the primary page in a HEIF image * `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide * `subifds`: Number of Sub Image File Directories in an OME-TIFF image +* `background`: Default background colour, if present, for PNG (bKGD) and GIF images, either an RGB Object or a single greyscale value * `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC) * `hasProfile`: Boolean indicating the presence of an embedded ICC profile * `hasAlpha`: Boolean indicating the presence of an alpha transparency channel diff --git a/docs/changelog.md b/docs/changelog.md index 323c3c1d..4b4093fe 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -8,6 +8,8 @@ Requires libvips v8.11.2 * Drop support for Node.js 10, now requires Node.js >= 12.13.0. +* Add `background` property to PNG and GIF image metadata. + * Add `compression` property to HEIF image metadata. [#2504](https://github.com/lovell/sharp/issues/2504) diff --git a/lib/input.js b/lib/input.js index 38f6105d..4de4673d 100644 --- a/lib/input.js +++ b/lib/input.js @@ -271,6 +271,7 @@ function _isStreamInput () { * - `pagePrimary`: Number of the primary page in a HEIF image * - `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide * - `subifds`: Number of Sub Image File Directories in an OME-TIFF image + * - `background`: Default background colour, if present, for PNG (bKGD) and GIF images, either an RGB Object or a single greyscale value * - `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC) * - `hasProfile`: Boolean indicating the presence of an embedded ICC profile * - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel diff --git a/src/metadata.cc b/src/metadata.cc index 771c14f0..9143b594 100644 --- a/src/metadata.cc +++ b/src/metadata.cc @@ -90,6 +90,9 @@ class MetadataWorker : public Napi::AsyncWorker { baton->subifds = image.get_int(VIPS_META_N_SUBIFDS); } baton->hasProfile = sharp::HasProfile(image); + if (image.get_typeof("background") == VIPS_TYPE_ARRAY_DOUBLE) { + baton->background = image.get_array_double("background"); + } // Derived attributes baton->hasAlpha = sharp::HasAlpha(image); baton->orientation = sharp::ExifOrientation(image); @@ -209,6 +212,17 @@ class MetadataWorker : public Napi::AsyncWorker { if (baton->subifds > 0) { info.Set("subifds", baton->subifds); } + if (!baton->background.empty()) { + if (baton->background.size() == 3) { + Napi::Object background = Napi::Object::New(env); + background.Set("r", baton->background[0]); + background.Set("g", baton->background[1]); + background.Set("b", baton->background[2]); + info.Set("background", background); + } else { + info.Set("background", baton->background[0]); + } + } info.Set("hasProfile", baton->hasProfile); info.Set("hasAlpha", baton->hasAlpha); if (baton->orientation > 0) { diff --git a/src/metadata.h b/src/metadata.h index 2262cd37..19816a65 100644 --- a/src/metadata.h +++ b/src/metadata.h @@ -42,6 +42,7 @@ struct MetadataBaton { std::string compression; std::vector> levels; int subifds; + std::vector background; bool hasProfile; bool hasAlpha; int orientation; diff --git a/test/unit/metadata.js b/test/unit/metadata.js index d32f6ba1..5fcb55f2 100644 --- a/test/unit/metadata.js +++ b/test/unit/metadata.js @@ -256,6 +256,7 @@ describe('Image metadata', function () { assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.icc); + assert.deepStrictEqual(metadata.background, { r: 138, g: 148, b: 102 }); done(); }); }); @@ -285,7 +286,7 @@ describe('Image metadata', function () { .then(({ format, width, height, space, channels, depth, isProgressive, pages, pageHeight, loop, delay, - hasProfile, hasAlpha + background, hasProfile, hasAlpha }) => { assert.strictEqual(format, 'gif'); assert.strictEqual(width, 80); @@ -298,6 +299,7 @@ describe('Image metadata', function () { assert.strictEqual(pageHeight, 80); assert.strictEqual(loop, 0); assert.deepStrictEqual(delay, Array(30).fill(30)); + assert.deepStrictEqual(background, { r: 0, g: 0, b: 0 }); assert.strictEqual(hasProfile, false); assert.strictEqual(hasAlpha, true); })