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.19 by greg, Fri Jan 7 22:05:30 2005 UTC vs.
Revision 3.35 by greg, Tue Oct 13 00:08:46 2020 UTC

# Line 9 | Line 9 | static const char      RCSid[] = "$Id$";
9  
10   #include "copyright.h"
11  
12 < #include        <stdio.h>
13 < #include        <string.h>
12 > #include        <stdlib.h>
13   #include        <math.h>
15 #include        <time.h>
14  
15 + #ifdef PCOND
16 + #include        "paths.h"
17 + #endif
18   #include        "tmprivat.h"
19 + #include        "rtio.h"
20   #include        "resolu.h"
19 #include        "rtprocess.h"
21  
22 < #ifndef TM_PIC_CTRANS
22 < #define TM_PIC_CTRANS   1               /* transform colors? (expensive) */
23 < #endif
22 > #define GAMTSZ  4096
23  
25 #define GAMTSZ  1024
26
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  
# Line 39 | Line 37 | static struct tmPackage        colrPkg = {     /* our package fun
37   };
38   static int      colrReg = -1;           /* our package registration number */
39  
40 < #define LOGISZ  260
43 < static TMbright logi[LOGISZ];
40 > static TMbright logi[256];
41  
42  
43   int
44   tmCvColrs(                              /* convert RGBE/XYZE colors */
45   TMstruct        *tms,
46   TMbright        *ls,
47 < BYTE    *cs,
47 > uby8    *cs,
48   COLR    *scan,
49   int     len
50   )
51   {
52          static const char       funcName[] = "tmCvColrs";
53 <        COLR    cmon;
54 <        register COLRDATA       *cd;
55 <        register int    i, bi, li;
53 >        int     cmon[4];
54 >        COLRDATA        *cd;
55 >        int     i, j, bi;
56 >        int32   li, vl;
57  
58          if (tms == NULL)
59                  returnErr(TM_E_TMINVAL);
60          if ((ls == NULL) | (scan == NULL) | (len < 0))
61                  returnErr(TM_E_ILLEGAL);
64 #if TM_PIC_CTRANS
65        if (tmNeedMatrix(tms)) {                /* need floating point */
66 #else
67        if (tms->inppri == TM_XYZPRIM) {        /* no way around this */
68 #endif
69                register COLOR  *newscan;
70                newscan = (COLOR *)tempbuffer(len*sizeof(COLOR));
71                if (newscan == NULL)
72                        returnErr(TM_E_NOMEM);
73                for (i = len; i--; )
74                        colr_color(newscan[i], scan[i]);
75                return(tmCvColors(tms, ls, cs, newscan, len));
76        }
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;
83                for (i = 256; i < LOGISZ; i++)
84                        logi[i] = 0;
68                  tmMkMesofact();
69          }
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 (li <= 0) {
99 <                        bi = TM_NOBRT;                  /* bogus value */
100 <                        li = 1;                         /* 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;
108                                                          /* mesopic adj. */
109                  if (tms->flags & TM_F_MESOPIC && bi < BMESUPPER) {
110 <                        register int    pf, sli = normscot(cmon);
111 <                        if (bi < BMESLOWER)
110 >                        int     pf, sli = normscot(cmon);
111 >                        if (bi < BMESLOWER) {
112                                  cmon[RED] = cmon[GRN] = cmon[BLU] = sli;
113 <                        else {
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                          cmon[RED] = cmon[GRN] = cmon[BLU] = li;
126 +                } else {
127 +                        for (j = 3; j--; )
128 +                                cmon[j] *= (cmon[j] > 0);
129                  }
130 <                bi = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 8;
130 >                bi = ( (uint32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12;
131                  cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
132 <                bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 8;
132 >                bi = ( (uint32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12;
133                  cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
134 <                bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 8;
134 >                bi = ( (uint32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12;
135                  cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
136          }
137          returnOK;
# Line 144 | Line 153 | static struct radhead {
153  
154   static int
155   headline(                       /* grok a header line */
156 <        register char   *s,
156 >        char    *s,
157          void    *vrh
158   )
159   {
160 <        char    fmt[32];
161 <        register struct radhead *rh = vrh;
160 >        char    fmt[MAXFMTLEN];
161 >        struct radhead  *rh = vrh;
162  
163          if (formatval(fmt, s)) {
164                  if (!strcmp(fmt, COLRFMT))
# Line 177 | Line 186 | int
186   tmLoadPicture(                          /* convert Radiance picture */
187   TMstruct        *tms,
188   TMbright        **lpp,
189 < BYTE    **cpp,
189 > uby8    **cpp,
190   int     *xp,
191   int     *yp,
192   char    *fname,
# Line 197 | Line 206 | FILE   *fp
206                          ((fname == NULL) & (fp == TM_GETFILE)))
207                  returnErr(TM_E_ILLEGAL);
208          *xp = *yp = 0;                          /* error precaution */
209 <        if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "r")) == NULL)
209 >        if ((inpf = fp) == TM_GETFILE && (inpf = fopen(fname, "rb")) == NULL)
210                  returnErr(TM_E_BADFILE);
211          *lpp = NULL;
212          if (cpp != TM_NOCHROMP) *cpp = NULL;
# Line 221 | Line 230 | FILE   *fp
230          if (*lpp == NULL)
231                  goto done;
232          if (cpp != TM_NOCHROMP) {
233 <                *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
233 >                *cpp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp);
234                  if (*cpp == NULL)
235                          goto done;
236          }
# Line 258 | Line 267 | done:                                          /* clean up */
267   #ifdef PCOND
268   static int                                      /* run pcond to map picture */
269   dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
270 < BYTE    **psp;
270 > uby8    **psp;
271   int     *xp, *yp;
272   int     flags;
273   RGBPRIMP        monpri;
# Line 269 | Line 278 | char   *fname;
278          TMstruct        *tms = NULL;
279          char    cmdbuf[1024];
280          FILE    *infp;
281 <        register COLR   *scan;
282 <        register BYTE   *rp;
281 >        COLR    *scan;
282 >        uby8    *rp;
283          int     y;
284 <        register int    x;
284 >        int     x;
285                                          /* set up gamma correction */
286          if (setcolrcor(pow, 1./gamval) < 0)
287                  returnErr(TM_E_NOMEM);
# Line 309 | Line 318 | char   *fname;
318                                          /* allocate arrays */
319          scan = (COLR *)malloc(sizeof(COLR) * *xp);
320          if (flags & TM_F_BW)
321 <                rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
321 >                rp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp);
322          else
323 <                rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
323 >                rp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp);
324          if (((*psp = rp) == NULL) | (scan == NULL)) {
325                  pclose(infp);
326                  returnErr(TM_E_NOMEM);
# Line 345 | Line 354 | char   *fname;
354  
355   int                                     /* map a Radiance picture */
356   tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
357 < BYTE    **psp;
357 > uby8    **psp;
358   int     *xp, *yp;
359   int     flags;
360   RGBPRIMP        monpri;
# Line 354 | Line 363 | char   *fname;
363   FILE    *fp;
364   {
365          char    *funcName = fname==NULL ? "tmMapPicture" : fname;
366 <        TMstruct        *tms;
367 <        BYTE    *cp;
366 >        TMstruct        *tms = NULL;
367 >        uby8    *cp;
368          TMbright        *lp;
369          int     err;
370                                                  /* check arguments */
# Line 385 | Line 394 | FILE   *fp;
394          }
395                                                  /* allocate space for result */
396          if (flags & TM_F_BW) {
397 <                *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
397 >                *psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp);
398                  if (*psp == NULL) {
399                          free((MEM_PTR)lp);
400                          tmDone(tms);
# Line 418 | Line 427 | done:                                          /* clean up */
427  
428   static void
429   colrNewSpace(tms)               /* color space changed for tone mapping */
430 < register TMstruct       *tms;
430 > TMstruct        *tms;
431   {
432 <        register COLRDATA       *cd;
432 >        COLRDATA        *cd;
433          double  d;
434 +        int     i, j;
435  
436          cd = (COLRDATA *)tms->pd[colrReg];
437 <        cd->clfb[RED] = 256.*tms->clf[RED] + .5;
438 <        cd->clfb[GRN] = 256.*tms->clf[GRN] + .5;
439 <        cd->clfb[BLU] = 256.*tms->clf[BLU] + .5;
440 <        cd->clfb[EXP] = COLXS;
441 <        d = TM_BRTSCALE*log(tms->inpsf);
442 <        cd->inpsfb = d<0. ? d-.5 : d+.5;
437 >        for (i = 3; i--; )
438 >                cd->clfb[i] = 0x1000*tms->clf[i] + .5;
439 >        cd->inpsfb = tmCvLuminance(tms->inpsf);
440 >        for (i = 3; i--; )
441 >                for (j = 3; j--; ) {
442 >                        d = tms->cmat[i][j] / tms->inpsf;
443 >                        cd->cmatb[i][j] = 0x10000*d + (d<0. ? -.5 : .5);
444 >                }
445   }
446  
447  
448   static MEM_PTR
449   colrInit(tms)                   /* initialize private data for tone mapping */
450 < register TMstruct       *tms;
450 > TMstruct        *tms;
451   {
452 <        register COLRDATA       *cd;
453 <        register int    i;
452 >        COLRDATA        *cd;
453 >        int     i;
454                                          /* allocate our data */
455          cd = (COLRDATA *)malloc(sizeof(COLRDATA));
456          if (cd == NULL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines