Ensure create has correct bit depth and colourspace #3139

This commit is contained in:
Lovell Fuller 2022-03-22 19:48:02 +00:00
parent b609df4b48
commit 1d36936954
6 changed files with 50 additions and 8 deletions

View File

@ -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`) * `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** Returns **Sharp**
## normalise ## normalise

View File

@ -4,6 +4,11 @@
Requires libvips v8.12.2 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 ### v0.30.3 - 14th March 2022
* Allow `sharpen` options to be provided more consistently as an Object. * Allow `sharpen` options to be provided more consistently as an Object.

View File

@ -420,6 +420,17 @@ function gamma (gamma, gammaOut) {
/** /**
* Produce the "negative" of the image. * 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 {Object} [options]
* @param {Boolean} [options.alpha=true] Whether or not to negate any alpha channel * @param {Boolean} [options.alpha=true] Whether or not to negate any alpha channel
* @returns {Sharp} * @returns {Sharp}

View File

@ -375,12 +375,6 @@ namespace sharp {
VImage::option()->set("mean", descriptor->createNoiseMean)->set("sigma", descriptor->createNoiseSigma))); VImage::option()->set("mean", descriptor->createNoiseMean)->set("sigma", descriptor->createNoiseSigma)));
} }
image = image.bandjoin(bands); 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 { } else {
std::vector<double> background = { std::vector<double> background = {
descriptor->createBackground[0], descriptor->createBackground[0],
@ -392,7 +386,8 @@ namespace sharp {
} }
image = VImage::new_matrix(descriptor->createWidth, descriptor->createHeight).new_from_image(background); 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; imageType = ImageType::RAW;
} else { } else {
// From filesystem // From filesystem

View File

@ -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 () { it('invalid alpha value', function () {
assert.throws(function () { assert.throws(function () {
sharp(fixtures.inputWebPWithTransparency).negate({ alpha: 'non-bool' }); sharp(fixtures.inputWebPWithTransparency).negate({ alpha: 'non-bool' });

View File

@ -19,7 +19,7 @@ describe('Gaussian noise', function () {
sigma: 30 sigma: 30
} }
} }
}); }).toColourspace('b-w');
noise.toFile(output, function (err, info) { noise.toFile(output, function (err, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
@ -131,6 +131,7 @@ describe('Gaussian noise', function () {
} }
}); });
noise noise
.toColourspace('b-w')
.toBuffer(function (err, data, info) { .toBuffer(function (err, data, info) {
if (err) throw err; if (err) throw err;
assert.strictEqual(1, info.channels); assert.strictEqual(1, info.channels);