From 0f111984738842d27d04aed2a3f823d82a943506 Mon Sep 17 00:00:00 2001 From: glennrp Date: Wed, 7 Jul 2010 20:18:33 +0000 Subject: [PATCH] Fixed PNG tRNS chunk handling for grayscale images. --- ChangeLog | 5 +++++ coders/png.c | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index fee8e54ab..4a3fb7fbe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2010-07-07 6.6.3-1 Glenn Randers-Pehrson + * The PNG decoder failed to handle the tRNS chunk properly for grayscale + images where the png bit_depth was not the same as the quantum depth, + and the transparent color was not black. + 2010-07-01 6.6.3-0 Chris Madison * Pinging the DJVU image format no longer faults. * Check for time-limit-exceeded in only one thread. diff --git a/coders/png.c b/coders/png.c index cd7b1c502..5b08196e5 100644 --- a/coders/png.c +++ b/coders/png.c @@ -2230,11 +2230,26 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, transparent_color.opacity= (Quantum) (ping_trans_color->gray); if (ping_color_type == PNG_COLOR_TYPE_GRAY) { - if (ping_bit_depth < 8) - { - transparent_color.opacity=(Quantum) ((( - ping_trans_color->gray)*255)/max_sample); - } +#if (MAGICKCORE_QUANTUM_DEPTH == 8) + if (ping_bit_depth < MAGICKCORE_QUANTUM_DEPTH) +#endif + transparent_color.opacity=(Quantum) ( + ping_trans_color->gray * + (QuantumRange/((1UL << ping_bit_depth)-1))); + +#if (MAGICKCORE_QUANTUM_DEPTH == 8) /* This needs testing. */ + else /* Reducing tRNS values from 16 to 8 merges some values */ + transparent_color.opacity=(Quantum) ( + ping_trans_color->gray * + (((1UL << ping_bit_depth)-1)/QuantumRange)); +#endif + if (logging != MagickFalse) + { + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " Raw tRNS graylevel is %d.",ping_trans_color->gray); + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " scaled graylevel is %d.",transparent_color.opacity); + } transparent_color.red=transparent_color.opacity; transparent_color.green=transparent_color.opacity; transparent_color.blue=transparent_color.opacity; @@ -6890,6 +6905,18 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info, { size_t one = 1; ping_bit_depth=1; + + if (image->colors == 0) + { + /* DO SOMETHING */ + (void) ThrowMagickException(&image->exception, + GetMagickModule(),CoderError, + "image has 0 colors", "`%s'",""); + } + + if (logging != MagickFalse) + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " SyncImage.2."); while ((int) (one << ping_bit_depth) < (ssize_t) image_colors) ping_bit_depth <<= 1; -- 2.40.0