| 1 | 
greg | 
3.1 | 
#ifndef lint | 
| 2 | 
greg | 
3.32 | 
static const char       RCSid[] = "$Id: tonemap.c,v 3.31 2008/07/03 03:30:21 greg Exp $"; | 
| 3 | 
greg | 
3.1 | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 * Tone mapping functions. | 
| 6 | 
  | 
  | 
 * See tonemap.h for detailed function descriptions. | 
| 7 | 
greg | 
3.9 | 
 * Added von Kries white-balance calculations 10/01 (GW). | 
| 8 | 
  | 
  | 
 * | 
| 9 | 
  | 
  | 
 * Externals declared in tonemap.h | 
| 10 | 
  | 
  | 
 */ | 
| 11 | 
  | 
  | 
 | 
| 12 | 
greg | 
3.10 | 
#include "copyright.h" | 
| 13 | 
greg | 
3.1 | 
 | 
| 14 | 
  | 
  | 
#include        <stdio.h> | 
| 15 | 
  | 
  | 
#include        <math.h> | 
| 16 | 
  | 
  | 
#include        "tmprivat.h" | 
| 17 | 
  | 
  | 
#include        "tmerrmsg.h" | 
| 18 | 
  | 
  | 
 | 
| 19 | 
  | 
  | 
#define exp10(x)        exp(M_LN10*(x)) | 
| 20 | 
  | 
  | 
 | 
| 21 | 
greg | 
3.4 | 
                                        /* our list of conversion packages */ | 
| 22 | 
  | 
  | 
struct tmPackage        *tmPkg[TM_MAXPKG]; | 
| 23 | 
  | 
  | 
int     tmNumPkgs = 0;                  /* number of registered packages */ | 
| 24 | 
greg | 
3.1 | 
 | 
| 25 | 
greg | 
3.23 | 
                                        /* luminance->brightness lookup */ | 
| 26 | 
greg | 
3.24 | 
static TMbright         *tmFloat2BrtLUT = NULL; | 
| 27 | 
greg | 
3.23 | 
 | 
| 28 | 
  | 
  | 
#define tmCvLumLUfp(pf) tmFloat2BrtLUT[*(int32 *)(pf) >> 15] | 
| 29 | 
  | 
  | 
 | 
| 30 | 
gregl | 
3.5 | 
 | 
| 31 | 
greg | 
3.16 | 
TMstruct * | 
| 32 | 
  | 
  | 
tmInit(                                 /* initialize new tone mapping */ | 
| 33 | 
  | 
  | 
int     flags, | 
| 34 | 
  | 
  | 
RGBPRIMP        monpri, | 
| 35 | 
  | 
  | 
double  gamval | 
| 36 | 
  | 
  | 
) | 
| 37 | 
greg | 
3.1 | 
{ | 
| 38 | 
  | 
  | 
        COLORMAT        cmat; | 
| 39 | 
greg | 
3.16 | 
        TMstruct        *tmnew; | 
| 40 | 
  | 
  | 
        int     i; | 
| 41 | 
greg | 
3.1 | 
                                                /* allocate structure */ | 
| 42 | 
greg | 
3.16 | 
        tmnew = (TMstruct *)malloc(sizeof(TMstruct)); | 
| 43 | 
greg | 
3.1 | 
        if (tmnew == NULL) | 
| 44 | 
  | 
  | 
                return(NULL); | 
| 45 | 
  | 
  | 
 | 
| 46 | 
  | 
  | 
        tmnew->flags = flags & ~TM_F_UNIMPL; | 
| 47 | 
greg | 
3.15 | 
        if (tmnew->flags & TM_F_BW) | 
| 48 | 
  | 
  | 
                tmnew->flags &= ~TM_F_MESOPIC; | 
| 49 | 
greg | 
3.1 | 
                                                /* set monitor transform */ | 
| 50 | 
  | 
  | 
        if (monpri == NULL || monpri == stdprims || tmnew->flags & TM_F_BW) { | 
| 51 | 
  | 
  | 
                tmnew->monpri = stdprims; | 
| 52 | 
  | 
  | 
                tmnew->clf[RED] = rgb2xyzmat[1][0]; | 
| 53 | 
  | 
  | 
                tmnew->clf[GRN] = rgb2xyzmat[1][1]; | 
| 54 | 
  | 
  | 
                tmnew->clf[BLU] = rgb2xyzmat[1][2]; | 
| 55 | 
  | 
  | 
        } else { | 
| 56 | 
greg | 
3.26 | 
                comprgb2xyzmat(cmat, tmnew->monpri=monpri); | 
| 57 | 
greg | 
3.1 | 
                tmnew->clf[RED] = cmat[1][0]; | 
| 58 | 
  | 
  | 
                tmnew->clf[GRN] = cmat[1][1]; | 
| 59 | 
  | 
  | 
                tmnew->clf[BLU] = cmat[1][2]; | 
| 60 | 
  | 
  | 
        } | 
| 61 | 
  | 
  | 
                                                /* set gamma value */ | 
| 62 | 
  | 
  | 
        if (gamval < MINGAM) | 
| 63 | 
  | 
  | 
                tmnew->mongam = DEFGAM; | 
| 64 | 
  | 
  | 
        else | 
| 65 | 
  | 
  | 
                tmnew->mongam = gamval; | 
| 66 | 
greg | 
3.4 | 
                                                /* set color divisors */ | 
| 67 | 
  | 
  | 
        for (i = 0; i < 3; i++) | 
| 68 | 
  | 
  | 
                tmnew->cdiv[i] = 256.*pow(tmnew->clf[i], 1./tmnew->mongam); | 
| 69 | 
  | 
  | 
 | 
| 70 | 
greg | 
3.1 | 
                                                /* set input transform */ | 
| 71 | 
  | 
  | 
        tmnew->inppri = tmnew->monpri; | 
| 72 | 
  | 
  | 
        tmnew->cmat[0][0] = tmnew->cmat[1][1] = tmnew->cmat[2][2] = | 
| 73 | 
  | 
  | 
                        tmnew->inpsf = WHTEFFICACY; | 
| 74 | 
  | 
  | 
        tmnew->cmat[0][1] = tmnew->cmat[0][2] = tmnew->cmat[1][0] = | 
| 75 | 
  | 
  | 
        tmnew->cmat[1][2] = tmnew->cmat[2][0] = tmnew->cmat[2][1] = 0.; | 
| 76 | 
greg | 
3.18 | 
        tmnew->inpdat = NULL; | 
| 77 | 
greg | 
3.9 | 
        tmnew->hbrmin = 10; tmnew->hbrmax = -10; | 
| 78 | 
greg | 
3.1 | 
        tmnew->histo = NULL; | 
| 79 | 
greg | 
3.9 | 
        tmnew->mbrmin = 10; tmnew->mbrmax = -10; | 
| 80 | 
greg | 
3.1 | 
        tmnew->lumap = NULL; | 
| 81 | 
greg | 
3.4 | 
                                                /* zero private data */ | 
| 82 | 
  | 
  | 
        for (i = TM_MAXPKG; i--; ) | 
| 83 | 
  | 
  | 
                tmnew->pd[i] = NULL; | 
| 84 | 
greg | 
3.17 | 
        tmnew->lastError = TM_E_OK; | 
| 85 | 
  | 
  | 
        tmnew->lastFunc = "NoErr"; | 
| 86 | 
greg | 
3.16 | 
                                                /* return new TMstruct */ | 
| 87 | 
  | 
  | 
        return(tmnew); | 
| 88 | 
greg | 
3.1 | 
} | 
| 89 | 
  | 
  | 
 | 
| 90 | 
  | 
  | 
 | 
| 91 | 
  | 
  | 
int | 
| 92 | 
greg | 
3.16 | 
tmSetSpace(                     /* set input color space for conversions */ | 
| 93 | 
  | 
  | 
TMstruct        *tms, | 
| 94 | 
  | 
  | 
RGBPRIMP        pri, | 
| 95 | 
greg | 
3.18 | 
double  sf, | 
| 96 | 
  | 
  | 
MEM_PTR dat | 
| 97 | 
greg | 
3.16 | 
) | 
| 98 | 
greg | 
3.1 | 
{ | 
| 99 | 
greg | 
3.17 | 
        static const char funcName[] = "tmSetSpace"; | 
| 100 | 
greg | 
3.16 | 
        int     i, j; | 
| 101 | 
greg | 
3.1 | 
                                                /* error check */ | 
| 102 | 
greg | 
3.16 | 
        if (tms == NULL) | 
| 103 | 
greg | 
3.1 | 
                returnErr(TM_E_TMINVAL); | 
| 104 | 
  | 
  | 
        if (sf <= 1e-12) | 
| 105 | 
  | 
  | 
                returnErr(TM_E_ILLEGAL); | 
| 106 | 
  | 
  | 
                                                /* check if no change */ | 
| 107 | 
greg | 
3.18 | 
        if (pri == tms->inppri && FEQ(sf, tms->inpsf) && dat == tms->inpdat) | 
| 108 | 
greg | 
3.1 | 
                returnOK; | 
| 109 | 
greg | 
3.16 | 
        tms->inppri = pri;                      /* let's set it */ | 
| 110 | 
  | 
  | 
        tms->inpsf = sf; | 
| 111 | 
greg | 
3.18 | 
        tms->inpdat = dat; | 
| 112 | 
greg | 
3.1 | 
 | 
| 113 | 
greg | 
3.16 | 
        if (tms->flags & TM_F_BW) {             /* color doesn't matter */ | 
| 114 | 
  | 
  | 
                tms->monpri = tms->inppri;              /* eliminate xform */ | 
| 115 | 
  | 
  | 
                if (tms->inppri == TM_XYZPRIM) { | 
| 116 | 
  | 
  | 
                        tms->clf[CIEX] = tms->clf[CIEZ] = 0.; | 
| 117 | 
  | 
  | 
                        tms->clf[CIEY] = 1.; | 
| 118 | 
greg | 
3.1 | 
                } else { | 
| 119 | 
greg | 
3.26 | 
                        comprgb2xyzmat(tms->cmat, tms->monpri); | 
| 120 | 
greg | 
3.16 | 
                        tms->clf[RED] = tms->cmat[1][0]; | 
| 121 | 
  | 
  | 
                        tms->clf[GRN] = tms->cmat[1][1]; | 
| 122 | 
  | 
  | 
                        tms->clf[BLU] = tms->cmat[1][2]; | 
| 123 | 
  | 
  | 
                } | 
| 124 | 
  | 
  | 
                tms->cmat[0][0] = tms->cmat[1][1] = tms->cmat[2][2] = | 
| 125 | 
  | 
  | 
                                tms->inpsf; | 
| 126 | 
  | 
  | 
                tms->cmat[0][1] = tms->cmat[0][2] = tms->cmat[1][0] = | 
| 127 | 
  | 
  | 
                tms->cmat[1][2] = tms->cmat[2][0] = tms->cmat[2][1] = 0.; | 
| 128 | 
greg | 
3.1 | 
 | 
| 129 | 
greg | 
3.16 | 
        } else if (tms->inppri == TM_XYZPRIM)   /* input is XYZ */ | 
| 130 | 
  | 
  | 
                compxyz2rgbWBmat(tms->cmat, tms->monpri); | 
| 131 | 
greg | 
3.1 | 
 | 
| 132 | 
  | 
  | 
        else {                                  /* input is RGB */ | 
| 133 | 
greg | 
3.16 | 
                if (tms->inppri != tms->monpri && | 
| 134 | 
  | 
  | 
                                PRIMEQ(tms->inppri, tms->monpri)) | 
| 135 | 
  | 
  | 
                        tms->inppri = tms->monpri;      /* no xform */ | 
| 136 | 
  | 
  | 
                comprgb2rgbWBmat(tms->cmat, tms->inppri, tms->monpri); | 
| 137 | 
greg | 
3.1 | 
        } | 
| 138 | 
  | 
  | 
        for (i = 0; i < 3; i++) | 
| 139 | 
  | 
  | 
                for (j = 0; j < 3; j++) | 
| 140 | 
greg | 
3.16 | 
                        tms->cmat[i][j] *= tms->inpsf; | 
| 141 | 
greg | 
3.4 | 
                                                /* set color divisors */ | 
| 142 | 
  | 
  | 
        for (i = 0; i < 3; i++) | 
| 143 | 
greg | 
3.16 | 
                if (tms->clf[i] > .001) | 
| 144 | 
  | 
  | 
                        tms->cdiv[i] = | 
| 145 | 
  | 
  | 
                                256.*pow(tms->clf[i], 1./tms->mongam); | 
| 146 | 
greg | 
3.4 | 
                else | 
| 147 | 
greg | 
3.16 | 
                        tms->cdiv[i] = 1; | 
| 148 | 
greg | 
3.4 | 
                                                /* notify packages */ | 
| 149 | 
  | 
  | 
        for (i = tmNumPkgs; i--; ) | 
| 150 | 
greg | 
3.16 | 
                if (tms->pd[i] != NULL && tmPkg[i]->NewSpace != NULL) | 
| 151 | 
  | 
  | 
                        (*tmPkg[i]->NewSpace)(tms); | 
| 152 | 
greg | 
3.1 | 
        returnOK; | 
| 153 | 
  | 
  | 
} | 
| 154 | 
  | 
  | 
 | 
| 155 | 
  | 
  | 
 | 
| 156 | 
  | 
  | 
void | 
| 157 | 
greg | 
3.23 | 
tmClearHisto(                           /* clear current histogram */ | 
| 158 | 
greg | 
3.16 | 
TMstruct        *tms | 
| 159 | 
  | 
  | 
) | 
| 160 | 
greg | 
3.1 | 
{ | 
| 161 | 
greg | 
3.16 | 
        if (tms == NULL || tms->histo == NULL) | 
| 162 | 
greg | 
3.1 | 
                return; | 
| 163 | 
greg | 
3.16 | 
        free((MEM_PTR)tms->histo); | 
| 164 | 
  | 
  | 
        tms->histo = NULL; | 
| 165 | 
greg | 
3.1 | 
} | 
| 166 | 
  | 
  | 
 | 
| 167 | 
  | 
  | 
 | 
| 168 | 
greg | 
3.23 | 
TMbright | 
| 169 | 
  | 
  | 
tmCvLuminance(                          /* convert a single luminance */ | 
| 170 | 
  | 
  | 
double  lum | 
| 171 | 
  | 
  | 
) | 
| 172 | 
  | 
  | 
{ | 
| 173 | 
  | 
  | 
        double  d; | 
| 174 | 
  | 
  | 
 | 
| 175 | 
  | 
  | 
#ifdef isfinite | 
| 176 | 
  | 
  | 
        if (!isfinite(lum) || lum <= TM_NOLUM) | 
| 177 | 
  | 
  | 
#else | 
| 178 | 
  | 
  | 
        if (lum <= TM_NOLUM) | 
| 179 | 
  | 
  | 
#endif | 
| 180 | 
  | 
  | 
                return(TM_NOBRT); | 
| 181 | 
  | 
  | 
        d = TM_BRTSCALE*log(lum); | 
| 182 | 
  | 
  | 
        if (d > 0.) | 
| 183 | 
  | 
  | 
                return((TMbright)(d+.5)); | 
| 184 | 
  | 
  | 
        return((TMbright)(d-.5)); | 
| 185 | 
  | 
  | 
} | 
| 186 | 
  | 
  | 
 | 
| 187 | 
  | 
  | 
 | 
| 188 | 
  | 
  | 
int | 
| 189 | 
  | 
  | 
tmCvLums(                               /* convert luminances using lookup */ | 
| 190 | 
  | 
  | 
TMbright        *ls, | 
| 191 | 
  | 
  | 
float           *scan, | 
| 192 | 
  | 
  | 
int             len | 
| 193 | 
  | 
  | 
) | 
| 194 | 
  | 
  | 
{ | 
| 195 | 
  | 
  | 
        if (tmFloat2BrtLUT == NULL) {   /* initialize lookup table */ | 
| 196 | 
  | 
  | 
                int32   i; | 
| 197 | 
  | 
  | 
                tmFloat2BrtLUT = (TMbright *)malloc(sizeof(TMbright)*0x10000); | 
| 198 | 
  | 
  | 
                if (tmFloat2BrtLUT == NULL) | 
| 199 | 
  | 
  | 
                        return(TM_E_NOMEM); | 
| 200 | 
  | 
  | 
                for (i = 0; i < 0x10000; i++) { | 
| 201 | 
  | 
  | 
                        int32   l = (i<<1 | 1) << 14; | 
| 202 | 
  | 
  | 
#ifndef isfinite | 
| 203 | 
  | 
  | 
                        if ((l & 0x7f800000) == 0x7f800000) | 
| 204 | 
  | 
  | 
                                tmFloat2BrtLUT[i] = TM_NOBRT; | 
| 205 | 
  | 
  | 
                        else | 
| 206 | 
  | 
  | 
#endif | 
| 207 | 
  | 
  | 
                        tmFloat2BrtLUT[i] = tmCvLuminance(*(float *)&l); | 
| 208 | 
  | 
  | 
                } | 
| 209 | 
  | 
  | 
        } | 
| 210 | 
  | 
  | 
        if (len <= 0) | 
| 211 | 
  | 
  | 
                return(TM_E_OK); | 
| 212 | 
  | 
  | 
        if ((ls == NULL) | (scan == NULL)) | 
| 213 | 
  | 
  | 
                return(TM_E_ILLEGAL); | 
| 214 | 
  | 
  | 
        while (len--) { | 
| 215 | 
  | 
  | 
                if (*scan <= TM_NOLUM) { | 
| 216 | 
  | 
  | 
                        *ls++ = TM_NOBRT; | 
| 217 | 
  | 
  | 
                        ++scan; | 
| 218 | 
  | 
  | 
                        continue; | 
| 219 | 
  | 
  | 
                } | 
| 220 | 
  | 
  | 
                *ls++ = tmCvLumLUfp(scan++); | 
| 221 | 
  | 
  | 
        } | 
| 222 | 
  | 
  | 
        return(TM_E_OK); | 
| 223 | 
  | 
  | 
} | 
| 224 | 
  | 
  | 
 | 
| 225 | 
  | 
  | 
 | 
| 226 | 
  | 
  | 
int | 
| 227 | 
  | 
  | 
tmCvGrays(                              /* convert float gray values */ | 
| 228 | 
  | 
  | 
TMstruct        *tms, | 
| 229 | 
  | 
  | 
TMbright        *ls, | 
| 230 | 
  | 
  | 
float           *scan, | 
| 231 | 
  | 
  | 
int             len | 
| 232 | 
  | 
  | 
) | 
| 233 | 
  | 
  | 
{ | 
| 234 | 
  | 
  | 
        static const char funcName[] = "tmCvGrays"; | 
| 235 | 
  | 
  | 
        int     i; | 
| 236 | 
  | 
  | 
 | 
| 237 | 
  | 
  | 
        if (tms == NULL) | 
| 238 | 
  | 
  | 
                returnErr(TM_E_TMINVAL); | 
| 239 | 
  | 
  | 
        if ((ls == NULL) | (scan == NULL) | (len < 0)) | 
| 240 | 
  | 
  | 
                returnErr(TM_E_ILLEGAL); | 
| 241 | 
greg | 
3.24 | 
        if (tmFloat2BrtLUT == NULL)                     /* initialize */ | 
| 242 | 
  | 
  | 
                tmCvLums(NULL, NULL, 0); | 
| 243 | 
greg | 
3.23 | 
        for (i = len; i--; ) { | 
| 244 | 
  | 
  | 
                float   lum = tms->inpsf * scan[i]; | 
| 245 | 
  | 
  | 
                if (lum <= TM_NOLUM) | 
| 246 | 
  | 
  | 
                        ls[i] = TM_NOBRT; | 
| 247 | 
  | 
  | 
                else | 
| 248 | 
  | 
  | 
                        ls[i] = tmCvLumLUfp(&lum); | 
| 249 | 
  | 
  | 
        } | 
| 250 | 
  | 
  | 
        returnOK; | 
| 251 | 
  | 
  | 
} | 
| 252 | 
  | 
  | 
 | 
| 253 | 
  | 
  | 
 | 
| 254 | 
greg | 
3.1 | 
int | 
| 255 | 
greg | 
3.16 | 
tmCvColors(                             /* convert float colors */ | 
| 256 | 
  | 
  | 
TMstruct        *tms, | 
| 257 | 
  | 
  | 
TMbright        *ls, | 
| 258 | 
  | 
  | 
BYTE    *cs, | 
| 259 | 
  | 
  | 
COLOR   *scan, | 
| 260 | 
  | 
  | 
int     len | 
| 261 | 
  | 
  | 
) | 
| 262 | 
greg | 
3.1 | 
{ | 
| 263 | 
greg | 
3.17 | 
        static const char funcName[] = "tmCvColors"; | 
| 264 | 
greg | 
3.20 | 
        static BYTE     gamtab[1024]; | 
| 265 | 
  | 
  | 
        static double   curgam = .0; | 
| 266 | 
greg | 
3.1 | 
        COLOR   cmon; | 
| 267 | 
greg | 
3.23 | 
        float   lum, slum, d; | 
| 268 | 
greg | 
3.16 | 
        int     i; | 
| 269 | 
greg | 
3.1 | 
 | 
| 270 | 
greg | 
3.16 | 
        if (tms == NULL) | 
| 271 | 
greg | 
3.1 | 
                returnErr(TM_E_TMINVAL); | 
| 272 | 
schorsch | 
3.13 | 
        if ((ls == NULL) | (scan == NULL) | (len < 0)) | 
| 273 | 
greg | 
3.1 | 
                returnErr(TM_E_ILLEGAL); | 
| 274 | 
greg | 
3.24 | 
        if (tmFloat2BrtLUT == NULL)                     /* initialize */ | 
| 275 | 
  | 
  | 
                tmCvLums(NULL, NULL, 0); | 
| 276 | 
greg | 
3.21 | 
        if (cs != TM_NOCHROM && fabs(tms->mongam - curgam) > .02) { | 
| 277 | 
greg | 
3.20 | 
                curgam = tms->mongam;                   /* (re)build table */ | 
| 278 | 
  | 
  | 
                for (i = 1024; i--; ) | 
| 279 | 
  | 
  | 
                        gamtab[i] = (int)(256.*pow((i+.5)/1024., 1./curgam)); | 
| 280 | 
  | 
  | 
        } | 
| 281 | 
greg | 
3.1 | 
        for (i = len; i--; ) { | 
| 282 | 
greg | 
3.16 | 
                if (tmNeedMatrix(tms)) {                /* get monitor RGB */ | 
| 283 | 
  | 
  | 
                        colortrans(cmon, tms->cmat, scan[i]); | 
| 284 | 
greg | 
3.15 | 
                } else { | 
| 285 | 
greg | 
3.16 | 
                        cmon[RED] = tms->inpsf*scan[i][RED]; | 
| 286 | 
  | 
  | 
                        cmon[GRN] = tms->inpsf*scan[i][GRN]; | 
| 287 | 
  | 
  | 
                        cmon[BLU] = tms->inpsf*scan[i][BLU]; | 
| 288 | 
greg | 
3.1 | 
                } | 
| 289 | 
greg | 
3.23 | 
#ifdef isfinite | 
| 290 | 
  | 
  | 
                if (!isfinite(cmon[RED]) || cmon[RED] < .0f) cmon[RED] = .0f; | 
| 291 | 
  | 
  | 
                if (!isfinite(cmon[GRN]) || cmon[GRN] < .0f) cmon[GRN] = .0f; | 
| 292 | 
  | 
  | 
                if (!isfinite(cmon[BLU]) || cmon[BLU] < .0f) cmon[BLU] = .0f; | 
| 293 | 
  | 
  | 
#else | 
| 294 | 
  | 
  | 
                if (cmon[RED] < .0f) cmon[RED] = .0f; | 
| 295 | 
  | 
  | 
                if (cmon[GRN] < .0f) cmon[GRN] = .0f; | 
| 296 | 
  | 
  | 
                if (cmon[BLU] < .0f) cmon[BLU] = .0f; | 
| 297 | 
greg | 
3.22 | 
#endif | 
| 298 | 
greg | 
3.1 | 
                                                        /* world luminance */ | 
| 299 | 
greg | 
3.16 | 
                lum =   tms->clf[RED]*cmon[RED] + | 
| 300 | 
  | 
  | 
                        tms->clf[GRN]*cmon[GRN] + | 
| 301 | 
  | 
  | 
                        tms->clf[BLU]*cmon[BLU] ; | 
| 302 | 
greg | 
3.25 | 
                if (lum <= TM_NOLUM) {                  /* convert brightness */ | 
| 303 | 
  | 
  | 
                        lum = cmon[RED] = cmon[GRN] = cmon[BLU] = TM_NOLUM; | 
| 304 | 
greg | 
3.23 | 
                        ls[i] = TM_NOBRT; | 
| 305 | 
greg | 
3.25 | 
                } else | 
| 306 | 
greg | 
3.23 | 
                        ls[i] = tmCvLumLUfp(&lum); | 
| 307 | 
greg | 
3.1 | 
                if (cs == TM_NOCHROM)                   /* no color? */ | 
| 308 | 
  | 
  | 
                        continue; | 
| 309 | 
greg | 
3.16 | 
                if (tms->flags & TM_F_MESOPIC && lum < LMESUPPER) { | 
| 310 | 
greg | 
3.1 | 
                        slum = scotlum(cmon);           /* mesopic adj. */ | 
| 311 | 
greg | 
3.25 | 
                        if (lum < LMESLOWER) { | 
| 312 | 
greg | 
3.1 | 
                                cmon[RED] = cmon[GRN] = cmon[BLU] = slum; | 
| 313 | 
greg | 
3.25 | 
                        } else { | 
| 314 | 
greg | 
3.1 | 
                                d = (lum - LMESLOWER)/(LMESUPPER - LMESLOWER); | 
| 315 | 
greg | 
3.16 | 
                                if (tms->flags & TM_F_BW) | 
| 316 | 
greg | 
3.1 | 
                                        cmon[RED] = cmon[GRN] = | 
| 317 | 
  | 
  | 
                                                        cmon[BLU] = d*lum; | 
| 318 | 
  | 
  | 
                                else | 
| 319 | 
  | 
  | 
                                        scalecolor(cmon, d); | 
| 320 | 
greg | 
3.23 | 
                                d = (1.f-d)*slum; | 
| 321 | 
greg | 
3.1 | 
                                cmon[RED] += d; | 
| 322 | 
  | 
  | 
                                cmon[GRN] += d; | 
| 323 | 
  | 
  | 
                                cmon[BLU] += d; | 
| 324 | 
  | 
  | 
                        } | 
| 325 | 
greg | 
3.16 | 
                } else if (tms->flags & TM_F_BW) { | 
| 326 | 
greg | 
3.1 | 
                        cmon[RED] = cmon[GRN] = cmon[BLU] = lum; | 
| 327 | 
  | 
  | 
                } | 
| 328 | 
greg | 
3.16 | 
                d = tms->clf[RED]*cmon[RED]/lum; | 
| 329 | 
greg | 
3.23 | 
                cs[3*i  ] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; | 
| 330 | 
greg | 
3.16 | 
                d = tms->clf[GRN]*cmon[GRN]/lum; | 
| 331 | 
greg | 
3.23 | 
                cs[3*i+1] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; | 
| 332 | 
greg | 
3.16 | 
                d = tms->clf[BLU]*cmon[BLU]/lum; | 
| 333 | 
greg | 
3.23 | 
                cs[3*i+2] = d>=.999f ? 255 : gamtab[(int)(1024.f*d)]; | 
| 334 | 
greg | 
3.1 | 
        } | 
| 335 | 
  | 
  | 
        returnOK; | 
| 336 | 
  | 
  | 
} | 
| 337 | 
  | 
  | 
 | 
| 338 | 
  | 
  | 
 | 
| 339 | 
greg | 
3.9 | 
int | 
| 340 | 
greg | 
3.16 | 
tmAddHisto(                             /* add values to histogram */ | 
| 341 | 
  | 
  | 
TMstruct        *tms, | 
| 342 | 
  | 
  | 
TMbright        *ls, | 
| 343 | 
  | 
  | 
int     len, | 
| 344 | 
  | 
  | 
int     wt | 
| 345 | 
  | 
  | 
) | 
| 346 | 
greg | 
3.1 | 
{ | 
| 347 | 
greg | 
3.17 | 
        static const char funcName[] = "tmAddHisto"; | 
| 348 | 
greg | 
3.9 | 
        int     oldorig=0, oldlen, horig, hlen; | 
| 349 | 
greg | 
3.16 | 
        int     i, j; | 
| 350 | 
greg | 
3.1 | 
 | 
| 351 | 
greg | 
3.16 | 
        if (tms == NULL) | 
| 352 | 
greg | 
3.1 | 
                returnErr(TM_E_TMINVAL); | 
| 353 | 
gwlarson | 
3.8 | 
        if (len < 0) | 
| 354 | 
  | 
  | 
                returnErr(TM_E_ILLEGAL); | 
| 355 | 
  | 
  | 
        if (len == 0) | 
| 356 | 
  | 
  | 
                returnOK; | 
| 357 | 
greg | 
3.1 | 
                                                /* first, grow limits */ | 
| 358 | 
greg | 
3.16 | 
        if (tms->histo == NULL) { | 
| 359 | 
greg | 
3.1 | 
                for (i = len; i-- && ls[i] < MINBRT; ) | 
| 360 | 
  | 
  | 
                        ; | 
| 361 | 
  | 
  | 
                if (i < 0) | 
| 362 | 
  | 
  | 
                        returnOK; | 
| 363 | 
greg | 
3.16 | 
                tms->hbrmin = tms->hbrmax = ls[i]; | 
| 364 | 
greg | 
3.1 | 
                oldlen = 0; | 
| 365 | 
  | 
  | 
        } else { | 
| 366 | 
greg | 
3.31 | 
                oldorig = HISTI(tms->hbrmin); | 
| 367 | 
  | 
  | 
                oldlen = HISTI(tms->hbrmax) + 1 - oldorig; | 
| 368 | 
greg | 
3.1 | 
        } | 
| 369 | 
  | 
  | 
        for (i = len; i--; ) { | 
| 370 | 
  | 
  | 
                if ((j = ls[i]) < MINBRT) | 
| 371 | 
  | 
  | 
                        continue; | 
| 372 | 
greg | 
3.16 | 
                if (j < tms->hbrmin) | 
| 373 | 
  | 
  | 
                        tms->hbrmin = j; | 
| 374 | 
  | 
  | 
                else if (j > tms->hbrmax) | 
| 375 | 
  | 
  | 
                        tms->hbrmax = j; | 
| 376 | 
greg | 
3.1 | 
        } | 
| 377 | 
greg | 
3.31 | 
        horig = HISTI(tms->hbrmin); | 
| 378 | 
  | 
  | 
        hlen = HISTI(tms->hbrmax) + 1 - horig; | 
| 379 | 
greg | 
3.1 | 
        if (hlen > oldlen) {                    /* (re)allocate histogram */ | 
| 380 | 
greg | 
3.16 | 
                int     *newhist = (int *)calloc(hlen, sizeof(int)); | 
| 381 | 
greg | 
3.1 | 
                if (newhist == NULL) | 
| 382 | 
  | 
  | 
                        returnErr(TM_E_NOMEM); | 
| 383 | 
  | 
  | 
                if (oldlen) {                   /* copy and free old */ | 
| 384 | 
greg | 
3.2 | 
                        for (i = oldlen, j = i+oldorig-horig; i; ) | 
| 385 | 
greg | 
3.16 | 
                                newhist[--j] = tms->histo[--i]; | 
| 386 | 
  | 
  | 
                        free((MEM_PTR)tms->histo); | 
| 387 | 
greg | 
3.1 | 
                } | 
| 388 | 
greg | 
3.16 | 
                tms->histo = newhist; | 
| 389 | 
greg | 
3.1 | 
        } | 
| 390 | 
  | 
  | 
        if (wt == 0) | 
| 391 | 
  | 
  | 
                returnOK; | 
| 392 | 
  | 
  | 
        for (i = len; i--; )                    /* add in new counts */ | 
| 393 | 
  | 
  | 
                if (ls[i] >= MINBRT) | 
| 394 | 
greg | 
3.31 | 
                        tms->histo[ HISTI(ls[i]) - horig ] += wt; | 
| 395 | 
greg | 
3.1 | 
        returnOK; | 
| 396 | 
  | 
  | 
} | 
| 397 | 
  | 
  | 
 | 
| 398 | 
  | 
  | 
 | 
| 399 | 
  | 
  | 
static double | 
| 400 | 
greg | 
3.16 | 
htcontrs(               /* human threshold contrast sensitivity, dL(La) */ | 
| 401 | 
  | 
  | 
double  La | 
| 402 | 
  | 
  | 
) | 
| 403 | 
greg | 
3.1 | 
{ | 
| 404 | 
  | 
  | 
        double  l10La, l10dL; | 
| 405 | 
  | 
  | 
                                /* formula taken from Ferwerda et al. [SG96] */ | 
| 406 | 
  | 
  | 
        if (La < 1.148e-4) | 
| 407 | 
  | 
  | 
                return(1.38e-3); | 
| 408 | 
  | 
  | 
        l10La = log10(La); | 
| 409 | 
  | 
  | 
        if (l10La < -1.44)              /* rod response regime */ | 
| 410 | 
  | 
  | 
                l10dL = pow(.405*l10La + 1.6, 2.18) - 2.86; | 
| 411 | 
  | 
  | 
        else if (l10La < -.0184) | 
| 412 | 
  | 
  | 
                l10dL = l10La - .395; | 
| 413 | 
  | 
  | 
        else if (l10La < 1.9)           /* cone response regime */ | 
| 414 | 
  | 
  | 
                l10dL = pow(.249*l10La + .65, 2.7) - .72; | 
| 415 | 
  | 
  | 
        else | 
| 416 | 
  | 
  | 
                l10dL = l10La - 1.255; | 
| 417 | 
  | 
  | 
 | 
| 418 | 
  | 
  | 
        return(exp10(l10dL)); | 
| 419 | 
  | 
  | 
} | 
| 420 | 
  | 
  | 
 | 
| 421 | 
  | 
  | 
 | 
| 422 | 
greg | 
3.9 | 
static int | 
| 423 | 
greg | 
3.16 | 
tmNewMap(                       /* allocate new tone-mapping array */ | 
| 424 | 
  | 
  | 
TMstruct        *tms | 
| 425 | 
  | 
  | 
) | 
| 426 | 
greg | 
3.9 | 
{ | 
| 427 | 
greg | 
3.16 | 
        if (tms->lumap != NULL && (tms->mbrmax - tms->mbrmin) != | 
| 428 | 
  | 
  | 
                                        (tms->hbrmax - tms->hbrmin)) { | 
| 429 | 
  | 
  | 
                free((MEM_PTR)tms->lumap); | 
| 430 | 
  | 
  | 
                tms->lumap = NULL; | 
| 431 | 
  | 
  | 
        } | 
| 432 | 
  | 
  | 
        tms->mbrmin = tms->hbrmin; | 
| 433 | 
  | 
  | 
        tms->mbrmax = tms->hbrmax; | 
| 434 | 
  | 
  | 
        if (tms->mbrmin > tms->mbrmax) | 
| 435 | 
greg | 
3.9 | 
                return 0; | 
| 436 | 
greg | 
3.16 | 
        if (tms->lumap == NULL) | 
| 437 | 
  | 
  | 
                tms->lumap = (unsigned short *)malloc(sizeof(unsigned short)* | 
| 438 | 
  | 
  | 
                                        (tms->mbrmax-tms->mbrmin+1)); | 
| 439 | 
  | 
  | 
        return(tms->lumap != NULL); | 
| 440 | 
greg | 
3.9 | 
} | 
| 441 | 
  | 
  | 
 | 
| 442 | 
  | 
  | 
 | 
| 443 | 
  | 
  | 
int | 
| 444 | 
greg | 
3.16 | 
tmFixedMapping(                 /* compute fixed, linear tone-mapping */ | 
| 445 | 
  | 
  | 
TMstruct        *tms, | 
| 446 | 
  | 
  | 
double  expmult, | 
| 447 | 
  | 
  | 
double  gamval | 
| 448 | 
  | 
  | 
) | 
| 449 | 
greg | 
3.9 | 
{ | 
| 450 | 
greg | 
3.17 | 
        static const char funcName[] = "tmFixedMapping"; | 
| 451 | 
greg | 
3.9 | 
        double          d; | 
| 452 | 
greg | 
3.16 | 
        int     i; | 
| 453 | 
greg | 
3.9 | 
         | 
| 454 | 
greg | 
3.16 | 
        if (!tmNewMap(tms)) | 
| 455 | 
greg | 
3.9 | 
                returnErr(TM_E_NOMEM); | 
| 456 | 
  | 
  | 
        if (expmult <= .0) | 
| 457 | 
  | 
  | 
                expmult = 1.; | 
| 458 | 
  | 
  | 
        if (gamval < MINGAM) | 
| 459 | 
greg | 
3.16 | 
                gamval = tms->mongam; | 
| 460 | 
  | 
  | 
        d = log(expmult/tms->inpsf); | 
| 461 | 
greg | 
3.28 | 
        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { | 
| 462 | 
  | 
  | 
                double  val = 256. * exp( | 
| 463 | 
greg | 
3.16 | 
                        ( d + (tms->mbrmin+i)*(1./TM_BRTSCALE) ) | 
| 464 | 
greg | 
3.28 | 
                        / gamval); | 
| 465 | 
  | 
  | 
                tms->lumap[i] = val >= (double)0xffff ? 0xffff : (int)val; | 
| 466 | 
  | 
  | 
        } | 
| 467 | 
greg | 
3.9 | 
        returnOK; | 
| 468 | 
  | 
  | 
} | 
| 469 | 
  | 
  | 
 | 
| 470 | 
  | 
  | 
 | 
| 471 | 
greg | 
3.1 | 
int | 
| 472 | 
greg | 
3.16 | 
tmComputeMapping(                       /* compute histogram tone-mapping */ | 
| 473 | 
  | 
  | 
TMstruct        *tms, | 
| 474 | 
  | 
  | 
double  gamval, | 
| 475 | 
  | 
  | 
double  Lddyn, | 
| 476 | 
  | 
  | 
double  Ldmax | 
| 477 | 
  | 
  | 
) | 
| 478 | 
greg | 
3.1 | 
{ | 
| 479 | 
greg | 
3.17 | 
        static const char funcName[] = "tmComputeMapping"; | 
| 480 | 
greg | 
3.1 | 
        int     *histo; | 
| 481 | 
  | 
  | 
        float   *cumf; | 
| 482 | 
greg | 
3.9 | 
        int     brt0, histlen, threshold, ceiling, trimmings; | 
| 483 | 
greg | 
3.1 | 
        double  logLddyn, Ldmin, Ldavg, Lwavg, Tr, Lw, Ld; | 
| 484 | 
greg | 
3.11 | 
        int32   histot; | 
| 485 | 
greg | 
3.9 | 
        double  sum; | 
| 486 | 
greg | 
3.16 | 
        double  d; | 
| 487 | 
  | 
  | 
        int     i, j; | 
| 488 | 
greg | 
3.1 | 
 | 
| 489 | 
greg | 
3.16 | 
        if (tms == NULL || tms->histo == NULL) | 
| 490 | 
greg | 
3.1 | 
                returnErr(TM_E_TMINVAL); | 
| 491 | 
  | 
  | 
                                        /* check arguments */ | 
| 492 | 
  | 
  | 
        if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; | 
| 493 | 
  | 
  | 
        if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; | 
| 494 | 
greg | 
3.16 | 
        if (gamval < MINGAM) gamval = tms->mongam; | 
| 495 | 
greg | 
3.1 | 
                                        /* compute handy values */ | 
| 496 | 
  | 
  | 
        Ldmin = Ldmax/Lddyn; | 
| 497 | 
  | 
  | 
        logLddyn = log(Lddyn); | 
| 498 | 
  | 
  | 
        Ldavg = sqrt(Ldmax*Ldmin); | 
| 499 | 
greg | 
3.31 | 
        i = HISTI(tms->hbrmin); | 
| 500 | 
greg | 
3.32 | 
        brt0 = HISTV(i); | 
| 501 | 
greg | 
3.31 | 
        histlen = HISTI(tms->hbrmax) + 1 - i; | 
| 502 | 
greg | 
3.1 | 
                                        /* histogram total and mean */ | 
| 503 | 
  | 
  | 
        histot = 0; sum = 0; | 
| 504 | 
  | 
  | 
        j = brt0 + histlen*HISTEP; | 
| 505 | 
  | 
  | 
        for (i = histlen; i--; ) { | 
| 506 | 
greg | 
3.16 | 
                histot += tms->histo[i]; | 
| 507 | 
greg | 
3.30 | 
                sum += (double)(j -= HISTEP) * tms->histo[i]; | 
| 508 | 
greg | 
3.1 | 
        } | 
| 509 | 
greg | 
3.14 | 
        threshold = histot*0.005 + .5; | 
| 510 | 
greg | 
3.29 | 
        if (!histot) | 
| 511 | 
greg | 
3.1 | 
                returnErr(TM_E_TMFAIL); | 
| 512 | 
  | 
  | 
        Lwavg = tmLuminance( (double)sum / histot ); | 
| 513 | 
greg | 
3.29 | 
                                        /* use linear tone mapping? */ | 
| 514 | 
greg | 
3.30 | 
        if (tms->flags & TM_F_LINEAR || threshold < 4 || | 
| 515 | 
  | 
  | 
                        tms->hbrmax - tms->hbrmin < TM_BRTSCALE*logLddyn) | 
| 516 | 
greg | 
3.29 | 
                goto linearmap; | 
| 517 | 
greg | 
3.9 | 
                                        /* clamp histogram */ | 
| 518 | 
  | 
  | 
        histo = (int *)malloc(histlen*sizeof(int)); | 
| 519 | 
  | 
  | 
        cumf = (float *)malloc((histlen+2)*sizeof(float)); | 
| 520 | 
schorsch | 
3.13 | 
        if ((histo == NULL) | (cumf == NULL)) | 
| 521 | 
greg | 
3.9 | 
                returnErr(TM_E_NOMEM); | 
| 522 | 
  | 
  | 
        cumf[histlen+1] = 1.;           /* guard for assignment code */ | 
| 523 | 
  | 
  | 
        for (i = histlen; i--; )        /* make malleable copy */ | 
| 524 | 
greg | 
3.16 | 
                histo[i] = tms->histo[i]; | 
| 525 | 
greg | 
3.9 | 
        do {                            /* iterate to solution */ | 
| 526 | 
  | 
  | 
                sum = 0;                /* cumulative probability */ | 
| 527 | 
  | 
  | 
                for (i = 0; i < histlen; i++) { | 
| 528 | 
  | 
  | 
                        cumf[i] = (double)sum/histot; | 
| 529 | 
  | 
  | 
                        sum += histo[i]; | 
| 530 | 
  | 
  | 
                } | 
| 531 | 
  | 
  | 
                cumf[histlen] = 1.; | 
| 532 | 
greg | 
3.16 | 
                Tr = histot * (double)(tms->hbrmax - tms->hbrmin) / | 
| 533 | 
greg | 
3.9 | 
                        ((double)histlen*TM_BRTSCALE) / logLddyn; | 
| 534 | 
  | 
  | 
                ceiling = Tr + 1.; | 
| 535 | 
  | 
  | 
                trimmings = 0;          /* clip to envelope */ | 
| 536 | 
  | 
  | 
                for (i = histlen; i--; ) { | 
| 537 | 
greg | 
3.16 | 
                        if (tms->flags & TM_F_HCONTR) { | 
| 538 | 
greg | 
3.9 | 
                                Lw = tmLuminance(brt0 + i*HISTEP); | 
| 539 | 
  | 
  | 
                                Ld = Ldmin * exp( logLddyn * | 
| 540 | 
  | 
  | 
                                        .5*(cumf[i]+cumf[i+1]) ); | 
| 541 | 
  | 
  | 
                                ceiling = Tr * (htcontrs(Ld) * Lw) / | 
| 542 | 
  | 
  | 
                                        (htcontrs(Lw) * Ld) + 1.; | 
| 543 | 
greg | 
3.2 | 
                        } | 
| 544 | 
greg | 
3.9 | 
                        if (histo[i] > ceiling) { | 
| 545 | 
  | 
  | 
                                trimmings += histo[i] - ceiling; | 
| 546 | 
  | 
  | 
                                histo[i] = ceiling; | 
| 547 | 
greg | 
3.1 | 
                        } | 
| 548 | 
  | 
  | 
                } | 
| 549 | 
greg | 
3.9 | 
                                        /* check if we're out of data */ | 
| 550 | 
  | 
  | 
                if ((histot -= trimmings) <= threshold) { | 
| 551 | 
  | 
  | 
                        free((MEM_PTR)histo); | 
| 552 | 
  | 
  | 
                        free((MEM_PTR)cumf); | 
| 553 | 
  | 
  | 
                        goto linearmap; | 
| 554 | 
  | 
  | 
                } | 
| 555 | 
  | 
  | 
        } while (trimmings > threshold); | 
| 556 | 
greg | 
3.30 | 
                                        /* allocate space for mapping */ | 
| 557 | 
  | 
  | 
        if (!tmNewMap(tms)) | 
| 558 | 
  | 
  | 
                returnErr(TM_E_NOMEM); | 
| 559 | 
greg | 
3.9 | 
                                        /* assign tone-mapping */ | 
| 560 | 
greg | 
3.16 | 
        for (i = tms->mbrmax-tms->mbrmin+1; i--; ) { | 
| 561 | 
  | 
  | 
                j = d = (double)i/(tms->mbrmax-tms->mbrmin)*histlen; | 
| 562 | 
greg | 
3.9 | 
                d -= (double)j; | 
| 563 | 
  | 
  | 
                Ld = Ldmin*exp(logLddyn*((1.-d)*cumf[j]+d*cumf[j+1])); | 
| 564 | 
  | 
  | 
                d = (Ld - Ldmin)/(Ldmax - Ldmin); | 
| 565 | 
greg | 
3.16 | 
                tms->lumap[i] = 256.*pow(d, 1./gamval); | 
| 566 | 
greg | 
3.1 | 
        } | 
| 567 | 
greg | 
3.9 | 
        free((MEM_PTR)histo);           /* clean up and return */ | 
| 568 | 
  | 
  | 
        free((MEM_PTR)cumf); | 
| 569 | 
greg | 
3.1 | 
        returnOK; | 
| 570 | 
greg | 
3.9 | 
linearmap:                              /* linear tone-mapping */ | 
| 571 | 
greg | 
3.16 | 
        if (tms->flags & TM_F_HCONTR) | 
| 572 | 
greg | 
3.9 | 
                d = htcontrs(Ldavg) / htcontrs(Lwavg); | 
| 573 | 
  | 
  | 
        else | 
| 574 | 
  | 
  | 
                d = Ldavg / Lwavg; | 
| 575 | 
greg | 
3.16 | 
        return(tmFixedMapping(tms, tms->inpsf*d/Ldmax, gamval)); | 
| 576 | 
greg | 
3.1 | 
} | 
| 577 | 
  | 
  | 
 | 
| 578 | 
  | 
  | 
 | 
| 579 | 
  | 
  | 
int | 
| 580 | 
greg | 
3.16 | 
tmMapPixels(                    /* apply tone-mapping to pixel(s) */ | 
| 581 | 
  | 
  | 
TMstruct        *tms, | 
| 582 | 
  | 
  | 
BYTE    *ps, | 
| 583 | 
  | 
  | 
TMbright        *ls, | 
| 584 | 
  | 
  | 
BYTE    *cs, | 
| 585 | 
  | 
  | 
int     len | 
| 586 | 
  | 
  | 
) | 
| 587 | 
greg | 
3.1 | 
{ | 
| 588 | 
greg | 
3.17 | 
        static const char funcName[] = "tmMapPixels"; | 
| 589 | 
greg | 
3.16 | 
        int32   li, pv; | 
| 590 | 
greg | 
3.1 | 
 | 
| 591 | 
greg | 
3.16 | 
        if (tms == NULL || tms->lumap == NULL) | 
| 592 | 
greg | 
3.1 | 
                returnErr(TM_E_TMINVAL); | 
| 593 | 
schorsch | 
3.13 | 
        if ((ps == NULL) | (ls == NULL) | (len < 0)) | 
| 594 | 
greg | 
3.1 | 
                returnErr(TM_E_ILLEGAL); | 
| 595 | 
  | 
  | 
        while (len--) { | 
| 596 | 
greg | 
3.16 | 
                if ((li = *ls++) < tms->mbrmin) { | 
| 597 | 
greg | 
3.9 | 
                        li = 0; | 
| 598 | 
  | 
  | 
                } else { | 
| 599 | 
greg | 
3.16 | 
                        if (li > tms->mbrmax) | 
| 600 | 
  | 
  | 
                                li = tms->mbrmax; | 
| 601 | 
  | 
  | 
                        li = tms->lumap[li - tms->mbrmin]; | 
| 602 | 
greg | 
3.9 | 
                } | 
| 603 | 
greg | 
3.1 | 
                if (cs == TM_NOCHROM) | 
| 604 | 
  | 
  | 
                        *ps++ = li>255 ? 255 : li; | 
| 605 | 
  | 
  | 
                else { | 
| 606 | 
greg | 
3.16 | 
                        pv = *cs++ * li / tms->cdiv[RED]; | 
| 607 | 
greg | 
3.1 | 
                        *ps++ = pv>255 ? 255 : pv; | 
| 608 | 
greg | 
3.16 | 
                        pv = *cs++ * li / tms->cdiv[GRN]; | 
| 609 | 
greg | 
3.1 | 
                        *ps++ = pv>255 ? 255 : pv; | 
| 610 | 
greg | 
3.16 | 
                        pv = *cs++ * li / tms->cdiv[BLU]; | 
| 611 | 
greg | 
3.1 | 
                        *ps++ = pv>255 ? 255 : pv; | 
| 612 | 
  | 
  | 
                } | 
| 613 | 
  | 
  | 
        } | 
| 614 | 
  | 
  | 
        returnOK; | 
| 615 | 
  | 
  | 
} | 
| 616 | 
  | 
  | 
 | 
| 617 | 
  | 
  | 
 | 
| 618 | 
  | 
  | 
 | 
| 619 | 
  | 
  | 
 | 
| 620 | 
greg | 
3.16 | 
TMstruct * | 
| 621 | 
  | 
  | 
tmDup(                          /* duplicate top tone mapping */ | 
| 622 | 
  | 
  | 
TMstruct        *tms | 
| 623 | 
  | 
  | 
) | 
| 624 | 
greg | 
3.3 | 
{ | 
| 625 | 
  | 
  | 
        int     len; | 
| 626 | 
greg | 
3.16 | 
        int     i; | 
| 627 | 
  | 
  | 
        TMstruct        *tmnew; | 
| 628 | 
greg | 
3.3 | 
 | 
| 629 | 
greg | 
3.16 | 
        if (tms == NULL)                /* anything to duplicate? */ | 
| 630 | 
greg | 
3.3 | 
                return(NULL); | 
| 631 | 
greg | 
3.16 | 
        tmnew = (TMstruct *)malloc(sizeof(TMstruct)); | 
| 632 | 
greg | 
3.3 | 
        if (tmnew == NULL) | 
| 633 | 
  | 
  | 
                return(NULL); | 
| 634 | 
greg | 
3.16 | 
        *tmnew = *tms;          /* copy everything */ | 
| 635 | 
greg | 
3.3 | 
        if (tmnew->histo != NULL) {     /* duplicate histogram */ | 
| 636 | 
greg | 
3.31 | 
                len = HISTI(tmnew->hbrmax) + 1 - HISTI(tmnew->hbrmin); | 
| 637 | 
greg | 
3.3 | 
                tmnew->histo = (int *)malloc(len*sizeof(int)); | 
| 638 | 
  | 
  | 
                if (tmnew->histo != NULL) | 
| 639 | 
  | 
  | 
                        for (i = len; i--; ) | 
| 640 | 
greg | 
3.16 | 
                                tmnew->histo[i] = tms->histo[i]; | 
| 641 | 
greg | 
3.3 | 
        } | 
| 642 | 
  | 
  | 
        if (tmnew->lumap != NULL) {     /* duplicate luminance mapping */ | 
| 643 | 
gregl | 
3.6 | 
                len = tmnew->mbrmax-tmnew->mbrmin+1; | 
| 644 | 
greg | 
3.3 | 
                tmnew->lumap = (unsigned short *)malloc( | 
| 645 | 
  | 
  | 
                                                len*sizeof(unsigned short) ); | 
| 646 | 
  | 
  | 
                if (tmnew->lumap != NULL) | 
| 647 | 
  | 
  | 
                        for (i = len; i--; ) | 
| 648 | 
greg | 
3.16 | 
                                tmnew->lumap[i] = tms->lumap[i]; | 
| 649 | 
greg | 
3.3 | 
        } | 
| 650 | 
greg | 
3.4 | 
                                        /* clear package data */ | 
| 651 | 
  | 
  | 
        for (i = tmNumPkgs; i--; ) | 
| 652 | 
  | 
  | 
                tmnew->pd[i] = NULL; | 
| 653 | 
greg | 
3.16 | 
                                        /* return copy */ | 
| 654 | 
  | 
  | 
        return(tmnew); | 
| 655 | 
greg | 
3.1 | 
} | 
| 656 | 
  | 
  | 
 | 
| 657 | 
  | 
  | 
 | 
| 658 | 
  | 
  | 
void | 
| 659 | 
  | 
  | 
tmDone(tms)                     /* done with tone mapping -- destroy it */ | 
| 660 | 
greg | 
3.16 | 
TMstruct        *tms; | 
| 661 | 
greg | 
3.1 | 
{ | 
| 662 | 
greg | 
3.16 | 
        int     i; | 
| 663 | 
  | 
  | 
                                        /* NULL arg. is equiv. to tms */ | 
| 664 | 
  | 
  | 
        if (tms == NULL) | 
| 665 | 
greg | 
3.1 | 
                return; | 
| 666 | 
  | 
  | 
                                        /* free tables */ | 
| 667 | 
  | 
  | 
        if (tms->histo != NULL) | 
| 668 | 
greg | 
3.4 | 
                free((MEM_PTR)tms->histo); | 
| 669 | 
greg | 
3.1 | 
        if (tms->lumap != NULL) | 
| 670 | 
greg | 
3.4 | 
                free((MEM_PTR)tms->lumap); | 
| 671 | 
  | 
  | 
                                        /* free private data */ | 
| 672 | 
  | 
  | 
        for (i = tmNumPkgs; i--; ) | 
| 673 | 
  | 
  | 
                if (tms->pd[i] != NULL) | 
| 674 | 
  | 
  | 
                        (*tmPkg[i]->Free)(tms->pd[i]); | 
| 675 | 
  | 
  | 
        free((MEM_PTR)tms);             /* free basic structure */ | 
| 676 | 
greg | 
3.9 | 
} | 
| 677 | 
  | 
  | 
 | 
| 678 | 
  | 
  | 
/******************** Shared but Private library routines *********************/ | 
| 679 | 
  | 
  | 
 | 
| 680 | 
  | 
  | 
BYTE    tmMesofact[BMESUPPER-BMESLOWER]; | 
| 681 | 
  | 
  | 
 | 
| 682 | 
  | 
  | 
void | 
| 683 | 
  | 
  | 
tmMkMesofact()                          /* build mesopic lookup factor table */ | 
| 684 | 
  | 
  | 
{ | 
| 685 | 
greg | 
3.16 | 
        int     i; | 
| 686 | 
greg | 
3.9 | 
 | 
| 687 | 
  | 
  | 
        if (tmMesofact[BMESUPPER-BMESLOWER-1]) | 
| 688 | 
  | 
  | 
                return; | 
| 689 | 
  | 
  | 
 | 
| 690 | 
  | 
  | 
        for (i = BMESLOWER; i < BMESUPPER; i++) | 
| 691 | 
  | 
  | 
                tmMesofact[i-BMESLOWER] = 256. * | 
| 692 | 
  | 
  | 
                                (tmLuminance(i) - LMESLOWER) / | 
| 693 | 
  | 
  | 
                                (LMESUPPER - LMESLOWER); | 
| 694 | 
  | 
  | 
} | 
| 695 | 
  | 
  | 
 | 
| 696 | 
  | 
  | 
 | 
| 697 | 
  | 
  | 
int | 
| 698 | 
greg | 
3.16 | 
tmErrorReturn(                          /* error return (with message) */ | 
| 699 | 
greg | 
3.17 | 
const char      *func, | 
| 700 | 
greg | 
3.16 | 
TMstruct        *tms, | 
| 701 | 
  | 
  | 
int     err | 
| 702 | 
  | 
  | 
) | 
| 703 | 
greg | 
3.9 | 
{ | 
| 704 | 
greg | 
3.17 | 
        if (tms != NULL) { | 
| 705 | 
  | 
  | 
                tms->lastFunc = func; | 
| 706 | 
  | 
  | 
                tms->lastError = err; | 
| 707 | 
  | 
  | 
                if (tms->flags & TM_F_NOSTDERR) | 
| 708 | 
  | 
  | 
                        return(err); | 
| 709 | 
  | 
  | 
        } | 
| 710 | 
greg | 
3.9 | 
        fputs(func, stderr); | 
| 711 | 
  | 
  | 
        fputs(": ", stderr); | 
| 712 | 
  | 
  | 
        fputs(tmErrorMessage[err], stderr); | 
| 713 | 
  | 
  | 
        fputs("!\n", stderr); | 
| 714 | 
  | 
  | 
        return(err); | 
| 715 | 
greg | 
3.1 | 
} |