From 54d8e4808f5f2e83148c3bd7ab9ae80d1c45fda5 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 16 May 2016 13:18:59 +0100 Subject: [PATCH] restore bandfmt after unpremultiply in thumbnail vips_unpremultiply() always outputs float. If you save the output of vipsthumbnail to a format that supports float files, like tiff, it won't ever get cast back to the source format. Example: $ vipsheader Opera-icon-high-res.png Opera-icon-high-res.png: 3056x3325 uchar, 4 bands, srgb, pngload $ vipsthumbnail Opera-icon-high-res.png -o x.tif $ vipsheader x.tif x.tif: 117x128 float, 4 bands, scrgb, tiffload This change makes it note and restore BandFmt aropund pre/unpremultiply. See https://github.com/jcupitt/libvips/issues/447 --- ChangeLog | 1 + tools/vipsthumbnail.c | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index e99e77f6..e487f530 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,7 @@ - dzsave won't write empty tiles in google mode, thanks bverem, perog, felixbuenemann - allow nested [] in CLI args +- restore BandFmt on unpremultiply in vipsthumbnail 15/4/16 started 8.3.1 - rename vips wrapper script, it was still vips-8.2, thanks Benjamin diff --git a/tools/vipsthumbnail.c b/tools/vipsthumbnail.c index 34546e59..9949846d 100644 --- a/tools/vipsthumbnail.c +++ b/tools/vipsthumbnail.c @@ -77,6 +77,8 @@ * - add webp --shrink support * 29/2/16 * - deprecate sharpen and interpolate + * 6/5/16 + * - restore BandFmt after unpremultiply */ #ifdef HAVE_CONFIG_H @@ -363,6 +365,7 @@ thumbnail_shrink( VipsObject *process, VipsImage *in ) /* TRUE if we've premultiplied and need to unpremultiply. */ gboolean have_premultiplied; + VipsBandFormat unpremultiplied_format; /* Sniff the incoming image and try to guess what the alpha max is. */ @@ -444,8 +447,14 @@ thumbnail_shrink( VipsObject *process, VipsImage *in ) "max_alpha", max_alpha, NULL ) ) return( NULL ); - in = t[3]; have_premultiplied = TRUE; + + /* vips_premultiply() makes a float image. When we + * vips_unpremultiply() below, we need to cast back to the + * pre-premultiply format. + */ + unpremultiplied_format = in->BandFmt; + in = t[3]; } shrink = calculate_shrink( in ); @@ -458,16 +467,17 @@ thumbnail_shrink( VipsObject *process, VipsImage *in ) vips_info( "vipsthumbnail", "unpremultiplying alpha" ); if( vips_unpremultiply( in, &t[5], "max_alpha", max_alpha, - NULL ) ) + NULL ) || + vips_cast( t[5], &t[6], unpremultiplied_format, NULL ) ) return( NULL ); - in = t[5]; + in = t[6]; } /* Colour management. * * If we've already imported, just export. Otherwise, we're in - * device space device and we need a combined - * import/export to transform to the target space. + * device space and we need a combined import/export to transform to + * the target space. */ if( have_imported ) { if( export_profile ||