ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/cmatrix.c
(Generate patch)

Comparing ray/src/util/cmatrix.c (file contents):
Revision 2.23 by greg, Wed Oct 23 17:00:14 2019 UTC vs.
Revision 2.32 by greg, Tue Jan 19 23:32:00 2021 UTC

# Line 15 | Line 15 | static const char RCSid[] = "$Id$";
15   #include "paths.h"
16   #include "resolu.h"
17  
18 + const char      stdin_name[] = "<stdin>";
19 +
20   const char      *cm_fmt_id[] = {
21 <                        "unknown", "ascii", COLRFMT, CIEFMT,
22 <                        "float", "double"
21 >                        "unknown", COLRFMT, CIEFMT,
22 >                        "float", "ascii", "double"
23                  };
24  
25   const int       cm_elem_size[] = {
26 <                        0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double)
26 >                        0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double)
27                  };
28  
29   /* Allocate a color coefficient matrix */
# Line 33 | Line 35 | cm_alloc(int nrows, int ncols)
35          if ((nrows <= 0) | (ncols <= 0))
36                  error(USER, "attempt to create empty matrix");
37          cm = (CMATRIX *)malloc(sizeof(CMATRIX) +
38 <                                sizeof(COLOR)*(nrows*ncols - 1));
38 >                                sizeof(COLOR)*((size_t)nrows*ncols - 1));
39          if (!cm)
40                  error(SYSTEM, "out of memory in cm_alloc()");
41          cm->nrows = nrows;
# Line 65 | Line 67 | cm_resize(CMATRIX *cm, int nrows)
67                  cm_free(cm);
68                  return(NULL);
69          }
70 <        old_size = sizeof(CMATRIX) + sizeof(COLOR)*(cm->nrows*cm->ncols - 1);
70 >        old_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)cm->nrows*cm->ncols - 1);
71          adjacent_ra_sizes(ra_bounds, old_size);
72 <        new_size = sizeof(CMATRIX) + sizeof(COLOR)*(nrows*cm->ncols - 1);
72 >        new_size = sizeof(CMATRIX) + sizeof(COLOR)*((size_t)nrows*cm->ncols - 1);
73          if (nrows < cm->nrows ? new_size <= ra_bounds[0] :
74                                  new_size > ra_bounds[1]) {
75                  adjacent_ra_sizes(ra_bounds, new_size);
# Line 83 | Line 85 | typedef struct {
85          int     dtype;          /* data type */
86          int     need2swap;      /* need byte swap? */
87          int     nrows, ncols;   /* matrix size */
88 +        COLOR   expos;          /* exposure value */
89          char    *err;           /* error message */
90   } CMINFO;               /* header info record */
91  
# Line 109 | Line 112 | get_cminfo(char *s, void *p)
112                  ip->need2swap = (nativebigendian() != i);
113                  return(0);
114          }
115 +        if (isexpos(s)) {
116 +                double  d = exposval(s);
117 +                scalecolor(ip->expos, d);
118 +                return(0);
119 +        }
120 +        if (iscolcor(s)) {
121 +                COLOR   ctmp;
122 +                colcorval(ctmp, s);
123 +                multcolor(ip->expos, ctmp);
124 +                return(0);
125 +        }
126          if (!formatval(fmt, s))
127                  return(0);
128          for (i = 1; i < DTend; i++)
# Line 119 | Line 133 | get_cminfo(char *s, void *p)
133  
134   /* Load header to obtain/check data type and number of columns */
135   char *
136 < cm_getheader(int *dt, int *nr, int *nc, int *swp, FILE *fp)
136 > cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp)
137   {
138          CMINFO  cmi;
139                                                  /* read header */
140          cmi.dtype = DTfromHeader;
141          cmi.need2swap = 0;
142 +        cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f;
143          cmi.nrows = cmi.ncols = 0;
144          cmi.err = "unexpected EOF in header";
145          if (getheader(fp, get_cminfo, &cmi) < 0)
# Line 152 | Line 167 | cm_getheader(int *dt, int *nr, int *nc, int *swp, FILE
167          }
168          if (swp)                                /* get/check swap? */
169                  *swp = cmi.need2swap;
170 +        if (scale) {                            /* transfer exposure comp. */
171 +                scale[0] = 1.f/cmi.expos[0];
172 +                scale[1] = 1.f/cmi.expos[1];
173 +                scale[2] = 1.f/cmi.expos[2];
174 +        }
175          return(NULL);
176   }
177  
178   /* Allocate and load image data into matrix */
179   static CMATRIX *
180 < cm_load_rgbe(FILE *fp, int nrows, int ncols)
180 > cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale)
181   {
182 +        int     doscale;
183          CMATRIX *cm;
184          COLORV  *mp;
185                                                  /* header already loaded */
165        if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) {
166                error(USER, "bad picture resolution string");
167                return(NULL);
168        }
186          cm = cm_alloc(nrows, ncols);
187          if (!cm)
188                  return(NULL);
189 +        doscale = (scale[0] < .99) | (scale[0] > 1.01) |
190 +                        (scale[1] < .99) | (scale[1] > 1.01) |
191 +                        (scale[2] < .99) | (scale[2] > 1.01) ;
192          mp = cm->cmem;
193          while (nrows--) {
194                  if (freadscan((COLOR *)mp, ncols, fp) < 0) {
# Line 176 | Line 196 | cm_load_rgbe(FILE *fp, int nrows, int ncols)
196                          cm_free(cm);
197                          return(NULL);
198                  }
199 <                mp += 3*ncols;
199 >                if (doscale) {
200 >                        int     i = ncols;
201 >                        while (i--) {
202 >                                *mp++ *= scale[0];
203 >                                *mp++ *= scale[1];
204 >                                *mp++ *= scale[2];
205 >                        }
206 >                } else
207 >                        mp += 3*ncols;
208          }                                       /* caller closes stream */
209          return(cm);
210   }
# Line 186 | Line 214 | CMATRIX *
214   cm_load(const char *inspec, int nrows, int ncols, int dtype)
215   {
216          const int       ROWINC = 2048;
189        FILE            *fp = stdin;
217          int             swap = 0;
218 +        FILE            *fp;
219 +        COLOR           scale;
220          CMATRIX         *cm;
221  
222 <        if (!inspec)
223 <                inspec = "<stdin>";
222 >        if (!inspec || !*inspec)
223 >                return(NULL);
224 >        if (inspec == stdin_name)
225 >                fp = stdin;
226          else if (inspec[0] == '!') {
227                  fp = popen(inspec+1, "r");
228                  if (!fp) {
# Line 208 | Line 239 | cm_load(const char *inspec, int nrows, int ncols, int
239          if (dtype != DTascii)
240                  SET_FILE_BINARY(fp);            /* doesn't really work */
241          if (!dtype | !ncols) {                  /* expecting header? */
242 <                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp);
242 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
243                  if (err)
244                          error(USER, err);
214                if (ncols <= 0)
215                        error(USER, "unspecified number of columns");
245          }
246 +        if (ncols <= 0 && !fscnresolu(&ncols, &nrows, fp))
247 +                error(USER, "unspecified number of columns");
248          switch (dtype) {
249          case DTascii:
250          case DTfloat:
# Line 221 | Line 252 | cm_load(const char *inspec, int nrows, int ncols, int
252                  break;
253          case DTrgbe:
254          case DTxyze:
255 <                cm = cm_load_rgbe(fp, nrows, ncols);
255 >                cm = cm_load_rgbe(fp, nrows, ncols, scale);
256                  goto cleanup;
257          default:
258                  error(USER, "unexpected data type in cm_load()");
259          }
260          if (nrows <= 0) {                       /* don't know length? */
261                  int     guessrows = 147;        /* usually big enough */
262 <                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
262 >                if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) {
263                          long    startpos = ftell(fp);
264                          if (fseek(fp, 0L, SEEK_END) == 0) {
265 +                                long    rowsiz = (long)ncols*cm_elem_size[dtype];
266                                  long    endpos = ftell(fp);
235                                long    elemsiz = 3*(dtype==DTfloat ?
236                                            sizeof(float) : sizeof(double));
267  
268 <                                if ((endpos - startpos) % (ncols*elemsiz)) {
268 >                                if ((endpos - startpos) % rowsiz) {
269                                          sprintf(errmsg,
270                                          "improper length for binary file '%s'",
271                                                          inspec);
272                                          error(USER, errmsg);
273                                  }
274 <                                guessrows = (endpos - startpos)/(ncols*elemsiz);
274 >                                guessrows = (endpos - startpos)/rowsiz;
275                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
276                                          sprintf(errmsg,
277                                                  "fseek() error on file '%s'",
# Line 283 | Line 313 | cm_load(const char *inspec, int nrows, int ncols, int
313                          }
314          } else {                                        /* read binary file */
315                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
316 <                        int     nread = 0;
316 >                        size_t  nread = 0;
317                          do {                            /* read all we can */
318                                  nread += getbinary(cm->cmem + 3*nread,
319                                                  sizeof(COLOR),
320 <                                                cm->nrows*cm->ncols - nread,
320 >                                                (size_t)cm->nrows*cm->ncols - nread,
321                                                  fp);
322                                  if (nrows <= 0) {       /* unknown length */
323 <                                        if (nread == cm->nrows*cm->ncols)
323 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
324                                                          /* need more space? */
325                                                  cm = cm_resize(cm, cm->nrows+ROWINC);
326                                          else if (nread && !(nread % cm->ncols))
# Line 298 | Line 328 | cm_load(const char *inspec, int nrows, int ncols, int
328                                                  cm = cm_resize(cm, nread/cm->ncols);
329                                          else            /* ended mid-row */
330                                                  goto EOFerror;
331 <                                } else if (nread < cm->nrows*cm->ncols)
331 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
332                                          goto EOFerror;
333 <                        } while (nread < cm->nrows*cm->ncols);
333 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
334  
335 +                        if (swap) {
336 +                                if (sizeof(COLORV) == 4)
337 +                                        swap32((char *)cm->cmem,
338 +                                                        3*(size_t)cm->nrows*cm->ncols);
339 +                                else /* sizeof(COLORV) == 8 */
340 +                                        swap64((char *)cm->cmem,
341 +                                                        3*(size_t)cm->nrows*cm->ncols);
342 +                        }
343                  } else if (dtype == DTdouble) {
344                          double  dc[3];                  /* load from double */
345                          COLORV  *cvp = cm->cmem;
346 <                        int     n = nrows*ncols;
346 >                        size_t  n = (size_t)nrows*ncols;
347  
348                          if (n <= 0)
349                                  goto not_handled;
350                          while (n--) {
351                                  if (getbinary(dc, sizeof(double), 3, fp) != 3)
352                                          goto EOFerror;
353 +                                if (swap) swap64((char *)dc, 3);
354                                  copycolor(cvp, dc);
355                                  cvp += 3;
356                          }
357                  } else /* dtype == DTfloat */ {
358                          float   fc[3];                  /* load from float */
359                          COLORV  *cvp = cm->cmem;
360 <                        int     n = nrows*ncols;
360 >                        size_t  n = (size_t)nrows*ncols;
361  
362                          if (n <= 0)
363                                  goto not_handled;
364                          while (n--) {
365                                  if (getbinary(fc, sizeof(float), 3, fp) != 3)
366                                          goto EOFerror;
367 +                                if (swap) swap32((char *)fc, 3);
368                                  copycolor(cvp, fc);
369                                  cvp += 3;
370                          }
# Line 336 | Line 376 | cm_load(const char *inspec, int nrows, int ncols, int
376                                  error(WARNING, errmsg);
377                  }
378          }
339        if (swap) {
340                if (dtype == DTfloat)
341                        swap32((char *)cm->cmem, 3*cm->nrows*cm->ncols);
342                else if (dtype == DTdouble)
343                        swap64((char *)cm->cmem, 3*cm->nrows*cm->ncols);
344        }
379   cleanup:
380          if (fp != stdin) {
381                  if (inspec[0] != '!')
# Line 453 | Line 487 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
487          static const char       tabEOL[2] = {'\t','\n'};
488          const COLORV            *mp;
489          int                     r, c;
490 +        size_t                  n, rv;
491  
492          if (!cm)
493                  return(0);
# Line 468 | Line 503 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
503          case DTfloat:
504          case DTdouble:
505                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
506 <                        r = cm->ncols*cm->nrows;
507 <                        while (r > 0) {
508 <                                c = putbinary(mp, sizeof(COLOR), r, fp);
509 <                                if (c <= 0)
506 >                        n = (size_t)cm->ncols*cm->nrows;
507 >                        while (n > 0) {
508 >                                rv = fwrite(mp, sizeof(COLOR), n, fp);
509 >                                if (rv <= 0)
510                                          return(0);
511 <                                mp += 3*c;
512 <                                r -= c;
511 >                                mp += 3*rv;
512 >                                n -= rv;
513                          }
514                  } else if (dtype == DTdouble) {
515                          double  dc[3];
516 <                        r = cm->ncols*cm->nrows;
517 <                        while (r--) {
516 >                        n = (size_t)cm->ncols*cm->nrows;
517 >                        while (n--) {
518                                  copycolor(dc, mp);
519                                  if (putbinary(dc, sizeof(double), 3, fp) != 3)
520                                          return(0);
# Line 487 | Line 522 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
522                          }
523                  } else /* dtype == DTfloat */ {
524                          float   fc[3];
525 <                        r = cm->ncols*cm->nrows;
526 <                        while (r--) {
525 >                        n = (size_t)cm->ncols*cm->nrows;
526 >                        while (n--) {
527                                  copycolor(fc, mp);
528                                  if (putbinary(fc, sizeof(float), 3, fp) != 3)
529                                          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines