| 1 | 
< | 
/* Copyright (c) 1990 Regents of the University of California */ | 
| 1 | 
> | 
/* Copyright (c) 1998 Silicon Graphics, Inc. */ | 
| 2 | 
  | 
 | 
| 3 | 
  | 
#ifndef lint | 
| 4 | 
< | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 4 | 
> | 
static char SCCSid[] = "$SunId$ SGI"; | 
| 5 | 
  | 
#endif | 
| 6 | 
  | 
 | 
| 7 | 
  | 
/* | 
| 10 | 
  | 
 | 
| 11 | 
  | 
#include "color.h" | 
| 12 | 
  | 
 | 
| 13 | 
< | 
#define MAXGSHIFT       15              /* maximum shift for gamma table */ | 
| 13 | 
> | 
#define NULL            0 | 
| 14 | 
  | 
 | 
| 15 | 
< | 
static BYTE     g_mant[256], g_nexp[256]; | 
| 15 | 
> | 
extern char     *bmalloc(); | 
| 16 | 
  | 
 | 
| 17 | 
< | 
static BYTE     g_bval[MAXGSHIFT+1][256]; | 
| 17 | 
> | 
#define MAXGSHIFT       31              /* maximum shift for gamma table */ | 
| 18 | 
  | 
 | 
| 19 | 
+ | 
static BYTE     *g_mant = NULL, *g_nexp = NULL; | 
| 20 | 
  | 
 | 
| 21 | 
< | 
setcolrgam(g)                   /* set gamma conversion */ | 
| 22 | 
< | 
double  g; | 
| 21 | 
> | 
static BYTE     (*g_bval)[256] = NULL; | 
| 22 | 
> | 
 | 
| 23 | 
> | 
#ifndef pow | 
| 24 | 
> | 
extern double   pow(); | 
| 25 | 
> | 
#endif | 
| 26 | 
> | 
 | 
| 27 | 
> | 
 | 
| 28 | 
> | 
setcolrcor(f, a2)               /* set brightness correction */ | 
| 29 | 
> | 
double  (*f)(); | 
| 30 | 
> | 
double  a2; | 
| 31 | 
  | 
{ | 
| 23 | 
– | 
        extern double   pow(); | 
| 32 | 
  | 
        double  mult; | 
| 33 | 
  | 
        register int    i, j; | 
| 34 | 
+ | 
                                        /* allocate tables */ | 
| 35 | 
+ | 
        if (g_bval == NULL && (g_bval = | 
| 36 | 
+ | 
                        (BYTE (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL) | 
| 37 | 
+ | 
                return(-1); | 
| 38 | 
  | 
                                        /* compute colr -> gamb mapping */ | 
| 39 | 
+ | 
        mult = 1.0/256.0; | 
| 40 | 
  | 
        for (i = 0; i <= MAXGSHIFT; i++) { | 
| 28 | 
– | 
                mult = pow(0.5, (double)(i+8)); | 
| 41 | 
  | 
                for (j = 0; j < 256; j++) | 
| 42 | 
< | 
                        g_bval[i][j] = 256.0 * pow((j+.5)*mult, 1.0/g); | 
| 42 | 
> | 
                        g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2); | 
| 43 | 
> | 
                mult *= 0.5; | 
| 44 | 
  | 
        } | 
| 45 | 
+ | 
        return(0); | 
| 46 | 
+ | 
} | 
| 47 | 
+ | 
 | 
| 48 | 
+ | 
 | 
| 49 | 
+ | 
setcolrinv(f, a2)               /* set inverse brightness correction */ | 
| 50 | 
+ | 
double  (*f)(); | 
| 51 | 
+ | 
double  a2; | 
| 52 | 
+ | 
{ | 
| 53 | 
+ | 
        double  mult; | 
| 54 | 
+ | 
        register int    i, j; | 
| 55 | 
+ | 
                                        /* allocate tables */ | 
| 56 | 
+ | 
        if (g_mant == NULL && (g_mant = (BYTE *)bmalloc(256)) == NULL) | 
| 57 | 
+ | 
                return(-1); | 
| 58 | 
+ | 
        if (g_nexp == NULL && (g_nexp = (BYTE *)bmalloc(256)) == NULL) | 
| 59 | 
+ | 
                return(-1); | 
| 60 | 
  | 
                                        /* compute gamb -> colr mapping */ | 
| 61 | 
  | 
        i = 0; | 
| 62 | 
  | 
        mult = 256.0; | 
| 63 | 
< | 
        for (j = 255; j > 0; j--) { | 
| 64 | 
< | 
                while ((g_mant[j] = mult * pow(j/256.0, g)) < 128) { | 
| 63 | 
> | 
        for (j = 256; j--; ) { | 
| 64 | 
> | 
                while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) { | 
| 65 | 
  | 
                        i++; | 
| 66 | 
  | 
                        mult *= 2.0; | 
| 67 | 
  | 
                } | 
| 68 | 
  | 
                g_nexp[j] = i; | 
| 69 | 
  | 
        } | 
| 70 | 
< | 
        g_mant[0] = 0; | 
| 43 | 
< | 
        g_nexp[0] = COLXS; | 
| 70 | 
> | 
        return(0); | 
| 71 | 
  | 
} | 
| 72 | 
  | 
 | 
| 73 | 
  | 
 | 
| 74 | 
+ | 
setcolrgam(g)                   /* set gamma conversion */ | 
| 75 | 
+ | 
double  g; | 
| 76 | 
+ | 
{ | 
| 77 | 
+ | 
        if (setcolrcor(pow, 1.0/g) < 0) | 
| 78 | 
+ | 
                return(-1); | 
| 79 | 
+ | 
        return(setcolrinv(pow, g)); | 
| 80 | 
+ | 
} | 
| 81 | 
+ | 
 | 
| 82 | 
+ | 
 | 
| 83 | 
  | 
colrs_gambs(scan, len)          /* convert scanline of colrs to gamma bytes */ | 
| 84 | 
  | 
register COLR   *scan; | 
| 85 | 
  | 
int     len; | 
| 86 | 
  | 
{ | 
| 87 | 
  | 
        register int    i, expo; | 
| 88 | 
  | 
 | 
| 89 | 
+ | 
        if (g_bval == NULL) | 
| 90 | 
+ | 
                return(-1); | 
| 91 | 
  | 
        while (len-- > 0) { | 
| 92 | 
  | 
                expo = scan[0][EXP] - COLXS; | 
| 93 | 
  | 
                if (expo < -MAXGSHIFT) { | 
| 125 | 
  | 
                scan[0][EXP] = COLXS; | 
| 126 | 
  | 
                scan++; | 
| 127 | 
  | 
        } | 
| 128 | 
+ | 
        return(0); | 
| 129 | 
  | 
} | 
| 130 | 
  | 
 | 
| 131 | 
  | 
 | 
| 135 | 
  | 
{ | 
| 136 | 
  | 
        register int    nexpo; | 
| 137 | 
  | 
 | 
| 138 | 
+ | 
        if (g_mant == NULL | g_nexp == NULL) | 
| 139 | 
+ | 
                return(-1); | 
| 140 | 
  | 
        while (len-- > 0) { | 
| 141 | 
  | 
                nexpo = g_nexp[scan[0][RED]]; | 
| 142 | 
  | 
                if (g_nexp[scan[0][GRN]] < nexpo) | 
| 161 | 
  | 
                scan[0][EXP] = COLXS - nexpo; | 
| 162 | 
  | 
                scan++; | 
| 163 | 
  | 
        } | 
| 164 | 
+ | 
        return(0); | 
| 165 | 
  | 
} | 
| 166 | 
  | 
 | 
| 167 | 
  | 
 |