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.4 by greg, Thu May 29 17:28:09 2014 UTC vs.
Revision 2.36 by greg, Sun Dec 4 16:58:08 2022 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <ctype.h>
11 + #include "platform.h"
12   #include "standard.h"
13   #include "cmatrix.h"
14   #include "platform.h"
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", "float", "double",
22 <                        COLRFMT, CIEFMT
21 >                        "unknown", COLRFMT, CIEFMT,
22 >                        "float", "ascii", "double"
23                  };
24  
25   const int       cm_elem_size[] = {
26 <                        0, 0, 3*sizeof(float), 3*sizeof(double), 4, 4
26 >                        0, 4, 4, 3*sizeof(float), 0, 3*sizeof(double)
27                  };
28  
29   /* Allocate a color coefficient matrix */
# Line 31 | 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));
39 <        if (cm == NULL)
38 >                                sizeof(COLOR)*((size_t)nrows*ncols - 1));
39 >        if (!cm)
40                  error(SYSTEM, "out of memory in cm_alloc()");
41          cm->nrows = nrows;
42          cm->ncols = ncols;
43          return(cm);
44   }
45  
46 + static void
47 + adjacent_ra_sizes(size_t bounds[2], size_t target)
48 + {
49 +        bounds[0] = 0; bounds[1] = 2048;
50 +        while (bounds[1] < target) {
51 +                bounds[0] = bounds[1];
52 +                bounds[1] += bounds[1]>>1;
53 +        }
54 + }
55 +
56   /* Resize color coefficient matrix */
57   CMATRIX *
58   cm_resize(CMATRIX *cm, int nrows)
59   {
60 +        size_t  old_size, new_size, ra_bounds[2];
61 +
62 +        if (!cm)
63 +                return(NULL);
64          if (nrows == cm->nrows)
65                  return(cm);
66          if (nrows <= 0) {
67                  cm_free(cm);
68                  return(NULL);
69          }
70 <        cm = (CMATRIX *)realloc(cm, sizeof(CMATRIX) +
71 <                        sizeof(COLOR)*(nrows*cm->ncols - 1));
72 <        if (cm == NULL)
73 <                error(SYSTEM, "out of memory in cm_resize()");
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)*((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);
76 >                cm = (CMATRIX *)realloc(cm, ra_bounds[1]);
77 >                if (!cm)
78 >                        error(SYSTEM, "out of memory in cm_resize()");
79 >        }
80          cm->nrows = nrows;
81          return(cm);
82   }
83  
84 + 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 +
92   static int
93 < getDT(char *s, void *p)
93 > get_cminfo(char *s, void *p)
94   {
95 <        char    fmt[32];
95 >        CMINFO  *ip = (CMINFO *)p;
96 >        char    fmt[MAXFMTLEN];
97          int     i;
98 <        
98 >
99 >        if (!strncmp(s, "NCOMP=", 6) && atoi(s+6) != 3) {
100 >                ip->err = "unexpected # components (must be 3)";
101 >                return(-1);
102 >        }
103 >        if (!strncmp(s, "NROWS=", 6)) {
104 >                ip->nrows = atoi(s+6);
105 >                return(0);
106 >        }
107 >        if (!strncmp(s, "NCOLS=", 6)) {
108 >                ip->ncols = atoi(s+6);
109 >                return(0);
110 >        }
111 >        if ((i = isbigendian(s)) >= 0) {
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++)
129                  if (!strcmp(fmt, cm_fmt_id[i]))
130 <                        *((int *)p) = i;
130 >                        ip->dtype = i;
131          return(0);
132   }
133  
134 < /* Load header to obtain data type */
135 < int
136 < getDTfromHeader(FILE *fp)
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, COLOR scale, FILE *fp)
137   {
138 <        int     dt = DTfromHeader;
139 <        
140 <        if (getheader(fp, getDT, &dt) < 0)
141 <                error(SYSTEM, "header read error");
142 <        if (dt == DTfromHeader)
143 <                error(USER, "missing data format in header");
144 <        return(dt);
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)
146 >                return(cmi.err);
147 >        if (dt) {                               /* get/check data type? */
148 >                if (cmi.dtype == DTfromHeader) {
149 >                        if (*dt == DTfromHeader)
150 >                                return("missing/unknown data format in header");
151 >                } else if (*dt == DTfromHeader)
152 >                        *dt = cmi.dtype;
153 >                else if (*dt != cmi.dtype)
154 >                        return("unexpected data format in header");
155 >        }
156 >        if (nr) {                               /* get/check #rows? */
157 >                if (*nr <= 0)
158 >                        *nr = cmi.nrows;
159 >                else if ((cmi.nrows > 0) & (*nr != cmi.nrows))
160 >                        return("unexpected row count in header");
161 >        }
162 >        if (nc) {                               /* get/check #columns? */
163 >                if (*nc <= 0)
164 >                        *nc = cmi.ncols;
165 >                else if ((cmi.ncols > 0) & (*nc != cmi.ncols))
166 >                        return("unexpected column count in header");
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 a matrix from the given file (or stdin if NULL) */
179 < CMATRIX *
180 < cm_load(const char *fname, int nrows, int ncols, int dtype)
178 > /* Allocate and load image data into matrix */
179 > static CMATRIX *
180 > cm_load_rgbe(FILE *fp, int nrows, int ncols, COLOR scale)
181   {
182 <        FILE    *fp = stdin;
182 >        int     doscale;
183          CMATRIX *cm;
184 +        COLORV  *mp;
185 +                                                /* header already loaded */
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) {
195 +                        error(USER, "error reading color picture as matrix");
196 +                        cm_free(cm);
197 +                        return(NULL);
198 +                }
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 + }
211  
212 <        if (ncols <= 0)
213 <                error(USER, "Non-positive number of columns");
214 <        if (fname == NULL)
215 <                fname = "<stdin>";
216 <        else if ((fp = fopen(fname, "r")) == NULL) {
217 <                sprintf(errmsg, "cannot open file '%s'", fname);
212 > /* Allocate and load a matrix from the given input (or stdin if NULL) */
213 > CMATRIX *
214 > cm_load(const char *inspec, int nrows, int ncols, int dtype)
215 > {
216 >        const int       ROWINC = 2048;
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_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);
233 >                        error(SYSTEM, errmsg);
234 >                }
235 >        } else if (!(fp = fopen(inspec, "r"))) {
236 >                sprintf(errmsg, "cannot open file '%s'", inspec);
237                  error(SYSTEM, errmsg);
238          }
239   #ifdef getc_unlocked
# Line 104 | Line 241 | cm_load(const char *fname, int nrows, int ncols, int d
241   #endif
242          if (dtype != DTascii)
243                  SET_FILE_BINARY(fp);            /* doesn't really work */
244 <        if (dtype == DTfromHeader)
245 <                dtype = getDTfromHeader(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 >                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:
256          case DTdouble:
257                  break;
258 +        case DTrgbe:
259 +        case DTxyze:
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)) {
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);
123                                long    elemsiz = 3*(dtype==DTfloat ?
124                                            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 <                                                        fname);
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'",
283 <                                                        fname);
283 >                                                        inspec);
284                                          error(SYSTEM, errmsg);
285                                  }
286                                  nrows = guessrows;      /* we're confident */
# Line 142 | Line 289 | cm_load(const char *fname, int nrows, int ncols, int d
289                  cm = cm_alloc(guessrows, ncols);
290          } else
291                  cm = cm_alloc(nrows, ncols);
292 <        if (cm == NULL)                                 /* XXX never happens */
292 >        if (!cm)                                        /* XXX never happens */
293                  return(NULL);
294          if (dtype == DTascii) {                         /* read text file */
295                  int     maxrow = (nrows > 0 ? nrows : 32000);
296                  int     r, c;
297                  for (r = 0; r < maxrow; r++) {
298                      if (r >= cm->nrows)                 /* need more space? */
299 <                        cm = cm_resize(cm, 2*cm->nrows);
299 >                        cm = cm_resize(cm, cm->nrows+ROWINC);
300                      for (c = 0; c < ncols; c++) {
301                          COLORV  *cv = cm_lval(cm,r,c);
302 <                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3)
302 >                        if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) {
303                                  if ((nrows <= 0) & (r > 0) & !c) {
304                                          cm = cm_resize(cm, maxrow=r);
305                                          break;
306                                  } else
307                                          goto EOFerror;
308 +                        }
309                      }
310                  }
311                  while ((c = getc(fp)) != EOF)
312                          if (!isspace(c)) {
313                                  sprintf(errmsg,
314 <                                "unexpected data at end of ascii file %s",
315 <                                                fname);
314 >                                "unexpected data at end of ascii input '%s'",
315 >                                                inspec);
316                                  error(WARNING, errmsg);
317                                  break;
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 += fread(cm->cmem + 3*nread,
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, 2*cm->nrows);
330 >                                                cm = cm_resize(cm, cm->nrows+ROWINC);
331                                          else if (nread && !(nread % cm->ncols))
332                                                          /* seem to be  done */
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 (fread(dc, sizeof(double), 3, fp) != 3)
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 (fread(fc, sizeof(float), 3, fp) != 3)
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                          }
376                  }
377                  if (fgetc(fp) != EOF) {
378                                  sprintf(errmsg,
379 <                                "unexpected data at end of binary file %s",
380 <                                                fname);
379 >                                "unexpected data at end of binary input '%s'",
380 >                                                inspec);
381                                  error(WARNING, errmsg);
382                  }
383          }
384 <        if (fp != stdin)
385 <                fclose(fp);
384 > cleanup:
385 >        if (fp != stdin) {
386 >                if (inspec[0] != '!')
387 >                        fclose(fp);
388 >                else if (pclose(fp)) {
389 >                        sprintf(errmsg, "error running command '%s'", inspec);
390 >                        error(WARNING, errmsg);
391 >                }
392 >        }
393   #ifdef getc_unlocked
394          else
395                  funlockfile(fp);
396   #endif
397          return(cm);
398   EOFerror:
399 <        sprintf(errmsg, "unexpected EOF reading %s", fname);
399 >        sprintf(errmsg, "unexpected EOF reading %s", inspec);
400          error(USER, errmsg);
401 +        return(NULL);
402   not_handled:
403          error(INTERNAL, "unhandled data size or length in cm_load()");
404          return(NULL);   /* gratis return */
# Line 245 | Line 411 | cm_column(const CMATRIX *cm, int c)
411          CMATRIX *cvr;
412          int     dr;
413  
414 +        if (!cm)
415 +                return(NULL);
416          if ((c < 0) | (c >= cm->ncols))
417                  error(INTERNAL, "column requested outside matrix");
418          cvr = cm_alloc(cm->nrows, 1);
419 <        if (cvr == NULL)
419 >        if (!cvr)
420                  return(NULL);
421          for (dr = 0; dr < cm->nrows; dr++) {
422                  const COLORV    *sp = cm_lval(cm,dr,c);
# Line 260 | Line 428 | cm_column(const CMATRIX *cm, int c)
428          return(cvr);
429   }
430  
263 /* Scale a matrix by a single value */
264 CMATRIX *
265 cm_scale(const CMATRIX *cm1, const COLOR sca)
266 {
267        CMATRIX *cmr;
268        int     dr, dc;
269
270        cmr = cm_alloc(cm1->nrows, cm1->ncols);
271        if (cmr == NULL)
272                return(NULL);
273        for (dr = 0; dr < cmr->nrows; dr++)
274            for (dc = 0; dc < cmr->ncols; dc++) {
275                const COLORV    *sp = cm_lval(cm1,dr,dc);
276                COLORV          *dp = cm_lval(cmr,dr,dc);
277                dp[0] = sp[0] * sca[0];
278                dp[1] = sp[1] * sca[1];
279                dp[2] = sp[2] * sca[2];
280            }
281        return(cmr);
282 }
283
431   /* Multiply two matrices (or a matrix and a vector) and allocate the result */
432   CMATRIX *
433   cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
# Line 289 | Line 436 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
436          CMATRIX *cmr;
437          int     dr, dc, i;
438  
439 +        if (!cm1 | !cm2)
440 +                return(NULL);
441          if ((cm1->ncols <= 0) | (cm1->ncols != cm2->nrows))
442                  error(INTERNAL, "matrix dimension mismatch in cm_multiply()");
443          cmr = cm_alloc(cm1->nrows, cm2->ncols);
444 <        if (cmr == NULL)
444 >        if (!cmr)
445                  return(NULL);
446                                  /* optimization: check for zero rows & cols */
447          if (((cm1->nrows > 5) | (cm2->ncols > 5)) & (cm1->ncols > 5)) {
# Line 317 | Line 466 | cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2)
466                  COLORV  *dp = cm_lval(cmr,dr,dc);
467                  double  res[3];
468                  dp[0] = dp[1] = dp[2] = 0;
469 <                if (rowcheck != NULL && !rowcheck[dr])
469 >                if (rowcheck && !rowcheck[dr])
470                          continue;
471 <                if (colcheck != NULL && !colcheck[dc])
471 >                if (colcheck && !colcheck[dc])
472                          continue;
473                  res[0] = res[1] = res[2] = 0;
474                  for (i = 0; i < cm1->ncols; i++) {
475                      const COLORV        *cp1 = cm_lval(cm1,dr,i);
476                      const COLORV        *cp2 = cm_lval(cm2,i,dc);
477 <                    res[0] += cp1[0] * cp2[0];
478 <                    res[1] += cp1[1] * cp2[1];
479 <                    res[2] += cp1[2] * cp2[2];
477 >                    res[0] += (double)cp1[0] * cp2[0];
478 >                    res[1] += (double)cp1[1] * cp2[1];
479 >                    res[2] += (double)cp1[2] * cp2[2];
480                  }
481                  copycolor(dp, res);
482              }
483 <        if (rowcheck != NULL) free(rowcheck);
484 <        if (colcheck != NULL) free(colcheck);
483 >        if (rowcheck) free(rowcheck);
484 >        if (colcheck) free(colcheck);
485          return(cmr);
486   }
487  
# Line 341 | Line 490 | int
490   cm_write(const CMATRIX *cm, int dtype, FILE *fp)
491   {
492          static const char       tabEOL[2] = {'\t','\n'};
493 <        const COLORV            *mp = cm->cmem;
493 >        const COLORV            *mp;
494          int                     r, c;
495 +        size_t                  n, rv;
496  
497 +        if (!cm)
498 +                return(0);
499 +        mp = cm->cmem;
500          switch (dtype) {
501          case DTascii:
502                  for (r = 0; r < cm->nrows; r++)
# Line 355 | 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 = fwrite(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 (fwrite(dc, sizeof(double), 3, fp) != 3)
524 >                                if (putbinary(dc, sizeof(double), 3, fp) != 3)
525                                          return(0);
526                                  mp += 3;
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 (fwrite(fc, sizeof(float), 3, fp) != 3)
533 >                                if (putbinary(fc, sizeof(float), 3, fp) != 3)
534                                          return(0);
535                                  mp += 3;
536                          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines