[comp.graphics] Gamma

gregs@wpi.WPI.EDU (Floppy) (04/22/91)

	I am writing a graphics program in C and one of the routines has to
apply the gamma function to do some lightening.  The program is in C on an
IBM PC under Dos 4.01.  The function has to be applied to GIFs, preferably,
but PCX might be liveable too.  I really only want to lighten a GIF, and I
don't know whether gamma is overkill or not.  The GIFs are going to be
16 color (or grayscale), but may become 256 color or grayscale if the program
gets bigger.  Any source code or pointers would be greatly appreciated; I've
already checked the common FTP sites, and can't find any source code.  
Thanks in advance.

+-------+   Greg Snyder  alias Floppy alias Brocolli Man  IS  gregs@wpi.wpi.edu
|       [       [Witty quote ommitted until further notice] *:^)
|   O   |
|   | ` |          Worcester Polytechnic Institute, Worcester MA
+-------+   --------------------------------------------------------------

jack@shograf.com (Jack Ritter) (04/24/91)

> 	I am writing a graphics program in C and one of the routines has to
> apply the gamma function to do some lightening.  The program is in C on an

If "gamma function" is to mean gamma correction, here's
code I use. GAMMA should be set to the tube's gamma
correction factor (usually around 2.5). The routine
converts a (linear) real from 0.0 -> 256.0 to 
a gamma-corrected int 0 -> 255.

#define GAMMA 2.5
int gammacorrect(intensity)
real intensity;
    {
    int ival;
    real dval;
    /* scale to 0-1 range */
    dval = intensity/255.0;
    if (dval > 1.0)
        dval = 1.0;
    if (dval<=0.0)
        dval = 0.0001; /* log(0) blows up */
    /* do gamma correction */
    dval = exp ( log(dval) / GAMMA );
    /* convert to integer range: 0-255 */
    dval *= 255.0;
    ival = (int) (dval+0.5);
    if (ival > 255) ival = 255;
    return(ival);
    }



-- 
"The traffic lights, they turn blue tomorrow"
     Jimi Hendrix

Jack Ritter