ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/colrops.c
Revision: 2.11
Committed: Wed Jul 30 10:11:06 2003 UTC (20 years, 8 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9
Changes since 2.10: +5 -4 lines
Log Message:
Added prototypes submitted by Randolph Fritz.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: colrops.c,v 2.10 2003/07/27 22:12:01 schorsch Exp $";
3 #endif
4 /*
5 * Integer operations on COLR scanlines
6 */
7
8 #include "copyright.h"
9
10 #include <stdio.h>
11 #include <math.h>
12
13 #include "rtmisc.h"
14 #include "color.h"
15
16
17 #define MAXGSHIFT 31 /* maximum shift for gamma table */
18
19 static BYTE *g_mant = NULL, *g_nexp = NULL;
20
21 static BYTE (*g_bval)[256] = NULL;
22
23
24 int
25 setcolrcor(f, a2) /* set brightness correction */
26 double (*f)(double,double);
27 double a2;
28 {
29 double mult;
30 register int i, j;
31 /* allocate tables */
32 if (g_bval == NULL && (g_bval =
33 (BYTE (*)[256])bmalloc((MAXGSHIFT+1)*256)) == NULL)
34 return(-1);
35 /* compute colr -> gamb mapping */
36 mult = 1.0/256.0;
37 for (i = 0; i <= MAXGSHIFT; i++) {
38 for (j = 0; j < 256; j++)
39 g_bval[i][j] = 256.0 * (*f)((j+.5)*mult, a2);
40 mult *= 0.5;
41 }
42 return(0);
43 }
44
45
46 int
47 setcolrinv(f, a2) /* set inverse brightness correction */
48 double (*f)(double,double);
49 double a2;
50 {
51 double mult;
52 register int i, j;
53 /* allocate tables */
54 if (g_mant == NULL && (g_mant = (BYTE *)bmalloc(256)) == NULL)
55 return(-1);
56 if (g_nexp == NULL && (g_nexp = (BYTE *)bmalloc(256)) == NULL)
57 return(-1);
58 /* compute gamb -> colr mapping */
59 i = 0;
60 mult = 256.0;
61 for (j = 256; j--; ) {
62 while ((g_mant[j] = mult * (*f)((j+.5)/256.0, a2)) < 128) {
63 i++;
64 mult *= 2.0;
65 }
66 g_nexp[j] = i;
67 }
68 return(0);
69 }
70
71
72 int
73 setcolrgam(g) /* set gamma conversion */
74 double g;
75 {
76 if (setcolrcor(pow, 1.0/g) < 0)
77 return(-1);
78 return(setcolrinv(pow, g));
79 }
80
81
82 int
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) {
94 if (expo < -MAXGSHIFT-8) {
95 scan[0][RED] =
96 scan[0][GRN] =
97 scan[0][BLU] = 0;
98 } else {
99 i = (-MAXGSHIFT-1) - expo;
100 scan[0][RED] =
101 g_bval[MAXGSHIFT][((scan[0][RED]>>i)+1)>>1];
102 scan[0][GRN] =
103 g_bval[MAXGSHIFT][((scan[0][GRN]>>i)+1)>>1];
104 scan[0][BLU] =
105 g_bval[MAXGSHIFT][((scan[0][BLU]>>i)+1)>>1];
106 }
107 } else if (expo > 0) {
108 if (expo > 8) {
109 scan[0][RED] =
110 scan[0][GRN] =
111 scan[0][BLU] = 255;
112 } else {
113 i = (scan[0][RED]<<1 | 1) << (expo-1);
114 scan[0][RED] = i > 255 ? 255 : g_bval[0][i];
115 i = (scan[0][GRN]<<1 | 1) << (expo-1);
116 scan[0][GRN] = i > 255 ? 255 : g_bval[0][i];
117 i = (scan[0][BLU]<<1 | 1) << (expo-1);
118 scan[0][BLU] = i > 255 ? 255 : g_bval[0][i];
119 }
120 } else {
121 scan[0][RED] = g_bval[-expo][scan[0][RED]];
122 scan[0][GRN] = g_bval[-expo][scan[0][GRN]];
123 scan[0][BLU] = g_bval[-expo][scan[0][BLU]];
124 }
125 scan[0][EXP] = COLXS;
126 scan++;
127 }
128 return(0);
129 }
130
131
132 int
133 gambs_colrs(scan, len) /* convert gamma bytes to colr scanline */
134 register COLR *scan;
135 int len;
136 {
137 register int nexpo;
138
139 if ((g_mant == NULL) | (g_nexp == NULL))
140 return(-1);
141 while (len-- > 0) {
142 nexpo = g_nexp[scan[0][RED]];
143 if (g_nexp[scan[0][GRN]] < nexpo)
144 nexpo = g_nexp[scan[0][GRN]];
145 if (g_nexp[scan[0][BLU]] < nexpo)
146 nexpo = g_nexp[scan[0][BLU]];
147 if (nexpo < g_nexp[scan[0][RED]])
148 scan[0][RED] = g_mant[scan[0][RED]]
149 >> (g_nexp[scan[0][RED]]-nexpo);
150 else
151 scan[0][RED] = g_mant[scan[0][RED]];
152 if (nexpo < g_nexp[scan[0][GRN]])
153 scan[0][GRN] = g_mant[scan[0][GRN]]
154 >> (g_nexp[scan[0][GRN]]-nexpo);
155 else
156 scan[0][GRN] = g_mant[scan[0][GRN]];
157 if (nexpo < g_nexp[scan[0][BLU]])
158 scan[0][BLU] = g_mant[scan[0][BLU]]
159 >> (g_nexp[scan[0][BLU]]-nexpo);
160 else
161 scan[0][BLU] = g_mant[scan[0][BLU]];
162 scan[0][EXP] = COLXS - nexpo;
163 scan++;
164 }
165 return(0);
166 }
167
168
169 void
170 shiftcolrs(scan, len, adjust) /* shift a scanline of colors by 2^adjust */
171 register COLR *scan;
172 register int len;
173 register int adjust;
174 {
175 int minexp;
176
177 if (adjust == 0)
178 return;
179 minexp = adjust < 0 ? -adjust : 0;
180 while (len-- > 0) {
181 if (scan[0][EXP] <= minexp)
182 scan[0][RED] = scan[0][GRN] = scan[0][BLU] =
183 scan[0][EXP] = 0;
184 else
185 scan[0][EXP] += adjust;
186 scan++;
187 }
188 }
189
190
191 void
192 normcolrs(scan, len, adjust) /* normalize a scanline of colrs */
193 register COLR *scan;
194 int len;
195 int adjust;
196 {
197 register int c;
198 register int shift;
199
200 while (len-- > 0) {
201 shift = scan[0][EXP] + adjust - COLXS;
202 if (shift > 0) {
203 if (shift > 8) {
204 scan[0][RED] =
205 scan[0][GRN] =
206 scan[0][BLU] = 255;
207 } else {
208 shift--;
209 c = (scan[0][RED]<<1 | 1) << shift;
210 scan[0][RED] = c > 255 ? 255 : c;
211 c = (scan[0][GRN]<<1 | 1) << shift;
212 scan[0][GRN] = c > 255 ? 255 : c;
213 c = (scan[0][BLU]<<1 | 1) << shift;
214 scan[0][BLU] = c > 255 ? 255 : c;
215 }
216 } else if (shift < 0) {
217 if (shift < -8) {
218 scan[0][RED] =
219 scan[0][GRN] =
220 scan[0][BLU] = 0;
221 } else {
222 shift = -1-shift;
223 scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1;
224 scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1;
225 scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1;
226 }
227 }
228 scan[0][EXP] = COLXS - adjust;
229 scan++;
230 }
231 }