From 39931145dec88f4ead9ac0a96c3991f4eb3f80e1 Mon Sep 17 00:00:00 2001 From: Tomas Aparicio Date: Wed, 8 Apr 2015 00:58:26 +0200 Subject: [PATCH] feat(test): vips --- vips.go | 76 ++++++++++++++++++++++++++-------------------------- vips_test.go | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 38 deletions(-) create mode 100644 vips_test.go diff --git a/vips.go b/vips.go index 33f8933..29278a4 100644 --- a/vips.go +++ b/vips.go @@ -19,6 +19,12 @@ var ( initialized bool = false ) +type vipsSaveOptions struct { + Quality int + Compression int + Type ImageType +} + func init() { if C.VIPS_MAJOR_VERSION <= 7 && C.VIPS_MINOR_VERSION < 40 { panic("unsupported old vips version!") @@ -109,6 +115,38 @@ func vipsRead(buf []byte) (*C.struct__VipsImage, ImageType, error) { return image, imageType, nil } +func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) { + var ptr unsafe.Pointer + length := C.size_t(0) + err := C.int(0) + + defer C.g_object_unref(C.gpointer(image)) + + switch { + case o.Type == PNG: + err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0) + break + case o.Type == WEBP: + err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) + break + default: + err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) + break + } + + if int(err) != 0 { + return nil, catchVipsError() + } + + buf := C.GoBytes(ptr, C.int(length)) + + // Cleanup + C.g_free(C.gpointer(ptr)) + C.vips_error_clear() + + return buf, nil +} + func vipsExtract(image *C.struct__VipsImage, left, top, width, height int) (*C.struct__VipsImage, error) { var buf *C.struct__VipsImage defer C.g_object_unref(C.gpointer(image)) @@ -207,44 +245,6 @@ func vipsImageType(buf []byte) ImageType { return imageType } -type vipsSaveOptions struct { - Quality int - Compression int - Type ImageType -} - -func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) { - var ptr unsafe.Pointer - length := C.size_t(0) - err := C.int(0) - - defer C.g_object_unref(C.gpointer(image)) - - switch { - case o.Type == PNG: - err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0) - break - case o.Type == WEBP: - err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) - break - default: - err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) - break - } - - if int(err) != 0 { - return nil, catchVipsError() - } - - buf := C.GoBytes(ptr, C.int(length)) - - // Cleanup - C.g_free(C.gpointer(ptr)) - C.vips_error_clear() - - return buf, nil -} - func vipsExifOrientation(image *C.struct__VipsImage) int { return int(C.vips_exif_orientation(image)) } diff --git a/vips_test.go b/vips_test.go new file mode 100644 index 0000000..533e810 --- /dev/null +++ b/vips_test.go @@ -0,0 +1,54 @@ +package bimg + +import ( + "io/ioutil" + "os" + "path" + "testing" +) + +func TestVipsRead(t *testing.T) { + files := []struct { + name string + expected ImageType + }{ + {"test.jpg", JPEG}, + {"test.png", PNG}, + {"test.webp", WEBP}, + } + + for _, file := range files { + img, _ := os.Open(path.Join("fixtures", file.name)) + buf, _ := ioutil.ReadAll(img) + defer img.Close() + + image, imageType, _ := vipsRead(buf) + if image == nil { + t.Fatal("Empty image") + } + if imageType != file.expected { + t.Fatal("Empty image") + } + } +} + +func TestVipsSave(t *testing.T) { + img, _ := os.Open(path.Join("fixtures", "test.jpg")) + buf, _ := ioutil.ReadAll(img) + defer img.Close() + + image, _, _ := vipsRead(buf) + if image == nil { + t.Fatal("Empty image") + } + + options := vipsSaveOptions{Quality: 95, Type: JPEG} + + buf, err := vipsSave(image, options) + if err != nil { + t.Fatal("Cannot save the image") + } + if len(buf) == 0 { + t.Fatal("Empty image") + } +}