ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/tmapcolrs.c
(Generate patch)

Comparing ray/src/common/tmapcolrs.c (file contents):
Revision 3.5 by gwlarson, Thu Oct 8 16:31:02 1998 UTC vs.
Revision 3.10 by greg, Sat Feb 22 02:07:22 2003 UTC

# Line 1 | Line 1
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 + /* ====================================================================
11 + * The Radiance Software License, Version 1.0
12 + *
13 + * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 + * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 + *
16 + * Redistribution and use in source and binary forms, with or without
17 + * modification, are permitted provided that the following conditions
18 + * are met:
19 + *
20 + * 1. Redistributions of source code must retain the above copyright
21 + *         notice, this list of conditions and the following disclaimer.
22 + *
23 + * 2. Redistributions in binary form must reproduce the above copyright
24 + *       notice, this list of conditions and the following disclaimer in
25 + *       the documentation and/or other materials provided with the
26 + *       distribution.
27 + *
28 + * 3. The end-user documentation included with the redistribution,
29 + *           if any, must include the following acknowledgment:
30 + *             "This product includes Radiance software
31 + *                 (http://radsite.lbl.gov/)
32 + *                 developed by the Lawrence Berkeley National Laboratory
33 + *               (http://www.lbl.gov/)."
34 + *       Alternately, this acknowledgment may appear in the software itself,
35 + *       if and wherever such third-party acknowledgments normally appear.
36 + *
37 + * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 + *       and "The Regents of the University of California" must
39 + *       not be used to endorse or promote products derived from this
40 + *       software without prior written permission. For written
41 + *       permission, please contact [email protected].
42 + *
43 + * 5. Products derived from this software may not be called "Radiance",
44 + *       nor may "Radiance" appear in their name, without prior written
45 + *       permission of Lawrence Berkeley National Laboratory.
46 + *
47 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 + * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 + * SUCH DAMAGE.
59 + * ====================================================================
60 + *
61 + * This software consists of voluntary contributions made by many
62 + * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 + * information on Lawrence Berkeley National Laboratory, please see
64 + * <http://www.lbl.gov/>.
65 + */
66 +
67   #include        <stdio.h>
68 + #include        <string.h>
69   #include        <math.h>
70 + #include        <time.h>
71   #include        "tmprivat.h"
72   #include        "resolu.h"
73  
74 + #ifndef TM_PIC_CTRANS
75 + #define TM_PIC_CTRANS   1               /* transform colors? (expensive) */
76 + #endif
77  
18 extern char     *tempbuffer();
19
78   #define GAMTSZ  1024
79  
80   typedef struct {
# Line 25 | Line 83 | typedef struct {
83          TMbright        inpsfb;         /* encoded tm->inpsf */
84   } COLRDATA;
85  
86 + #ifdef NOPROTO
87   static MEM_PTR  colrInit();
88   static void     colrNewSpace();
89 < extern void     free();
89 > #else
90 > static MEM_PTR  colrInit(struct tmStruct *);
91 > static void     colrNewSpace(struct tmStruct *);
92 > #endif
93   static struct tmPackage colrPkg = {     /* our package functions */
94          colrInit, colrNewSpace, free
95   };
# Line 35 | Line 97 | static int     colrReg = -1;           /* our package registration
97  
98   #define LOGISZ  260
99   static TMbright logi[LOGISZ];
38 static BYTE     photofact[BMESUPPER-BMESLOWER];
100  
101  
102   int
103 < tmCvColrs(ls, cs, scan, len)            /* tone map RGBE/XYZE colors */
103 > tmCvColrs(ls, cs, scan, len)            /* convert RGBE/XYZE colors */
104   TMbright        *ls;
105   BYTE    *cs;
106   COLR    *scan;
# Line 54 | Line 115 | int    len;
115                  returnErr(TM_E_TMINVAL);
116          if (ls == NULL | scan == NULL | len < 0)
117                  returnErr(TM_E_ILLEGAL);
118 + #if TM_PIC_CTRANS
119          if (tmNeedMatrix(tmTop)) {              /* need floating point */
120 + #else
121 +        if (tmTop->inppri == TM_XYZPRIM) {      /* no way around this */
122 + #endif
123                  register COLOR  *newscan;
124                  newscan = (COLOR *)tempbuffer(len*sizeof(COLOR));
125                  if (newscan == NULL)
# Line 63 | Line 128 | int    len;
128                          colr_color(newscan[i], scan[i]);
129                  return(tmCvColors(ls, cs, newscan, len));
130          }
131 <        if (colrReg == -1) {                    /* build tables if necessary */
131 >        if (colrReg < 0) {                      /* build tables if necessary */
132                  colrReg = tmRegPkg(&colrPkg);
133                  if (colrReg < 0)
134                          returnErr(TM_E_CODERR1);
135                  for (i = 256; i--; )
136                          logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5;
137                  for (i = 256; i < LOGISZ; i++)
138 <                        logi[i] = logi[255];
139 <                for (i = BMESLOWER; i < BMESUPPER; i++)
75 <                        photofact[i-BMESLOWER] = 256. *
76 <                                        (tmLuminance(i) - LMESLOWER) /
77 <                                        (LMESUPPER - LMESLOWER);
138 >                        logi[i] = 0;
139 >                tmMkMesofact();
140          }
141          if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
142                  returnErr(TM_E_NOMEM);
# Line 84 | Line 146 | int    len;
146                  li =  ( cd->clfb[RED]*cmon[RED] +
147                          cd->clfb[GRN]*cmon[GRN] +
148                          cd->clfb[BLU]*cmon[BLU] ) >> 8;
149 <                bi = BRT2SCALE*(cmon[EXP]-COLXS) +
149 >                bi = BRT2SCALE(cmon[EXP]-COLXS) +
150                                  logi[li] + cd->inpsfb;
151 <                if (bi < MINBRT) {
152 <                        bi = MINBRT-1;                  /* bogus value */
153 <                        li++;                           /* avoid li==0 */
151 >                if (li <= 0) {
152 >                        bi = TM_NOBRT;                  /* bogus value */
153 >                        li = 1;                         /* avoid li==0 */
154                  }
155                  ls[i] = bi;
156                  if (cs == TM_NOCHROM)                   /* no color? */
157                          continue;
96                if (tmTop->flags & TM_F_BW)
97                        cmon[RED] = cmon[GRN] = cmon[BLU] = li;
158                                                          /* mesopic adj. */
159                  if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) {
160                          register int    pf, sli = normscot(cmon);
161                          if (bi < BMESLOWER)
162                                  cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
163                          else {
164 <                                pf = photofact[bi-BMESLOWER];
164 >                                if (tmTop->flags & TM_F_BW)
165 >                                        cmon[RED] = cmon[GRN] = cmon[BLU] = li;
166 >                                pf = tmMesofact[bi-BMESLOWER];
167                                  sli *= 256 - pf;
168                                  cmon[RED] = ( sli + pf*cmon[RED] ) >> 8;
169                                  cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8;
170                                  cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8;
171                          }
172 +                } else if (tmTop->flags & TM_F_BW) {
173 +                        cmon[RED] = cmon[GRN] = cmon[BLU] = li;
174                  }
175                  bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
176                  cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
# Line 146 | Line 210 | register struct radhead        *rh;
210                          rh->format = FMTCIE;
211                  else
212                          rh->format = FMTBAD;
213 <                return;
213 >                return(0);
214          }
215          if (isexpos(s)) {
216                  rh->expos *= exposval(s);
217 <                return;
217 >                return(0);
218          }
219          if (isprims(s)) {
220                  primsval(rh->mypri, s);
221                  rh->primp = rh->mypri;
222 <                return;
222 >                return(0);
223          }
224 +        return(0);
225   }
226  
227  
# Line 183 | Line 248 | FILE   *fp;
248          *xp = *yp = 0;                          /* error precaution */
249          if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
250                  returnErr(TM_E_BADFILE);
251 +        *lpp = NULL;
252 +        if (cpp != TM_NOCHROMP) *cpp = NULL;
253          info = rhdefault;                       /* get our header */
254          getheader(inpf, headline, (char *)&info);
255          if (info.format == FMTBAD | info.expos <= 0. ||
# Line 225 | Line 292 | done:                                          /* clean up */
292          if (fp == NULL)
293                  fclose(inpf);
294          if (scanin != NULL)
295 <                free((char *)scanin);
296 <        if (err != TM_E_OK)
295 >                free((MEM_PTR)scanin);
296 >        if (err != TM_E_OK) {
297 >                if (*lpp != NULL)
298 >                        free((MEM_PTR)*lpp);
299 >                if (cpp != TM_NOCHROMP && *cpp != NULL)
300 >                        free((MEM_PTR)*cpp);
301                  returnErr(err);
302 +        }
303          returnOK;
304   }
305  
306  
307 + #ifdef PCOND
308   int                                     /* run pcond to map picture */
309   dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
310   BYTE    **psp;
# Line 242 | Line 315 | double gamval, Lddyn, Ldmax;
315   char    *fname;
316   {
317          char    *funcName = fname;
318 <        char    cmdbuf[512];
318 >        char    cmdbuf[1024];
319          FILE    *infp;
320          register COLR   *scan;
321          register BYTE   *rp;
# Line 252 | Line 325 | char   *fname;
325          if (setcolrcor(pow, 1./gamval) < 0)
326                  returnErr(TM_E_NOMEM);
327                                          /* create command */
328 <        strcpy(cmdbuf, "pcond ");
328 >        strcpy(cmdbuf, PCOND);
329          if (flags & TM_F_HCONTR)
330 <                strcat(cmdbuf, "-s ");
330 >                strcat(cmdbuf, " -s");
331          if (flags & TM_F_MESOPIC)
332 <                strcat(cmdbuf, "-c ");
332 >                strcat(cmdbuf, " -c");
333          if (flags & TM_F_LINEAR)
334 <                strcat(cmdbuf, "-l ");
334 >                strcat(cmdbuf, " -l");
335          if (flags & TM_F_ACUITY)
336 <                strcat(cmdbuf, "-a ");
336 >                strcat(cmdbuf, " -a");
337          if (flags & TM_F_VEIL)
338 <                strcat(cmdbuf, "-v ");
338 >                strcat(cmdbuf, " -v");
339          if (flags & TM_F_CWEIGHT)
340 <                strcat(cmdbuf, "-w ");
341 <        sprintf(cmdbuf+strlen(cmdbuf),
342 <                        "-p %f %f %f %f %f %f %f %f -d %f -u %f %s",
343 <                        monpri[RED][CIEX], monpri[RED][CIEY],
344 <                        monpri[GRN][CIEX], monpri[GRN][CIEY],
345 <                        monpri[BLU][CIEX], monpri[BLU][CIEY],
346 <                        monpri[WHT][CIEX], monpri[WHT][CIEY],
347 <                        Lddyn, Ldmax, fname);
340 >                strcat(cmdbuf, " -w");
341 >        if (monpri != stdprims)
342 >                sprintf(cmdbuf+strlen(cmdbuf), " -p %f %f %f %f %f %f %f %f",
343 >                                monpri[RED][CIEX], monpri[RED][CIEY],
344 >                                monpri[GRN][CIEX], monpri[GRN][CIEY],
345 >                                monpri[BLU][CIEX], monpri[BLU][CIEY],
346 >                                monpri[WHT][CIEX], monpri[WHT][CIEY]);
347 >        sprintf(cmdbuf+strlen(cmdbuf), " -d %f -u %f %s", Lddyn, Ldmax, fname);
348                                          /* start pcond */
349          if ((infp = popen(cmdbuf, "r")) == NULL)
350                  returnErr(TM_E_BADFILE);
# Line 295 | Line 368 | char   *fname;
368          for (y = 0; y < *yp; y++) {
369                  if (freadcolrs(scan, *xp, infp) < 0) {
370                          pclose(infp);
371 <                        free((char *)scan);
372 <                        free((char *)*psp);
371 >                        free((MEM_PTR)scan);
372 >                        free((MEM_PTR)*psp);
373                          *psp = NULL;
374                          returnErr(TM_E_BADFILE);
375                  }
# Line 311 | Line 384 | char   *fname;
384                                  *rp++ = scan[x][BLU];
385                          }
386          }
387 <        free((char *)scan);
387 >        free((MEM_PTR)scan);
388          pclose(infp);
389          returnOK;
390   }
391 + #endif
392  
393  
394   int                                     /* map a Radiance picture */
# Line 328 | Line 402 | char   *fname;
402   FILE    *fp;
403   {
404          char    *funcName = fname==NULL ? "tmMapPicture" : fname;
331        FILE    *inpf;
405          BYTE    *cp;
406          TMbright        *lp;
407          int     err;
# Line 341 | Line 414 | FILE   *fp;
414          if (Lddyn < MINLDDYN) Lddyn = DEFLDDYN;
415          if (Ldmax < MINLDMAX) Ldmax = DEFLDMAX;
416          if (flags & TM_F_BW) monpri = stdprims;
417 + #ifdef PCOND
418                                                  /* check for pcond run */
419          if (fp == TM_GETFILE && flags & TM_F_UNIMPL)
420                  return( dopcond(psp, xp, yp, flags,
421                                  monpri, gamval, Lddyn, Ldmax, fname) );
422 + #endif
423                                                  /* initialize tone mapping */
424          if (tmInit(flags, monpri, gamval) == NULL)
425                  returnErr(TM_E_NOMEM);
# Line 359 | Line 434 | FILE   *fp;
434          if (flags & TM_F_BW) {
435                  *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
436                  if (*psp == NULL) {
437 <                        free((char *)lp);
437 >                        free((MEM_PTR)lp);
438                          tmDone(NULL);
439                          returnErr(TM_E_NOMEM);
440                  }
# Line 377 | Line 452 | FILE   *fp;
452          err = tmMapPixels(*psp, lp, cp, *xp * *yp);
453  
454   done:                                           /* clean up */
455 <        free((char *)lp);
455 >        free((MEM_PTR)lp);
456          tmDone(NULL);
457          if (err != TM_E_OK) {                   /* free memory on error */
458 <                free((char *)*psp);
458 >                free((MEM_PTR)*psp);
459                  *psp = NULL;
460                  returnErr(err);
461          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines