From 0a91e2d8c7a3c1062e7d94205dc924ccd075c459 Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Wed, 11 Nov 2020 20:14:02 +0100 Subject: [PATCH 1/3] Ensure that streams are properly read in spngload --- libvips/foreign/spngload.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/libvips/foreign/spngload.c b/libvips/foreign/spngload.c index baba7a4a..b3e6a601 100644 --- a/libvips/foreign/spngload.c +++ b/libvips/foreign/spngload.c @@ -94,13 +94,18 @@ vips_foreign_load_png_stream( spng_ctx *ctx, void *user, { VipsSource *source = VIPS_SOURCE( user ); - gint64 bytes_read; + while( length > 0 ) { + gint64 bytes_read; - bytes_read = vips_source_read( source, dest, length ); - if( bytes_read < 0 ) - return( SPNG_IO_ERROR ); - if( bytes_read < length ) - return( SPNG_IO_EOF); + bytes_read = vips_source_read( source, dest, length ); + if( bytes_read < 0 ) + return( SPNG_IO_ERROR ); + if( bytes_read == 0 ) + return( SPNG_IO_EOF ); + + dest += bytes_read; + length -= bytes_read; + } return( 0 ); } From c0102c5814157aeeb220020cb835192b14ce9647 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 16 Oct 2020 00:05:17 +0100 Subject: [PATCH 2/3] better GraphicsMagick image write We were not setting matte or depth correctly, thanks bfriesen. --- ChangeLog | 1 + libvips/foreign/magick.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/ChangeLog b/ChangeLog index 77fd80ce..9628590a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ - add missing flushes on write to target [harukizaemon] - hide info messages you could get with some older glibs [kleisauke] - fix --no-strip on dzsave with icc-profiles [altert] +- better GraphicsMagick image write [bfriesen] 6/9/20 started 8.10.2 - update magicksave/load profile handling [kelilevi] diff --git a/libvips/foreign/magick.c b/libvips/foreign/magick.c index ce6000f4..f4a5eab0 100644 --- a/libvips/foreign/magick.c +++ b/libvips/foreign/magick.c @@ -4,6 +4,8 @@ * * 24/7/18 * - add the sniffer + * 16/10/20 [bfriesen] + * - set matte and depth appropriately for GM in magick_import_pixels() */ /* @@ -319,6 +321,7 @@ magick_import_pixels( Image *image, const ssize_t x, const ssize_t y, type, pixels ) ); #else /*!HAVE_IMPORTIMAGEPIXELS*/ Image *constitute_image; + unsigned int storage_type_depth; g_assert( image ); g_assert( image->signature == MagickSignature ); @@ -328,7 +331,43 @@ magick_import_pixels( Image *image, const ssize_t x, const ssize_t y, if( !constitute_image ) return( MagickFalse ); + /* image needs to inherit these fields from constitute_image. + */ + switch( type ) { + case CharPixel: + storage_type_depth = sizeof( unsigned char ) * 8; + break; + + case ShortPixel: + storage_type_depth = sizeof( unsigned short ) * 8; + break; + + case IntegerPixel: + storage_type_depth = sizeof( unsigned short ) * 8; + break; + + case LongPixel: + storage_type_depth = sizeof( unsigned long ) * 8; + break; + + case FloatPixel: + storage_type_depth = sizeof( float ) * 8; + break; + + case DoublePixel: + storage_type_depth = sizeof( double ) * 8; + break; + + default: + storage_type_depth = QuantumDepth; + break; + + } + image->depth = VIPS_MIN( storage_type_depth, QuantumDepth ); + image->matte = constitute_image->matte; + (void) CompositeImage( image, CopyCompositeOp, constitute_image, x, y ); + DestroyImage( constitute_image ); return( image->exception.severity == UndefinedException ); From ff450497ff38a330eca0f38ed38324779e461523 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 12 Nov 2020 12:11:35 +0000 Subject: [PATCH 3/3] add read loops to heif and ppm as well We were not looping on vips_source_read() in these loaders, so they could fail when reading from very slow pipes. See https://github.com/kleisauke/net-vips/issues/101 --- libvips/foreign/heifload.c | 12 ++++++++---- libvips/foreign/ppmload.c | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/libvips/foreign/heifload.c b/libvips/foreign/heifload.c index 77e5f4bc..b4a14e56 100644 --- a/libvips/foreign/heifload.c +++ b/libvips/foreign/heifload.c @@ -963,11 +963,15 @@ vips_foreign_load_heif_read( void *data, size_t size, void *userdata ) { VipsForeignLoadHeif *heif = (VipsForeignLoadHeif *) userdata; - gint64 result; + while( size > 0 ) { + gint64 result; - result = vips_source_read( heif->source, data, size ); - if( result < 0 ) - return( -1 ); + result = vips_source_read( heif->source, data, size ); + if( result <= 0 ) + return( -1 ); + + size -= result; + } return( 0 ); } diff --git a/libvips/foreign/ppmload.c b/libvips/foreign/ppmload.c index bcc2e79a..77dfbb60 100644 --- a/libvips/foreign/ppmload.c +++ b/libvips/foreign/ppmload.c @@ -458,13 +458,21 @@ vips_foreign_load_ppm_generate_binary( VipsRegion *or, for( y = 0; y < r->height; y++ ) { VipsPel *q = VIPS_REGION_ADDR( or, 0, r->top + y ); - size_t bytes_read; + size_t n_bytes; - bytes_read = vips_source_read( ppm->source, q, sizeof_line ); - if( bytes_read != sizeof_line ) { - vips_error( class->nickname, - "%s", _( "file truncated" ) ); - return( -1 ); + n_bytes = sizeof_line; + while( n_bytes > 0 ) { + size_t bytes_read; + + bytes_read = vips_source_read( ppm->source, + q, sizeof_line ); + if( bytes_read == 0 ) { + vips_error( class->nickname, + "%s", _( "file truncated" ) ); + return( -1 ); + } + + n_bytes -= bytes_read; } }