Ensure background metadata can be parsed #4090

This commit is contained in:
Lovell Fuller 2024-11-02 13:03:58 +00:00
parent 3796dd8a87
commit 04e7f58cea
9 changed files with 55 additions and 7 deletions

View File

@ -31,7 +31,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
- `background`: Default background colour, if present, for PNG (bKGD) and GIF images
- `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC)
- `resolutionUnit`: The unit of resolution (density), either `inch` or `cm`, if present
- `hasProfile`: Boolean indicating the presence of an embedded ICC profile

View File

@ -10,6 +10,9 @@ Requires libvips v8.16.0-rc2
* Expose WebP `smartDeblock` output option.
* Ensure `background` metadata can be parsed by `color` package.
[#4090](https://github.com/lovell/sharp/issues/4090)
* TypeScript: Ensure channel counts use the correct range.
[#4197](https://github.com/lovell/sharp/pull/4197)
[@DavidVaness](https://github.com/DavidVaness)

4
lib/index.d.ts vendored
View File

@ -1106,8 +1106,8 @@ declare namespace sharp {
tifftagPhotoshop?: Buffer | undefined;
/** The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC) */
compression?: 'av1' | 'hevc';
/** Default background colour, if present, for PNG (bKGD) and GIF images, either an RGB Object or a single greyscale value */
background?: { r: number; g: number; b: number } | number;
/** Default background colour, if present, for PNG (bKGD) and GIF images */
background?: { r: number; g: number; b: number } | { gray: number };
/** Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide */
levels?: LevelMetadata[] | undefined;
/** Number of Sub Image File Directories in an OME-TIFF image */

View File

@ -443,7 +443,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
* - `background`: Default background colour, if present, for PNG (bKGD) and GIF images
* - `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC)
* - `resolutionUnit`: The unit of resolution (density), either `inch` or `cm`, if present
* - `hasProfile`: Boolean indicating the presence of an embedded ICC profile

View File

@ -3,6 +3,7 @@
#include <numeric>
#include <vector>
#include <cmath>
#include <napi.h>
#include <vips/vips8>
@ -226,15 +227,15 @@ class MetadataWorker : public Napi::AsyncWorker {
info.Set("subifds", baton->subifds);
}
if (!baton->background.empty()) {
if (baton->background.size() == 3) {
Napi::Object background = Napi::Object::New(env);
if (baton->background.size() == 3) {
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]);
background.Set("gray", round(baton->background[0] * 100 / 255));
}
info.Set("background", background);
}
info.Set("hasProfile", baton->hasProfile);
info.Set("hasAlpha", baton->hasAlpha);

BIN
test/fixtures/bgbn4a08.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

BIN
test/fixtures/bggn4a16.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -80,6 +80,8 @@ module.exports = {
inputPngWithGreyAlpha: getPath('grey-8bit-alpha.png'),
inputPngWithOneColor: getPath('2x2_fdcce6.png'),
inputPngWithTransparency16bit: getPath('tbgn2c16.png'), // http://www.schaik.com/pngsuite/tbgn2c16.png
inputPng8BitGreyBackground: getPath('bgbn4a08.png'), // http://www.schaik.com/pngsuite/bgbn4a08.png
inputPng16BitGreyBackground: getPath('bggn4a16.png'), // http://www.schaik.com/pngsuite/bggn4a16.png
inputPng16BitGreyAlpha: getPath('16-bit-grey-alpha.png'), // CC-BY-NC-SA florc http://www.colourlovers.com/pattern/50713/pat
inputPngOverlayLayer0: getPath('alpha-layer-0-background.png'),
inputPngOverlayLayer1: getPath('alpha-layer-1-fill.png'),

View File

@ -201,6 +201,48 @@ describe('Image metadata', function () {
});
});
it('PNG with greyscale bKGD chunk - 8 bit', async () => {
const data = await sharp(fixtures.inputPng8BitGreyBackground).metadata();
assert.deepStrictEqual(data, {
background: {
gray: 0
},
bitsPerSample: 8,
channels: 2,
density: 72,
depth: 'uchar',
format: 'png',
hasAlpha: true,
hasProfile: false,
height: 32,
isPalette: false,
isProgressive: false,
space: 'b-w',
width: 32
});
});
it('PNG with greyscale bKGD chunk - 16 bit', async () => {
const data = await sharp(fixtures.inputPng16BitGreyBackground).metadata();
assert.deepStrictEqual(data, {
background: {
gray: 67
},
bitsPerSample: 16,
channels: 2,
density: 72,
depth: 'ushort',
format: 'png',
hasAlpha: true,
hasProfile: false,
height: 32,
isPalette: false,
isProgressive: false,
space: 'grey16',
width: 32
});
});
it('WebP', function (done) {
sharp(fixtures.inputWebP).metadata(function (err, metadata) {
if (err) throw err;