diff --git a/processing/watermark.go b/processing/watermark.go index 2665043d..dc12b780 100644 --- a/processing/watermark.go +++ b/processing/watermark.go @@ -72,7 +72,7 @@ func prepareWatermark(wm *vips.Image, wmData *imagedata.ImageData, opts *options } if opts.ShouldReplicate() { - if err := wm.Replicate(imgWidth, imgHeight); err != nil { + if err := wm.Replicate(imgWidth, imgHeight, true); err != nil { return err } } @@ -112,7 +112,7 @@ func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options. // If we replicated the watermark and need to apply it to an animated image, // it is faster to replicate the watermark to all the image and apply it single-pass if opts.ShouldReplicate() && framesCount > 1 { - if err := wm.Replicate(width, height); err != nil { + if err := wm.Replicate(width, height, false); err != nil { return err } diff --git a/vips/vips.c b/vips/vips.c index dcd5d991..3d7a27cb 100644 --- a/vips/vips.c +++ b/vips/vips.c @@ -691,26 +691,27 @@ vips_trim(VipsImage *in, VipsImage **out, double threshold, } int -vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) +vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height, int centered) { VipsImage *tmp; int across = VIPS_CEIL((double) width / in->Xsize); int down = VIPS_CEIL((double) height / in->Ysize); - if (across % 2 == 0) - across++; - if (down % 2 == 0) - down++; + if (centered) { + if (across % 2 == 0) + across++; + if (down % 2 == 0) + down++; + } if (vips_replicate(in, &tmp, across, down, NULL)) return 1; - if (vips_extract_area(tmp, out, - (tmp->Xsize - width) / 2, - (tmp->Ysize - height) / 2, - width, height, - NULL)) { + const int left = centered ? (tmp->Xsize - width) / 2 : 0; + const int top = centered ? (tmp->Ysize - height) / 2 : 0; + + if (vips_extract_area(tmp, out, left, top, width, height, NULL)) { clear_image(&tmp); return 1; } diff --git a/vips/vips.go b/vips/vips.go index 41bc028e..fa8fb1da 100644 --- a/vips/vips.go +++ b/vips/vips.go @@ -852,10 +852,10 @@ func (img *Image) CopyMemory() error { return nil } -func (img *Image) Replicate(width, height int) error { +func (img *Image) Replicate(width, height int, centered bool) error { var tmp *C.VipsImage - if C.vips_replicate_go(img.VipsImage, &tmp, C.int(width), C.int(height)) != 0 { + if C.vips_replicate_go(img.VipsImage, &tmp, C.int(width), C.int(height), gbool(centered)) != 0 { return Error() } C.swap_and_clear(&img.VipsImage, tmp) diff --git a/vips/vips.h b/vips/vips.h index 5a972383..8e27f9be 100644 --- a/vips/vips.h +++ b/vips/vips.h @@ -67,7 +67,7 @@ int vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma, double int vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b); -int vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down); +int vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down, int centered); int vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height); int vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top,