PNG corruption when resizing, transparency and PseudoClass

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
bobinounet
Posts: 2
Joined: 2012-02-16T18:18:01-07:00
Authentication code: 8675308

PNG corruption when resizing, transparency and PseudoClass

Post by bobinounet »

My source image is a 96x96 png file with transparency:
Image

Code: Select all

convert source.png -resize 50% result.png
creates a file that looks fine:
Image
but actually appears to be corrupted when opened in PhotoShop:
Image

However if I specify PNG32, the result is correct:

Code: Select all

convert source.png -resize 50% PNG32:result32.png
Image

identify says:
source.png PNG 96x96 96x96+0+0 8-bit DirectClass 754B 0.000u 0:00.000
result.png PNG 48x48 48x48+0+0 8-bit PseudoClass 110c 1.13KB 0.000u 0:00.000
result32.png PNG 48x48 48x48+0+0 8-bit DirectClass 1.01KB 0.000u 0:00.000

ImageMagick 6.7.5-6 2012-02-16 Q8, Mac OSX 10.7.3, built using libpng 1.5.8
Last edited by bobinounet on 2012-02-17T01:12:13-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNG corruption when resizing, transparency and PseudoCla

Post by fmw42 »

You need to specify either PNG32: or PNG8: for output to properly format the png so that PS can read it. The former is 32-bit color with an alpha channel. The latter is 8bit color with 1-bit transparency (similar to gif). Otherwise, you don't set the PNG information correctly at least for compatibility with PS.

see
http://www.imagemagick.org/Usage/formats/#png_formats


From what I can see your resize is interpolating colors and alpha values (from 3 colors with 1 bit transparency to 159 colors with 8-bit transparency). Then you are getting type palettematte as it should be, but not getting a 1-bit alpha channel. So you don't have it properly as PNG32 (truecolor) or PNG8 with 1-bit transparency.

This may (?) have to do with the fact that your alpha channel in your original is acting both as an alpha channel for transparency and to set part of the resulting image for the letters. It might have been better to have made the underlying image contain everything, letters included and then used the alpha channel for transparency (preferably 1-bit). Alternately, you could threshold the alpha channel after resizing to make it binary and that might have worked, though you would have expanded the 3 colors to 159 values rather than your original 3 values. By setting the output to PNG8, you get back to your 3 colors with 1 bit transparency.


The IM PNG developer, glennrp, can probably explain better (or correct me) as to what is going on.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: PNG corruption when resizing, transparency and PseudoCla

Post by glennrp »

Yes that's about right. Try

Code: Select all

convert HD.png -sample 50% -define png:exclude-chunk=date png8:HD-sampled.png
The define keeps IM from writing the create-time and modify-time chunks. -sample prevents
it from adding colors by interpolation, and png8: thresholds the alpha channel to 1-bit.
bobinounet
Posts: 2
Joined: 2012-02-16T18:18:01-07:00
Authentication code: 8675308

Re: PNG corruption when resizing, transparency and PseudoCla

Post by bobinounet »

Thanks for your answers.

I don't see how the date is part of the problem, but I have no problem removing it.

As per -sample, I don't want to rely on a point filter and I would actually use -filter Lanczos in that case. Introducing new colors is fine by me if that helps anti-aliasing.

From what you say, it looks like I have to use png8: or png32: to get an image that is readable by PS. I guess this is the part that surprised me since I couldn't imagine a valid png file could not be properly opened by PS. Hence my classification as a bug, but I take from your comments that this is more a png format support issue. Am I correct?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: PNG corruption when resizing, transparency and PseudoCla

Post by glennrp »

bobinounet wrote: I don't see how the date is part of the problem, but I have no problem removing it.
It isn't part of the problem; it's just something that I usually do.
As per -sample, I don't want to rely on a point filter and I would actually use -filter Lanczos in that case. Introducing new colors is fine by me if that helps anti-aliasing.
OK. I don't know how much it helps with the anti-aliasing if it isn't used in combination with semitransparency, but I guess it helps some.
From what you say, it looks like I have to use png8: or png32: to get an image that is readable by PS. I guess this is the part that surprised me since I couldn't imagine a valid png file could not be properly opened by PS. Hence my classification as a bug, but I take from your comments that this is more a png format support issue. Am I correct?
It's a PS bug. There's nothing wrong with the PNG format. Fortunately IM can use the PNG8 subformat to create files that PS (and IE) can read properly.
Post Reply