From fcf06169b67cb58a2ab649c11889f91cb400dfd8 Mon Sep 17 00:00:00 2001 From: glennrp Date: Mon, 5 Nov 2012 14:57:08 +0000 Subject: [PATCH] Let libpng expand sub-8-bit pixels. --- coders/png.c | 84 ++++++++++------------------------------------------ 1 file changed, 15 insertions(+), 69 deletions(-) diff --git a/coders/png.c b/coders/png.c index 86dac72c9..f0ee8457f 100644 --- a/coders/png.c +++ b/coders/png.c @@ -2025,6 +2025,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, pass, ping_bit_depth, ping_color_type, + ping_file_depth, ping_interlace_method, ping_compression_method, ping_filter_method, @@ -2309,6 +2310,8 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, &ping_interlace_method,&ping_compression_method, &ping_filter_method); + ping_file_depth = ping_bit_depth; + (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans, &ping_trans_color); @@ -2316,11 +2319,8 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, if (ping_bit_depth < 8) { - if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)) - { - png_set_packing(ping); - ping_bit_depth = 8; - } + png_set_packing(ping); + ping_bit_depth = 8; } image->depth=ping_bit_depth; @@ -2648,16 +2648,16 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, bkgd_scale = 1; - if (ping_bit_depth == 1) + if (ping_file_depth == 1) bkgd_scale = 255; - else if (ping_bit_depth == 2) + else if (ping_file_depth == 2) bkgd_scale = 85; - else if (ping_bit_depth == 4) + else if (ping_file_depth == 4) bkgd_scale = 17; - if (ping_bit_depth <= 8) + if (ping_file_depth <= 8) bkgd_scale *= 257; ping_background->red *= bkgd_scale; @@ -2709,7 +2709,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, (void) LogMagickEvent(CoderEvent,GetMagickModule(), " Reading PNG tRNS chunk."); - max_sample = (int) ((one << ping_bit_depth) - 1); + max_sample = (int) ((one << ping_file_depth) - 1); if ((ping_color_type == PNG_COLOR_TYPE_GRAY && (int)ping_trans_color->gray > max_sample) || @@ -2730,7 +2730,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, int scale_to_short; - scale_to_short = 65535L/((1UL << ping_bit_depth)-1); + scale_to_short = 65535L/((1UL << ping_file_depth)-1); /* Scale transparent_color to short */ transparent_color.red= scale_to_short*ping_trans_color->red; @@ -2818,7 +2818,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, image->storage_class=PseudoClass; one=1; - image->colors=one << ping_bit_depth; + image->colors=one << ping_file_depth; #if (MAGICKCORE_QUANTUM_DEPTH == 8) if (image->colors > 256) image->colors=256; @@ -2875,7 +2875,7 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, size_t scale; - scale=(QuantumRange/((1UL << ping_bit_depth)-1)); + scale=(QuantumRange/((1UL << ping_file_depth)-1)); if (scale < 1) scale=1; @@ -2894,14 +2894,14 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, char msg[MaxTextExtent]; - /* encode ping_width, ping_height, ping_bit_depth, ping_color_type, + /* encode ping_width, ping_height, ping_file_depth, ping_color_type, ping_interlace_method in value */ (void) FormatLocaleString(msg,MaxTextExtent, "%d, %d",(int) ping_width, (int) ping_height); (void) SetImageProperty(image,"png:IHDR.width,height ",msg,exception); - (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_bit_depth); + (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_file_depth); (void) SetImageProperty(image,"png:IHDR.bit_depth ",msg,exception); (void) FormatLocaleString(msg,MaxTextExtent,"%d (%s)", @@ -3151,60 +3151,6 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, switch (ping_bit_depth) { - case 1: - { - register ssize_t - bit; - - for (x=(ssize_t) image->columns-7; x > 0; x-=8) - { - for (bit=7; bit >= 0; bit--) - *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00); - p++; - } - - if ((image->columns % 8) != 0) - { - for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--) - *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00); - } - - break; - } - - case 2: - { - for (x=(ssize_t) image->columns-3; x > 0; x-=4) - { - *r++=(*p >> 6) & 0x03; - *r++=(*p >> 4) & 0x03; - *r++=(*p >> 2) & 0x03; - *r++=(*p++) & 0x03; - } - - if ((image->columns % 4) != 0) - { - for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--) - *r++=(Quantum) ((*p >> (i*2)) & 0x03); - } - - break; - } - - case 4: - { - for (x=(ssize_t) image->columns-1; x > 0; x-=2) - { - *r++=(*p >> 4) & 0x0f; - *r++=(*p++) & 0x0f; - } - - if ((image->columns % 2) != 0) - *r++=(*p++ >> 4) & 0x0f; - - break; - } - case 8: { if (ping_color_type == 4) -- 2.40.0