Add support for clipping/cutting out (#435) (#448)

USAGE: overlayWith('overlayimage.png', { cutout: true } )
This commit is contained in:
Kleis Auke Wolthuizen
2016-06-25 17:48:01 +02:00
committed by Lovell Fuller
parent f1ead06645
commit 2e9cd83ed2
21 changed files with 167 additions and 10 deletions

View File

@@ -45,6 +45,7 @@ using vips::VOption;
using vips::VError;
using sharp::Composite;
using sharp::Cutout;
using sharp::Normalize;
using sharp::Gamma;
using sharp::Blur;
@@ -464,8 +465,9 @@ class PipelineWorker : public AsyncWorker {
bool shouldBlur = baton->blurSigma != 0.0;
bool shouldSharpen = baton->sharpenSigma != 0.0;
bool shouldThreshold = baton->threshold != 0;
bool shouldCutout = baton->overlayCutout;
bool shouldPremultiplyAlpha = HasAlpha(image) &&
(shouldAffineTransform || shouldBlur || shouldSharpen || hasOverlay);
(shouldAffineTransform || shouldBlur || shouldSharpen || (hasOverlay && !shouldCutout));
// Premultiply image alpha channel before all transformations to avoid
// dark fringing around bright pixels
@@ -699,10 +701,15 @@ class PipelineWorker : public AsyncWorker {
// the overlayGravity was used for extract_area, therefore set it back to its default value of 0
baton->overlayGravity = 0;
}
// Ensure overlay is premultiplied sRGB
overlayImage = overlayImage.colourspace(VIPS_INTERPRETATION_sRGB).premultiply();
// Composite images with given gravity
image = Composite(overlayImage, image, baton->overlayGravity);
if(shouldCutout) {
// 'cut out' the image, premultiplication is not required
image = Cutout(overlayImage, image, baton->overlayGravity);
} else {
// Ensure overlay is premultiplied sRGB
overlayImage = overlayImage.colourspace(VIPS_INTERPRETATION_sRGB).premultiply();
// Composite images with given gravity
image = Composite(overlayImage, image, baton->overlayGravity);
}
}
// Reverse premultiplication after all transformations:
@@ -1086,6 +1093,7 @@ NAN_METHOD(pipeline) {
}
baton->overlayGravity = attrAs<int32_t>(options, "overlayGravity");
baton->overlayTile = attrAs<bool>(options, "overlayTile");
baton->overlayCutout = attrAs<bool>(options, "overlayCutout");
// Resize options
baton->withoutEnlargement = attrAs<bool>(options, "withoutEnlargement");
baton->crop = attrAs<int32_t>(options, "crop");