From 31a54299fb2f2aa0194e2ae92187969b1fe5446e Mon Sep 17 00:00:00 2001 From: James Schofield Date: Thu, 3 Nov 2016 17:44:53 -0400 Subject: [PATCH 1/2] Handle 16-bit PNGs --- resize.go | 17 ++++------------- vips.go | 11 +++++++++++ vips.h | 3 +-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/resize.go b/resize.go index 8b5b8df..a8e0aeb 100644 --- a/resize.go +++ b/resize.go @@ -112,11 +112,8 @@ func Resize(buf []byte, o Options) ([]byte, error) { return nil, err } - // Flatten image on a background, if necessary - image, err = imageFlatten(image, imageType, o) - if err != nil { - return nil, err - } + // Only flatten PNG for now + flatten := imageType == PNG && o.Background != ColorBlack saveOptions := vipsSaveOptions{ Quality: o.Quality, @@ -125,6 +122,8 @@ func Resize(buf []byte, o Options) ([]byte, error) { Interlace: o.Interlace, NoProfile: o.NoProfile, Interpretation: o.Interpretation, + Flatten: flatten, + Background: o.Background, } // Finally get the resultant buffer @@ -325,14 +324,6 @@ func watermakImage(image *C.VipsImage, w Watermark) (*C.VipsImage, error) { return image, nil } -func imageFlatten(image *C.VipsImage, imageType ImageType, o Options) (*C.VipsImage, error) { - // Only PNG images are supported for now - if imageType != PNG || o.Background == ColorBlack { - return image, nil - } - return vipsFlattenBackground(image, o.Background) -} - func zoomImage(image *C.VipsImage, zoom int) (*C.VipsImage, error) { if zoom == 0 { return image, nil diff --git a/vips.go b/vips.go index cbf2304..644bcf6 100644 --- a/vips.go +++ b/vips.go @@ -55,6 +55,8 @@ type vipsSaveOptions struct { Interlace bool NoProfile bool Interpretation Interpretation + Flatten bool + Background Color } type vipsWatermarkOptions struct { @@ -341,6 +343,15 @@ func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) { image = outImage } + // Flatten image on a background, if necessary + if o.Flatten { + var err error + if outImage, err = vipsFlattenBackground(image, o.Background); err != nil { + return nil, err + } + image = outImage + } + return image, nil } diff --git a/vips.h b/vips.h index 371de43..3e4a1a4 100644 --- a/vips.h +++ b/vips.h @@ -287,8 +287,7 @@ vips_flatten_background_brigde(VipsImage *in, VipsImage **out, double r, double VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3); return vips_flatten(in, out, - "background", vipsBackground, - "max_alpha", vips_is_16bit(in->Type) ? 65535.0 : 255.0, + "background", vipsBackground, "max_alpha", 255.0, NULL ); } From a1b9efdb97aebb716b2415742f7bfeb065da3a9c Mon Sep 17 00:00:00 2001 From: James Schofield Date: Thu, 10 Nov 2016 15:15:52 -0500 Subject: [PATCH 2/2] Cleaner fix --- resize.go | 17 +++++++++++++---- vips.go | 11 ----------- vips.h | 9 ++++++++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/resize.go b/resize.go index a8e0aeb..8b5b8df 100644 --- a/resize.go +++ b/resize.go @@ -112,8 +112,11 @@ func Resize(buf []byte, o Options) ([]byte, error) { return nil, err } - // Only flatten PNG for now - flatten := imageType == PNG && o.Background != ColorBlack + // Flatten image on a background, if necessary + image, err = imageFlatten(image, imageType, o) + if err != nil { + return nil, err + } saveOptions := vipsSaveOptions{ Quality: o.Quality, @@ -122,8 +125,6 @@ func Resize(buf []byte, o Options) ([]byte, error) { Interlace: o.Interlace, NoProfile: o.NoProfile, Interpretation: o.Interpretation, - Flatten: flatten, - Background: o.Background, } // Finally get the resultant buffer @@ -324,6 +325,14 @@ func watermakImage(image *C.VipsImage, w Watermark) (*C.VipsImage, error) { return image, nil } +func imageFlatten(image *C.VipsImage, imageType ImageType, o Options) (*C.VipsImage, error) { + // Only PNG images are supported for now + if imageType != PNG || o.Background == ColorBlack { + return image, nil + } + return vipsFlattenBackground(image, o.Background) +} + func zoomImage(image *C.VipsImage, zoom int) (*C.VipsImage, error) { if zoom == 0 { return image, nil diff --git a/vips.go b/vips.go index 644bcf6..cbf2304 100644 --- a/vips.go +++ b/vips.go @@ -55,8 +55,6 @@ type vipsSaveOptions struct { Interlace bool NoProfile bool Interpretation Interpretation - Flatten bool - Background Color } type vipsWatermarkOptions struct { @@ -343,15 +341,6 @@ func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) { image = outImage } - // Flatten image on a background, if necessary - if o.Flatten { - var err error - if outImage, err = vipsFlattenBackground(image, o.Background); err != nil { - return nil, err - } - image = outImage - } - return image, nil } diff --git a/vips.h b/vips.h index 3e4a1a4..c3f7d9e 100644 --- a/vips.h +++ b/vips.h @@ -283,11 +283,18 @@ vips_is_16bit (VipsInterpretation interpretation) { int vips_flatten_background_brigde(VipsImage *in, VipsImage **out, double r, double g, double b) { + if (vips_is_16bit(in->Type)) { + r = 65535 * r / 255; + g = 65535 * g / 255; + b = 65535 * b / 255; + } + double background[3] = {r, g, b}; VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3); return vips_flatten(in, out, - "background", vipsBackground, "max_alpha", 255.0, + "background", vipsBackground, + "max_alpha", vips_is_16bit(in->Type) ? 65535.0 : 255.0, NULL ); }