ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/colrops.c
Revision: 2.13
Committed: Mon Dec 18 18:54:38 2023 UTC (4 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.12: +36 -29 lines
Log Message:
refactor: ANSIfication

File Contents

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