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.21 by greg, Mon Aug 12 18:28:37 2019 UTC vs.
Revision 2.26 by greg, Thu Mar 26 02:48:31 2020 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include "resolu.h"
17  
18   const char      *cm_fmt_id[] = {
19 <                        "unknown", "ascii", COLRFMT, CIEFMT,
20 <                        "float", "double"
19 >                        "unknown", COLRFMT, CIEFMT,
20 >                        "float", "ascii", "double"
21                  };
22  
23   const int       cm_elem_size[] = {
24 <                        0, 0, 4, 4, 3*sizeof(float), 3*sizeof(double)
24 >                        0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double)
25                  };
26  
27   /* Allocate a color coefficient matrix */
# Line 81 | Line 81 | cm_resize(CMATRIX *cm, int nrows)
81  
82   typedef struct {
83          int     dtype;          /* data type */
84 +        int     need2swap;      /* need byte swap? */
85          int     nrows, ncols;   /* matrix size */
86 +        COLOR   expos;          /* exposure value */
87          char    *err;           /* error message */
88   } CMINFO;               /* header info record */
89  
# Line 104 | Line 106 | get_cminfo(char *s, void *p)
106                  ip->ncols = atoi(s+6);
107                  return(0);
108          }
109 +        if ((i = isbigendian(s)) >= 0) {
110 +                ip->need2swap = (nativebigendian() != i);
111 +                return(0);
112 +        }
113 +        if (isexpos(s)) {
114 +                double  d = exposval(s);
115 +                scalecolor(ip->expos, d);
116 +                return(0);
117 +        }
118 +        if (iscolcor(s)) {
119 +                COLOR   ctmp;
120 +                colcorval(ctmp, s);
121 +                multcolor(ip->expos, ctmp);
122 +                return(0);
123 +        }
124          if (!formatval(fmt, s))
125                  return(0);
126          for (i = 1; i < DTend; i++)
# Line 114 | Line 131 | get_cminfo(char *s, void *p)
131  
132   /* Load header to obtain/check data type and number of columns */
133   char *
134 < cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
134 > cm_getheader(int *dt, int *nr, int *nc, int *swp, COLOR scale, FILE *fp)
135   {
136          CMINFO  cmi;
137                                                  /* read header */
138          cmi.dtype = DTfromHeader;
139 +        cmi.need2swap = 0;
140 +        cmi.expos[0] = cmi.expos[1] = cmi.expos[2] = 1.f;
141          cmi.nrows = cmi.ncols = 0;
142          cmi.err = "unexpected EOF in header";
143          if (getheader(fp, get_cminfo, &cmi) < 0)
# Line 144 | Line 163 | cm_getheader(int *dt, int *nr, int *nc, FILE *fp)
163                  else if ((cmi.ncols > 0) & (*nc != cmi.ncols))
164                          return("unexpected column count in header");
165          }
166 +        if (swp)                                /* get/check swap? */
167 +                *swp = cmi.need2swap;
168 +        if (scale) {                            /* transfer exposure comp. */
169 +                scale[0] = 1.f/cmi.expos[0];
170 +                scale[1] = 1.f/cmi.expos[1];
171 +                scale[2] = 1.f/cmi.expos[2];
172 +        }
173          return(NULL);
174   }
175  
176 + /* Allocate and load image data into matrix */
177 + static CMATRIX *
178 + cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale)
179 + {
180 +        int     doscale;
181 +        CMATRIX *cm;
182 +        COLORV  *mp;
183 +                                                /* header already loaded */
184 +        if ((nrows <= 0) | (ncols <= 0) && !fscnresolu(&ncols, &nrows, fp)) {
185 +                error(USER, "bad picture resolution string");
186 +                return(NULL);
187 +        }
188 +        cm = cm_alloc(nrows, ncols);
189 +        if (!cm)
190 +                return(NULL);
191 +        doscale = (scale[0] < .99) | (scale[0] > 1.01) |
192 +                        (scale[1] < .99) | (scale[1] > 1.01) |
193 +                        (scale[2] < .99) | (scale[2] > 1.01) ;
194 +        mp = cm->cmem;
195 +        while (nrows--) {
196 +                if (freadscan((COLOR *)mp, ncols, fp) < 0) {
197 +                        error(USER, "error reading color picture as matrix");
198 +                        cm_free(cm);
199 +                        return(NULL);
200 +                }
201 +                if (doscale) {
202 +                        int     i = ncols;
203 +                        while (i--) {
204 +                                *mp++ *= scale[0];
205 +                                *mp++ *= scale[1];
206 +                                *mp++ *= scale[2];
207 +                        }
208 +                } else
209 +                        mp += 3*ncols;
210 +        }                                       /* caller closes stream */
211 +        return(cm);
212 + }
213 +
214   /* Allocate and load a matrix from the given input (or stdin if NULL) */
215   CMATRIX *
216   cm_load(const char *inspec, int nrows, int ncols, int dtype)
217   {
218          const int       ROWINC = 2048;
219          FILE            *fp = stdin;
220 +        int             swap = 0;
221 +        COLOR           scale;
222          CMATRIX         *cm;
223  
224          if (!inspec)
# Line 173 | 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, fp);
242 >                char    *err = cm_getheader(&dtype, &nrows, &ncols, &swap, scale, fp);
243                  if (err)
244                          error(USER, err);
245                  if (ncols <= 0)
# Line 184 | Line 250 | cm_load(const char *inspec, int nrows, int ncols, int
250          case DTfloat:
251          case DTdouble:
252                  break;
253 +        case DTrgbe:
254 +        case DTxyze:
255 +                cm = cm_load_rgbe(fp, nrows, ncols, scale);
256 +                goto cleanup;
257          default:
258                  error(USER, "unexpected data type in cm_load()");
259          }
# Line 263 | Line 333 | cm_load(const char *inspec, int nrows, int ncols, int
333                                          goto EOFerror;
334                          } while (nread < cm->nrows*cm->ncols);
335  
336 +                        if (swap) {
337 +                                if (sizeof(COLORV) == 4)
338 +                                        swap32((char *)cm->cmem,
339 +                                                        3*cm->nrows*cm->ncols);
340 +                                else /* sizeof(COLORV) == 8 */
341 +                                        swap64((char *)cm->cmem,
342 +                                                        3*cm->nrows*cm->ncols);
343 +                        }
344                  } else if (dtype == DTdouble) {
345                          double  dc[3];                  /* load from double */
346                          COLORV  *cvp = cm->cmem;
# Line 273 | Line 351 | cm_load(const char *inspec, int nrows, int ncols, int
351                          while (n--) {
352                                  if (getbinary(dc, sizeof(double), 3, fp) != 3)
353                                          goto EOFerror;
354 +                                if (swap) swap64((char *)dc, 3);
355                                  copycolor(cvp, dc);
356                                  cvp += 3;
357                          }
# Line 286 | Line 365 | cm_load(const char *inspec, int nrows, int ncols, int
365                          while (n--) {
366                                  if (getbinary(fc, sizeof(float), 3, fp) != 3)
367                                          goto EOFerror;
368 +                                if (swap) swap32((char *)fc, 3);
369                                  copycolor(cvp, fc);
370                                  cvp += 3;
371                          }
# Line 297 | Line 377 | cm_load(const char *inspec, int nrows, int ncols, int
377                                  error(WARNING, errmsg);
378                  }
379          }
380 + cleanup:
381          if (fp != stdin) {
382                  if (inspec[0] != '!')
383                          fclose(fp);
# Line 313 | Line 394 | cm_load(const char *inspec, int nrows, int ncols, int
394   EOFerror:
395          sprintf(errmsg, "unexpected EOF reading %s", inspec);
396          error(USER, errmsg);
397 +        return(NULL);
398   not_handled:
399          error(INTERNAL, "unhandled data size or length in cm_load()");
400          return(NULL);   /* gratis return */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines