From f06c9f3bcffec2c9f09c8ca460ac534cc03c2188 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 28 Apr 2022 09:08:34 +0100 Subject: [PATCH] add fail-on to thumbnail see https://github.com/libvips/libvips/discussions/2781 --- ChangeLog | 1 + libvips/resample/thumbnail.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/ChangeLog b/ChangeLog index 0bcb8b66..bd179405 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,6 +18,7 @@ - update libnsgif API [tlsa] - deprecate "properties" option to dzsave (now always on) - always set the min stack size for pthreads, if we can +- add fail-on to thumbnail 26/11/21 started 8.12.3 - better arg checking for hist_find_ndim [travisbell] diff --git a/libvips/resample/thumbnail.c b/libvips/resample/thumbnail.c index 8844dc17..f41dad1c 100644 --- a/libvips/resample/thumbnail.c +++ b/libvips/resample/thumbnail.c @@ -35,6 +35,8 @@ * mode * 14/4/22 * - add a seq to thumbnail_image to stop cache thrashing + * 28/4/22 + * - add fail-on */ /* @@ -113,6 +115,7 @@ typedef struct _VipsThumbnail { char *export_profile; char *import_profile; VipsIntent intent; + VipsFailOn fail_on; /* Bits of info we read from the input image when we get the header of * the original. @@ -1017,6 +1020,13 @@ vips_thumbnail_class_init( VipsThumbnailClass *class ) G_STRUCT_OFFSET( VipsThumbnail, intent ), VIPS_TYPE_INTENT, VIPS_INTENT_RELATIVE ); + VIPS_ARG_ENUM( class, "fail-on", 121, + _( "Fail on" ), + _( "Error level to fail on" ), + VIPS_ARGUMENT_OPTIONAL_INPUT, + G_STRUCT_OFFSET( VipsThumbnail, fail_on ), + VIPS_TYPE_FAIL_ON, VIPS_FAIL_ON_NONE ); + /* BOOL args which default TRUE arguments don't work with the * command-line -- GOption does not allow --auto-rotate=false. * @@ -1039,6 +1049,7 @@ vips_thumbnail_init( VipsThumbnail *thumbnail ) thumbnail->height = 1; thumbnail->auto_rotate = TRUE; thumbnail->intent = VIPS_INTENT_RELATIVE; + thumbnail->fail_on = VIPS_FAIL_ON_NONE; } typedef struct _VipsThumbnailFile { @@ -1084,6 +1095,7 @@ vips_thumbnail_file_open( VipsThumbnail *thumbnail, double factor ) if( vips_isprefix( "VipsForeignLoadJpeg", thumbnail->loader ) ) { return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, "shrink", (int) factor, NULL ) ); } @@ -1091,6 +1103,7 @@ vips_thumbnail_file_open( VipsThumbnail *thumbnail, double factor ) thumbnail->loader ) ) { return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, "level", (int) factor, NULL ) ); } @@ -1099,6 +1112,7 @@ vips_thumbnail_file_open( VipsThumbnail *thumbnail, double factor ) vips_isprefix( "VipsForeignLoadWebp", thumbnail->loader ) ) { return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, "scale", 1.0 / factor, NULL ) ); } @@ -1108,6 +1122,7 @@ vips_thumbnail_file_open( VipsThumbnail *thumbnail, double factor ) if( thumbnail->page_pyramid ) return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, "page", (int) factor, NULL ) ); else @@ -1122,27 +1137,32 @@ vips_thumbnail_file_open( VipsThumbnail *thumbnail, double factor ) if( thumbnail->subifd_pyramid ) return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, "subifd", (int) factor, NULL ) ); else if( thumbnail->page_pyramid ) return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, "page", (int) factor, NULL ) ); else return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, NULL ) ); } else if( vips_isprefix( "VipsForeignLoadHeif", thumbnail->loader ) ) { return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, "thumbnail", (int) factor, NULL ) ); } else { return( vips_image_new_from_file( file->filename, "access", VIPS_ACCESS_SEQUENTIAL, + "fail-on", thumbnail->fail_on, NULL ) ); } } @@ -1194,6 +1214,7 @@ vips_thumbnail_file_init( VipsThumbnailFile *file ) * * @import_profile: %gchararray, fallback import ICC profile * * @export_profile: %gchararray, export ICC profile * * @intent: #VipsIntent, rendering intent + * * @fail_on: #VipsFailOn, load error types to fail on * * Make a thumbnail from a file. Shrinking is done in three stages: using any * shrink-on-load features available in the file import library, using a block @@ -1238,6 +1259,9 @@ vips_thumbnail_file_init( VipsThumbnailFile *file ) * Use @intent to set the rendering intent for any ICC transform. The default * is #VIPS_INTENT_RELATIVE. * + * Use @fail_on to control the types of error that will cause loading to fail. + * The default is #VIPS_FAIL_ON_NONE, ie. thumbnail is permissive. + * * See also: vips_thumbnail_buffer(). * * Returns: 0 on success, -1 on error. @@ -1439,6 +1463,7 @@ vips_thumbnail_buffer_init( VipsThumbnailBuffer *buffer ) * * @import_profile: %gchararray, fallback import ICC profile * * @export_profile: %gchararray, export ICC profile * * @intent: #VipsIntent, rendering intent + * * @fail_on: #VipsFailOn, load error types to fail on * * @option_string: %gchararray, extra loader options * * Exactly as vips_thumbnail(), but read from a memory buffer. One extra @@ -1652,6 +1677,7 @@ vips_thumbnail_source_init( VipsThumbnailSource *source ) * * @import_profile: %gchararray, fallback import ICC profile * * @export_profile: %gchararray, export ICC profile * * @intent: #VipsIntent, rendering intent + * * @fail_on: #VipsFailOn, load error types to fail on * * @option_string: %gchararray, extra loader options * * Exactly as vips_thumbnail(), but read from a source. One extra @@ -1768,6 +1794,7 @@ vips_thumbnail_image_init( VipsThumbnailImage *image ) * * @import_profile: %gchararray, fallback import ICC profile * * @export_profile: %gchararray, export ICC profile * * @intent: #VipsIntent, rendering intent + * * @fail_on: #VipsFailOn, load error types to fail on * * Exactly as vips_thumbnail(), but read from an existing image. *