From aeaafe568b0468c87aa99ba1b5b687ec78444ff4 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 24 Sep 2013 11:38:17 +0100 Subject: [PATCH] better sanity check for buffer sizing --- TODO | 6 ------ libvips/foreign/vips2tiff.c | 38 ++++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/TODO b/TODO index 2753e8dd..ffa19961 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,3 @@ -- tiff save needs relaxing too - - support all vips numeric formats, including complex and double comples - - tiffsave.c and vips2tiff.c both need work - - finish hist_ismonotonic() needs im_conv() diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index a203ef13..8faac8c5 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -596,17 +596,6 @@ write_tiff_header( TiffWrite *tw, TIFF *tif, int width, int height ) TIFFSetField( tif, TIFFTAG_SAMPLEFORMAT, format ); - /* Double check: buffers should match in size. - */ - if( tw->im->Coding != VIPS_CODING_LABQ && - !tw->onebit && - TIFFScanlineSize( tif ) != - VIPS_IMAGE_SIZEOF_LINE( tw->im ) ) { - vips_error( "vips2tiff", - "%s", _( "unsupported image format" ) ); - return( -1 ); - } - return( 0 ); } @@ -1096,6 +1085,22 @@ write_tif_tilewise( TiffWrite *tw ) { VipsImage *im = tw->im; + /* Double check: buffers should match in size, except for onebit and + * labq modes. + */ +{ + size_t vips_tile_size = + VIPS_IMAGE_SIZEOF_PEL( im ) * tw->tilew * tw->tileh; + + if( tw->im->Coding != VIPS_CODING_LABQ && + !tw->onebit && + TIFFTileSize( tw->tif ) != vips_tile_size ) { + vips_error( "vips2tiff", + "%s", _( "unsupported image format" ) ); + return( -1 ); + } +} + g_assert( !tw->tbuf ); if( !(tw->tbuf = vips_malloc( NULL, TIFFTileSize( tw->tif ) )) ) return( -1 ); @@ -1158,6 +1163,17 @@ write_tif_stripwise( TiffWrite *tw ) { g_assert( !tw->tbuf ); + /* Double check: buffers should match in size, except for onebit and + * labq modes. + */ + if( tw->im->Coding != VIPS_CODING_LABQ && + !tw->onebit && + TIFFTileSize( tw->tif ) != VIPS_IMAGE_SIZEOF_LINE( tw->im ) ) { + vips_error( "vips2tiff", + "%s", _( "unsupported image format" ) ); + return( -1 ); + } + if( !(tw->tbuf = vips_malloc( NULL, TIFFScanlineSize( tw->tif ) )) ) return( -1 );