Skip to content

Commit

Permalink
Ensure create has correct bit depth and colourspace #3139
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Mar 22, 2022
1 parent b609df4 commit 1d36936
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
14 changes: 14 additions & 0 deletions docs/api-operation.md
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.md
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions lib/operation.js
Expand Up @@ -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}
Expand Down
9 changes: 2 additions & 7 deletions src/common.cc
Expand Up @@ -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<double> background = {
descriptor->createBackground[0],
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/unit/negate.js
Expand Up @@ -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' });
Expand Down
3 changes: 2 additions & 1 deletion test/unit/noise.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1d36936

Please sign in to comment.