PNG gAMA chunk seems to be inverted

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
roelandsch
Posts: 20
Joined: 2014-10-27T16:10:38-07:00
Authentication code: 6789

PNG gAMA chunk seems to be inverted

Post by roelandsch »

Hi all,

I found a minor issue with how gamma values are written to PNG files. After a -gamma operation, the PNG encoder takes this into account when writing the gAMA chunk. But the value is inverted; after applying -gamma 2.2 to a linear image it will write 2.2, while it should write .45455.

Some test cases:

Code: Select all

convert test.exr test.png
The exr image is linear. this command converts it to a PNG image, with linear values, and adds a gAMA chunk set to 1.0 (as expected).

There are however programs out there which ignore the gAMA chunk, so I tried to produce a PNG file with the usual gamma correction (1 / 2.2).

Code: Select all

convert test.exr -gamma 2.2 test.png
Now the brightness values are encoded with the usual gamma correction of .45455. But the gAMA chunk is set to 2.2. Programs which understand the gAMA chunk (eg. modern browsers) will get confused, and display the image with way too bright colours.

It's possible to work around like this:

Code: Select all

convert test.exr -gamma 2.2 +gamma .45455 test.png
Now the PNG file has the usual gamma correction and a correct gAMA chunk.

The colorspace option works as expected:

Code: Select all

convert test.exr -colorspace sRGB test.png
--
Roeland
Post Reply