diff --git a/libvips/conversion/unpremultiply.c b/libvips/conversion/unpremultiply.c index efc90b53..219fac28 100644 --- a/libvips/conversion/unpremultiply.c +++ b/libvips/conversion/unpremultiply.c @@ -78,17 +78,17 @@ G_DEFINE_TYPE( VipsUnpremultiply, vips_unpremultiply, VIPS_TYPE_CONVERSION ); \ for( x = 0; x < width; x++ ) { \ IN alpha = p[alpha_band]; \ - IN clip_alpha = VIPS_CLIP( 0, alpha, max_alpha ); \ - OUT nalpha = (OUT) clip_alpha / max_alpha; \ \ - if( nalpha == 0 ) \ + if( alpha != 0 ) { \ + OUT factor = max_alpha / alpha; \ + \ + for( i = 0; i < alpha_band; i++ ) \ + q[i] = factor * p[i]; \ + q[alpha_band] = alpha; \ + } \ + else \ for( i = 0; i < alpha_band + 1; i++ ) \ q[i] = 0; \ - else { \ - for( i = 0; i < alpha_band; i++ ) \ - q[i] = p[i] / nalpha; \ - q[alpha_band] = clip_alpha; \ - } \ \ for( i = alpha_band + 1; i < bands; i++ ) \ q[i] = p[i]; \ @@ -106,21 +106,21 @@ G_DEFINE_TYPE( VipsUnpremultiply, vips_unpremultiply, VIPS_TYPE_CONVERSION ); \ for( x = 0; x < width; x++ ) { \ IN alpha = p[3]; \ - IN clip_alpha = VIPS_CLIP( 0, alpha, max_alpha ); \ - OUT nalpha = (OUT) clip_alpha / max_alpha; \ \ - if( nalpha == 0 ) { \ + if( alpha != 0 ) { \ + OUT factor = max_alpha / alpha; \ + \ + q[0] = factor * p[0]; \ + q[1] = factor * p[1]; \ + q[2] = factor * p[2]; \ + q[3] = alpha; \ + } \ + else { \ q[0] = 0; \ q[1] = 0; \ q[2] = 0; \ q[3] = 0; \ } \ - else { \ - q[0] = p[0] / nalpha; \ - q[1] = p[1] / nalpha; \ - q[2] = p[2] / nalpha; \ - q[3] = clip_alpha; \ - } \ \ p += 4; \ q += 4; \