Add default background metadata for PNG and GIF images

This commit is contained in:
Lovell Fuller 2021-07-19 14:55:22 +01:00
parent 719c2db8da
commit 3f08f6a359
6 changed files with 22 additions and 1 deletions

View File

@ -22,6 +22,7 @@ A `Promise` is returned when `callback` is not provided.
* `pagePrimary`: Number of the primary page in a HEIF image * `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 * `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 * `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) * `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC)
* `hasProfile`: Boolean indicating the presence of an embedded ICC profile * `hasProfile`: Boolean indicating the presence of an embedded ICC profile
* `hasAlpha`: Boolean indicating the presence of an alpha transparency channel * `hasAlpha`: Boolean indicating the presence of an alpha transparency channel

View File

@ -8,6 +8,8 @@ Requires libvips v8.11.2
* Drop support for Node.js 10, now requires Node.js >= 12.13.0. * 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. * Add `compression` property to HEIF image metadata.
[#2504](https://github.com/lovell/sharp/issues/2504) [#2504](https://github.com/lovell/sharp/issues/2504)

View File

@ -271,6 +271,7 @@ function _isStreamInput () {
* - `pagePrimary`: Number of the primary page in a HEIF image * - `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 * - `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 * - `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) * - `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC)
* - `hasProfile`: Boolean indicating the presence of an embedded ICC profile * - `hasProfile`: Boolean indicating the presence of an embedded ICC profile
* - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel * - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel

View File

@ -90,6 +90,9 @@ class MetadataWorker : public Napi::AsyncWorker {
baton->subifds = image.get_int(VIPS_META_N_SUBIFDS); baton->subifds = image.get_int(VIPS_META_N_SUBIFDS);
} }
baton->hasProfile = sharp::HasProfile(image); baton->hasProfile = sharp::HasProfile(image);
if (image.get_typeof("background") == VIPS_TYPE_ARRAY_DOUBLE) {
baton->background = image.get_array_double("background");
}
// Derived attributes // Derived attributes
baton->hasAlpha = sharp::HasAlpha(image); baton->hasAlpha = sharp::HasAlpha(image);
baton->orientation = sharp::ExifOrientation(image); baton->orientation = sharp::ExifOrientation(image);
@ -209,6 +212,17 @@ class MetadataWorker : public Napi::AsyncWorker {
if (baton->subifds > 0) { if (baton->subifds > 0) {
info.Set("subifds", baton->subifds); 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("hasProfile", baton->hasProfile);
info.Set("hasAlpha", baton->hasAlpha); info.Set("hasAlpha", baton->hasAlpha);
if (baton->orientation > 0) { if (baton->orientation > 0) {

View File

@ -42,6 +42,7 @@ struct MetadataBaton {
std::string compression; std::string compression;
std::vector<std::pair<int, int>> levels; std::vector<std::pair<int, int>> levels;
int subifds; int subifds;
std::vector<double> background;
bool hasProfile; bool hasProfile;
bool hasAlpha; bool hasAlpha;
int orientation; int orientation;

View File

@ -256,6 +256,7 @@ describe('Image metadata', function () {
assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.orientation);
assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.exif);
assert.strictEqual('undefined', typeof metadata.icc); assert.strictEqual('undefined', typeof metadata.icc);
assert.deepStrictEqual(metadata.background, { r: 138, g: 148, b: 102 });
done(); done();
}); });
}); });
@ -285,7 +286,7 @@ describe('Image metadata', function () {
.then(({ .then(({
format, width, height, space, channels, depth, format, width, height, space, channels, depth,
isProgressive, pages, pageHeight, loop, delay, isProgressive, pages, pageHeight, loop, delay,
hasProfile, hasAlpha background, hasProfile, hasAlpha
}) => { }) => {
assert.strictEqual(format, 'gif'); assert.strictEqual(format, 'gif');
assert.strictEqual(width, 80); assert.strictEqual(width, 80);
@ -298,6 +299,7 @@ describe('Image metadata', function () {
assert.strictEqual(pageHeight, 80); assert.strictEqual(pageHeight, 80);
assert.strictEqual(loop, 0); assert.strictEqual(loop, 0);
assert.deepStrictEqual(delay, Array(30).fill(30)); assert.deepStrictEqual(delay, Array(30).fill(30));
assert.deepStrictEqual(background, { r: 0, g: 0, b: 0 });
assert.strictEqual(hasProfile, false); assert.strictEqual(hasProfile, false);
assert.strictEqual(hasAlpha, true); assert.strictEqual(hasAlpha, true);
}) })