Sunday, July 29, 2007

Gamma and textures.

Gamma correction issues are always a pain. A review: given 256 grey levels, it makes sense to make them perceptually uniform. Thus a 20% grey intensity (one fifth the physical intensity of white) should be around 127 as it is perceptually half way between black and white. But what happens when you want to use it as a texture map in a physically based renderer? You need to "de-gamma" the image. If you are using somebody else's renderer, should you do that as a preprocess or might they do it? For my own purposes in that situation I have made three textures of the grey squares in the macbeth color checker chart. The grey square in position 3 from the left (i.e., the third darkest) should look mid-grey in your final render. The 1.0 image has 0.19*255 in that square so it is not gamma corrected (i.e. the byte values are linear in intensity). Of course to make this all more complicated, a renderer might or might not do something different if there is something in the texture's color profile or not.


Bramz said...

I'm a bit confused here. I always thought gamma correction was all about compensation for the nonlinearity in the display, due to the nonlinearity of the electron gun in a CRT for example. Thus a compensation to make sure that 50% grey indeed was displayed as 50% grey.

The 18% reflectance is indeed what humans perceive as middle grey, but I don't see the relation with the gamma correction (18% ** 1/2.2 is not 50% btw, 22% ** 1/2.2 is).

I don't really regard gamma correction as part of the tone mapping process, but rather a device dependent compensation.

But of course, whatever the reason behind the gamma correction is, it doesn't change a thing about the need to de-gamma your textures when using them for rendering.

Bramz said...

Anyway, as far as I understand the whole zone system thing, a surface with 18% reflectance should be tonemapped on middle gray, that is on 50% grey in print.

And for a CRT display, that means that that again should be gamma corrected to 72% to show as 50% linear intensity.

I find it all very confusing though, so I could be totally wrong about it =)

Peter Shirley said...

Gamma correction is indeed there for the non-linearity of the display, but why is that non-linearity there? Manufacturers could easily make a gamma 1 system and in fact Evans and Sutherland did just that in the mid-90s. In the darks it was awful due to the big distance between 0, 1, 2, etc. In addition, 250, 251, ..., 255 all looked the same. So I am reasonably sure that the reason for gamma is perceptual.

I am curious whether anybody knows the history of what gamma is used. There was a time when macs were 1.8 and PCs were 2.2 and images didn't have headers with that info so we all had to be acutely aware of the lack of standardization.

Gamma (as far as I know) came into being as part of the NTSC work. So you may be right that originally that was all to do with CRT compensation.

As to the 18%, remember that white is around 90% so that 18% should probably be 0.2 in your 0 to 1 scale. And interestingly, the macbeth color checker has 19% as the middle grey.

Bramz said...

If you need to gamma correct a linear image that is encoded in 8 bit pixels, then you indeed get these horrible steps in the dark. Adding some noise first might help I think.

AFAIK, it is all due a inherent nonlinearity in the electron guns. It is of course possible to compensate for that in the electronic circuit, but somehow they never did. Except for the odd attempt apparently. Maybe they liked "the effect". Nobody was thinking about exact colour reproduction at that time.

Depending on the exact construction, they might have a different gamma value. So that's probably why apple 1.8 (or even 1.4), PC has 2.2, and SGI 1.7. Nowdays, with sRGB they seem to settle for 2.2 and actually now "compensate modern displays" to get the 2.2 gamma =)

Anyway, as long as it is standard, it's all right for me ;)

That 18% is more or less 50% once gamma corrected, is I think more of a coincidence. As they were thinking about middle greys and zone systems in the analog era. OTOH, maybe someone thought 2.2 to be a good gamma because it mapped 18% grey on +/- 50%.

Ah, I don't know. I'm just totally lost ;)

Bramz said...

No, forget that last sentence. If 18% is gamma corrected to 50% to be shown on a display with a gamma of 2.2, it would still show as 18% grey, not the middle grey of zone V.

It really is confusing, isn't it? =)

Tien La said...
This comment has been removed by a blog administrator.