| 1 | 
greg | 
3.1 | 
#ifndef lint | 
| 2 | 
greg | 
3.17 | 
static const char       RCSid[] = "$Id: tmapcolrs.c,v 3.16 2004/10/23 18:55:52 schorsch Exp $"; | 
| 3 | 
greg | 
3.1 | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 * Routines for tone mapping on Radiance RGBE and XYZE pictures. | 
| 6 | 
greg | 
3.10 | 
 * | 
| 7 | 
  | 
  | 
 * Externals declared in tonemap.h | 
| 8 | 
  | 
  | 
 */ | 
| 9 | 
  | 
  | 
 | 
| 10 | 
greg | 
3.11 | 
#include "copyright.h" | 
| 11 | 
greg | 
3.1 | 
 | 
| 12 | 
  | 
  | 
#include        <stdio.h> | 
| 13 | 
greg | 
3.10 | 
#include        <string.h> | 
| 14 | 
greg | 
3.1 | 
#include        <math.h> | 
| 15 | 
greg | 
3.10 | 
#include        <time.h> | 
| 16 | 
schorsch | 
3.16 | 
 | 
| 17 | 
greg | 
3.1 | 
#include        "tmprivat.h" | 
| 18 | 
  | 
  | 
#include        "resolu.h" | 
| 19 | 
schorsch | 
3.16 | 
#include        "rtprocess.h" | 
| 20 | 
greg | 
3.1 | 
 | 
| 21 | 
greg | 
3.10 | 
#ifndef TM_PIC_CTRANS | 
| 22 | 
  | 
  | 
#define TM_PIC_CTRANS   1               /* transform colors? (expensive) */ | 
| 23 | 
  | 
  | 
#endif | 
| 24 | 
greg | 
3.1 | 
 | 
| 25 | 
greg | 
3.3 | 
#define GAMTSZ  1024 | 
| 26 | 
  | 
  | 
 | 
| 27 | 
  | 
  | 
typedef struct { | 
| 28 | 
  | 
  | 
        BYTE            gamb[GAMTSZ];   /* gamma lookup table */ | 
| 29 | 
  | 
  | 
        COLR            clfb;           /* encoded tm->clf */ | 
| 30 | 
  | 
  | 
        TMbright        inpsfb;         /* encoded tm->inpsf */ | 
| 31 | 
  | 
  | 
} COLRDATA; | 
| 32 | 
  | 
  | 
 | 
| 33 | 
greg | 
3.17 | 
static MEM_PTR  colrInit(TMstruct *); | 
| 34 | 
  | 
  | 
static void     colrNewSpace(TMstruct *); | 
| 35 | 
schorsch | 
3.15 | 
static gethfunc headline; | 
| 36 | 
  | 
  | 
 | 
| 37 | 
greg | 
3.3 | 
static struct tmPackage colrPkg = {     /* our package functions */ | 
| 38 | 
  | 
  | 
        colrInit, colrNewSpace, free | 
| 39 | 
  | 
  | 
}; | 
| 40 | 
  | 
  | 
static int      colrReg = -1;           /* our package registration number */ | 
| 41 | 
  | 
  | 
 | 
| 42 | 
greg | 
3.1 | 
#define LOGISZ  260 | 
| 43 | 
  | 
  | 
static TMbright logi[LOGISZ]; | 
| 44 | 
  | 
  | 
 | 
| 45 | 
  | 
  | 
 | 
| 46 | 
  | 
  | 
int | 
| 47 | 
greg | 
3.17 | 
tmCvColrs(                              /* convert RGBE/XYZE colors */ | 
| 48 | 
  | 
  | 
TMstruct        *tms, | 
| 49 | 
  | 
  | 
TMbright        *ls, | 
| 50 | 
  | 
  | 
BYTE    *cs, | 
| 51 | 
  | 
  | 
COLR    *scan, | 
| 52 | 
  | 
  | 
int     len | 
| 53 | 
  | 
  | 
) | 
| 54 | 
greg | 
3.1 | 
{ | 
| 55 | 
  | 
  | 
        static char     funcName[] = "tmCvColrs"; | 
| 56 | 
  | 
  | 
        COLR    cmon; | 
| 57 | 
greg | 
3.3 | 
        register COLRDATA       *cd; | 
| 58 | 
greg | 
3.1 | 
        register int    i, bi, li; | 
| 59 | 
  | 
  | 
 | 
| 60 | 
greg | 
3.17 | 
        if (tms == NULL) | 
| 61 | 
greg | 
3.1 | 
                returnErr(TM_E_TMINVAL); | 
| 62 | 
schorsch | 
3.14 | 
        if ((ls == NULL) | (scan == NULL) | (len < 0)) | 
| 63 | 
greg | 
3.1 | 
                returnErr(TM_E_ILLEGAL); | 
| 64 | 
greg | 
3.10 | 
#if TM_PIC_CTRANS | 
| 65 | 
greg | 
3.17 | 
        if (tmNeedMatrix(tms)) {                /* need floating point */ | 
| 66 | 
greg | 
3.10 | 
#else | 
| 67 | 
greg | 
3.17 | 
        if (tms->inppri == TM_XYZPRIM) {        /* no way around this */ | 
| 68 | 
greg | 
3.10 | 
#endif | 
| 69 | 
greg | 
3.1 | 
                register COLOR  *newscan; | 
| 70 | 
  | 
  | 
                newscan = (COLOR *)tempbuffer(len*sizeof(COLOR)); | 
| 71 | 
  | 
  | 
                if (newscan == NULL) | 
| 72 | 
  | 
  | 
                        returnErr(TM_E_NOMEM); | 
| 73 | 
  | 
  | 
                for (i = len; i--; ) | 
| 74 | 
  | 
  | 
                        colr_color(newscan[i], scan[i]); | 
| 75 | 
greg | 
3.17 | 
                return(tmCvColors(tms, ls, cs, newscan, len)); | 
| 76 | 
greg | 
3.1 | 
        } | 
| 77 | 
gwlarson | 
3.6 | 
        if (colrReg < 0) {                      /* build tables if necessary */ | 
| 78 | 
greg | 
3.3 | 
                colrReg = tmRegPkg(&colrPkg); | 
| 79 | 
  | 
  | 
                if (colrReg < 0) | 
| 80 | 
  | 
  | 
                        returnErr(TM_E_CODERR1); | 
| 81 | 
greg | 
3.1 | 
                for (i = 256; i--; ) | 
| 82 | 
  | 
  | 
                        logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5; | 
| 83 | 
  | 
  | 
                for (i = 256; i < LOGISZ; i++) | 
| 84 | 
greg | 
3.10 | 
                        logi[i] = 0; | 
| 85 | 
  | 
  | 
                tmMkMesofact(); | 
| 86 | 
greg | 
3.1 | 
        } | 
| 87 | 
greg | 
3.17 | 
        if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL) | 
| 88 | 
greg | 
3.3 | 
                returnErr(TM_E_NOMEM); | 
| 89 | 
greg | 
3.1 | 
        for (i = len; i--; ) { | 
| 90 | 
  | 
  | 
                copycolr(cmon, scan[i]); | 
| 91 | 
  | 
  | 
                                                        /* world luminance */ | 
| 92 | 
greg | 
3.3 | 
                li =  ( cd->clfb[RED]*cmon[RED] + | 
| 93 | 
  | 
  | 
                        cd->clfb[GRN]*cmon[GRN] + | 
| 94 | 
  | 
  | 
                        cd->clfb[BLU]*cmon[BLU] ) >> 8; | 
| 95 | 
greg | 
3.10 | 
                bi = BRT2SCALE(cmon[EXP]-COLXS) + | 
| 96 | 
greg | 
3.3 | 
                                logi[li] + cd->inpsfb; | 
| 97 | 
greg | 
3.10 | 
                if (li <= 0) { | 
| 98 | 
  | 
  | 
                        bi = TM_NOBRT;                  /* bogus value */ | 
| 99 | 
  | 
  | 
                        li = 1;                         /* avoid li==0 */ | 
| 100 | 
greg | 
3.1 | 
                } | 
| 101 | 
  | 
  | 
                ls[i] = bi; | 
| 102 | 
  | 
  | 
                if (cs == TM_NOCHROM)                   /* no color? */ | 
| 103 | 
  | 
  | 
                        continue; | 
| 104 | 
  | 
  | 
                                                        /* mesopic adj. */ | 
| 105 | 
greg | 
3.17 | 
                if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) { | 
| 106 | 
greg | 
3.1 | 
                        register int    pf, sli = normscot(cmon); | 
| 107 | 
  | 
  | 
                        if (bi < BMESLOWER) | 
| 108 | 
  | 
  | 
                                cmon[RED] = cmon[GRN] = cmon[BLU] = sli; | 
| 109 | 
  | 
  | 
                        else { | 
| 110 | 
greg | 
3.17 | 
                                if (tms->flags & TM_F_BW) | 
| 111 | 
gwlarson | 
3.8 | 
                                        cmon[RED] = cmon[GRN] = cmon[BLU] = li; | 
| 112 | 
greg | 
3.10 | 
                                pf = tmMesofact[bi-BMESLOWER]; | 
| 113 | 
greg | 
3.1 | 
                                sli *= 256 - pf; | 
| 114 | 
  | 
  | 
                                cmon[RED] = ( sli + pf*cmon[RED] ) >> 8; | 
| 115 | 
  | 
  | 
                                cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8; | 
| 116 | 
  | 
  | 
                                cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8; | 
| 117 | 
  | 
  | 
                        } | 
| 118 | 
greg | 
3.17 | 
                } else if (tms->flags & TM_F_BW) { | 
| 119 | 
gwlarson | 
3.8 | 
                        cmon[RED] = cmon[GRN] = cmon[BLU] = li; | 
| 120 | 
greg | 
3.1 | 
                } | 
| 121 | 
greg | 
3.12 | 
                bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8; | 
| 122 | 
greg | 
3.3 | 
                cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; | 
| 123 | 
greg | 
3.12 | 
                bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8; | 
| 124 | 
greg | 
3.3 | 
                cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; | 
| 125 | 
greg | 
3.12 | 
                bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8; | 
| 126 | 
greg | 
3.3 | 
                cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi]; | 
| 127 | 
greg | 
3.1 | 
        } | 
| 128 | 
  | 
  | 
        returnOK; | 
| 129 | 
  | 
  | 
} | 
| 130 | 
  | 
  | 
 | 
| 131 | 
  | 
  | 
 | 
| 132 | 
  | 
  | 
#define FMTRGB          1       /* Input is RGBE */ | 
| 133 | 
  | 
  | 
#define FMTCIE          2       /* Input is CIE XYZE */ | 
| 134 | 
  | 
  | 
#define FMTUNK          3       /* Input format is unspecified */ | 
| 135 | 
  | 
  | 
#define FMTBAD          4       /* Input is not a recognized format */ | 
| 136 | 
  | 
  | 
 | 
| 137 | 
  | 
  | 
static struct radhead { | 
| 138 | 
  | 
  | 
        int     format;         /* FMTRGB, FMTCIE, FMTUNK, FMTBAD */ | 
| 139 | 
  | 
  | 
        double  expos;          /* input exposure value */ | 
| 140 | 
  | 
  | 
        RGBPRIMP        primp;  /* input primaries */ | 
| 141 | 
  | 
  | 
        RGBPRIMS        mypri;  /* custom primaries */ | 
| 142 | 
  | 
  | 
} rhdefault = {FMTUNK, 1., stdprims, STDPRIMS}; | 
| 143 | 
  | 
  | 
 | 
| 144 | 
  | 
  | 
 | 
| 145 | 
  | 
  | 
static int | 
| 146 | 
schorsch | 
3.15 | 
headline(                       /* grok a header line */ | 
| 147 | 
  | 
  | 
        register char   *s, | 
| 148 | 
  | 
  | 
        void    *vrh | 
| 149 | 
  | 
  | 
) | 
| 150 | 
greg | 
3.1 | 
{ | 
| 151 | 
  | 
  | 
        char    fmt[32]; | 
| 152 | 
schorsch | 
3.15 | 
        register struct radhead *rh = vrh; | 
| 153 | 
greg | 
3.1 | 
 | 
| 154 | 
  | 
  | 
        if (formatval(fmt, s)) { | 
| 155 | 
  | 
  | 
                if (!strcmp(fmt, COLRFMT)) | 
| 156 | 
  | 
  | 
                        rh->format = FMTRGB; | 
| 157 | 
  | 
  | 
                else if (!strcmp(fmt, CIEFMT)) | 
| 158 | 
  | 
  | 
                        rh->format = FMTCIE; | 
| 159 | 
  | 
  | 
                else | 
| 160 | 
  | 
  | 
                        rh->format = FMTBAD; | 
| 161 | 
gwlarson | 
3.7 | 
                return(0); | 
| 162 | 
greg | 
3.1 | 
        } | 
| 163 | 
  | 
  | 
        if (isexpos(s)) { | 
| 164 | 
  | 
  | 
                rh->expos *= exposval(s); | 
| 165 | 
gwlarson | 
3.7 | 
                return(0); | 
| 166 | 
greg | 
3.1 | 
        } | 
| 167 | 
  | 
  | 
        if (isprims(s)) { | 
| 168 | 
  | 
  | 
                primsval(rh->mypri, s); | 
| 169 | 
  | 
  | 
                rh->primp = rh->mypri; | 
| 170 | 
gwlarson | 
3.7 | 
                return(0); | 
| 171 | 
greg | 
3.1 | 
        } | 
| 172 | 
gwlarson | 
3.7 | 
        return(0); | 
| 173 | 
greg | 
3.1 | 
} | 
| 174 | 
  | 
  | 
 | 
| 175 | 
  | 
  | 
 | 
| 176 | 
  | 
  | 
int | 
| 177 | 
greg | 
3.17 | 
tmLoadPicture(                          /* convert Radiance picture */ | 
| 178 | 
  | 
  | 
TMstruct        *tms, | 
| 179 | 
  | 
  | 
TMbright        **lpp, | 
| 180 | 
  | 
  | 
BYTE    **cpp, | 
| 181 | 
  | 
  | 
int     *xp, | 
| 182 | 
  | 
  | 
int     *yp, | 
| 183 | 
  | 
  | 
char    *fname, | 
| 184 | 
  | 
  | 
FILE    *fp | 
| 185 | 
  | 
  | 
) | 
| 186 | 
greg | 
3.1 | 
{ | 
| 187 | 
  | 
  | 
        char    *funcName = fname==NULL ? "tmLoadPicture" : fname; | 
| 188 | 
  | 
  | 
        FILE    *inpf; | 
| 189 | 
  | 
  | 
        struct radhead  info; | 
| 190 | 
  | 
  | 
        int     err; | 
| 191 | 
  | 
  | 
        COLR    *scanin = NULL; | 
| 192 | 
  | 
  | 
        int     i; | 
| 193 | 
  | 
  | 
                                                /* check arguments */ | 
| 194 | 
greg | 
3.17 | 
        if (tms == NULL) | 
| 195 | 
greg | 
3.1 | 
                returnErr(TM_E_TMINVAL); | 
| 196 | 
schorsch | 
3.14 | 
        if ((lpp == NULL) | (xp == NULL) | (yp == NULL) | | 
| 197 | 
  | 
  | 
                        ((fname == NULL) & (fp == TM_GETFILE))) | 
| 198 | 
greg | 
3.1 | 
                returnErr(TM_E_ILLEGAL); | 
| 199 | 
  | 
  | 
        *xp = *yp = 0;                          /* error precaution */ | 
| 200 | 
  | 
  | 
        if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL) | 
| 201 | 
  | 
  | 
                returnErr(TM_E_BADFILE); | 
| 202 | 
gwlarson | 
3.6 | 
        *lpp = NULL; | 
| 203 | 
  | 
  | 
        if (cpp != TM_NOCHROMP) *cpp = NULL; | 
| 204 | 
greg | 
3.1 | 
        info = rhdefault;                       /* get our header */ | 
| 205 | 
schorsch | 
3.15 | 
        getheader(inpf, headline, &info); | 
| 206 | 
schorsch | 
3.14 | 
        if ((info.format == FMTBAD) | (info.expos <= 0.) || | 
| 207 | 
greg | 
3.1 | 
                        fgetresolu(xp, yp, inpf) < 0) { | 
| 208 | 
  | 
  | 
                err = TM_E_BADFILE; goto done; | 
| 209 | 
  | 
  | 
        } | 
| 210 | 
  | 
  | 
        if (info.format == FMTUNK)              /* assume RGBE format */ | 
| 211 | 
  | 
  | 
                info.format = FMTRGB; | 
| 212 | 
  | 
  | 
        if (info.format == FMTRGB) | 
| 213 | 
  | 
  | 
                info.expos /= WHTEFFICACY; | 
| 214 | 
  | 
  | 
        else if (info.format == FMTCIE) | 
| 215 | 
  | 
  | 
                info.primp = TM_XYZPRIM; | 
| 216 | 
  | 
  | 
                                                /* prepare library */ | 
| 217 | 
greg | 
3.17 | 
        if ((err = tmSetSpace(tms, info.primp, 1./info.expos)) != TM_E_OK) | 
| 218 | 
greg | 
3.1 | 
                goto done; | 
| 219 | 
  | 
  | 
        err = TM_E_NOMEM;                       /* allocate arrays */ | 
| 220 | 
  | 
  | 
        *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp); | 
| 221 | 
  | 
  | 
        if (*lpp == NULL) | 
| 222 | 
  | 
  | 
                goto done; | 
| 223 | 
  | 
  | 
        if (cpp != TM_NOCHROMP) { | 
| 224 | 
  | 
  | 
                *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); | 
| 225 | 
  | 
  | 
                if (*cpp == NULL) | 
| 226 | 
  | 
  | 
                        goto done; | 
| 227 | 
  | 
  | 
        } | 
| 228 | 
  | 
  | 
        scanin = (COLR *)malloc(sizeof(COLR) * *xp); | 
| 229 | 
  | 
  | 
        if (scanin == NULL) | 
| 230 | 
  | 
  | 
                goto done; | 
| 231 | 
  | 
  | 
        err = TM_E_BADFILE;                     /* read & convert scanlines */ | 
| 232 | 
  | 
  | 
        for (i = 0; i < *yp; i++) { | 
| 233 | 
  | 
  | 
                if (freadcolrs(scanin, *xp, inpf) < 0) { | 
| 234 | 
  | 
  | 
                        err = TM_E_BADFILE; break; | 
| 235 | 
  | 
  | 
                } | 
| 236 | 
greg | 
3.17 | 
                err = tmCvColrs(tms, *lpp + (i * *xp), | 
| 237 | 
greg | 
3.1 | 
                        cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp), | 
| 238 | 
  | 
  | 
                                scanin, *xp); | 
| 239 | 
  | 
  | 
                if (err != TM_E_OK) | 
| 240 | 
  | 
  | 
                        break; | 
| 241 | 
  | 
  | 
        } | 
| 242 | 
  | 
  | 
done:                                           /* clean up */ | 
| 243 | 
  | 
  | 
        if (fp == NULL) | 
| 244 | 
  | 
  | 
                fclose(inpf); | 
| 245 | 
  | 
  | 
        if (scanin != NULL) | 
| 246 | 
gwlarson | 
3.6 | 
                free((MEM_PTR)scanin); | 
| 247 | 
  | 
  | 
        if (err != TM_E_OK) { | 
| 248 | 
  | 
  | 
                if (*lpp != NULL) | 
| 249 | 
  | 
  | 
                        free((MEM_PTR)*lpp); | 
| 250 | 
  | 
  | 
                if (cpp != TM_NOCHROMP && *cpp != NULL) | 
| 251 | 
  | 
  | 
                        free((MEM_PTR)*cpp); | 
| 252 | 
greg | 
3.1 | 
                returnErr(err); | 
| 253 | 
gwlarson | 
3.6 | 
        } | 
| 254 | 
greg | 
3.1 | 
        returnOK; | 
| 255 | 
  | 
  | 
} | 
| 256 | 
  | 
  | 
 | 
| 257 | 
  | 
  | 
 | 
| 258 | 
gwlarson | 
3.9 | 
#ifdef PCOND | 
| 259 | 
schorsch | 
3.13 | 
static int                                      /* run pcond to map picture */ | 
| 260 | 
greg | 
3.1 | 
dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname) | 
| 261 | 
  | 
  | 
BYTE    **psp; | 
| 262 | 
  | 
  | 
int     *xp, *yp; | 
| 263 | 
  | 
  | 
int     flags; | 
| 264 | 
  | 
  | 
RGBPRIMP        monpri; | 
| 265 | 
  | 
  | 
double  gamval, Lddyn, Ldmax; | 
| 266 | 
  | 
  | 
char    *fname; | 
| 267 | 
  | 
  | 
{ | 
| 268 | 
  | 
  | 
        char    *funcName = fname; | 
| 269 | 
greg | 
3.17 | 
        TMstruct        *tms = NULL; | 
| 270 | 
greg | 
3.10 | 
        char    cmdbuf[1024]; | 
| 271 | 
greg | 
3.1 | 
        FILE    *infp; | 
| 272 | 
  | 
  | 
        register COLR   *scan; | 
| 273 | 
  | 
  | 
        register BYTE   *rp; | 
| 274 | 
  | 
  | 
        int     y; | 
| 275 | 
  | 
  | 
        register int    x; | 
| 276 | 
  | 
  | 
                                        /* set up gamma correction */ | 
| 277 | 
  | 
  | 
        if (setcolrcor(pow, 1./gamval) < 0) | 
| 278 | 
  | 
  | 
                returnErr(TM_E_NOMEM); | 
| 279 | 
  | 
  | 
                                        /* create command */ | 
| 280 | 
gwlarson | 
3.9 | 
        strcpy(cmdbuf, PCOND); | 
| 281 | 
greg | 
3.1 | 
        if (flags & TM_F_HCONTR) | 
| 282 | 
gwlarson | 
3.9 | 
                strcat(cmdbuf, " -s"); | 
| 283 | 
greg | 
3.1 | 
        if (flags & TM_F_MESOPIC) | 
| 284 | 
gwlarson | 
3.9 | 
                strcat(cmdbuf, " -c"); | 
| 285 | 
greg | 
3.1 | 
        if (flags & TM_F_LINEAR) | 
| 286 | 
gwlarson | 
3.9 | 
                strcat(cmdbuf, " -l"); | 
| 287 | 
greg | 
3.1 | 
        if (flags & TM_F_ACUITY) | 
| 288 | 
gwlarson | 
3.9 | 
                strcat(cmdbuf, " -a"); | 
| 289 | 
greg | 
3.1 | 
        if (flags & TM_F_VEIL) | 
| 290 | 
gwlarson | 
3.9 | 
                strcat(cmdbuf, " -v"); | 
| 291 | 
greg | 
3.1 | 
        if (flags & TM_F_CWEIGHT) | 
| 292 | 
gwlarson | 
3.9 | 
                strcat(cmdbuf, " -w"); | 
| 293 | 
  | 
  | 
        if (monpri != stdprims) | 
| 294 | 
  | 
  | 
                sprintf(cmdbuf+strlen(cmdbuf), " -p %f %f %f %f %f %f %f %f", | 
| 295 | 
  | 
  | 
                                monpri[RED][CIEX], monpri[RED][CIEY], | 
| 296 | 
  | 
  | 
                                monpri[GRN][CIEX], monpri[GRN][CIEY], | 
| 297 | 
  | 
  | 
                                monpri[BLU][CIEX], monpri[BLU][CIEY], | 
| 298 | 
  | 
  | 
                                monpri[WHT][CIEX], monpri[WHT][CIEY]); | 
| 299 | 
  | 
  | 
        sprintf(cmdbuf+strlen(cmdbuf), " -d %f -u %f %s", Lddyn, Ldmax, fname); | 
| 300 | 
greg | 
3.1 | 
                                        /* start pcond */ | 
| 301 | 
  | 
  | 
        if ((infp = popen(cmdbuf, "r")) == NULL) | 
| 302 | 
  | 
  | 
                returnErr(TM_E_BADFILE); | 
| 303 | 
  | 
  | 
                                        /* check picture format and size */ | 
| 304 | 
  | 
  | 
        if (checkheader(infp, COLRFMT, NULL) < 0 || | 
| 305 | 
  | 
  | 
                        fgetresolu(xp, yp, infp) < 0) { | 
| 306 | 
  | 
  | 
                pclose(infp); | 
| 307 | 
  | 
  | 
                returnErr(TM_E_BADFILE); | 
| 308 | 
  | 
  | 
        } | 
| 309 | 
  | 
  | 
                                        /* allocate arrays */ | 
| 310 | 
greg | 
3.2 | 
        scan = (COLR *)malloc(sizeof(COLR) * *xp); | 
| 311 | 
greg | 
3.1 | 
        if (flags & TM_F_BW) | 
| 312 | 
  | 
  | 
                rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); | 
| 313 | 
  | 
  | 
        else | 
| 314 | 
  | 
  | 
                rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp); | 
| 315 | 
schorsch | 
3.14 | 
        if (((*psp = rp) == NULL) | (scan == NULL)) { | 
| 316 | 
greg | 
3.1 | 
                pclose(infp); | 
| 317 | 
  | 
  | 
                returnErr(TM_E_NOMEM); | 
| 318 | 
  | 
  | 
        } | 
| 319 | 
  | 
  | 
                                        /* read and gamma map file */ | 
| 320 | 
  | 
  | 
        for (y = 0; y < *yp; y++) { | 
| 321 | 
  | 
  | 
                if (freadcolrs(scan, *xp, infp) < 0) { | 
| 322 | 
  | 
  | 
                        pclose(infp); | 
| 323 | 
gwlarson | 
3.6 | 
                        free((MEM_PTR)scan); | 
| 324 | 
  | 
  | 
                        free((MEM_PTR)*psp); | 
| 325 | 
greg | 
3.2 | 
                        *psp = NULL; | 
| 326 | 
greg | 
3.1 | 
                        returnErr(TM_E_BADFILE); | 
| 327 | 
  | 
  | 
                } | 
| 328 | 
  | 
  | 
                colrs_gambs(scan, *xp); | 
| 329 | 
  | 
  | 
                if (flags & TM_F_BW) | 
| 330 | 
  | 
  | 
                        for (x = 0; x < *xp; x++) | 
| 331 | 
  | 
  | 
                                *rp++ = normbright(scan[x]); | 
| 332 | 
  | 
  | 
                else | 
| 333 | 
  | 
  | 
                        for (x = 0; x < *xp; x++) { | 
| 334 | 
  | 
  | 
                                *rp++ = scan[x][RED]; | 
| 335 | 
  | 
  | 
                                *rp++ = scan[x][GRN]; | 
| 336 | 
  | 
  | 
                                *rp++ = scan[x][BLU]; | 
| 337 | 
  | 
  | 
                        } | 
| 338 | 
  | 
  | 
        } | 
| 339 | 
gwlarson | 
3.6 | 
        free((MEM_PTR)scan); | 
| 340 | 
greg | 
3.1 | 
        pclose(infp); | 
| 341 | 
  | 
  | 
        returnOK; | 
| 342 | 
  | 
  | 
} | 
| 343 | 
gwlarson | 
3.9 | 
#endif | 
| 344 | 
greg | 
3.1 | 
 | 
| 345 | 
  | 
  | 
 | 
| 346 | 
  | 
  | 
int                                     /* map a Radiance picture */ | 
| 347 | 
  | 
  | 
tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp) | 
| 348 | 
  | 
  | 
BYTE    **psp; | 
| 349 | 
  | 
  | 
int     *xp, *yp; | 
| 350 | 
  | 
  | 
int     flags; | 
| 351 | 
  | 
  | 
RGBPRIMP        monpri; | 
| 352 | 
  | 
  | 
double  gamval, Lddyn, Ldmax; | 
| 353 | 
  | 
  | 
char    *fname; | 
| 354 | 
  | 
  | 
FILE    *fp; | 
| 355 | 
  | 
  | 
{ | 
| 356 | 
  | 
  | 
        char    *funcName = fname==NULL ? "tmMapPicture" : fname; | 
| 357 | 
greg | 
3.17 | 
        TMstruct        *tms; | 
| 358 | 
greg | 
3.1 | 
        BYTE    *cp; | 
| 359 | 
  | 
  | 
        TMbright        *lp; | 
| 360 | 
  | 
  | 
        int     err; | 
| 361 | 
  | 
  | 
                                                /* check arguments */ | 
| 362 | 
schorsch | 
3.14 | 
        if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) | | 
| 363 | 
  | 
  | 
                        ((fname == NULL) & (fp == TM_GETFILE))) | 
| 364 | 
greg | 
3.1 | 
                returnErr(TM_E_ILLEGAL); | 
| 365 | 
  | 
  | 
                                                /* set defaults */ | 
| 366 | 
  | 
  | 
        if (gamval < MINGAM) gamval = DEFGAM; | 
| 367 | 
  | 
  | 
        if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN; | 
| 368 | 
  | 
  | 
        if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX; | 
| 369 | 
  | 
  | 
        if (flags & TM_F_BW) monpri = stdprims; | 
| 370 | 
gwlarson | 
3.9 | 
#ifdef PCOND | 
| 371 | 
greg | 
3.1 | 
                                                /* check for pcond run */ | 
| 372 | 
  | 
  | 
        if (fp == TM_GETFILE && flags & TM_F_UNIMPL) | 
| 373 | 
  | 
  | 
                return( dopcond(psp, xp, yp, flags, | 
| 374 | 
  | 
  | 
                                monpri, gamval, Lddyn, Ldmax, fname) ); | 
| 375 | 
gwlarson | 
3.9 | 
#endif | 
| 376 | 
greg | 
3.1 | 
                                                /* initialize tone mapping */ | 
| 377 | 
greg | 
3.17 | 
        if ((tms = tmInit(flags, monpri, gamval)) == NULL) | 
| 378 | 
greg | 
3.1 | 
                returnErr(TM_E_NOMEM); | 
| 379 | 
  | 
  | 
                                                /* load & convert picture */ | 
| 380 | 
greg | 
3.17 | 
        err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp, | 
| 381 | 
greg | 
3.1 | 
                        xp, yp, fname, fp); | 
| 382 | 
  | 
  | 
        if (err != TM_E_OK) { | 
| 383 | 
greg | 
3.17 | 
                tmDone(tms); | 
| 384 | 
greg | 
3.1 | 
                return(err); | 
| 385 | 
  | 
  | 
        } | 
| 386 | 
  | 
  | 
                                                /* allocate space for result */ | 
| 387 | 
  | 
  | 
        if (flags & TM_F_BW) { | 
| 388 | 
  | 
  | 
                *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp); | 
| 389 | 
greg | 
3.2 | 
                if (*psp == NULL) { | 
| 390 | 
gwlarson | 
3.6 | 
                        free((MEM_PTR)lp); | 
| 391 | 
greg | 
3.17 | 
                        tmDone(tms); | 
| 392 | 
greg | 
3.1 | 
                        returnErr(TM_E_NOMEM); | 
| 393 | 
greg | 
3.2 | 
                } | 
| 394 | 
greg | 
3.1 | 
                cp = TM_NOCHROM; | 
| 395 | 
  | 
  | 
        } else | 
| 396 | 
  | 
  | 
                *psp = cp; | 
| 397 | 
  | 
  | 
                                                /* compute color mapping */ | 
| 398 | 
greg | 
3.17 | 
        err = tmAddHisto(tms, lp, *xp * *yp, 1); | 
| 399 | 
greg | 
3.1 | 
        if (err != TM_E_OK) | 
| 400 | 
  | 
  | 
                goto done; | 
| 401 | 
greg | 
3.17 | 
        err = tmComputeMapping(tms, gamval, Lddyn, Ldmax); | 
| 402 | 
greg | 
3.1 | 
        if (err != TM_E_OK) | 
| 403 | 
  | 
  | 
                goto done; | 
| 404 | 
  | 
  | 
                                                /* map colors */ | 
| 405 | 
greg | 
3.17 | 
        err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp); | 
| 406 | 
greg | 
3.1 | 
 | 
| 407 | 
  | 
  | 
done:                                           /* clean up */ | 
| 408 | 
gwlarson | 
3.6 | 
        free((MEM_PTR)lp); | 
| 409 | 
greg | 
3.17 | 
        tmDone(tms); | 
| 410 | 
greg | 
3.1 | 
        if (err != TM_E_OK) {                   /* free memory on error */ | 
| 411 | 
gwlarson | 
3.6 | 
                free((MEM_PTR)*psp); | 
| 412 | 
greg | 
3.1 | 
                *psp = NULL; | 
| 413 | 
  | 
  | 
                returnErr(err); | 
| 414 | 
  | 
  | 
        } | 
| 415 | 
  | 
  | 
        returnOK; | 
| 416 | 
greg | 
3.3 | 
} | 
| 417 | 
  | 
  | 
 | 
| 418 | 
  | 
  | 
 | 
| 419 | 
  | 
  | 
static void | 
| 420 | 
  | 
  | 
colrNewSpace(tms)               /* color space changed for tone mapping */ | 
| 421 | 
greg | 
3.17 | 
register TMstruct       *tms; | 
| 422 | 
greg | 
3.3 | 
{ | 
| 423 | 
  | 
  | 
        register COLRDATA       *cd; | 
| 424 | 
  | 
  | 
        double  d; | 
| 425 | 
  | 
  | 
 | 
| 426 | 
  | 
  | 
        cd = (COLRDATA *)tms->pd[colrReg]; | 
| 427 | 
  | 
  | 
        cd->clfb[RED] = 256.*tms->clf[RED] + .5; | 
| 428 | 
  | 
  | 
        cd->clfb[GRN] = 256.*tms->clf[GRN] + .5; | 
| 429 | 
  | 
  | 
        cd->clfb[BLU] = 256.*tms->clf[BLU] + .5; | 
| 430 | 
  | 
  | 
        cd->clfb[EXP] = COLXS; | 
| 431 | 
  | 
  | 
        d = TM_BRTSCALE*log(tms->inpsf); | 
| 432 | 
  | 
  | 
        cd->inpsfb = d<0. ? d-.5 : d+.5; | 
| 433 | 
  | 
  | 
} | 
| 434 | 
  | 
  | 
 | 
| 435 | 
  | 
  | 
 | 
| 436 | 
  | 
  | 
static MEM_PTR | 
| 437 | 
  | 
  | 
colrInit(tms)                   /* initialize private data for tone mapping */ | 
| 438 | 
greg | 
3.17 | 
register TMstruct       *tms; | 
| 439 | 
greg | 
3.3 | 
{ | 
| 440 | 
  | 
  | 
        register COLRDATA       *cd; | 
| 441 | 
  | 
  | 
        register int    i; | 
| 442 | 
  | 
  | 
                                        /* allocate our data */ | 
| 443 | 
  | 
  | 
        cd = (COLRDATA *)malloc(sizeof(COLRDATA)); | 
| 444 | 
  | 
  | 
        if (cd == NULL) | 
| 445 | 
  | 
  | 
                return(NULL); | 
| 446 | 
  | 
  | 
        tms->pd[colrReg] = (MEM_PTR)cd; | 
| 447 | 
  | 
  | 
                                        /* compute gamma table */ | 
| 448 | 
  | 
  | 
        for (i = GAMTSZ; i--; ) | 
| 449 | 
  | 
  | 
                cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam); | 
| 450 | 
  | 
  | 
                                        /* compute color and scale factors */ | 
| 451 | 
  | 
  | 
        colrNewSpace(tms); | 
| 452 | 
  | 
  | 
        return((MEM_PTR)cd); | 
| 453 | 
greg | 
3.1 | 
} |