image: how do I figure out if it is in linear or sRGB color space?

2,675 views
Skip to first unread message

Philipp Schumann

unread,
Jan 27, 2013, 12:46:01 PM1/27/13
to golan...@googlegroups.com
Hi everyone,

Go's image package provides the image/color.Model type for a number of color models (rgba, nrgba, grayscale, luminance/alpha) but I'm wondering how to figure out, upon Decode()ing a jpg or png (or other arbitrary image.Image), if that image was originally encoded in linear space or gamma space (sRGB).

Or are all images automagically converted to linear or non-linear color space? Probably not.

Specific use-case, when loading images I need to ensure I load them in linear space or convert them into it. This can be approximated by doing rgb = pow(rgb, 2.2) but I shouldn't do this for images that are already in linear space. To the best of my knowledge, the file formats (and image-saving apps) themselves (jpg, png, possibly others) may *support* storing sRGB (non-linear) imagery, but don't *enforce* one way or the other. So upon Decode()ing an image.Image, how could I best figure out whether I need to convert an image to linear or whether I shouldn't because it already is meant to be linear?

Job van der Zwan

unread,
Jan 27, 2013, 2:01:26 PM1/27/13
to golan...@googlegroups.com
I know this doesn't answer your question, but maybe this package is of general use to your current project?

David DENG

unread,
Jan 27, 2013, 5:56:21 PM1/27/13
to golan...@googlegroups.com
Does an image file contain gamma information?

David

Nigel Tao

unread,
Jan 27, 2013, 8:33:33 PM1/27/13
to Philipp Schumann, golang-nuts
On Mon, Jan 28, 2013 at 4:46 AM, Philipp Schumann
<philipp....@gmail.com> wrote:
> Go's image package provides the image/color.Model type for a number of color
> models (rgba, nrgba, grayscale, luminance/alpha) but I'm wondering how to
> figure out, upon Decode()ing a jpg or png (or other arbitrary image.Image),
> if that image was originally encoded in linear space or gamma space (sRGB).
>
> Or are all images automagically converted to linear or non-linear color
> space? Probably not.

There is no automatic conversion, and the Go 1.0 image.RGBA and
color.RGBA types do not hold gamma information.

If I were you, I would fork e.g. the image/png package and change it
to additionally return color space information
(http://www.w3.org/TR/PNG/#11addnlcolinfo describes the cHRM, gAMA,
iCCP, sBIT, and sRGB chunks). If forking image/jpeg, you will probably
need to look in the EXIF metadata: http://www.exiv2.org/tags.html
lists a number of fields mentioning "color space" or "gamma", but I
don't know the details, or the gamma-aware way to convert YCbCr to
RGB.

Philipp Schumann

unread,
Jan 28, 2013, 2:17:22 AM1/28/13
to golan...@googlegroups.com, Philipp Schumann
Thanks Nigel, useful pointers!

Lars Pensjö

unread,
Jan 31, 2013, 7:11:52 AM1/31/13
to golan...@googlegroups.com, Philipp Schumann
I think almost all images are in SRGB. If you open the image using Photoshop or Gimp, and it looks the way you expect it to look, then it is in SRGB.

Peter A. Cejchan

unread,
Jan 31, 2013, 7:28:05 AM1/31/13
to golan...@googlegroups.com

exif 2.2 says:
Tag (hex)     Tag (dec)     IFD     Key     Type     Tag description
0xa001     40961     Photo     Exif.Photo.ColorSpace     Short     The color space information tag is always recorded as the color space specifier.

HTH,
Peter


On Sunday, 27 January 2013 18:46:01 UTC+1, Philipp Schumann wrote:
Reply all
Reply to author
Forward
0 new messages