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