Back to index

radiance  4R0+20100331
falsecolor.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: falsecolor.c,v 3.9 2008/07/03 03:35:10 greg Exp $";
00003 #endif
00004 /*
00005  * False color mapping functions.
00006  * See falsecolor.h for detailed function descriptions.
00007  *
00008  * Externals declared in falsecolor.h
00009  */
00010 
00011 #include "copyright.h"
00012 
00013 #include      <stdio.h>
00014 #include      <math.h>
00015 #include      <string.h>
00016 #include      "tmprivat.h"
00017 #include      "falsecolor.h"
00018 
00019 /* Initialize new false color mapping */
00020 FCstruct *
00021 fcInit(BYTE fcscale[256][3])
00022 {
00023        FCstruct      *fcs = (FCstruct *)malloc(sizeof(FCstruct));
00024        
00025        if (fcs == NULL)
00026               return(NULL);
00027        fcs->mbrmin = 10; fcs->mbrmax = -10;
00028        fcs->lumap = NULL;
00029        if ((fcs->scale = fcscale) == NULL)
00030               fcs->scale = fcDefaultScale;
00031        return(fcs);
00032 }
00033 
00034 /* Assign fixed linear false color map */
00035 int
00036 fcFixedLinear(FCstruct *fcs, double Lwmax)
00037 {
00038        double mult;
00039        int    i;
00040 
00041        if ((fcs == NULL) | (Lwmax <= MINLUM))
00042               return(TM_E_ILLEGAL);
00043        if (fcs->lumap != NULL)
00044               free((void *)fcs->lumap);
00045        fcs->mbrmin = tmCvLuminance(Lwmax/256.);
00046        fcs->mbrmax = tmCvLuminance(Lwmax);
00047        fcs->lumap = (BYTE *)malloc(sizeof(BYTE)*(fcs->mbrmax - fcs->mbrmin + 1));
00048        if (fcs->lumap == NULL)
00049               return(TM_E_NOMEM);
00050        mult = 255.999/tmLuminance(fcs->mbrmax);
00051        for (i = fcs->mbrmin; i <= fcs->mbrmax; i++)
00052               fcs->lumap[i - fcs->mbrmin] = (int)(mult * tmLuminance(i));
00053        returnOK;
00054 }
00055 
00056 /* Assign fixed logarithmic false color map */
00057 int
00058 fcFixedLog(FCstruct *fcs, double Lwmin, double Lwmax)
00059 {
00060        int    i;
00061 
00062        if ((fcs == NULL) | (Lwmin <= MINLUM) | (Lwmax <= Lwmin))
00063               return(TM_E_ILLEGAL);
00064        if (fcs->lumap != NULL)
00065               free((void *)fcs->lumap);
00066        fcs->mbrmin = tmCvLuminance(Lwmin);
00067        fcs->mbrmax = tmCvLuminance(Lwmax);
00068        if (fcs->mbrmin >= fcs->mbrmax) {
00069               fcs->lumap = NULL;
00070               return(TM_E_ILLEGAL);
00071        }
00072        fcs->lumap = (BYTE *)malloc(sizeof(BYTE)*(fcs->mbrmax - fcs->mbrmin + 1));
00073        if (fcs->lumap == NULL)
00074               return(TM_E_NOMEM);
00075        for (i = fcs->mbrmax - fcs->mbrmin; i >= 0; i--)
00076               fcs->lumap[i] = 256L * i / (fcs->mbrmax - fcs->mbrmin + 1);
00077        returnOK;
00078 }
00079 
00080 /* Compute linear false color map */
00081 int
00082 fcLinearMapping(FCstruct *fcs, TMstruct *tms, double pctile)
00083 {
00084        int           i, histlen;
00085        int32         histot, cnt;
00086        int           brt0;
00087 
00088        if ((fcs == NULL) | (tms == NULL) || (tms->histo == NULL) |
00089                      (0 > pctile) | (pctile >= 50))
00090               return(TM_E_ILLEGAL);
00091        i = HISTI(tms->hbrmin);
00092        brt0 = HISTV(i);
00093        histlen = HISTI(tms->hbrmax) + 1 - i;
00094        histot = 0;
00095        for (i = histlen; i--; )
00096               histot += tms->histo[i];
00097        cnt = histot * pctile / 100;
00098        for (i = histlen; i--; )
00099               if ((cnt -= tms->histo[i]) < 0)
00100                      break;
00101        if (i <= 0)
00102               return(TM_E_TMFAIL);
00103        return(fcFixedLinear(fcs, tmLuminance(brt0 + i*HISTEP)));
00104 }
00105 
00106 /* Compute logarithmic false color map */
00107 int
00108 fcLogMapping(FCstruct *fcs, TMstruct *tms, double pctile)
00109 {
00110        int           i, histlen;
00111        int32         histot, cnt;
00112        int           brt0, wbrmin, wbrmax;
00113 
00114        if ((fcs == NULL) | (tms == NULL) || (tms->histo == NULL) |
00115                      (.0 > pctile) | (pctile >= 50.))
00116               return(TM_E_ILLEGAL);
00117        i = HISTI(tms->hbrmin);
00118        brt0 = HISTV(i);
00119        histlen = HISTI(tms->hbrmax) + 1 - i;
00120        histot = 0;
00121        for (i = histlen; i--; )
00122               histot += tms->histo[i];
00123        cnt = histot * pctile * .01;
00124        for (i = 0; i < histlen; i++)
00125               if ((cnt -= tms->histo[i]) < 0)
00126                      break;
00127        if (i >= histlen)
00128               return(TM_E_TMFAIL);
00129        wbrmin = brt0 + i*HISTEP;
00130        cnt = histot * pctile * .01;
00131        for (i = histlen; i--; )
00132               if ((cnt -= tms->histo[i]) < 0)
00133                      break;
00134        wbrmax = brt0 + i*HISTEP;
00135        if (wbrmax <= wbrmin)
00136               return(TM_E_TMFAIL);
00137        return(fcFixedLog(fcs, tmLuminance(wbrmin), tmLuminance(wbrmax)));
00138 }
00139  
00140 /* Apply false color mapping to pixel values */
00141 int
00142 fcMapPixels(FCstruct *fcs, BYTE *ps, TMbright *ls, int len)
00143 {
00144        int    li;
00145 
00146        if (fcs == NULL || (fcs->lumap == NULL) | (fcs->scale == NULL))
00147               return(TM_E_ILLEGAL);
00148        if ((ps == NULL) | (ls == NULL) | (len < 0))
00149               return(TM_E_ILLEGAL);
00150        while (len--) {
00151               if ((li = *ls++) < fcs->mbrmin)
00152                      li = 0;
00153               else if (li > fcs->mbrmax)
00154                      li = 255;
00155               else
00156                      li = fcs->lumap[li - fcs->mbrmin];
00157               *ps++ = fcs->scale[li][RED];
00158               *ps++ = fcs->scale[li][GRN];
00159               *ps++ = fcs->scale[li][BLU];
00160        }
00161        returnOK;
00162 }
00163 
00164 /* Determine if false color mapping is logarithmic */
00165 int
00166 fcIsLogMap(FCstruct *fcs)
00167 {
00168        int    miderr;
00169 
00170        if (fcs == NULL || fcs->lumap == NULL)
00171               return(-1);
00172        
00173        miderr = (fcs->mbrmax - fcs->mbrmin)/2;
00174        miderr = fcs->lumap[miderr] -
00175                      256L * miderr / (fcs->mbrmax - fcs->mbrmin + 1);
00176 
00177        return((-1 <= miderr) & (miderr <= 1));
00178 }
00179 
00180 /* Duplicate a false color structure */
00181 FCstruct *
00182 fcDup(FCstruct *fcs)
00183 {
00184        FCstruct      *fcnew;
00185 
00186        if (fcs == NULL)
00187               return(NULL);
00188        fcnew = fcInit(fcs->scale);
00189        if (fcnew == NULL)
00190               return(NULL);
00191        if (fcs->lumap != NULL) {
00192               fcnew->lumap = (BYTE *)malloc(sizeof(BYTE)*(fcs->mbrmax -
00193                                                  fcs->mbrmin + 1));
00194               if (fcnew->lumap == NULL)
00195                      return(fcnew);
00196               fcnew->mbrmin = fcs->mbrmin; fcnew->mbrmax = fcs->mbrmax;
00197               memcpy((void *)fcnew->lumap, (void *)fcs->lumap,
00198                             sizeof(BYTE)*(fcs->mbrmax - fcs->mbrmin + 1));
00199        }
00200        return(fcnew);
00201 }
00202 
00203 /* Free data associated with the given false color mapping structure */
00204 void
00205 fcDone(FCstruct *fcs)
00206 {
00207        if (fcs == NULL)
00208               return;
00209        if (fcs->lumap != NULL)
00210               free((void *)fcs->lumap);
00211        free((void *)fcs);
00212 }
00213 
00214 BYTE   fcDefaultScale[256][3] = {  /* default false color scale */
00215        111,8,132,
00216        108,7,133,
00217        105,7,134,
00218        102,6,136,
00219        98,6,137,
00220        93,5,139,
00221        89,4,141,
00222        84,3,143,
00223        79,2,145,
00224        74,1,148,
00225        68,0,150,
00226        63,0,153,
00227        57,0,155,
00228        52,0,157,
00229        46,0,160,
00230        41,0,162,
00231        36,0,164,
00232        31,0,166,
00233        26,0,168,
00234        22,0,170,
00235        18,0,172,
00236        14,2,174,
00237        11,4,175,
00238         8,7,176,
00239         7,9,177,
00240         6,11,177,
00241         5,13,178,
00242         4,16,178,
00243         3,18,179,
00244         2,21,180,
00245         1,24,180,
00246         1,28,181,
00247         0,31,181,
00248         0,35,182,
00249         0,38,182,
00250         0,42,183,
00251         0,46,184,
00252         0,50,184,
00253         0,54,184,
00254         0,58,185,
00255         0,63,185,
00256         0,67,186,
00257         0,71,186,
00258         0,76,186,
00259         0,80,187,
00260         0,84,187,
00261         0,89,187,
00262         0,93,187,
00263         1,97,187,
00264         1,102,187,
00265         1,106,187,
00266         2,110,187,
00267         2,114,187,
00268         3,118,186,
00269         3,122,186,
00270         4,126,186,
00271         4,130,185,
00272         4,133,185,
00273         5,137,184,
00274         5,140,183,
00275         6,143,182,
00276         6,146,181,
00277         6,149,180,
00278         7,151,179,
00279         7,154,178,
00280         7,156,177,
00281         8,158,175,
00282         8,161,172,
00283         9,163,169,
00284         9,165,165,
00285         9,167,161,
00286         9,169,157,
00287        10,170,153,
00288        10,172,148,
00289        10,173,143,
00290        11,174,138,
00291        11,174,133,
00292        11,175,127,
00293        12,175,122,
00294        12,176,117,
00295        13,176,111,
00296        14,176,106,
00297        14,176,101,
00298        15,175,95,
00299        16,175,90,
00300        17,175,86,
00301        18,174,81,
00302        20,174,77,
00303        21,173,73,
00304        22,172,69,
00305        24,172,66,
00306        26,171,63,
00307        28,170,60,
00308        30,169,58,
00309        32,168,57,
00310        34,167,56,
00311        37,166,55,
00312        40,165,54,
00313        42,164,54,
00314        45,163,54,
00315        48,162,55,
00316        52,160,55,
00317        55,158,56,
00318        58,157,57,
00319        62,155,57,
00320        66,153,59,
00321        69,152,60,
00322        73,150,61,
00323        77,148,63,
00324        81,146,64,
00325        84,144,66,
00326        88,142,67,
00327        92,139,69,
00328        96,137,70,
00329        99,135,72,
00330        103,133,73,
00331        107,131,75,
00332        110,128,76,
00333        113,126,77,
00334        117,124,78,
00335        120,121,79,
00336        123,119,80,
00337        126,117,80,
00338        128,114,81,
00339        131,112,81,
00340        133,110,81,
00341        135,108,80,
00342        136,106,80,
00343        137,105,80,
00344        138,104,79,
00345        139,102,79,
00346        140,101,79,
00347        141,100,78,
00348        142,98,78,
00349        143,96,77,
00350        144,95,76,
00351        144,93,76,
00352        145,92,75,
00353        146,90,74,
00354        146,89,73,
00355        147,87,73,
00356        148,85,72,
00357        148,84,71,
00358        149,82,70,
00359        149,80,69,
00360        150,79,68,
00361        150,77,67,
00362        151,75,66,
00363        151,73,65,
00364        151,72,64,
00365        152,70,63,
00366        152,68,62,
00367        153,66,61,
00368        153,65,60,
00369        153,63,59,
00370        154,61,58,
00371        154,60,57,
00372        154,58,56,
00373        154,56,55,
00374        155,55,54,
00375        155,53,53,
00376        155,51,51,
00377        156,50,50,
00378        156,48,49,
00379        156,46,48,
00380        157,45,47,
00381        157,43,46,
00382        157,42,45,
00383        158,40,44,
00384        158,39,43,
00385        158,37,42,
00386        159,36,41,
00387        159,34,40,
00388        159,33,39,
00389        160,32,38,
00390        160,31,37,
00391        161,29,37,
00392        161,28,36,
00393        162,27,35,
00394        162,26,34,
00395        163,25,33,
00396        163,24,33,
00397        164,23,32,
00398        165,22,31,
00399        165,21,31,
00400        168,18,29,
00401        170,16,28,
00402        172,13,26,
00403        175,11,25,
00404        177,9,24,
00405        180,7,23,
00406        183,5,22,
00407        185,3,21,
00408        188,2,21,
00409        191,1,20,
00410        194,0,19,
00411        197,0,19,
00412        199,0,18,
00413        202,0,17,
00414        205,0,17,
00415        207,0,16,
00416        210,2,16,
00417        213,3,15,
00418        215,6,14,
00419        217,8,13,
00420        219,11,13,
00421        220,13,12,
00422        222,17,11,
00423        224,20,11,
00424        226,24,10,
00425        227,28,9,
00426        229,32,8,
00427        231,37,7,
00428        232,42,6,
00429        234,47,5,
00430        236,52,5,
00431        237,57,4,
00432        239,63,3,
00433        240,68,2,
00434        242,74,2,
00435        243,79,1,
00436        245,85,0,
00437        246,91,0,
00438        247,96,0,
00439        248,102,0,
00440        250,108,0,
00441        251,113,0,
00442        252,118,0,
00443        253,123,0,
00444        254,128,0,
00445        254,133,0,
00446        255,138,0,
00447        255,143,1,
00448        255,148,2,
00449        255,154,3,
00450        255,159,4,
00451        255,165,6,
00452        255,170,7,
00453        255,176,9,
00454        255,181,11,
00455        255,187,13,
00456        255,192,15,
00457        255,198,17,
00458        255,203,20,
00459        255,208,22,
00460        255,213,24,
00461        255,218,26,
00462        255,223,28,
00463        255,227,30,
00464        255,232,32,
00465        255,236,34,
00466        254,240,35,
00467        254,243,37,
00468        254,246,38,
00469        254,249,39,
00470        254,252,40,
00471 };