From 5aa37f69df93407ddf94afdfd2504f708d8b3242 Mon Sep 17 00:00:00 2001 From: glennrp Date: Sun, 2 Jan 2011 03:07:57 +0000 Subject: [PATCH] Fix colortype selection for PNG grayscale output. --- ChangeLog | 2 ++ coders/png.c | 54 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index a6277f4df..1e258eb1d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ * The PNG decoder now checks whether the image actually has any transparency when the input file contains an alpha channel. (Reference http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=17748) + * Recently changes sometimes caused the PNG encoder to select colortype=2 + (Truecolor) instead of 0 (Grayscale) for grayscale images. 2010-12-26 6.6.6-8 Cristy * -posterize fails with more than 40 levels (reference diff --git a/coders/png.c b/coders/png.c index 50f32870d..13c4df883 100644 --- a/coders/png.c +++ b/coders/png.c @@ -2489,7 +2489,9 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, /* Is there a transparent pixel in the row? */ for (x=(ssize_t) image->columns-1; x >= 0; x--) { - if (q->opacity != OpaqueOpacity) + if ((ping_color_type == PNG_COLOR_TYPE_RGBA || + ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) && + (q->opacity != OpaqueOpacity)) { found_transparent_pixel = MagickTrue; break; @@ -2765,8 +2767,11 @@ static Image *ReadOnePNGImage(MngInfo *mng_info, (void) LogMagickEvent(CoderEvent,GetMagickModule(), " Found transparent pixel"); else - (void) LogMagickEvent(CoderEvent,GetMagickModule(), - " No transparent pixel was found"); + { + (void) LogMagickEvent(CoderEvent,GetMagickModule(), + " No transparent pixel was found"); + ping_color_type&=0x03; + } } } @@ -7623,28 +7628,43 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info, image_matte=MagickTrue; } + if (image_info->type == PaletteType || + image_info->type == PaletteMatteType) + ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; + if (image_info->type == UndefinedType || image_info->type == OptimizeType) { - - if ((image_info->type == GrayscaleType) && - image_matte == MagickFalse && ping_have_color != MagickFalse) + if (ping_have_color == MagickFalse) { - ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; - image_matte=MagickFalse; - } + if (image_matte == MagickFalse) + { + ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; + image_matte=MagickFalse; + } - else if ((image_info->type == GrayscaleMatteType) && - image_matte == MagickTrue && ping_have_color != MagickFalse) - { - ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA; - image_matte=MagickTrue; + else + { + ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA; + image_matte=MagickTrue; + } } + else + { + if (image_matte == MagickFalse) + { + ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; + image_matte=MagickFalse; + } - else if (image_info->type == PaletteType || - image_info->type == PaletteMatteType) - ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; + else + { + ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA; + image_matte=MagickTrue; + } + } } + } if (logging != MagickFalse) -- 2.40.0