1 |
– |
/* Copyright (c) 1990 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Integer operations on COLR scanlines |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
+ |
#include <stdio.h> |
11 |
+ |
#include <math.h> |
12 |
|
#include "color.h" |
13 |
|
|
14 |
< |
#define MAXGSHIFT 4 /* maximum shift for gamma table */ |
14 |
> |
extern char *bmalloc(); |
15 |
|
|
16 |
< |
static BYTE g_mant[256], g_nexp[256]; |
16 |
> |
#define MAXGSHIFT 31 /* maximum shift for gamma table */ |
17 |
|
|
18 |
< |
static BYTE g_bval[MAXGSHIFT+1][256]; |
18 |
> |
static BYTE *g_mant = NULL, *g_nexp = NULL; |
19 |
|
|
20 |
+ |
static BYTE (*g_bval)[256] = NULL; |
21 |
|
|
22 |
< |
setcolrgam(g) /* set gamma conversion */ |
23 |
< |
double g; |
22 |
> |
|
23 |
> |
int |
24 |
> |
setcolrcor(f, a2) /* set brightness correction */ |
25 |
> |
double (*f)(); |
26 |
> |
double a2; |
27 |
|
{ |
23 |
– |
extern double pow(); |
28 |
|
double mult; |
29 |
|
register int i, j; |
30 |
+ |
/* allocate tables */ |
31 |
+ |
if (g_bval == NULL && (g_bval = |
32 |
+ |
(BYTE (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL) |
33 |
+ |
return(-1); |
34 |
|
/* compute colr -> gamb mapping */ |
35 |
+ |
mult = 1.0/256.0; |
36 |
|
for (i = 0; i <= MAXGSHIFT; i++) { |
28 |
– |
mult = pow(0.5, (double)(i+8)); |
37 |
|
for (j = 0; j < 256; j++) |
38 |
< |
g_bval[i][j] = 256.0 * pow((j+.5)*mult, 1.0/g); |
38 |
> |
g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2); |
39 |
> |
mult *= 0.5; |
40 |
|
} |
41 |
+ |
return(0); |
42 |
+ |
} |
43 |
+ |
|
44 |
+ |
|
45 |
+ |
int |
46 |
+ |
setcolrinv(f, a2) /* set inverse brightness correction */ |
47 |
+ |
double (*f)(); |
48 |
+ |
double a2; |
49 |
+ |
{ |
50 |
+ |
double mult; |
51 |
+ |
register int i, j; |
52 |
+ |
/* allocate tables */ |
53 |
+ |
if (g_mant == NULL && (g_mant = (BYTE *)bmalloc(256)) == NULL) |
54 |
+ |
return(-1); |
55 |
+ |
if (g_nexp == NULL && (g_nexp = (BYTE *)bmalloc(256)) == NULL) |
56 |
+ |
return(-1); |
57 |
|
/* compute gamb -> colr mapping */ |
58 |
|
i = 0; |
59 |
|
mult = 256.0; |
60 |
< |
for (j = 255; j > 0; j--) { |
61 |
< |
while ((g_mant[j] = mult * pow(j/256.0, g)) < 128) { |
60 |
> |
for (j = 256; j--; ) { |
61 |
> |
while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) { |
62 |
|
i++; |
63 |
|
mult *= 2.0; |
64 |
|
} |
65 |
|
g_nexp[j] = i; |
66 |
|
} |
67 |
< |
g_mant[0] = 0; |
43 |
< |
g_nexp[0] = COLXS; |
67 |
> |
return(0); |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
+ |
int |
72 |
+ |
setcolrgam(g) /* set gamma conversion */ |
73 |
+ |
double g; |
74 |
+ |
{ |
75 |
+ |
if (setcolrcor(pow, 1.0/g) < 0) |
76 |
+ |
return(-1); |
77 |
+ |
return(setcolrinv(pow, g)); |
78 |
+ |
} |
79 |
+ |
|
80 |
+ |
|
81 |
+ |
int |
82 |
|
colrs_gambs(scan, len) /* convert scanline of colrs to gamma bytes */ |
83 |
|
register COLR *scan; |
84 |
|
int len; |
85 |
|
{ |
86 |
|
register int i, expo; |
87 |
|
|
88 |
+ |
if (g_bval == NULL) |
89 |
+ |
return(-1); |
90 |
|
while (len-- > 0) { |
91 |
|
expo = scan[0][EXP] - COLXS; |
92 |
|
if (expo < -MAXGSHIFT) { |
124 |
|
scan[0][EXP] = COLXS; |
125 |
|
scan++; |
126 |
|
} |
127 |
+ |
return(0); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
+ |
int |
132 |
|
gambs_colrs(scan, len) /* convert gamma bytes to colr scanline */ |
133 |
|
register COLR *scan; |
134 |
|
int len; |
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 |
|
|
168 |
+ |
void |
169 |
|
shiftcolrs(scan, len, adjust) /* shift a scanline of colors by 2^adjust */ |
170 |
|
register COLR *scan; |
171 |
|
register int len; |
172 |
|
register int adjust; |
173 |
|
{ |
174 |
+ |
int minexp; |
175 |
+ |
|
176 |
+ |
if (adjust == 0) |
177 |
+ |
return; |
178 |
+ |
minexp = adjust < 0 ? -adjust : 0; |
179 |
|
while (len-- > 0) { |
180 |
< |
scan[0][EXP] += adjust; |
180 |
> |
if (scan[0][EXP] <= minexp) |
181 |
> |
scan[0][RED] = scan[0][GRN] = scan[0][BLU] = |
182 |
> |
scan[0][EXP] = 0; |
183 |
> |
else |
184 |
> |
scan[0][EXP] += adjust; |
185 |
|
scan++; |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
+ |
void |
191 |
|
normcolrs(scan, len, adjust) /* normalize a scanline of colrs */ |
192 |
|
register COLR *scan; |
193 |
|
int len; |