ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/falsecolor.c
Revision: 3.5
Committed: Fri Nov 18 22:50:20 2005 UTC (18 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.4: +257 -257 lines
Log Message:
Made false color scale less bloody than before

File Contents

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