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.57 by greg, Mon Mar 14 23:28:14 2022 UTC vs.
Revision 2.83 by greg, Tue Oct 8 00:43:57 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 41 | Line 46 | int
46   rmx_prepare(RMATRIX *rm)
47   {
48          if (!rm) return(0);
49 <        if (rm->mtx)
49 >        if (rm->mtx)                    /* assume it's right size */
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 = (rmx_dtype *)malloc(rmx_array_size(rm));
54 >        rm->pflags |= RMF_FREEMEM;
55          return(rm->mtx != NULL);
56   }
57  
# Line 53 | Line 61 | rmx_alloc(int nr, int nc, int n)
61   {
62          RMATRIX *dnew = rmx_new(nr, nc, n);
63  
64 <        if (dnew && !rmx_prepare(dnew)) {
64 >        if (!rmx_prepare(dnew)) {
65                  rmx_free(dnew);
66 <                dnew = NULL;
66 >                return(NULL);
67          }
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   #ifdef MAP_FILE
81 <        if (rm->mapped)
82 <                munmap(rm->mapped, mapped_size(rm));
83 <        else
81 >        if (rm->mapped) {
82 >                munmap(rm->mapped, rmx_mapped_size(rm));
83 >                rm->mapped = NULL;
84 >        } else
85   #endif
86 +        if (rm->pflags & RMF_FREEMEM) {
87                  free(rm->mtx);
88 +                rm->pflags &= ~RMF_FREEMEM;
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 93 | Line 116 | rmx_newtype(int dtyp1, int dtyp2)
116   int
117   rmx_addinfo(RMATRIX *rm, const char *info)
118   {
119 <        int     oldlen = 0;
119 >        size_t  oldlen = 0;
120  
121          if (!rm || !info || !*info)
122                  return(0);
123          if (!rm->info) {
124                  rm->info = (char *)malloc(strlen(info)+1);
102                if (rm->info) rm->info[0] = '\0';
125          } else {
126                  oldlen = strlen(rm->info);
127                  rm->info = (char *)realloc(rm->info,
# Line 118 | Line 140 | get_dminfo(char *s, void *p)
140          char    fmt[MAXFMTLEN];
141          int     i;
142  
143 <        if (headidval(fmt, s))
143 >        if (isheadid(s))
144                  return(0);
145 <        if (!strncmp(s, "NCOMP=", 6)) {
146 <                ip->ncomp = atoi(s+6);
147 <                return(0);
145 >        if (isncomp(s)) {
146 >                ip->ncomp = ncompval(s);
147 >                return(ip->ncomp - 1);
148          }
149          if (!strncmp(s, "NROWS=", 6)) {
150                  ip->nrows = atoi(s+6);
151 <                return(0);
151 >                return(ip->nrows - 1);
152          }
153          if (!strncmp(s, "NCOLS=", 6)) {
154                  ip->ncols = atoi(s+6);
155 <                return(0);
155 >                return(ip->ncols - 1);
156          }
157          if ((i = isbigendian(s)) >= 0) {
158 <                ip->swapin = (nativebigendian() != i);
158 >                if (nativebigendian() != i)
159 >                        ip->pflags |= RMF_SWAPIN;
160 >                else
161 >                        ip->pflags &= ~RMF_SWAPIN;
162                  return(0);
163          }
164          if (isexpos(s)) {
165                  float   f = exposval(s);
166                  scalecolor(ip->cexp, f);
167 <                return(0);
167 >                return(f > .0 ? 0 : -1);
168          }
169          if (iscolcor(s)) {
170                  COLOR   ctmp;
171 <                colcorval(ctmp, s);
171 >                if (!colcorval(ctmp, s)) return(-1);
172                  multcolor(ip->cexp, ctmp);
173                  return(0);
174          }
175 +        if (iswlsplit(s))
176 +                return(wlsplitval(ip->wlpart, s) - 1);
177 +
178          if (!formatval(fmt, s)) {
179                  rmx_addinfo(ip, s);
180                  return(0);
# Line 156 | Line 184 | get_dminfo(char *s, void *p)
184                          ip->dtype = i;
185                          return(0);
186                  }
187 <        return(-1);
187 >        return(-1);             /* bad format */
188   }
189  
190   static int
191 < rmx_load_ascii(RMATRIX *rm, FILE *fp)
191 > rmx_load_ascii(rmx_dtype *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++) {
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 <            }
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(rmx_dtype *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))
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);
212 >        for (j = 0; j < rm->ncols; j++) {
213                  if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
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 <            }
214 >                        return(0);
215 >                if (rm->pflags & RMF_SWAPIN)
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(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
225   {
226 <        int     i;
208 < #ifdef MAP_FILE
209 <        long    pos;            /* map memory for file > 1MB 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 <                }               /* else fall back on reading into memory */
218 <                rm->mapped = NULL;
219 <        }
220 < #endif
221 <        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,
230 <                                        rm->ncols, fp) != rm->ncols)
231 <                        return(0);
232 <                if (rm->swapin)
233 <                        swap64((char *)rmx_lval(rm,i,0), rm->ncols*rm->ncomp);
228 >        if (rm->pflags & RMF_SWAPIN)
229 >                swap64((char *)drp, rm->ncols*rm->ncomp);
230 >        return(1);
231 > }
232 >
233 > static int
234 > rmx_load_rgbe(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
235 > {
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 (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          }
253          return(1);
254   }
255  
256   static int
257 < rmx_load_rgbe(RMATRIX *rm, FILE *fp)
257 > rmx_load_spec(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
258   {
259 <        COLOR   *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
260 <        int     i, j;
259 >        uby8    *scan;
260 >        SCOLOR  scol;
261 >        int     j, k;
262  
263 +        if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP))
264 +                return(0);
265 +        scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols);
266          if (!scan)
267                  return(0);
268 <        if (!rmx_prepare(rm))
268 >        if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0)
269                  return(0);
270 <        for (i = 0; i < rm->nrows; i++) {
271 <            double      *dp = rmx_lval(rm,i,0);
272 <            if (freadscan(scan, rm->ncols, fp) < 0) {
273 <                free(scan);
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 >        }
275 >        return(1);
276 > }
277 >
278 > /* Read matrix header from input stream (cannot be XML) */
279 > int
280 > rmx_load_header(RMATRIX *rm, FILE *fp)
281 > {
282 >        if (!rm | !fp)
283                  return(0);
284 <            }
285 <            for (j = 0; j < rm->ncols; j++, dp += 3) {
286 <                dp[0] = colval(scan[j],RED);
287 <                dp[1] = colval(scan[j],GRN);
288 <                dp[2] = colval(scan[j],BLU);
289 <            }
284 >        rmx_reset(rm);                          /* clear state */
285 >        if (rm->nrows | rm->ncols | !rm->dtype) {
286 >                rm->nrows = rm->ncols = 0;
287 >                rm->ncomp = 3;
288 >                setcolor(rm->cexp, 1.f, 1.f, 1.f);
289 >                memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart));
290 >                rm->pflags = 0;
291          }
292 <        free(scan);
292 >        rm->dtype = DTascii;                    /* assumed w/o FORMAT */
293 >        if (getheader(fp, get_dminfo, rm) < 0) {
294 >                fputs("Bad matrix header\n", stderr);
295 >                return(0);
296 >        }
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 + /* Load next row as rmx_dtype (cannot be XML) */
311 + int
312 + rmx_load_row(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
313 + {
314 +        switch (rm->dtype) {
315 +        case DTascii:
316 +                return(rmx_load_ascii(drp, rm, fp));
317 +        case DTfloat:
318 +                return(rmx_load_float(drp, rm, fp));
319 +        case DTdouble:
320 +                return(rmx_load_double(drp, rm, fp));
321 +        case DTrgbe:
322 +        case DTxyze:
323 +                return(rmx_load_rgbe(drp, rm, fp));
324 +        case DTspec:
325 +                return(rmx_load_spec(drp, rm, fp));
326 +        default:
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 == DTrmx_native) & !(rm->pflags & RMF_SWAPIN) &
340 +                        (rmx_array_size(rm) >= 1L<<20) &&
341 +                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(rmx_dtype))) {
342 +                rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE,
343 +                                        MAP_PRIVATE, fileno(fp), 0);
344 +                if (rm->mapped != MAP_FAILED) {
345 +                        if (rm->pflags & RMF_FREEMEM)
346 +                                free(rm->mtx);
347 +                        rm->mtx = (rmx_dtype *)rm->mapped + pos/sizeof(rmx_dtype);
348 +                        rm->pflags &= ~RMF_FREEMEM;
349 +                        return(1);
350 +                }               /* else fall back on reading into memory */
351 +                rm->mapped = NULL;
352 +        }
353 + #endif
354 +        if (!rmx_prepare(rm)) { /* need in-core matrix array */
355 +                fprintf(stderr, "Cannot allocate %g MByte matrix array\n",
356 +                                (1./(1L<<20))*(double)rmx_array_size(rm));
357 +                return(0);
358 +        }
359 +        for (i = 0; i < rm->nrows; i++)
360 +                if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp))
361 +                        return(0);
362 +        return(1);
363 + }
364 +
365   /* Load matrix from supported file type */
366   RMATRIX *
367   rmx_load(const char *inspec, RMPref rmp)
368   {
369          FILE            *fp;
370          RMATRIX         *dnew;
371 +        int             ok;
372  
373          if (!inspec)
374                  inspec = stdin_name;
375          else if (!*inspec)
376                  return(NULL);
377 <        if (inspec == stdin_name) {             /* reading from stdin? */
377 >        if (inspec == stdin_name)               /* reading from stdin? */
378                  fp = stdin;
379 <        } else if (inspec[0] == '!') {
380 <                if (!(fp = popen(inspec+1, "r")))
381 <                        return(NULL);
275 <        } else {
379 >        else if (inspec[0] == '!')
380 >                fp = popen(inspec+1, "r");
381 >        else {
382                  const char      *sp = inspec;   /* check suffix */
383                  while (*sp)
384                          ++sp;
385                  while (sp > inspec && sp[-1] != '.')
386                          --sp;
387                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
388 <                        CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) :
388 >                        CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL :
389 >                                        rmp==RMPtrans ? cm_loadBTDF(inspec) :
390                                          cm_loadBRDF(inspec, rmp==RMPreflB) ;
391                          if (!cm)
392                                  return(NULL);
393                          dnew = rmx_from_cmatrix(cm);
394                          cm_free(cm);
395                          dnew->dtype = DTascii;
396 <                        return(dnew);
397 <                }
398 <                                                /* else open it ourselves */
292 <                if (!(fp = fopen(inspec, "r")))
293 <                        return(NULL);
396 >                        return(dnew);           /* return here */
397 >                }                               /* else open it ourselves */
398 >                fp = fopen(inspec, "r");
399          }
400 <        SET_FILE_BINARY(fp);
400 >        if (!fp) {
401 >                fprintf(stderr, "Cannot open for reading: %s\n", inspec);
402 >                return(NULL);
403 >        }
404   #ifdef getc_unlocked
405          flockfile(fp);
406   #endif
407 <        if (!(dnew = rmx_new(0,0,3))) {
408 <                fclose(fp);
407 >        SET_FILE_BINARY(fp);                    /* load header info */
408 >        if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) {
409 >                fprintf(stderr, "Bad header in: %s\n", inspec);
410 >                if (inspec[0] == '!') pclose(fp);
411 >                else fclose(fp);
412 >                rmx_free(dnew);
413                  return(NULL);
414          }
415 <        dnew->dtype = DTascii;                  /* assumed w/o FORMAT */
416 <        dnew->cexp[0] = dnew->cexp[1] = dnew->cexp[2] = 1.f;
417 <        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) {
415 >        ok = rmx_load_data(dnew, fp);           /* allocate & load data */
416 >
417 >        if (fp != stdin) {                      /* close input stream */
418                  if (inspec[0] == '!')
419                          pclose(fp);
420                  else
# Line 362 | Line 424 | rmx_load(const char *inspec, RMPref rmp)
424          else
425                  funlockfile(fp);
426   #endif
427 +        if (!ok) {                              /* load failure? */
428 +                fprintf(stderr, "Error loading data from: %s\n", inspec);
429 +                rmx_free(dnew);
430 +                return(NULL);
431 +        }
432 +                                                /* undo exposure? */
433 +        if ((dnew->cexp[0] != 1.f) |
434 +                        (dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) {
435 +                double  cmlt[MAXCSAMP];
436 +                int     i;
437 +                if (dnew->ncomp > MAXCSAMP) {
438 +                        fprintf(stderr, "Excess spectral components in: %s\n",
439 +                                        inspec);
440 +                        rmx_free(dnew);
441 +                        return(NULL);
442 +                }
443 +                cmlt[0] = 1./dnew->cexp[0];
444 +                cmlt[1] = 1./dnew->cexp[1];
445 +                cmlt[2] = 1./dnew->cexp[2];
446 +                for (i = dnew->ncomp; i-- > 3; )
447 +                        cmlt[i] = cmlt[1];      /* XXX hack! */
448 +                rmx_scale(dnew, cmlt);
449 +                setcolor(dnew->cexp, 1.f, 1.f, 1.f);
450 +        }
451          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);
452   }
453  
454   static int
455 < rmx_write_ascii(const RMATRIX *rm, FILE *fp)
455 > rmx_write_ascii(const rmx_dtype *dp, int nc, int len, FILE *fp)
456   {
457 <        const char      *fmt = (rm->dtype == DTfloat) ? " %.7e" :
458 <                        (rm->dtype == DTrgbe) | (rm->dtype == DTxyze) ? " %.3e" :
459 <                                " %.15e" ;
460 <        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]);
457 >        while (len-- > 0) {
458 >                int     k = nc;
459 >                while (k-- > 0)
460 >                        fprintf(fp, " %.7e", *dp++);
461                  fputc('\t', fp);
389            }
390            fputc('\n', fp);
462          }
463 <        return(1);
463 >        return(fputc('\n', fp) != EOF);
464   }
465  
466   static int
467 < rmx_write_float(const RMATRIX *rm, FILE *fp)
467 > rmx_write_float(const rmx_dtype *dp, int len, FILE *fp)
468   {
469 <        int     i, j, k;
399 <        float   val[100];
469 >        float   val;
470  
471 <        if (rm->ncomp > 100) {
472 <                fputs("Unsupported # components in rmx_write_float()\n", stderr);
473 <                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)
471 >        while (len--) {
472 >                val = *dp++;
473 >                if (putbinary(&val, sizeof(float), 1, fp) != 1)
474                          return(0);
475 <            }
475 >        }
476          return(1);
477   }
478  
479   static int
480 < rmx_write_double(const RMATRIX *rm, FILE *fp)
480 > rmx_write_rgbe(const rmx_dtype *dp, int nc, int len, FILE *fp)
481   {
482 <        int     i;
482 >        COLR    *scan;
483 >        int     j;
484  
485 <        for (i = 0; i < rm->nrows; i++)
486 <                if (putbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp,
487 <                                        rm->ncols, fp) != rm->ncols)
488 <                        return(0);
489 <        return(1);
485 >        if ((nc != 1) & (nc != 3)) return(0);
486 >        scan = (COLR *)tempbuffer(sizeof(COLR)*len);
487 >        if (!scan) return(0);
488 >
489 >        for (j = 0; j < len; j++, dp += nc)
490 >                if (nc == 1)
491 >                        setcolr(scan[j], dp[0], dp[0], dp[0]);
492 >                else
493 >                        setcolr(scan[j], dp[0], dp[1], dp[2]);
494 >
495 >        return(fwritecolrs(scan, len, fp) >= 0);
496   }
497  
498   static int
499 < rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
499 > rmx_write_spec(const rmx_dtype *dp, int nc, int len, FILE *fp)
500   {
501 <        COLR    *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols);
502 <        int     i, j;
501 >        uby8    *scan;
502 >        SCOLOR  scol;
503 >        int     j, k;
504  
505 <        if (!scan)
506 <                return(0);
507 <        for (i = 0; i < rm->nrows; i++) {
508 <            for (j = rm->ncols; j--; ) {
509 <                const double    *dp = rmx_lval(rm,i,j);
510 <                if (rm->ncomp == 1)
511 <                        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 <            }
505 >        if (nc < 3) return(0);
506 >        scan = (uby8 *)tempbuffer((nc+1)*len);
507 >        if (!scan) return(0);
508 >        for (j = 0; j < len; j++, dp += nc) {
509 >                for (k = nc; k--; )
510 >                        scol[k] = dp[k];
511 >                scolor2scolr(scan+j*(nc+1), scol, nc);
512          }
513 <        free(scan);
450 <        return(1);
513 >        return(fwritescolrs(scan, nc, len, fp) >= 0);
514   }
515  
516   /* Check if CIE XYZ primaries were specified */
# Line 467 | Line 530 | findCIEprims(const char *info)
530                          (prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01));
531   }
532  
533 < /* Write matrix to file type indicated by dtype */
533 > /* Finish writing header data with resolution and format, returning type used */
534   int
535 < rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
535 > rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp)
536   {
537 <        int     ok = 1;
475 <
476 <        if (!rm | !fp || !rm->mtx)
537 >        if (!rm | !fp || rm->ncols <= 0)
538                  return(0);
478 #ifdef getc_unlocked
479        flockfile(fp);
480 #endif
481                                                /* complete header */
539          if (rm->info)
540                  fputs(rm->info, fp);
541          if (dtype == DTfromHeader)
# Line 488 | Line 545 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
545                  dtype = DTxyze;
546          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
547                  dtype = DTrgbe;
548 <        if ((dtype != DTrgbe) & (dtype != DTxyze)) {
549 <                fprintf(fp, "NROWS=%d\n", rm->nrows);
548 >        if ((dtype < DTspec) & (rm->ncomp > 3))
549 >                dtype = DTspec;
550 >        else if ((dtype == DTspec) & (rm->ncomp <= 3))
551 >                return(0);
552 >
553 >        if (dtype == DTascii)                   /* set file type (WINDOWS) */
554 >                SET_FILE_TEXT(fp);
555 >        else
556 >                SET_FILE_BINARY(fp);
557 >                                                /* write exposure? */
558 >        if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) |
559 >                        (rm->cexp[GRN] != rm->cexp[BLU]))
560 >                fputcolcor(rm->cexp, fp);
561 >        else if (rm->cexp[GRN] != 1.f)
562 >                fputexpos(rm->cexp[GRN], fp);
563 >                                                /* matrix size? */
564 >        if ((dtype > DTspec) | (rm->nrows <= 0)) {
565 >                if (rm->nrows > 0)
566 >                        fprintf(fp, "NROWS=%d\n", rm->nrows);
567                  fprintf(fp, "NCOLS=%d\n", rm->ncols);
568 <                fprintf(fp, "NCOMP=%d\n", rm->ncomp);
568 >        }
569 >        if (dtype >= DTspec) {                  /* # components & split? */
570 >                fputncomp(rm->ncomp, fp);
571 >                if (rm->ncomp > 3 &&
572 >                                memcmp(rm->wlpart, WLPART, sizeof(WLPART)))
573 >                        fputwlsplit(rm->wlpart, fp);
574          } else if ((rm->ncomp != 3) & (rm->ncomp != 1))
575                  return(0);                      /* wrong # components */
576          if ((dtype == DTfloat) | (dtype == DTdouble))
577                  fputendian(fp);                 /* important to record */
578          fputformat(cm_fmt_id[dtype], fp);
579 <        fputc('\n', fp);
580 <        switch (dtype) {                        /* write data */
579 >        fputc('\n', fp);                        /* end of header */
580 >        if ((dtype <= DTspec) & (rm->nrows > 0))
581 >                fprtresolu(rm->ncols, rm->nrows, fp);
582 >        return(dtype);
583 > }
584 >
585 > /* Write out matrix data (usually by row) */
586 > int
587 > rmx_write_data(const rmx_dtype *dp, int nc, int len, int dtype, FILE *fp)
588 > {
589 >        switch (dtype) {
590          case DTascii:
591 <                ok = rmx_write_ascii(rm, fp);
504 <                break;
591 >                return(rmx_write_ascii(dp, nc, len, fp));
592          case DTfloat:
593 <                ok = rmx_write_float(rm, fp);
594 <                break;
595 <        case DTdouble:
509 <                ok = rmx_write_double(rm, fp);
510 <                break;
593 >                return(rmx_write_float(dp, nc*len, fp));
594 >        case DTrmx_native:
595 >                return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len);
596          case DTrgbe:
597          case DTxyze:
598 <                fprtresolu(rm->ncols, rm->nrows, fp);
599 <                ok = rmx_write_rgbe(rm, fp);
600 <                break;
516 <        default:
517 <                return(0);
598 >                return(rmx_write_rgbe(dp, nc, len, fp));
599 >        case DTspec:
600 >                return(rmx_write_spec(dp, nc, len, fp));
601          }
602 <        ok &= (fflush(fp) == 0);
602 >        return(0);
603 > }
604 >
605 > /* Write matrix using file format indicated by dtype */
606 > int
607 > rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
608 > {
609 >        int     ok = 0;
610 >        int     i;
611 >                                                /* complete header */
612 >        dtype = rmx_write_header(rm, dtype, fp);
613 >        if (dtype <= 0)
614 >                return(0);
615   #ifdef getc_unlocked
616 +        flockfile(fp);
617 + #endif
618 +        if (dtype == DTrmx_native)              /* write all at once? */
619 +                ok = rmx_write_data(rm->mtx, rm->ncomp,
620 +                                rm->nrows*rm->ncols, dtype, fp);
621 +        else                                    /* else row by row */
622 +                for (i = 0; i < rm->nrows; i++) {
623 +                        ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp,
624 +                                        rm->ncols, dtype, fp);
625 +                        if (!ok) break;
626 +                }
627 +
628 +        if (ok) ok = (fflush(fp) == 0);
629 + #ifdef getc_unlocked
630          funlockfile(fp);
631   #endif
632 +        if (!ok) fputs("Error writing matrix\n", stderr);
633          return(ok);
634   }
635  
# Line 532 | Line 642 | rmx_identity(const int dim, const int n)
642  
643          if (!rid)
644                  return(NULL);
645 <        memset(rid->mtx, 0, array_size(rid));
645 >        memset(rid->mtx, 0, rmx_array_size(rid));
646          for (i = dim; i--; ) {
647 <            double      *dp = rmx_lval(rid,i,i);
647 >            rmx_dtype   *dp = rmx_lval(rid,i,i);
648              for (k = n; k--; )
649                  dp[k] = 1.;
650          }
651          return(rid);
652   }
653  
654 < /* Duplicate the given matrix */
654 > /* Duplicate the given matrix (may be unallocated) */
655   RMATRIX *
656   rmx_copy(const RMATRIX *rm)
657   {
# Line 549 | Line 659 | rmx_copy(const RMATRIX *rm)
659  
660          if (!rm)
661                  return(NULL);
662 <        dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
662 >        dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp);
663          if (!dnew)
664                  return(NULL);
665 +        if (rm->mtx) {
666 +                if (!rmx_prepare(dnew)) {
667 +                        rmx_free(dnew);
668 +                        return(NULL);
669 +                }
670 +                memcpy(dnew->mtx, rm->mtx, rmx_array_size(dnew));
671 +        }
672          rmx_addinfo(dnew, rm->info);
673          dnew->dtype = rm->dtype;
674 <        memcpy(dnew->mtx, rm->mtx, array_size(dnew));
674 >        copycolor(dnew->cexp, rm->cexp);
675 >        memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
676          return(dnew);
677   }
678  
679 + /* Replace data in first matrix with data from second */
680 + int
681 + rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa)
682 + {
683 +        if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) |
684 +                        (rdst->ncols != rsrc->ncols) |
685 +                        (rdst->ncomp != rsrc->ncomp))
686 +                return(0);
687 +
688 +        if (dometa) {           /* transfer everything? */
689 +                rmx_reset(rdst);
690 +                *rdst = *rsrc;
691 +                rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL;
692 +                return(1);
693 +        }
694 + #ifdef MAP_FILE                 /* just matrix data -- leave metadata */
695 +        if (rdst->mapped)
696 +                munmap(rdst->mapped, rmx_mapped_size(rdst));
697 +        else
698 + #endif
699 +        if (rdst->pflags & RMF_FREEMEM) {
700 +                free(rdst->mtx);
701 +                rdst->pflags &= ~RMF_FREEMEM;
702 +        }
703 +        rdst->mapped = rsrc->mapped;
704 +        rdst->mtx = rsrc->mtx;
705 +        rdst->pflags |= rsrc->pflags & RMF_FREEMEM;
706 +        rsrc->mapped = NULL; rsrc->mtx = NULL;
707 +        return(1);
708 + }
709 +
710   /* Allocate and assign transposed matrix */
711   RMATRIX *
712   rmx_transpose(const RMATRIX *rm)
# Line 565 | Line 714 | rmx_transpose(const RMATRIX *rm)
714          RMATRIX *dnew;
715          int     i, j;
716  
717 <        if (!rm)
717 >        if (!rm || !rm->mtx)
718                  return(0);
719          if ((rm->nrows == 1) | (rm->ncols == 1)) {
720                  dnew = rmx_copy(rm);
# Line 583 | Line 732 | rmx_transpose(const RMATRIX *rm)
732                  rmx_addinfo(dnew, "Transposed rows and columns\n");
733          }
734          dnew->dtype = rm->dtype;
735 +        copycolor(dnew->cexp, rm->cexp);
736 +        memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
737          for (j = dnew->ncols; j--; )
738              for (i = dnew->nrows; i--; )
739 <                memcpy(rmx_lval(dnew,i,j), rmx_lval(rm,j,i),
740 <                                sizeof(double)*dnew->ncomp);
739 >                memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i),
740 >                                sizeof(rmx_dtype)*dnew->ncomp);
741          return(dnew);
742   }
743  
# Line 597 | Line 748 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
748          RMATRIX *mres;
749          int     i, j, k, h;
750  
751 <        if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
751 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
752 >                        (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
753                  return(NULL);
754          mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
755          if (!mres)
# Line 610 | Line 762 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
762          for (i = mres->nrows; i--; )
763              for (j = mres->ncols; j--; )
764                  for (k = mres->ncomp; k--; ) {
765 <                    double      d = 0;
765 >                    rmx_dtype   d = 0;
766                      for (h = m1->ncols; h--; )
767 <                        d += rmx_lval(m1,i,h)[k] * rmx_lval(m2,h,j)[k];
767 >                        d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k];
768                      rmx_lval(mres,i,j)[k] = d;
769                  }
770          return(mres);
# Line 625 | Line 777 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
777          int     zeroDivides = 0;
778          int     i, j, k;
779  
780 <        if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
780 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
781 >                         (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
782                  return(0);
783          if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
784                  return(0);
# Line 637 | Line 790 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
790          for (i = m1->nrows; i--; )
791              for (j = m1->ncols; j--; )
792                  if (divide) {
793 <                    double      d;
793 >                    rmx_dtype   d;
794                      if (m2->ncomp == 1) {
795 <                        d = rmx_lval(m2,i,j)[0];
795 >                        d = rmx_val(m2,i,j)[0];
796                          if (d == 0) {
797                              ++zeroDivides;
798                              for (k = m1->ncomp; k--; )
# Line 651 | Line 804 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
804                          }
805                      } else
806                          for (k = m1->ncomp; k--; ) {
807 <                            d = rmx_lval(m2,i,j)[k];
807 >                            d = rmx_val(m2,i,j)[k];
808                              if (d == 0) {
809                                  ++zeroDivides;
810                                  rmx_lval(m1,i,j)[k] = 0;
# Line 660 | Line 813 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
813                          }
814                  } else {
815                      if (m2->ncomp == 1) {
816 <                        const double    d = rmx_lval(m2,i,j)[0];
816 >                        const rmx_dtype d = rmx_val(m2,i,j)[0];
817                          for (k = m1->ncomp; k--; )
818                              rmx_lval(m1,i,j)[k] *= d;
819                      } else
820                          for (k = m1->ncomp; k--; )
821 <                            rmx_lval(m1,i,j)[k] *= rmx_lval(m2,i,j)[k];
821 >                            rmx_lval(m1,i,j)[k] *= rmx_val(m2,i,j)[k];
822                  }
823          if (zeroDivides) {
824                  rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n");
# Line 681 | Line 834 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
834          double  *mysf = NULL;
835          int     i, j, k;
836  
837 <        if (!msum | !madd ||
837 >        if (!msum | !madd || !msum->mtx | !madd->mtx |
838                          (msum->nrows != madd->nrows) |
839                          (msum->ncols != madd->ncols) |
840                          (msum->ncomp != madd->ncomp))
# Line 701 | Line 854 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
854                  rmx_addinfo(msum, rmx_mismatch_warn);
855          for (i = msum->nrows; i--; )
856              for (j = msum->ncols; j--; ) {
857 <                const double    *da = rmx_lval(madd,i,j);
858 <                double          *ds = rmx_lval(msum,i,j);
857 >                const rmx_dtype *da = rmx_val(madd,i,j);
858 >                rmx_dtype       *ds = rmx_lval(msum,i,j);
859                  for (k = msum->ncomp; k--; )
860                       ds[k] += sf[k] * da[k];
861              }
# Line 717 | Line 870 | rmx_scale(RMATRIX *rm, const double sf[])
870   {
871          int     i, j, k;
872  
873 <        if (!rm | !sf)
873 >        if (!rm | !sf || !rm->mtx)
874                  return(0);
875          for (i = rm->nrows; i--; )
876              for (j = rm->ncols; j--; ) {
877 <                double  *dp = rmx_lval(rm,i,j);
877 >                rmx_dtype       *dp = rmx_lval(rm,i,j);
878                  for (k = rm->ncomp; k--; )
879                      dp[k] *= sf[k];
880              }
881          if (rm->info)
882                  rmx_addinfo(rm, "Applied scalar\n");
883 +        /* XXX: should record as exposure for COLR and SCOLR types? */
884          return(1);
885   }
886  
# Line 737 | Line 891 | rmx_transform(const RMATRIX *msrc, int n, const double
891          int     i, j, ks, kd;
892          RMATRIX *dnew;
893  
894 <        if (!msrc | (n <= 0) | !cmat)
894 >        if (!msrc | (n <= 0) | !cmat || !msrc->mtx)
895                  return(NULL);
896          dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
897          if (!dnew)
# Line 752 | Line 906 | rmx_transform(const RMATRIX *msrc, int n, const double
906          dnew->dtype = msrc->dtype;
907          for (i = dnew->nrows; i--; )
908              for (j = dnew->ncols; j--; ) {
909 <                const double    *ds = rmx_lval(msrc,i,j);
909 >                const rmx_dtype *ds = rmx_val(msrc,i,j);
910                  for (kd = dnew->ncomp; kd--; ) {
911 <                    double      d = 0;
911 >                    rmx_dtype   d = 0;
912                      for (ks = msrc->ncomp; ks--; )
913                          d += cmat[kd*msrc->ncomp + ks] * ds[ks];
914                      rmx_lval(dnew,i,j)[kd] = d;
# Line 779 | Line 933 | rmx_from_cmatrix(const CMATRIX *cm)
933          for (i = dnew->nrows; i--; )
934              for (j = dnew->ncols; j--; ) {
935                  const COLORV    *cv = cm_lval(cm,i,j);
936 <                double          *dp = rmx_lval(dnew,i,j);
936 >                rmx_dtype       *dp = rmx_lval(dnew,i,j);
937                  dp[0] = cv[0];
938                  dp[1] = cv[1];
939                  dp[2] = cv[2];
# Line 794 | Line 948 | cm_from_rmatrix(const RMATRIX *rm)
948          int     i, j;
949          CMATRIX *cnew;
950  
951 <        if (!rm || !rm->mtx | ((rm->ncomp != 3) & (rm->ncomp != 1)))
951 >        if (!rm || !rm->mtx | (rm->ncomp == 2))
952                  return(NULL);
953          cnew = cm_alloc(rm->nrows, rm->ncols);
954          if (!cnew)
955                  return(NULL);
956          for (i = cnew->nrows; i--; )
957              for (j = cnew->ncols; j--; ) {
958 <                const double    *dp = rmx_lval(rm,i,j);
958 >                const rmx_dtype *dp = rmx_val(rm,i,j);
959                  COLORV          *cv = cm_lval(cnew,i,j);
960 <                if (rm->ncomp == 1)
961 <                    setcolor(cv, dp[0], dp[0], dp[0]);
808 <                else
960 >                switch (rm->ncomp) {
961 >                case 3:
962                      setcolor(cv, dp[0], dp[1], dp[2]);
963 +                    break;
964 +                case 1:
965 +                    setcolor(cv, dp[0], dp[0], dp[0]);
966 +                    break;
967 +                default: {
968 +                        SCOLOR  scol;
969 +                        int     k;
970 +                        for (k = rm->ncomp; k--; )
971 +                                scol[k] = dp[k];
972 +                        scolor2color(cv, scol, rm->ncomp, rm->wlpart);
973 +                    } break;
974 +                }
975              }
976          return(cnew);
977   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines