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.55 by greg, Sun Mar 6 17:21:49 2022 UTC vs.
Revision 2.80 by greg, Tue Jun 4 21:23:11 2024 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include <sys/mman.h>
17   #endif
18  
19 < static char     rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
19 > static const char       rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
20  
21 #define array_size(rm)  (sizeof(double)*(rm)->nrows*(rm)->ncols*(rm)->ncomp)
22 #define mapped_size(rm) ((char *)(rm)->mtx + array_size(rm) - (char *)(rm)->mapped)
23
21   /* Initialize a RMATRIX struct but don't allocate array space */
22   RMATRIX *
23   rmx_new(int nr, int nc, int n)
24   {
25 <        RMATRIX *dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX));
25 >        RMATRIX *dnew;
26  
27 <        if (dnew) {
28 <                dnew->dtype = DTdouble;
29 <                dnew->nrows = nr;
30 <                dnew->ncols = nc;
31 <                dnew->ncomp = n;
32 <        }
27 >        if (n <= 0)
28 >                return(NULL);
29 >
30 >        dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX));
31 >        if (!dnew)
32 >                return(NULL);
33 >
34 >        dnew->dtype = DTrmx_native;
35 >        dnew->nrows = nr;
36 >        dnew->ncols = nc;
37 >        dnew->ncomp = n;
38 >        setcolor(dnew->cexp, 1.f, 1.f, 1.f);
39 >        memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart));
40 >
41          return(dnew);
42   }
43  
# Line 43 | Line 48 | rmx_prepare(RMATRIX *rm)
48          if (!rm) return(0);
49          if (rm->mtx)
50                  return(1);
51 <        rm->mtx = (double *)malloc(array_size(rm));
51 >        if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0))
52 >                return(0);
53 >        rm->mtx = (double *)malloc(rmx_array_size(rm));
54 >        rm->pflags |= RMF_OURMEM;
55          return(rm->mtx != NULL);
56   }
57  
# Line 60 | Line 68 | rmx_alloc(int nr, int nc, int n)
68          return(dnew);
69   }
70  
71 < /* Free a RMATRIX array */
71 > /* Clear state by freeing info and matrix data */
72   void
73 < rmx_free(RMATRIX *rm)
73 > rmx_reset(RMATRIX *rm)
74   {
75          if (!rm) return;
76 <        if (rm->info)
76 >        if (rm->info) {
77                  free(rm->info);
78 +                rm->info = NULL;
79 +        }
80 +        if (rm->mtx && rm->pflags & RMF_OURMEM) {
81   #ifdef MAP_FILE
82 <        if (rm->mapped)
83 <                munmap(rm->mapped, mapped_size(rm));
84 <        else
82 >                if (rm->mapped) {
83 >                        munmap(rm->mapped, rmx_mapped_size(rm));
84 >                        rm->mapped = NULL;
85 >                } else
86   #endif
87 <                free(rm->mtx);
87 >                        free(rm->mtx);
88 >                rm->pflags &= ~RMF_OURMEM;
89 >        }
90 >        rm->mtx = NULL;
91 > }
92 >
93 > /* Free an RMATRIX struct and data */
94 > void
95 > rmx_free(RMATRIX *rm)
96 > {
97 >        if (!rm) return;
98 >        rmx_reset(rm);
99          free(rm);
100   }
101  
# Line 80 | Line 103 | rmx_free(RMATRIX *rm)
103   int
104   rmx_newtype(int dtyp1, int dtyp2)
105   {
106 <        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) |
107 <                        (dtyp2==DTxyze) | (dtyp2==DTrgbe)
106 >        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) | (dtyp1==DTspec) |
107 >                        (dtyp2==DTxyze) | (dtyp2==DTrgbe) | (dtyp2==DTspec)
108                          && dtyp1 != dtyp2)
109                  return(0);
110          if (dtyp1 < dtyp2)
# Line 118 | Line 141 | get_dminfo(char *s, void *p)
141          char    fmt[MAXFMTLEN];
142          int     i;
143  
144 <        if (headidval(fmt, s))
144 >        if (headidval(NULL, s))
145                  return(0);
146 <        if (!strncmp(s, "NCOMP=", 6)) {
147 <                ip->ncomp = atoi(s+6);
146 >        if (isncomp(s)) {
147 >                ip->ncomp = ncompval(s);
148                  return(0);
149          }
150          if (!strncmp(s, "NROWS=", 6)) {
# Line 133 | Line 156 | get_dminfo(char *s, void *p)
156                  return(0);
157          }
158          if ((i = isbigendian(s)) >= 0) {
159 <                ip->swapin = (nativebigendian() != i);
159 >                if (nativebigendian() != i)
160 >                        ip->pflags |= RMF_SWAPIN;
161 >                else
162 >                        ip->pflags &= ~RMF_SWAPIN;
163                  return(0);
164          }
165          if (isexpos(s)) {
# Line 147 | Line 173 | get_dminfo(char *s, void *p)
173                  multcolor(ip->cexp, ctmp);
174                  return(0);
175          }
176 +        if (iswlsplit(s)) {
177 +                wlsplitval(ip->wlpart, s);
178 +                return(0);
179 +        }
180          if (!formatval(fmt, s)) {
181                  rmx_addinfo(ip, s);
182                  return(0);
# Line 160 | Line 190 | get_dminfo(char *s, void *p)
190   }
191  
192   static int
193 < rmx_load_ascii(RMATRIX *rm, FILE *fp)
193 > rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *fp)
194   {
195 <        int     i, j, k;
195 >        int     j, k;
196  
197 <        if (!rmx_prepare(rm))
198 <                return(0);
199 <        for (i = 0; i < rm->nrows; i++)
200 <            for (j = 0; j < rm->ncols; j++) {
171 <                double  *dp = rmx_lval(rm,i,j);
172 <                for (k = 0; k < rm->ncomp; k++)
173 <                    if (fscanf(fp, "%lf", &dp[k]) != 1)
174 <                        return(0);
175 <            }
197 >        for (j = 0; j < rm->ncols; j++)
198 >                for (k = rm->ncomp; k-- > 0; )
199 >                        if (fscanf(fp, "%lf", drp++) != 1)
200 >                                return(0);
201          return(1);
202   }
203  
204   static int
205 < rmx_load_float(RMATRIX *rm, FILE *fp)
205 > rmx_load_float(double *drp, const RMATRIX *rm, FILE *fp)
206   {
207 <        int     i, j, k;
207 >        int     j, k;
208          float   val[100];
209  
210          if (rm->ncomp > 100) {
211                  fputs("Unsupported # components in rmx_load_float()\n", stderr);
212                  exit(1);
213          }
214 <        if (!rmx_prepare(rm))
190 <                return(0);
191 <        for (i = 0; i < rm->nrows; i++)
192 <            for (j = 0; j < rm->ncols; j++) {
193 <                double  *dp = rmx_lval(rm,i,j);
214 >        for (j = 0; j < rm->ncols; j++) {
215                  if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
216 <                    return(0);
217 <                if (rm->swapin)
218 <                    swap32((char *)val, rm->ncomp);
219 <                for (k = rm->ncomp; k--; )
220 <                     dp[k] = val[k];
221 <            }
216 >                        return(0);
217 >                if (rm->pflags & RMF_SWAPIN)
218 >                        swap32((char *)val, rm->ncomp);
219 >                for (k = 0; k < rm->ncomp; k++)
220 >                        *drp++ = val[k];
221 >        }
222          return(1);
223   }
224  
225   static int
226 < rmx_load_double(RMATRIX *rm, FILE *fp)
226 > rmx_load_double(double *drp, const RMATRIX *rm, FILE *fp)
227   {
228 <        int     i;
208 < #ifdef MAP_FILE
209 <        long    pos;            /* map memory to file if possible */
210 <        if (!rm->swapin && array_size(rm) >= 1L<<20 &&
211 <                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) {
212 <                rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE,
213 <                                        MAP_PRIVATE, fileno(fp), 0);
214 <                if (rm->mapped != MAP_FAILED) {
215 <                        rm->mtx = (double *)rm->mapped + pos/sizeof(double);
216 <                        return(1);
217 <                }
218 <                rm->mapped = NULL;
219 <        }
220 < #endif
221 <        if (!rmx_prepare(rm))
228 >        if (DTrmx_native != DTdouble)
229                  return(0);
230 <        for (i = 0; i < rm->nrows; i++) {
231 <                if (getbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp,
232 <                                        rm->ncols, fp) != rm->ncols)
233 <                        return(0);
234 <                if (rm->swapin)
235 <                        swap64((char *)rmx_lval(rm,i,0), rm->ncols*rm->ncomp);
230 >        if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols)
231 >                return(0);
232 >        if (rm->pflags & RMF_SWAPIN)
233 >                swap64((char *)drp, rm->ncols*rm->ncomp);
234 >        return(1);
235 > }
236 >
237 > static int
238 > rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp)
239 > {
240 >        COLR    *scan;
241 >        COLOR   col;
242 >        int     j;
243 >
244 >        if (rm->ncomp != 3)
245 >                return(0);
246 >        scan = (COLR *)tempbuffer(sizeof(COLR)*rm->ncols);
247 >        if (!scan)
248 >                return(0);
249 >        if (freadcolrs(scan, rm->ncols, fp) < 0)
250 >                return(0);
251 >        for (j = 0; j < rm->ncols; j++) {
252 >                colr_color(col, scan[j]);
253 >                *drp++ = colval(col,RED);
254 >                *drp++ = colval(col,GRN);
255 >                *drp++ = colval(col,BLU);
256          }
257          return(1);
258   }
259  
260   static int
261 < rmx_load_rgbe(RMATRIX *rm, FILE *fp)
261 > rmx_load_spec(double *drp, const RMATRIX *rm, FILE *fp)
262   {
263 <        COLOR   *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
264 <        int     i, j;
263 >        uby8    *scan;
264 >        SCOLOR  scol;
265 >        int     j, k;
266  
267 +        if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP))
268 +                return(0);
269 +        scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols);
270          if (!scan)
271                  return(0);
272 <        if (!rmx_prepare(rm))
272 >        if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0)
273                  return(0);
274 <        for (i = 0; i < rm->nrows; i++) {
275 <            double      *dp = rmx_lval(rm,i,j);
276 <            if (freadscan(scan, rm->ncols, fp) < 0) {
277 <                free(scan);
274 >        for (j = 0; j < rm->ncols; j++) {
275 >                scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp);
276 >                for (k = 0; k < rm->ncomp; k++)
277 >                        *drp++ = scol[k];
278 >        }
279 >        return(1);
280 > }
281 >
282 > /* Read matrix header from input stream (cannot be XML) */
283 > int
284 > rmx_load_header(RMATRIX *rm, FILE *fp)
285 > {
286 >        if (!rm | !fp)
287                  return(0);
288 <            }
289 <            for (j = 0; j < rm->ncols; j++, dp += 3) {
290 <                dp[0] = colval(scan[j],RED);
291 <                dp[1] = colval(scan[j],GRN);
292 <                dp[2] = colval(scan[j],BLU);
293 <            }
288 >        rmx_reset(rm);                          /* clear state */
289 >        if (rm->nrows | rm->ncols | !rm->dtype) {
290 >                rm->nrows = rm->ncols = 0;
291 >                rm->ncomp = 3;
292 >                setcolor(rm->cexp, 1.f, 1.f, 1.f);
293 >                memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart));
294 >                rm->pflags = 0;
295          }
296 <        free(scan);
296 >        rm->dtype = DTascii;                    /* assumed w/o FORMAT */
297 >        if (getheader(fp, get_dminfo, rm) < 0) {
298 >                fputs("Unrecognized matrix format\n", stderr);
299 >                return(0);
300 >        }
301 >        if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
302 >                        rm->ncomp != 3)
303 >                return(0);
304 >        if (rm->ncols <= 0 &&                   /* resolution string? */
305 >                        !fscnresolu(&rm->ncols, &rm->nrows, fp))
306 >                return(0);
307 >        if (rm->dtype == DTascii)               /* set file type (WINDOWS) */
308 >                SET_FILE_TEXT(fp);
309 >        else
310 >                SET_FILE_BINARY(fp);
311          return(1);
312   }
313  
314 + /* Load next row as double (cannot be XML) */
315 + int
316 + rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp)
317 + {
318 +        switch (rm->dtype) {
319 +        case DTascii:
320 +                return(rmx_load_ascii(drp, rm, fp));
321 +        case DTfloat:
322 +                return(rmx_load_float(drp, rm, fp));
323 +        case DTdouble:
324 +                return(rmx_load_double(drp, rm, fp));
325 +        case DTrgbe:
326 +        case DTxyze:
327 +                return(rmx_load_rgbe(drp, rm, fp));
328 +        case DTspec:
329 +                return(rmx_load_spec(drp, rm, fp));
330 +        default:
331 +                fputs("Unsupported data type in rmx_load_row()\n", stderr);
332 +        }
333 +        return(0);
334 + }
335 +
336 + /* Allocate & load post-header data from stream given type set in rm->dtype */
337 + int
338 + rmx_load_data(RMATRIX *rm, FILE *fp)
339 + {
340 +        int     i;
341 + #ifdef MAP_FILE
342 +        long    pos;            /* map memory for file > 1MB if possible */
343 +        if ((rm->dtype == DTrmx_native) & !(rm->pflags & RMF_SWAPIN) &&
344 +                        rmx_array_size(rm) >= 1L<<20 &&
345 +                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) {
346 +                rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE,
347 +                                        MAP_PRIVATE, fileno(fp), 0);
348 +                if (rm->mapped != MAP_FAILED) {
349 +                        rm->mtx = (double *)rm->mapped + pos/sizeof(double);
350 +                        rm->pflags |= RMF_OURMEM;
351 +                        return(1);
352 +                }               /* else fall back on reading into memory */
353 +                rm->mapped = NULL;
354 +        }
355 + #endif
356 +        if (!rmx_prepare(rm)) { /* need in-core matrix array */
357 +                fprintf(stderr, "Cannot allocate %g MByte matrix array\n",
358 +                                (1./(1L<<20))*(double)rmx_array_size(rm));
359 +                return(0);
360 +        }
361 +        for (i = 0; i < rm->nrows; i++)
362 +                if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp))
363 +                        return(0);
364 +        return(1);
365 + }
366 +
367   /* Load matrix from supported file type */
368   RMATRIX *
369   rmx_load(const char *inspec, RMPref rmp)
370   {
371          FILE            *fp;
372          RMATRIX         *dnew;
373 +        int             ok;
374  
375          if (!inspec)
376                  inspec = stdin_name;
377          else if (!*inspec)
378                  return(NULL);
379 <        if (inspec == stdin_name) {             /* reading from stdin? */
379 >        if (inspec == stdin_name)               /* reading from stdin? */
380                  fp = stdin;
381 <        } else if (inspec[0] == '!') {
382 <                if (!(fp = popen(inspec+1, "r")))
383 <                        return(NULL);
275 <        } else {
381 >        else if (inspec[0] == '!')
382 >                fp = popen(inspec+1, "r");
383 >        else {
384                  const char      *sp = inspec;   /* check suffix */
385                  while (*sp)
386                          ++sp;
387                  while (sp > inspec && sp[-1] != '.')
388                          --sp;
389                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
390 <                        CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) :
390 >                        CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL :
391 >                                        rmp==RMPtrans ? cm_loadBTDF(inspec) :
392                                          cm_loadBRDF(inspec, rmp==RMPreflB) ;
393                          if (!cm)
394                                  return(NULL);
395                          dnew = rmx_from_cmatrix(cm);
396                          cm_free(cm);
397                          dnew->dtype = DTascii;
398 <                        return(dnew);
399 <                }
400 <                                                /* else open it ourselves */
292 <                if (!(fp = fopen(inspec, "r")))
293 <                        return(NULL);
398 >                        return(dnew);           /* return here */
399 >                }                               /* else open it ourselves */
400 >                fp = fopen(inspec, "r");
401          }
402 <        SET_FILE_BINARY(fp);
402 >        if (!fp) {
403 >                fprintf(stderr, "Cannot open for reading: %s\n", inspec);
404 >                return(NULL);
405 >        }
406   #ifdef getc_unlocked
407          flockfile(fp);
408   #endif
409 <        if (!(dnew = rmx_new(0,0,3))) {
410 <                fclose(fp);
409 >        SET_FILE_BINARY(fp);                    /* load header info */
410 >        if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) {
411 >                fprintf(stderr, "Bad header in: %s\n", inspec);
412 >                if (inspec[0] == '!') pclose(fp);
413 >                else fclose(fp);
414 >                rmx_free(dnew);
415                  return(NULL);
416          }
417 <        dnew->dtype = DTascii;                  /* assumed w/o FORMAT */
418 <        dnew->cexp[0] = dnew->cexp[1] = dnew->cexp[2] = 1.f;
419 <        if (getheader(fp, get_dminfo, dnew) < 0) {
306 <                fclose(fp);
307 <                return(NULL);
308 <        }
309 <        if ((dnew->nrows <= 0) | (dnew->ncols <= 0)) {
310 <                if (!fscnresolu(&dnew->ncols, &dnew->nrows, fp)) {
311 <                        fclose(fp);
312 <                        return(NULL);
313 <                }
314 <                if ((dnew->dtype == DTrgbe) | (dnew->dtype == DTxyze) &&
315 <                                dnew->ncomp != 3) {
316 <                        fclose(fp);
317 <                        return(NULL);
318 <                }
319 <        }
320 <        switch (dnew->dtype) {
321 <        case DTascii:
322 <                SET_FILE_TEXT(fp);
323 <                if (!rmx_load_ascii(dnew, fp))
324 <                        goto loaderr;
325 <                dnew->dtype = DTascii;          /* should leave double? */
326 <                break;
327 <        case DTfloat:
328 <                if (!rmx_load_float(dnew, fp))
329 <                        goto loaderr;
330 <                dnew->dtype = DTfloat;
331 <                break;
332 <        case DTdouble:
333 <                if (!rmx_load_double(dnew, fp))
334 <                        goto loaderr;
335 <                dnew->dtype = DTdouble;
336 <                break;
337 <        case DTrgbe:
338 <        case DTxyze:
339 <                if (!rmx_load_rgbe(dnew, fp))
340 <                        goto loaderr;
341 <                                                /* undo exposure? */
342 <                if ((dnew->cexp[0] != 1.f) | (dnew->cexp[1] != 1.f) |
343 <                                (dnew->cexp[2] != 1.f)) {
344 <                        double  cmlt[3];
345 <                        cmlt[0] = 1./dnew->cexp[0];
346 <                        cmlt[1] = 1./dnew->cexp[1];
347 <                        cmlt[2] = 1./dnew->cexp[2];
348 <                        rmx_scale(dnew, cmlt);
349 <                }
350 <                dnew->swapin = 0;
351 <                break;
352 <        default:
353 <                goto loaderr;
354 <        }
355 <        if (fp != stdin) {
417 >        ok = rmx_load_data(dnew, fp);           /* allocate & load data */
418 >
419 >        if (fp != stdin) {                      /* close input stream */
420                  if (inspec[0] == '!')
421                          pclose(fp);
422                  else
# Line 362 | Line 426 | rmx_load(const char *inspec, RMPref rmp)
426          else
427                  funlockfile(fp);
428   #endif
429 +        if (!ok) {                              /* load failure? */
430 +                fprintf(stderr, "Error loading data from: %s\n", inspec);
431 +                rmx_free(dnew);
432 +                return(NULL);
433 +        }
434 +                                                /* undo exposure? */
435 +        if ((dnew->cexp[0] != 1.f) |
436 +                        (dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) {
437 +                double  cmlt[MAXCSAMP];
438 +                int     i;
439 +                cmlt[0] = 1./dnew->cexp[0];
440 +                cmlt[1] = 1./dnew->cexp[1];
441 +                cmlt[2] = 1./dnew->cexp[2];
442 +                if (dnew->ncomp > MAXCSAMP) {
443 +                        fprintf(stderr, "Excess spectral components in: %s\n",
444 +                                        inspec);
445 +                        rmx_free(dnew);
446 +                        return(NULL);
447 +                }
448 +                for (i = dnew->ncomp; i-- > 3; )
449 +                        cmlt[i] = cmlt[1];
450 +                rmx_scale(dnew, cmlt);
451 +                setcolor(dnew->cexp, 1.f, 1.f, 1.f);
452 +        }
453          return(dnew);
366 loaderr:                                        /* should report error? */
367        if (inspec[0] == '!')
368                pclose(fp);
369        else
370                fclose(fp);
371        rmx_free(dnew);
372        return(NULL);
454   }
455  
456   static int
457 < rmx_write_ascii(const RMATRIX *rm, FILE *fp)
457 > rmx_write_ascii(const double *dp, int nc, int len, FILE *fp)
458   {
459 <        const char      *fmt = (rm->dtype == DTfloat) ? " %.7e" :
460 <                        (rm->dtype == DTrgbe) | (rm->dtype == DTxyze) ? " %.3e" :
461 <                                " %.15e" ;
462 <        int     i, j, k;
382 <
383 <        for (i = 0; i < rm->nrows; i++) {
384 <            for (j = 0; j < rm->ncols; j++) {
385 <                const double    *dp = rmx_lval(rm,i,j);
386 <                for (k = 0; k < rm->ncomp; k++)
387 <                    fprintf(fp, fmt, dp[k]);
459 >        while (len-- > 0) {
460 >                int     k = nc;
461 >                while (k-- > 0)
462 >                        fprintf(fp, " %.7e", *dp++);
463                  fputc('\t', fp);
389            }
390            fputc('\n', fp);
464          }
465 <        return(1);
465 >        return(fputc('\n', fp) != EOF);
466   }
467  
468   static int
469 < rmx_write_float(const RMATRIX *rm, FILE *fp)
469 > rmx_write_float(const double *dp, int len, FILE *fp)
470   {
471 <        int     i, j, k;
399 <        float   val[100];
471 >        float   val;
472  
473 <        if (rm->ncomp > 100) {
474 <                fputs("Unsupported # components in rmx_write_float()\n", stderr);
475 <                exit(1);
404 <        }
405 <        for (i = 0; i < rm->nrows; i++)
406 <            for (j = 0; j < rm->ncols; j++) {
407 <                const double    *dp = rmx_lval(rm,i,j);
408 <                for (k = rm->ncomp; k--; )
409 <                    val[k] = (float)dp[k];
410 <                if (putbinary(val, sizeof(float), rm->ncomp, fp) != rm->ncomp)
473 >        while (len--) {
474 >                val = *dp++;
475 >                if (putbinary(&val, sizeof(float), 1, fp) != 1)
476                          return(0);
477 <            }
477 >        }
478          return(1);
479   }
480  
481   static int
482 < rmx_write_double(const RMATRIX *rm, FILE *fp)
482 > rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp)
483   {
484 <        int     i;
484 >        COLR    *scan;
485 >        int     j;
486  
487 <        for (i = 0; i < rm->nrows; i++)
488 <                if (putbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp,
489 <                                        rm->ncols, fp) != rm->ncols)
490 <                        return(0);
491 <        return(1);
487 >        if ((nc != 1) & (nc != 3)) return(0);
488 >        scan = (COLR *)tempbuffer(sizeof(COLR)*len);
489 >        if (!scan) return(0);
490 >
491 >        for (j = 0; j < len; j++, dp += nc)
492 >                if (nc == 1)
493 >                        setcolr(scan[j], dp[0], dp[0], dp[0]);
494 >                else
495 >                        setcolr(scan[j], dp[0], dp[1], dp[2]);
496 >
497 >        return(fwritecolrs(scan, len, fp) >= 0);
498   }
499  
500   static int
501 < rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
501 > rmx_write_spec(const double *dp, int nc, int len, FILE *fp)
502   {
503 <        COLR    *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols);
504 <        int     i, j;
503 >        uby8    *scan;
504 >        SCOLOR  scol;
505 >        int     j, k;
506  
507 <        if (!scan)
508 <                return(0);
509 <        for (i = 0; i < rm->nrows; i++) {
510 <            for (j = rm->ncols; j--; ) {
511 <                const double    *dp = rmx_lval(rm,i,j);
512 <                if (rm->ncomp == 1)
513 <                        setcolr(scan[j], dp[0], dp[0], dp[0]);
441 <                else
442 <                        setcolr(scan[j], dp[0], dp[1], dp[2]);
443 <            }
444 <            if (fwritecolrs(scan, rm->ncols, fp) < 0) {
445 <                free(scan);
446 <                return(0);
447 <            }
507 >        if (nc < 3) return(0);
508 >        scan = (uby8 *)tempbuffer((nc+1)*len);
509 >        if (!scan) return(0);
510 >        for (j = 0; j < len; j++, dp += nc) {
511 >                for (k = nc; k--; )
512 >                        scol[k] = dp[k];
513 >                scolor2scolr(scan+j*(nc+1), scol, nc);
514          }
515 <        free(scan);
450 <        return(1);
515 >        return(fwritescolrs(scan, nc, len, fp) >= 0);
516   }
517  
518   /* Check if CIE XYZ primaries were specified */
# Line 467 | Line 532 | findCIEprims(const char *info)
532                          (prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01));
533   }
534  
535 < /* Write matrix to file type indicated by dtype */
535 > /* Finish writing header data with resolution and format, returning type used */
536   int
537 < rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
537 > rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp)
538   {
539 <        int     ok = 1;
475 <
476 <        if (!rm | !fp || !rm->mtx)
539 >        if (!rm | !fp || rm->ncols <= 0)
540                  return(0);
478 #ifdef getc_unlocked
479        flockfile(fp);
480 #endif
481                                                /* complete header */
541          if (rm->info)
542                  fputs(rm->info, fp);
543          if (dtype == DTfromHeader)
# Line 488 | Line 547 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
547                  dtype = DTxyze;
548          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
549                  dtype = DTrgbe;
550 <        if ((dtype != DTrgbe) & (dtype != DTxyze)) {
551 <                fprintf(fp, "NROWS=%d\n", rm->nrows);
550 >        if ((dtype < DTspec) & (rm->ncomp > 3))
551 >                dtype = DTspec;
552 >        else if ((dtype == DTspec) & (rm->ncomp <= 3))
553 >                return(0);
554 >
555 >        if (dtype == DTascii)                   /* set file type (WINDOWS) */
556 >                SET_FILE_TEXT(fp);
557 >        else
558 >                SET_FILE_BINARY(fp);
559 >                                                /* write exposure? */
560 >        if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) |
561 >                        (rm->cexp[GRN] != rm->cexp[BLU]))
562 >                fputcolcor(rm->cexp, fp);
563 >        else if (rm->cexp[GRN] != 1.f)
564 >                fputexpos(rm->cexp[GRN], fp);
565 >                                                /* matrix size? */
566 >        if ((dtype > DTspec) | (rm->nrows <= 0)) {
567 >                if (rm->nrows > 0)
568 >                        fprintf(fp, "NROWS=%d\n", rm->nrows);
569                  fprintf(fp, "NCOLS=%d\n", rm->ncols);
570 <                fprintf(fp, "NCOMP=%d\n", rm->ncomp);
570 >        }
571 >        if (dtype >= DTspec) {                  /* # components & split? */
572 >                fputncomp(rm->ncomp, fp);
573 >                if (rm->ncomp > 3 &&
574 >                                memcmp(rm->wlpart, WLPART, sizeof(WLPART)))
575 >                        fputwlsplit(rm->wlpart, fp);
576          } else if ((rm->ncomp != 3) & (rm->ncomp != 1))
577                  return(0);                      /* wrong # components */
578          if ((dtype == DTfloat) | (dtype == DTdouble))
579                  fputendian(fp);                 /* important to record */
580          fputformat(cm_fmt_id[dtype], fp);
581 <        fputc('\n', fp);
582 <        switch (dtype) {                        /* write data */
581 >        fputc('\n', fp);                        /* end of header */
582 >        if ((dtype <= DTspec) & (rm->nrows > 0))
583 >                fprtresolu(rm->ncols, rm->nrows, fp);
584 >        return(dtype);
585 > }
586 >
587 > /* Write out matrix data (usually by row) */
588 > int
589 > rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp)
590 > {
591 >        switch (dtype) {
592          case DTascii:
593 <                ok = rmx_write_ascii(rm, fp);
504 <                break;
593 >                return(rmx_write_ascii(dp, nc, len, fp));
594          case DTfloat:
595 <                ok = rmx_write_float(rm, fp);
596 <                break;
597 <        case DTdouble:
509 <                ok = rmx_write_double(rm, fp);
510 <                break;
595 >                return(rmx_write_float(dp, nc*len, fp));
596 >        case DTrmx_native:
597 >                return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len);
598          case DTrgbe:
599          case DTxyze:
600 <                fprtresolu(rm->ncols, rm->nrows, fp);
601 <                ok = rmx_write_rgbe(rm, fp);
602 <                break;
516 <        default:
517 <                return(0);
600 >                return(rmx_write_rgbe(dp, nc, len, fp));
601 >        case DTspec:
602 >                return(rmx_write_spec(dp, nc, len, fp));
603          }
604 <        ok &= (fflush(fp) == 0);
604 >        return(0);
605 > }
606 >
607 > /* Write matrix using file format indicated by dtype */
608 > int
609 > rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
610 > {
611 >        int     ok = 0;
612 >        int     i;
613 >                                                /* complete header */
614 >        dtype = rmx_write_header(rm, dtype, fp);
615 >        if (dtype <= 0)
616 >                return(0);
617   #ifdef getc_unlocked
618 +        flockfile(fp);
619 + #endif
620 +        if (dtype == DTrmx_native)              /* write all at once? */
621 +                ok = rmx_write_data(rm->mtx, rm->ncomp,
622 +                                rm->nrows*rm->ncols, dtype, fp);
623 +        else                                    /* else row by row */
624 +                for (i = 0; i < rm->nrows; i++) {
625 +                        ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp,
626 +                                        rm->ncols, dtype, fp);
627 +                        if (!ok) break;
628 +                }
629 +
630 +        if (ok) ok = (fflush(fp) == 0);
631 + #ifdef getc_unlocked
632          funlockfile(fp);
633   #endif
634 +        if (!ok) fputs("Error writing matrix\n", stderr);
635          return(ok);
636   }
637  
# Line 532 | Line 644 | rmx_identity(const int dim, const int n)
644  
645          if (!rid)
646                  return(NULL);
647 <        memset(rid->mtx, 0, array_size(rid));
647 >        memset(rid->mtx, 0, rmx_array_size(rid));
648          for (i = dim; i--; ) {
649              double      *dp = rmx_lval(rid,i,i);
650              for (k = n; k--; )
# Line 541 | Line 653 | rmx_identity(const int dim, const int n)
653          return(rid);
654   }
655  
656 < /* Duplicate the given matrix */
656 > /* Duplicate the given matrix (may be unallocated) */
657   RMATRIX *
658   rmx_copy(const RMATRIX *rm)
659   {
# Line 549 | Line 661 | rmx_copy(const RMATRIX *rm)
661  
662          if (!rm)
663                  return(NULL);
664 <        dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
664 >        dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp);
665          if (!dnew)
666                  return(NULL);
667 +        if (rm->mtx) {
668 +                if (!rmx_prepare(dnew)) {
669 +                        rmx_free(dnew);
670 +                        return(NULL);
671 +                }
672 +                memcpy(dnew->mtx, rm->mtx, rmx_array_size(dnew));
673 +        }
674          rmx_addinfo(dnew, rm->info);
675          dnew->dtype = rm->dtype;
676 <        memcpy(dnew->mtx, rm->mtx, array_size(dnew));
676 >        copycolor(dnew->cexp, rm->cexp);
677 >        memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
678          return(dnew);
679   }
680  
681 + /* Replace data in first matrix with data from second */
682 + int
683 + rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa)
684 + {
685 +        if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) |
686 +                        (rdst->ncols != rsrc->ncols) |
687 +                        (rdst->ncomp != rsrc->ncomp))
688 +                return(0);
689 +
690 +        if (dometa) {           /* transfer everything? */
691 +                rmx_reset(rdst);
692 +                *rdst = *rsrc;
693 +                rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL;
694 +                return(1);
695 +        }
696 +        if (rdst->pflags & RMF_OURMEM) {
697 + #ifdef MAP_FILE                 /* just matrix data -- leave metadata */
698 +                if (rdst->mapped)
699 +                        munmap(rdst->mapped, rmx_mapped_size(rdst));
700 +                else
701 + #endif
702 +                if (rdst->mtx)
703 +                        free(rdst->mtx);
704 +        }
705 +        rdst->mapped = rsrc->mapped;
706 +        rdst->mtx = rsrc->mtx;
707 +        if (rsrc->pflags & RMF_OURMEM)
708 +                rdst->pflags |= RMF_OURMEM;
709 +        else
710 +                rdst->pflags &= ~RMF_OURMEM;
711 +        rsrc->mapped = NULL; rsrc->mtx = NULL;
712 +        return(1);
713 + }
714 +
715   /* Allocate and assign transposed matrix */
716   RMATRIX *
717   rmx_transpose(const RMATRIX *rm)
# Line 565 | Line 719 | rmx_transpose(const RMATRIX *rm)
719          RMATRIX *dnew;
720          int     i, j;
721  
722 <        if (!rm)
722 >        if (!rm || !rm->mtx)
723                  return(0);
724          if ((rm->nrows == 1) | (rm->ncols == 1)) {
725                  dnew = rmx_copy(rm);
# Line 583 | Line 737 | rmx_transpose(const RMATRIX *rm)
737                  rmx_addinfo(dnew, "Transposed rows and columns\n");
738          }
739          dnew->dtype = rm->dtype;
740 <        for (i = dnew->nrows; i--; )
741 <            for (j = dnew->ncols; j--; )
742 <                memcpy(rmx_lval(dnew,i,j), rmx_lval(rm,j,i),
740 >        copycolor(dnew->cexp, rm->cexp);
741 >        memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
742 >        for (j = dnew->ncols; j--; )
743 >            for (i = dnew->nrows; i--; )
744 >                memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i),
745                                  sizeof(double)*dnew->ncomp);
746          return(dnew);
747   }
# Line 597 | Line 753 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
753          RMATRIX *mres;
754          int     i, j, k, h;
755  
756 <        if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
756 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
757 >                        (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
758                  return(NULL);
759          mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
760          if (!mres)
# Line 612 | Line 769 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
769                  for (k = mres->ncomp; k--; ) {
770                      double      d = 0;
771                      for (h = m1->ncols; h--; )
772 <                        d += rmx_lval(m1,i,h)[k] * rmx_lval(m2,h,j)[k];
772 >                        d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k];
773                      rmx_lval(mres,i,j)[k] = d;
774                  }
775          return(mres);
# Line 625 | Line 782 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
782          int     zeroDivides = 0;
783          int     i, j, k;
784  
785 <        if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
785 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
786 >                         (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
787                  return(0);
788          if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
789                  return(0);
# Line 639 | Line 797 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
797                  if (divide) {
798                      double      d;
799                      if (m2->ncomp == 1) {
800 <                        d = rmx_lval(m2,i,j)[0];
800 >                        d = rmx_val(m2,i,j)[0];
801                          if (d == 0) {
802                              ++zeroDivides;
803                              for (k = m1->ncomp; k--; )
# Line 651 | Line 809 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
809                          }
810                      } else
811                          for (k = m1->ncomp; k--; ) {
812 <                            d = rmx_lval(m2,i,j)[k];
812 >                            d = rmx_val(m2,i,j)[k];
813                              if (d == 0) {
814                                  ++zeroDivides;
815                                  rmx_lval(m1,i,j)[k] = 0;
# Line 660 | Line 818 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
818                          }
819                  } else {
820                      if (m2->ncomp == 1) {
821 <                        const double    d = rmx_lval(m2,i,j)[0];
821 >                        const double    d = rmx_val(m2,i,j)[0];
822                          for (k = m1->ncomp; k--; )
823                              rmx_lval(m1,i,j)[k] *= d;
824                      } else
825                          for (k = m1->ncomp; k--; )
826 <                            rmx_lval(m1,i,j)[k] *= rmx_lval(m2,i,j)[k];
826 >                            rmx_lval(m1,i,j)[k] *= rmx_val(m2,i,j)[k];
827                  }
828          if (zeroDivides) {
829                  rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n");
# Line 681 | Line 839 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
839          double  *mysf = NULL;
840          int     i, j, k;
841  
842 <        if (!msum | !madd ||
842 >        if (!msum | !madd || !msum->mtx | !madd->mtx |
843                          (msum->nrows != madd->nrows) |
844                          (msum->ncols != madd->ncols) |
845                          (msum->ncomp != madd->ncomp))
# Line 701 | Line 859 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
859                  rmx_addinfo(msum, rmx_mismatch_warn);
860          for (i = msum->nrows; i--; )
861              for (j = msum->ncols; j--; ) {
862 <                const double    *da = rmx_lval(madd,i,j);
862 >                const double    *da = rmx_val(madd,i,j);
863                  double          *ds = rmx_lval(msum,i,j);
864                  for (k = msum->ncomp; k--; )
865                       ds[k] += sf[k] * da[k];
# Line 717 | Line 875 | rmx_scale(RMATRIX *rm, const double sf[])
875   {
876          int     i, j, k;
877  
878 <        if (!rm | !sf)
878 >        if (!rm | !sf || !rm->mtx)
879                  return(0);
880          for (i = rm->nrows; i--; )
881              for (j = rm->ncols; j--; ) {
# Line 727 | Line 885 | rmx_scale(RMATRIX *rm, const double sf[])
885              }
886          if (rm->info)
887                  rmx_addinfo(rm, "Applied scalar\n");
888 +        /* XXX: should record as exposure for COLR and SCOLR types? */
889          return(1);
890   }
891  
# Line 737 | Line 896 | rmx_transform(const RMATRIX *msrc, int n, const double
896          int     i, j, ks, kd;
897          RMATRIX *dnew;
898  
899 <        if (!msrc | (n <= 0) | !cmat)
899 >        if (!msrc | (n <= 0) | !cmat || !msrc->mtx)
900                  return(NULL);
901          dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
902          if (!dnew)
# Line 752 | Line 911 | rmx_transform(const RMATRIX *msrc, int n, const double
911          dnew->dtype = msrc->dtype;
912          for (i = dnew->nrows; i--; )
913              for (j = dnew->ncols; j--; ) {
914 <                const double    *ds = rmx_lval(msrc,i,j);
914 >                const double    *ds = rmx_val(msrc,i,j);
915                  for (kd = dnew->ncomp; kd--; ) {
916                      double      d = 0;
917                      for (ks = msrc->ncomp; ks--; )
# Line 794 | Line 953 | cm_from_rmatrix(const RMATRIX *rm)
953          int     i, j;
954          CMATRIX *cnew;
955  
956 <        if (!rm || !rm->mtx | ((rm->ncomp != 3) & (rm->ncomp != 1)))
956 >        if (!rm || !rm->mtx | (rm->ncomp == 2))
957                  return(NULL);
958          cnew = cm_alloc(rm->nrows, rm->ncols);
959          if (!cnew)
960                  return(NULL);
961          for (i = cnew->nrows; i--; )
962              for (j = cnew->ncols; j--; ) {
963 <                const double    *dp = rmx_lval(rm,i,j);
963 >                const double    *dp = rmx_val(rm,i,j);
964                  COLORV          *cv = cm_lval(cnew,i,j);
965 <                if (rm->ncomp == 1)
966 <                    setcolor(cv, dp[0], dp[0], dp[0]);
808 <                else
965 >                switch (rm->ncomp) {
966 >                case 3:
967                      setcolor(cv, dp[0], dp[1], dp[2]);
968 +                    break;
969 +                case 1:
970 +                    setcolor(cv, dp[0], dp[0], dp[0]);
971 +                    break;
972 +                default: {
973 +                        SCOLOR  scol;
974 +                        int     k;
975 +                        for (k = rm->ncomp; k--; )
976 +                                scol[k] = dp[k];
977 +                        scolor2color(cv, scol, rm->ncomp, rm->wlpart);
978 +                    } break;
979 +                }
980              }
981          return(cnew);
982   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines