Explorar el Código

Move c code to vips.c; Move complex vips operations to C

DarthSim hace 6 años
padre
commit
4d174e51cf
Se han modificado 5 ficheros con 553 adiciones y 475 borrados
  1. 0 9
      image_types.h
  2. 22 161
      process.go
  3. 1 1
      processing_options.go
  4. 479 0
      vips.c
  5. 51 304
      vips.h

+ 0 - 9
image_types.h

@@ -1,9 +0,0 @@
-enum types {
-  UNKNOWN = 0,
-  JPEG,
-  PNG,
-  WEBP,
-  GIF,
-  ICO,
-  SVG
-};

+ 22 - 161
process.go

@@ -258,32 +258,6 @@ func calcCrop(width, height, cropWidth, cropHeight int, gravity *gravityOptions)
 	return
 }
 
-func resizeImage(img **C.VipsImage, scale float64, hasAlpha bool) error {
-	var err error
-
-	premultiplied := false
-	var bandFormat C.VipsBandFormat
-
-	if hasAlpha {
-		if bandFormat, err = vipsPremultiply(img); err != nil {
-			return err
-		}
-		premultiplied = true
-	}
-
-	if err = vipsResize(img, scale); err != nil {
-		return err
-	}
-
-	if premultiplied {
-		if err = vipsUnpremultiply(img, bandFormat); err != nil {
-			return err
-		}
-	}
-
-	return nil
-}
-
 func transformImage(ctx context.Context, img **C.VipsImage, data []byte, po *processingOptions, imgtype imageType) error {
 	var err error
 
@@ -313,7 +287,7 @@ func transformImage(ctx context.Context, img **C.VipsImage, data []byte, po *pro
 				}
 			}
 
-			if err = resizeImage(img, scale, hasAlpha); err != nil {
+			if err = vipsResize(img, scale, hasAlpha); err != nil {
 				return err
 			}
 		}
@@ -392,7 +366,7 @@ func transformImage(ctx context.Context, img **C.VipsImage, data []byte, po *pro
 
 	if po.Enlarge && po.Resize == resizeCrop && po.Dpr > 1 {
 		// We didn't enlarge the image before, because is wasn't optimal. Now it's time to do it
-		if err = resizeImage(img, po.Dpr, hasAlpha); err != nil {
+		if err = vipsResize(img, po.Dpr, hasAlpha); err != nil {
 			return err
 		}
 		if err = vipsImageCopyMemory(img); err != nil {
@@ -578,37 +552,10 @@ func vipsPrepareWatermark() error {
 
 	var tmp *C.VipsImage
 
-	if cConf.WatermarkOpacity < 1 {
-		if vipsImageHasAlpha(watermark) {
-			var alpha *C.VipsImage
-			defer C.clear_image(&alpha)
-
-			if C.vips_extract_band_go(watermark, &tmp, (*watermark).Bands-1, 1) != 0 {
-				return vipsError()
-			}
-			C.swap_and_clear(&alpha, tmp)
-
-			if C.vips_extract_band_go(watermark, &tmp, 0, (*watermark).Bands-1) != 0 {
-				return vipsError()
-			}
-			C.swap_and_clear(&watermark, tmp)
-
-			if C.vips_linear_go(alpha, &tmp, cConf.WatermarkOpacity, 0) != 0 {
-				return vipsError()
-			}
-			C.swap_and_clear(&alpha, tmp)
-
-			if C.vips_bandjoin_go(watermark, alpha, &tmp) != 0 {
-				return vipsError()
-			}
-			C.swap_and_clear(&watermark, tmp)
-		} else {
-			if C.vips_bandjoin_const_go(watermark, &tmp, cConf.WatermarkOpacity*255) != 0 {
-				return vipsError()
-			}
-			C.swap_and_clear(&watermark, tmp)
-		}
+	if C.vips_apply_opacity(watermark, &tmp, C.double(conf.WatermarkOpacity)) != 0 {
+		return vipsError()
 	}
+	C.swap_and_clear(&watermark, tmp)
 
 	if tmp = C.vips_image_copy_memory(watermark); tmp == nil {
 		return vipsError()
@@ -725,35 +672,6 @@ func vipsSetInt(img *C.VipsImage, name string, value int) {
 	C.vips_image_set_int(img, cachedCString(name), C.int(value))
 }
 
-func vipsPremultiply(img **C.VipsImage) (C.VipsBandFormat, error) {
-	var tmp *C.VipsImage
-
-	format := C.vips_band_format(*img)
-
-	if C.vips_premultiply_go(*img, &tmp) != 0 {
-		return 0, vipsError()
-	}
-
-	C.swap_and_clear(img, tmp)
-	return format, nil
-}
-
-func vipsUnpremultiply(img **C.VipsImage, format C.VipsBandFormat) error {
-	var tmp *C.VipsImage
-
-	if C.vips_unpremultiply_go(*img, &tmp) != 0 {
-		return vipsError()
-	}
-	C.swap_and_clear(img, tmp)
-
-	if C.vips_cast_go(*img, &tmp, format) != 0 {
-		return vipsError()
-	}
-	C.swap_and_clear(img, tmp)
-
-	return nil
-}
-
 func vipsCastUchar(img **C.VipsImage) error {
 	var tmp *C.VipsImage
 
@@ -767,14 +685,21 @@ func vipsCastUchar(img **C.VipsImage) error {
 	return nil
 }
 
-func vipsResize(img **C.VipsImage, scale float64) error {
+func vipsResize(img **C.VipsImage, scale float64, hasAlpa bool) error {
 	var tmp *C.VipsImage
 
-	if C.vips_resize_go(*img, &tmp, C.double(scale)) != 0 {
-		return vipsError()
+	if hasAlpa {
+		if C.vips_resize_with_premultiply(*img, &tmp, C.double(scale)) != 0 {
+			return vipsError()
+		}
+	} else {
+		if C.vips_resize_go(*img, &tmp, C.double(scale)) != 0 {
+			return vipsError()
+		}
 	}
 
 	C.swap_and_clear(img, tmp)
+
 	return nil
 }
 
@@ -905,12 +830,7 @@ func vipsImageCopyMemory(img **C.VipsImage) error {
 func vipsReplicate(img **C.VipsImage, width, height C.int) error {
 	var tmp *C.VipsImage
 
-	if C.vips_replicate_go(*img, &tmp, 1+width/(*img).Xsize, 1+height/(*img).Ysize) != 0 {
-		return vipsError()
-	}
-	C.swap_and_clear(img, tmp)
-
-	if C.vips_extract_area_go(*img, &tmp, 0, 0, width, height) != 0 {
+	if C.vips_replicate_go(*img, &tmp, width, height) != 0 {
 		return vipsError()
 	}
 	C.swap_and_clear(img, tmp)
@@ -979,7 +899,7 @@ func vipsResizeWatermark(width, height int) (wm *C.VipsImage, err error) {
 		scale = 1 / wmH
 	}
 
-	if C.vips_resize_go(watermark, &wm, C.double(scale)) != 0 {
+	if C.vips_resize_with_premultiply(watermark, &wm, C.double(scale)) != 0 {
 		err = vipsError()
 	}
 
@@ -991,17 +911,16 @@ func vipsApplyWatermark(img **C.VipsImage, opts *watermarkOptions) error {
 		return nil
 	}
 
-	var wm, wmAlpha, tmp *C.VipsImage
-	var err error
-
+	var wm, tmp *C.VipsImage
 	defer C.clear_image(&wm)
-	defer C.clear_image(&wmAlpha)
+
+	var err error
 
 	imgW := (*img).Xsize
 	imgH := (*img).Ysize
 
 	if opts.Scale == 0 {
-		if wm = C.vips_image_copy_memory(watermark); wm == nil {
+		if C.vips_copy_go(watermark, &wm) != 0 {
 			return vipsError()
 		}
 	} else {
@@ -1023,69 +942,11 @@ func vipsApplyWatermark(img **C.VipsImage, opts *watermarkOptions) error {
 		}
 	}
 
-	if C.vips_extract_band_go(wm, &tmp, (*wm).Bands-1, 1) != 0 {
-		return vipsError()
-	}
-	C.swap_and_clear(&wmAlpha, tmp)
-
-	if C.vips_extract_band_go(wm, &tmp, 0, (*wm).Bands-1) != 0 {
-		return vipsError()
-	}
-	C.swap_and_clear(&wm, tmp)
-
-	imgInterpolation := C.vips_image_guess_interpretation(*img)
-
-	if imgInterpolation != C.vips_image_guess_interpretation(wm) {
-		if C.vips_colourspace_go(wm, &tmp, imgInterpolation) != 0 {
-			return vipsError()
-		}
-		C.swap_and_clear(&wm, tmp)
-	}
-
-	if opts.Opacity < 1 {
-		if C.vips_linear_go(wmAlpha, &tmp, C.double(opts.Opacity), 0) != 0 {
-			return vipsError()
-		}
-		C.swap_and_clear(&wmAlpha, tmp)
-	}
-
-	imgFormat := C.vips_image_get_format(*img)
-
-	var imgAlpha *C.VipsImage
-	defer C.clear_image(&imgAlpha)
-
-	hasAlpha := vipsImageHasAlpha(*img)
-
-	if hasAlpha {
-		if C.vips_extract_band_go(*img, &imgAlpha, (**img).Bands-1, 1) != 0 {
-			return vipsError()
-		}
-
-		if C.vips_extract_band_go(*img, &tmp, 0, (**img).Bands-1) != 0 {
-			return vipsError()
-		}
-		C.swap_and_clear(img, tmp)
-	}
-
-	if C.vips_ifthenelse_go(wmAlpha, wm, *img, &tmp) != 0 {
+	if C.vips_apply_watermark(*img, wm, &tmp, C.double(opts.Opacity)) != 0 {
 		return vipsError()
 	}
 	C.swap_and_clear(img, tmp)
 
-	if hasAlpha {
-		if C.vips_bandjoin_go(*img, imgAlpha, &tmp) != 0 {
-			return vipsError()
-		}
-		C.swap_and_clear(img, tmp)
-	}
-
-	if imgFormat != C.vips_image_get_format(*img) {
-		if C.vips_cast_go(*img, &tmp, imgFormat) != 0 {
-			return vipsError()
-		}
-		C.swap_and_clear(img, tmp)
-	}
-
 	return nil
 }
 

+ 1 - 1
processing_options.go

@@ -2,7 +2,7 @@ package main
 
 /*
 #cgo LDFLAGS: -s -w
-#include "image_types.h"
+#include "vips.h"
 */
 import "C"
 

+ 479 - 0
vips.c

@@ -0,0 +1,479 @@
+#include "vips.h"
+
+#define VIPS_SUPPORT_SMARTCROP \
+  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
+
+#define VIPS_SUPPORT_HASALPHA \
+  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
+
+#define VIPS_SUPPORT_GIF \
+  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
+
+#define VIPS_SUPPORT_SVG \
+  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
+
+#define VIPS_SUPPORT_MAGICK \
+  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
+
+#define EXIF_ORIENTATION "exif-ifd0-Orientation"
+
+int
+vips_initialize() {
+  return vips_init("imgproxy");
+}
+
+void
+clear_image(VipsImage **in) {
+  if (G_IS_OBJECT(*in)) g_clear_object(in);
+}
+
+void
+g_free_go(void **buf) {
+  g_free(*buf);
+}
+
+void
+swap_and_clear(VipsImage **in, VipsImage *out) {
+  clear_image(in);
+  *in = out;
+}
+
+int
+vips_type_find_load_go(int imgtype) {
+  switch (imgtype)
+  {
+  case (JPEG):
+    return vips_type_find("VipsOperation", "jpegload_buffer");
+  case (PNG):
+    return vips_type_find("VipsOperation", "pngload_buffer");
+  case (WEBP):
+    return vips_type_find("VipsOperation", "webpload_buffer");
+  case (GIF):
+    return vips_type_find("VipsOperation", "gifload_buffer");
+  case (SVG):
+    return vips_type_find("VipsOperation", "svgload_buffer");
+  }
+  return 0;
+}
+
+int
+vips_type_find_save_go(int imgtype) {
+  switch (imgtype)
+  {
+  case (JPEG):
+    return vips_type_find("VipsOperation", "jpegsave_buffer");
+  case (PNG):
+    return vips_type_find("VipsOperation", "pngsave_buffer");
+  case (WEBP):
+    return vips_type_find("VipsOperation", "webpsave_buffer");
+  case (GIF):
+    return vips_type_find("VipsOperation", "magicksave_buffer");
+  case (ICO):
+    return vips_type_find("VipsOperation", "magicksave_buffer");
+  }
+
+  return 0;
+}
+
+int
+vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
+  if (shrink > 1)
+    return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
+
+  return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
+}
+
+int
+vips_pngload_go(void *buf, size_t len, VipsImage **out) {
+  return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
+}
+
+int
+vips_webpload_go(void *buf, size_t len, int shrink, VipsImage **out) {
+  if (shrink > 1)
+    return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
+
+  return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
+}
+
+int
+vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
+  #if VIPS_SUPPORT_GIF
+    return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
+  #else
+    vips_error("vips_gifload_go", "Loading GIF is not supported");
+    return 1;
+  #endif
+}
+
+int
+vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
+  #if VIPS_SUPPORT_SVG
+    return vips_svgload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "scale", scale, NULL);
+  #else
+    vips_error("vips_svgload_go", "Loading SVG is not supported");
+    return 1;
+  #endif
+}
+
+int
+vips_get_exif_orientation(VipsImage *image) {
+	const char *orientation;
+
+	if (
+		vips_image_get_typeof(image, EXIF_ORIENTATION) != G_TYPE_INVALID &&
+		vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
+	) return atoi(&orientation[0]);
+
+	return 1;
+}
+
+int
+vips_support_smartcrop() {
+#if VIPS_SUPPORT_SMARTCROP
+  return 1;
+#else
+  return 0;
+#endif
+}
+
+VipsBandFormat
+vips_band_format(VipsImage *in) {
+  return in->BandFmt;
+}
+
+gboolean
+vips_is_animated_gif(VipsImage * in) {
+  return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
+          vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
+          vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
+}
+
+gboolean
+vips_image_hasalpha_go(VipsImage * in) {
+#if VIPS_SUPPORT_HASALPHA
+  return vips_image_hasalpha(in);
+#else
+  return( in->Bands == 2 ||
+		      (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
+		      in->Bands > 4 );
+#endif
+}
+
+int
+vips_copy_go(VipsImage *in, VipsImage **out) {
+  return vips_copy(in, out, NULL);
+}
+
+int
+vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
+  return vips_cast(in, out, format, NULL);
+}
+
+int
+vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
+  return vips_resize(in, out, scale, NULL);
+}
+
+int
+vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale) {
+	VipsBandFormat format;
+  VipsImage *tmp1, *tmp2;
+
+  format = vips_band_format(in);
+
+  if (vips_premultiply(in, &tmp1, NULL))
+    return 1;
+
+	if (vips_resize(tmp1, &tmp2, scale, NULL)) {
+    clear_image(&tmp1);
+		return 1;
+  }
+  swap_and_clear(&tmp1, tmp2);
+
+  if (vips_unpremultiply(tmp1, &tmp2, NULL)) {
+    clear_image(&tmp1);
+		return 1;
+  }
+  swap_and_clear(&tmp1, tmp2);
+
+  if (vips_cast(tmp1, out, format, NULL)) {
+    clear_image(&tmp1);
+		return 1;
+  }
+
+  clear_image(&tmp1);
+
+  return 0;
+}
+
+int
+vips_need_icc_import(VipsImage *in) {
+  return in->Type == VIPS_INTERPRETATION_CMYK;
+}
+
+int
+vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
+  return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
+}
+
+int
+vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
+  return vips_colourspace(in, out, cs, NULL);
+}
+
+int
+vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
+  return vips_rot(in, out, angle, NULL);
+}
+
+int
+vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
+  return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
+}
+
+int
+vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
+#if VIPS_SUPPORT_SMARTCROP
+  return vips_smartcrop(in, out, width, height, NULL);
+#else
+  vips_error("vips_smartcrop_go", "Smart crop is not supported");
+  return 1;
+#endif
+}
+
+int
+vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
+  return vips_gaussblur(in, out, sigma, NULL);
+}
+
+int
+vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
+  return vips_sharpen(in, out, "sigma", sigma, NULL);
+}
+
+int
+vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
+  VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
+  int res = vips_flatten(in, out, "background", bg, NULL);
+  vips_area_unref((VipsArea *)bg);
+  return res;
+}
+
+int
+vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
+  return vips_extract_area(in, out, left, top, width, height, NULL);
+}
+
+int
+vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
+  VipsImage *tmp;
+
+  if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
+    return 1;
+
+	if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
+    clear_image(&tmp);
+		return 1;
+  }
+
+  clear_image(&tmp);
+
+  return 0;
+}
+
+int
+vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
+  return vips_embed(in, out, x, y, width, height, NULL);
+}
+
+int
+vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity){
+  gboolean has_alpha = vips_image_hasalpha_go(in);
+
+  if (has_alpha) {
+    if (opacity < 1) {
+      VipsImage *img, *img_alpha, *tmp;
+
+			if (vips_extract_band(in, &img, 0, "n", in->Bands - 1, NULL))
+				return 1;
+
+      if (vips_extract_band(in, &img_alpha, in->Bands - 1, "n", 1, NULL)) {
+        clear_image(&img);
+				return 1;
+			}
+
+			if (vips_linear1(img_alpha, &tmp, opacity, 0, NULL)) {
+        clear_image(&img);
+        clear_image(&img_alpha);
+				return 1;
+			}
+			swap_and_clear(&img_alpha, tmp);
+
+			if (vips_bandjoin2(img, img_alpha, out, NULL)) {
+        clear_image(&img);
+        clear_image(&img_alpha);
+				return 1;
+			}
+
+      clear_image(&img);
+      clear_image(&img_alpha);
+    } else {
+      if (vips_copy(in, out, NULL)) {
+        return 1;
+      }
+    }
+  } else {
+    if (vips_bandjoin_const1(in, out, opacity * 255, NULL)) {
+      return 1;
+    }
+  }
+
+  return 0;
+}
+
+int
+vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
+  VipsImage *wm, *wm_alpha, *tmp;
+
+	if (vips_extract_band(watermark, &wm, 0, "n", watermark->Bands - 1, NULL))
+		return 1;
+
+  if (vips_extract_band(watermark, &wm_alpha, watermark->Bands - 1, "n", 1, NULL)) {
+    clear_image(&wm);
+		return 1;
+	}
+
+	VipsInterpretation img_interpolation = vips_image_guess_interpretation(in);
+
+	if (img_interpolation != vips_image_guess_interpretation(wm)) {
+		if (vips_colourspace(wm, &tmp, img_interpolation, NULL)) {
+      clear_image(&wm);
+      clear_image(&wm_alpha);
+			return 1;
+		}
+		swap_and_clear(&wm, tmp);
+	}
+
+	if (opacity < 1) {
+		if (vips_linear1(wm_alpha, &tmp, opacity, 0, NULL)) {
+      clear_image(&wm);
+      clear_image(&wm_alpha);
+			return 1;
+		}
+
+		swap_and_clear(&wm_alpha, tmp);
+	}
+
+	VipsBandFormat img_format;
+	VipsImage *img, *img_alpha;
+
+	img_format = vips_image_get_format(in);
+
+  gboolean has_alpha = vips_image_hasalpha_go(in);
+
+	if (has_alpha) {
+		if (vips_extract_band(in, &img, 0, "n", in->Bands - 1, NULL)) {
+      clear_image(&wm);
+      clear_image(&wm_alpha);
+			return 1;
+		}
+
+		if (vips_extract_band(in, &img_alpha, in->Bands - 1, "n", 1, NULL)) {
+      clear_image(&wm);
+      clear_image(&wm_alpha);
+      clear_image(&img);
+			return 1;
+		}
+	} else {
+    if (vips_copy(in, &img, NULL)) {
+      clear_image(&wm);
+      clear_image(&wm_alpha);
+			return 1;
+    }
+  }
+
+	if (vips_ifthenelse(wm_alpha, wm, img, &tmp, "blend", TRUE, NULL)) {
+    clear_image(&wm);
+    clear_image(&wm_alpha);
+    clear_image(&img);
+    clear_image(&img_alpha);
+		return 1;
+	}
+
+	swap_and_clear(&img, tmp);
+  clear_image(&wm);
+  clear_image(&wm_alpha);
+
+	if (has_alpha) {
+		if (vips_bandjoin2(img, img_alpha, &tmp, NULL)) {
+      clear_image(&img);
+      clear_image(&img_alpha);
+			return 1;
+		}
+
+		swap_and_clear(&img, tmp);
+    clear_image(&img_alpha);
+	}
+
+	if (img_format != vips_image_get_format(img)) {
+		if (vips_cast(img, &tmp, img_format, NULL)) {
+      clear_image(&img);
+			return 1;
+		}
+		swap_and_clear(&img, tmp);
+	}
+
+  *out = img;
+
+  return 0;
+}
+
+int
+vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
+  return vips_arrayjoin(in, out, n, "across", 1, NULL);
+}
+
+int
+vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
+  return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
+}
+
+int
+vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int embed_profile) {
+  if (embed_profile)
+    return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
+
+  return vips_pngsave_buffer(in, buf, len, "profile", "none", "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
+}
+
+int
+vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
+  return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
+}
+
+int
+vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
+#if VIPS_SUPPORT_MAGICK
+  return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
+#else
+  vips_error("vips_gifsave_go", "Saving GIF is not supported");
+  return 1;
+#endif
+}
+
+int
+vips_icosave_go(VipsImage *in, void **buf, size_t *len) {
+#if VIPS_SUPPORT_MAGICK
+  return vips_magicksave_buffer(in, buf, len, "format", "ico", NULL);
+#else
+  vips_error("vips_icosave_go", "Saving ICO is not supported");
+  return 1;
+#endif
+}
+
+void
+vips_cleanup() {
+  vips_error_clear();
+  vips_thread_shutdown();
+}

+ 51 - 304
vips.h

@@ -1,330 +1,77 @@
 #include <stdlib.h>
+
 #include <vips/vips.h>
 #include <vips/vips7compat.h>
-#include "image_types.h"
-
-#define VIPS_SUPPORT_SMARTCROP \
-  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
-
-#define VIPS_SUPPORT_HASALPHA \
-  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
-
-#define VIPS_SUPPORT_GIF \
-  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
-
-#define VIPS_SUPPORT_SVG \
-  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
-
-#define VIPS_SUPPORT_MAGICK \
-  (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
-
-#define EXIF_ORIENTATION "exif-ifd0-Orientation"
-
-int
-vips_initialize() {
-  return vips_init("imgproxy");
-}
-
-void
-clear_image(VipsImage **in) {
-  if (G_IS_OBJECT(*in)) g_clear_object(in);
-}
-
-void
-g_free_go(void **buf) {
-  g_free(*buf);
-}
-
-void
-swap_and_clear(VipsImage **in, VipsImage *out) {
-  clear_image(in);
-  *in = out;
-}
-
-int
-vips_type_find_load_go(int imgtype) {
-  if (imgtype == JPEG) {
-    return vips_type_find("VipsOperation", "jpegload_buffer");
-  }
-  if (imgtype == PNG) {
-    return vips_type_find("VipsOperation", "pngload_buffer");
-  }
-  if (imgtype == WEBP) {
-    return vips_type_find("VipsOperation", "webpload_buffer");
-  }
-  if (imgtype == GIF) {
-    return vips_type_find("VipsOperation", "gifload_buffer");
-  }
-  if (imgtype == SVG) {
-    return vips_type_find("VipsOperation", "svgload_buffer");
-  }
-  return 0;
-}
-
-int
-vips_type_find_save_go(int imgtype) {
-  if (imgtype == JPEG) {
-    return vips_type_find("VipsOperation", "jpegsave_buffer");
-  }
-  if (imgtype == PNG) {
-    return vips_type_find("VipsOperation", "pngsave_buffer");
-  }
-  if (imgtype == WEBP) {
-    return vips_type_find("VipsOperation", "webpsave_buffer");
-  }
-  if (imgtype == GIF) {
-    return vips_type_find("VipsOperation", "magicksave_buffer");
-  }
-  if (imgtype == ICO) {
-    return vips_type_find("VipsOperation", "magicksave_buffer");
-  }
-  return 0;
-}
-
-int
-vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
-  if (shrink > 1) {
-    return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
-  }
-  return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
-}
-
-int
-vips_pngload_go(void *buf, size_t len, VipsImage **out) {
-  return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
-}
-
-int
-vips_webpload_go(void *buf, size_t len, int shrink, VipsImage **out) {
-  if (shrink > 1) {
-    return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
-  }
-  return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
-}
-
-int
-vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
-  #if VIPS_SUPPORT_GIF
-    return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
-  #else
-    vips_error("vips_gifload_go", "Loading GIF is not supported");
-    return 1;
-  #endif
-}
-
-int
-vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
-  #if VIPS_SUPPORT_SVG
-    return vips_svgload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "scale", scale, NULL);
-  #else
-    vips_error("vips_svgload_go", "Loading SVG is not supported");
-    return 1;
-  #endif
-}
-
-int
-vips_get_exif_orientation(VipsImage *image) {
-	const char *orientation;
-
-	if (
-		vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
-		vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
-	) return atoi(&orientation[0]);
-
-	return 1;
-}
-
-int
-vips_support_smartcrop() {
-#if VIPS_SUPPORT_SMARTCROP
-  return 1;
-#else
-  return 0;
-#endif
-}
-
-VipsBandFormat
-vips_band_format(VipsImage *in) {
-  return in->BandFmt;
-}
-
-gboolean
-vips_is_animated_gif(VipsImage * in) {
-  return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
-          vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
-          vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
-}
-
-gboolean
-vips_image_hasalpha_go(VipsImage * in) {
-#if VIPS_SUPPORT_HASALPHA
-  return vips_image_hasalpha(in);
-#else
-  return( in->Bands == 2 ||
-		      (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
-		      in->Bands > 4 );
-#endif
-}
-
-int
-vips_premultiply_go(VipsImage *in, VipsImage **out) {
-  return vips_premultiply(in, out, NULL);
-}
-
-int
-vips_unpremultiply_go(VipsImage *in, VipsImage **out) {
-  return vips_unpremultiply(in, out, NULL);
-}
-
-int
-vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
-  return vips_cast(in, out, format, NULL);
-}
-
-int
-vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
-  return vips_resize(in, out, scale, NULL);
-}
-
-int
-vips_need_icc_import(VipsImage *in) {
-  return in->Type == VIPS_INTERPRETATION_CMYK;
-}
-
-int
-vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
-  return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
-}
 
-int
-vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
-  return vips_colourspace(in, out, cs, NULL);
-}
+enum ImgproxyImageTypes {
+  UNKNOWN = 0,
+  JPEG,
+  PNG,
+  WEBP,
+  GIF,
+  ICO,
+  SVG
+};
 
-int
-vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
-  return vips_rot(in, out, angle, NULL);
-}
+int vips_initialize();
 
-int
-vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
-  return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
-}
+void clear_image(VipsImage **in);
+void g_free_go(void **buf);
 
-int
-vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
-#if VIPS_SUPPORT_SMARTCROP
-  return vips_smartcrop(in, out, width, height, NULL);
-#else
-  vips_error("vips_smartcrop_go", "Smart crop is not supported");
-  return 1;
-#endif
-}
+void swap_and_clear(VipsImage **in, VipsImage *out);
 
-int
-vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
-  return vips_gaussblur(in, out, sigma, NULL);
-}
+int vips_type_find_load_go(int imgtype);
+int vips_type_find_save_go(int imgtype);
 
-int
-vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
-  return vips_sharpen(in, out, "sigma", sigma, NULL);
-}
+int vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out);
+int vips_pngload_go(void *buf, size_t len, VipsImage **out);
+int vips_webpload_go(void *buf, size_t len, int shrink, VipsImage **out);
+int vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out);
+int vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out);
 
-int
-vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
-  VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
-  int res = vips_flatten(in, out, "background", bg, NULL);
-  vips_area_unref((VipsArea *)bg);
-  return res;
-}
+int vips_get_exif_orientation(VipsImage *image);
 
-int
-vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
-  return vips_extract_area(in, out, left, top, width, height, NULL);
-}
+int vips_support_smartcrop();
 
-int
-vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down) {
-  return vips_replicate(in, out, across, down, NULL);
-}
+VipsBandFormat vips_band_format(VipsImage *in);
 
-int
-vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
-  return vips_embed(in, out, x, y, width, height, NULL);
-}
+gboolean vips_is_animated_gif(VipsImage * in);
+gboolean vips_image_hasalpha_go(VipsImage * in);
 
-int
-vips_extract_band_go(VipsImage *in, VipsImage **out, int band, int band_num) {
-  return vips_extract_band(in, out, band, "n", band_num, NULL);
-}
+int vips_copy_go(VipsImage *in, VipsImage **out);
 
-int
-vips_bandjoin_go (VipsImage *in1, VipsImage *in2, VipsImage **out) {
-  return vips_bandjoin2(in1, in2, out, NULL);
-}
+int vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format);
 
-int
-vips_bandjoin_const_go (VipsImage *in, VipsImage **out, double c) {
-  return vips_bandjoin_const1(in, out, c, NULL);
-}
+int vips_resize_go(VipsImage *in, VipsImage **out, double scale);
+int vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale);
 
-int
-vips_linear_go (VipsImage *in, VipsImage **out, double a, double b) {
-  return vips_linear1(in, out, a, b, NULL);
-}
+int vips_need_icc_import(VipsImage *in);
+int vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile);
+int vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs);
 
-int
-vips_ifthenelse_go(VipsImage *cond, VipsImage *in1, VipsImage *in2, VipsImage **out) {
-  return vips_ifthenelse(cond, in1, in2, out, "blend", TRUE, NULL);
-}
+int vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle);
+int vips_flip_horizontal_go(VipsImage *in, VipsImage **out);
 
-int
-vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
-  return vips_arrayjoin(in, out, n, "across", 1, NULL);
-}
+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_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
-  return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
-}
+int vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma);
+int vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma);
 
-int
-vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int embed_profile) {
-  if (embed_profile) {
-    return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
-  }
+int vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b);
 
-  return vips_pngsave_buffer(in, buf, len, "profile", "none", "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
-}
+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_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
-  return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
-}
+int vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity);
+int vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity);
 
-int
-vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
-#if VIPS_SUPPORT_MAGICK
-  return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
-#else
-  vips_error("vips_gifsave_go", "Saving GIF is not supported");
-  return 1;
-#endif
-}
+int vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n);
 
-int
-vips_icosave_go(VipsImage *in, void **buf, size_t *len) {
-#if VIPS_SUPPORT_MAGICK
-  return vips_magicksave_buffer(in, buf, len, "format", "ico", NULL);
-#else
-  vips_error("vips_icosave_go", "Saving ICO is not supported");
-  return 1;
-#endif
-}
+int vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace);
+int vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int embed_profile);
+int vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality);
+int vips_gifsave_go(VipsImage *in, void **buf, size_t *len);
+int vips_icosave_go(VipsImage *in, void **buf, size_t *len);
 
-void
-vips_cleanup() {
-  vips_error_clear();
-  vips_thread_shutdown();
-}
+void vips_cleanup();