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.28 by greg, Fri Apr 22 17:06:26 2011 UTC vs.
Revision 3.39 by greg, Thu Nov 21 17:15:54 2024 UTC

# Line 9 | Line 9 | static const char      RCSid[] = "$Id$";
9  
10   #include "copyright.h"
11  
12 #include        <stdio.h>
12   #include        <stdlib.h>
14 #include        <string.h>
13   #include        <math.h>
16 #include        <time.h>
14  
15   #ifdef PCOND
16 < #include        "rtprocess.h"
16 > #include        "paths.h"
17   #endif
18   #include        "tmprivat.h"
19 + #include        "rtio.h"
20   #include        "resolu.h"
21  
22   #define GAMTSZ  4096
23  
24   typedef struct {
25 <        BYTE            gamb[GAMTSZ];   /* gamma lookup table */
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  
31 < static MEM_PTR  colrInit(TMstruct *);
31 > static void *   colrInit(TMstruct *);
32   static void     colrNewSpace(TMstruct *);
33   static gethfunc headline;
34  
# Line 46 | Line 44 | 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   )
# Line 73 | Line 71 | int    len
71                  returnErr(TM_E_NOMEM);
72          for (i = len; 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) cmon[j] = vl/0x10000;
80 <                                else cmon[j] = vl>>16;
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 */
# Line 116 | Line 122 | int    len
122                                  }
123                          }
124                  } else if (tms->flags & TM_F_BW) {
125 <                        cmon[RED] = cmon[GRN] = cmon[BLU] = li;
125 >                        for (j = 3; j--; )
126 >                                cs[3*i+j] = tms->cdiv[j]/(TM_BRES>>8);
127 >                        continue;
128                  } else {
129                          for (j = 3; j--; )
130 <                                if (cmon[j] < 0) cmon[j] = 0;
130 >                                cmon[j] *= (cmon[j] > 0);
131                  }
132                  bi = ( (uint32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12;
133                  cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
# Line 134 | Line 142 | int    len
142  
143   #define FMTRGB          1       /* Input is RGBE */
144   #define FMTCIE          2       /* Input is CIE XYZE */
145 < #define FMTUNK          3       /* Input format is unspecified */
146 < #define FMTBAD          4       /* Input is not a recognized format */
145 > #define FMTSPEC         3       /* Input is N-component spectral data */
146 > #define FMTUNK          0       /* Input format is unspecified */
147 > #define FMTBAD          (-1)    /* Input is not a recognized format */
148  
149   static struct radhead {
150          int     format;         /* FMTRGB, FMTCIE, FMTUNK, FMTBAD */
151          double  expos;          /* input exposure value */
152          RGBPRIMP        primp;  /* input primaries */
153          RGBPRIMS        mypri;  /* custom primaries */
154 < } rhdefault = {FMTUNK, 1., stdprims, STDPRIMS};
154 >        int             ncs;    /* number of color samples */
155 >        float           wpt[4]; /* spectral partition */
156 > } rhdefault = {FMTUNK, 1., stdprims, STDPRIMS, 3, {0,0,0,0}};
157  
158  
159   static int
# Line 151 | Line 162 | headline(                      /* grok a header line */
162          void    *vrh
163   )
164   {
165 <        char    fmt[32];
165 >        char    fmt[MAXFMTLEN];
166          struct radhead  *rh = vrh;
167  
168          if (formatval(fmt, s)) {
# Line 159 | Line 170 | headline(                      /* grok a header line */
170                          rh->format = FMTRGB;
171                  else if (!strcmp(fmt, CIEFMT))
172                          rh->format = FMTCIE;
173 +                else if (!strcmp(fmt, SPECFMT))
174 +                        rh->format = FMTSPEC;
175                  else
176                          rh->format = FMTBAD;
177                  return(0);
# Line 172 | Line 185 | headline(                      /* grok a header line */
185                  rh->primp = rh->mypri;
186                  return(0);
187          }
188 +        if (isncomp(s)) {
189 +                rh->ncs = ncompval(s);
190 +                return(0);
191 +        }
192 +        if (iswlsplit(s)) {
193 +                wlsplitval(rh->wpt, s);
194 +                return(0);
195 +        }
196          return(0);
197   }
198  
# Line 180 | Line 201 | int
201   tmLoadPicture(                          /* convert Radiance picture */
202   TMstruct        *tms,
203   TMbright        **lpp,
204 < BYTE    **cpp,
204 > uby8    **cpp,
205   int     *xp,
206   int     *yp,
207   char    *fname,
# Line 210 | Line 231 | FILE   *fp
231                          fgetresolu(xp, yp, inpf) < 0) {
232                  err = TM_E_BADFILE; goto done;
233          }
234 <        if (info.format == FMTUNK)              /* assume RGBE format */
234 >        if (info.format == FMTSPEC) {           /* valid spectrum? */
235 >                if (info.ncs <= 3) {
236 >                        err = TM_E_BADFILE; goto done;
237 >                }
238 >                if (info.wpt[0] == 0)
239 >                        memcpy(info.wpt, WLPART, sizeof(info.wpt));
240 >        } else if (info.format == FMTUNK)       /* assume RGBE format? */
241                  info.format = FMTRGB;
242 <        if (info.format == FMTRGB)
243 <                info.expos /= WHTEFFICACY;
217 <        else if (info.format == FMTCIE)
242 >
243 >        if (info.format == FMTCIE)
244                  info.primp = TM_XYZPRIM;
245 +        else
246 +                info.expos /= WHTEFFICACY;
247                                                  /* prepare library */
248 <        if ((err = tmSetSpace(tms, info.primp, 1./info.expos, NULL)) != TM_E_OK)
248 >        if ((err = tmSetSpace(tms, info.primp, 1./info.expos)) != TM_E_OK)
249                  goto done;
250          err = TM_E_NOMEM;                       /* allocate arrays */
251          *lpp = (TMbright *)malloc(sizeof(TMbright) * *xp * *yp);
252          if (*lpp == NULL)
253                  goto done;
254          if (cpp != TM_NOCHROMP) {
255 <                *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
255 >                *cpp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp);
256                  if (*cpp == NULL)
257                          goto done;
258          }
# Line 233 | Line 261 | FILE   *fp
261                  goto done;
262          err = TM_E_BADFILE;                     /* read & convert scanlines */
263          for (i = 0; i < *yp; i++) {
264 <                if (freadcolrs(scanin, *xp, inpf) < 0) {
264 >                if (fread2colrs(scanin, *xp, inpf, info.ncs, info.wpt) < 0) {
265                          err = TM_E_BADFILE; break;
266                  }
267                  err = tmCvColrs(tms, *lpp + (i * *xp),
# Line 246 | Line 274 | done:                                          /* clean up */
274          if (fp == NULL)
275                  fclose(inpf);
276          if (scanin != NULL)
277 <                free((MEM_PTR)scanin);
277 >                free(scanin);
278          if (err != TM_E_OK) {
279                  if (*lpp != NULL)
280 <                        free((MEM_PTR)*lpp);
280 >                        free(*lpp);
281                  if (cpp != TM_NOCHROMP && *cpp != NULL)
282 <                        free((MEM_PTR)*cpp);
282 >                        free(*cpp);
283                  returnErr(err);
284          }
285          returnOK;
# Line 261 | Line 289 | done:                                          /* clean up */
289   #ifdef PCOND
290   static int                                      /* run pcond to map picture */
291   dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
292 < BYTE    **psp;
292 > uby8    **psp;
293   int     *xp, *yp;
294   int     flags;
295   RGBPRIMP        monpri;
# Line 273 | Line 301 | char   *fname;
301          char    cmdbuf[1024];
302          FILE    *infp;
303          COLR    *scan;
304 <        BYTE    *rp;
304 >        uby8    *rp;
305          int     y;
306          int     x;
307                                          /* set up gamma correction */
# Line 312 | Line 340 | char   *fname;
340                                          /* allocate arrays */
341          scan = (COLR *)malloc(sizeof(COLR) * *xp);
342          if (flags & TM_F_BW)
343 <                rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
343 >                rp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp);
344          else
345 <                rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
345 >                rp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp);
346          if (((*psp = rp) == NULL) | (scan == NULL)) {
347                  pclose(infp);
348                  returnErr(TM_E_NOMEM);
# Line 323 | Line 351 | char   *fname;
351          for (y = 0; y < *yp; y++) {
352                  if (freadcolrs(scan, *xp, infp) < 0) {
353                          pclose(infp);
354 <                        free((MEM_PTR)scan);
355 <                        free((MEM_PTR)*psp);
354 >                        free(scan);
355 >                        free(*psp);
356                          *psp = NULL;
357                          returnErr(TM_E_BADFILE);
358                  }
# Line 339 | Line 367 | char   *fname;
367                                  *rp++ = scan[x][BLU];
368                          }
369          }
370 <        free((MEM_PTR)scan);
370 >        free(scan);
371          pclose(infp);
372          returnOK;
373   }
# Line 348 | Line 376 | char   *fname;
376  
377   int                                     /* map a Radiance picture */
378   tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
379 < BYTE    **psp;
379 > uby8    **psp;
380   int     *xp, *yp;
381   int     flags;
382   RGBPRIMP        monpri;
# Line 357 | Line 385 | char   *fname;
385   FILE    *fp;
386   {
387          char    *funcName = fname==NULL ? "tmMapPicture" : fname;
388 <        TMstruct        *tms;
389 <        BYTE    *cp;
388 >        TMstruct        *tms = NULL;
389 >        uby8    *cp;
390          TMbright        *lp;
391          int     err;
392                                                  /* check arguments */
# Line 388 | Line 416 | FILE   *fp;
416          }
417                                                  /* allocate space for result */
418          if (flags & TM_F_BW) {
419 <                *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
419 >                *psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp);
420                  if (*psp == NULL) {
421 <                        free((MEM_PTR)lp);
421 >                        free(lp);
422                          tmDone(tms);
423                          returnErr(TM_E_NOMEM);
424                  }
# Line 408 | Line 436 | FILE   *fp;
436          err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp);
437  
438   done:                                           /* clean up */
439 <        free((MEM_PTR)lp);
439 >        free(lp);
440          tmDone(tms);
441          if (err != TM_E_OK) {                   /* free memory on error */
442 <                free((MEM_PTR)*psp);
442 >                free(*psp);
443                  *psp = NULL;
444                  returnErr(err);
445          }
# Line 439 | Line 467 | TMstruct       *tms;
467   }
468  
469  
470 < static MEM_PTR
470 > static void *
471   colrInit(tms)                   /* initialize private data for tone mapping */
472   TMstruct        *tms;
473   {
# Line 449 | Line 477 | TMstruct       *tms;
477          cd = (COLRDATA *)malloc(sizeof(COLRDATA));
478          if (cd == NULL)
479                  return(NULL);
480 <        tms->pd[colrReg] = (MEM_PTR)cd;
480 >        tms->pd[colrReg] = (void *)cd;
481                                          /* compute gamma table */
482          for (i = GAMTSZ; i--; )
483                  cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
484                                          /* compute color and scale factors */
485          colrNewSpace(tms);
486 <        return((MEM_PTR)cd);
486 >        return((void *)cd);
487   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines