Skip to content

Commit

Permalink
Fix watermark replication across animation frames
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed Apr 24, 2024
1 parent 9721794 commit b43c6a7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions processing/watermark.go
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}

Expand Down
21 changes: 11 additions & 10 deletions vips/vips.c
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions vips/vips.go
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion vips/vips.h
Expand Up @@ -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,
Expand Down

0 comments on commit b43c6a7

Please sign in to comment.