|
|
|
@ -224,41 +224,85 @@ func vipsRead(buf []byte) (*C.struct__VipsImage, ImageType, error) {
|
|
|
|
return image, imageType, nil
|
|
|
|
return image, imageType, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
|
|
|
func vipsColourspaceIsSupportedBuffer(buf []byte) (bool, error) {
|
|
|
|
length := C.size_t(0)
|
|
|
|
image, _, err := vipsRead(buf)
|
|
|
|
err := C.int(0)
|
|
|
|
defer C.g_object_unref(C.gpointer(image))
|
|
|
|
interlace := C.int(boolToInt(o.Interlace))
|
|
|
|
if err != nil {
|
|
|
|
if o.Interpretation == 0 {
|
|
|
|
return false, err
|
|
|
|
o.Interpretation = sRGB
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
interpretation := C.VipsInterpretation(o.Interpretation)
|
|
|
|
return vipsColourspaceIsSupported(image), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func vipsColourspaceIsSupported(image *C.struct__VipsImage) bool {
|
|
|
|
|
|
|
|
return int(C.vips_colourspace_issupported_bridge(image)) == 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 vipsInterpretation(image), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func vipsInterpretation(image *C.struct__VipsImage) Interpretation {
|
|
|
|
|
|
|
|
return Interpretation(C.vips_image_guess_interpretation_bridge(image))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func vipsPreSave(image *C.struct__VipsImage, o *vipsSaveOptions) (*C.struct__VipsImage, error) {
|
|
|
|
// Remove ICC profile metadata
|
|
|
|
// Remove ICC profile metadata
|
|
|
|
if o.NoProfile {
|
|
|
|
if o.NoProfile {
|
|
|
|
C.remove_profile(image)
|
|
|
|
C.remove_profile(image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Force RGB color space
|
|
|
|
// Use a default interpretation and cast it to C type
|
|
|
|
|
|
|
|
if o.Interpretation == 0 {
|
|
|
|
|
|
|
|
o.Interpretation = INTERPRETATION_sRGB
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
interpretation := C.VipsInterpretation(o.Interpretation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Apply the proper colour space
|
|
|
|
var outImage *C.struct__VipsImage
|
|
|
|
var outImage *C.struct__VipsImage
|
|
|
|
C.vips_colourspace_bridge(image, &outImage, interpretation)
|
|
|
|
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()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
image = outImage
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return image, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
|
|
|
defer C.g_object_unref(C.gpointer(image))
|
|
|
|
defer C.g_object_unref(C.gpointer(image))
|
|
|
|
defer C.g_object_unref(C.gpointer(outImage))
|
|
|
|
|
|
|
|
|
|
|
|
image, err := vipsPreSave(image, &o)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
length := C.size_t(0)
|
|
|
|
|
|
|
|
saveErr := C.int(0)
|
|
|
|
|
|
|
|
interlace := C.int(boolToInt(o.Interlace))
|
|
|
|
|
|
|
|
quality := C.int(o.Quality)
|
|
|
|
|
|
|
|
|
|
|
|
var ptr unsafe.Pointer
|
|
|
|
var ptr unsafe.Pointer
|
|
|
|
switch o.Type {
|
|
|
|
switch o.Type {
|
|
|
|
case PNG:
|
|
|
|
|
|
|
|
err = C.vips_pngsave_bridge(outImage, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), interlace)
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case WEBP:
|
|
|
|
case WEBP:
|
|
|
|
err = C.vips_webpsave_bridge(outImage, &ptr, &length, 1, C.int(o.Quality))
|
|
|
|
saveErr = C.vips_webpsave_bridge(image, &ptr, &length, 1, quality)
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case PNG:
|
|
|
|
|
|
|
|
saveErr = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), quality, interlace)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
err = C.vips_jpegsave_bridge(outImage, &ptr, &length, 1, C.int(o.Quality), interlace)
|
|
|
|
saveErr = C.vips_jpegsave_bridge(image, &ptr, &length, 1, quality, interlace)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if int(err) != 0 {
|
|
|
|
if int(saveErr) != 0 {
|
|
|
|
return nil, catchVipsError()
|
|
|
|
return nil, catchVipsError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|