From 10bada0161abf0cc8b33c8284b8173ba3dea3852 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 21 Sep 2020 09:38:11 +0100 Subject: [PATCH] tiny thumbnail speedup thumbnail can skip premultiply/unpre if there's no residual resize --- libvips/resample/thumbnail.c | 38 +++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/libvips/resample/thumbnail.c b/libvips/resample/thumbnail.c index b6ffe672..88b18d8a 100644 --- a/libvips/resample/thumbnail.c +++ b/libvips/resample/thumbnail.c @@ -736,24 +736,6 @@ vips_thumbnail_build( VipsObject *object ) return( -1 ); in = t[2]; - /* If there's an alpha, we have to premultiply before shrinking. See - * https://github.com/libvips/libvips/issues/291 - */ - have_premultiplied = FALSE; - if( vips_image_hasalpha( in ) ) { - g_info( "premultiplying alpha" ); - if( vips_premultiply( in, &t[3], NULL ) ) - return( -1 ); - 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 to preshrunk_page_height, so we work for multi-page images. */ vips_thumbnail_calculate_shrink( thumbnail, @@ -771,6 +753,26 @@ vips_thumbnail_build( VipsObject *object ) vshrink = (double) in->Ysize / target_image_height; } + /* If there's an alpha, we have to premultiply before shrinking. See + * https://github.com/libvips/libvips/issues/291 + */ + have_premultiplied = FALSE; + if( vips_image_hasalpha( in ) && + hshrink != 1.0 && + vshrink != 1.0 ) { + g_info( "premultiplying alpha" ); + if( vips_premultiply( in, &t[3], NULL ) ) + return( -1 ); + 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]; + } + if( vips_resize( in, &t[4], 1.0 / hshrink, "vscale", 1.0 / vshrink, NULL ) )