ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/falsecolor.c
Revision: 3.4
Committed: Wed Nov 16 08:30:15 2005 UTC (18 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.3: +28 -18 lines
Log Message:
Fixed bug in TMstruct-based false color mappings

File Contents

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