From b2feb4805fa6ca46978b81176b7b96ecbace76b6 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 18 Jun 2020 14:54:09 +0100 Subject: [PATCH] fix flush of left-over bits we were not adjusting for pixel size, thanks MathemanFlo see https://github.com/libvips/libvips/pull/1672#issuecomment-644807739 --- libvips/foreign/vips2tiff.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index ea939e6d..5ff5834d 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -1310,8 +1310,14 @@ eightbit2nbit( Wtiff *wtiff, VipsPel *q, VipsPel *p, int n ) /* Any left-over bits? Need to be left-aligned. */ - if( (x & pixel_mask) != 0 ) - *q++ = (bits ^ mask) << (8 - (x & pixel_mask)); + if( (x & pixel_mask) != 0 ) { + /* The number of bits we've collected in bits and must + * left-align and flush. + */ + int collected_bits = (x & pixel_mask) << (wtiff->bitdepth - 1); + + *q++ = (bits ^ mask) << (8 - collected_bits); + } } /* Swap the sense of the first channel, if necessary.