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

Comparing ray/src/util/rmatrix.c (file contents):
Revision 2.63 by greg, Tue Nov 28 00:46:23 2023 UTC vs.
Revision 2.103 by greg, Thu Oct 30 17:26:45 2025 UTC

# Line 18 | Line 18 | static const char RCSid[] = "$Id$";
18  
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)
23 > rmx_new(int nr, int nc, int ncomp)
24   {
25          RMATRIX *dnew;
26  
27 <        if (n <= 0)
27 >        if (ncomp <= 0)
28                  return(NULL);
29  
30          dnew = (RMATRIX *)calloc(1, sizeof(RMATRIX));
31 <        if (dnew) {
32 <                dnew->dtype = DTdouble;
33 <                dnew->nrows = nr;
34 <                dnew->ncols = nc;
35 <                dnew->ncomp = n;
36 <                setcolor(dnew->cexp, 1.f, 1.f, 1.f);
37 <                memcpy(dnew->wlpart, WLPART, sizeof(dnew->wlpart));
38 <        }
31 >        if (!dnew)
32 >                return(NULL);
33 >
34 >        dnew->dtype = DTrmx_native;
35 >        dnew->nrows = nr;
36 >        dnew->ncols = nc;
37 >        dnew->ncomp = ncomp;
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 47 | 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          if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0))
52                  return(0);
53 <        rm->mtx = (double *)malloc(array_size(rm));
53 >        rm->mtx = (rmx_dtype *)malloc(rmx_array_size(rm));
54 >        rm->pflags |= RMF_FREEMEM;
55          return(rm->mtx != NULL);
56   }
57  
58   /* Call rmx_new() and rmx_prepare() */
59   RMATRIX *
60 < rmx_alloc(int nr, int nc, int n)
60 > rmx_alloc(int nr, int nc, int ncomp)
61   {
62 <        RMATRIX *dnew = rmx_new(nr, nc, n);
62 >        RMATRIX *dnew = rmx_new(nr, nc, ncomp);
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 101 | 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);
110                if (rm->info) rm->info[0] = '\0';
125          } else {
126                  oldlen = strlen(rm->info);
127                  rm->info = (char *)realloc(rm->info,
# Line 126 | Line 140 | get_dminfo(char *s, void *p)
140          char    fmt[MAXFMTLEN];
141          int     i;
142  
143 <        if (headidval(NULL, s))
143 >        if (isheadid(s))
144                  return(0);
145          if (isncomp(s)) {
146                  ip->ncomp = ncompval(s);
147 <                return(0);
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 <                wlsplitval(ip->wlpart, s);
177 <                return(0);
161 <        }
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 168 | 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++) {
183 <                double  *dp = rmx_lval(rm,i,j);
184 <                for (k = 0; k < rm->ncomp; k++)
185 <                    if (fscanf(fp, "%lf", &dp[k]) != 1)
186 <                        return(0);
187 <            }
195 >        for (j = 0; j < rm->ncols; j++)
196 >                for (k = rm->ncomp; k-- > 0; )
197 >                        if (fscanf(fp, rmx_scanfmt, 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;
206 <        float   val[100];
205 > #if DTrmx_native==DTfloat
206 >        if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols)
207 >                return(0);
208 >        if (rm->pflags & RMF_SWAPIN)
209 >                swap32((char *)drp, rm->ncols*rm->ncomp);
210 > #else
211 >        int     j, k;
212 >        float   val[MAXCOMP];
213  
214 <        if (rm->ncomp > 100) {
214 >        if (rm->ncomp > MAXCOMP) {
215                  fputs("Unsupported # components in rmx_load_float()\n", stderr);
216                  exit(1);
217          }
218 <        if (!rmx_prepare(rm))
202 <                return(0);
203 <        for (i = 0; i < rm->nrows; i++)
204 <            for (j = 0; j < rm->ncols; j++) {
205 <                double  *dp = rmx_lval(rm,i,j);
218 >        for (j = 0; j < rm->ncols; j++) {
219                  if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
207                    return(0);
208                if (rm->swapin)
209                    swap32((char *)val, rm->ncomp);
210                for (k = rm->ncomp; k--; )
211                     dp[k] = val[k];
212            }
213        return(1);
214 }
215
216 static int
217 rmx_load_double(RMATRIX *rm, FILE *fp)
218 {
219        int     i;
220 #ifdef MAP_FILE
221        long    pos;            /* map memory for file > 1MB if possible */
222        if (!rm->swapin && array_size(rm) >= 1L<<20 &&
223                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) {
224                rm->mapped = mmap(NULL, array_size(rm)+pos, PROT_READ|PROT_WRITE,
225                                        MAP_PRIVATE, fileno(fp), 0);
226                if (rm->mapped != MAP_FAILED) {
227                        rm->mtx = (double *)rm->mapped + pos/sizeof(double);
228                        return(1);
229                }               /* else fall back on reading into memory */
230                rm->mapped = NULL;
231        }
232 #endif
233        if (!rmx_prepare(rm))
234                return(0);
235        for (i = 0; i < rm->nrows; i++) {
236                if (getbinary(rmx_lval(rm,i,0), sizeof(double)*rm->ncomp,
237                                        rm->ncols, fp) != rm->ncols)
220                          return(0);
221 <                if (rm->swapin)
222 <                        swap64((char *)rmx_lval(rm,i,0), rm->ncols*rm->ncomp);
221 >                if (rm->pflags & RMF_SWAPIN)
222 >                        swap32((char *)val, rm->ncomp);
223 >                for (k = 0; k < rm->ncomp; k++)
224 >                        *drp++ = val[k];
225          }
226 + #endif
227          return(1);
228   }
229  
230   static int
231 < rmx_load_rgbe(RMATRIX *rm, FILE *fp)
231 > rmx_load_double(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
232   {
233 <        COLOR   *scan = (COLOR *)malloc(sizeof(COLOR)*rm->ncols);
234 <        int     i, j;
250 <
251 <        if (!scan)
233 > #if DTrmx_native==DTdouble
234 >        if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols)
235                  return(0);
236 <        if (!rmx_prepare(rm)) {
237 <                free(scan);
238 <                return(0);
236 >        if (rm->pflags & RMF_SWAPIN)
237 >                swap64((char *)drp, rm->ncols*rm->ncomp);
238 > #else
239 >        int     j, k;
240 >        double  val[MAXCOMP];
241 >
242 >        if (rm->ncomp > MAXCOMP) {
243 >                fputs("Unsupported # components in rmx_load_double()\n", stderr);
244 >                exit(1);
245          }
246 <        for (i = 0; i < rm->nrows; i++) {
247 <            double      *dp = rmx_lval(rm,i,0);
248 <            if (freadscan(scan, rm->ncols, fp) < 0) {
249 <                free(scan);
250 <                return(0);
251 <            }
252 <            for (j = 0; j < rm->ncols; j++, dp += 3) {
264 <                dp[0] = colval(scan[j],RED);
265 <                dp[1] = colval(scan[j],GRN);
266 <                dp[2] = colval(scan[j],BLU);
267 <            }
246 >        for (j = 0; j < rm->ncols; j++) {
247 >                if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp)
248 >                        return(0);
249 >                if (rm->pflags & RMF_SWAPIN)
250 >                        swap64((char *)val, rm->ncomp);
251 >                for (k = 0; k < rm->ncomp; k++)
252 >                        *drp++ = (float)val[k];
253          }
254 <        free(scan);
254 > #endif
255          return(1);
256   }
257  
258 + #if DTrmx_native==DTfloat
259 + #define rmx_load_spec(dp,rm,fp) (freadsscan(dp,(rm)->ncomp,(rm)->ncols,fp) >= 0)
260 + #else
261   static int
262 < rmx_load_spec(RMATRIX *rm, FILE *fp)
262 > rmx_load_spec(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
263   {
264 <        uby8    *scan;
265 <        SCOLOR  scol;
266 <        int     i, j, k;
264 >        COLRV   *scan;
265 >        COLORV  scol[MAXCOMP];
266 >        int     j, k;
267  
268 <        if (rm->ncomp < 3)
268 >        if ((rm->ncomp < 3) | (rm->ncomp > MAXCOMP))
269                  return(0);
270 <        scan = (uby8 *)malloc((rm->ncomp+1)*rm->ncols);
270 >        scan = (COLRV *)tempbuffer((rm->ncomp+1)*rm->ncols);
271          if (!scan)
272                  return(0);
273 <        if (!rmx_prepare(rm)) {
286 <                free(scan);
273 >        if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0)
274                  return(0);
275 +        for (j = 0; j < rm->ncols; j++) {
276 +                scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp);
277 +                for (k = 0; k < rm->ncomp; k++)
278 +                        *drp++ = scol[k];
279          }
289        for (i = 0; i < rm->nrows; i++) {
290            double      *dp = rmx_lval(rm,i,0);
291            if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) {
292                free(scan);
293                return(0);
294            }
295            for (j = 0; j < rm->ncols; j++) {
296                scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp);
297                for (k = 0; k < rm->ncomp; k++)
298                        *dp++ = scol[k];
299            }
300        }
301        free(scan);
280          return(1);
281   }
282 + #endif
283  
284   /* Read matrix header from input stream (cannot be XML) */
285   int
# Line 308 | Line 287 | rmx_load_header(RMATRIX *rm, FILE *fp)
287   {
288          if (!rm | !fp)
289                  return(0);
290 <        if (rm->info) {                         /* clear state */
312 <                free(rm->info);
313 <                rm->info = NULL;
314 <        }
315 <        if (rm->mtx) {                          /* ...and data */
316 < #ifdef MAP_FILE
317 <                if (rm->mapped) {
318 <                        munmap(rm->mapped, mapped_size(rm));
319 <                        rm->mapped = NULL;
320 <                } else
321 < #endif
322 <                        free(rm->mtx);
323 <                rm->mtx = NULL;
324 <        }
290 >        rmx_reset(rm);                          /* clear state */
291          if (rm->nrows | rm->ncols | !rm->dtype) {
292                  rm->nrows = rm->ncols = 0;
293                  rm->ncomp = 3;
294                  setcolor(rm->cexp, 1.f, 1.f, 1.f);
295                  memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart));
296 <                rm->swapin = 0;
296 >                rm->pflags = 0;
297          }
332        SET_FILE_BINARY(fp);
298          rm->dtype = DTascii;                    /* assumed w/o FORMAT */
299          if (getheader(fp, get_dminfo, rm) < 0) {
300 <                fputs("Unrecognized matrix format\n", stderr);
300 >                fputs("Bad matrix header\n", stderr);
301                  return(0);
302          }
303 <                                                /* resolution string? */
304 <        if ((rm->nrows <= 0) | (rm->ncols <= 0)) {
305 <                if (!fscnresolu(&rm->ncols, &rm->nrows, fp))
306 <                        return(0);
307 <                if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
308 <                                rm->ncomp != 3)
309 <                        return(0);
310 <        }
303 >        if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) &&
304 >                        rm->ncomp != 3)
305 >                return(0);
306 >        if (rm->ncols <= 0 &&                   /* resolution string? */
307 >                        !fscnresolu(&rm->ncols, &rm->nrows, fp))
308 >                return(0);
309 >        if (rm->dtype == DTascii)               /* set file type (WINDOWS) */
310 >                SET_FILE_TEXT(fp);
311 >        else
312 >                SET_FILE_BINARY(fp);
313          return(1);
314   }
315  
316 < /* Allocate & load post-header data from stream given type set in rm->dtype */
316 > /* Load next row as rmx_dtype (cannot be XML) */
317   int
318 < rmx_load_data(RMATRIX *rm, FILE *fp)
318 > rmx_load_row(rmx_dtype *drp, const RMATRIX *rm, FILE *fp)
319   {
320          switch (rm->dtype) {
321          case DTascii:
322 <                SET_FILE_TEXT(fp);
356 <                return(rmx_load_ascii(rm, fp));
322 >                return(rmx_load_ascii(drp, rm, fp));
323          case DTfloat:
324 <                return(rmx_load_float(rm, fp));
324 >                return(rmx_load_float(drp, rm, fp));
325          case DTdouble:
326 <                return(rmx_load_double(rm, fp));
326 >                return(rmx_load_double(drp, rm, fp));
327          case DTrgbe:
328          case DTxyze:
363                return(rmx_load_rgbe(rm, fp));
329          case DTspec:
330 <                return(rmx_load_spec(rm, fp));
330 >                return(rmx_load_spec(drp, rm, fp));
331          default:
332 <                fputs("Unsupported data type in rmx_loaddata()\n", stderr);
332 >                fputs("Unsupported data type in rmx_load_row()\n", stderr);
333          }
334          return(0);
335   }
336  
337 + /* Allocate & load post-header data from stream given type set in rm->dtype */
338 + int
339 + rmx_load_data(RMATRIX *rm, FILE *fp)
340 + {
341 +        int     i;
342 + #ifdef MAP_FILE
343 +        long    pos;            /* map memory for file > 1MB if possible */
344 +        if ((rm->dtype == DTrmx_native) & !(rm->pflags & RMF_SWAPIN) &
345 +                        (rmx_array_size(rm) >= 1L<<20) &&
346 +                        (pos = ftell(fp)) >= 0 && !(pos % sizeof(rmx_dtype))) {
347 +                rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE,
348 +                                        MAP_PRIVATE, fileno(fp), 0);
349 +                if (rm->mapped != MAP_FAILED) {
350 +                        if (rm->pflags & RMF_FREEMEM)
351 +                                free(rm->mtx);
352 +                        rm->mtx = (rmx_dtype *)rm->mapped + pos/sizeof(rmx_dtype);
353 +                        rm->pflags &= ~RMF_FREEMEM;
354 +                        return(1);
355 +                }               /* else fall back on reading into memory */
356 +                rm->mapped = NULL;
357 +        }
358 + #endif
359 +        if (!rmx_prepare(rm)) { /* need in-core matrix array */
360 +                fprintf(stderr, "Cannot allocate %g MByte matrix array\n",
361 +                                (1./(1L<<20))*(double)rmx_array_size(rm));
362 +                return(0);
363 +        }
364 +        for (i = 0; i < rm->nrows; i++)
365 +                if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp))
366 +                        return(0);
367 +        return(1);
368 + }
369 +
370   /* Load matrix from supported file type */
371   RMATRIX *
372 < rmx_load(const char *inspec, RMPref rmp)
372 > rmx_load(const char *inspec)
373   {
374          FILE            *fp;
375          RMATRIX         *dnew;
# Line 385 | Line 383 | rmx_load(const char *inspec, RMPref rmp)
383                  fp = stdin;
384          else if (inspec[0] == '!')
385                  fp = popen(inspec+1, "r");
386 <        else {
389 <                const char      *sp = inspec;   /* check suffix */
390 <                while (*sp)
391 <                        ++sp;
392 <                while (sp > inspec && sp[-1] != '.')
393 <                        --sp;
394 <                if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
395 <                        CMATRIX *cm = rmp==RMPtrans ? cm_loadBTDF(inspec) :
396 <                                        cm_loadBRDF(inspec, rmp==RMPreflB) ;
397 <                        if (!cm)
398 <                                return(NULL);
399 <                        dnew = rmx_from_cmatrix(cm);
400 <                        cm_free(cm);
401 <                        dnew->dtype = DTascii;
402 <                        return(dnew);           /* return here */
403 <                }                               /* else open it ourselves */
386 >        else
387                  fp = fopen(inspec, "r");
388 <        }
389 <        if (!fp)
388 >        if (!fp) {
389 >                fprintf(stderr, "Cannot open for reading: %s\n", inspec);
390                  return(NULL);
391 +        }
392   #ifdef getc_unlocked
393          flockfile(fp);
394   #endif
395 <                                                /* load header info */
395 >        SET_FILE_BINARY(fp);                    /* load header info */
396          if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) {
397                  fprintf(stderr, "Bad header in: %s\n", inspec);
398                  if (inspec[0] == '!') pclose(fp);
# Line 420 | Line 404 | rmx_load(const char *inspec, RMPref rmp)
404  
405          if (fp != stdin) {                      /* close input stream */
406                  if (inspec[0] == '!')
407 <                        pclose(fp);
407 >                        ok &= pclose(fp)==0;
408                  else
409                          fclose(fp);
410          }
# Line 436 | Line 420 | rmx_load(const char *inspec, RMPref rmp)
420                                                  /* undo exposure? */
421          if ((dnew->cexp[0] != 1.f) |
422                          (dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) {
423 <                double  cmlt[MAXCSAMP];
423 >                double  cmlt[MAXCOMP];
424                  int     i;
425 <                cmlt[0] = 1./dnew->cexp[0];
442 <                cmlt[1] = 1./dnew->cexp[1];
443 <                cmlt[2] = 1./dnew->cexp[2];
444 <                if (dnew->ncomp > MAXCSAMP) {
425 >                if (dnew->ncomp > MAXCOMP) {
426                          fprintf(stderr, "Excess spectral components in: %s\n",
427                                          inspec);
428                          rmx_free(dnew);
429                          return(NULL);
430                  }
431 +                cmlt[0] = 1./dnew->cexp[0];
432 +                cmlt[1] = 1./dnew->cexp[1];
433 +                cmlt[2] = 1./dnew->cexp[2];
434                  for (i = dnew->ncomp; i-- > 3; )
435 <                        cmlt[i] = cmlt[1];
435 >                        cmlt[i] = cmlt[1];      /* XXX hack! */
436                  rmx_scale(dnew, cmlt);
437                  setcolor(dnew->cexp, 1.f, 1.f, 1.f);
438          }
439          return(dnew);
440   }
441  
442 + #if DTrmx_native==DTdouble
443   static int
444 < rmx_write_ascii(const RMATRIX *rm, FILE *fp)
444 > rmx_write_float(const rmx_dtype *dp, int len, FILE *fp)
445   {
446 <        const char      *fmt = (rm->dtype == DTfloat) ? " %.7e" :
462 <                        (rm->dtype == DTrgbe) | (rm->dtype == DTxyze) |
463 <                        (rm->dtype == DTspec) ? " %.3e" : " %.15e" ;
464 <        int     i, j, k;
446 >        float   val;
447  
448 <        for (i = 0; i < rm->nrows; i++) {
449 <            for (j = 0; j < rm->ncols; j++) {
450 <                const double    *dp = rmx_val(rm,i,j);
451 <                for (k = 0; k < rm->ncomp; k++)
470 <                    fprintf(fp, fmt, dp[k]);
471 <                fputc('\t', fp);
472 <            }
473 <            fputc('\n', fp);
448 >        while (len--) {
449 >                val = (float)*dp++;
450 >                if (putbinary(&val, sizeof(val), 1, fp) != 1)
451 >                        return(0);
452          }
453          return(1);
454   }
455 <
455 > #else
456   static int
457 < rmx_write_float(const RMATRIX *rm, FILE *fp)
457 > rmx_write_double(const rmx_dtype *dp, int len, FILE *fp)
458   {
459 <        int     i, j, k;
482 <        float   val[100];
459 >        double  val;
460  
461 <        if (rm->ncomp > 100) {
462 <                fputs("Unsupported # components in rmx_write_float()\n", stderr);
463 <                exit(1);
487 <        }
488 <        for (i = 0; i < rm->nrows; i++)
489 <            for (j = 0; j < rm->ncols; j++) {
490 <                const double    *dp = rmx_val(rm,i,j);
491 <                for (k = rm->ncomp; k--; )
492 <                    val[k] = (float)dp[k];
493 <                if (putbinary(val, sizeof(float), rm->ncomp, fp) != rm->ncomp)
461 >        while (len--) {
462 >                val = *dp++;
463 >                if (putbinary(&val, sizeof(val), 1, fp) != 1)
464                          return(0);
465 <            }
465 >        }
466          return(1);
467   }
468 + #endif
469  
470   static int
471 < rmx_write_double(const RMATRIX *rm, FILE *fp)
471 > rmx_write_ascii(const rmx_dtype *dp, int ncomp, int len, FILE *fp)
472   {
473 <        int     i;
474 <
475 <        for (i = 0; i < rm->nrows; i++)
476 <                if (putbinary(rmx_val(rm,i,0), sizeof(double)*rm->ncomp,
477 <                                        rm->ncols, fp) != rm->ncols)
478 <                        return(0);
479 <        return(1);
473 >        while (len-- > 0) {
474 >                int     k = ncomp;
475 >                while (k-- > 0)
476 >                        fprintf(fp, " %.7e", *dp++);
477 >                fputc('\t', fp);
478 >        }
479 >        return(fputc('\n', fp) != EOF);
480   }
481  
482   static int
483 < rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
483 > rmx_write_rgbe(const rmx_dtype *dp, int ncomp, int len, FILE *fp)
484   {
485 <        COLR    *scan = (COLR *)malloc(sizeof(COLR)*rm->ncols);
486 <        int     i, j;
485 >        COLR    *scan;
486 >        int     j;
487  
488 <        if (!scan)
489 <                return(0);
490 <        for (i = 0; i < rm->nrows; i++) {
491 <            for (j = rm->ncols; j--; ) {
492 <                const double    *dp = rmx_val(rm,i,j);
493 <                if (rm->ncomp == 1)
488 >        if ((ncomp != 1) & (ncomp != 3)) return(0);
489 >        scan = (COLR *)tempbuffer(sizeof(COLR)*len);
490 >        if (!scan) return(0);
491 >
492 >        for (j = 0; j < len; j++, dp += ncomp)
493 >                if (ncomp == 1)
494                          setcolr(scan[j], dp[0], dp[0], dp[0]);
495                  else
496                          setcolr(scan[j], dp[0], dp[1], dp[2]);
497 <            }
498 <            if (fwritecolrs(scan, rm->ncols, fp) < 0) {
528 <                free(scan);
529 <                return(0);
530 <            }
531 <        }
532 <        free(scan);
533 <        return(1);
497 >
498 >        return(fwritecolrs(scan, len, fp) >= 0);
499   }
500  
501 + #if DTrmx_native==DTfloat
502 + #define rmx_write_spec(dp,nc,ln,fp)     (fwritesscan(dp,nc,ln,fp) >= 0)
503 + #else
504   static int
505 < rmx_write_spec(const RMATRIX *rm, FILE *fp)
505 > rmx_write_spec(const rmx_dtype *dp, int ncomp, int len, FILE *fp)
506   {
507 <        uby8    *scan = (uby8 *)malloc((rm->ncomp+1)*rm->ncols);
508 <        int     ok = 1;
509 <        SCOLOR  scol;
542 <        int     i, j, k;
507 >        COLRV   *scan;
508 >        COLORV  scol[MAXCOMP];
509 >        int     j, k;
510  
511 <        if (!scan)
512 <                return(0);
513 <        for (i = 0; i < rm->nrows; i++) {
514 <            for (j = rm->ncols; j--; ) {
515 <                const double    *dp = rmx_val(rm,i,j);
549 <                for (k = rm->ncomp; k--; )
511 >        if ((ncomp < 3) | (ncomp > MAXCOMP)) return(0);
512 >        scan = (COLRV *)tempbuffer((ncomp+1)*len);
513 >        if (!scan) return(0);
514 >        for (j = 0; j < len; j++, dp += ncomp) {
515 >                for (k = ncomp; k--; )
516                          scol[k] = dp[k];
517 <                scolor2scolr(scan+j*(rm->ncomp+1), scol, rm->ncomp);
552 <            }
553 <            if (fwritescolrs(scan, rm->ncomp, rm->ncols, fp) < 0) {
554 <                ok = 0;
555 <                break;
556 <            }
517 >                scolor2scolr(scan+j*(ncomp+1), scol, ncomp);
518          }
519 <        free(scan);
559 <        return(ok);
519 >        return(fwritescolrs(scan, ncomp, len, fp) >= 0);
520   }
521 + #endif
522  
523   /* Check if CIE XYZ primaries were specified */
524   static int
# Line 576 | Line 537 | findCIEprims(const char *info)
537                          (prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01));
538   }
539  
540 < /* Write matrix to file type indicated by dtype */
540 > /* Finish writing header data with resolution and format, returning type used */
541   int
542 < rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
542 > rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp)
543   {
544 <        int     ok = 1;
584 <
585 <        if (!rm | !fp || !rm->mtx)
544 >        if (!rm | !fp || rm->ncols <= 0)
545                  return(0);
587 #ifdef getc_unlocked
588        flockfile(fp);
589 #endif
590                                                /* complete header */
546          if (rm->info)
547                  fputs(rm->info, fp);
548 <        if (dtype == DTfromHeader)
548 >        if (dtype == DTfromHeader) {
549                  dtype = rm->dtype;
550 <        else if (dtype == DTrgbe && (rm->dtype == DTxyze ||
550 > #if DTrmx_native==DTfloat
551 >                if (dtype == DTdouble)          /* but stored as float? */
552 >                        dtype = DTfloat;
553 > #endif
554 >        } else if (dtype == DTrgbe && (rm->dtype == DTxyze ||
555                                          findCIEprims(rm->info)))
556                  dtype = DTxyze;
557          else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
558                  dtype = DTrgbe;
559 +        if ((dtype < DTspec) & (rm->ncomp > 3))
560 +                dtype = DTspec;
561 +        else if ((dtype == DTspec) & (rm->ncomp <= 3))
562 +                return(0);
563 +
564 +        if (dtype == DTascii)                   /* set file type (WINDOWS) */
565 +                SET_FILE_TEXT(fp);
566 +        else
567 +                SET_FILE_BINARY(fp);
568                                                  /* write exposure? */
569          if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) |
570                          (rm->cexp[GRN] != rm->cexp[BLU]))
571                  fputcolcor(rm->cexp, fp);
572          else if (rm->cexp[GRN] != 1.f)
573                  fputexpos(rm->cexp[GRN], fp);
574 <        if ((dtype != DTrgbe) & (dtype != DTxyze)) {
575 <                if (dtype == DTspec) {
576 <                        if (rm->ncomp < 3)
609 <                                return(0);      /* bad # components */
610 <                        fputwlsplit(rm->wlpart, fp);
611 <                } else {
574 >                                                /* matrix size? */
575 >        if ((dtype > DTspec) | (rm->nrows <= 0)) {
576 >                if (rm->nrows > 0)
577                          fprintf(fp, "NROWS=%d\n", rm->nrows);
578 <                        fprintf(fp, "NCOLS=%d\n", rm->ncols);
579 <                }
578 >                fprintf(fp, "NCOLS=%d\n", rm->ncols);
579 >        }
580 >        if (dtype >= DTspec) {                  /* # components & split? */
581                  fputncomp(rm->ncomp, fp);
582 +                if (rm->ncomp > 3 &&
583 +                                memcmp(rm->wlpart, WLPART, sizeof(WLPART)))
584 +                        fputwlsplit(rm->wlpart, fp);
585          } else if ((rm->ncomp != 3) & (rm->ncomp != 1))
586                  return(0);                      /* wrong # components */
587          if ((dtype == DTfloat) | (dtype == DTdouble))
588                  fputendian(fp);                 /* important to record */
589          fputformat(cm_fmt_id[dtype], fp);
590 <        fputc('\n', fp);
591 <        switch (dtype) {                        /* write data */
592 <        case DTascii:
593 <                ok = rmx_write_ascii(rm, fp);
594 <                break;
590 >        fputc('\n', fp);                        /* end of header */
591 >        if ((dtype <= DTspec) & (rm->nrows > 0))
592 >                fprtresolu(rm->ncols, rm->nrows, fp);
593 >        return(dtype);
594 > }
595 >
596 > /* Write out matrix data (usually by row) */
597 > int
598 > rmx_write_data(const rmx_dtype *dp, int ncomp, int len, int dtype, FILE *fp)
599 > {
600 >        switch (dtype) {
601 > #if DTrmx_native==DTdouble
602          case DTfloat:
603 <                ok = rmx_write_float(rm, fp);
604 <                break;
603 >                return(rmx_write_float(dp, ncomp*len, fp));
604 > #else
605          case DTdouble:
606 <                ok = rmx_write_double(rm, fp);
607 <                break;
606 >                return(rmx_write_double(dp, ncomp*len, fp));
607 > #endif
608 >        case DTrmx_native:
609 >                return(putbinary(dp, sizeof(*dp)*ncomp, len, fp) == len);
610 >        case DTascii:
611 >                return(rmx_write_ascii(dp, ncomp, len, fp));
612          case DTrgbe:
613          case DTxyze:
614 <                fprtresolu(rm->ncols, rm->nrows, fp);
635 <                ok = rmx_write_rgbe(rm, fp);
636 <                break;
614 >                return(rmx_write_rgbe(dp, ncomp, len, fp));
615          case DTspec:
616 <                fprtresolu(rm->ncols, rm->nrows, fp);
639 <                ok = rmx_write_spec(rm, fp);
640 <                break;
641 <        default:
642 <                return(0);
616 >                return(rmx_write_spec(dp, ncomp, len, fp));
617          }
618 <        ok &= (fflush(fp) == 0);
618 >        return(0);
619 > }
620 >
621 > /* Write matrix using file format indicated by dtype */
622 > int
623 > rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
624 > {
625 >        int     ok = 0;
626 >        int     i;
627 >                                                /* complete header */
628 >        dtype = rmx_write_header(rm, dtype, fp);
629 >        if (dtype <= 0)
630 >                return(0);
631   #ifdef getc_unlocked
632 +        flockfile(fp);
633 + #endif
634 +        if (dtype == DTrmx_native)              /* write all at once? */
635 +                ok = rmx_write_data(rm->mtx, rm->ncomp,
636 +                                rm->nrows*rm->ncols, dtype, fp);
637 +        else                                    /* else row by row */
638 +                for (i = 0; i < rm->nrows; i++) {
639 +                        ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp,
640 +                                        rm->ncols, dtype, fp);
641 +                        if (!ok) break;
642 +                }
643 +
644 +        if (ok) ok = (fflush(fp) == 0);
645 + #ifdef getc_unlocked
646          funlockfile(fp);
647   #endif
648          if (!ok) fputs("Error writing matrix\n", stderr);
# Line 658 | Line 658 | rmx_identity(const int dim, const int n)
658  
659          if (!rid)
660                  return(NULL);
661 <        memset(rid->mtx, 0, array_size(rid));
661 >        memset(rid->mtx, 0, rmx_array_size(rid));
662          for (i = dim; i--; ) {
663 <            double      *dp = rmx_lval(rid,i,i);
663 >            rmx_dtype   *dp = rmx_lval(rid,i,i);
664              for (k = n; k--; )
665                  dp[k] = 1.;
666          }
667          return(rid);
668   }
669  
670 < /* Duplicate the given matrix */
670 > /* Duplicate the given matrix (may be unallocated) */
671   RMATRIX *
672   rmx_copy(const RMATRIX *rm)
673   {
# Line 675 | Line 675 | rmx_copy(const RMATRIX *rm)
675  
676          if (!rm)
677                  return(NULL);
678 <        dnew = rmx_alloc(rm->nrows, rm->ncols, rm->ncomp);
678 >        dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp);
679          if (!dnew)
680                  return(NULL);
681 +        if (rm->mtx) {
682 +                if (!rmx_prepare(dnew)) {
683 +                        rmx_free(dnew);
684 +                        return(NULL);
685 +                }
686 +                memcpy(dnew->mtx, rm->mtx, rmx_array_size(dnew));
687 +        }
688          rmx_addinfo(dnew, rm->info);
689          dnew->dtype = rm->dtype;
690          copycolor(dnew->cexp, rm->cexp);
691          memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
685        memcpy(dnew->mtx, rm->mtx, array_size(dnew));
692          return(dnew);
693   }
694  
695 < /* Allocate and assign transposed matrix */
696 < RMATRIX *
697 < rmx_transpose(const RMATRIX *rm)
695 > /* Replace data in first matrix with data from second */
696 > int
697 > rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa)
698   {
699 <        RMATRIX *dnew;
700 <        int     i, j;
699 >        if (!rdst | !rsrc)
700 >                return(0);
701 >        if (dometa) {           /* transfer everything? */
702 >                rmx_reset(rdst);
703 >                *rdst = *rsrc;
704 >                rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL;
705 >                return(1);
706 >        }
707 >                                /* just matrix data -- leave metadata */
708 >        if ((rdst->nrows != rsrc->nrows) |
709 >                        (rdst->ncols != rsrc->ncols) |
710 >                        (rdst->ncomp != rsrc->ncomp))
711 >                return(0);
712 > #ifdef MAP_FILE
713 >        if (rdst->mapped)
714 >                munmap(rdst->mapped, rmx_mapped_size(rdst));
715 >        else
716 > #endif
717 >        if (rdst->pflags & RMF_FREEMEM) {
718 >                free(rdst->mtx);
719 >                rdst->pflags &= ~RMF_FREEMEM;
720 >        }
721 >        rdst->mapped = rsrc->mapped;
722 >        rdst->mtx = rsrc->mtx;
723 >        rdst->pflags |= rsrc->pflags & RMF_FREEMEM;
724 >        rsrc->mapped = NULL; rsrc->mtx = NULL;
725 >        return(1);
726 > }
727  
728 <        if (!rm)
728 > /* Transpose the given matrix */
729 > int
730 > rmx_transpose(RMATRIX *rm)
731 > {
732 >        uby8            *bmap;
733 >        rmx_dtype       val[MAXCOMP];
734 >        RMATRIX         dold;
735 >        int             i, j;
736 >
737 >        if (!rm || !rm->mtx | (rm->ncomp > MAXCOMP))
738                  return(0);
739 <        if ((rm->nrows == 1) | (rm->ncols == 1)) {
740 <                dnew = rmx_copy(rm);
741 <                if (!dnew)
742 <                        return(NULL);
743 <                dnew->nrows = rm->ncols;
744 <                dnew->ncols = rm->nrows;
745 <                return(dnew);
739 >        if (rm->info)
740 >                rmx_addinfo(rm, "Transposed rows and columns\n");
741 >        if ((rm->nrows == 1) | (rm->ncols == 1)) { /* vector? */
742 >                j = rm->ncols;
743 >                rm->ncols = rm->nrows;
744 >                rm->nrows = j;
745 >                return(1);
746          }
747 <        dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp);
748 <        if (!dnew)
749 <                return(NULL);
750 <        if (rm->info) {
751 <                rmx_addinfo(dnew, rm->info);
752 <                rmx_addinfo(dnew, "Transposed rows and columns\n");
747 >        if (rm->nrows == rm->ncols) {   /* square matrix case */
748 >             for (i = rm->nrows; --i > 0; )
749 >                for (j = i; j-- > 0; ) {
750 >                    memcpy(val, rmx_val(rm,i,j),
751 >                                sizeof(rmx_dtype)*rm->ncomp);
752 >                    memcpy(rmx_lval(rm,i,j), rmx_val(rm,j,i),
753 >                                sizeof(rmx_dtype)*rm->ncomp);
754 >                    memcpy(rmx_lval(rm,j,i), val,
755 >                                sizeof(rmx_dtype)*rm->ncomp);
756 >                }
757 >            return(1);
758          }
759 <        dnew->dtype = rm->dtype;
760 <        copycolor(dnew->cexp, rm->cexp);
761 <        memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart));
762 <        for (j = dnew->ncols; j--; )
763 <            for (i = dnew->nrows; i--; )
764 <                memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i),
765 <                                sizeof(double)*dnew->ncomp);
766 <        return(dnew);
759 > #define bmbyte(r,c)     bmap[((r)*rm->ncols+(c))>>3]
760 > #define bmbit(r,c)      (1 << ((r)*rm->ncols+(c) & 7))
761 > #define bmop(r,c, op)   (bmbyte(r,c) op bmbit(r,c))
762 > #define bmtest(r,c)     bmop(r,c,&)
763 > #define bmset(r,c)      bmop(r,c,|=)
764 >                                        /* loop completion bitmap */
765 >        bmap = (uby8 *)calloc(((size_t)rm->nrows*rm->ncols+7)>>3, 1);
766 >        if (!bmap)
767 >                return(0);
768 >        dold = *rm;
769 >        rm->ncols = dold.nrows; rm->nrows = dold.ncols;
770 >        for (i = rm->nrows; i--; )      /* try every starting point */
771 >            for (j = rm->ncols; j--; ) {
772 >                int     i0, j0;
773 >                int     i1 = i;
774 >                size_t  j1 = j;
775 >                if (bmtest(i, j))
776 >                        continue;       /* traversed loop earlier */
777 >                memcpy(val, rmx_val(rm,i,j),
778 >                        sizeof(rmx_dtype)*rm->ncomp);
779 >                for ( ; ; ) {           /* new transpose loop */
780 >                    const rmx_dtype     *ds;
781 >                    i0 = i1; j0 = j1;
782 >                    ds = rmx_val(&dold, j0, i0);
783 >                    j1 = (ds - dold.mtx)/dold.ncomp;
784 >                    i1 = j1 / rm->ncols;
785 >                    j1 -= (size_t)i1*rm->ncols;
786 >                    bmset(i1, j1);      /* mark as done */
787 >                    if ((i1 == i) & (j1 == j))
788 >                        break;          /* back at start */
789 >                    memcpy(rmx_lval(rm,i0,j0), ds,
790 >                                sizeof(rmx_dtype)*rm->ncomp);
791 >                }                       /* complete the loop */
792 >                memcpy(rmx_lval(rm,i0,j0), val,
793 >                        sizeof(rmx_dtype)*rm->ncomp);
794 >            }
795 >        free(bmap);                     /* all done! */
796 >        return(1);
797 > #undef  bmbyte
798 > #undef  bmbit
799 > #undef  bmop
800 > #undef  bmtest
801 > #undef  bmset
802   }
803  
804   /* Multiply (concatenate) two matrices and allocate the result */
# Line 727 | Line 808 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
808          RMATRIX *mres;
809          int     i, j, k, h;
810  
811 <        if (!m1 | !m2 || (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
811 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
812 >                        (m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows))
813                  return(NULL);
814          mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp);
815          if (!mres)
# Line 742 | Line 824 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
824                  for (k = mres->ncomp; k--; ) {
825                      double      d = 0;
826                      for (h = m1->ncols; h--; )
827 <                        d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k];
828 <                    rmx_lval(mres,i,j)[k] = d;
827 >                        d += (double)rmx_val(m1,i,h)[k] *
828 >                                        rmx_val(m2,h,j)[k];
829 >                    rmx_lval(mres,i,j)[k] = (rmx_dtype)d;
830                  }
831          return(mres);
832   }
# Line 755 | Line 838 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
838          int     zeroDivides = 0;
839          int     i, j, k;
840  
841 <        if (!m1 | !m2 || (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
841 >        if (!m1 | !m2 || !m1->mtx | !m2->mtx |
842 >                         (m1->ncols != m2->ncols) | (m1->nrows != m2->nrows))
843                  return(0);
844          if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp))
845                  return(0);
# Line 767 | Line 851 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
851          for (i = m1->nrows; i--; )
852              for (j = m1->ncols; j--; )
853                  if (divide) {
854 <                    double      d;
854 >                    rmx_dtype   d;
855                      if (m2->ncomp == 1) {
856                          d = rmx_val(m2,i,j)[0];
857                          if (d == 0) {
# Line 790 | Line 874 | rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide
874                          }
875                  } else {
876                      if (m2->ncomp == 1) {
877 <                        const double    d = rmx_val(m2,i,j)[0];
877 >                        const rmx_dtype d = rmx_val(m2,i,j)[0];
878                          for (k = m1->ncomp; k--; )
879                              rmx_lval(m1,i,j)[k] *= d;
880                      } else
# Line 811 | Line 895 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
895          double  *mysf = NULL;
896          int     i, j, k;
897  
898 <        if (!msum | !madd ||
898 >        if (!msum | !madd || !msum->mtx | !madd->mtx |
899                          (msum->nrows != madd->nrows) |
900                          (msum->ncols != madd->ncols) |
901                          (msum->ncomp != madd->ncomp))
# Line 831 | Line 915 | rmx_sum(RMATRIX *msum, const RMATRIX *madd, const doub
915                  rmx_addinfo(msum, rmx_mismatch_warn);
916          for (i = msum->nrows; i--; )
917              for (j = msum->ncols; j--; ) {
918 <                const double    *da = rmx_val(madd,i,j);
919 <                double          *ds = rmx_lval(msum,i,j);
918 >                const rmx_dtype *da = rmx_val(madd,i,j);
919 >                rmx_dtype       *ds = rmx_lval(msum,i,j);
920                  for (k = msum->ncomp; k--; )
921 <                     ds[k] += sf[k] * da[k];
921 >                     ds[k] += (rmx_dtype)sf[k] * da[k];
922              }
923          if (mysf)
924                  free(mysf);
# Line 847 | Line 931 | rmx_scale(RMATRIX *rm, const double sf[])
931   {
932          int     i, j, k;
933  
934 <        if (!rm | !sf)
934 >        if (!rm | !sf || !rm->mtx)
935                  return(0);
936          for (i = rm->nrows; i--; )
937              for (j = rm->ncols; j--; ) {
938 <                double  *dp = rmx_lval(rm,i,j);
938 >                rmx_dtype       *dp = rmx_lval(rm,i,j);
939                  for (k = rm->ncomp; k--; )
940 <                    dp[k] *= sf[k];
940 >                    dp[k] *= (rmx_dtype)sf[k];
941              }
942          if (rm->info)
943                  rmx_addinfo(rm, "Applied scalar\n");
# Line 868 | Line 952 | rmx_transform(const RMATRIX *msrc, int n, const double
952          int     i, j, ks, kd;
953          RMATRIX *dnew;
954  
955 <        if (!msrc | (n <= 0) | !cmat)
955 >        if (!msrc | (n <= 0) | !cmat || !msrc->mtx)
956                  return(NULL);
957          dnew = rmx_alloc(msrc->nrows, msrc->ncols, n);
958          if (!dnew)
# Line 883 | Line 967 | rmx_transform(const RMATRIX *msrc, int n, const double
967          dnew->dtype = msrc->dtype;
968          for (i = dnew->nrows; i--; )
969              for (j = dnew->ncols; j--; ) {
970 <                const double    *ds = rmx_val(msrc,i,j);
970 >                const rmx_dtype *ds = rmx_val(msrc,i,j);
971                  for (kd = dnew->ncomp; kd--; ) {
972                      double      d = 0;
973                      for (ks = msrc->ncomp; ks--; )
974                          d += cmat[kd*msrc->ncomp + ks] * ds[ks];
975 <                    rmx_lval(dnew,i,j)[kd] = d;
975 >                    rmx_lval(dnew,i,j)[kd] = (rmx_dtype)d;
976                  }
977              }
978          return(dnew);
979   }
980  
897 /* Convert a color matrix to newly allocated RMATRIX buffer */
898 RMATRIX *
899 rmx_from_cmatrix(const CMATRIX *cm)
900 {
901        int     i, j;
902        RMATRIX *dnew;
903
904        if (!cm)
905                return(NULL);
906        dnew = rmx_alloc(cm->nrows, cm->ncols, 3);
907        if (!dnew)
908                return(NULL);
909        dnew->dtype = DTfloat;
910        for (i = dnew->nrows; i--; )
911            for (j = dnew->ncols; j--; ) {
912                const COLORV    *cv = cm_lval(cm,i,j);
913                double          *dp = rmx_lval(dnew,i,j);
914                dp[0] = cv[0];
915                dp[1] = cv[1];
916                dp[2] = cv[2];
917            }
918        return(dnew);
919 }
920
921 /* Convert general matrix to newly allocated CMATRIX buffer */
922 CMATRIX *
923 cm_from_rmatrix(const RMATRIX *rm)
924 {
925        int     i, j;
926        CMATRIX *cnew;
927
928        if (!rm || !rm->mtx | (rm->ncomp == 2))
929                return(NULL);
930        cnew = cm_alloc(rm->nrows, rm->ncols);
931        if (!cnew)
932                return(NULL);
933        for (i = cnew->nrows; i--; )
934            for (j = cnew->ncols; j--; ) {
935                const double    *dp = rmx_val(rm,i,j);
936                COLORV          *cv = cm_lval(cnew,i,j);
937                switch (rm->ncomp) {
938                case 3:
939                    setcolor(cv, dp[0], dp[1], dp[2]);
940                    break;
941                case 1:
942                    setcolor(cv, dp[0], dp[0], dp[0]);
943                    break;
944                default: {
945                        SCOLOR  scol;
946                        int     k;
947                        for (k = rm->ncomp; k--; )
948                                scol[k] = dp[k];
949                        scolor2color(cv, scol, rm->ncomp, rm->wlpart);
950                    } break;
951                }
952            }
953        return(cnew);
954 }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)