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.14 by schorsch, Sun Jul 27 22:12:01 2003 UTC vs.
Revision 3.21 by greg, Sat Sep 24 20:15:54 2005 UTC

# Line 13 | Line 13 | static const char      RCSid[] = "$Id$";
13   #include        <string.h>
14   #include        <math.h>
15   #include        <time.h>
16 +
17   #include        "tmprivat.h"
18   #include        "resolu.h"
19 <
20 < #ifndef TM_PIC_CTRANS
20 < #define TM_PIC_CTRANS   1               /* transform colors? (expensive) */
19 > #ifdef PCOND
20 > #include        "rtprocess.h"
21   #endif
22  
23   #define GAMTSZ  1024
24  
25   typedef struct {
26          BYTE            gamb[GAMTSZ];   /* gamma lookup table */
27 <        COLR            clfb;           /* encoded tm->clf */
27 >        int             clfb[3];        /* encoded tm->clf */
28 >        int32           cmatb[3][3];    /* encoded color transform */
29          TMbright        inpsfb;         /* encoded tm->inpsf */
30   } COLRDATA;
31  
32 < #ifdef NOPROTO
33 < static MEM_PTR  colrInit();
34 < static void     colrNewSpace();
35 < #else
35 < static MEM_PTR  colrInit(struct tmStruct *);
36 < static void     colrNewSpace(struct tmStruct *);
37 < #endif
32 > static MEM_PTR  colrInit(TMstruct *);
33 > static void     colrNewSpace(TMstruct *);
34 > static gethfunc headline;
35 >
36   static struct tmPackage colrPkg = {     /* our package functions */
37          colrInit, colrNewSpace, free
38   };
# Line 45 | Line 43 | static TMbright        logi[LOGISZ];
43  
44  
45   int
46 < tmCvColrs(ls, cs, scan, len)            /* convert RGBE/XYZE colors */
47 < TMbright        *ls;
48 < BYTE    *cs;
49 < COLR    *scan;
50 < int     len;
46 > tmCvColrs(                              /* convert RGBE/XYZE colors */
47 > TMstruct        *tms,
48 > TMbright        *ls,
49 > BYTE    *cs,
50 > COLR    *scan,
51 > int     len
52 > )
53   {
54 <        static char     funcName[] = "tmCvColrs";
55 <        COLR    cmon;
54 >        static const char       funcName[] = "tmCvColrs";
55 >        int     cmon[4];
56          register COLRDATA       *cd;
57 <        register int    i, bi, li;
57 >        register int    i, j, li, bi;
58 >        int32   vl;
59  
60 <        if (tmTop == NULL)
60 >        if (tms == NULL)
61                  returnErr(TM_E_TMINVAL);
62          if ((ls == NULL) | (scan == NULL) | (len < 0))
63                  returnErr(TM_E_ILLEGAL);
63 #if TM_PIC_CTRANS
64        if (tmNeedMatrix(tmTop)) {              /* need floating point */
65 #else
66        if (tmTop->inppri == TM_XYZPRIM) {      /* no way around this */
67 #endif
68                register COLOR  *newscan;
69                newscan = (COLOR *)tempbuffer(len*sizeof(COLOR));
70                if (newscan == NULL)
71                        returnErr(TM_E_NOMEM);
72                for (i = len; i--; )
73                        colr_color(newscan[i], scan[i]);
74                return(tmCvColors(ls, cs, newscan, len));
75        }
64          if (colrReg < 0) {                      /* build tables if necessary */
65                  colrReg = tmRegPkg(&colrPkg);
66                  if (colrReg < 0)
# Line 83 | Line 71 | int    len;
71                          logi[i] = 0;
72                  tmMkMesofact();
73          }
74 <        if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
74 >        if ((cd = (COLRDATA *)tmPkgData(tms,colrReg)) == NULL)
75                  returnErr(TM_E_NOMEM);
76          for (i = len; i--; ) {
77 <                copycolr(cmon, scan[i]);
77 >                if (tmNeedMatrix(tms)) {                /* apply color xform */
78 >                        for (j = 3; j--; ) {
79 >                                vl =    cd->cmatb[j][RED]*(int32)scan[i][RED] +
80 >                                        cd->cmatb[j][GRN]*(int32)scan[i][GRN] +
81 >                                        cd->cmatb[j][BLU]*(int32)scan[i][BLU] ;
82 >                                if (vl < 0) cmon[j] = vl/0x10000;
83 >                                else cmon[j] = vl>>16;
84 >                        }
85 >                        cmon[EXP] = scan[i][EXP];
86 >                } else
87 >                        copycolr(cmon, scan[i]);
88                                                          /* world luminance */
89 <                li =  ( cd->clfb[RED]*cmon[RED] +
89 >                li =    cd->clfb[RED]*cmon[RED] +
90                          cd->clfb[GRN]*cmon[GRN] +
91 <                        cd->clfb[BLU]*cmon[BLU] ) >> 8;
92 <                bi = BRT2SCALE(cmon[EXP]-COLXS) +
93 <                                logi[li] + cd->inpsfb;
91 >                        cd->clfb[BLU]*cmon[BLU] ;
92 >                if (li >= 0xff00) li = 255;
93 >                else li >>= 8;
94                  if (li <= 0) {
95                          bi = TM_NOBRT;                  /* bogus value */
96                          li = 1;                         /* avoid li==0 */
97 +                } else {
98 +                        bi = BRT2SCALE(cmon[EXP]-COLXS) +
99 +                                logi[li] + cd->inpsfb;
100                  }
101                  ls[i] = bi;
102                  if (cs == TM_NOCHROM)                   /* no color? */
103                          continue;
104                                                          /* mesopic adj. */
105 <                if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) {
106 <                        register int    pf, sli = normscot(cmon);
107 <                        if (bi < BMESLOWER)
105 >                if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) {
106 >                        int     pf, sli = normscot(cmon);
107 >                        if (bi < BMESLOWER) {
108                                  cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
109 <                        else {
110 <                                if (tmTop->flags & TM_F_BW)
109 >                        } else {
110 >                                if (tms->flags & TM_F_BW)
111                                          cmon[RED] = cmon[GRN] = cmon[BLU] = li;
112                                  pf = tmMesofact[bi-BMESLOWER];
113                                  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;
114 >                                for (j = 3; j--; ) {
115 >                                        cmon[j] = sli + pf*cmon[j];
116 >                                        if (cmon[j] <= 0) cmon[j] = 0;
117 >                                        else cmon[j] >>= 8;
118 >                                }
119                          }
120 <                } else if (tmTop->flags & TM_F_BW) {
120 >                } else if (tms->flags & TM_F_BW) {
121                          cmon[RED] = cmon[GRN] = cmon[BLU] = li;
122 +                } else {
123 +                        for (j = 3; j--; )
124 +                                if (cmon[j] < 0) cmon[j] = 0;
125                  }
126                  bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
127                  cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
# Line 142 | Line 148 | static struct radhead {
148  
149  
150   static int
151 < headline(s, rh)                 /* grok a header line */
152 < register char   *s;
153 < register struct radhead *rh;
151 > headline(                       /* grok a header line */
152 >        register char   *s,
153 >        void    *vrh
154 > )
155   {
156          char    fmt[32];
157 +        register struct radhead *rh = vrh;
158  
159          if (formatval(fmt, s)) {
160                  if (!strcmp(fmt, COLRFMT))
# Line 171 | Line 179 | register struct radhead        *rh;
179  
180  
181   int
182 < tmLoadPicture(lpp, cpp, xp, yp, fname, fp)      /* convert Radiance picture */
183 < TMbright        **lpp;
184 < BYTE    **cpp;
185 < int     *xp, *yp;
186 < char    *fname;
187 < FILE    *fp;
182 > tmLoadPicture(                          /* convert Radiance picture */
183 > TMstruct        *tms,
184 > TMbright        **lpp,
185 > BYTE    **cpp,
186 > int     *xp,
187 > int     *yp,
188 > char    *fname,
189 > FILE    *fp
190 > )
191   {
192          char    *funcName = fname==NULL ? "tmLoadPicture" : fname;
193          FILE    *inpf;
# Line 185 | Line 196 | FILE   *fp;
196          COLR    *scanin = NULL;
197          int     i;
198                                                  /* check arguments */
199 <        if (tmTop == NULL)
199 >        if (tms == NULL)
200                  returnErr(TM_E_TMINVAL);
201          if ((lpp == NULL) | (xp == NULL) | (yp == NULL) |
202                          ((fname == NULL) & (fp == TM_GETFILE)))
203                  returnErr(TM_E_ILLEGAL);
204          *xp = *yp = 0;                          /* error precaution */
205 <        if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
205 >        if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL)
206                  returnErr(TM_E_BADFILE);
207          *lpp = NULL;
208          if (cpp != TM_NOCHROMP) *cpp = NULL;
209          info = rhdefault;                       /* get our header */
210 <        getheader(inpf, headline, (char *)&info);
210 >        getheader(inpf, headline, &info);
211          if ((info.format == FMTBAD) | (info.expos <= 0.) ||
212                          fgetresolu(xp, yp, inpf) < 0) {
213                  err = TM_E_BADFILE; goto done;
# Line 208 | Line 219 | FILE   *fp;
219          else if (info.format == FMTCIE)
220                  info.primp = TM_XYZPRIM;
221                                                  /* prepare library */
222 <        if ((err = tmSetSpace(info.primp, 1./info.expos)) != TM_E_OK)
222 >        if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK)
223                  goto done;
224          err = TM_E_NOMEM;                       /* allocate arrays */
225          *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
# Line 227 | Line 238 | FILE   *fp;
238                  if (freadcolrs(scanin, *xp, inpf) < 0) {
239                          err = TM_E_BADFILE; break;
240                  }
241 <                err = tmCvColrs(*lpp + (i * *xp),
241 >                err = tmCvColrs(tms, *lpp + (i * *xp),
242                          cpp==TM_NOCHROMP ? TM_NOCHROM : *cpp + (i * 3 * *xp),
243                                  scanin, *xp);
244                  if (err != TM_E_OK)
# Line 260 | Line 271 | double gamval, Lddyn, Ldmax;
271   char    *fname;
272   {
273          char    *funcName = fname;
274 +        TMstruct        *tms = NULL;
275          char    cmdbuf[1024];
276          FILE    *infp;
277          register COLR   *scan;
# Line 347 | Line 359 | char   *fname;
359   FILE    *fp;
360   {
361          char    *funcName = fname==NULL ? "tmMapPicture" : fname;
362 +        TMstruct        *tms;
363          BYTE    *cp;
364          TMbright        *lp;
365          int     err;
# Line 366 | Line 379 | FILE   *fp;
379                                  monpri, gamval, Lddyn, Ldmax, fname) );
380   #endif
381                                                  /* initialize tone mapping */
382 <        if (tmInit(flags, monpri, gamval) == NULL)
382 >        if ((tms = tmInit(flags, monpri, gamval)) == NULL)
383                  returnErr(TM_E_NOMEM);
384                                                  /* load & convert picture */
385 <        err = tmLoadPicture(&lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
385 >        err = tmLoadPicture(tms, &lp, (flags&TM_F_BW) ? TM_NOCHROMP : &cp,
386                          xp, yp, fname, fp);
387          if (err != TM_E_OK) {
388 <                tmDone(NULL);
388 >                tmDone(tms);
389                  return(err);
390          }
391                                                  /* allocate space for result */
# Line 380 | Line 393 | FILE   *fp;
393                  *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
394                  if (*psp == NULL) {
395                          free((MEM_PTR)lp);
396 <                        tmDone(NULL);
396 >                        tmDone(tms);
397                          returnErr(TM_E_NOMEM);
398                  }
399                  cp = TM_NOCHROM;
400          } else
401                  *psp = cp;
402                                                  /* compute color mapping */
403 <        err = tmAddHisto(lp, *xp * *yp, 1);
403 >        err = tmAddHisto(tms, lp, *xp * *yp, 1);
404          if (err != TM_E_OK)
405                  goto done;
406 <        err = tmComputeMapping(gamval, Lddyn, Ldmax);
406 >        err = tmComputeMapping(tms, gamval, Lddyn, Ldmax);
407          if (err != TM_E_OK)
408                  goto done;
409                                                  /* map colors */
410 <        err = tmMapPixels(*psp, lp, cp, *xp * *yp);
410 >        err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp);
411  
412   done:                                           /* clean up */
413          free((MEM_PTR)lp);
414 <        tmDone(NULL);
414 >        tmDone(tms);
415          if (err != TM_E_OK) {                   /* free memory on error */
416                  free((MEM_PTR)*psp);
417                  *psp = NULL;
# Line 410 | Line 423 | done:                                          /* clean up */
423  
424   static void
425   colrNewSpace(tms)               /* color space changed for tone mapping */
426 < register struct tmStruct        *tms;
426 > register TMstruct       *tms;
427   {
428          register COLRDATA       *cd;
429          double  d;
430 +        int     i, j;
431  
432          cd = (COLRDATA *)tms->pd[colrReg];
433 <        cd->clfb[RED] = 256.*tms->clf[RED] + .5;
434 <        cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
421 <        cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
422 <        cd->clfb[EXP] = COLXS;
433 >        for (i = 3; i--; )
434 >                cd->clfb[i] = 0x100*tms->clf[i] + .5;
435          d = TM_BRTSCALE*log(tms->inpsf);
436          cd->inpsfb = d<0. ? d-.5 : d+.5;
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