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