Add support for negating only non-alpha channels

Fixes #1035
This commit is contained in:
Espen Hovlandsdal 2021-07-22 23:27:27 +02:00 committed by Lovell Fuller
parent 21d1a7ca62
commit b7ddbe71f7
14 changed files with 122 additions and 6 deletions

View File

@ -221,7 +221,9 @@ Produce the "negative" of the image.
### Parameters
* `negate` **[Boolean][6]** (optional, default `true`)
* `options` **[Object][2]?**
* `options.alpha` **[Boolean][6]** Whether or not to negate any alpha channel (optional, default `true`)
Returns **Sharp**

File diff suppressed because one or more lines are too long

View File

@ -179,6 +179,7 @@ const Sharp = function (input, options) {
flatten: false,
flattenBackground: [0, 0, 0],
negate: false,
negateAlpha: true,
medianSize: 0,
blurSigma: 0,
sharpenSigma: 0,

View File

@ -325,11 +325,19 @@ function gamma (gamma, gammaOut) {
/**
* Produce the "negative" of the image.
* @param {Boolean} [negate=true]
* @param {Object} [options]
* @param {Boolean} [options.alpha=true] Whether or not to negate any alpha channel
* @returns {Sharp}
*/
function negate (negate) {
this.options.negate = is.bool(negate) ? negate : true;
function negate (options) {
this.options.negate = is.bool(options) ? options : true;
if (is.plainObject(options) && 'alpha' in options) {
if (!is.bool(options.alpha)) {
throw is.invalidParameterError('alpha', 'should be boolean value', options.alpha);
} else {
this.options.negateAlpha = options.alpha;
}
}
return this;
}

View File

@ -112,6 +112,19 @@ namespace sharp {
}
}
/**
* Produce the "negative" of the image.
*/
VImage Negate(VImage image, bool const negateAlpha) {
if (HasAlpha(image) && !negateAlpha) {
// Separate alpha channel
VImage alpha = image[image.bands() - 1];
return RemoveAlpha(image).invert().bandjoin(alpha);
} else {
return image.invert();
}
}
/*
* Gaussian blur. Use sigma of -1.0 for fast blur.
*/

View File

@ -45,6 +45,11 @@ namespace sharp {
*/
VImage Gamma(VImage image, double const exponent);
/*
* Produce the "negative" of the image.
*/
VImage Negate(VImage image, bool const negateAlpha);
/*
* Gaussian blur. Use sigma of -1.0 for fast blur.
*/

View File

@ -327,7 +327,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Negate the colours in the image
if (baton->negate) {
image = image.invert();
image = sharp::Negate(image, baton->negateAlpha);
}
// Gamma encoding (darken)
@ -1320,6 +1320,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->flatten = sharp::AttrAsBool(options, "flatten");
baton->flattenBackground = sharp::AttrAsVectorOfDouble(options, "flattenBackground");
baton->negate = sharp::AttrAsBool(options, "negate");
baton->negateAlpha = sharp::AttrAsBool(options, "negateAlpha");
baton->blurSigma = sharp::AttrAsDouble(options, "blurSigma");
baton->brightness = sharp::AttrAsDouble(options, "brightness");
baton->saturation = sharp::AttrAsDouble(options, "saturation");

View File

@ -90,6 +90,7 @@ struct PipelineBaton {
bool flatten;
std::vector<double> flattenBackground;
bool negate;
bool negateAlpha;
double blurSigma;
double brightness;
double saturation;
@ -220,6 +221,7 @@ struct PipelineBaton {
flatten(false),
flattenBackground{ 0.0, 0.0, 0.0 },
negate(false),
negateAlpha(true),
blurSigma(0.0),
brightness(1.0),
saturation(1.0),

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -107,4 +107,88 @@ describe('Negate', function () {
done();
});
});
it('negate ({alpha: true})', function (done) {
sharp(fixtures.inputJpg)
.resize(320, 240)
.negate({ alpha: true })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('negate.jpg'), data, done);
});
});
it('negate non-alpha channels (png)', function (done) {
sharp(fixtures.inputPng)
.resize(320, 240)
.negate({ alpha: false })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha.png'), data, done);
});
});
it('negate non-alpha channels (png, trans)', function (done) {
sharp(fixtures.inputPngWithTransparency)
.resize(320, 240)
.negate({ alpha: false })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha-trans.png'), data, done);
});
});
it('negate non-alpha channels (png, alpha)', function (done) {
sharp(fixtures.inputPngWithGreyAlpha)
.resize(320, 240)
.negate({ alpha: false })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha-grey.png'), data, done);
});
});
it('negate non-alpha channels (webp)', function (done) {
sharp(fixtures.inputWebP)
.resize(320, 240)
.negate({ alpha: false })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha.webp'), data, done);
});
});
it('negate non-alpha channels (webp, trans)', function (done) {
sharp(fixtures.inputWebPWithTransparency)
.resize(320, 240)
.negate({ alpha: false })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height);
fixtures.assertSimilar(fixtures.expected('negate-preserve-alpha-trans.webp'), data, done);
});
});
it('invalid alpha value', function () {
assert.throws(function () {
sharp(fixtures.inputWebPWithTransparency).negate({ alpha: 'non-bool' });
});
});
});