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

@@ -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),