コード例 #1
0
ファイル: vips.go プロジェクト: routing/bimg
func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) {
	// Remove ICC profile metadata
	if o.NoProfile {
		C.remove_profile(image)
	}

	// 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.VipsImage
	if vipsColourspaceIsSupported(image) {
		err := int(C.vips_colourspace_bridge(image, &outImage, interpretation))
		if err != 0 {
			return nil, catchVipsError()
		}
		C.g_object_unref(C.gpointer(image))
		image = outImage
	}

	return image, nil
}
コード例 #2
0
ファイル: vips.go プロジェクト: slav123/bimg
func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
	length := C.size_t(0)
	err := C.int(0)
	interlace := C.int(boolToInt(o.Interlace))

	// Remove ICC profile metadata
	if o.NoProfile {
		C.remove_profile(image)
	}

	// Force RGB color space
	var outImage *C.struct__VipsImage
	C.vips_colourspace_bridge(image, &outImage)

	defer C.g_object_unref(C.gpointer(image))
	defer C.g_object_unref(C.gpointer(outImage))

	var ptr unsafe.Pointer
	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:
		err = C.vips_webpsave_bridge(outImage, &ptr, &length, 1, C.int(o.Quality))
		break
	default:
		err = C.vips_jpegsave_bridge(outImage, &ptr, &length, 1, C.int(o.Quality), interlace)
		break
	}

	if int(err) != 0 {
		return nil, catchVipsError()
	}

	buf := C.GoBytes(ptr, C.int(length))

	// Clean up
	C.g_free(C.gpointer(ptr))
	C.vips_error_clear()

	return buf, nil
}