Converting grayscale png affects bit depth

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
albertisi
Posts: 15
Joined: 2014-07-28T12:17:08-07:00
Authentication code: 6789

Converting grayscale png affects bit depth

Post by albertisi »

Hi,
I have 2 test scenarios:
First converting 16 bit Grayscale produces, 48 bit RGB.
Second converting 8 bit Grayscale produces, 24 bit RGB.

Tested using this image:
16 bit grayscale: http://cosmin.users.sourceforge.net/tes ... 6g_lin.png
8 bit grayscale: http://upload.wikimedia.org/wikipedia/c ... _image.png

performing these convert commands:

convert grayscale16.png grayscale16_2.png
Changes bit depth to 48 bit RGB

convert grayscale8.png grayscale8_2.png
changes bit depth to 24 bit RGB

I tested using ImageMagick 6.9.1-4

Debugged into png.c and seems like ping_have_color is defaulted to MagickTrue, and never gets set to MagickFalse, or somehow the logic needs to set ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY;
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting grayscale png affects bit depth

Post by fmw42 »

IM does not seem to honor -define png:color-type=0 when using it in your convert, either.

The PNG developer glennrp will have to take a look and inform us further.
albertisi
Posts: 15
Joined: 2014-07-28T12:17:08-07:00
Authentication code: 6789

Re: Converting grayscale png affects bit depth

Post by albertisi »

Thank you! I already PM Glen.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Converting grayscale png affects bit depth

Post by glennrp »

albertisi wrote:Hi,
Debugged into png.c and seems like ping_have_color is defaulted to MagickTrue, and never gets set to MagickFalse, or somehow the logic needs to set ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY;
It's due to a recent change to avoid inspecting every pixel; there was a complaint that it was too costly.
albertisi
Posts: 15
Joined: 2014-07-28T12:17:08-07:00
Authentication code: 6789

Re: Converting grayscale png affects bit depth

Post by albertisi »

Yes Glen, I was complaining about it...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting grayscale png affects bit depth

Post by fmw42 »

Glenn,

Perhaps you can still do what you did to speed up things, but respect the -define png:color-type=0 when used to process it so it keeps the grayscale.
albertisi
Posts: 15
Joined: 2014-07-28T12:17:08-07:00
Authentication code: 6789

Re: Converting grayscale png affects bit depth

Post by albertisi »

Here is the updated png.c.
All i did was flipped the ping_have_color default value from MagickTrue to MagickFalse. It fixes my problem with performance, and bit depth. Tested it in my workflows with different images.

My patch is here:

File: png.c
line: 7969

Code: Select all

 ping_have_blob=MagickFalse;
  ping_have_cheap_transparency=MagickFalse;
  ping_have_color=MagickFalse; //MagickTrue; //Set Default to False, for Grayscale img handling
  ping_have_non_bw=MagickTrue;
  ping_have_PLTE=MagickFalse;
Not sure how to submit it, so i am posting it here.
Post Reply