1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rmatrix.c,v 2.80 2024/06/04 21:23:11 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* General matrix operations. |
6 |
*/ |
7 |
|
8 |
#include <stdlib.h> |
9 |
#include <errno.h> |
10 |
#include "rtio.h" |
11 |
#include "platform.h" |
12 |
#include "resolu.h" |
13 |
#include "paths.h" |
14 |
#include "rmatrix.h" |
15 |
#if !defined(_WIN32) && !defined(_WIN64) |
16 |
#include <sys/mman.h> |
17 |
#endif |
18 |
|
19 |
static const char rmx_mismatch_warn[] = "WARNING: data type mismatch\n"; |
20 |
|
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; |
26 |
|
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 |
|
44 |
/* Prepare a RMATRIX for writing (allocate array if needed) */ |
45 |
int |
46 |
rmx_prepare(RMATRIX *rm) |
47 |
{ |
48 |
if (!rm) return(0); |
49 |
if (rm->mtx) |
50 |
return(1); |
51 |
if ((rm->nrows <= 0) | (rm->ncols <= 0) | (rm->ncomp <= 0)) |
52 |
return(0); |
53 |
rm->mtx = (double *)malloc(rmx_array_size(rm)); |
54 |
rm->pflags |= RMF_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) |
61 |
{ |
62 |
RMATRIX *dnew = rmx_new(nr, nc, n); |
63 |
|
64 |
if (dnew && !rmx_prepare(dnew)) { |
65 |
rmx_free(dnew); |
66 |
dnew = NULL; |
67 |
} |
68 |
return(dnew); |
69 |
} |
70 |
|
71 |
/* Clear state by freeing info and matrix data */ |
72 |
void |
73 |
rmx_reset(RMATRIX *rm) |
74 |
{ |
75 |
if (!rm) return; |
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, 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 |
|
102 |
/* Resolve data type based on two input types (returns 0 for mismatch) */ |
103 |
int |
104 |
rmx_newtype(int dtyp1, int dtyp2) |
105 |
{ |
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) |
111 |
return(dtyp1); |
112 |
return(dtyp2); |
113 |
} |
114 |
|
115 |
/* Append header information associated with matrix data */ |
116 |
int |
117 |
rmx_addinfo(RMATRIX *rm, const char *info) |
118 |
{ |
119 |
int oldlen = 0; |
120 |
|
121 |
if (!rm || !info || !*info) |
122 |
return(0); |
123 |
if (!rm->info) { |
124 |
rm->info = (char *)malloc(strlen(info)+1); |
125 |
if (rm->info) rm->info[0] = '\0'; |
126 |
} else { |
127 |
oldlen = strlen(rm->info); |
128 |
rm->info = (char *)realloc(rm->info, |
129 |
oldlen+strlen(info)+1); |
130 |
} |
131 |
if (!rm->info) |
132 |
return(0); |
133 |
strcpy(rm->info+oldlen, info); |
134 |
return(1); |
135 |
} |
136 |
|
137 |
static int |
138 |
get_dminfo(char *s, void *p) |
139 |
{ |
140 |
RMATRIX *ip = (RMATRIX *)p; |
141 |
char fmt[MAXFMTLEN]; |
142 |
int i; |
143 |
|
144 |
if (headidval(NULL, s)) |
145 |
return(0); |
146 |
if (isncomp(s)) { |
147 |
ip->ncomp = ncompval(s); |
148 |
return(0); |
149 |
} |
150 |
if (!strncmp(s, "NROWS=", 6)) { |
151 |
ip->nrows = atoi(s+6); |
152 |
return(0); |
153 |
} |
154 |
if (!strncmp(s, "NCOLS=", 6)) { |
155 |
ip->ncols = atoi(s+6); |
156 |
return(0); |
157 |
} |
158 |
if ((i = isbigendian(s)) >= 0) { |
159 |
if (nativebigendian() != i) |
160 |
ip->pflags |= RMF_SWAPIN; |
161 |
else |
162 |
ip->pflags &= ~RMF_SWAPIN; |
163 |
return(0); |
164 |
} |
165 |
if (isexpos(s)) { |
166 |
float f = exposval(s); |
167 |
scalecolor(ip->cexp, f); |
168 |
return(0); |
169 |
} |
170 |
if (iscolcor(s)) { |
171 |
COLOR ctmp; |
172 |
colcorval(ctmp, s); |
173 |
multcolor(ip->cexp, ctmp); |
174 |
return(0); |
175 |
} |
176 |
if (iswlsplit(s)) { |
177 |
wlsplitval(ip->wlpart, s); |
178 |
return(0); |
179 |
} |
180 |
if (!formatval(fmt, s)) { |
181 |
rmx_addinfo(ip, s); |
182 |
return(0); |
183 |
} /* else check format */ |
184 |
for (i = 1; i < DTend; i++) |
185 |
if (!strcmp(fmt, cm_fmt_id[i])) { |
186 |
ip->dtype = i; |
187 |
return(0); |
188 |
} |
189 |
return(-1); |
190 |
} |
191 |
|
192 |
static int |
193 |
rmx_load_ascii(double *drp, const RMATRIX *rm, FILE *fp) |
194 |
{ |
195 |
int j, k; |
196 |
|
197 |
for (j = 0; j < rm->ncols; j++) |
198 |
for (k = rm->ncomp; k-- > 0; ) |
199 |
if (fscanf(fp, "%lf", drp++) != 1) |
200 |
return(0); |
201 |
return(1); |
202 |
} |
203 |
|
204 |
static int |
205 |
rmx_load_float(double *drp, const RMATRIX *rm, FILE *fp) |
206 |
{ |
207 |
int j, k; |
208 |
float val[100]; |
209 |
|
210 |
if (rm->ncomp > 100) { |
211 |
fputs("Unsupported # components in rmx_load_float()\n", stderr); |
212 |
exit(1); |
213 |
} |
214 |
for (j = 0; j < rm->ncols; j++) { |
215 |
if (getbinary(val, sizeof(val[0]), rm->ncomp, fp) != rm->ncomp) |
216 |
return(0); |
217 |
if (rm->pflags & RMF_SWAPIN) |
218 |
swap32((char *)val, rm->ncomp); |
219 |
for (k = 0; k < rm->ncomp; k++) |
220 |
*drp++ = val[k]; |
221 |
} |
222 |
return(1); |
223 |
} |
224 |
|
225 |
static int |
226 |
rmx_load_double(double *drp, const RMATRIX *rm, FILE *fp) |
227 |
{ |
228 |
if (DTrmx_native != DTdouble) |
229 |
return(0); |
230 |
if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols) |
231 |
return(0); |
232 |
if (rm->pflags & RMF_SWAPIN) |
233 |
swap64((char *)drp, rm->ncols*rm->ncomp); |
234 |
return(1); |
235 |
} |
236 |
|
237 |
static int |
238 |
rmx_load_rgbe(double *drp, const RMATRIX *rm, FILE *fp) |
239 |
{ |
240 |
COLR *scan; |
241 |
COLOR col; |
242 |
int j; |
243 |
|
244 |
if (rm->ncomp != 3) |
245 |
return(0); |
246 |
scan = (COLR *)tempbuffer(sizeof(COLR)*rm->ncols); |
247 |
if (!scan) |
248 |
return(0); |
249 |
if (freadcolrs(scan, rm->ncols, fp) < 0) |
250 |
return(0); |
251 |
for (j = 0; j < rm->ncols; j++) { |
252 |
colr_color(col, scan[j]); |
253 |
*drp++ = colval(col,RED); |
254 |
*drp++ = colval(col,GRN); |
255 |
*drp++ = colval(col,BLU); |
256 |
} |
257 |
return(1); |
258 |
} |
259 |
|
260 |
static int |
261 |
rmx_load_spec(double *drp, const RMATRIX *rm, FILE *fp) |
262 |
{ |
263 |
uby8 *scan; |
264 |
SCOLOR scol; |
265 |
int j, k; |
266 |
|
267 |
if ((rm->ncomp < 3) | (rm->ncomp > MAXCSAMP)) |
268 |
return(0); |
269 |
scan = (uby8 *)tempbuffer((rm->ncomp+1)*rm->ncols); |
270 |
if (!scan) |
271 |
return(0); |
272 |
if (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) |
273 |
return(0); |
274 |
for (j = 0; j < rm->ncols; j++) { |
275 |
scolr2scolor(scol, scan+j*(rm->ncomp+1), rm->ncomp); |
276 |
for (k = 0; k < rm->ncomp; k++) |
277 |
*drp++ = scol[k]; |
278 |
} |
279 |
return(1); |
280 |
} |
281 |
|
282 |
/* Read matrix header from input stream (cannot be XML) */ |
283 |
int |
284 |
rmx_load_header(RMATRIX *rm, FILE *fp) |
285 |
{ |
286 |
if (!rm | !fp) |
287 |
return(0); |
288 |
rmx_reset(rm); /* clear state */ |
289 |
if (rm->nrows | rm->ncols | !rm->dtype) { |
290 |
rm->nrows = rm->ncols = 0; |
291 |
rm->ncomp = 3; |
292 |
setcolor(rm->cexp, 1.f, 1.f, 1.f); |
293 |
memcpy(rm->wlpart, WLPART, sizeof(rm->wlpart)); |
294 |
rm->pflags = 0; |
295 |
} |
296 |
rm->dtype = DTascii; /* assumed w/o FORMAT */ |
297 |
if (getheader(fp, get_dminfo, rm) < 0) { |
298 |
fputs("Unrecognized matrix format\n", stderr); |
299 |
return(0); |
300 |
} |
301 |
if ((rm->dtype == DTrgbe) | (rm->dtype == DTxyze) && |
302 |
rm->ncomp != 3) |
303 |
return(0); |
304 |
if (rm->ncols <= 0 && /* resolution string? */ |
305 |
!fscnresolu(&rm->ncols, &rm->nrows, fp)) |
306 |
return(0); |
307 |
if (rm->dtype == DTascii) /* set file type (WINDOWS) */ |
308 |
SET_FILE_TEXT(fp); |
309 |
else |
310 |
SET_FILE_BINARY(fp); |
311 |
return(1); |
312 |
} |
313 |
|
314 |
/* Load next row as double (cannot be XML) */ |
315 |
int |
316 |
rmx_load_row(double *drp, const RMATRIX *rm, FILE *fp) |
317 |
{ |
318 |
switch (rm->dtype) { |
319 |
case DTascii: |
320 |
return(rmx_load_ascii(drp, rm, fp)); |
321 |
case DTfloat: |
322 |
return(rmx_load_float(drp, rm, fp)); |
323 |
case DTdouble: |
324 |
return(rmx_load_double(drp, rm, fp)); |
325 |
case DTrgbe: |
326 |
case DTxyze: |
327 |
return(rmx_load_rgbe(drp, rm, fp)); |
328 |
case DTspec: |
329 |
return(rmx_load_spec(drp, rm, fp)); |
330 |
default: |
331 |
fputs("Unsupported data type in rmx_load_row()\n", stderr); |
332 |
} |
333 |
return(0); |
334 |
} |
335 |
|
336 |
/* Allocate & load post-header data from stream given type set in rm->dtype */ |
337 |
int |
338 |
rmx_load_data(RMATRIX *rm, FILE *fp) |
339 |
{ |
340 |
int i; |
341 |
#ifdef MAP_FILE |
342 |
long pos; /* map memory for file > 1MB if possible */ |
343 |
if ((rm->dtype == DTrmx_native) & !(rm->pflags & RMF_SWAPIN) & |
344 |
(rmx_array_size(rm) >= 1L<<20) && |
345 |
(pos = ftell(fp)) >= 0 && !(pos % sizeof(double))) { |
346 |
rm->mapped = mmap(NULL, rmx_array_size(rm)+pos, PROT_READ|PROT_WRITE, |
347 |
MAP_PRIVATE, fileno(fp), 0); |
348 |
if (rm->mapped != MAP_FAILED) { |
349 |
if (rm->pflags & RMF_FREEMEM) |
350 |
free(rm->mtx); |
351 |
rm->mtx = (double *)rm->mapped + pos/sizeof(double); |
352 |
rm->pflags &= ~RMF_FREEMEM; |
353 |
return(1); |
354 |
} /* else fall back on reading into memory */ |
355 |
rm->mapped = NULL; |
356 |
} |
357 |
#endif |
358 |
if (!rmx_prepare(rm)) { /* need in-core matrix array */ |
359 |
fprintf(stderr, "Cannot allocate %g MByte matrix array\n", |
360 |
(1./(1L<<20))*(double)rmx_array_size(rm)); |
361 |
return(0); |
362 |
} |
363 |
for (i = 0; i < rm->nrows; i++) |
364 |
if (!rmx_load_row(rmx_lval(rm,i,0), rm, fp)) |
365 |
return(0); |
366 |
return(1); |
367 |
} |
368 |
|
369 |
/* Load matrix from supported file type */ |
370 |
RMATRIX * |
371 |
rmx_load(const char *inspec, RMPref rmp) |
372 |
{ |
373 |
FILE *fp; |
374 |
RMATRIX *dnew; |
375 |
int ok; |
376 |
|
377 |
if (!inspec) |
378 |
inspec = stdin_name; |
379 |
else if (!*inspec) |
380 |
return(NULL); |
381 |
if (inspec == stdin_name) /* reading from stdin? */ |
382 |
fp = stdin; |
383 |
else if (inspec[0] == '!') |
384 |
fp = popen(inspec+1, "r"); |
385 |
else { |
386 |
const char *sp = inspec; /* check suffix */ |
387 |
while (*sp) |
388 |
++sp; |
389 |
while (sp > inspec && sp[-1] != '.') |
390 |
--sp; |
391 |
if (!strcasecmp(sp, "XML")) { /* assume it's a BSDF */ |
392 |
CMATRIX *cm = rmp==RMPnone ? (CMATRIX *)NULL : |
393 |
rmp==RMPtrans ? cm_loadBTDF(inspec) : |
394 |
cm_loadBRDF(inspec, rmp==RMPreflB) ; |
395 |
if (!cm) |
396 |
return(NULL); |
397 |
dnew = rmx_from_cmatrix(cm); |
398 |
cm_free(cm); |
399 |
dnew->dtype = DTascii; |
400 |
return(dnew); /* return here */ |
401 |
} /* else open it ourselves */ |
402 |
fp = fopen(inspec, "r"); |
403 |
} |
404 |
if (!fp) { |
405 |
fprintf(stderr, "Cannot open for reading: %s\n", inspec); |
406 |
return(NULL); |
407 |
} |
408 |
#ifdef getc_unlocked |
409 |
flockfile(fp); |
410 |
#endif |
411 |
SET_FILE_BINARY(fp); /* load header info */ |
412 |
if (!rmx_load_header(dnew = rmx_new(0,0,3), fp)) { |
413 |
fprintf(stderr, "Bad header in: %s\n", inspec); |
414 |
if (inspec[0] == '!') pclose(fp); |
415 |
else fclose(fp); |
416 |
rmx_free(dnew); |
417 |
return(NULL); |
418 |
} |
419 |
ok = rmx_load_data(dnew, fp); /* allocate & load data */ |
420 |
|
421 |
if (fp != stdin) { /* close input stream */ |
422 |
if (inspec[0] == '!') |
423 |
pclose(fp); |
424 |
else |
425 |
fclose(fp); |
426 |
} |
427 |
#ifdef getc_unlocked |
428 |
else |
429 |
funlockfile(fp); |
430 |
#endif |
431 |
if (!ok) { /* load failure? */ |
432 |
fprintf(stderr, "Error loading data from: %s\n", inspec); |
433 |
rmx_free(dnew); |
434 |
return(NULL); |
435 |
} |
436 |
/* undo exposure? */ |
437 |
if ((dnew->cexp[0] != 1.f) | |
438 |
(dnew->cexp[1] != 1.f) | (dnew->cexp[2] != 1.f)) { |
439 |
double cmlt[MAXCSAMP]; |
440 |
int i; |
441 |
if (dnew->ncomp > MAXCSAMP) { |
442 |
fprintf(stderr, "Excess spectral components in: %s\n", |
443 |
inspec); |
444 |
rmx_free(dnew); |
445 |
return(NULL); |
446 |
} |
447 |
cmlt[0] = 1./dnew->cexp[0]; |
448 |
cmlt[1] = 1./dnew->cexp[1]; |
449 |
cmlt[2] = 1./dnew->cexp[2]; |
450 |
for (i = dnew->ncomp; i-- > 3; ) |
451 |
cmlt[i] = cmlt[1]; /* XXX hack! */ |
452 |
rmx_scale(dnew, cmlt); |
453 |
setcolor(dnew->cexp, 1.f, 1.f, 1.f); |
454 |
} |
455 |
return(dnew); |
456 |
} |
457 |
|
458 |
static int |
459 |
rmx_write_ascii(const double *dp, int nc, int len, FILE *fp) |
460 |
{ |
461 |
while (len-- > 0) { |
462 |
int k = nc; |
463 |
while (k-- > 0) |
464 |
fprintf(fp, " %.7e", *dp++); |
465 |
fputc('\t', fp); |
466 |
} |
467 |
return(fputc('\n', fp) != EOF); |
468 |
} |
469 |
|
470 |
static int |
471 |
rmx_write_float(const double *dp, int len, FILE *fp) |
472 |
{ |
473 |
float val; |
474 |
|
475 |
while (len--) { |
476 |
val = *dp++; |
477 |
if (putbinary(&val, sizeof(float), 1, fp) != 1) |
478 |
return(0); |
479 |
} |
480 |
return(1); |
481 |
} |
482 |
|
483 |
static int |
484 |
rmx_write_rgbe(const double *dp, int nc, int len, FILE *fp) |
485 |
{ |
486 |
COLR *scan; |
487 |
int j; |
488 |
|
489 |
if ((nc != 1) & (nc != 3)) return(0); |
490 |
scan = (COLR *)tempbuffer(sizeof(COLR)*len); |
491 |
if (!scan) return(0); |
492 |
|
493 |
for (j = 0; j < len; j++, dp += nc) |
494 |
if (nc == 1) |
495 |
setcolr(scan[j], dp[0], dp[0], dp[0]); |
496 |
else |
497 |
setcolr(scan[j], dp[0], dp[1], dp[2]); |
498 |
|
499 |
return(fwritecolrs(scan, len, fp) >= 0); |
500 |
} |
501 |
|
502 |
static int |
503 |
rmx_write_spec(const double *dp, int nc, int len, FILE *fp) |
504 |
{ |
505 |
uby8 *scan; |
506 |
SCOLOR scol; |
507 |
int j, k; |
508 |
|
509 |
if (nc < 3) return(0); |
510 |
scan = (uby8 *)tempbuffer((nc+1)*len); |
511 |
if (!scan) return(0); |
512 |
for (j = 0; j < len; j++, dp += nc) { |
513 |
for (k = nc; k--; ) |
514 |
scol[k] = dp[k]; |
515 |
scolor2scolr(scan+j*(nc+1), scol, nc); |
516 |
} |
517 |
return(fwritescolrs(scan, nc, len, fp) >= 0); |
518 |
} |
519 |
|
520 |
/* Check if CIE XYZ primaries were specified */ |
521 |
static int |
522 |
findCIEprims(const char *info) |
523 |
{ |
524 |
RGBPRIMS prims; |
525 |
|
526 |
if (!info) |
527 |
return(0); |
528 |
info = strstr(info, PRIMARYSTR); |
529 |
if (!info || !primsval(prims, info)) |
530 |
return(0); |
531 |
|
532 |
return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) && |
533 |
(prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) && |
534 |
(prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01)); |
535 |
} |
536 |
|
537 |
/* Finish writing header data with resolution and format, returning type used */ |
538 |
int |
539 |
rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp) |
540 |
{ |
541 |
if (!rm | !fp || rm->ncols <= 0) |
542 |
return(0); |
543 |
if (rm->info) |
544 |
fputs(rm->info, fp); |
545 |
if (dtype == DTfromHeader) |
546 |
dtype = rm->dtype; |
547 |
else if (dtype == DTrgbe && (rm->dtype == DTxyze || |
548 |
findCIEprims(rm->info))) |
549 |
dtype = DTxyze; |
550 |
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
551 |
dtype = DTrgbe; |
552 |
if ((dtype < DTspec) & (rm->ncomp > 3)) |
553 |
dtype = DTspec; |
554 |
else if ((dtype == DTspec) & (rm->ncomp <= 3)) |
555 |
return(0); |
556 |
|
557 |
if (dtype == DTascii) /* set file type (WINDOWS) */ |
558 |
SET_FILE_TEXT(fp); |
559 |
else |
560 |
SET_FILE_BINARY(fp); |
561 |
/* write exposure? */ |
562 |
if (rm->ncomp == 3 && (rm->cexp[RED] != rm->cexp[GRN]) | |
563 |
(rm->cexp[GRN] != rm->cexp[BLU])) |
564 |
fputcolcor(rm->cexp, fp); |
565 |
else if (rm->cexp[GRN] != 1.f) |
566 |
fputexpos(rm->cexp[GRN], fp); |
567 |
/* matrix size? */ |
568 |
if ((dtype > DTspec) | (rm->nrows <= 0)) { |
569 |
if (rm->nrows > 0) |
570 |
fprintf(fp, "NROWS=%d\n", rm->nrows); |
571 |
fprintf(fp, "NCOLS=%d\n", rm->ncols); |
572 |
} |
573 |
if (dtype >= DTspec) { /* # components & split? */ |
574 |
fputncomp(rm->ncomp, fp); |
575 |
if (rm->ncomp > 3 && |
576 |
memcmp(rm->wlpart, WLPART, sizeof(WLPART))) |
577 |
fputwlsplit(rm->wlpart, fp); |
578 |
} else if ((rm->ncomp != 3) & (rm->ncomp != 1)) |
579 |
return(0); /* wrong # components */ |
580 |
if ((dtype == DTfloat) | (dtype == DTdouble)) |
581 |
fputendian(fp); /* important to record */ |
582 |
fputformat(cm_fmt_id[dtype], fp); |
583 |
fputc('\n', fp); /* end of header */ |
584 |
if ((dtype <= DTspec) & (rm->nrows > 0)) |
585 |
fprtresolu(rm->ncols, rm->nrows, fp); |
586 |
return(dtype); |
587 |
} |
588 |
|
589 |
/* Write out matrix data (usually by row) */ |
590 |
int |
591 |
rmx_write_data(const double *dp, int nc, int len, int dtype, FILE *fp) |
592 |
{ |
593 |
switch (dtype) { |
594 |
case DTascii: |
595 |
return(rmx_write_ascii(dp, nc, len, fp)); |
596 |
case DTfloat: |
597 |
return(rmx_write_float(dp, nc*len, fp)); |
598 |
case DTrmx_native: |
599 |
return(putbinary(dp, sizeof(*dp)*nc, len, fp) == len); |
600 |
case DTrgbe: |
601 |
case DTxyze: |
602 |
return(rmx_write_rgbe(dp, nc, len, fp)); |
603 |
case DTspec: |
604 |
return(rmx_write_spec(dp, nc, len, fp)); |
605 |
} |
606 |
return(0); |
607 |
} |
608 |
|
609 |
/* Write matrix using file format indicated by dtype */ |
610 |
int |
611 |
rmx_write(const RMATRIX *rm, int dtype, FILE *fp) |
612 |
{ |
613 |
int ok = 0; |
614 |
int i; |
615 |
/* complete header */ |
616 |
dtype = rmx_write_header(rm, dtype, fp); |
617 |
if (dtype <= 0) |
618 |
return(0); |
619 |
#ifdef getc_unlocked |
620 |
flockfile(fp); |
621 |
#endif |
622 |
if (dtype == DTrmx_native) /* write all at once? */ |
623 |
ok = rmx_write_data(rm->mtx, rm->ncomp, |
624 |
rm->nrows*rm->ncols, dtype, fp); |
625 |
else /* else row by row */ |
626 |
for (i = 0; i < rm->nrows; i++) { |
627 |
ok = rmx_write_data(rmx_val(rm,i,0), rm->ncomp, |
628 |
rm->ncols, dtype, fp); |
629 |
if (!ok) break; |
630 |
} |
631 |
|
632 |
if (ok) ok = (fflush(fp) == 0); |
633 |
#ifdef getc_unlocked |
634 |
funlockfile(fp); |
635 |
#endif |
636 |
if (!ok) fputs("Error writing matrix\n", stderr); |
637 |
return(ok); |
638 |
} |
639 |
|
640 |
/* Allocate and assign square identity matrix with n components */ |
641 |
RMATRIX * |
642 |
rmx_identity(const int dim, const int n) |
643 |
{ |
644 |
RMATRIX *rid = rmx_alloc(dim, dim, n); |
645 |
int i, k; |
646 |
|
647 |
if (!rid) |
648 |
return(NULL); |
649 |
memset(rid->mtx, 0, rmx_array_size(rid)); |
650 |
for (i = dim; i--; ) { |
651 |
double *dp = rmx_lval(rid,i,i); |
652 |
for (k = n; k--; ) |
653 |
dp[k] = 1.; |
654 |
} |
655 |
return(rid); |
656 |
} |
657 |
|
658 |
/* Duplicate the given matrix (may be unallocated) */ |
659 |
RMATRIX * |
660 |
rmx_copy(const RMATRIX *rm) |
661 |
{ |
662 |
RMATRIX *dnew; |
663 |
|
664 |
if (!rm) |
665 |
return(NULL); |
666 |
dnew = rmx_new(rm->nrows, rm->ncols, rm->ncomp); |
667 |
if (!dnew) |
668 |
return(NULL); |
669 |
if (rm->mtx) { |
670 |
if (!rmx_prepare(dnew)) { |
671 |
rmx_free(dnew); |
672 |
return(NULL); |
673 |
} |
674 |
memcpy(dnew->mtx, rm->mtx, rmx_array_size(dnew)); |
675 |
} |
676 |
rmx_addinfo(dnew, rm->info); |
677 |
dnew->dtype = rm->dtype; |
678 |
copycolor(dnew->cexp, rm->cexp); |
679 |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
680 |
return(dnew); |
681 |
} |
682 |
|
683 |
/* Replace data in first matrix with data from second */ |
684 |
int |
685 |
rmx_transfer_data(RMATRIX *rdst, RMATRIX *rsrc, int dometa) |
686 |
{ |
687 |
if (!rdst | !rsrc || (rdst->nrows != rsrc->nrows) | |
688 |
(rdst->ncols != rsrc->ncols) | |
689 |
(rdst->ncomp != rsrc->ncomp)) |
690 |
return(0); |
691 |
|
692 |
if (dometa) { /* transfer everything? */ |
693 |
rmx_reset(rdst); |
694 |
*rdst = *rsrc; |
695 |
rsrc->info = NULL; rsrc->mapped = NULL; rsrc->mtx = NULL; |
696 |
return(1); |
697 |
} |
698 |
#ifdef MAP_FILE /* just matrix data -- leave metadata */ |
699 |
if (rdst->mapped) |
700 |
munmap(rdst->mapped, rmx_mapped_size(rdst)); |
701 |
else |
702 |
#endif |
703 |
if (rdst->pflags & RMF_FREEMEM) |
704 |
free(rdst->mtx); |
705 |
rdst->mapped = rsrc->mapped; |
706 |
rdst->mtx = rsrc->mtx; |
707 |
if (rsrc->pflags & RMF_FREEMEM) |
708 |
rdst->pflags |= RMF_FREEMEM; |
709 |
else |
710 |
rdst->pflags &= ~RMF_FREEMEM; |
711 |
rsrc->mapped = NULL; rsrc->mtx = NULL; |
712 |
return(1); |
713 |
} |
714 |
|
715 |
/* Allocate and assign transposed matrix */ |
716 |
RMATRIX * |
717 |
rmx_transpose(const RMATRIX *rm) |
718 |
{ |
719 |
RMATRIX *dnew; |
720 |
int i, j; |
721 |
|
722 |
if (!rm || !rm->mtx) |
723 |
return(0); |
724 |
if ((rm->nrows == 1) | (rm->ncols == 1)) { |
725 |
dnew = rmx_copy(rm); |
726 |
if (!dnew) |
727 |
return(NULL); |
728 |
dnew->nrows = rm->ncols; |
729 |
dnew->ncols = rm->nrows; |
730 |
return(dnew); |
731 |
} |
732 |
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
733 |
if (!dnew) |
734 |
return(NULL); |
735 |
if (rm->info) { |
736 |
rmx_addinfo(dnew, rm->info); |
737 |
rmx_addinfo(dnew, "Transposed rows and columns\n"); |
738 |
} |
739 |
dnew->dtype = rm->dtype; |
740 |
copycolor(dnew->cexp, rm->cexp); |
741 |
memcpy(dnew->wlpart, rm->wlpart, sizeof(dnew->wlpart)); |
742 |
for (j = dnew->ncols; j--; ) |
743 |
for (i = dnew->nrows; i--; ) |
744 |
memcpy(rmx_lval(dnew,i,j), rmx_val(rm,j,i), |
745 |
sizeof(double)*dnew->ncomp); |
746 |
return(dnew); |
747 |
} |
748 |
|
749 |
/* Multiply (concatenate) two matrices and allocate the result */ |
750 |
RMATRIX * |
751 |
rmx_multiply(const RMATRIX *m1, const RMATRIX *m2) |
752 |
{ |
753 |
RMATRIX *mres; |
754 |
int i, j, k, h; |
755 |
|
756 |
if (!m1 | !m2 || !m1->mtx | !m2->mtx | |
757 |
(m1->ncomp != m2->ncomp) | (m1->ncols != m2->nrows)) |
758 |
return(NULL); |
759 |
mres = rmx_alloc(m1->nrows, m2->ncols, m1->ncomp); |
760 |
if (!mres) |
761 |
return(NULL); |
762 |
i = rmx_newtype(m1->dtype, m2->dtype); |
763 |
if (i) |
764 |
mres->dtype = i; |
765 |
else |
766 |
rmx_addinfo(mres, rmx_mismatch_warn); |
767 |
for (i = mres->nrows; i--; ) |
768 |
for (j = mres->ncols; j--; ) |
769 |
for (k = mres->ncomp; k--; ) { |
770 |
double d = 0; |
771 |
for (h = m1->ncols; h--; ) |
772 |
d += rmx_val(m1,i,h)[k] * rmx_val(m2,h,j)[k]; |
773 |
rmx_lval(mres,i,j)[k] = d; |
774 |
} |
775 |
return(mres); |
776 |
} |
777 |
|
778 |
/* Element-wise multiplication (or division) of m2 into m1 */ |
779 |
int |
780 |
rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide) |
781 |
{ |
782 |
int zeroDivides = 0; |
783 |
int i, j, k; |
784 |
|
785 |
if (!m1 | !m2 || !m1->mtx | !m2->mtx | |
786 |
(m1->ncols != m2->ncols) | (m1->nrows != m2->nrows)) |
787 |
return(0); |
788 |
if ((m2->ncomp > 1) & (m2->ncomp != m1->ncomp)) |
789 |
return(0); |
790 |
i = rmx_newtype(m1->dtype, m2->dtype); |
791 |
if (i) |
792 |
m1->dtype = i; |
793 |
else |
794 |
rmx_addinfo(m1, rmx_mismatch_warn); |
795 |
for (i = m1->nrows; i--; ) |
796 |
for (j = m1->ncols; j--; ) |
797 |
if (divide) { |
798 |
double d; |
799 |
if (m2->ncomp == 1) { |
800 |
d = rmx_val(m2,i,j)[0]; |
801 |
if (d == 0) { |
802 |
++zeroDivides; |
803 |
for (k = m1->ncomp; k--; ) |
804 |
rmx_lval(m1,i,j)[k] = 0; |
805 |
} else { |
806 |
d = 1./d; |
807 |
for (k = m1->ncomp; k--; ) |
808 |
rmx_lval(m1,i,j)[k] *= d; |
809 |
} |
810 |
} else |
811 |
for (k = m1->ncomp; k--; ) { |
812 |
d = rmx_val(m2,i,j)[k]; |
813 |
if (d == 0) { |
814 |
++zeroDivides; |
815 |
rmx_lval(m1,i,j)[k] = 0; |
816 |
} else |
817 |
rmx_lval(m1,i,j)[k] /= d; |
818 |
} |
819 |
} else { |
820 |
if (m2->ncomp == 1) { |
821 |
const double d = rmx_val(m2,i,j)[0]; |
822 |
for (k = m1->ncomp; k--; ) |
823 |
rmx_lval(m1,i,j)[k] *= d; |
824 |
} else |
825 |
for (k = m1->ncomp; k--; ) |
826 |
rmx_lval(m1,i,j)[k] *= rmx_val(m2,i,j)[k]; |
827 |
} |
828 |
if (zeroDivides) { |
829 |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
830 |
errno = ERANGE; |
831 |
} |
832 |
return(1); |
833 |
} |
834 |
|
835 |
/* Sum second matrix into first, applying scale factor beforehand */ |
836 |
int |
837 |
rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[]) |
838 |
{ |
839 |
double *mysf = NULL; |
840 |
int i, j, k; |
841 |
|
842 |
if (!msum | !madd || !msum->mtx | !madd->mtx | |
843 |
(msum->nrows != madd->nrows) | |
844 |
(msum->ncols != madd->ncols) | |
845 |
(msum->ncomp != madd->ncomp)) |
846 |
return(0); |
847 |
if (!sf) { |
848 |
mysf = (double *)malloc(sizeof(double)*msum->ncomp); |
849 |
if (!mysf) |
850 |
return(0); |
851 |
for (k = msum->ncomp; k--; ) |
852 |
mysf[k] = 1; |
853 |
sf = mysf; |
854 |
} |
855 |
i = rmx_newtype(msum->dtype, madd->dtype); |
856 |
if (i) |
857 |
msum->dtype = i; |
858 |
else |
859 |
rmx_addinfo(msum, rmx_mismatch_warn); |
860 |
for (i = msum->nrows; i--; ) |
861 |
for (j = msum->ncols; j--; ) { |
862 |
const double *da = rmx_val(madd,i,j); |
863 |
double *ds = rmx_lval(msum,i,j); |
864 |
for (k = msum->ncomp; k--; ) |
865 |
ds[k] += sf[k] * da[k]; |
866 |
} |
867 |
if (mysf) |
868 |
free(mysf); |
869 |
return(1); |
870 |
} |
871 |
|
872 |
/* Scale the given matrix by the indicated scalar component vector */ |
873 |
int |
874 |
rmx_scale(RMATRIX *rm, const double sf[]) |
875 |
{ |
876 |
int i, j, k; |
877 |
|
878 |
if (!rm | !sf || !rm->mtx) |
879 |
return(0); |
880 |
for (i = rm->nrows; i--; ) |
881 |
for (j = rm->ncols; j--; ) { |
882 |
double *dp = rmx_lval(rm,i,j); |
883 |
for (k = rm->ncomp; k--; ) |
884 |
dp[k] *= sf[k]; |
885 |
} |
886 |
if (rm->info) |
887 |
rmx_addinfo(rm, "Applied scalar\n"); |
888 |
/* XXX: should record as exposure for COLR and SCOLR types? */ |
889 |
return(1); |
890 |
} |
891 |
|
892 |
/* Allocate new matrix and apply component transformation */ |
893 |
RMATRIX * |
894 |
rmx_transform(const RMATRIX *msrc, int n, const double cmat[]) |
895 |
{ |
896 |
int i, j, ks, kd; |
897 |
RMATRIX *dnew; |
898 |
|
899 |
if (!msrc | (n <= 0) | !cmat || !msrc->mtx) |
900 |
return(NULL); |
901 |
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
902 |
if (!dnew) |
903 |
return(NULL); |
904 |
if (msrc->info) { |
905 |
char buf[128]; |
906 |
sprintf(buf, "Applied %dx%d component transform\n", |
907 |
dnew->ncomp, msrc->ncomp); |
908 |
rmx_addinfo(dnew, msrc->info); |
909 |
rmx_addinfo(dnew, buf); |
910 |
} |
911 |
dnew->dtype = msrc->dtype; |
912 |
for (i = dnew->nrows; i--; ) |
913 |
for (j = dnew->ncols; j--; ) { |
914 |
const double *ds = rmx_val(msrc,i,j); |
915 |
for (kd = dnew->ncomp; kd--; ) { |
916 |
double d = 0; |
917 |
for (ks = msrc->ncomp; ks--; ) |
918 |
d += cmat[kd*msrc->ncomp + ks] * ds[ks]; |
919 |
rmx_lval(dnew,i,j)[kd] = d; |
920 |
} |
921 |
} |
922 |
return(dnew); |
923 |
} |
924 |
|
925 |
/* Convert a color matrix to newly allocated RMATRIX buffer */ |
926 |
RMATRIX * |
927 |
rmx_from_cmatrix(const CMATRIX *cm) |
928 |
{ |
929 |
int i, j; |
930 |
RMATRIX *dnew; |
931 |
|
932 |
if (!cm) |
933 |
return(NULL); |
934 |
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
935 |
if (!dnew) |
936 |
return(NULL); |
937 |
dnew->dtype = DTfloat; |
938 |
for (i = dnew->nrows; i--; ) |
939 |
for (j = dnew->ncols; j--; ) { |
940 |
const COLORV *cv = cm_lval(cm,i,j); |
941 |
double *dp = rmx_lval(dnew,i,j); |
942 |
dp[0] = cv[0]; |
943 |
dp[1] = cv[1]; |
944 |
dp[2] = cv[2]; |
945 |
} |
946 |
return(dnew); |
947 |
} |
948 |
|
949 |
/* Convert general matrix to newly allocated CMATRIX buffer */ |
950 |
CMATRIX * |
951 |
cm_from_rmatrix(const RMATRIX *rm) |
952 |
{ |
953 |
int i, j; |
954 |
CMATRIX *cnew; |
955 |
|
956 |
if (!rm || !rm->mtx | (rm->ncomp == 2)) |
957 |
return(NULL); |
958 |
cnew = cm_alloc(rm->nrows, rm->ncols); |
959 |
if (!cnew) |
960 |
return(NULL); |
961 |
for (i = cnew->nrows; i--; ) |
962 |
for (j = cnew->ncols; j--; ) { |
963 |
const double *dp = rmx_val(rm,i,j); |
964 |
COLORV *cv = cm_lval(cnew,i,j); |
965 |
switch (rm->ncomp) { |
966 |
case 3: |
967 |
setcolor(cv, dp[0], dp[1], dp[2]); |
968 |
break; |
969 |
case 1: |
970 |
setcolor(cv, dp[0], dp[0], dp[0]); |
971 |
break; |
972 |
default: { |
973 |
SCOLOR scol; |
974 |
int k; |
975 |
for (k = rm->ncomp; k--; ) |
976 |
scol[k] = dp[k]; |
977 |
scolor2color(cv, scol, rm->ncomp, rm->wlpart); |
978 |
} break; |
979 |
} |
980 |
} |
981 |
return(cnew); |
982 |
} |