mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
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:
@@ -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");
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user