[comp.graphics] Gamma correction

doug_rands_merritt@cup.portal.com (04/23/88)

Speaking of painful subjects that get asked over, and over, and over
again...I seem to have mislaid all of the old articles that discuss
gamma correction. Sigh. Anyone care to forward something on the topic
to me?

And since there's so much disagreement about creating *.newusers,
what about the idea of just posting "Answers to commonly asked questions"
once per month to *this* group??? (or twice monthly, given aging???)
Thanks,
   Doug
      Doug Merritt        ucbvax!sun.com!cup.portal.com!doug-merritt
                      or  ucbvax!eris!doug (doug@eris.berkeley.edu)
                      or  ucbvax!unisoft!certes!doug

dlou@dino.ucsd.edu (Dennis Lou) (08/26/90)

I recently read an old Macworld article on scanners.  Where I work, we
have a scanner with rather limited software with it.  In this article,
however, I read about contrast enhancement using something called
Gamma Correction.  I'd like to try it for the Sunraster and TIFF files
we have at work.  I know the basic principle of remapping the
intensities, but I don't know how to compute the curve.

What I'd like to know is what's the formula for Gamma Correction?
Does anyone have any info they can tell me about it?  THanks in
advance.

Please E=mail responses please!


--
Dennis Lou                Disclaimer: I don't use lame disks.
dlou@dino.ucsd.edu         "But Yossarian, what if everyone thought that way?"
[backbone]!ucsd!dino!dlou  "Then I'd be crazy to think any other way!"

sg04@harvey.gte.com (Steven Gutfreund) (02/22/91)

Foley & Van Dam tells me how to do gamma correction for b/w images,
how do I do it for rbg?

-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Yechezkal Shimon Gutfreund		 		  sgutfreund@gte.com
GTE Laboratories, Waltham MA			    harvard!bunny!sgutfreund
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

srnelson@nelsun.Eng.Sun.COM (Scott R. Nelson) (02/25/91)

From article <10652@bunny.GTE.COM>, by sg04@harvey.gte.com (Steven Gutfreund):
> Foley & Van Dam tells me how to do gamma correction for b/w images,
> how do I do it for rbg?

You do the same thing three times, once for red, once for green and once
for blue.

Really.

Or you can do what most graphics hardware manufacturers do and set the
gamma correction value to be the same for red, green and blue.  If the
CRT is of good quality and is adjusted properly the gamma values for each
of the three color channels will be the same.  A grey-scale ramp (bars of
color increasing linearly across the screen) where each intensity has
equal values of red, green and blue will appear as a uniform grey with no
colored spots.

For those of you that don't have Foley & Van Dam or that have a hard
time converting mathematic notation to code, here is a very short C
program that prints a gamma correction table:

/*
 * gamma.c
 *
 *	Print a gamma table.  Compile on a unix machine as follows:
 *		cc -o gamma gamma.c -lm
 */

#include <math.h>

#define CLUT_GAMMA 2.5			/* Gamma setting, typically 2.0 to 2.5 */

main()
{
    int i;				/* The CLUT entry number */
    int g;				/* The computed gamma value */

    for (i = 0; i < 256; i++) {
	g = (int)(255.0 * pow((double) i / 255.0, 1.0 / CLUT_GAMMA));
	printf("%d: %d\n", i, g);
    }
}

---

Scott R. Nelson			srnelson@eng.sun.com
Sun Microsystems

"Proofread carefully to see if you any words out."

srnelson@nelsun.Eng.Sun.COM (Scott R. Nelson) (05/28/91)

From article <1991May27.135349.5072@vax5.cit.cornell.edu>, by wuly@vax5.cit.cornell.edu:
> With regard to the "too dark" radiosity results discussion:
> 
> After talking with a grad student in computer graphics here at cornell:
> 	(1) everybody using radiosity algorithms has this problem.
> 	(2) a good way to assign colors to radiosity results is to do it
> 	    linearly, and then essentially gamma-correct the resulting image.
> 
> I have found that gamma correction does wonders for me, and my images are
> darker than most due to inaccurate (too low) energy transfers (it made
> calculations very fast, though).  For those of you using more accurate
> routines, a gamma correction style transformation during color assignment
> should be helpful.
> 
> For my case, a gamma correction of 1.8 looks nice, but of course that depends
> on the monitor and the image.


When is the graphics community going to catch on to the need for gamma
correction in ALL shaded images?

Graphics calculations need to be performed in linear space to simulate
the physical mixing of light.  All CRT electron guns are nonlinear.  You
need to use gamma correction to map these linear intensity values in the
frame buffer to the nonlinear values displayed on the CRT.  Gamma
correction does this.

The broadcast television industry has settled on a standard gamma value of
2.222222 (1.0/0.45).  This has been build into television sets for
decades.  This value happens to look correct on all properly adjusted
monitors that I have seen.

Also, since gamma correction is attempting to correct for the nonlinearity
of the CRT electron guns, and since the three electron guns in a color CRT
are of the same design, the gamma values for the three channels should be
the same.

If you are computing an image using radiosity, ray tracing, antialiased
lines or even gouraud shading, you need to use a gamma corrected color
lookup table to properly view your image.

This has been posted before, but here is a C code fragment to print a
gamma correction ramp for an 8-bit lookup table:

/*
 * gamma.c
 *
 *	Print a gamma correction table.
 *
 * cc -o gamma gamma.c -lm
 */

#include <math.h>
#define clut_gamma 2.222222

main()
{
    int i;

    for (i = 0; i < 256; i++) {
        if (i % 16 == 0)
            printf("\n");
        printf("%d: %d\n", i,
            (int) (255.0 * pow((double) i / 255.0, 1.0 / clut_gamma)));
    }
}

---

Scott R. Nelson			srnelson@eng.sun.com
Sun Microsystems

"Proofread carefully to see if you any words out."

billd@fps.com (Bill Davidson) (05/29/91)

In article <14070@exodus.Eng.Sun.COM> srnelson@nelsun.Eng.Sun.COM (Scott R. Nelson) writes:
>The broadcast television industry has settled on a standard gamma value of
>2.222222 (1.0/0.45).  This has been build into television sets for
>decades.  This value happens to look correct on all properly adjusted
>monitors that I have seen.

Huh?  It seems like every color monitor I come across has a different
gamma value and some are extreme (I know of one that needs about 3.2).
Other than that, I agree with your post.  Manufacturers of RGB monitors
don't seem to be able to agree on anything.

--Bill

bcorrie@csr (Brian Corrie) (05/29/91)

wuly@vax5.cit.cornell.edu writes:
[Stuff about Radiosity problem deleted]

>I have found that gamma correction does wonders for me, and my images are
>darker than most due to inaccurate (too low) energy transfers (it made
>calculations very fast, though).  For those of you using more accurate
>routines, a gamma correction style transformation during color assignment
>should be helpful.

Kind of a different but the same question.... I have been using SUN
workstations to display generated images over the last couple of years. We
recently got a NeXTstation Color. I convert the 24 bit SUN files to tiff
files and display them on the NeXT, and the images are a lot darker than
the images as they are displayed on the SUN.

The difference is quite significant (to the point that the image isn't
really worth looking at). My question is, is this a problem that could be
solved by gamma correction, or is it likely a result of the conversion
process? Anybody?

You may have guessed that I am not an expert in Gamma correction. I always
thought it was a detail for those who are perfectionists, and want an image
to look identical on every monitor. Is it possible that not doing gamma
correction on an image could make this much difference?

Note: Some of the images are much worse than others, but all are definitely
darker on the NeXTstation color than they are on the SUNs.

>For my case, a gamma correction of 1.8 looks nice, but of course that depends
>on the monitor and the image.

>wuly@vax5.cit.cornell.edu

Thanks for any help you can give me,

	B

P.S. Is this a question for comp.graphics or comp.graphics.research? To me,
it isn't research, nor is it an inquiry about research, its an enquiry about
a fact. Therefore, it belongs in comp.graphics. I hope the experts don't stop
reading comp.graphics so these questions will still be answered.

--
                  Brian Corrie (bcorrie@csr.uvic.ca)
Under the most rigorously controlled conditions of pressure, temperature,
volume, humidity and other variables, the organism will do as it damn well
pleases. Sounds like some of the code I have written......  8-)

osmoviita@cc.helsinki.fi (05/30/91)

In article <14070@exodus.Eng.Sun.COM>, srnelson@nelsun.Eng.Sun.COM (Scott R. Nelson) writes:
> 
> Also, since gamma correction is attempting to correct for the nonlinearity
> of the CRT electron guns, and since the three electron guns in a color CRT
> are of the same design, the gamma values for the three channels should be
> the same.

At least the people who sell monitor calibrator systems say that gamma
values are different for different values -- phosphors age differently and
perhaps there is some other reasons.

> If you are computing an image using radiosity, ray tracing, antialiased
> lines or even gouraud shading, you need to use a gamma corrected color
> lookup table to properly view your image.
> 
> This has been posted before, but here is a C code fragment to print a
> gamma correction ramp for an 8-bit lookup table:
> 
> /*
>  * gamma.c
>  *
>  *	Print a gamma correction table.
>  *
>  * cc -o gamma gamma.c -lm
>  */
> 
> #include <math.h>
> #define clut_gamma 2.222222
> 
> main()
> {
>     int i;
> 
>     for (i = 0; i < 256; i++) {
>         if (i % 16 == 0)
>             printf("\n");
>         printf("%d: %d\n", i,
>             (int) (255.0 * pow((double) i / 255.0, 1.0 / clut_gamma)));
>     }
> }
>

It is not sufficient for every monitor to define gamma correction table by
so simple function. And gamma values vary much between monitors and
adjustments. So if you need accurate images you must adjust your monitor,
measure its transfer function and calculate the correction table from your
measurements. And remember to fix contrast and brightness controls
unmovable and do all measuring and viewings in dark room :-)

 
> ---
> 
> Scott R. Nelson			srnelson@eng.sun.com
> Sun Microsystems
> 
> "Proofread carefully to see if you any words out."

Kari Osmoviita		osmoviita@cc.helsinki.fi
University of Helsinki

pierce@radius.com (Pierce T. Wetter III) (05/30/91)

>When is the graphics community going to catch on to the need for gamma
>correction in ALL shaded images?

>The broadcast television industry has settled on a standard gamma value of
>2.222222 (1.0/0.45).  This has been build into television sets for
>decades.  This value happens to look correct on all properly adjusted
>monitors that I have seen.

 What does properly adjusted mean?


SOAPBOX ON

  Television "as broadcast" is broadcast for viewing on a screen with a 
 gamma of 2.22. Your average run of the mill TV screens have a gamma of 
 2.6 to 2.8. 

  The value of 2.22 is chosen for its similarities to human vision as well
 as the added benifit of only correcting for gamma once at the broadcast
 station. By choosing 2.22, when quantizing to 8 bits, or 6 Mhz, 
 the maximum real information bandwidth is maintained. (Humans can
 see 1% differences, so a perfectly perceptual space would be logorithmic,
 but I digress.).

 If you really want to adjust your image for best display, you should adjust
 it for a gamma correction of 2.6 to 2.8 depending on your monitor. If you
 are sending this out to video tape, then by all means use 2.222, since you'll
 at least be conistent with everyone else.

  If you want the best possible display, you will have to take samples of
 every possible value of red,green and blue, and make a conversion table.
 Gamma is only the first order effect, in actual monitors all sorts of other
 non-linearities creep in (like the brightness and contrast knobs) that
 require seperate treatement.

>Also, since gamma correction is attempting to correct for the nonlinearity
>of the CRT electron guns, and since the three electron guns in a color CRT
>are of the same design, the gamma values for the three channels should be
>the same.

  Nope, there three different guns, and the monitor has about 3 pots for each
  gun that can be tweaked + the brightness and contrast knobs. If you're 
 going to approximate the brightness/voltage curve with one parameter, you'll
 find its different for each gun.


SOAPBOX OFF


  Pierce

-- 
My postings are my opinions, and my opinions are my own not that of my employer.
You can get me at radius!pierce@apple.com.
(Wha'ja want? Some cute signature file? Hah! I have real work to do.

pierce@radius.com (Pierce T. Wetter III) (05/30/91)

billd@fps.com (Bill Davidson) writes:

>In article <14070@exodus.Eng.Sun.COM> srnelson@nelsun.Eng.Sun.COM (Scott R. Nelson) writes:
>>The broadcast television industry has settled on a standard gamma value of
>>2.222222 (1.0/0.45).  This has been build into television sets for
>>decades.  This value happens to look correct on all properly adjusted
>>monitors that I have seen.

>Huh?  It seems like every color monitor I come across has a different
>gamma value and some are extreme (I know of one that needs about 3.2).
>Other than that, I agree with your post.  Manufacturers of RGB monitors
>don't seem to be able to agree on anything.

  

  Actually, the one good thing to come out of the HDTV stuff is that they
 all managed to agree on one phosphor set: CCIR-Rec709, and they all pledged
 to use it. 

  Of course, different coatings change the perceived color...

Pierce
-- 
My postings are my opinions, and my opinions are my own not that of my employer.
You can get me at radius!pierce@apple.com.
(Wha'ja want? Some cute signature file? Hah! I have real work to do.

turk@Apple.COM (Ken "Turk" Turkowski) (05/31/91)

bcorrie@csr (Brian  Corrie) writes:
>P.S. Is this a question for comp.graphics or comp.graphics.research? To me,
>it isn't research, nor is it an inquiry about research, its an enquiry about
>a fact. Therefore, it belongs in comp.graphics. I hope the experts don't stop
>reading comp.graphics so these questions will still be answered.

Don't assume that graphics researcher will stop reading comp.graphics
just because there is a comp.graphics.research group; they'll just read
the research group first.
-- 
Ken Turkowski @ Apple Computer, Inc., Cupertino, CA
Internet: turk@apple.com
Applelink: TURK
UUCP: sun!apple!turk

dave@imax.com (Dave Martindale) (06/06/91)

In article <14070@exodus.Eng.Sun.COM> srnelson@nelsun.Eng.Sun.COM (Scott R. Nelson) writes:
>
>The broadcast television industry has settled on a standard gamma value of
>2.222222 (1.0/0.45).  This has been build into television sets for
>decades.  This value happens to look correct on all properly adjusted
>monitors that I have seen.

Well, actually, NTSC specifies a monitor gamma of 2.2 exactly, giving a
camera (or lookup table, since your frame buffer mimics a camera) of
0.4545.  For practical purposes, 0.45, but it is the 2.2 number that is
defined, not 0.45.  This was done with full knowledge that the
"average" receiver has a gamma of about 2.8, because they wanted the
image on screen to have a gamma higher than is "realistic" because
people prefer that when looking at images in a room with dimmed
lighting.  The result of gamma correction for 2.2 followed by display
on a monitor with a real gamma of 2.8 is an image with a gamma of
2.8/2.2 = 1.27 relative to the original scene.

PAL specifies a monitor gamma of 2.8 instead of 2.2.  This should give
more accurate tone reproduction on sets with a real gamma of 2.8.
However I have heard at least one report that European broadcasters
adjust cameras for a gamma of 0.45 anyway, not 0.36, because they want
the same contrast increase that the NTSC standard gives.

(Is there anyone out there who works in a European television studio?
Are cameras set up for a gamma of 0.45 or 0.36, or something else
entirely?)

The HDTV production standard specifies a more complex "opto-electronic
transfer characteristic" that is basically gamma correction with a
factor of 0.45 in bright areas, spliced to a linear function at low
brightnesses.  They did this to have a characteristic that camera
gamma-correctors could adhere tightly to; the NTSC and PAL
gamma-correction functions are not physically realizable for dark areas
of the picture.

So, the "gamma correction" function you should use is standardized, but
the standard you should use depends on the television system you are
encoding your picture for.

On the other hand, if you just want to display your picture on a
monitor, you should gamma-correct for that particular monitor.  If you
want to accurately reproduce tones, you should fully correct for the
monitor gamma, to give an on-screen picture with a gamma of 1.0
relative to the calculated intensities.  If you want to see what the
image will look like on TV, though, you might want to use a lookup
table that gives the on-screen picture a gamma of about 1.25 relative
to the calculated intensities, since NTSC encoding will do that.  And
if you want to see what it will look like when recorded on film and
projected in a theatre, you might want to adjust the on-screen picture
for a gamma of 1.5, since that's what film does.

Of course, as Scott points out, not doing any gamma correction at all
will get you an extremely dark picture that bears little relationship
to the intensities you calculated.  It's better to gamma-correct using
almost any value of gamma than to ignore the problem.  Picking the
*precise* value of gamma that is appropriate to your circumstances
is less important.

	Dave Martindale

dave@imax.com (Dave Martindale) (06/06/91)

In article <1466@radius.com> pierce@radius.com (Pierce T. Wetter III) writes:
>
>>Also, since gamma correction is attempting to correct for the nonlinearity
>>of the CRT electron guns, and since the three electron guns in a color CRT
>>are of the same design, the gamma values for the three channels should be
>>the same.
>
>  Nope, there three different guns, and the monitor has about 3 pots for each
>  gun that can be tweaked + the brightness and contrast knobs. If you're 
> going to approximate the brightness/voltage curve with one parameter, you'll
> find its different for each gun.

All the discussion about gamma correction assumes that the monitor has
been properly adjusted in the first place.  The black level should be
set so that the guns are just barely cut off with zero input voltage,
and so that very dim greys are neutral in colour.  The individual
colour gains should be set to give white of the correct colour
temperature.  A grey scale that goes from black to white should
remain the same colour temperature throughout the scale.

If you've adjusted your monitor so it meets all these requirements,
I think you'll find that this constrains the adjustment of almost
every control in the monitor except "contrast".  When this is done,
you should find that the brightness/voltage curves for the three
colours track quite accurately (if they didn't, the grey scale
would show colour shifts).  And that a single value for gamma
pretty accurately describes how the monitor responds to voltage.

If the monitor is misadjusted, then the best gamma-correction value
may be different for each colour.  But in that case, the transfer
function of at least one of RGB is likely to look nothing like
the power-law function that gamma correction models, and gamma
correction isn't going to provide good compensation for this.
You need something more powerful than simple gamma correction.
Fixing the monitor's calibration is probably easier than fixing
the model.

If you want to get more accurate, you could take a bunch of brightness
vs. voltage measurements and fit a curve to it - the relationship
of brightness to voltage is not an exact power-law curve, and a bunch
of numbers can describe it better than one.  But still, the three
colours should track each other pretty closely unless the monitor
is misadjusted.

	Dave Martindale