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.66 by greg, Tue Nov 28 21:28:11 2023 UTC vs.
Revision 2.69 by greg, Mon Dec 4 22:02:40 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 265 | Line 281 | rmx_load_header(RMATRIX *rm, FILE *fp)
281   {
282          if (!rm | !fp)
283                  return(0);
284 <        if (rm->info) {                         /* clear state */
269 <                free(rm->info);
270 <                rm->info = NULL;
271 <        }
272 <        if (rm->mtx) {                          /* ...and data */
273 < #ifdef MAP_FILE
274 <                if (rm->mapped) {
275 <                        munmap(rm->mapped, mapped_size(rm));
276 <                        rm->mapped = NULL;
277 <                } else
278 < #endif
279 <                        free(rm->mtx);
280 <                rm->mtx = NULL;
281 <        }
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 286 | Line 289 | rmx_load_header(RMATRIX *rm, FILE *fp)
289                  memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart));
290                  rm->swapin = 0;
291          }
289        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  
# Line 348 | Line 352 | rmx_load_data(RMATRIX *rm, FILE *fp)
352                                  (1./(1L<<20))*(double)array_size(rm));
353                  return(0);
354          }
351        if (rm->dtype == DTascii)
352                SET_FILE_TEXT(fp);
355          for (i = 0; i < rm->nrows; i++)
356                  if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp))
357                          return(0);
# Line 372 | Line 374 | rmx_load(const char *inspec, RMPref rmp)
374                  fp = stdin;
375          else if (inspec[0] == '!')
376                  fp = popen(inspec+1, "r");
377 <        else {
377 >        else if (rmp != RMPnone) {
378                  const char      *sp = inspec;   /* check suffix */
379                  while (*sp)
380                          ++sp;
# Line 395 | Line 397 | rmx_load(const char *inspec, RMPref rmp)
397   #ifdef getc_unlocked
398          flockfile(fp);
399   #endif
400 <                                                /* load header info */
400 >        SET_FILE_BINARY(fp);                    /* load header info */
401          if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) {
402                  fprintf(stderr, "Bad header in: %s\n", inspec);
403                  if (inspec[0] == '!') pclose(fp);
# Line 443 | Line 445 | rmx_load(const char *inspec, RMPref rmp)
445   }
446  
447   static int
448 < rmx_write_ascii(const RMATRIX *rm, FILE *fp)
448 > rmx_write_ascii(const double *dp, int nc, int len, FILE *fp)
449   {
450 <        const char      *fmt = (rm->dtype == DTfloat) ? " %.7e" :
451 <                        (rm->dtype == DTrgbe) | (rm->dtype == DTxyze) |
452 <                        (rm->dtype == DTspec) ? " %.3e" : " %.15e" ;
453 <        int     i, j, k;
452 <
453 <        for (i = 0; i < rm->nrows; i++) {
454 <            for (j = 0; j < rm->ncols; j++) {
455 <                const double    *dp = rmx_val(rm,i,j);
456 <                for (k = 0; k < rm->ncomp; k++)
457 <                    fprintf(fp, fmt, dp[k]);
450 >        while (len-- > 0) {
451 >                int     k = nc;
452 >                while (nc-- > 0)
453 >                        fprintf(fp, " %.7e", *dp++);
454                  fputc('\t', fp);
459            }
460            fputc('\n', fp);
455          }
456 <        return(1);
456 >        return(fputc('\n', fp) != EOF);
457   }
458  
459   static int
460 < rmx_write_float(const RMATRIX *rm, FILE *fp)
460 > rmx_write_float(const double *dp, int len, FILE *fp)
461   {
462 <        int     i, j, k;
469 <        float   val[100];
462 >        float   val;
463  
464 <        if (rm->ncomp > 100) {
465 <                fputs("Unsupported # components in rmx_write_float()\n", stderr);
466 <                exit(1);
474 <        }
475 <        for (i = 0; i < rm->nrows; i++)
476 <            for (j = 0; j < rm->ncols; j++) {
477 <                const double    *dp = rmx_val(rm,i,j);
478 <                for (k = rm->ncomp; k--; )
479 <                    val[k] = (float)dp[k];
480 <                if (putbinary(val, sizeof(float), rm->ncomp, fp) != rm->ncomp)
464 >        while (len--) {
465 >                val = *dp++;
466 >                if (putbinary(&val, sizeof(float), 1, fp) != 1)
467                          return(0);
468 <            }
468 >        }
469          return(1);
470   }
471  
472   static int
473 < rmx_write_double(const RMATRIX *rm, FILE *fp)
473 > rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp)
474   {
475 <        return(putbinary(rm->mtx, sizeof(rm->mtx[0])*rm->ncomp,
476 <                        rm->nrows*rm->ncols, fp) == rm->nrows*rm->ncols);
491 < }
475 >        COLR    *scan;
476 >        int     j;
477  
478 < static int
479 < rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
480 < {
496 <        COLR    *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols);
497 <        int     i, j;
478 >        if ((nc != 1) & (nc != 3)) return(0);
479 >        scan = (COLR *)tempbuffer(sizeof(COLR)*len);
480 >        if (!scan) return(0);
481  
482 <        if (!scan)
483 <                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);
504 <                if (rm->ncomp == 1)
482 >        for (j = 0; j < len; j++, dp += nc)
483 >                if (nc == 1)
484                          setcolr(scan[j], dp[0], dp[0], dp[0]);
485                  else
486                          setcolr(scan[j], dp[0], dp[1], dp[2]);
487 <            }
488 <            if (fwritecolrs(scan, rm->ncols, fp) < 0) {
510 <                free(scan);
511 <                return(0);
512 <            }
513 <        }
514 <        free(scan);
515 <        return(1);
487 >
488 >        return(fwritecolrs(scan, len, fp) >= 0);
489   }
490  
491   static int
492 < rmx_write_spec(const RMATRIX *rm, FILE *fp)
492 > rmx_write_spec(const double *dp, int nc, int len, FILE *fp)
493   {
494 <        uby8    *scan = (uby8 *)malloc((rm->ncomp+1)*rm->ncols);
522 <        int     ok = 1;
494 >        uby8    *scan;
495          SCOLOR  scol;
496 <        int     i, j, k;
496 >        int     j, k;
497  
498 <        if (!scan)
499 <                return(0);
500 <        for (i = 0; i < rm->nrows; i++) {
501 <            for (j = rm->ncols; j--; ) {
502 <                const double    *dp = rmx_val(rm,i,j);
531 <                for (k = rm->ncomp; k--; )
498 >        if (nc < 3) return(0);
499 >        scan = (uby8 *)tempbuffer((nc+1)*len);
500 >        if (!scan) return(0);
501 >        for (j = len; j--; dp += nc) {
502 >                for (k = nc; k--; )
503                          scol[k] = dp[k];
504 <                scolor2scolr(scan+j*(rm->ncomp+1), scol, rm->ncomp);
534 <            }
535 <            if (fwritescolrs(scan, rm->ncomp, rm->ncols, fp) < 0) {
536 <                ok = 0;
537 <                break;
538 <            }
504 >                scolor2scolr(scan+j*(nc+1), scol, nc);
505          }
506 <        free(scan);
541 <        return(ok);
506 >        return(fwritescolrs(scan, nc, len, fp) >= 0);
507   }
508  
509   /* Check if CIE XYZ primaries were specified */
# Line 558 | Line 523 | findCIEprims(const char *info)
523                          (prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01));
524   }
525  
526 < /* Write matrix to file type indicated by dtype */
526 > /* Finish writing header data with resolution and format, returning type used */
527   int
528 < rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
528 > rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp)
529   {
530 <        int     ok = 1;
566 <
567 <        if (!rm | !fp || !rm->mtx)
530 >        if (!rm | !fp || !rm->mtx | (rm->ncols <= 0))
531                  return(0);
532 < #ifdef getc_unlocked
570 <        flockfile(fp);
571 < #endif
572 <        if (rm->info)                           /* complete header */
532 >        if (rm->info)
533                  fputs(rm->info, fp);
534          if (dtype == DTfromHeader)
535                  dtype = rm->dtype;
# Line 578 | Line 538 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
538                  dtype = DTxyze;
539          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
540                  dtype = DTrgbe;
541 +        if ((dtype == DTspec) & (rm->ncomp < 3))
542 +                return(0);
543 +
544 +        if (dtype == DTascii)                   /* set file type (WINDOWS) */
545 +                SET_FILE_TEXT(fp);
546 +        else
547 +                SET_FILE_BINARY(fp);
548                                                  /* write exposure? */
549          if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) |
550                          (rm->cexp[GRN] != rm->cexp[BLU]))
551                  fputcolcor(rm->cexp, fp);
552          else if (rm->cexp[GRN] != 1.f)
553                  fputexpos(rm->cexp[GRN], fp);
554 <        if ((dtype != DTrgbe) & (dtype != DTxyze)) {
555 <                if (dtype != DTspec) {
554 >                                                /* matrix size? */
555 >        if ((dtype > DTspec) | (rm->nrows <= 0)) {
556 >                if (rm->nrows > 0)
557                          fprintf(fp, "NROWS=%d\n", rm->nrows);
558 <                        fprintf(fp, "NCOLS=%d\n", rm->ncols);
559 <                } else if (rm->ncomp < 3)
560 <                        return(0);              /* bad # components */
558 >                fprintf(fp, "NCOLS=%d\n", rm->ncols);
559 >        }
560 >        if (dtype >= DTspec) {                  /* # components & split? */
561                  fputncomp(rm->ncomp, fp);
562                  if (dtype == DTspec || (rm->ncomp > 3 &&
563                                  memcmp(rm->wlpart, WLPART, sizeof(WLPART))))
# Line 600 | Line 568 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
568                  fputendian(fp);                 /* important to record */
569          fputformat(cm_fmt_id[dtype], fp);
570          fputc('\n', fp);                        /* end of header */
571 <        switch (dtype) {                        /* write data */
571 >        if ((dtype <= DTspec) & (rm->nrows > 0))
572 >                fprtresolu(rm->ncols, rm->nrows, fp);
573 >        return(dtype);
574 > }
575 >
576 > /* Write out matrix data (usually by row) */
577 > int
578 > rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp)
579 > {
580 >        switch (dtype) {
581          case DTascii:
582 <                ok = rmx_write_ascii(rm, fp);
606 <                break;
582 >                return(rmx_write_ascii(dp, nc, len, fp));
583          case DTfloat:
584 <                ok = rmx_write_float(rm, fp);
609 <                break;
584 >                return(rmx_write_float(dp, nc*len, fp));
585          case DTdouble:
586 <                ok = rmx_write_double(rm, fp);
612 <                break;
586 >                return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len);
587          case DTrgbe:
588          case DTxyze:
589 <                fprtresolu(rm->ncols, rm->nrows, fp);
616 <                ok = rmx_write_rgbe(rm, fp);
617 <                break;
589 >                return(rmx_write_rgbe(dp, nc, len, fp));
590          case DTspec:
591 <                fprtresolu(rm->ncols, rm->nrows, fp);
620 <                ok = rmx_write_spec(rm, fp);
621 <                break;
622 <        default:
623 <                return(0);
591 >                return(rmx_write_spec(dp, nc, len, fp));
592          }
593 <        ok &= (fflush(fp) == 0);
593 >        return(0);
594 > }
595 >
596 > /* Write matrix using file format indicated by dtype */
597 > int
598 > rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
599 > {
600 >        int     ok = 0;
601 >        int     i;
602 >                                                /* complete header */
603 >        dtype = rmx_write_header(rm, dtype, fp);
604 >        if (dtype <= 0)
605 >                return(0);
606   #ifdef getc_unlocked
607 +        flockfile(fp);
608 + #endif
609 +        if (dtype == DTdouble)                  /* write all at once? */
610 +                ok = rmx_write_data(rm->mtx, rm->ncomp,
611 +                                rm->nrows*rm->ncols, dtype, fp);
612 +        else                                    /* else row by row */
613 +                for (i = 0; i < rm->nrows; i++) {
614 +                        ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp,
615 +                                        rm->ncols, dtype, fp);
616 +                        if (!ok) break;
617 +                }
618 +
619 +        if (ok) ok = (fflush(fp) == 0);
620 + #ifdef getc_unlocked
621          funlockfile(fp);
622   #endif
623          if (!ok) fputs("Error writing matrix\n", stderr);
# Line 648 | Line 642 | rmx_identity(const int dim, const int n)
642          return(rid);
643   }
644  
645 < /* Duplicate the given matrix */
645 > /* Duplicate the given matrix (may be unallocated) */
646   RMATRIX *
647   rmx_copy(const RMATRIX *rm)
648   {
# Line 656 | Line 650 | rmx_copy(const RMATRIX *rm)
650  
651          if (!rm)
652                  return(NULL);
653 <        dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
653 >        dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp);
654          if (!dnew)
655                  return(NULL);
656 +        if (rm->mtx) {
657 +                if (!rmx_prepare(dnew)) {
658 +                        rmx_free(dnew);
659 +                        return(NULL);
660 +                }
661 +                memcpy(dnew->mtx, rm->mtx, array_size(dnew));
662 +        }
663          rmx_addinfo(dnew, rm->info);
664          dnew->dtype = rm->dtype;
665          copycolor(dnew->cexp, rm->cexp);
666          memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
666        memcpy(dnew->mtx, rm->mtx, array_size(dnew));
667          return(dnew);
668   }
669  
# Line 674 | Line 674 | rmx_transpose(const RMATRIX *rm)
674          RMATRIX *dnew;
675          int     i, j;
676  
677 <        if (!rm)
677 >        if (!rm || !rm->mtx)
678                  return(0);
679          if ((rm->nrows == 1) | (rm->ncols == 1)) {
680                  dnew = rmx_copy(rm);
# Line 708 | Line 708 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
708          RMATRIX *mres;
709          int     i, j, k, h;
710  
711 <        if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
711 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
712 >                        (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
713                  return(NULL);
714          mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
715          if (!mres)
# Line 736 | Line 737 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
737          int     zeroDivides = 0;
738          int     i, j, k;
739  
740 <        if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
740 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
741 >                         (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
742                  return(0);
743          if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
744                  return(0);
# Line 792 | Line 794 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
794          double  *mysf = NULL;
795          int     i, j, k;
796  
797 <        if (!msum | !madd ||
797 >        if (!msum | !madd || !msum->mtx | !madd->mtx |
798                          (msum->nrows != madd->nrows) |
799                          (msum->ncols != madd->ncols) |
800                          (msum->ncomp != madd->ncomp))
# Line 828 | Line 830 | rmx_scale(RMATRIX *rm, const double sf[])
830   {
831          int     i, j, k;
832  
833 <        if (!rm | !sf)
833 >        if (!rm | !sf || !rm->mtx)
834                  return(0);
835          for (i = rm->nrows; i--; )
836              for (j = rm->ncols; j--; ) {
# Line 849 | Line 851 | rmx_transform(const RMATRIX *msrc, int n, const double
851          int     i, j, ks, kd;
852          RMATRIX *dnew;
853  
854 <        if (!msrc | (n <= 0) | !cmat)
854 >        if (!msrc | (n <= 0) | !cmat || !msrc->mtx)
855                  return(NULL);
856          dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
857          if (!dnew)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines