mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-07 21:48:13 -08:00
Adding support for TIFF save.
Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
This commit is contained in:
parent
190fe92e28
commit
dcd91c85e0
3 changed files with 62 additions and 18 deletions
30
vips.go
30
vips.go
|
|
@ -168,6 +168,25 @@ func VipsIsTypeSupported(t ImageType) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VipsIsTypeSupportedSave returns true if the given image type
|
||||||
|
// is supported by the current libvips compilation for the
|
||||||
|
// save operation.
|
||||||
|
func VipsIsTypeSupportedSave(t ImageType) bool {
|
||||||
|
if t == JPEG {
|
||||||
|
return int(C.vips_type_find_save_bridge(C.JPEG)) != 0
|
||||||
|
}
|
||||||
|
if t == WEBP {
|
||||||
|
return int(C.vips_type_find_save_bridge(C.WEBP)) != 0
|
||||||
|
}
|
||||||
|
if t == PNG {
|
||||||
|
return int(C.vips_type_find_save_bridge(C.PNG)) != 0
|
||||||
|
}
|
||||||
|
if t == TIFF {
|
||||||
|
return int(C.vips_type_find_save_bridge(C.TIFF)) != 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func vipsExifOrientation(image *C.VipsImage) int {
|
func vipsExifOrientation(image *C.VipsImage) int {
|
||||||
return int(C.vips_exif_orientation(image))
|
return int(C.vips_exif_orientation(image))
|
||||||
}
|
}
|
||||||
|
|
@ -367,18 +386,19 @@ func vipsSave(image *C.VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
interlace := C.int(boolToInt(o.Interlace))
|
interlace := C.int(boolToInt(o.Interlace))
|
||||||
quality := C.int(o.Quality)
|
quality := C.int(o.Quality)
|
||||||
|
|
||||||
|
if o.Type != 0 && !VipsIsTypeSupportedSave(o.Type) {
|
||||||
|
return nil, fmt.Errorf("VIPS cannot save to %#v", ImageTypes[o.Type])
|
||||||
|
}
|
||||||
var ptr unsafe.Pointer
|
var ptr unsafe.Pointer
|
||||||
switch o.Type {
|
switch o.Type {
|
||||||
case WEBP:
|
case WEBP:
|
||||||
saveErr = C.vips_webpsave_bridge(tmpImage, &ptr, &length, 1, quality)
|
saveErr = C.vips_webpsave_bridge(tmpImage, &ptr, &length, 1, quality)
|
||||||
case PNG:
|
case PNG:
|
||||||
saveErr = C.vips_pngsave_bridge(tmpImage, &ptr, &length, 1, C.int(o.Compression), quality, interlace)
|
saveErr = C.vips_pngsave_bridge(tmpImage, &ptr, &length, 1, C.int(o.Compression), quality, interlace)
|
||||||
case 0:
|
case TIFF:
|
||||||
saveErr = C.vips_jpegsave_bridge(tmpImage, &ptr, &length, 1, quality, interlace)
|
saveErr = C.vips_tiffsave_bridge(tmpImage, &ptr, &length)
|
||||||
case JPEG:
|
|
||||||
saveErr = C.vips_jpegsave_bridge(tmpImage, &ptr, &length, 1, quality, interlace)
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("VIPS cannot save to %v.", ImageTypes[o.Type])
|
saveErr = C.vips_jpegsave_bridge(tmpImage, &ptr, &length, 1, quality, interlace)
|
||||||
}
|
}
|
||||||
|
|
||||||
if int(saveErr) != 0 {
|
if int(saveErr) != 0 {
|
||||||
|
|
|
||||||
28
vips.h
28
vips.h
|
|
@ -144,6 +144,23 @@ vips_type_find_bridge(int t) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
vips_type_find_save_bridge(int t) {
|
||||||
|
if (t == TIFF) {
|
||||||
|
return vips_type_find("VipsOperation", "tiffsave_buffer");
|
||||||
|
}
|
||||||
|
if (t == WEBP) {
|
||||||
|
return vips_type_find("VipsOperation", "webpsave_buffer");
|
||||||
|
}
|
||||||
|
if (t == PNG) {
|
||||||
|
return vips_type_find("VipsOperation", "pngsave_buffer");
|
||||||
|
}
|
||||||
|
if (t == JPEG) {
|
||||||
|
return vips_type_find("VipsOperation", "jpegsave_buffer");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_rotate(VipsImage *in, VipsImage **out, int angle) {
|
vips_rotate(VipsImage *in, VipsImage **out, int angle) {
|
||||||
int rotate = VIPS_ANGLE_D0;
|
int rotate = VIPS_ANGLE_D0;
|
||||||
|
|
@ -276,6 +293,17 @@ vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int qual
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
vips_tiffsave_bridge(VipsImage *in, void **buf, size_t *len) {
|
||||||
|
#if (VIPS_MAJOR_VERSION >= 8 && VIPS_MINOR_VERSION >= 5)
|
||||||
|
return vips_tiffsave_buffer(in, buf, len,
|
||||||
|
NULL
|
||||||
|
);
|
||||||
|
#else
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_is_16bit (VipsInterpretation interpretation) {
|
vips_is_16bit (VipsInterpretation interpretation) {
|
||||||
return interpretation == VIPS_INTERPRETATION_RGB16 || interpretation == VIPS_INTERPRETATION_GREY16;
|
return interpretation == VIPS_INTERPRETATION_RGB16 || interpretation == VIPS_INTERPRETATION_GREY16;
|
||||||
|
|
|
||||||
22
vips_test.go
22
vips_test.go
|
|
@ -45,20 +45,16 @@ func TestVipsSave(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVipsCannotSave(t *testing.T) {
|
func TestVipsSaveTiff(t *testing.T) {
|
||||||
types := [...]ImageType{GIF, MAGICK, PDF, SVG, TIFF}
|
if !VipsIsTypeSupportedSave(TIFF) {
|
||||||
|
t.Skipf("Format %#v is not supported", ImageTypes[TIFF])
|
||||||
|
}
|
||||||
|
image, _, _ := vipsRead(readImage("test.jpg"))
|
||||||
|
options := vipsSaveOptions{Quality: 95, Type: TIFF}
|
||||||
|
buf, _ := vipsSave(image, options)
|
||||||
|
|
||||||
for _, typ := range types {
|
if len(buf) == 0 {
|
||||||
image, _, _ := vipsRead(readImage("test.jpg"))
|
t.Fatalf("Empty saved '%v' image", ImageTypes[TIFF])
|
||||||
options := vipsSaveOptions{Quality: 95, Type: typ}
|
|
||||||
|
|
||||||
buf, err := vipsSave(image, options)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("Format '%v' shouldn't be supported", ImageTypes[typ])
|
|
||||||
}
|
|
||||||
if len(buf) != 0 {
|
|
||||||
t.Fatalf("'%v' image is not empty", ImageTypes[typ])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue