From d61a85f6c9c5400d8fbdd8f3ff67d5f82a9d6604 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 27 Aug 2019 14:47:36 +0100 Subject: [PATCH] prevent over-pre-shrink in thumbnail We could pre-shrink so much that an axis went to 0. See https://github.com/lovell/sharp/issues/1782#issuecomment-525249430 --- ChangeLog | 1 + libvips/resample/thumbnail.c | 33 ++++++++++++++------------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 61d84138..edb52682 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,7 @@ - better feof() handling in GIF load - clip coding and interpretation on vips image read - check image bounds for GIF load +- prevent over-pre-shrink in thumbnail [kleisauke] 24/5/19 started 8.8.1 - improve realpath() use on older libc diff --git a/libvips/resample/thumbnail.c b/libvips/resample/thumbnail.c index 89fbf48a..22838ae4 100644 --- a/libvips/resample/thumbnail.c +++ b/libvips/resample/thumbnail.c @@ -22,6 +22,8 @@ * - don't force import CMYK, since colourspace knows about it now * 24/4/19 * - support multi-page (animated) images + * 27/8/19 kleisauke + * - prevent over-pre-shrink in thumbnail */ /* @@ -464,36 +466,24 @@ vips_thumbnail_open( VipsThumbnail *thumbnail ) factor = 1.0; - if( vips_isprefix( "VipsForeignLoadJpeg", thumbnail->loader ) ) { + if( vips_isprefix( "VipsForeignLoadJpeg", thumbnail->loader ) ) factor = vips_thumbnail_find_jpegshrink( thumbnail, thumbnail->input_width, thumbnail->input_height ); - - g_info( "loading jpeg with factor %g pre-shrink", factor ); - } else if( vips_isprefix( "VipsForeignLoadTiff", thumbnail->loader ) || vips_isprefix( "VipsForeignLoadOpenslide", - thumbnail->loader ) ) { + thumbnail->loader ) ) factor = vips_thumbnail_find_pyrlevel( thumbnail, thumbnail->input_width, thumbnail->input_height ); - - g_info( "loading pyr level %g", factor ); - } - else if( vips_isprefix( "VipsForeignLoadPdf", thumbnail->loader ) ) { + else if( vips_isprefix( "VipsForeignLoadPdf", thumbnail->loader ) ) factor = 1.0 / vips_thumbnail_calculate_common_shrink( thumbnail, thumbnail->input_width, thumbnail->page_height ); - - g_info( "loading PDF with factor %g pre-scale", factor ); - } - else if( vips_isprefix( "VipsForeignLoadSvg", thumbnail->loader ) ) { + else if( vips_isprefix( "VipsForeignLoadSvg", thumbnail->loader ) ) factor = 1.0 / vips_thumbnail_calculate_common_shrink( thumbnail, thumbnail->input_width, thumbnail->input_height ); - - g_info( "loading SVG with factor %g pre-scale", factor ); - } else if( vips_isprefix( "VipsForeignLoadHeif", thumbnail->loader ) ) { /* 'factor' is a gboolean which enables thumbnail load instead * of image load. @@ -507,14 +497,19 @@ vips_thumbnail_open( VipsThumbnail *thumbnail ) factor = 0.0; } - else if( vips_isprefix( "VipsForeignLoadWebp", thumbnail->loader ) ) { + else if( vips_isprefix( "VipsForeignLoadWebp", thumbnail->loader ) ) factor = 1.0 / vips_thumbnail_calculate_common_shrink( thumbnail, thumbnail->input_width, thumbnail->page_height ); - g_info( "loading webp with factor %g pre-scale", factor ); - } + /* We don't want to pre-shrink so much that we send an axis to 0. + */ + if( factor > thumbnail->input_width || + factor > thumbnail->input_height ) + factor = 1.0; + + g_info( "loading with factor %g pre-shrink", factor ); if( !(im = class->open( thumbnail, factor )) ) return( NULL );