Browse Source

Add C.RGB struct

DarthSim 1 week ago
parent
commit
9b59fb2d53
3 changed files with 31 additions and 19 deletions
  1. 13 13
      vips/vips.c
  2. 10 3
      vips/vips.go
  3. 8 3
      vips/vips.h

+ 13 - 13
vips/vips.c

@@ -689,14 +689,14 @@ vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
 }
 
 int
-vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b)
+vips_flatten_go(VipsImage *in, VipsImage **out, RGB bg)
 {
   if (!vips_image_hasalpha(in))
     return vips_copy(in, out, NULL);
 
-  VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
-  int res = vips_flatten(in, out, "background", bg, NULL);
-  vips_area_unref((VipsArea *) bg);
+  VipsArrayDouble *bga = vips_array_double_newv(3, bg.r, bg.g, bg.b);
+  int res = vips_flatten(in, out, "background", bga, NULL);
+  vips_area_unref((VipsArea *) bga);
   return res;
 }
 
@@ -708,8 +708,7 @@ vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int widt
 
 int
 vips_trim(VipsImage *in, VipsImage **out, double threshold,
-    gboolean smart, double r, double g, double b,
-    gboolean equal_hor, gboolean equal_ver)
+    gboolean smart, RGB bg, gboolean equal_hor, gboolean equal_ver)
 {
 
   VipsImage *base = vips_image_new();
@@ -726,26 +725,27 @@ vips_trim(VipsImage *in, VipsImage **out, double threshold,
   }
 
   if (vips_image_hasalpha(tmp)) {
-    if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
+    RGB f_bg = { 255.0, 0, 255.0 };
+    if (vips_flatten_go(tmp, &t[1], f_bg)) {
       clear_image(&base);
       return 1;
     }
     tmp = t[1];
   }
 
-  double *bg = NULL;
-  int bgn;
+  double *img_bg = NULL;
+  int img_bgn;
   VipsArrayDouble *bga;
 
   if (smart) {
-    if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
+    if (vips_getpoint(tmp, &img_bg, &img_bgn, 0, 0, NULL)) {
       clear_image(&base);
       return 1;
     }
-    bga = vips_array_double_new(bg, bgn);
+    bga = vips_array_double_new(img_bg, img_bgn);
   }
   else {
-    bga = vips_array_double_newv(3, r, g, b);
+    bga = vips_array_double_newv(3, bg.r, bg.g, bg.b);
   }
 
   int left, right, top, bot, width, height, diff;
@@ -753,7 +753,7 @@ vips_trim(VipsImage *in, VipsImage **out, double threshold,
 
   clear_image(&base);
   vips_area_unref((VipsArea *) bga);
-  g_free(bg);
+  g_free(img_bg);
 
   if (res) {
     return 1;

+ 10 - 3
vips/vips.go

@@ -301,6 +301,14 @@ func gbool(b bool) C.gboolean {
 	return C.gboolean(0)
 }
 
+func cRGB(c Color) C.RGB {
+	return C.RGB{
+		r: C.double(c.R),
+		g: C.double(c.G),
+		b: C.double(c.B),
+	}
+}
+
 func ptrToBytes(ptr unsafe.Pointer, size int) []byte {
 	return (*[math.MaxInt32]byte)(ptr)[:int(size):int(size)]
 }
@@ -695,8 +703,7 @@ func (img *Image) Trim(threshold float64, smart bool, color Color, equalHor bool
 	}
 
 	if C.vips_trim(img.VipsImage, &tmp, C.double(threshold),
-		gbool(smart), C.double(color.R), C.double(color.G), C.double(color.B),
-		gbool(equalHor), gbool(equalVer)) != 0 {
+		gbool(smart), cRGB(color), gbool(equalHor), gbool(equalVer)) != 0 {
 		return Error()
 	}
 
@@ -707,7 +714,7 @@ func (img *Image) Trim(threshold float64, smart bool, color Color, equalHor bool
 func (img *Image) Flatten(bg Color) error {
 	var tmp *C.VipsImage
 
-	if C.vips_flatten_go(img.VipsImage, &tmp, C.double(bg.R), C.double(bg.G), C.double(bg.B)) != 0 {
+	if C.vips_flatten_go(img.VipsImage, &tmp, cRGB(bg)) != 0 {
 		return Error()
 	}
 	C.swap_and_clear(&img.VipsImage, tmp)

+ 8 - 3
vips/vips.h

@@ -4,6 +4,12 @@
 #include <vips/vips7compat.h>
 #include <vips/vector.h>
 
+typedef struct _RGB {
+  double r;
+  double g;
+  double b;
+} RGB;
+
 int vips_initialize();
 
 void clear_image(VipsImage **in);
@@ -62,13 +68,12 @@ int vips_flip_horizontal_go(VipsImage *in, VipsImage **out);
 
 int vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height);
 int vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height);
-int vips_trim(VipsImage *in, VipsImage **out, double threshold, gboolean smart, double r, double g,
-    double b, gboolean equal_hor, gboolean equal_ver);
+int vips_trim(VipsImage *in, VipsImage **out, double threshold, gboolean smart, RGB bg, gboolean equal_hor, gboolean equal_ver);
 
 int vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma, double sharp_sigma,
     int pixelate_pixels);
 
-int vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b);
+int vips_flatten_go(VipsImage *in, VipsImage **out, RGB bg);
 
 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);