From 1d3693695495b53348a215586975852d9a537136 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Tue, 22 Mar 2022 19:48:02 +0000 Subject: [PATCH] Ensure create has correct bit depth and colourspace #3139 --- docs/api-operation.md | 14 ++++++++++++++ docs/changelog.md | 5 +++++ lib/operation.js | 11 +++++++++++ src/common.cc | 9 ++------- test/unit/negate.js | 16 ++++++++++++++++ test/unit/noise.js | 3 ++- 6 files changed, 50 insertions(+), 8 deletions(-) diff --git a/docs/api-operation.md b/docs/api-operation.md index ced286603..a087efa9b 100644 --- a/docs/api-operation.md +++ b/docs/api-operation.md @@ -289,6 +289,20 @@ Produce the "negative" of the image. * `options.alpha` **[Boolean][6]** Whether or not to negate any alpha channel (optional, default `true`) +### Examples + +```javascript +const output = await sharp(input) + .negate() + .toBuffer(); +``` + +```javascript +const output = await sharp(input) + .negate({ alpha: false }) + .toBuffer(); +``` + Returns **Sharp** ## normalise diff --git a/docs/changelog.md b/docs/changelog.md index 4779fe911..9e062c5b1 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,6 +4,11 @@ Requires libvips v8.12.2 +### v0.30.4 - TBD + +* Ensure `create` input image has correct bit depth and colour space. + [#3139](https://github.com/lovell/sharp/issues/3139) + ### v0.30.3 - 14th March 2022 * Allow `sharpen` options to be provided more consistently as an Object. diff --git a/lib/operation.js b/lib/operation.js index f3094b397..55782c6e1 100644 --- a/lib/operation.js +++ b/lib/operation.js @@ -420,6 +420,17 @@ function gamma (gamma, gammaOut) { /** * Produce the "negative" of the image. + * + * @example + * const output = await sharp(input) + * .negate() + * .toBuffer(); + * + * @example + * const output = await sharp(input) + * .negate({ alpha: false }) + * .toBuffer(); + * * @param {Object} [options] * @param {Boolean} [options.alpha=true] Whether or not to negate any alpha channel * @returns {Sharp} diff --git a/src/common.cc b/src/common.cc index 7ad66639f..0ec61c93c 100644 --- a/src/common.cc +++ b/src/common.cc @@ -375,12 +375,6 @@ namespace sharp { VImage::option()->set("mean", descriptor->createNoiseMean)->set("sigma", descriptor->createNoiseSigma))); } image = image.bandjoin(bands); - image = image.cast(VipsBandFormat::VIPS_FORMAT_UCHAR); - if (channels < 3) { - image = image.colourspace(VIPS_INTERPRETATION_B_W); - } else { - image = image.colourspace(VIPS_INTERPRETATION_sRGB); - } } else { std::vector background = { descriptor->createBackground[0], @@ -392,7 +386,8 @@ namespace sharp { } image = VImage::new_matrix(descriptor->createWidth, descriptor->createHeight).new_from_image(background); } - image.get_image()->Type = VIPS_INTERPRETATION_sRGB; + image.get_image()->Type = image.guess_interpretation(); + image = image.cast(VIPS_FORMAT_UCHAR); imageType = ImageType::RAW; } else { // From filesystem diff --git a/test/unit/negate.js b/test/unit/negate.js index db1214e27..5b6557da3 100644 --- a/test/unit/negate.js +++ b/test/unit/negate.js @@ -186,6 +186,22 @@ describe('Negate', function () { }); }); + it('negate create', async () => { + const [r, g, b] = await sharp({ + create: { + width: 1, + height: 1, + channels: 3, + background: { r: 10, g: 20, b: 30 } + } + }) + .negate() + .raw() + .toBuffer(); + + assert.deepStrictEqual({ r, g, b }, { r: 245, g: 235, b: 225 }); + }); + it('invalid alpha value', function () { assert.throws(function () { sharp(fixtures.inputWebPWithTransparency).negate({ alpha: 'non-bool' }); diff --git a/test/unit/noise.js b/test/unit/noise.js index 555a39eb0..864851ab0 100644 --- a/test/unit/noise.js +++ b/test/unit/noise.js @@ -19,7 +19,7 @@ describe('Gaussian noise', function () { sigma: 30 } } - }); + }).toColourspace('b-w'); noise.toFile(output, function (err, info) { if (err) throw err; assert.strictEqual('png', info.format); @@ -131,6 +131,7 @@ describe('Gaussian noise', function () { } }); noise + .toColourspace('b-w') .toBuffer(function (err, data, info) { if (err) throw err; assert.strictEqual(1, info.channels);