diff --git a/README.md b/README.md index c4dc79a..6779cac 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,25 @@ if size.Width != 1000 || size.Height != 500 { } ``` +#### Custom colour space (black & white) + +```go +buffer, err := bimg.Read("image.jpg") +if err != nil { + fmt.Fprintln(os.Stderr, err) +} + +newImage, err := bimg.NewImage(buffer).Colourspace(bimg.INTERPRETATION_B_W) +if err != nil { + fmt.Fprintln(os.Stderr, err) +} + +colourSpace, _ := bimg.ImageInterpretation(newImage) +if colourSpace != bimg.INTERPRETATION_B_W { + fmt.Fprintln(os.Stderr, "Invalid colour space") +} +``` + #### Custom options See [Options](https://godoc.org/github.com/h2non/bimg#Options) struct to discover all the available fields diff --git a/image_test.go b/image_test.go index b837dc7..fb20c62 100644 --- a/image_test.go +++ b/image_test.go @@ -272,15 +272,25 @@ func TestInterpretation(t *testing.T) { } } -func TestImageColourspaceBW(t *testing.T) { - buf, err := initImage("test.jpg").Colourspace(INTERPRETATION_B_W) - if err != nil { - t.Errorf("Cannot process the image: %#v", err) - } - - interpretation, err := ImageInterpretation(buf) - if interpretation != INTERPRETATION_B_W { - t.Errorf("Invalid colourspace") +func TestImageColourspace(t *testing.T) { + tests := []struct { + file string + interpretation Interpretation + }{ + {"test.jpg", INTERPRETATION_sRGB}, + {"test.jpg", INTERPRETATION_B_W}, + } + + for _, test := range tests { + buf, err := initImage(test.file).Colourspace(test.interpretation) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + + interpretation, err := ImageInterpretation(buf) + if interpretation != test.interpretation { + t.Errorf("Invalid colourspace") + } } } diff --git a/options.go b/options.go index 8cf00cf..67af96d 100644 --- a/options.go +++ b/options.go @@ -69,6 +69,8 @@ const ( INTERPRETATION_RGB16 Interpretation = C.VIPS_INTERPRETATION_RGB16 INTERPRETATION_GREY16 Interpretation = C.VIPS_INTERPRETATION_GREY16 INTERPRETATION_scRGB Interpretation = C.VIPS_INTERPRETATION_scRGB + INTERPRETATION_LAB Interpretation = C.VIPS_INTERPRETATION_LAB + INTERPRETATION_XYZ Interpretation = C.VIPS_INTERPRETATION_XYZ ) const WATERMARK_FONT = "sans 10" diff --git a/vips.go b/vips.go index d9504a7..f41e8de 100644 --- a/vips.go +++ b/vips.go @@ -226,10 +226,10 @@ func vipsRead(buf []byte) (*C.struct__VipsImage, ImageType, error) { func vipsColourspaceIsSupportedBuffer(buf []byte) (bool, error) { image, _, err := vipsRead(buf) - defer C.g_object_unref(C.gpointer(image)) if err != nil { return false, err } + C.g_object_unref(C.gpointer(image)) return vipsColourspaceIsSupported(image), nil } @@ -239,10 +239,10 @@ func vipsColourspaceIsSupported(image *C.struct__VipsImage) bool { func vipsInterpretationBuffer(buf []byte) (Interpretation, error) { image, _, err := vipsRead(buf) - defer C.g_object_unref(C.gpointer(image)) if err != nil { - return Interpretation(-1), err + return INTERPRETATION_ERROR, err } + C.g_object_unref(C.gpointer(image)) return vipsInterpretation(image), nil } @@ -266,10 +266,10 @@ func vipsPreSave(image *C.struct__VipsImage, o *vipsSaveOptions) (*C.struct__Vip var outImage *C.struct__VipsImage if vipsColourspaceIsSupported(image) { err := int(C.vips_colourspace_bridge(image, &outImage, interpretation)) - C.g_object_unref(C.gpointer(image)) if err != 0 { return nil, catchVipsError() } + C.g_object_unref(C.gpointer(image)) image = outImage }