Forráskód Böngészése

Remove C from imagetype

DarthSim 4 éve
szülő
commit
0b612da5fc
5 módosított fájl, 50 hozzáadás és 95 törlés
  1. 13 19
      imagetype/imagetype.go
  2. 0 13
      imagetype/imagetype.h
  3. 0 53
      vips/vips.c
  4. 37 8
      vips/vips.go
  5. 0 2
      vips/vips.h

+ 13 - 19
imagetype/imagetype.go

@@ -1,11 +1,5 @@
 package imagetype
 package imagetype
 
 
-/*
-#cgo LDFLAGS: -s -w
-#include "imagetype.h"
-*/
-import "C"
-
 import (
 import (
 	"fmt"
 	"fmt"
 	"net/url"
 	"net/url"
@@ -16,21 +10,21 @@ import (
 type Type int
 type Type int
 
 
 const (
 const (
-	Unknown = Type(C.UNKNOWN)
-	JPEG    = Type(C.JPEG)
-	PNG     = Type(C.PNG)
-	WEBP    = Type(C.WEBP)
-	GIF     = Type(C.GIF)
-	ICO     = Type(C.ICO)
-	SVG     = Type(C.SVG)
-	HEIC    = Type(C.HEIC)
-	AVIF    = Type(C.AVIF)
-	BMP     = Type(C.BMP)
-	TIFF    = Type(C.TIFF)
-
-	contentDispositionFilenameFallback = "image"
+	Unknown Type = iota
+	JPEG
+	PNG
+	WEBP
+	GIF
+	ICO
+	SVG
+	HEIC
+	AVIF
+	BMP
+	TIFF
 )
 )
 
 
+const contentDispositionFilenameFallback = "image"
+
 var (
 var (
 	Types = map[string]Type{
 	Types = map[string]Type{
 		"jpeg": JPEG,
 		"jpeg": JPEG,

+ 0 - 13
imagetype/imagetype.h

@@ -1,13 +0,0 @@
-enum ImgproxyImageTypes {
-  UNKNOWN = 0,
-  JPEG,
-  PNG,
-  WEBP,
-  GIF,
-  ICO,
-  SVG,
-  HEIC,
-  AVIF,
-  BMP,
-  TIFF
-};

+ 0 - 53
vips/vips.c

@@ -31,59 +31,6 @@ swap_and_clear(VipsImage **in, VipsImage *out) {
   *in = out;
   *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");
-  case (HEIC):
-    return vips_type_find("VipsOperation", "heifload_buffer");
-  case (AVIF):
-    return vips_type_find("VipsOperation", "heifload_buffer");
-  case (BMP):
-    return vips_type_find("VipsOperation", "magickload_buffer");
-  case (TIFF):
-    return vips_type_find("VipsOperation", "tiffload_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");
-#if VIPS_SUPPORT_AVIF
-  case (AVIF):
-    return vips_type_find("VipsOperation", "heifsave_buffer");
-#endif
-  case (ICO):
-    return vips_type_find("VipsOperation", "pngsave_buffer");
-  case (BMP):
-    return vips_type_find("VipsOperation", "magicksave_buffer");
-  case (TIFF):
-    return vips_type_find("VipsOperation", "tiffsave_buffer");
-  }
-
-  return 0;
-}
-
 int
 int
 vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
 vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
   if (shrink > 1)
   if (shrink > 1)

+ 37 - 8
vips/vips.go

@@ -116,16 +116,36 @@ func Error() error {
 	return ierrors.NewUnexpected(C.GoString(C.vips_error_buffer()), 1)
 	return ierrors.NewUnexpected(C.GoString(C.vips_error_buffer()), 1)
 }
 }
 
 
+func hasOperation(name string) bool {
+	return C.vips_type_find(cachedCString("VipsOperation"), cachedCString(name)) != 0
+}
+
 func SupportsLoad(it imagetype.Type) bool {
 func SupportsLoad(it imagetype.Type) bool {
 	if sup, ok := typeSupportLoad[it]; ok {
 	if sup, ok := typeSupportLoad[it]; ok {
 		return sup
 		return sup
 	}
 	}
 
 
 	sup := false
 	sup := false
-	if it == imagetype.ICO {
+
+	switch it {
+	case imagetype.JPEG:
+		sup = hasOperation("jpegload_buffer")
+	case imagetype.PNG:
+		sup = hasOperation("pngload_buffer")
+	case imagetype.WEBP:
+		sup = hasOperation("webpload_buffer")
+	case imagetype.GIF:
+		sup = hasOperation("gifload_buffer")
+	case imagetype.ICO:
 		sup = true
 		sup = true
-	} else {
-		sup = int(C.vips_type_find_load_go(C.int(it))) != 0
+	case imagetype.SVG:
+		sup = hasOperation("svgload_buffer")
+	case imagetype.HEIC, imagetype.AVIF:
+		sup = hasOperation("heifload_buffer")
+	case imagetype.BMP:
+		sup = hasOperation("magickload_buffer")
+	case imagetype.TIFF:
+		sup = hasOperation("tiffload_buffer")
 	}
 	}
 
 
 	typeSupportLoad[it] = sup
 	typeSupportLoad[it] = sup
@@ -139,11 +159,20 @@ func SupportsSave(it imagetype.Type) bool {
 	}
 	}
 
 
 	sup := false
 	sup := false
-	if it == imagetype.ICO {
-		// We save ICO content as PNG so we need to check it
-		sup = int(C.vips_type_find_save_go(C.int(imagetype.PNG))) != 0
-	} else {
-		sup = int(C.vips_type_find_save_go(C.int(it))) != 0
+
+	switch it {
+	case imagetype.JPEG:
+		return hasOperation("jpegsave_buffer")
+	case imagetype.PNG, imagetype.ICO:
+		return hasOperation("pngsave_buffer")
+	case imagetype.WEBP:
+		return hasOperation("webpsave_buffer")
+	case imagetype.GIF, imagetype.BMP:
+		return hasOperation("magicksave_buffer")
+	case imagetype.AVIF:
+		return hasOperation("heifsave_buffer")
+	case imagetype.TIFF:
+		return hasOperation("tiffsave_buffer")
 	}
 	}
 
 
 	typeSupportSave[it] = sup
 	typeSupportSave[it] = sup

+ 0 - 2
vips/vips.h

@@ -4,8 +4,6 @@
 #include <vips/vips7compat.h>
 #include <vips/vips7compat.h>
 #include <vips/vector.h>
 #include <vips/vector.h>
 
 
-#include "../imagetype/imagetype.h"
-
 int vips_initialize();
 int vips_initialize();
 
 
 void clear_image(VipsImage **in);
 void clear_image(VipsImage **in);