Explorar el Código

Fix watermark replication across animation frames

DarthSim hace 1 año
padre
commit
b43c6a7db7
Se han modificado 4 ficheros con 16 adiciones y 15 borrados
  1. 2 2
      processing/watermark.go
  2. 11 10
      vips/vips.c
  3. 2 2
      vips/vips.go
  4. 1 1
      vips/vips.h

+ 2 - 2
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
 		}
 

+ 11 - 10
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;
   }

+ 2 - 2
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)

+ 1 - 1
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,