Add support for repeated/tiled overlay image (#443)

USAGE: overlayWith('overlayimage.png', { tile: true, gravity: northwest} )
When using the tile option, the gravity option is applied to the extracted part of the tiled overlay image.
This commit is contained in:
lemnisk8
2016-05-26 21:12:17 +05:30
committed by Lovell Fuller
parent e699e36270
commit 62554b766f
16 changed files with 103 additions and 0 deletions

View File

@@ -664,6 +664,35 @@ class PipelineWorker : public AsyncWorker {
if (overlayImageType == ImageType::UNKNOWN) {
return Error();
}
// Check if overlay is tiled
if (baton->overlayTile) {
int overlayImageWidth = overlayImage.width();
int overlayImageHeight = overlayImage.height();
int across = 0;
int down = 0;
// use gravity in ovelay
if(overlayImageWidth <= baton->width) {
across = static_cast<int>(ceil(static_cast<double>(baton->width) / overlayImageWidth));
}
if(overlayImageHeight <= baton->height) {
down = static_cast<int>(ceil(static_cast<double>(baton->height) / overlayImageHeight));
}
if(across != 0 || down != 0) {
int left;
int top;
overlayImage = overlayImage.replicate(across, down);
// the overlayGravity will now be used to CalculateCrop for extract_area
std::tie(left, top) = CalculateCrop(
overlayImage.width(), overlayImage.height(), baton->width, baton->height, baton->overlayGravity
);
overlayImage = overlayImage.extract_area(
left, top, baton->width, baton->height
);
}
// 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
@@ -1050,6 +1079,7 @@ NAN_METHOD(pipeline) {
baton->overlayBufferIn = node::Buffer::Data(overlayBufferIn);
}
baton->overlayGravity = attrAs<int32_t>(options, "overlayGravity");
baton->overlayTile = attrAs<bool>(options, "overlayTile");
// Resize options
baton->withoutEnlargement = attrAs<bool>(options, "withoutEnlargement");
baton->crop = attrAs<int32_t>(options, "crop");

View File

@@ -33,6 +33,7 @@ struct PipelineBaton {
char *overlayBufferIn;
size_t overlayBufferInLength;
int overlayGravity;
bool overlayTile;
int topOffsetPre;
int leftOffsetPre;
int widthPre;
@@ -97,6 +98,7 @@ struct PipelineBaton {
bufferOutLength(0),
overlayBufferInLength(0),
overlayGravity(0),
overlayTile(false),
topOffsetPre(-1),
topOffsetPost(-1),
channels(0),