Allow ensureAlpha to set alpha transparency level #2634

This commit is contained in:
Lovell Fuller
2021-04-01 21:14:06 +01:00
parent fe0767df13
commit 8c33d0aa56
10 changed files with 91 additions and 32 deletions

View File

@@ -799,10 +799,10 @@ namespace sharp {
/*
Ensures alpha channel, if missing.
*/
VImage EnsureAlpha(VImage image) {
VImage EnsureAlpha(VImage image, double const value) {
if (!HasAlpha(image)) {
std::vector<double> alpha;
alpha.push_back(sharp::MaximumImageAlpha(image.interpretation()));
alpha.push_back(value * sharp::MaximumImageAlpha(image.interpretation()));
image = image.bandjoin_const(alpha);
}
return image;

View File

@@ -296,7 +296,7 @@ namespace sharp {
/*
Ensures alpha channel, if missing.
*/
VImage EnsureAlpha(VImage image);
VImage EnsureAlpha(VImage image, double const value);
} // namespace sharp

View File

@@ -346,7 +346,7 @@ class PipelineWorker : public Napi::AsyncWorker {
bool const shouldModulate = baton->brightness != 1.0 || baton->saturation != 1.0 || baton->hue != 0.0;
if (shouldComposite && !sharp::HasAlpha(image)) {
image = sharp::EnsureAlpha(image);
image = sharp::EnsureAlpha(image, 1);
}
bool const shouldPremultiplyAlpha = sharp::HasAlpha(image) &&
@@ -594,7 +594,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Ensure image to composite is sRGB with premultiplied alpha
compositeImage = compositeImage.colourspace(VIPS_INTERPRETATION_sRGB);
if (!sharp::HasAlpha(compositeImage)) {
compositeImage = sharp::EnsureAlpha(compositeImage);
compositeImage = sharp::EnsureAlpha(compositeImage, 1);
}
if (!composite->premultiplied) compositeImage = compositeImage.premultiply();
// Calculate position
@@ -691,8 +691,8 @@ class PipelineWorker : public Napi::AsyncWorker {
}
// Ensure alpha channel, if missing
if (baton->ensureAlpha) {
image = sharp::EnsureAlpha(image);
if (baton->ensureAlpha != -1) {
image = sharp::EnsureAlpha(image, baton->ensureAlpha);
}
// Convert image to sRGB, if not already
@@ -1341,7 +1341,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->affineInterpolator = vips::VInterpolate::new_from_name(sharp::AttrAsStr(options, "affineInterpolator").data());
baton->removeAlpha = sharp::AttrAsBool(options, "removeAlpha");
baton->ensureAlpha = sharp::AttrAsBool(options, "ensureAlpha");
baton->ensureAlpha = sharp::AttrAsDouble(options, "ensureAlpha");
if (options.Has("boolean")) {
baton->boolean = sharp::CreateInputDescriptor(options.Get("boolean").As<Napi::Object>());
baton->booleanOp = sharp::GetBooleanOperation(sharp::AttrAsStr(options, "booleanOp"));

View File

@@ -178,7 +178,7 @@ struct PipelineBaton {
VipsOperationBoolean bandBoolOp;
int extractChannel;
bool removeAlpha;
bool ensureAlpha;
double ensureAlpha;
VipsInterpretation colourspace;
int pageHeight;
std::vector<int> delay;
@@ -297,7 +297,7 @@ struct PipelineBaton {
bandBoolOp(VIPS_OPERATION_BOOLEAN_LAST),
extractChannel(-1),
removeAlpha(false),
ensureAlpha(false),
ensureAlpha(-1.0),
colourspace(VIPS_INTERPRETATION_LAST),
pageHeight(0),
delay{-1},