From 6bf83d1ddc3ea22c14c76145f56ccbc1d0e361bc Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Sat, 18 Mar 2017 10:55:38 +0000 Subject: [PATCH] premultiply before startcrop search or there might be stuff hidden in the fully transparent areas see https://github.com/jcupitt/libvips/issues/619 --- libvips/conversion/smartcrop.c | 23 +++++++++++++++++------ whatsnew-8.5.md | 4 ++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/libvips/conversion/smartcrop.c b/libvips/conversion/smartcrop.c index c2be29a8..caddf771 100644 --- a/libvips/conversion/smartcrop.c +++ b/libvips/conversion/smartcrop.c @@ -257,6 +257,7 @@ vips_smartcrop_build( VipsObject *object ) VipsSmartcrop *smartcrop = (VipsSmartcrop *) object; VipsImage **t = (VipsImage **) vips_object_local_array( object, 2 ); + VipsImage *in; int left; int top; @@ -271,6 +272,18 @@ vips_smartcrop_build( VipsObject *object ) return( -1 ); } + in = smartcrop->in; + + /* If there's an alpha, we have to premultiply before searching for + * content. There could be stuff in transparent areas which we don't + * want to consider. + */ + if( vips_image_hasalpha( in ) ) { + if( vips_premultiply( in, &t[0], NULL ) ) + return( -1 ); + in = t[0]; + } + switch( smartcrop->interesting ) { case VIPS_INTERESTING_NONE: break; @@ -281,14 +294,12 @@ vips_smartcrop_build( VipsObject *object ) break; case VIPS_INTERESTING_ENTROPY: - if( vips_smartcrop_entropy( smartcrop, - smartcrop->in, &left, &top ) ) + if( vips_smartcrop_entropy( smartcrop, in, &left, &top ) ) return( -1 ); break; case VIPS_INTERESTING_ATTENTION: - if( vips_smartcrop_attention( smartcrop, - smartcrop->in, &left, &top ) ) + if( vips_smartcrop_attention( smartcrop, in, &left, &top ) ) return( -1 ); break; @@ -297,9 +308,9 @@ vips_smartcrop_build( VipsObject *object ) break; } - if( vips_extract_area( smartcrop->in, &t[0], + if( vips_extract_area( smartcrop->in, &t[1], left, top, smartcrop->width, smartcrop->height, NULL ) || - vips_image_write( t[0], conversion->out ) ) + vips_image_write( t[1], conversion->out ) ) return( -1 ); return( 0 ); diff --git a/whatsnew-8.5.md b/whatsnew-8.5.md index bd11e6fa..c8fea323 100644 --- a/whatsnew-8.5.md +++ b/whatsnew-8.5.md @@ -179,5 +179,5 @@ As usual, there are a range of improvements to file format read and write. ## Other -Many small bug fixes, improvements to the C++ binding. -As usual, the ChanegLog has more detail if you're interested. +Many small bug fixes, improvements to the C++ binding. As usual, the +ChangeLog has more detail, if you're interested.