Fanning Software Consulting

PNG Image Renders Darkly

QUESTION: I downloaded a transparent PNG file from the Internet. I simply read the PNG image file in IDL and then wrote it out again as a transparent PNG file with an alpha channel.

    IDL> image = Read_PNG('example.png')
   IDL> Help, image    IMAGE           BYTE      = Array[4, 162, 150]
   IDL> Write_PNG, 'test.png', image 

But I notice that the two images, which appear to me to have identical image data, are rendered differently by all the web browers I have checked. See the figure below. What is going on here?

example.png test.png
Firefox rendering of example.png on the left,
and test.png on the right.
 

ANSWER: Well, it turns out, it is possible to include all kinds of metadata in PNG files. And this metadata is almost completely non-standard, so that is is extremely difficult, if not impossible, to write a PNG file reader that can preserve the metadata in its original form. The IDL PNG reader, as you might imagine, does not even try. (This is what I would do, too, if I were writing a PNG reader.)

The example.png file that you downloaded from the Internet contains a gAMA metadata tag. This tag indicates the gamma value that should be used in transferring the image data to the display. My Firefox and Internet Explorer browsers read that data and do the transfer correctly.

The test.png file you created in IDL from the example.png file does not include this gAMA tag in the test.png file. It does, however, write the image data correctly. Thus, your browsers render this file in darker colors than the previous file, since there is no gamma correction being made.

To be certain this was the explanation. I downloaded a program named TweakPNG that allowed me to examine the metadata chucks in these PNG files. I exported the gAMA chunk from example.png and then imported that chunk into test.png. Low and behold, the test.png file now renders exactly like example.png in my Firefox browser.

Version of IDL used to prepare this article: IDL 7.1.

Google
 
Web Coyote's Guide to IDL Programming