فهرست منبع

Drop vips_ensure_alpha; Refactor vips_apply_watermark

DarthSim 2 سال پیش
والد
کامیت
dda20f1a1d
3فایلهای تغییر یافته به همراه20 افزوده شده و 36 حذف شده
  1. 20 23
      vips/vips.c
  2. 0 11
      vips/vips.go
  3. 0 2
      vips/vips.h

+ 20 - 23
vips/vips.c

@@ -510,23 +510,21 @@ vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
 
 int
 vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
-  VipsImage *tmp;
+  VipsImage *tmp = NULL;
 
-  int ret =
-    vips_ensure_alpha(in, &tmp) ||
-    vips_embed(tmp, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
+  if (!vips_image_hasalpha(in)) {
+    if (vips_bandjoin_const1(in, &tmp, 255, NULL))
+      return 1;
 
-  clear_image(&tmp);
+    in = tmp;
+  }
 
-  return ret;
-}
+  int ret =
+    vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
 
-int
-vips_ensure_alpha(VipsImage *in, VipsImage **out) {
-  if (vips_image_hasalpha(in))
-    return vips_copy(in, out, NULL);
+  if (tmp) clear_image(&tmp);
 
-  return vips_bandjoin_const1(in, out, 255, NULL);
+  return ret;
 }
 
 int
@@ -534,30 +532,29 @@ vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, doubl
   VipsImage *base = vips_image_new();
   VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 6);
 
-  if (vips_ensure_alpha(watermark, &t[0])) {
-    clear_image(&base);
-    return 1;
+  if (!vips_image_hasalpha(watermark)) {
+    if (vips_bandjoin_const1(watermark, &t[0], 255, NULL))
+      return 1;
+
+    watermark = t[0];
   }
 
   if (opacity < 1) {
     if (
-      vips_extract_band(t[0], &t[1], 0, "n", t[0]->Bands - 1, NULL) ||
-      vips_extract_band(t[0], &t[2], t[0]->Bands - 1, "n", 1, NULL) ||
+      vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
+      vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
       vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
       vips_bandjoin2(t[1], t[3], &t[4], NULL)
     ) {
       clear_image(&base);
       return 1;
     }
-  } else {
-    if (vips_copy(t[0], &t[4], NULL)) {
-      clear_image(&base);
-      return 1;
-    }
+
+    watermark = t[4];
   }
 
   int res =
-    vips_composite2(in, t[4], &t[5], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
+    vips_composite2(in, watermark, &t[5], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
     vips_cast(t[5], out, vips_image_get_format(in), NULL);
 
   clear_image(&base);

+ 0 - 11
vips/vips.go

@@ -553,17 +553,6 @@ func (img *Image) Trim(threshold float64, smart bool, color Color, equalHor bool
 	return nil
 }
 
-func (img *Image) EnsureAlpha() error {
-	var tmp *C.VipsImage
-
-	if C.vips_ensure_alpha(img.VipsImage, &tmp) != 0 {
-		return Error()
-	}
-
-	C.swap_and_clear(&img.VipsImage, tmp)
-	return nil
-}
-
 func (img *Image) Flatten(bg Color) error {
 	var tmp *C.VipsImage
 

+ 0 - 2
vips/vips.h

@@ -67,8 +67,6 @@ 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_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height);
 
-int vips_ensure_alpha(VipsImage *in, VipsImage **out);
-
 int vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity);
 
 int vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n);