mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-11 08:20:34 -07:00
refactor(colourspace)
This commit is contained in:
parent
fa55264a97
commit
851e65e71e
4 changed files with 42 additions and 11 deletions
19
README.md
19
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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
func TestImageColourspace(t *testing.T) {
|
||||
tests := []struct {
|
||||
file string
|
||||
interpretation Interpretation
|
||||
}{
|
||||
{"test.jpg", INTERPRETATION_sRGB},
|
||||
{"test.jpg", INTERPRETATION_B_W},
|
||||
}
|
||||
|
||||
interpretation, err := ImageInterpretation(buf)
|
||||
if interpretation != INTERPRETATION_B_W {
|
||||
t.Errorf("Invalid colourspace")
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
8
vips.go
8
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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue