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

Comparing ray/src/util/rmatrix.c (file contents):
Revision 2.64 by greg, Tue Nov 28 16:19:31 2023 UTC vs.
Revision 2.74 by greg, Fri Dec 8 00:12:31 2023 UTC

# Line 31 | Line 31 | rmx_new(int nr, int nc, int n)
31                  return(NULL);
32  
33          dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX));
34 <        if (dnew) {
35 <                dnew->dtype = DTdouble;
36 <                dnew->nrows = nr;
37 <                dnew->ncols = nc;
38 <                dnew->ncomp = n;
39 <                setcolor(dnew->cexp, 1.f, 1.f, 1.f);
40 <                memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart));
41 <        }
34 >        if (!dnew)
35 >                return(NULL);
36 >
37 >        dnew->dtype = DTdouble;
38 >        dnew->nrows = nr;
39 >        dnew->ncols = nc;
40 >        dnew->ncomp = n;
41 >        setcolor(dnew->cexp, 1.f, 1.f, 1.f);
42 >        memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart));
43 >
44          return(dnew);
45   }
46  
# Line 68 | Line 70 | rmx_alloc(int nr, int nc, int n)
70          return(dnew);
71   }
72  
73 < /* Free a RMATRIX array */
73 > /* Clear state by freeing info and matrix data */
74   void
75 < rmx_free(RMATRIX *rm)
75 > rmx_reset(RMATRIX *rm)
76   {
77          if (!rm) return;
78 <        if (rm->info)
78 >        if (rm->info) {
79                  free(rm->info);
80 +                rm->info = NULL;
81 +        }
82 +        if (rm->mtx) {
83   #ifdef MAP_FILE
84 <        if (rm->mapped)
85 <                munmap(rm->mapped, mapped_size(rm));
86 <        else
84 >                if (rm->mapped) {
85 >                        munmap(rm->mapped, mapped_size(rm));
86 >                        rm->mapped = NULL;
87 >                } else
88   #endif
89 <                free(rm->mtx);
89 >                        free(rm->mtx);
90 >                rm->mtx = NULL;
91 >        }
92 > }
93 >
94 > /* Free an RMATRIX struct and data */
95 > void
96 > rmx_free(RMATRIX *rm)
97 > {
98 >        if (!rm) return;
99 >        rmx_reset(rm);
100          free(rm);
101   }
102  
# Line 172 | Line 188 | get_dminfo(char *s, void *p)
188   }
189  
190   static int
191 < rmx_load_ascii(RMATRIX *rm, FILE *fp)
191 > rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *fp)
192   {
193 <        int     i, j, k;
193 >        int     j, k;
194  
195 <        if (!rmx_prepare(rm))
196 <                return(0);
197 <        for (i = 0; i < rm->nrows; i++)
198 <            for (j = 0; j < rm->ncols; j++) {
183 <                double  *dp = rmx_lval(rm,i,j);
184 <                for (k = 0; k < rm->ncomp; k++)
185 <                    if (fscanf(fp, "%lf", &dp[k]) != 1)
186 <                        return(0);
187 <            }
195 >        for (j = 0; j < rm->ncols; j++)
196 >                for (k = rm->ncomp; k-- > 0; )
197 >                        if (fscanf(fp, "%lf", drp++) != 1)
198 >                                return(0);
199          return(1);
200   }
201  
202   static int
203 < rmx_load_float(RMATRIX *rm, FILE *fp)
203 > rmx_load_float(double *drp, const RMATRIX *rm, FILE *fp)
204   {
205 <        int     i, j, k;
205 >        int     j, k;
206          float   val[100];
207  
208          if (rm->ncomp > 100) {
209                  fputs("Unsupported # components in rmx_load_float()\n", stderr);
210                  exit(1);
211          }
212 <        if (!rmx_prepare(rm))
202 <                return(0);
203 <        for (i = 0; i < rm->nrows; i++)
204 <            for (j = 0; j < rm->ncols; j++) {
205 <                double  *dp = rmx_lval(rm,i,j);
212 >        for (j = 0; j < rm->ncols; j++) {
213                  if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
214 <                    return(0);
214 >                        return(0);
215                  if (rm->swapin)
216 <                    swap32((char *)val, rm->ncomp);
217 <                for (k = rm->ncomp; k--; )
218 <                     dp[k] = val[k];
219 <            }
216 >                        swap32((char *)val, rm->ncomp);
217 >                for (k = 0; k < rm->ncomp; k++)
218 >                        *drp++ = val[k];
219 >        }
220          return(1);
221   }
222  
223   static int
224 < rmx_load_double(RMATRIX *rm, FILE *fp)
224 > rmx_load_double(double *drp, const RMATRIX *rm, FILE *fp)
225   {
226 <        int     i;
220 < #ifdef MAP_FILE
221 <        long    pos;            /* map memory for file > 1MB if possible */
222 <        if (!rm->swapin && array_size(rm) >= 1L<<20 &&
223 <                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) {
224 <                rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE,
225 <                                        MAP_PRIVATE, fileno(fp), 0);
226 <                if (rm->mapped != MAP_FAILED) {
227 <                        rm->mtx = (double *)rm->mapped + pos/sizeof(double);
228 <                        return(1);
229 <                }               /* else fall back on reading into memory */
230 <                rm->mapped = NULL;
231 <        }
232 < #endif
233 <        if (!rmx_prepare(rm))
226 >        if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols)
227                  return(0);
228 <        for (i = 0; i < rm->nrows; i++) {
229 <                if (getbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp,
237 <                                        rm->ncols, fp) != rm->ncols)
238 <                        return(0);
239 <                if (rm->swapin)
240 <                        swap64((char *)rmx_lval(rm,i,0), rm->ncols*rm->ncomp);
241 <        }
228 >        if (rm->swapin)
229 >                swap64((char *)drp, rm->ncols*rm->ncomp);
230          return(1);
231   }
232  
233   static int
234 < rmx_load_rgbe(RMATRIX *rm, FILE *fp)
234 > rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp)
235   {
236 <        COLOR   *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
237 <        int     i, j;
236 >        COLR    *scan;
237 >        COLOR   col;
238 >        int     j;
239  
240 +        if (rm->ncomp != 3)
241 +                return(0);
242 +        scan = (COLR *)tempbuffer(sizeof(COLR)*rm->ncols);
243          if (!scan)
244                  return(0);
245 <        if (!rmx_prepare(rm)) {
254 <                free(scan);
245 >        if (freadcolrs(scan, rm->ncols, fp) < 0)
246                  return(0);
247 +        for (j = 0; j < rm->ncols; j++) {
248 +                colr_color(col, scan[j]);
249 +                *drp++ = colval(col,RED);
250 +                *drp++ = colval(col,GRN);
251 +                *drp++ = colval(col,BLU);
252          }
257        for (i = 0; i < rm->nrows; i++) {
258            double      *dp = rmx_lval(rm,i,0);
259            if (freadscan(scan, rm->ncols, fp) < 0) {
260                free(scan);
261                return(0);
262            }
263            for (j = 0; j < rm->ncols; j++, dp += 3) {
264                dp[0] = colval(scan[j],RED);
265                dp[1] = colval(scan[j],GRN);
266                dp[2] = colval(scan[j],BLU);
267            }
268        }
269        free(scan);
253          return(1);
254   }
255  
256   static int
257 < rmx_load_spec(RMATRIX *rm, FILE *fp)
257 > rmx_load_spec(double *drp, const RMATRIX *rm, FILE *fp)
258   {
259          uby8    *scan;
260          SCOLOR  scol;
261 <        int     i, j, k;
261 >        int     j, k;
262  
263 <        if (rm->ncomp < 3)
263 >        if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP))
264                  return(0);
265 <        scan = (uby8 *)malloc((rm->ncomp+1)*rm->ncols);
265 >        scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols);
266          if (!scan)
267                  return(0);
268 <        if (!rmx_prepare(rm)) {
286 <                free(scan);
268 >        if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0)
269                  return(0);
270 +        for (j = 0; j < rm->ncols; j++) {
271 +                scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp);
272 +                for (k = 0; k < rm->ncomp; k++)
273 +                        *drp++ = scol[k];
274          }
289        for (i = 0; i < rm->nrows; i++) {
290            double      *dp = rmx_lval(rm,i,0);
291            if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) {
292                free(scan);
293                return(0);
294            }
295            for (j = 0; j < rm->ncols; j++) {
296                scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp);
297                for (k = 0; k < rm->ncomp; k++)
298                        *dp++ = scol[k];
299            }
300        }
301        free(scan);
275          return(1);
276   }
277  
# Line 308 | Line 281 | rmx_load_header(RMATRIX *rm, FILE *fp)
281   {
282          if (!rm | !fp)
283                  return(0);
284 <        if (rm->info) {                         /* clear state */
312 <                free(rm->info);
313 <                rm->info = NULL;
314 <        }
315 <        if (rm->mtx) {                          /* ...and data */
316 < #ifdef MAP_FILE
317 <                if (rm->mapped) {
318 <                        munmap(rm->mapped, mapped_size(rm));
319 <                        rm->mapped = NULL;
320 <                } else
321 < #endif
322 <                        free(rm->mtx);
323 <                rm->mtx = NULL;
324 <        }
284 >        rmx_reset(rm);                          /* clear state */
285          if (rm->nrows | rm->ncols | !rm->dtype) {
286                  rm->nrows = rm->ncols = 0;
287                  rm->ncomp = 3;
# Line 329 | Line 289 | rmx_load_header(RMATRIX *rm, FILE *fp)
289                  memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart));
290                  rm->swapin = 0;
291          }
332        SET_FILE_BINARY(fp);
292          rm->dtype = DTascii;                    /* assumed w/o FORMAT */
293          if (getheader(fp, get_dminfo, rm) < 0) {
294                  fputs("Unrecognized matrix format\n", stderr);
295                  return(0);
296          }
297 <                                                /* resolution string? */
298 <        if ((rm->nrows <= 0) | (rm->ncols <= 0)) {
299 <                if (!fscnresolu(&rm->ncols, &rm->nrows, fp))
300 <                        return(0);
301 <                if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
302 <                                rm->ncomp != 3)
303 <                        return(0);
304 <        }
297 >        if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
298 >                        rm->ncomp != 3)
299 >                return(0);
300 >        if (rm->ncols <= 0 &&                   /* resolution string? */
301 >                        !fscnresolu(&rm->ncols, &rm->nrows, fp))
302 >                return(0);
303 >        if (rm->dtype == DTascii)               /* set file type (WINDOWS) */
304 >                SET_FILE_TEXT(fp);
305 >        else
306 >                SET_FILE_BINARY(fp);
307          return(1);
308   }
309  
310 < /* Allocate & load post-header data from stream given type set in rm->dtype */
310 > /* Load next row as double (cannot be XML) */
311   int
312 < rmx_load_data(RMATRIX *rm, FILE *fp)
312 > rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp)
313   {
314          switch (rm->dtype) {
315          case DTascii:
316 <                SET_FILE_TEXT(fp);
356 <                return(rmx_load_ascii(rm, fp));
316 >                return(rmx_load_ascii(drp, rm, fp));
317          case DTfloat:
318 <                return(rmx_load_float(rm, fp));
318 >                return(rmx_load_float(drp, rm, fp));
319          case DTdouble:
320 <                return(rmx_load_double(rm, fp));
320 >                return(rmx_load_double(drp, rm, fp));
321          case DTrgbe:
322          case DTxyze:
323 <                return(rmx_load_rgbe(rm, fp));
323 >                return(rmx_load_rgbe(drp, rm, fp));
324          case DTspec:
325 <                return(rmx_load_spec(rm, fp));
325 >                return(rmx_load_spec(drp, rm, fp));
326          default:
327 <                fputs("Unsupported data type in rmx_loaddata()\n", stderr);
327 >                fputs("Unsupported data type in rmx_load_row()\n", stderr);
328          }
329          return(0);
330   }
331  
332 + /* Allocate & load post-header data from stream given type set in rm->dtype */
333 + int
334 + rmx_load_data(RMATRIX *rm, FILE *fp)
335 + {
336 +        int     i;
337 + #ifdef MAP_FILE
338 +        long    pos;            /* map memory for file > 1MB if possible */
339 +        if ((rm->dtype == DTdouble) & !rm->swapin && array_size(rm) >= 1L<<20 &&
340 +                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) {
341 +                rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE,
342 +                                        MAP_PRIVATE, fileno(fp), 0);
343 +                if (rm->mapped != MAP_FAILED) {
344 +                        rm->mtx = (double *)rm->mapped + pos/sizeof(double);
345 +                        return(1);
346 +                }               /* else fall back on reading into memory */
347 +                rm->mapped = NULL;
348 +        }
349 + #endif
350 +        if (!rmx_prepare(rm)) { /* need in-core matrix array */
351 +                fprintf(stderr, "Cannot allocate %g MByte matrix array\n",
352 +                                (1./(1L<<20))*(double)array_size(rm));
353 +                return(0);
354 +        }
355 +        for (i = 0; i < rm->nrows; i++)
356 +                if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp))
357 +                        return(0);
358 +        return(1);
359 + }
360 +
361   /* Load matrix from supported file type */
362   RMATRIX *
363   rmx_load(const char *inspec, RMPref rmp)
# Line 392 | Line 381 | rmx_load(const char *inspec, RMPref rmp)
381                  while (sp > inspec && sp[-1] != '.')
382                          --sp;
383                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
384 <                        CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) :
384 >                        CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL :
385 >                                        rmp==RMPtrans ? cm_loadBTDF(inspec) :
386                                          cm_loadBRDF(inspec, rmp==RMPreflB) ;
387                          if (!cm)
388                                  return(NULL);
# Line 408 | Line 398 | rmx_load(const char *inspec, RMPref rmp)
398   #ifdef getc_unlocked
399          flockfile(fp);
400   #endif
401 <                                                /* load header info */
401 >        SET_FILE_BINARY(fp);                    /* load header info */
402          if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) {
403                  fprintf(stderr, "Bad header in: %s\n", inspec);
404                  if (inspec[0] == '!') pclose(fp);
# Line 456 | Line 446 | rmx_load(const char *inspec, RMPref rmp)
446   }
447  
448   static int
449 < rmx_write_ascii(const RMATRIX *rm, FILE *fp)
449 > rmx_write_ascii(const double *dp, int nc, int len, FILE *fp)
450   {
451 <        const char      *fmt = (rm->dtype == DTfloat) ? " %.7e" :
452 <                        (rm->dtype == DTrgbe) | (rm->dtype == DTxyze) |
453 <                        (rm->dtype == DTspec) ? " %.3e" : " %.15e" ;
454 <        int     i, j, k;
465 <
466 <        for (i = 0; i < rm->nrows; i++) {
467 <            for (j = 0; j < rm->ncols; j++) {
468 <                const double    *dp = rmx_val(rm,i,j);
469 <                for (k = 0; k < rm->ncomp; k++)
470 <                    fprintf(fp, fmt, dp[k]);
451 >        while (len-- > 0) {
452 >                int     k = nc;
453 >                while (k-- > 0)
454 >                        fprintf(fp, " %.7e", *dp++);
455                  fputc('\t', fp);
472            }
473            fputc('\n', fp);
456          }
457 <        return(1);
457 >        return(fputc('\n', fp) != EOF);
458   }
459  
460   static int
461 < rmx_write_float(const RMATRIX *rm, FILE *fp)
461 > rmx_write_float(const double *dp, int len, FILE *fp)
462   {
463 <        int     i, j, k;
482 <        float   val[100];
463 >        float   val;
464  
465 <        if (rm->ncomp > 100) {
466 <                fputs("Unsupported # components in rmx_write_float()\n", stderr);
467 <                exit(1);
487 <        }
488 <        for (i = 0; i < rm->nrows; i++)
489 <            for (j = 0; j < rm->ncols; j++) {
490 <                const double    *dp = rmx_val(rm,i,j);
491 <                for (k = rm->ncomp; k--; )
492 <                    val[k] = (float)dp[k];
493 <                if (putbinary(val, sizeof(float), rm->ncomp, fp) != rm->ncomp)
465 >        while (len--) {
466 >                val = *dp++;
467 >                if (putbinary(&val, sizeof(float), 1, fp) != 1)
468                          return(0);
469 <            }
469 >        }
470          return(1);
471   }
472  
473   static int
474 < rmx_write_double(const RMATRIX *rm, FILE *fp)
474 > rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp)
475   {
476 <        int     i;
476 >        COLR    *scan;
477 >        int     j;
478  
479 <        for (i = 0; i < rm->nrows; i++)
480 <                if (putbinary(rmx_val(rm,i,0), sizeof(double)*rm->ncomp,
481 <                                        rm->ncols, fp) != rm->ncols)
507 <                        return(0);
508 <        return(1);
509 < }
479 >        if ((nc != 1) & (nc != 3)) return(0);
480 >        scan = (COLR *)tempbuffer(sizeof(COLR)*len);
481 >        if (!scan) return(0);
482  
483 < static int
484 < rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
513 < {
514 <        COLR    *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols);
515 <        int     i, j;
516 <
517 <        if (!scan)
518 <                return(0);
519 <        for (i = 0; i < rm->nrows; i++) {
520 <            for (j = rm->ncols; j--; ) {
521 <                const double    *dp = rmx_val(rm,i,j);
522 <                if (rm->ncomp == 1)
483 >        for (j = 0; j < len; j++, dp += nc)
484 >                if (nc == 1)
485                          setcolr(scan[j], dp[0], dp[0], dp[0]);
486                  else
487                          setcolr(scan[j], dp[0], dp[1], dp[2]);
488 <            }
489 <            if (fwritecolrs(scan, rm->ncols, fp) < 0) {
528 <                free(scan);
529 <                return(0);
530 <            }
531 <        }
532 <        free(scan);
533 <        return(1);
488 >
489 >        return(fwritecolrs(scan, len, fp) >= 0);
490   }
491  
492   static int
493 < rmx_write_spec(const RMATRIX *rm, FILE *fp)
493 > rmx_write_spec(const double *dp, int nc, int len, FILE *fp)
494   {
495 <        uby8    *scan = (uby8 *)malloc((rm->ncomp+1)*rm->ncols);
540 <        int     ok = 1;
495 >        uby8    *scan;
496          SCOLOR  scol;
497 <        int     i, j, k;
497 >        int     j, k;
498  
499 <        if (!scan)
500 <                return(0);
501 <        for (i = 0; i < rm->nrows; i++) {
502 <            for (j = rm->ncols; j--; ) {
503 <                const double    *dp = rmx_val(rm,i,j);
549 <                for (k = rm->ncomp; k--; )
499 >        if (nc < 3) return(0);
500 >        scan = (uby8 *)tempbuffer((nc+1)*len);
501 >        if (!scan) return(0);
502 >        for (j = len; j--; dp += nc) {
503 >                for (k = nc; k--; )
504                          scol[k] = dp[k];
505 <                scolor2scolr(scan+j*(rm->ncomp+1), scol, rm->ncomp);
552 <            }
553 <            if (fwritescolrs(scan, rm->ncomp, rm->ncols, fp) < 0) {
554 <                ok = 0;
555 <                break;
556 <            }
505 >                scolor2scolr(scan+j*(nc+1), scol, nc);
506          }
507 <        free(scan);
559 <        return(ok);
507 >        return(fwritescolrs(scan, nc, len, fp) >= 0);
508   }
509  
510   /* Check if CIE XYZ primaries were specified */
# Line 576 | Line 524 | findCIEprims(const char *info)
524                          (prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01));
525   }
526  
527 < /* Write matrix to file type indicated by dtype */
527 > /* Finish writing header data with resolution and format, returning type used */
528   int
529 < rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
529 > rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp)
530   {
531 <        int     ok = 1;
584 <
585 <        if (!rm | !fp || !rm->mtx)
531 >        if (!rm | !fp || rm->ncols <= 0)
532                  return(0);
533 < #ifdef getc_unlocked
588 <        flockfile(fp);
589 < #endif
590 <        if (rm->info)                           /* complete header */
533 >        if (rm->info)
534                  fputs(rm->info, fp);
535          if (dtype == DTfromHeader)
536                  dtype = rm->dtype;
# Line 596 | Line 539 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
539                  dtype = DTxyze;
540          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
541                  dtype = DTrgbe;
542 +        if ((dtype == DTspec) & (rm->ncomp <= 3))
543 +                return(0);
544 +
545 +        if (dtype == DTascii)                   /* set file type (WINDOWS) */
546 +                SET_FILE_TEXT(fp);
547 +        else
548 +                SET_FILE_BINARY(fp);
549                                                  /* write exposure? */
550          if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) |
551                          (rm->cexp[GRN] != rm->cexp[BLU]))
552                  fputcolcor(rm->cexp, fp);
553          else if (rm->cexp[GRN] != 1.f)
554                  fputexpos(rm->cexp[GRN], fp);
555 <        if ((dtype != DTrgbe) & (dtype != DTxyze)) {
556 <                if (dtype != DTspec) {
555 >                                                /* matrix size? */
556 >        if ((dtype > DTspec) | (rm->nrows <= 0)) {
557 >                if (rm->nrows > 0)
558                          fprintf(fp, "NROWS=%d\n", rm->nrows);
559 <                        fprintf(fp, "NCOLS=%d\n", rm->ncols);
560 <                } else if (rm->ncomp < 3)
561 <                        return(0);              /* bad # components */
559 >                fprintf(fp, "NCOLS=%d\n", rm->ncols);
560 >        }
561 >        if (dtype >= DTspec) {                  /* # components & split? */
562                  fputncomp(rm->ncomp, fp);
563 <                if (dtype == DTspec || (rm->ncomp > 3 &&
564 <                                memcmp(rm->wlpart, WLPART, sizeof(WLPART))))
563 >                if (rm->ncomp > 3 &&
564 >                                memcmp(rm->wlpart, WLPART, sizeof(WLPART)))
565                          fputwlsplit(rm->wlpart, fp);
566          } else if ((rm->ncomp != 3) & (rm->ncomp != 1))
567                  return(0);                      /* wrong # components */
# Line 618 | Line 569 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
569                  fputendian(fp);                 /* important to record */
570          fputformat(cm_fmt_id[dtype], fp);
571          fputc('\n', fp);                        /* end of header */
572 <        switch (dtype) {                        /* write data */
572 >        if ((dtype <= DTspec) & (rm->nrows > 0))
573 >                fprtresolu(rm->ncols, rm->nrows, fp);
574 >        return(dtype);
575 > }
576 >
577 > /* Write out matrix data (usually by row) */
578 > int
579 > rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp)
580 > {
581 >        switch (dtype) {
582          case DTascii:
583 <                ok = rmx_write_ascii(rm, fp);
624 <                break;
583 >                return(rmx_write_ascii(dp, nc, len, fp));
584          case DTfloat:
585 <                ok = rmx_write_float(rm, fp);
627 <                break;
585 >                return(rmx_write_float(dp, nc*len, fp));
586          case DTdouble:
587 <                ok = rmx_write_double(rm, fp);
630 <                break;
587 >                return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len);
588          case DTrgbe:
589          case DTxyze:
590 <                fprtresolu(rm->ncols, rm->nrows, fp);
634 <                ok = rmx_write_rgbe(rm, fp);
635 <                break;
590 >                return(rmx_write_rgbe(dp, nc, len, fp));
591          case DTspec:
592 <                fprtresolu(rm->ncols, rm->nrows, fp);
638 <                ok = rmx_write_spec(rm, fp);
639 <                break;
640 <        default:
641 <                return(0);
592 >                return(rmx_write_spec(dp, nc, len, fp));
593          }
594 <        ok &= (fflush(fp) == 0);
594 >        return(0);
595 > }
596 >
597 > /* Write matrix using file format indicated by dtype */
598 > int
599 > rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
600 > {
601 >        int     ok = 0;
602 >        int     i;
603 >                                                /* complete header */
604 >        dtype = rmx_write_header(rm, dtype, fp);
605 >        if (dtype <= 0)
606 >                return(0);
607   #ifdef getc_unlocked
608 +        flockfile(fp);
609 + #endif
610 +        if (dtype == DTdouble)                  /* write all at once? */
611 +                ok = rmx_write_data(rm->mtx, rm->ncomp,
612 +                                rm->nrows*rm->ncols, dtype, fp);
613 +        else                                    /* else row by row */
614 +                for (i = 0; i < rm->nrows; i++) {
615 +                        ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp,
616 +                                        rm->ncols, dtype, fp);
617 +                        if (!ok) break;
618 +                }
619 +
620 +        if (ok) ok = (fflush(fp) == 0);
621 + #ifdef getc_unlocked
622          funlockfile(fp);
623   #endif
624          if (!ok) fputs("Error writing matrix\n", stderr);
# Line 666 | Line 643 | rmx_identity(const int dim, const int n)
643          return(rid);
644   }
645  
646 < /* Duplicate the given matrix */
646 > /* Duplicate the given matrix (may be unallocated) */
647   RMATRIX *
648   rmx_copy(const RMATRIX *rm)
649   {
# Line 674 | Line 651 | rmx_copy(const RMATRIX *rm)
651  
652          if (!rm)
653                  return(NULL);
654 <        dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
654 >        dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp);
655          if (!dnew)
656                  return(NULL);
657 +        if (rm->mtx) {
658 +                if (!rmx_prepare(dnew)) {
659 +                        rmx_free(dnew);
660 +                        return(NULL);
661 +                }
662 +                memcpy(dnew->mtx, rm->mtx, array_size(dnew));
663 +        }
664          rmx_addinfo(dnew, rm->info);
665          dnew->dtype = rm->dtype;
666          copycolor(dnew->cexp, rm->cexp);
667          memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
684        memcpy(dnew->mtx, rm->mtx, array_size(dnew));
668          return(dnew);
669   }
670  
671 + /* Replace data in first matrix with data from second */
672 + int
673 + rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa)
674 + {
675 +        if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) |
676 +                        (rdst->ncols != rsrc->ncols) |
677 +                        (rdst->ncomp != rsrc->ncomp))
678 +                return(0);
679 +
680 +        if (dometa) {           /* transfer everything? */
681 +                rmx_reset(rdst);
682 +                *rdst = *rsrc;
683 +                rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL;
684 +                return(1);
685 +        }
686 +        if (rdst->mapped)
687 +                return(0);      /* XXX can't handle this case */
688 +                                /* just matrix data -- leave metadata */
689 +        if (rdst->mtx) free(rdst->mtx);
690 +        rdst->mtx = rsrc->mtx;
691 +        rsrc->mtx = NULL;
692 +        return(1);
693 + }
694 +
695   /* Allocate and assign transposed matrix */
696   RMATRIX *
697   rmx_transpose(const RMATRIX *rm)
# Line 692 | Line 699 | rmx_transpose(const RMATRIX *rm)
699          RMATRIX *dnew;
700          int     i, j;
701  
702 <        if (!rm)
702 >        if (!rm || !rm->mtx)
703                  return(0);
704          if ((rm->nrows == 1) | (rm->ncols == 1)) {
705                  dnew = rmx_copy(rm);
# Line 726 | Line 733 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
733          RMATRIX *mres;
734          int     i, j, k, h;
735  
736 <        if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
736 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
737 >                        (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
738                  return(NULL);
739          mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
740          if (!mres)
# Line 754 | Line 762 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
762          int     zeroDivides = 0;
763          int     i, j, k;
764  
765 <        if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
765 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
766 >                         (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
767                  return(0);
768          if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
769                  return(0);
# Line 810 | Line 819 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
819          double  *mysf = NULL;
820          int     i, j, k;
821  
822 <        if (!msum | !madd ||
822 >        if (!msum | !madd || !msum->mtx | !madd->mtx |
823                          (msum->nrows != madd->nrows) |
824                          (msum->ncols != madd->ncols) |
825                          (msum->ncomp != madd->ncomp))
# Line 846 | Line 855 | rmx_scale(RMATRIX *rm, const double sf[])
855   {
856          int     i, j, k;
857  
858 <        if (!rm | !sf)
858 >        if (!rm | !sf || !rm->mtx)
859                  return(0);
860          for (i = rm->nrows; i--; )
861              for (j = rm->ncols; j--; ) {
# Line 867 | Line 876 | rmx_transform(const RMATRIX *msrc, int n, const double
876          int     i, j, ks, kd;
877          RMATRIX *dnew;
878  
879 <        if (!msrc | (n <= 0) | !cmat)
879 >        if (!msrc | (n <= 0) | !cmat || !msrc->mtx)
880                  return(NULL);
881          dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
882          if (!dnew)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines