From c1e0c73d50ab4464344fa56acac0154f681550ca Mon Sep 17 00:00:00 2001 From: Tomas Aparicio Date: Tue, 28 Jul 2015 21:29:47 +0100 Subject: [PATCH] feat(#49) --- vips.go | 53 ++++++++++++++++++++++++++++------------------------- vips.h | 6 ++++++ 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/vips.go b/vips.go index f41e8de..04f44b4 100644 --- a/vips.go +++ b/vips.go @@ -15,6 +15,8 @@ import ( "unsafe" ) +const HasMagickSupport = int(C.VIPS_MAGICK_SUPPORT) == 1 + const ( maxCacheMem = 100 * 1024 * 1024 maxCacheSize = 500 @@ -384,36 +386,37 @@ func vipsAffine(input *C.struct__VipsImage, residualx, residualy float64, i Inte return image, nil } -func vipsImageType(buf []byte) ImageType { - imageType := UNKNOWN +func vipsImageType(bytes []byte) ImageType { + if len(bytes) == 0 { + return UNKNOWN + } - if len(buf) == 0 { - return imageType + if bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47 { + return PNG + } + if bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF { + return JPEG + } + if bytes[8] == 0x57 && bytes[9] == 0x45 && bytes[10] == 0x42 && bytes[11] == 0x50 { + return WEBP } + if (bytes[0] == 0x49 && bytes[1] == 0x49 && bytes[2] == 0x2A && bytes[3] == 0x0) || + (bytes[0] == 0x4D && bytes[1] == 0x4D && bytes[2] == 0x0 && bytes[3] == 0x2A) { + return TIFF + } + if HasMagickSupport && strings.HasSuffix(readImageType(bytes), "MagickBuffer") { + return MAGICK + } + + return UNKNOWN +} +func readImageType(buf []byte) string { length := C.size_t(len(buf)) imageBuf := unsafe.Pointer(&buf[0]) - bufferType := C.GoString(C.vips_foreign_find_load_buffer(imageBuf, length)) - - switch { - case strings.HasSuffix(bufferType, "JpegBuffer"): - imageType = JPEG - break - case strings.HasSuffix(bufferType, "PngBuffer"): - imageType = PNG - break - case strings.HasSuffix(bufferType, "TiffBuffer"): - imageType = TIFF - break - case strings.HasSuffix(bufferType, "WebpBuffer"): - imageType = WEBP - break - case strings.HasSuffix(bufferType, "MagickBuffer"): - imageType = MAGICK - break - } - - return imageType + load := C.vips_foreign_find_load_buffer(imageBuf, length) + defer C.free(imageBuf) + return C.GoString(load) } func catchVipsError() error { diff --git a/vips.h b/vips.h index 1948907..a97fe20 100644 --- a/vips.h +++ b/vips.h @@ -2,6 +2,12 @@ #include #include +#ifdef VIPS_MAGICK_H +#define VIPS_MAGICK_SUPPORT 1 +#else +#define VIPS_MAGICK_SUPPORT 0 +#endif + enum types { UNKNOWN = 0, JPEG,