revise heic thumbnail workaround

This commit is contained in:
John Cupitt 2020-08-01 11:49:01 +01:00
parent 80abdce923
commit 1099bd068c
1 changed files with 43 additions and 43 deletions

View File

@ -285,25 +285,19 @@ vips_foreign_load_heif_get_flags( VipsForeignLoad *load )
static int static int
vips_foreign_load_heif_set_thumbnail( VipsForeignLoadHeif *heif ) vips_foreign_load_heif_set_thumbnail( VipsForeignLoadHeif *heif )
{ {
double main_aspect;
double thumb_aspect;
heif_item_id thumb_ids[1]; heif_item_id thumb_ids[1];
int n_thumbs; int n_thumbs;
struct heif_image_handle *thumb_handle; struct heif_image_handle *thumb_handle;
struct heif_image *thumb_img; struct heif_image *thumb_img;
struct heif_error error; struct heif_error error;
double main_aspect;
/* We need the main image aspect ratio so we can sanity-check double thumb_aspect;
* the thumbnail.
*/
main_aspect = (double)
heif_image_handle_get_width( heif->handle ) /
heif_image_handle_get_height( heif->handle );
n_thumbs = heif_image_handle_get_list_of_thumbnail_IDs( n_thumbs = heif_image_handle_get_list_of_thumbnail_IDs(
heif->handle, thumb_ids, 1 ); heif->handle, thumb_ids, 1 );
if( n_thumbs == 0 )
return( 0 );
if( n_thumbs > 0 ) {
error = heif_image_handle_get_thumbnail( heif->handle, error = heif_image_handle_get_thumbnail( heif->handle,
thumb_ids[0], &thumb_handle ); thumb_ids[0], &thumb_handle );
if( error.code ) { if( error.code ) {
@ -312,34 +306,40 @@ vips_foreign_load_heif_set_thumbnail( VipsForeignLoadHeif *heif )
} }
/* Just checking the width and height of the handle isn't /* Just checking the width and height of the handle isn't
* enough -- we have to decode it and test the decoded * enough -- we have to experimentally decode it and test the
* dimensions. * decoded dimensions.
*/ */
error = heif_decode_image( thumb_handle, &thumb_img, error = heif_decode_image( thumb_handle, &thumb_img,
heif_colorspace_RGB, heif_colorspace_RGB,
heif_chroma_interleaved_RGB, heif_chroma_interleaved_RGB,
NULL ); NULL );
if( error.code ) { if( error.code ) {
VIPS_FREEF( heif_image_handle_release, thumb_handle );
vips__heif_error( &error ); vips__heif_error( &error );
return( -1 ); return( -1 );
} }
thumb_aspect = (double) thumb_aspect = (double)
heif_image_get_width( thumb_img, heif_image_get_width( thumb_img, heif_channel_interleaved ) /
heif_channel_interleaved ) / heif_image_get_height( thumb_img, heif_channel_interleaved );
heif_image_get_height( thumb_img,
heif_channel_interleaved );
VIPS_FREEF( heif_image_release, thumb_img ); VIPS_FREEF( heif_image_release, thumb_img );
if( fabs( main_aspect - thumb_aspect ) < 0.1 ) { main_aspect = (double)
heif_image_handle_get_width( heif->handle ) /
heif_image_handle_get_height( heif->handle );
/* The bug we are working around has decoded thumbs as 512x512
* with the main image as 6kx4k, so a 0.1 threshold is more
* than tight enough to spot the error.
*/
if( fabs( main_aspect - thumb_aspect ) > 0.1 ) {
VIPS_FREEF( heif_image_handle_release, thumb_handle );
return( 0 );
}
VIPS_FREEF( heif_image_handle_release, heif->handle ); VIPS_FREEF( heif_image_handle_release, heif->handle );
heif->handle = thumb_handle; heif->handle = thumb_handle;
}
else {
VIPS_FREEF( heif_image_handle_release, thumb_handle );
}
}
return( 0 ); return( 0 );
} }