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