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.24 by greg, Thu May 18 01:58:18 2006 UTC vs.
Revision 3.37 by greg, Sat Jan 15 16:57:46 2022 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  
17 #include        "tmprivat.h"
18 #include        "resolu.h"
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  1024
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 38 | Line 37 | static struct tmPackage        colrPkg = {     /* our package fun
37   };
38   static int      colrReg = -1;           /* our package registration number */
39  
40 < #define LOGISZ  260
42 < 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          int     cmon[4];
54 <        register COLRDATA       *cd;
55 <        register int    i, j, li, bi;
56 <        int32   vl;
54 >        COLRDATA        *cd;
55 >        int     i, j, bi;
56 >        int32   li, vl;
57  
58          if (tms == NULL)
59                  returnErr(TM_E_TMINVAL);
# Line 67 | Line 65 | int    len
65                          returnErr(TM_E_CODERR1);
66                  for (i = 256; i--; )
67                          logi[i] = TM_BRTSCALE*log((i+.5)/256.) - .5;
70                for (i = 256; i < LOGISZ; i++)
71                        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                  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 */
93 <                li =    cd->clfb[RED]*cmon[RED] +
94 <                        cd->clfb[GRN]*cmon[GRN] +
95 <                        cd->clfb[BLU]*cmon[BLU] ;
96 <                if (li >= 0xfff00) li = 255;
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)
# Line 118 | 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 = ( (int32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12;
132 >                bi = ( (uint32)GAMTSZ*cd->clfb[RED]*cmon[RED]/li ) >> 12;
133                  cs[3*i  ] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
134 <                bi = ( (int32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12;
134 >                bi = ( (uint32)GAMTSZ*cd->clfb[GRN]*cmon[GRN]/li ) >> 12;
135                  cs[3*i+1] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
136 <                bi = ( (int32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12;
136 >                bi = ( (uint32)GAMTSZ*cd->clfb[BLU]*cmon[BLU]/li ) >> 12;
137                  cs[3*i+2] = bi>=GAMTSZ ? 255 : cd->gamb[bi];
138          }
139          returnOK;
# Line 149 | Line 155 | static struct radhead {
155  
156   static int
157   headline(                       /* grok a header line */
158 <        register char   *s,
158 >        char    *s,
159          void    *vrh
160   )
161   {
162 <        char    fmt[32];
163 <        register struct radhead *rh = vrh;
162 >        char    fmt[MAXFMTLEN];
163 >        struct radhead  *rh = vrh;
164  
165          if (formatval(fmt, s)) {
166                  if (!strcmp(fmt, COLRFMT))
# Line 182 | Line 188 | int
188   tmLoadPicture(                          /* convert Radiance picture */
189   TMstruct        *tms,
190   TMbright        **lpp,
191 < BYTE    **cpp,
191 > uby8    **cpp,
192   int     *xp,
193   int     *yp,
194   char    *fname,
# Line 226 | Line 232 | FILE   *fp
232          if (*lpp == NULL)
233                  goto done;
234          if (cpp != TM_NOCHROMP) {
235 <                *cpp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
235 >                *cpp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp);
236                  if (*cpp == NULL)
237                          goto done;
238          }
# Line 248 | Line 254 | done:                                          /* clean up */
254          if (fp == NULL)
255                  fclose(inpf);
256          if (scanin != NULL)
257 <                free((MEM_PTR)scanin);
257 >                free(scanin);
258          if (err != TM_E_OK) {
259                  if (*lpp != NULL)
260 <                        free((MEM_PTR)*lpp);
260 >                        free(*lpp);
261                  if (cpp != TM_NOCHROMP && *cpp != NULL)
262 <                        free((MEM_PTR)*cpp);
262 >                        free(*cpp);
263                  returnErr(err);
264          }
265          returnOK;
# Line 263 | Line 269 | done:                                          /* clean up */
269   #ifdef PCOND
270   static int                                      /* run pcond to map picture */
271   dopcond(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname)
272 < BYTE    **psp;
272 > uby8    **psp;
273   int     *xp, *yp;
274   int     flags;
275   RGBPRIMP        monpri;
# Line 274 | Line 280 | char   *fname;
280          TMstruct        *tms = NULL;
281          char    cmdbuf[1024];
282          FILE    *infp;
283 <        register COLR   *scan;
284 <        register BYTE   *rp;
283 >        COLR    *scan;
284 >        uby8    *rp;
285          int     y;
286 <        register int    x;
286 >        int     x;
287                                          /* set up gamma correction */
288          if (setcolrcor(pow, 1./gamval) < 0)
289                  returnErr(TM_E_NOMEM);
# Line 314 | Line 320 | char   *fname;
320                                          /* allocate arrays */
321          scan = (COLR *)malloc(sizeof(COLR) * *xp);
322          if (flags & TM_F_BW)
323 <                rp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
323 >                rp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp);
324          else
325 <                rp = (BYTE *)malloc(3*sizeof(BYTE) * *xp * *yp);
325 >                rp = (uby8 *)malloc(3*sizeof(uby8) * *xp * *yp);
326          if (((*psp = rp) == NULL) | (scan == NULL)) {
327                  pclose(infp);
328                  returnErr(TM_E_NOMEM);
# Line 325 | Line 331 | char   *fname;
331          for (y = 0; y < *yp; y++) {
332                  if (freadcolrs(scan, *xp, infp) < 0) {
333                          pclose(infp);
334 <                        free((MEM_PTR)scan);
335 <                        free((MEM_PTR)*psp);
334 >                        free(scan);
335 >                        free(*psp);
336                          *psp = NULL;
337                          returnErr(TM_E_BADFILE);
338                  }
# Line 341 | Line 347 | char   *fname;
347                                  *rp++ = scan[x][BLU];
348                          }
349          }
350 <        free((MEM_PTR)scan);
350 >        free(scan);
351          pclose(infp);
352          returnOK;
353   }
# Line 350 | Line 356 | char   *fname;
356  
357   int                                     /* map a Radiance picture */
358   tmMapPicture(psp, xp, yp, flags, monpri, gamval, Lddyn, Ldmax, fname, fp)
359 < BYTE    **psp;
359 > uby8    **psp;
360   int     *xp, *yp;
361   int     flags;
362   RGBPRIMP        monpri;
# Line 359 | Line 365 | char   *fname;
365   FILE    *fp;
366   {
367          char    *funcName = fname==NULL ? "tmMapPicture" : fname;
368 <        TMstruct        *tms;
369 <        BYTE    *cp;
368 >        TMstruct        *tms = NULL;
369 >        uby8    *cp;
370          TMbright        *lp;
371          int     err;
372                                                  /* check arguments */
# Line 390 | Line 396 | FILE   *fp;
396          }
397                                                  /* allocate space for result */
398          if (flags & TM_F_BW) {
399 <                *psp = (BYTE *)malloc(sizeof(BYTE) * *xp * *yp);
399 >                *psp = (uby8 *)malloc(sizeof(uby8) * *xp * *yp);
400                  if (*psp == NULL) {
401 <                        free((MEM_PTR)lp);
401 >                        free(lp);
402                          tmDone(tms);
403                          returnErr(TM_E_NOMEM);
404                  }
# Line 410 | Line 416 | FILE   *fp;
416          err = tmMapPixels(tms, *psp, lp, cp, *xp * *yp);
417  
418   done:                                           /* clean up */
419 <        free((MEM_PTR)lp);
419 >        free(lp);
420          tmDone(tms);
421          if (err != TM_E_OK) {                   /* free memory on error */
422 <                free((MEM_PTR)*psp);
422 >                free(*psp);
423                  *psp = NULL;
424                  returnErr(err);
425          }
# Line 423 | Line 429 | done:                                          /* clean up */
429  
430   static void
431   colrNewSpace(tms)               /* color space changed for tone mapping */
432 < register TMstruct       *tms;
432 > TMstruct        *tms;
433   {
434 <        register COLRDATA       *cd;
434 >        COLRDATA        *cd;
435          double  d;
436          int     i, j;
437  
438          cd = (COLRDATA *)tms->pd[colrReg];
439          for (i = 3; i--; )
440                  cd->clfb[i] = 0x1000*tms->clf[i] + .5;
435 fprintf(stderr, "(%d %d %d)\n", cd->clfb[0], cd->clfb[1], cd->clfb[2]);
441          cd->inpsfb = tmCvLuminance(tms->inpsf);
442          for (i = 3; i--; )
443                  for (j = 3; j--; ) {
# Line 442 | Line 447 | fprintf(stderr, "(%d %d %d)\n", cd->clfb[0], cd->clfb[
447   }
448  
449  
450 < static MEM_PTR
450 > static void *
451   colrInit(tms)                   /* initialize private data for tone mapping */
452 < register TMstruct       *tms;
452 > TMstruct        *tms;
453   {
454 <        register COLRDATA       *cd;
455 <        register int    i;
454 >        COLRDATA        *cd;
455 >        int     i;
456                                          /* allocate our data */
457          cd = (COLRDATA *)malloc(sizeof(COLRDATA));
458          if (cd == NULL)
459                  return(NULL);
460 <        tms->pd[colrReg] = (MEM_PTR)cd;
460 >        tms->pd[colrReg] = (void *)cd;
461                                          /* compute gamma table */
462          for (i = GAMTSZ; i--; )
463                  cd->gamb[i] = 256.*pow((i+.5)/GAMTSZ, 1./tms->mongam);
464                                          /* compute color and scale factors */
465          colrNewSpace(tms);
466 <        return((MEM_PTR)cd);
466 >        return((void *)cd);
467   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines