mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
Add ensureAlpha op, adds alpha channel if missing #1153
This commit is contained in:
@@ -38,6 +38,18 @@ namespace sharp {
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
Ensures alpha channel, if missing.
|
||||
*/
|
||||
VImage EnsureAlpha(VImage image) {
|
||||
if (!HasAlpha(image)) {
|
||||
std::vector<double> alpha;
|
||||
alpha.push_back(sharp::MaximumImageAlpha(image.interpretation()));
|
||||
image = image.bandjoin_const(alpha);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
Composite overlayImage over image at given position
|
||||
Assumes alpha channels are already premultiplied and will be unpremultiplied after
|
||||
|
||||
@@ -30,6 +30,11 @@ namespace sharp {
|
||||
*/
|
||||
VImage RemoveAlpha(VImage image);
|
||||
|
||||
/*
|
||||
Ensures alpha channel, if missing.
|
||||
*/
|
||||
VImage EnsureAlpha(VImage image);
|
||||
|
||||
/*
|
||||
Alpha composite src over dst with given gravity.
|
||||
Assumes alpha channels are already premultiplied and will be unpremultiplied after.
|
||||
|
||||
@@ -677,6 +677,11 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = sharp::RemoveAlpha(image);
|
||||
}
|
||||
|
||||
// Ensure alpha channel, if missing
|
||||
if (baton->ensureAlpha) {
|
||||
image = sharp::EnsureAlpha(image);
|
||||
}
|
||||
|
||||
// Convert image to sRGB, if not already
|
||||
if (sharp::Is16Bit(image.interpretation())) {
|
||||
image = image.cast(VIPS_FORMAT_USHORT);
|
||||
@@ -1236,6 +1241,7 @@ NAN_METHOD(pipeline) {
|
||||
baton->extractChannel = AttrTo<int32_t>(options, "extractChannel");
|
||||
|
||||
baton->removeAlpha = AttrTo<bool>(options, "removeAlpha");
|
||||
baton->ensureAlpha = AttrTo<bool>(options, "ensureAlpha");
|
||||
if (HasAttr(options, "boolean")) {
|
||||
baton->boolean = CreateInputDescriptor(AttrAs<v8::Object>(options, "boolean"), buffersToPersist);
|
||||
baton->booleanOp = sharp::GetBooleanOperation(AttrAsStr(options, "booleanOp"));
|
||||
|
||||
@@ -142,6 +142,7 @@ struct PipelineBaton {
|
||||
VipsOperationBoolean bandBoolOp;
|
||||
int extractChannel;
|
||||
bool removeAlpha;
|
||||
bool ensureAlpha;
|
||||
VipsInterpretation colourspace;
|
||||
int tileSize;
|
||||
int tileOverlap;
|
||||
@@ -237,6 +238,7 @@ struct PipelineBaton {
|
||||
bandBoolOp(VIPS_OPERATION_BOOLEAN_LAST),
|
||||
extractChannel(-1),
|
||||
removeAlpha(false),
|
||||
ensureAlpha(false),
|
||||
colourspace(VIPS_INTERPRETATION_LAST),
|
||||
tileSize(256),
|
||||
tileOverlap(0),
|
||||
|
||||
Reference in New Issue
Block a user