From 069ff8982c6535dba017628f8dbfb29bb13129a9 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 10 Dec 2014 09:41:31 +0000 Subject: [PATCH] zero out edge tile buffers before tiff jpeg write see https://github.com/jcupitt/libvips/issues/204 thanks iwbh15 --- libvips/foreign/vips2tiff.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index d7e13e38..b330765d 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -142,6 +142,8 @@ * - cache input in tile write mode to keep us sequential * 3/12/14 * - embed XMP in output + * 10/12/14 + * - zero out edge tile buffers before jpeg write, thanks iwbh15 */ /* @@ -384,6 +386,18 @@ pack2tiff( TiffWrite *tw, VipsRegion *in, VipsPel *q, VipsRect *area ) { int y; + /* JPEG compression can read outside the pixel area for edge tiles. It + * always compresses 8x8 blocks, so if the image width or height is + * not a multiple of 8, it can look beyond the pixels we will write. + * + * Black out the tile first to make sure these edge pixels are always + * zero. + */ + if( tw->compression == COMPRESSION_JPEG && + (area->width < tw->tilew || + area->height < tw->tileh) ) + memset( q, 0, TIFFTileSize( tw->tif ) ); + for( y = area->top; y < VIPS_RECT_BOTTOM( area ); y++ ) { VipsPel *p = (VipsPel *) VIPS_REGION_ADDR( in, area->left, y ); @@ -697,7 +711,8 @@ build_pyramid( TiffWrite *tw, PyramidLayer *above, */ *zap = layer; - if( layer->width > tw->tilew || layer->height > tw->tileh ) + if( layer->width > tw->tilew || + layer->height > tw->tileh ) if( build_pyramid( tw, layer, &layer->below, layer->width, layer->height ) ) return( -1 );