1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: falsecolor.c,v 3.1 2005/11/14 22:18:18 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] = (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->mbrmin; i <= fcs->mbrmax; i++) |
72 |
fcs->lumap[i] = 256*(i - fcs->mbrmin) / |
73 |
(fcs->mbrmax - fcs->mbrmin + 1); |
74 |
returnOK; |
75 |
} |
76 |
|
77 |
/* Compute linear false color map */ |
78 |
int |
79 |
fcLinearMapping(FCstruct *fcs, TMstruct *tms, int pctile) |
80 |
{ |
81 |
int i; |
82 |
int32 histot, cnt; |
83 |
TMbright wbrmin, wbrmax; |
84 |
|
85 |
if ((fcs == NULL) | (tms == NULL) || (tms->histo == NULL) | |
86 |
(0 > pctile) | (pctile >= 100)) |
87 |
return(TM_E_ILLEGAL); |
88 |
histot = 0; |
89 |
for (i = tms->hbrmax - tms->hbrmin; i >= 0; ) |
90 |
histot += tms->histo[i--]; |
91 |
cnt = histot * pctile / 100; |
92 |
for (i = tms->hbrmax - tms->hbrmin; i >= 0; i--) |
93 |
if ((cnt -= tms->histo[i]) < 0) |
94 |
break; |
95 |
if (i < 0) |
96 |
return(TM_E_TMFAIL); |
97 |
return(fcFixedLinear(fcs, tmLuminance(tms->hbrmin + i))); |
98 |
} |
99 |
|
100 |
/* Compute logarithmic false color map */ |
101 |
int |
102 |
fcLogMapping(FCstruct *fcs, TMstruct *tms, int pctile) |
103 |
{ |
104 |
int i; |
105 |
int32 histot, cnt; |
106 |
TMbright wbrmin, wbrmax; |
107 |
|
108 |
if ((fcs == NULL) | (tms == NULL) || (tms->histo == NULL) | |
109 |
(0 > pctile) | (pctile >= 100)) |
110 |
return(TM_E_ILLEGAL); |
111 |
histot = 0; |
112 |
for (i = tms->hbrmax - tms->hbrmin; i >= 0; ) |
113 |
histot += tms->histo[i--]; |
114 |
cnt = histot * pctile / 100; |
115 |
for (i = 0; i <= tms->hbrmax - tms->hbrmin; i++) |
116 |
if ((cnt -= tms->histo[i]) < 0) |
117 |
break; |
118 |
if (i >= tms->hbrmax - tms->hbrmin) |
119 |
return(TM_E_TMFAIL); |
120 |
wbrmin = tms->hbrmin + i; |
121 |
cnt = histot * pctile / 100; |
122 |
for (i = tms->hbrmax - tms->hbrmin; i >= 0; i--) |
123 |
if ((cnt -= tms->histo[i]) < 0) |
124 |
break; |
125 |
if (i < 0) |
126 |
return(TM_E_TMFAIL); |
127 |
wbrmax = tms->hbrmin + i; |
128 |
return(fcFixedLog(fcs, tmLuminance(wbrmin), tmLuminance(wbrmax))); |
129 |
} |
130 |
|
131 |
/* Apply false color mapping to pixel values */ |
132 |
int |
133 |
fcMapPixels(FCstruct *fcs, BYTE *ps, TMbright *ls, int len) |
134 |
{ |
135 |
int li; |
136 |
|
137 |
if (fcs == NULL || (fcs->lumap == NULL) | (fcs->scale == NULL)) |
138 |
return(TM_E_ILLEGAL); |
139 |
if ((ps == NULL) | (ls == NULL) | (len < 0)) |
140 |
return(TM_E_ILLEGAL); |
141 |
while (len--) { |
142 |
if ((li = *ls++) < fcs->mbrmin) |
143 |
li = 0; |
144 |
else if (li > fcs->mbrmax) |
145 |
li = 255; |
146 |
else |
147 |
li = fcs->lumap[li - fcs->mbrmin]; |
148 |
*ps++ = fcs->scale[li][RED]; |
149 |
*ps++ = fcs->scale[li][GRN]; |
150 |
*ps++ = fcs->scale[li][BLU]; |
151 |
} |
152 |
returnOK; |
153 |
} |
154 |
|
155 |
/* Determine if false color mapping is logarithmic */ |
156 |
int |
157 |
fcIsLogMap(FCstruct *fcs) |
158 |
{ |
159 |
int midval; |
160 |
|
161 |
if (fcs == NULL || fcs->lumap == NULL) |
162 |
return(-1); |
163 |
midval = fcs->lumap[(fcs->mbrmax - fcs->mbrmin)/2]; |
164 |
return((127 <= midval) & (midval <= 129)); |
165 |
} |
166 |
|
167 |
/* Duplicate a false color structure */ |
168 |
FCstruct * |
169 |
fcDup(FCstruct *fcs) |
170 |
{ |
171 |
FCstruct *fcnew; |
172 |
|
173 |
if (fcs == NULL) |
174 |
return(NULL); |
175 |
fcnew = fcInit(fcs->scale); |
176 |
if (fcnew == NULL) |
177 |
return(NULL); |
178 |
if (fcs->lumap != NULL) { |
179 |
fcnew->lumap = (BYTE *)malloc(sizeof(BYTE)*(fcs->mbrmax - |
180 |
fcs->mbrmin + 1)); |
181 |
if (fcnew->lumap == NULL) |
182 |
return(fcnew); |
183 |
fcnew->mbrmin = fcs->mbrmin; fcnew->mbrmax = fcs->mbrmax; |
184 |
memcpy((void *)fcnew->lumap, (void *)fcs->lumap, |
185 |
sizeof(BYTE)*(fcs->mbrmax - fcs->mbrmin + 1)); |
186 |
} |
187 |
return(fcnew); |
188 |
} |
189 |
|
190 |
/* Free data associated with the given false color mapping structure */ |
191 |
void |
192 |
fcDone(FCstruct *fcs) |
193 |
{ |
194 |
if (fcs == NULL) |
195 |
return; |
196 |
if (fcs->lumap != NULL) |
197 |
free((void *)fcs->lumap); |
198 |
free((void *)fcs); |
199 |
} |
200 |
|
201 |
BYTE fcDefaultScale[256][3] = { /* default false color scale */ |
202 |
48,0,68, 45,0,70, 42,0,72, 39,0,74, 36,0,76, 33,0,78, 30,0,81, |
203 |
27,0,83, 24,0,85, 21,0,87, 18,0,89, 15,0,91, 13,0,94, 12,0,96, |
204 |
11,0,99, 9,0,101, 8,0,104, 7,0,106, 6,0,109, 5,0,111, 4,0,114, |
205 |
2,0,116, 1,0,119, 0,0,121, 0,0,122, 0,1,123, 0,1,124, 0,2,125, |
206 |
0,2,125, 0,3,126, 0,4,127, 0,4,128, 0,5,129, 0,5,129, 0,6,130, |
207 |
0,7,131, 0,9,131, 0,11,132, 0,13,133, 0,16,133, 0,18,134, |
208 |
0,20,134, 0,22,135, 0,24,135, 0,26,136, 0,29,136, 0,31,137, |
209 |
0,34,137, 0,37,136, 0,41,136, 0,45,135, 0,48,135, 0,52,135, |
210 |
0,55,134, 0,59,134, 0,62,134, 0,66,133, 0,69,133, 0,73,132, |
211 |
0,76,130, 0,79,127, 0,82,125, 0,85,123, 0,88,120, 0,91,118, |
212 |
0,94,115, 0,97,113, 0,101,110, 0,104,108, 0,107,106, 0,109,102, |
213 |
0,110,97, 0,111,91, 0,112,86, 0,113,81, 0,115,76, 0,116,71, |
214 |
0,117,65, 0,118,60, 0,119,55, 0,120,50, 0,122,45, 0,121,42, |
215 |
1,120,39, 1,119,36, 1,119,34, 1,118,31, 2,117,28, 2,116,26, |
216 |
2,115,23, 2,115,20, 3,114,18, 3,113,15, 3,112,13, 4,110,13, |
217 |
5,108,13, 6,106,13, 7,104,12, 9,102,12, 10,100,12, 11,98,12, |
218 |
12,97,12, 13,95,12, 14,93,11, 15,91,11, 17,89,12, 19,86,12, |
219 |
22,83,12, 24,81,13, 26,78,13, 29,76,14, 31,73,14, 34,70,15, |
220 |
36,68,15, 39,65,16, 41,63,16, 44,60,17, 46,58,17, 49,56,17, |
221 |
51,54,17, 54,52,17, 57,50,17, 59,48,17, 62,45,17, 64,43,17, |
222 |
67,41,17, 70,39,17, 72,37,17, 74,35,17, 75,34,16, 76,33,16, |
223 |
77,32,16, 79,31,15, 80,30,15, 81,29,14, 82,28,14, 83,26,13, |
224 |
84,25,13, 85,24,13, 87,23,12, 87,22,12, 88,21,11, 89,20,11, |
225 |
90,19,10, 91,18,10, 92,17,9, 93,16,9, 94,15,8, 95,14,8, |
226 |
95,13,7, 96,12,7, 97,11,7, 98,11,6, 99,10,6, 99,9,5, 100,9,5, |
227 |
101,8,5, 102,8,4, 102,7,4, 103,6,4, 104,6,3, 104,5,3, 105,4,2, |
228 |
106,4,2, 107,4,2, 107,3,2, 108,3,2, 109,3,2, 109,2,1, 110,2,1, |
229 |
111,2,1, 112,1,1, 112,1,1, 113,1,0, 114,1,0, 115,0,0, 116,0,0, |
230 |
117,0,0, 118,0,0, 119,0,0, 121,0,0, 122,0,0, 123,0,0, 124,0,0, |
231 |
125,0,0, 126,0,0, 128,0,0, 131,0,0, 134,0,0, 137,0,0, 140,0,0, |
232 |
144,0,0, 147,0,0, 150,0,0, 153,0,0, 156,0,0, 159,0,0, 162,0,0, |
233 |
165,0,0, 168,0,0, 171,0,0, 174,0,0, 177,0,0, 180,1,0, 183,1,0, |
234 |
186,1,0, 189,1,0, 192,1,0, 195,2,0, 198,2,0, 201,5,0, 204,7,0, |
235 |
207,9,0, 210,11,0, 213,13,0, 216,15,0, 219,17,0, 222,19,0, |
236 |
225,21,0, 228,23,0, 230,25,0, 233,29,0, 235,34,0, 237,39,0, |
237 |
239,43,0, 241,48,0, 243,52,0, 245,57,0, 247,61,0, 250,66,0, |
238 |
252,71,0, 254,75,0, 255,80,0, 255,88,0, 255,95,1, 255,103,1, |
239 |
255,110,1, 255,117,1, 255,125,1, 255,132,2, 255,139,2, 255,147,2, |
240 |
255,154,2, 255,162,2, 255,169,3, 255,176,3, 255,183,3, 254,190,4, |
241 |
254,198,4, 254,205,4, 254,212,4, 253,219,5, 253,226,5, 253,234,5, |
242 |
252,241,6, 252,248,6 |
243 |
}; |