diff --git a/README.md b/README.md index c70df2e..2a23a26 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,7 @@ options := bimg.Options{ Crop: true, Quality: 95, Rotate: 180, + Interlace: 1, } buffer, err := bimg.Read("image.jpg") @@ -625,6 +626,7 @@ type Options struct { Flip bool Flop bool NoAutoRotate bool + Interlace int Rotate Angle Gravity Gravity Watermark Watermark diff --git a/options.go b/options.go index 4495a55..3485de0 100644 --- a/options.go +++ b/options.go @@ -88,6 +88,7 @@ type Options struct { Flip bool Flop bool NoAutoRotate bool + Interlace bool Rotate Angle Gravity Gravity Watermark Watermark diff --git a/resize.go b/resize.go index 36bb10a..a89583c 100644 --- a/resize.go +++ b/resize.go @@ -126,6 +126,7 @@ func Resize(buf []byte, o Options) ([]byte, error) { Quality: o.Quality, Type: o.Type, Compression: o.Compression, + Interlace: o.Interlace, } // Finally save as buffer diff --git a/type.go b/type.go index 295dc61..3be6dd4 100644 --- a/type.go +++ b/type.go @@ -23,13 +23,16 @@ func DetermineImageTypeName(buf []byte) string { // Check if a given image type is supported func IsTypeSupported(t ImageType) bool { - return t == JPEG || t == PNG || t == WEBP + return t == JPEG || t == PNG || t == WEBP || t == MAGICK } // Check if a given image type name is supported func IsTypeNameSupported(t string) bool { - return t == "jpeg" || t == "jpg" || - t == "png" || t == "webp" + return t == "jpeg" || + t == "jpg" || + t == "png" || + t == "webp" || + t == "magick" } func getImageTypeName(code ImageType) string { diff --git a/vips.go b/vips.go index 2c89e76..d43646c 100644 --- a/vips.go +++ b/vips.go @@ -35,6 +35,7 @@ type vipsSaveOptions struct { Quality int Compression int Type ImageType + Interlace bool } type vipsWatermarkOptions struct { @@ -81,7 +82,7 @@ func Initialize() { if os.Getenv("VIPS_CONCURRENCY") == "" { C.vips_concurrency_set(1) } - // Enable libvips cache tracing + // Enable libvips cache tracing if os.Getenv("VIPS_TRACE") != "" { C.vips_enable_cache_set_trace() } @@ -223,18 +224,19 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) { var ptr unsafe.Pointer length := C.size_t(0) err := C.int(0) + interlace := C.int(boolToInt(o.Interlace)) defer C.g_object_unref(C.gpointer(image)) switch o.Type { case PNG: - err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0) + err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), interlace) break case WEBP: - err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) + err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality)) break default: - err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) + err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), interlace) break } @@ -358,3 +360,10 @@ func catchVipsError() error { C.vips_thread_shutdown() return errors.New(s) } + +func boolToInt(b bool) int { + if b { + return 1 + } + return 0 +} diff --git a/vips.h b/vips.h index 37bcdde..f24dadf 100644 --- a/vips.h +++ b/vips.h @@ -129,28 +129,52 @@ vips_extract_area_bridge(VipsImage *in, VipsImage **out, int left, int top, int return vips_extract_area(in, out, left, top, width, height, NULL); }; +gboolean +with_interlace(int interlace) { + return interlace > 0 ? TRUE : FALSE; +}; + int vips_jpegsave_bridge(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); + return vips_jpegsave_buffer(in, buf, len, + "strip", strip, + "Q", quality, + "optimize_coding", TRUE, + "interlace", with_interlace(interlace), + NULL + ); }; int vips_pngsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace) { #if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42)) - return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression, - "interlace", interlace, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL); + return vips_pngsave_buffer(in, buf, len, + "strip", FALSE, + "compression", compression, + "interlace", with_interlace(interlace), + "filter", VIPS_FOREIGN_PNG_FILTER_NONE, + NULL + ); #else - return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression, - "interlace", interlace, NULL); + return vips_pngsave_buffer(in, buf, len, + "strip", FALSE, + "compression", compression, + "interlace", with_interlace(interlace), + NULL + ); #endif }; int -vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) +vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality) { - return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL); + return vips_webpsave_buffer(in, buf, len, + "strip", strip, + "Q", quality, + NULL + ); }; int diff --git a/vips_test.go b/vips_test.go index 5eab2c3..b448d69 100644 --- a/vips_test.go +++ b/vips_test.go @@ -30,7 +30,7 @@ func TestVipsRead(t *testing.T) { func TestVipsSave(t *testing.T) { image, _, _ := vipsRead(readImage("test.jpg")) - options := vipsSaveOptions{Quality: 95, Type: JPEG} + options := vipsSaveOptions{Quality: 95, Type: JPEG, Interlace: true} buf, err := vipsSave(image, options) if err != nil {