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.2 by greg, Wed Apr 16 20:28:05 1997 UTC vs.
Revision 3.5 by gwlarson, Thu Oct 8 16:31:02 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Regents of the University of California */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   extern char     *tempbuffer();
19  
20 + #define GAMTSZ  1024
21 +
22 + typedef struct {
23 +        BYTE            gamb[GAMTSZ];   /* gamma lookup table */
24 +        COLR            clfb;           /* encoded tm->clf */
25 +        TMbright        inpsfb;         /* encoded tm->inpsf */
26 + } COLRDATA;
27 +
28 + static MEM_PTR  colrInit();
29 + static void     colrNewSpace();
30 + extern void     free();
31 + static struct tmPackage colrPkg = {     /* our package functions */
32 +        colrInit, colrNewSpace, free
33 + };
34 + static int      colrReg = -1;           /* our package registration number */
35 +
36   #define LOGISZ  260
37   static TMbright logi[LOGISZ];
38   static BYTE     photofact[BMESUPPER-BMESLOWER];
# Line 31 | Line 47 | int    len;
47   {
48          static char     funcName[] = "tmCvColrs";
49          COLR    cmon;
50 +        register COLRDATA       *cd;
51          register int    i, bi, li;
52  
53          if (tmTop == NULL)
54                  returnErr(TM_E_TMINVAL);
55 <        if (ls == NULL | scan == NULL | len <= 0)
55 >        if (ls == NULL | scan == NULL | len < 0)
56                  returnErr(TM_E_ILLEGAL);
57 <        if (tmTop->flags & TM_F_NEEDMAT) {      /* need floating point */
57 >        if (tmNeedMatrix(tmTop)) {              /* need floating point */
58                  register COLOR  *newscan;
59                  newscan = (COLOR *)tempbuffer(len*sizeof(COLOR));
60                  if (newscan == NULL)
# Line 46 | Line 63 | int    len;
63                          colr_color(newscan[i], scan[i]);
64                  return(tmCvColors(ls, cs, newscan, len));
65          }
66 <        if (logi[0] == 0) {                     /* build tables if necessary */
66 >        if (colrReg == -1) {                    /* build tables if necessary */
67 >                colrReg = tmRegPkg(&colrPkg);
68 >                if (colrReg < 0)
69 >                        returnErr(TM_E_CODERR1);
70                  for (i = 256; i--; )
71                          logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5;
72                  for (i = 256; i < LOGISZ; i++)
# Line 56 | Line 76 | int    len;
76                                          (tmLuminance(i) - LMESLOWER) /
77                                          (LMESUPPER - LMESLOWER);
78          }
79 +        if ((cd = (COLRDATA *)tmPkgData(tmTop,colrReg)) == NULL)
80 +                returnErr(TM_E_NOMEM);
81          for (i = len; i--; ) {
82                  copycolr(cmon, scan[i]);
83                                                          /* world luminance */
84 <                li =  ( tmTop->clfb[RED]*cmon[RED] +
85 <                        tmTop->clfb[GRN]*cmon[GRN] +
86 <                        tmTop->clfb[BLU]*cmon[BLU] ) >> 8;
84 >                li =  ( cd->clfb[RED]*cmon[RED] +
85 >                        cd->clfb[GRN]*cmon[GRN] +
86 >                        cd->clfb[BLU]*cmon[BLU] ) >> 8;
87                  bi = BRT2SCALE*(cmon[EXP]-COLXS) +
88 <                                logi[li] + tmTop->inpsfb;
88 >                                logi[li] + cd->inpsfb;
89                  if (bi < MINBRT) {
90                          bi = MINBRT-1;                  /* bogus value */
91                          li++;                           /* avoid li==0 */
# Line 71 | Line 93 | int    len;
93                  ls[i] = bi;
94                  if (cs == TM_NOCHROM)                   /* no color? */
95                          continue;
96 +                if (tmTop->flags & TM_F_BW)
97 +                        cmon[RED] = cmon[GRN] = cmon[BLU] = li;
98                                                          /* mesopic adj. */
99                  if (tmTop->flags & TM_F_MESOPIC && bi < BMESUPPER) {
100                          register int    pf, sli = normscot(cmon);
101                          if (bi < BMESLOWER)
102                                  cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
103                          else {
80                                if (tmTop->flags & TM_F_BW)
81                                        cmon[RED] = cmon[GRN] = cmon[BLU] = li;
104                                  pf = photofact[bi-BMESLOWER];
105                                  sli *= 256 - pf;
106                                  cmon[RED] = ( sli + pf*cmon[RED] ) >> 8;
107                                  cmon[GRN] = ( sli + pf*cmon[GRN] ) >> 8;
108                                  cmon[BLU] = ( sli + pf*cmon[BLU] ) >> 8;
109                          }
88                } else if (tmTop->flags & TM_F_BW) {
89                        cmon[RED] = cmon[GRN] = cmon[BLU] = li;
110                  }
111 <                bi = ( (int4)TM_GAMTSZ*tmTop->clfb[RED]*cmon[RED]/li ) >> 8;
112 <                cs[3*i  ] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi];
113 <                bi = ( (int4)TM_GAMTSZ*tmTop->clfb[GRN]*cmon[GRN]/li ) >> 8;
114 <                cs[3*i+1] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi];
115 <                bi = ( (int4)TM_GAMTSZ*tmTop->clfb[BLU]*cmon[BLU]/li ) >> 8;
116 <                cs[3*i+2] = bi>=TM_GAMTSZ ? 255 : tmTop->gamb[bi];
111 >                bi = ( (int4)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
112 >                cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
113 >                bi = ( (int4)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8;
114 >                cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
115 >                bi = ( (int4)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8;
116 >                cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
117          }
118          returnOK;
119   }
# Line 365 | Line 385 | done:                                          /* clean up */
385                  returnErr(err);
386          }
387          returnOK;
388 + }
389 +
390 +
391 + static void
392 + colrNewSpace(tms)               /* color space changed for tone mapping */
393 + register struct tmStruct        *tms;
394 + {
395 +        register COLRDATA       *cd;
396 +        double  d;
397 +
398 +        cd = (COLRDATA *)tms->pd[colrReg];
399 +        cd->clfb[RED] = 256.*tms->clf[RED] + .5;
400 +        cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
401 +        cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
402 +        cd->clfb[EXP] = COLXS;
403 +        d = TM_BRTSCALE*log(tms->inpsf);
404 +        cd->inpsfb = d<0. ? d-.5 : d+.5;
405 + }
406 +
407 +
408 + static MEM_PTR
409 + colrInit(tms)                   /* initialize private data for tone mapping */
410 + register struct tmStruct        *tms;
411 + {
412 +        register COLRDATA       *cd;
413 +        register int    i;
414 +                                        /* allocate our data */
415 +        cd = (COLRDATA *)malloc(sizeof(COLRDATA));
416 +        if (cd == NULL)
417 +                return(NULL);
418 +        tms->pd[colrReg] = (MEM_PTR)cd;
419 +                                        /* compute gamma table */
420 +        for (i = GAMTSZ; i--; )
421 +                cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
422 +                                        /* compute color and scale factors */
423 +        colrNewSpace(tms);
424 +        return((MEM_PTR)cd);
425   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines