refactor(colourspace)

master
Tomas Aparicio 11 years ago
parent fa55264a97
commit 851e65e71e

@ -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

@ -272,16 +272,26 @@ func TestInterpretation(t *testing.T) {
}
}
func TestImageColourspaceBW(t *testing.T) {
buf, err := initImage("test.jpg").Colourspace(INTERPRETATION_B_W)
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 != INTERPRETATION_B_W {
if interpretation != test.interpretation {
t.Errorf("Invalid colourspace")
}
}
}
func TestImageColourspaceIsSupported(t *testing.T) {

@ -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"

@ -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
}

Loading…
Cancel
Save