ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/falsecolor.c
Revision: 3.10
Committed: Fri May 20 02:06:38 2011 UTC (12 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R2P1, rad5R3
Changes since 3.9: +8 -8 lines
Log Message:
Changed every instance of BYTE to uby8 to avoid conflicts

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: falsecolor.c,v 3.9 2008/07/03 03:35:10 greg Exp $";
3 #endif
4 /*
5 * False color mapping functions.
6 * See falsecolor.h for detailed function descriptions.
7 *
8 * Externals declared in falsecolor.h
9 */
10
11 #include "copyright.h"
12
13 #include <stdio.h>
14 #include <math.h>
15 #include <string.h>
16 #include "tmprivat.h"
17 #include "falsecolor.h"
18
19 /* Initialize new false color mapping */
20 FCstruct *
21 fcInit(uby8 fcscale[256][3])
22 {
23 FCstruct *fcs = (FCstruct *)malloc(sizeof(FCstruct));
24
25 if (fcs == NULL)
26 return(NULL);
27 fcs->mbrmin = 10; fcs->mbrmax = -10;
28 fcs->lumap = NULL;
29 if ((fcs->scale = fcscale) == NULL)
30 fcs->scale = fcDefaultScale;
31 return(fcs);
32 }
33
34 /* Assign fixed linear false color map */
35 int
36 fcFixedLinear(FCstruct *fcs, double Lwmax)
37 {
38 double mult;
39 int i;
40
41 if ((fcs == NULL) | (Lwmax <= MINLUM))
42 return(TM_E_ILLEGAL);
43 if (fcs->lumap != NULL)
44 free((void *)fcs->lumap);
45 fcs->mbrmin = tmCvLuminance(Lwmax/256.);
46 fcs->mbrmax = tmCvLuminance(Lwmax);
47 fcs->lumap = (uby8 *)malloc(sizeof(uby8)*(fcs->mbrmax - fcs->mbrmin + 1));
48 if (fcs->lumap == NULL)
49 return(TM_E_NOMEM);
50 mult = 255.999/tmLuminance(fcs->mbrmax);
51 for (i = fcs->mbrmin; i <= fcs->mbrmax; i++)
52 fcs->lumap[i - fcs->mbrmin] = (int)(mult * tmLuminance(i));
53 returnOK;
54 }
55
56 /* Assign fixed logarithmic false color map */
57 int
58 fcFixedLog(FCstruct *fcs, double Lwmin, double Lwmax)
59 {
60 int i;
61
62 if ((fcs == NULL) | (Lwmin <= MINLUM) | (Lwmax <= Lwmin))
63 return(TM_E_ILLEGAL);
64 if (fcs->lumap != NULL)
65 free((void *)fcs->lumap);
66 fcs->mbrmin = tmCvLuminance(Lwmin);
67 fcs->mbrmax = tmCvLuminance(Lwmax);
68 if (fcs->mbrmin >= fcs->mbrmax) {
69 fcs->lumap = NULL;
70 return(TM_E_ILLEGAL);
71 }
72 fcs->lumap = (uby8 *)malloc(sizeof(uby8)*(fcs->mbrmax - fcs->mbrmin + 1));
73 if (fcs->lumap == NULL)
74 return(TM_E_NOMEM);
75 for (i = fcs->mbrmax - fcs->mbrmin; i >= 0; i--)
76 fcs->lumap[i] = 256L * i / (fcs->mbrmax - fcs->mbrmin + 1);
77 returnOK;
78 }
79
80 /* Compute linear false color map */
81 int
82 fcLinearMapping(FCstruct *fcs, TMstruct *tms, double pctile)
83 {
84 int i, histlen;
85 int32 histot, cnt;
86 int brt0;
87
88 if ((fcs == NULL) | (tms == NULL) || (tms->histo == NULL) |
89 (0 > pctile) | (pctile >= 50))
90 return(TM_E_ILLEGAL);
91 i = HISTI(tms->hbrmin);
92 brt0 = HISTV(i);
93 histlen = HISTI(tms->hbrmax) + 1 - i;
94 histot = 0;
95 for (i = histlen; i--; )
96 histot += tms->histo[i];
97 cnt = histot * pctile / 100;
98 for (i = histlen; i--; )
99 if ((cnt -= tms->histo[i]) < 0)
100 break;
101 if (i <= 0)
102 return(TM_E_TMFAIL);
103 return(fcFixedLinear(fcs, tmLuminance(brt0 + i*HISTEP)));
104 }
105
106 /* Compute logarithmic false color map */
107 int
108 fcLogMapping(FCstruct *fcs, TMstruct *tms, double pctile)
109 {
110 int i, histlen;
111 int32 histot, cnt;
112 int brt0, wbrmin, wbrmax;
113
114 if ((fcs == NULL) | (tms == NULL) || (tms->histo == NULL) |
115 (.0 > pctile) | (pctile >= 50.))
116 return(TM_E_ILLEGAL);
117 i = HISTI(tms->hbrmin);
118 brt0 = HISTV(i);
119 histlen = HISTI(tms->hbrmax) + 1 - i;
120 histot = 0;
121 for (i = histlen; i--; )
122 histot += tms->histo[i];
123 cnt = histot * pctile * .01;
124 for (i = 0; i < histlen; i++)
125 if ((cnt -= tms->histo[i]) < 0)
126 break;
127 if (i >= histlen)
128 return(TM_E_TMFAIL);
129 wbrmin = brt0 + i*HISTEP;
130 cnt = histot * pctile * .01;
131 for (i = histlen; i--; )
132 if ((cnt -= tms->histo[i]) < 0)
133 break;
134 wbrmax = brt0 + i*HISTEP;
135 if (wbrmax <= wbrmin)
136 return(TM_E_TMFAIL);
137 return(fcFixedLog(fcs, tmLuminance(wbrmin), tmLuminance(wbrmax)));
138 }
139
140 /* Apply false color mapping to pixel values */
141 int
142 fcMapPixels(FCstruct *fcs, uby8 *ps, TMbright *ls, int len)
143 {
144 int li;
145
146 if (fcs == NULL || (fcs->lumap == NULL) | (fcs->scale == NULL))
147 return(TM_E_ILLEGAL);
148 if ((ps == NULL) | (ls == NULL) | (len < 0))
149 return(TM_E_ILLEGAL);
150 while (len--) {
151 if ((li = *ls++) < fcs->mbrmin)
152 li = 0;
153 else if (li > fcs->mbrmax)
154 li = 255;
155 else
156 li = fcs->lumap[li - fcs->mbrmin];
157 *ps++ = fcs->scale[li][RED];
158 *ps++ = fcs->scale[li][GRN];
159 *ps++ = fcs->scale[li][BLU];
160 }
161 returnOK;
162 }
163
164 /* Determine if false color mapping is logarithmic */
165 int
166 fcIsLogMap(FCstruct *fcs)
167 {
168 int miderr;
169
170 if (fcs == NULL || fcs->lumap == NULL)
171 return(-1);
172
173 miderr = (fcs->mbrmax - fcs->mbrmin)/2;
174 miderr = fcs->lumap[miderr] -
175 256L * miderr / (fcs->mbrmax - fcs->mbrmin + 1);
176
177 return((-1 <= miderr) & (miderr <= 1));
178 }
179
180 /* Duplicate a false color structure */
181 FCstruct *
182 fcDup(FCstruct *fcs)
183 {
184 FCstruct *fcnew;
185
186 if (fcs == NULL)
187 return(NULL);
188 fcnew = fcInit(fcs->scale);
189 if (fcnew == NULL)
190 return(NULL);
191 if (fcs->lumap != NULL) {
192 fcnew->lumap = (uby8 *)malloc(sizeof(uby8)*(fcs->mbrmax -
193 fcs->mbrmin + 1));
194 if (fcnew->lumap == NULL)
195 return(fcnew);
196 fcnew->mbrmin = fcs->mbrmin; fcnew->mbrmax = fcs->mbrmax;
197 memcpy((void *)fcnew->lumap, (void *)fcs->lumap,
198 sizeof(uby8)*(fcs->mbrmax - fcs->mbrmin + 1));
199 }
200 return(fcnew);
201 }
202
203 /* Free data associated with the given false color mapping structure */
204 void
205 fcDone(FCstruct *fcs)
206 {
207 if (fcs == NULL)
208 return;
209 if (fcs->lumap != NULL)
210 free((void *)fcs->lumap);
211 free((void *)fcs);
212 }
213
214 uby8 fcDefaultScale[256][3] = { /* default false color scale */
215 111,8,132,
216 108,7,133,
217 105,7,134,
218 102,6,136,
219 98,6,137,
220 93,5,139,
221 89,4,141,
222 84,3,143,
223 79,2,145,
224 74,1,148,
225 68,0,150,
226 63,0,153,
227 57,0,155,
228 52,0,157,
229 46,0,160,
230 41,0,162,
231 36,0,164,
232 31,0,166,
233 26,0,168,
234 22,0,170,
235 18,0,172,
236 14,2,174,
237 11,4,175,
238 8,7,176,
239 7,9,177,
240 6,11,177,
241 5,13,178,
242 4,16,178,
243 3,18,179,
244 2,21,180,
245 1,24,180,
246 1,28,181,
247 0,31,181,
248 0,35,182,
249 0,38,182,
250 0,42,183,
251 0,46,184,
252 0,50,184,
253 0,54,184,
254 0,58,185,
255 0,63,185,
256 0,67,186,
257 0,71,186,
258 0,76,186,
259 0,80,187,
260 0,84,187,
261 0,89,187,
262 0,93,187,
263 1,97,187,
264 1,102,187,
265 1,106,187,
266 2,110,187,
267 2,114,187,
268 3,118,186,
269 3,122,186,
270 4,126,186,
271 4,130,185,
272 4,133,185,
273 5,137,184,
274 5,140,183,
275 6,143,182,
276 6,146,181,
277 6,149,180,
278 7,151,179,
279 7,154,178,
280 7,156,177,
281 8,158,175,
282 8,161,172,
283 9,163,169,
284 9,165,165,
285 9,167,161,
286 9,169,157,
287 10,170,153,
288 10,172,148,
289 10,173,143,
290 11,174,138,
291 11,174,133,
292 11,175,127,
293 12,175,122,
294 12,176,117,
295 13,176,111,
296 14,176,106,
297 14,176,101,
298 15,175,95,
299 16,175,90,
300 17,175,86,
301 18,174,81,
302 20,174,77,
303 21,173,73,
304 22,172,69,
305 24,172,66,
306 26,171,63,
307 28,170,60,
308 30,169,58,
309 32,168,57,
310 34,167,56,
311 37,166,55,
312 40,165,54,
313 42,164,54,
314 45,163,54,
315 48,162,55,
316 52,160,55,
317 55,158,56,
318 58,157,57,
319 62,155,57,
320 66,153,59,
321 69,152,60,
322 73,150,61,
323 77,148,63,
324 81,146,64,
325 84,144,66,
326 88,142,67,
327 92,139,69,
328 96,137,70,
329 99,135,72,
330 103,133,73,
331 107,131,75,
332 110,128,76,
333 113,126,77,
334 117,124,78,
335 120,121,79,
336 123,119,80,
337 126,117,80,
338 128,114,81,
339 131,112,81,
340 133,110,81,
341 135,108,80,
342 136,106,80,
343 137,105,80,
344 138,104,79,
345 139,102,79,
346 140,101,79,
347 141,100,78,
348 142,98,78,
349 143,96,77,
350 144,95,76,
351 144,93,76,
352 145,92,75,
353 146,90,74,
354 146,89,73,
355 147,87,73,
356 148,85,72,
357 148,84,71,
358 149,82,70,
359 149,80,69,
360 150,79,68,
361 150,77,67,
362 151,75,66,
363 151,73,65,
364 151,72,64,
365 152,70,63,
366 152,68,62,
367 153,66,61,
368 153,65,60,
369 153,63,59,
370 154,61,58,
371 154,60,57,
372 154,58,56,
373 154,56,55,
374 155,55,54,
375 155,53,53,
376 155,51,51,
377 156,50,50,
378 156,48,49,
379 156,46,48,
380 157,45,47,
381 157,43,46,
382 157,42,45,
383 158,40,44,
384 158,39,43,
385 158,37,42,
386 159,36,41,
387 159,34,40,
388 159,33,39,
389 160,32,38,
390 160,31,37,
391 161,29,37,
392 161,28,36,
393 162,27,35,
394 162,26,34,
395 163,25,33,
396 163,24,33,
397 164,23,32,
398 165,22,31,
399 165,21,31,
400 168,18,29,
401 170,16,28,
402 172,13,26,
403 175,11,25,
404 177,9,24,
405 180,7,23,
406 183,5,22,
407 185,3,21,
408 188,2,21,
409 191,1,20,
410 194,0,19,
411 197,0,19,
412 199,0,18,
413 202,0,17,
414 205,0,17,
415 207,0,16,
416 210,2,16,
417 213,3,15,
418 215,6,14,
419 217,8,13,
420 219,11,13,
421 220,13,12,
422 222,17,11,
423 224,20,11,
424 226,24,10,
425 227,28,9,
426 229,32,8,
427 231,37,7,
428 232,42,6,
429 234,47,5,
430 236,52,5,
431 237,57,4,
432 239,63,3,
433 240,68,2,
434 242,74,2,
435 243,79,1,
436 245,85,0,
437 246,91,0,
438 247,96,0,
439 248,102,0,
440 250,108,0,
441 251,113,0,
442 252,118,0,
443 253,123,0,
444 254,128,0,
445 254,133,0,
446 255,138,0,
447 255,143,1,
448 255,148,2,
449 255,154,3,
450 255,159,4,
451 255,165,6,
452 255,170,7,
453 255,176,9,
454 255,181,11,
455 255,187,13,
456 255,192,15,
457 255,198,17,
458 255,203,20,
459 255,208,22,
460 255,213,24,
461 255,218,26,
462 255,223,28,
463 255,227,30,
464 255,232,32,
465 255,236,34,
466 254,240,35,
467 254,243,37,
468 254,246,38,
469 254,249,39,
470 254,252,40,
471 };