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