From 140a3b3ed07ced4ab70af9f02b3f9b6acc69410f Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Sat, 23 Dec 2017 14:53:15 +0000 Subject: [PATCH] try to improve error handling in magicksave --- libvips/foreign/magick.c | 34 ++++++++++++++-------------------- libvips/foreign/magick.h | 4 ++-- libvips/foreign/magicksave.c | 22 ++++++++++------------ 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/libvips/foreign/magick.c b/libvips/foreign/magick.c index 1a152d5e..61815944 100644 --- a/libvips/foreign/magick.c +++ b/libvips/foreign/magick.c @@ -96,26 +96,6 @@ magick_inherit_exception( ExceptionInfo *exception, Image *image ) (void) image; } -static void * -magick_genesis_cb( void *client ) -{ -#ifdef DEBUG - printf( "magick_genesis_cb:\n" ); -#endif /*DEBUG*/ - - MagickCoreGenesis( vips_get_argv0(), MagickFalse ); - - return( NULL ); -} - -void -magick_genesis( void ) -{ - static GOnce once = G_ONCE_INIT; - - VIPS_ONCE( &once, magick_genesis_cb, NULL ); -} - #endif /*HAVE_MAGICK7*/ #ifdef HAVE_MAGICK @@ -175,6 +155,20 @@ magick_inherit_exception( ExceptionInfo *exception, Image *image ) InheritException( exception, &image->exception ); } +#endif /*HAVE_MAGICK*/ + +#if defined(HAVE_MAGICK) || defined(HAVE_MAGICK7) + +void +magick_vips_error( const char *domain, ExceptionInfo *exception ) +{ + if( exception && + exception->reason && + exception->description ) + vips_error( domain, _( "libMagick error: %s %s" ), + exception->reason, exception->description ); +} + static void * magick_genesis_cb( void *client ) { diff --git a/libvips/foreign/magick.h b/libvips/foreign/magick.h index ef27a8db..c5aad7ab 100644 --- a/libvips/foreign/magick.h +++ b/libvips/foreign/magick.h @@ -53,9 +53,9 @@ int magick_import_pixels( Image *image, const ssize_t x, const ssize_t y, const StorageType type,const void *pixels, ExceptionInfo *exception ); void magick_set_property( Image *image, const char *property, const char *value, ExceptionInfo *exception ); -int magick_set_image_colorspace( Image *image, const ColorspaceType colorspace, - ExceptionInfo *exception ); + void magick_inherit_exception( ExceptionInfo *exception, Image *image ); +void magick_vips_error( const char *domain, ExceptionInfo *exception ); void magick_genesis( void ); diff --git a/libvips/foreign/magicksave.c b/libvips/foreign/magicksave.c index 2bd58763..fed63226 100644 --- a/libvips/foreign/magicksave.c +++ b/libvips/foreign/magicksave.c @@ -201,6 +201,7 @@ vips_foreign_save_magick_create( VipsForeignSaveMagick *magick, VipsImage *im ) static int vips_foreign_save_magick_build( VipsObject *object ) { + VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object ); VipsForeignSave *save = (VipsForeignSave *) object; VipsForeignSaveMagick *magick = (VipsForeignSaveMagick *) object; @@ -246,6 +247,8 @@ vips_foreign_save_magick_build( VipsObject *object ) break; default: + vips_error( class->nickname, + "%s", _( "unsupported image format" ) ); return( -1 ); } @@ -275,6 +278,8 @@ vips_foreign_save_magick_build( VipsObject *object ) break; default: + vips_error( class->nickname, + "%s", _( "unsupported number of image bands" ) ); return( -1 ); } @@ -294,8 +299,10 @@ vips_foreign_save_magick_build( VipsObject *object ) if( magick->quality > 0 ) magick->image_info->quality = magick->quality; - if( vips_foreign_save_magick_create( magick, im ) ) + if( vips_foreign_save_magick_create( magick, im ) ) { + magick_vips_error( class->nickname, magick->exception ); return( -1 ); + } return( 0 ); } @@ -385,12 +392,7 @@ vips_foreign_save_magick_file_build( VipsObject *object ) if( !WriteImages( magick->image_info, magick->images, magick->image_info->filename, magick->exception ) ) { magick_inherit_exception( magick->exception, magick->images ); - vips_error( class->nickname, - _( "unable to write file \"%s\"\n" - "libMagick error: %s %s" ), - file->filename, - magick->exception->reason, - magick->exception->description ); + magick_vips_error( class->nickname, magick->exception ); return( -1 ); } @@ -459,11 +461,7 @@ vips_foreign_save_magick_buffer_build( VipsObject *object ) if( !(obuf = ImagesToBlob( magick->image_info, magick->images, &olen, magick->exception )) ) { magick_inherit_exception( magick->exception, magick->images ); - vips_error( class->nickname, - _( "unable to write buffer\n" - "libMagick error: %s %s" ), - magick->exception->reason, - magick->exception->description ); + magick_vips_error( class->nickname, magick->exception ); return( -1 ); }