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.9 by gwlarson, Wed Oct 28 09:26:03 1998 UTC vs.
Revision 3.26 by greg, Mon Feb 9 20:48:08 2009 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 + #include "copyright.h"
11 +
12   #include        <stdio.h>
13 + #include        <stdlib.h>
14 + #include        <string.h>
15   #include        <math.h>
16 + #include        <time.h>
17 +
18   #include        "tmprivat.h"
19   #include        "resolu.h"
20 + #ifdef PCOND
21 + #include        "rtprocess.h"
22 + #endif
23  
17
18 extern char     *tempbuffer();
19
24   #define GAMTSZ  1024
25  
26   typedef struct {
27          BYTE            gamb[GAMTSZ];   /* gamma lookup table */
28 <        COLR            clfb;           /* encoded tm->clf */
28 >        int             clfb[3];        /* encoded tm->clf */
29 >        int32           cmatb[3][3];    /* encoded color transform */
30          TMbright        inpsfb;         /* encoded tm->inpsf */
31   } COLRDATA;
32  
33 < static MEM_PTR  colrInit();
34 < static void     colrNewSpace();
35 < extern void     free();
33 > static MEM_PTR  colrInit(TMstruct *);
34 > static void     colrNewSpace(TMstruct *);
35 > static gethfunc headline;
36 >
37   static struct tmPackage colrPkg = {     /* our package functions */
38          colrInit, colrNewSpace, free
39   };
# Line 35 | Line 41 | static int     colrReg = -1;           /* our package registration
41  
42   #define LOGISZ  260
43   static TMbright logi[LOGISZ];
38 static BYTE     photofact[BMESUPPER-BMESLOWER];
44  
45  
46   int
47 < tmCvColrs(ls, cs, scan, len)            /* convert RGBE/XYZE colors */
48 < TMbright        *ls;
49 < BYTE    *cs;
50 < COLR    *scan;
51 < int     len;
47 > tmCvColrs(                              /* convert RGBE/XYZE colors */
48 > TMstruct        *tms,
49 > TMbright        *ls,
50 > BYTE    *cs,
51 > COLR    *scan,
52 > int     len
53 > )
54   {
55 <        static char     funcName[] = "tmCvColrs";
56 <        COLR    cmon;
55 >        static const char       funcName[] = "tmCvColrs";
56 >        int     cmon[4];
57          register COLRDATA       *cd;
58 <        register int    i, bi, li;
58 >        register int    i, j, li, bi;
59 >        int32   vl;
60  
61 <        if (tmTop == NULL)
61 >        if (tms == NULL)
62                  returnErr(TM_E_TMINVAL);
63 <        if (ls == NULL | scan == NULL | len < 0)
63 >        if ((ls == NULL) | (scan == NULL) | (len < 0))
64                  returnErr(TM_E_ILLEGAL);
57        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        }
65          if (colrReg < 0) {                      /* build tables if necessary */
66                  colrReg = tmRegPkg(&colrPkg);
67                  if (colrReg < 0)
# Line 70 | Line 69 | int    len;
69                  for (i = 256; i--; )
70                          logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5;
71                  for (i = 256; i < LOGISZ; i++)
72 <                        logi[i] = logi[255];
73 <                for (i = BMESLOWER; i < BMESUPPER; i++)
75 <                        photofact[i-BMESLOWER] = 256. *
76 <                                        (tmLuminance(i) - LMESLOWER) /
77 <                                        (LMESUPPER - LMESLOWER);
72 >                        logi[i] = 0;
73 >                tmMkMesofact();
74          }
75 <        if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
75 >        if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL)
76                  returnErr(TM_E_NOMEM);
77          for (i = len; i--; ) {
78 <                copycolr(cmon, scan[i]);
78 >                if (tmNeedMatrix(tms)) {                /* apply color xform */
79 >                        for (j = 3; j--; ) {
80 >                                vl =    cd->cmatb[j][RED]*(int32)scan[i][RED] +
81 >                                        cd->cmatb[j][GRN]*(int32)scan[i][GRN] +
82 >                                        cd->cmatb[j][BLU]*(int32)scan[i][BLU] ;
83 >                                if (vl < 0) cmon[j] = vl/0x10000;
84 >                                else cmon[j] = vl>>16;
85 >                        }
86 >                        cmon[EXP] = scan[i][EXP];
87 >                } else
88 >                        copycolr(cmon, scan[i]);
89                                                          /* world luminance */
90 <                li =  ( cd->clfb[RED]*cmon[RED] +
90 >                li =    cd->clfb[RED]*cmon[RED] +
91                          cd->clfb[GRN]*cmon[GRN] +
92 <                        cd->clfb[BLU]*cmon[BLU] ) >> 8;
93 <                bi = BRT2SCALE*(cmon[EXP]-COLXS) +
94 <                                logi[li] + cd->inpsfb;
95 <                if (bi < MINBRT) {
96 <                        bi = MINBRT-1;                  /* bogus value */
97 <                        li++;                           /* avoid li==0 */
92 >                        cd->clfb[BLU]*cmon[BLU] ;
93 >                if (li >= 0xfff00) li = 255;
94 >                else li >>= 12;
95 >                bi = BRT2SCALE(cmon[EXP]-COLXS) + cd->inpsfb;
96 >                if (li > 0)
97 >                        bi += logi[li];
98 >                else {
99 >                        bi += logi[0];
100 >                        li = 1;                         /* avoid /0 */
101                  }
102                  ls[i] = bi;
103                  if (cs == TM_NOCHROM)                   /* no color? */
104                          continue;
105                                                          /* mesopic adj. */
106 <                if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) {
107 <                        register int    pf, sli = normscot(cmon);
108 <                        if (bi < BMESLOWER)
106 >                if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) {
107 >                        int     pf, sli = normscot(cmon);
108 >                        if (bi < BMESLOWER) {
109                                  cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
110 <                        else {
111 <                                if (tmTop->flags & TM_F_BW)
110 >                        } else {
111 >                                if (tms->flags & TM_F_BW)
112                                          cmon[RED] = cmon[GRN] = cmon[BLU] = li;
113 <                                pf = photofact[bi-BMESLOWER];
113 >                                pf = tmMesofact[bi-BMESLOWER];
114                                  sli *= 256 - pf;
115 <                                cmon[RED] = ( sli + pf*cmon[RED] ) >> 8;
116 <                                cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8;
117 <                                cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8;
115 >                                for (j = 3; j--; ) {
116 >                                        cmon[j] = sli + pf*cmon[j];
117 >                                        if (cmon[j] <= 0) cmon[j] = 0;
118 >                                        else cmon[j] >>= 8;
119 >                                }
120                          }
121 <                } else if (tmTop->flags & TM_F_BW) {
121 >                } else if (tms->flags & TM_F_BW) {
122                          cmon[RED] = cmon[GRN] = cmon[BLU] = li;
123 +                } else {
124 +                        for (j = 3; j--; )
125 +                                if (cmon[j] < 0) cmon[j] = 0;
126                  }
127 <                bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
127 >                bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12;
128                  cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
129 <                bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8;
129 >                bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12;
130                  cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
131 <                bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8;
131 >                bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12;
132                  cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
133          }
134          returnOK;
# Line 135 | Line 149 | static struct radhead {
149  
150  
151   static int
152 < headline(s, rh)                 /* grok a header line */
153 < register char   *s;
154 < register struct radhead *rh;
152 > headline(                       /* grok a header line */
153 >        register char   *s,
154 >        void    *vrh
155 > )
156   {
157          char    fmt[32];
158 +        register struct radhead *rh = vrh;
159  
160          if (formatval(fmt, s)) {
161                  if (!strcmp(fmt, COLRFMT))
# Line 164 | Line 180 | register struct radhead        *rh;
180  
181  
182   int
183 < tmLoadPicture(lpp, cpp, xp, yp, fname, fp)      /* convert Radiance picture */
184 < TMbright        **lpp;
185 < BYTE    **cpp;
186 < int     *xp, *yp;
187 < char    *fname;
188 < FILE    *fp;
183 > tmLoadPicture(                          /* convert Radiance picture */
184 > TMstruct        *tms,
185 > TMbright        **lpp,
186 > BYTE    **cpp,
187 > int     *xp,
188 > int     *yp,
189 > char    *fname,
190 > FILE    *fp
191 > )
192   {
193          char    *funcName = fname==NULL ? "tmLoadPicture" : fname;
194          FILE    *inpf;
# Line 178 | Line 197 | FILE   *fp;
197          COLR    *scanin = NULL;
198          int     i;
199                                                  /* check arguments */
200 <        if (tmTop == NULL)
200 >        if (tms == NULL)
201                  returnErr(TM_E_TMINVAL);
202 <        if (lpp == NULL | xp == NULL | yp == NULL |
203 <                        (fname == NULL & fp == TM_GETFILE))
202 >        if ((lpp == NULL) | (xp == NULL) | (yp == NULL) |
203 >                        ((fname == NULL) & (fp == TM_GETFILE)))
204                  returnErr(TM_E_ILLEGAL);
205          *xp = *yp = 0;                          /* error precaution */
206 <        if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
206 >        if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL)
207                  returnErr(TM_E_BADFILE);
208          *lpp = NULL;
209          if (cpp != TM_NOCHROMP) *cpp = NULL;
210          info = rhdefault;                       /* get our header */
211 <        getheader(inpf, headline, (MEM_PTR)&info);
212 <        if (info.format == FMTBAD | info.expos <= 0. ||
211 >        getheader(inpf, headline, &info);
212 >        if ((info.format == FMTBAD) | (info.expos <= 0.) ||
213                          fgetresolu(xp, yp, inpf) < 0) {
214                  err = TM_E_BADFILE; goto done;
215          }
# Line 201 | Line 220 | FILE   *fp;
220          else if (info.format == FMTCIE)
221                  info.primp = TM_XYZPRIM;
222                                                  /* prepare library */
223 <        if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK)
223 >        if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK)
224                  goto done;
225          err = TM_E_NOMEM;                       /* allocate arrays */
226          *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
# Line 220 | Line 239 | FILE   *fp;
239                  if (freadcolrs(scanin, *xp, inpf) < 0) {
240                          err = TM_E_BADFILE; break;
241                  }
242 <                err = tmCvColrs(*lpp + (i * *xp),
242 >                err = tmCvColrs(tms, *lpp + (i * *xp),
243                          cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp),
244                                  scanin, *xp);
245                  if (err != TM_E_OK)
# Line 243 | Line 262 | done:                                          /* clean up */
262  
263  
264   #ifdef PCOND
265 < int                                     /* run pcond to map picture */
265 > static int                                      /* run pcond to map picture */
266   dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
267   BYTE    **psp;
268   int     *xp, *yp;
# Line 253 | Line 272 | double gamval, Lddyn, Ldmax;
272   char    *fname;
273   {
274          char    *funcName = fname;
275 <        char    cmdbuf[512];
275 >        TMstruct        *tms = NULL;
276 >        char    cmdbuf[1024];
277          FILE    *infp;
278          register COLR   *scan;
279          register BYTE   *rp;
# Line 298 | Line 318 | char   *fname;
318                  rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
319          else
320                  rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
321 <        if ((*psp = rp) == NULL | scan == NULL) {
321 >        if (((*psp = rp) == NULL) | (scan == NULL)) {
322                  pclose(infp);
323                  returnErr(TM_E_NOMEM);
324          }
# Line 340 | Line 360 | char   *fname;
360   FILE    *fp;
361   {
362          char    *funcName = fname==NULL ? "tmMapPicture" : fname;
363 +        TMstruct        *tms;
364          BYTE    *cp;
365          TMbright        *lp;
366          int     err;
367                                                  /* check arguments */
368 <        if (psp == NULL | xp == NULL | yp == NULL | monpri == NULL |
369 <                        (fname == NULL & fp == TM_GETFILE))
368 >        if ((psp == NULL) | (xp == NULL) | (yp == NULL) | (monpri == NULL) |
369 >                        ((fname == NULL) & (fp == TM_GETFILE)))
370                  returnErr(TM_E_ILLEGAL);
371                                                  /* set defaults */
372          if (gamval < MINGAM) gamval = DEFGAM;
# Line 359 | Line 380 | FILE   *fp;
380                                  monpri, gamval, Lddyn, Ldmax, fname) );
381   #endif
382                                                  /* initialize tone mapping */
383 <        if (tmInit(flags, monpri, gamval) == NULL)
383 >        if ((tms = tmInit(flags, monpri, gamval)) == NULL)
384                  returnErr(TM_E_NOMEM);
385                                                  /* load & convert picture */
386 <        err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
386 >        err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
387                          xp, yp, fname, fp);
388          if (err != TM_E_OK) {
389 <                tmDone(NULL);
389 >                tmDone(tms);
390                  return(err);
391          }
392                                                  /* allocate space for result */
# Line 373 | Line 394 | FILE   *fp;
394                  *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
395                  if (*psp == NULL) {
396                          free((MEM_PTR)lp);
397 <                        tmDone(NULL);
397 >                        tmDone(tms);
398                          returnErr(TM_E_NOMEM);
399                  }
400                  cp = TM_NOCHROM;
401          } else
402                  *psp = cp;
403                                                  /* compute color mapping */
404 <        err = tmAddHisto(lp, *xp * *yp, 1);
404 >        err = tmAddHisto(tms, lp, *xp * *yp, 1);
405          if (err != TM_E_OK)
406                  goto done;
407 <        err = tmComputeMapping(gamval, Lddyn, Ldmax);
407 >        err = tmComputeMapping(tms, gamval, Lddyn, Ldmax);
408          if (err != TM_E_OK)
409                  goto done;
410                                                  /* map colors */
411 <        err = tmMapPixels(*psp, lp, cp, *xp * *yp);
411 >        err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp);
412  
413   done:                                           /* clean up */
414          free((MEM_PTR)lp);
415 <        tmDone(NULL);
415 >        tmDone(tms);
416          if (err != TM_E_OK) {                   /* free memory on error */
417                  free((MEM_PTR)*psp);
418                  *psp = NULL;
# Line 403 | Line 424 | done:                                          /* clean up */
424  
425   static void
426   colrNewSpace(tms)               /* color space changed for tone mapping */
427 < register struct tmStruct        *tms;
427 > register TMstruct       *tms;
428   {
429          register COLRDATA       *cd;
430          double  d;
431 +        int     i, j;
432  
433          cd = (COLRDATA *)tms->pd[colrReg];
434 <        cd->clfb[RED] = 256.*tms->clf[RED] + .5;
435 <        cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
436 <        cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
437 <        cd->clfb[EXP] = COLXS;
438 <        d = TM_BRTSCALE*log(tms->inpsf);
439 <        cd->inpsfb = d<0. ? d-.5 : d+.5;
434 >        for (i = 3; i--; )
435 >                cd->clfb[i] = 0x1000*tms->clf[i] + .5;
436 >        cd->inpsfb = tmCvLuminance(tms->inpsf);
437 >        for (i = 3; i--; )
438 >                for (j = 3; j--; ) {
439 >                        d = tms->cmat[i][j] / tms->inpsf;
440 >                        cd->cmatb[i][j] = 0x10000*d + (d<0. ? -.5 : .5);
441 >                }
442   }
443  
444  
445   static MEM_PTR
446   colrInit(tms)                   /* initialize private data for tone mapping */
447 < register struct tmStruct        *tms;
447 > register TMstruct       *tms;
448   {
449          register COLRDATA       *cd;
450          register int    i;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines