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.24 by greg, Thu Nov 7 23:10:18 2019 UTC vs.
Revision 2.36 by greg, Sun Dec 4 16:58:08 2022 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", 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;
217 <        FILE            *fp = stdin;
217 >        int             dimsOK = (dtype == DTascii) | (nrows > 0) && ncols;
218          int             swap = 0;
219 +        FILE            *fp;
220 +        COLOR           scale;
221          CMATRIX         *cm;
222  
223          if (!inspec)
224 <                inspec = "<stdin>";
225 <        else if (inspec[0] == '!') {
224 >                inspec = stdin_name;
225 >        else if (!*inspec)
226 >                return(NULL);
227 >        if (inspec == stdin_name) {             /* reading from stdin? */
228 >                fp = stdin;
229 >        } else if (inspec[0] == '!') {
230                  fp = popen(inspec+1, "r");
231                  if (!fp) {
232                          sprintf(errmsg, "cannot start command '%s'", inspec);
# Line 207 | Line 241 | cm_load(const char *inspec, int nrows, int ncols, int
241   #endif
242          if (dtype != DTascii)
243                  SET_FILE_BINARY(fp);            /* doesn't really work */
244 <        if (!dtype | !ncols) {                  /* expecting header? */
245 <                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, fp);
244 >        if (!dtype | !dimsOK) {                 /* expecting header? */
245 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
246                  if (err)
247                          error(USER, err);
248 <                if (ncols <= 0)
249 <                        error(USER, "unspecified number of columns");
248 >                dimsOK = ncols > 0 && ( nrows > 0 ||
249 >                                (dtype != DTrgbe) & (dtype != DTxyze) );
250          }
251 +        if (!dimsOK && !fscnresolu(&ncols, &nrows, fp))
252 +                error(USER, "unspecified matrix size");
253          switch (dtype) {
254          case DTascii:
255          case DTfloat:
# Line 221 | Line 257 | cm_load(const char *inspec, int nrows, int ncols, int
257                  break;
258          case DTrgbe:
259          case DTxyze:
260 <                cm = cm_load_rgbe(fp, nrows, ncols);
260 >                cm = cm_load_rgbe(fp, nrows, ncols, scale);
261                  goto cleanup;
262          default:
263                  error(USER, "unexpected data type in cm_load()");
264          }
265          if (nrows <= 0) {                       /* don't know length? */
266                  int     guessrows = 147;        /* usually big enough */
267 <                if ((dtype != DTascii) & (fp != stdin) & (inspec[0] != '!')) {
267 >                if (cm_elem_size[dtype] && (fp != stdin) & (inspec[0] != '!')) {
268                          long    startpos = ftell(fp);
269                          if (fseek(fp, 0L, SEEK_END) == 0) {
270 +                                long    rowsiz = (long)ncols*cm_elem_size[dtype];
271                                  long    endpos = ftell(fp);
235                                long    elemsiz = 3*(dtype==DTfloat ?
236                                            sizeof(float) : sizeof(double));
272  
273 <                                if ((endpos - startpos) % (ncols*elemsiz)) {
273 >                                if ((endpos - startpos) % rowsiz) {
274                                          sprintf(errmsg,
275                                          "improper length for binary file '%s'",
276                                                          inspec);
277                                          error(USER, errmsg);
278                                  }
279 <                                guessrows = (endpos - startpos)/(ncols*elemsiz);
279 >                                guessrows = (endpos - startpos)/rowsiz;
280                                  if (fseek(fp, startpos, SEEK_SET) < 0) {
281                                          sprintf(errmsg,
282                                                  "fseek() error on file '%s'",
# Line 283 | Line 318 | cm_load(const char *inspec, int nrows, int ncols, int
318                          }
319          } else {                                        /* read binary file */
320                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
321 <                        int     nread = 0;
321 >                        size_t  nread = 0;
322                          do {                            /* read all we can */
323                                  nread += getbinary(cm->cmem + 3*nread,
324                                                  sizeof(COLOR),
325 <                                                cm->nrows*cm->ncols - nread,
325 >                                                (size_t)cm->nrows*cm->ncols - nread,
326                                                  fp);
327                                  if (nrows <= 0) {       /* unknown length */
328 <                                        if (nread == cm->nrows*cm->ncols)
328 >                                        if (nread == (size_t)cm->nrows*cm->ncols)
329                                                          /* need more space? */
330                                                  cm = cm_resize(cm, cm->nrows+ROWINC);
331                                          else if (nread && !(nread % cm->ncols))
# Line 298 | Line 333 | cm_load(const char *inspec, int nrows, int ncols, int
333                                                  cm = cm_resize(cm, nread/cm->ncols);
334                                          else            /* ended mid-row */
335                                                  goto EOFerror;
336 <                                } else if (nread < cm->nrows*cm->ncols)
336 >                                } else if (nread < (size_t)cm->nrows*cm->ncols)
337                                          goto EOFerror;
338 <                        } while (nread < cm->nrows*cm->ncols);
338 >                        } while (nread < (size_t)cm->nrows*cm->ncols);
339  
340 +                        if (swap) {
341 +                                if (sizeof(COLORV) == 4)
342 +                                        swap32((char *)cm->cmem,
343 +                                                        3*(size_t)cm->nrows*cm->ncols);
344 +                                else /* sizeof(COLORV) == 8 */
345 +                                        swap64((char *)cm->cmem,
346 +                                                        3*(size_t)cm->nrows*cm->ncols);
347 +                        }
348                  } else if (dtype == DTdouble) {
349                          double  dc[3];                  /* load from double */
350                          COLORV  *cvp = cm->cmem;
351 <                        int     n = nrows*ncols;
351 >                        size_t  n = (size_t)nrows*ncols;
352  
353                          if (n <= 0)
354                                  goto not_handled;
355                          while (n--) {
356                                  if (getbinary(dc, sizeof(double), 3, fp) != 3)
357                                          goto EOFerror;
358 +                                if (swap) swap64((char *)dc, 3);
359                                  copycolor(cvp, dc);
360                                  cvp += 3;
361                          }
362                  } else /* dtype == DTfloat */ {
363                          float   fc[3];                  /* load from float */
364                          COLORV  *cvp = cm->cmem;
365 <                        int     n = nrows*ncols;
365 >                        size_t  n = (size_t)nrows*ncols;
366  
367                          if (n <= 0)
368                                  goto not_handled;
369                          while (n--) {
370                                  if (getbinary(fc, sizeof(float), 3, fp) != 3)
371                                          goto EOFerror;
372 +                                if (swap) swap32((char *)fc, 3);
373                                  copycolor(cvp, fc);
374                                  cvp += 3;
375                          }
# Line 336 | Line 381 | cm_load(const char *inspec, int nrows, int ncols, int
381                                  error(WARNING, errmsg);
382                  }
383          }
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        }
384   cleanup:
385          if (fp != stdin) {
386                  if (inspec[0] != '!')
# Line 453 | Line 492 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
492          static const char       tabEOL[2] = {'\t','\n'};
493          const COLORV            *mp;
494          int                     r, c;
495 +        size_t                  n, rv;
496  
497          if (!cm)
498                  return(0);
# Line 468 | Line 508 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
508          case DTfloat:
509          case DTdouble:
510                  if (sizeof(COLOR) == cm_elem_size[dtype]) {
511 <                        r = cm->ncols*cm->nrows;
512 <                        while (r > 0) {
513 <                                c = putbinary(mp, sizeof(COLOR), r, fp);
514 <                                if (c <= 0)
511 >                        n = (size_t)cm->ncols*cm->nrows;
512 >                        while (n > 0) {
513 >                                rv = fwrite(mp, sizeof(COLOR), n, fp);
514 >                                if (rv <= 0)
515                                          return(0);
516 <                                mp += 3*c;
517 <                                r -= c;
516 >                                mp += 3*rv;
517 >                                n -= rv;
518                          }
519                  } else if (dtype == DTdouble) {
520                          double  dc[3];
521 <                        r = cm->ncols*cm->nrows;
522 <                        while (r--) {
521 >                        n = (size_t)cm->ncols*cm->nrows;
522 >                        while (n--) {
523                                  copycolor(dc, mp);
524                                  if (putbinary(dc, sizeof(double), 3, fp) != 3)
525                                          return(0);
# Line 487 | Line 527 | cm_write(const CMATRIX *cm, int dtype, FILE *fp)
527                          }
528                  } else /* dtype == DTfloat */ {
529                          float   fc[3];
530 <                        r = cm->ncols*cm->nrows;
531 <                        while (r--) {
530 >                        n = (size_t)cm->ncols*cm->nrows;
531 >                        while (n--) {
532                                  copycolor(fc, mp);
533                                  if (putbinary(fc, sizeof(float), 3, fp) != 3)
534                                          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines