fix(#42): change interlace type. fix C bindings

master
Tomas Aparicio 11 years ago
commit 7be9d7bad5

@ -166,6 +166,7 @@ options := bimg.Options{
Crop: true, Crop: true,
Quality: 95, Quality: 95,
Rotate: 180, Rotate: 180,
Interlace: 1,
} }
buffer, err := bimg.Read("image.jpg") buffer, err := bimg.Read("image.jpg")
@ -625,6 +626,7 @@ type Options struct {
Flip bool Flip bool
Flop bool Flop bool
NoAutoRotate bool NoAutoRotate bool
Interlace int
Rotate Angle Rotate Angle
Gravity Gravity Gravity Gravity
Watermark Watermark Watermark Watermark

@ -88,6 +88,7 @@ type Options struct {
Flip bool Flip bool
Flop bool Flop bool
NoAutoRotate bool NoAutoRotate bool
Interlace bool
Rotate Angle Rotate Angle
Gravity Gravity Gravity Gravity
Watermark Watermark Watermark Watermark

@ -126,6 +126,7 @@ func Resize(buf []byte, o Options) ([]byte, error) {
Quality: o.Quality, Quality: o.Quality,
Type: o.Type, Type: o.Type,
Compression: o.Compression, Compression: o.Compression,
Interlace: o.Interlace,
} }
// Finally save as buffer // Finally save as buffer

@ -23,13 +23,16 @@ func DetermineImageTypeName(buf []byte) string {
// Check if a given image type is supported // Check if a given image type is supported
func IsTypeSupported(t ImageType) bool { func IsTypeSupported(t ImageType) bool {
return t == JPEG || t == PNG || t == WEBP return t == JPEG || t == PNG || t == WEBP || t == MAGICK
} }
// Check if a given image type name is supported // Check if a given image type name is supported
func IsTypeNameSupported(t string) bool { func IsTypeNameSupported(t string) bool {
return t == "jpeg" || t == "jpg" || return t == "jpeg" ||
t == "png" || t == "webp" t == "jpg" ||
t == "png" ||
t == "webp" ||
t == "magick"
} }
func getImageTypeName(code ImageType) string { func getImageTypeName(code ImageType) string {

@ -35,6 +35,7 @@ type vipsSaveOptions struct {
Quality int Quality int
Compression int Compression int
Type ImageType Type ImageType
Interlace bool
} }
type vipsWatermarkOptions struct { type vipsWatermarkOptions struct {
@ -81,7 +82,7 @@ func Initialize() {
if os.Getenv("VIPS_CONCURRENCY") == "" { if os.Getenv("VIPS_CONCURRENCY") == "" {
C.vips_concurrency_set(1) C.vips_concurrency_set(1)
} }
// Enable libvips cache tracing // Enable libvips cache tracing
if os.Getenv("VIPS_TRACE") != "" { if os.Getenv("VIPS_TRACE") != "" {
C.vips_enable_cache_set_trace() C.vips_enable_cache_set_trace()
} }
@ -223,18 +224,19 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
var ptr unsafe.Pointer var ptr unsafe.Pointer
length := C.size_t(0) length := C.size_t(0)
err := C.int(0) err := C.int(0)
interlace := C.int(boolToInt(o.Interlace))
defer C.g_object_unref(C.gpointer(image)) defer C.g_object_unref(C.gpointer(image))
switch o.Type { switch o.Type {
case PNG: case PNG:
err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0) err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), interlace)
break break
case WEBP: case WEBP:
err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality))
break break
default: default:
err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0) err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), interlace)
break break
} }
@ -358,3 +360,10 @@ func catchVipsError() error {
C.vips_thread_shutdown() C.vips_thread_shutdown()
return errors.New(s) return errors.New(s)
} }
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}

@ -129,28 +129,52 @@ vips_extract_area_bridge(VipsImage *in, VipsImage **out, int left, int top, int
return vips_extract_area(in, out, left, top, width, height, NULL); return vips_extract_area(in, out, left, top, width, height, NULL);
}; };
gboolean
with_interlace(int interlace) {
return interlace > 0 ? TRUE : FALSE;
};
int int
vips_jpegsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) vips_jpegsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace)
{ {
return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL); return vips_jpegsave_buffer(in, buf, len,
"strip", strip,
"Q", quality,
"optimize_coding", TRUE,
"interlace", with_interlace(interlace),
NULL
);
}; };
int int
vips_pngsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace) vips_pngsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace)
{ {
#if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42)) #if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42))
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression, return vips_pngsave_buffer(in, buf, len,
"interlace", interlace, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL); "strip", FALSE,
"compression", compression,
"interlace", with_interlace(interlace),
"filter", VIPS_FOREIGN_PNG_FILTER_NONE,
NULL
);
#else #else
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression, return vips_pngsave_buffer(in, buf, len,
"interlace", interlace, NULL); "strip", FALSE,
"compression", compression,
"interlace", with_interlace(interlace),
NULL
);
#endif #endif
}; };
int int
vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int quality)
{ {
return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL); return vips_webpsave_buffer(in, buf, len,
"strip", strip,
"Q", quality,
NULL
);
}; };
int int

@ -30,7 +30,7 @@ func TestVipsRead(t *testing.T) {
func TestVipsSave(t *testing.T) { func TestVipsSave(t *testing.T) {
image, _, _ := vipsRead(readImage("test.jpg")) image, _, _ := vipsRead(readImage("test.jpg"))
options := vipsSaveOptions{Quality: 95, Type: JPEG} options := vipsSaveOptions{Quality: 95, Type: JPEG, Interlace: true}
buf, err := vipsSave(image, options) buf, err := vipsSave(image, options)
if err != nil { if err != nil {

Loading…
Cancel
Save