1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rmatrix.c,v 2.82 2024/06/06 17:01:05 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) /* 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 = (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) |
61 |
{ |
62 |
RMATRIX *dnew = rmx_new(nr, nc, n); |
63 |
|
64 |
if (!rmx_prepare(dnew)) { |
65 |
rmx_free(dnew); |
66 |
return(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 |
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); |
125 |
} else { |
126 |
oldlen = strlen(rm->info); |
127 |
rm->info = (char *)realloc(rm->info, |
128 |
oldlen+strlen(info)+1); |
129 |
} |
130 |
if (!rm->info) |
131 |
return(0); |
132 |
strcpy(rm->info+oldlen, info); |
133 |
return(1); |
134 |
} |
135 |
|
136 |
static int |
137 |
get_dminfo(char *s, void *p) |
138 |
{ |
139 |
RMATRIX *ip = (RMATRIX *)p; |
140 |
char fmt[MAXFMTLEN]; |
141 |
int i; |
142 |
|
143 |
if (isheadid(s)) |
144 |
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(ip->nrows - 1); |
152 |
} |
153 |
if (!strncmp(s, "NCOLS=", 6)) { |
154 |
ip->ncols = atoi(s+6); |
155 |
return(ip->ncols - 1); |
156 |
} |
157 |
if ((i = isbigendian(s)) >= 0) { |
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(f > .0 ? 0 : -1); |
168 |
} |
169 |
if (iscolcor(s)) { |
170 |
COLOR ctmp; |
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); |
181 |
} /* else check format */ |
182 |
for (i = 1; i < DTend; i++) |
183 |
if (!strcmp(fmt, cm_fmt_id[i])) { |
184 |
ip->dtype = i; |
185 |
return(0); |
186 |
} |
187 |
return(-1); /* bad format */ |
188 |
} |
189 |
|
190 |
static int |
191 |
rmx_load_ascii(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) |
192 |
{ |
193 |
int j, k; |
194 |
|
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(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) |
204 |
{ |
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 |
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->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(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) |
225 |
{ |
226 |
if (getbinary(drp, sizeof(*drp)*rm->ncomp, rm->ncols, fp) != rm->ncols) |
227 |
return(0); |
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_spec(rmx_dtype *drp, const RMATRIX *rm, FILE *fp) |
258 |
{ |
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 (freadscolrs(scan, rm->ncomp, rm->ncols, fp) < 0) |
269 |
return(0); |
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 |
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 |
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? */ |
378 |
fp = stdin; |
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==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); /* return here */ |
397 |
} /* else open it ourselves */ |
398 |
fp = fopen(inspec, "r"); |
399 |
} |
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 |
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 |
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 |
421 |
fclose(fp); |
422 |
} |
423 |
#ifdef getc_unlocked |
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); |
452 |
} |
453 |
|
454 |
static int |
455 |
rmx_write_ascii(const rmx_dtype *dp, int nc, int len, FILE *fp) |
456 |
{ |
457 |
while (len-- > 0) { |
458 |
int k = nc; |
459 |
while (k-- > 0) |
460 |
fprintf(fp, " %.7e", *dp++); |
461 |
fputc('\t', fp); |
462 |
} |
463 |
return(fputc('\n', fp) != EOF); |
464 |
} |
465 |
|
466 |
static int |
467 |
rmx_write_float(const rmx_dtype *dp, int len, FILE *fp) |
468 |
{ |
469 |
float val; |
470 |
|
471 |
while (len--) { |
472 |
val = *dp++; |
473 |
if (putbinary(&val, sizeof(float), 1, fp) != 1) |
474 |
return(0); |
475 |
} |
476 |
return(1); |
477 |
} |
478 |
|
479 |
static int |
480 |
rmx_write_rgbe(const rmx_dtype *dp, int nc, int len, FILE *fp) |
481 |
{ |
482 |
COLR *scan; |
483 |
int j; |
484 |
|
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_spec(const rmx_dtype *dp, int nc, int len, FILE *fp) |
500 |
{ |
501 |
uby8 *scan; |
502 |
SCOLOR scol; |
503 |
int j, k; |
504 |
|
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 |
return(fwritescolrs(scan, nc, len, fp) >= 0); |
514 |
} |
515 |
|
516 |
/* Check if CIE XYZ primaries were specified */ |
517 |
static int |
518 |
findCIEprims(const char *info) |
519 |
{ |
520 |
RGBPRIMS prims; |
521 |
|
522 |
if (!info) |
523 |
return(0); |
524 |
info = strstr(info, PRIMARYSTR); |
525 |
if (!info || !primsval(prims, info)) |
526 |
return(0); |
527 |
|
528 |
return((prims[RED][CIEX] > .99) & (prims[RED][CIEY] < .01) && |
529 |
(prims[GRN][CIEX] < .01) & (prims[GRN][CIEY] > .99) && |
530 |
(prims[BLU][CIEX] < .01) & (prims[BLU][CIEY] < .01)); |
531 |
} |
532 |
|
533 |
/* Finish writing header data with resolution and format, returning type used */ |
534 |
int |
535 |
rmx_write_header(const RMATRIX *rm, int dtype, FILE *fp) |
536 |
{ |
537 |
if (!rm | !fp || rm->ncols <= 0) |
538 |
return(0); |
539 |
if (rm->info) |
540 |
fputs(rm->info, fp); |
541 |
if (dtype == DTfromHeader) |
542 |
dtype = rm->dtype; |
543 |
else if (dtype == DTrgbe && (rm->dtype == DTxyze || |
544 |
findCIEprims(rm->info))) |
545 |
dtype = DTxyze; |
546 |
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
547 |
dtype = DTrgbe; |
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 |
} |
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); /* 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 |
return(rmx_write_ascii(dp, nc, len, fp)); |
592 |
case DTfloat: |
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 |
return(rmx_write_rgbe(dp, nc, len, fp)); |
599 |
case DTspec: |
600 |
return(rmx_write_spec(dp, nc, len, fp)); |
601 |
} |
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 |
|
636 |
/* Allocate and assign square identity matrix with n components */ |
637 |
RMATRIX * |
638 |
rmx_identity(const int dim, const int n) |
639 |
{ |
640 |
RMATRIX *rid = rmx_alloc(dim, dim, n); |
641 |
int i, k; |
642 |
|
643 |
if (!rid) |
644 |
return(NULL); |
645 |
memset(rid->mtx, 0, rmx_array_size(rid)); |
646 |
for (i = dim; 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 (may be unallocated) */ |
655 |
RMATRIX * |
656 |
rmx_copy(const RMATRIX *rm) |
657 |
{ |
658 |
RMATRIX *dnew; |
659 |
|
660 |
if (!rm) |
661 |
return(NULL); |
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 |
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) |
713 |
{ |
714 |
RMATRIX *dnew; |
715 |
int i, j; |
716 |
|
717 |
if (!rm || !rm->mtx) |
718 |
return(0); |
719 |
if ((rm->nrows == 1) | (rm->ncols == 1)) { |
720 |
dnew = rmx_copy(rm); |
721 |
if (!dnew) |
722 |
return(NULL); |
723 |
dnew->nrows = rm->ncols; |
724 |
dnew->ncols = rm->nrows; |
725 |
return(dnew); |
726 |
} |
727 |
dnew = rmx_alloc(rm->ncols, rm->nrows, rm->ncomp); |
728 |
if (!dnew) |
729 |
return(NULL); |
730 |
if (rm->info) { |
731 |
rmx_addinfo(dnew, rm->info); |
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_val(rm,j,i), |
740 |
sizeof(rmx_dtype)*dnew->ncomp); |
741 |
return(dnew); |
742 |
} |
743 |
|
744 |
/* Multiply (concatenate) two matrices and allocate the result */ |
745 |
RMATRIX * |
746 |
rmx_multiply(const RMATRIX *m1, const RMATRIX *m2) |
747 |
{ |
748 |
RMATRIX *mres; |
749 |
int i, j, k, h; |
750 |
|
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) |
756 |
return(NULL); |
757 |
i = rmx_newtype(m1->dtype, m2->dtype); |
758 |
if (i) |
759 |
mres->dtype = i; |
760 |
else |
761 |
rmx_addinfo(mres, rmx_mismatch_warn); |
762 |
for (i = mres->nrows; i--; ) |
763 |
for (j = mres->ncols; j--; ) |
764 |
for (k = mres->ncomp; k--; ) { |
765 |
rmx_dtype d = 0; |
766 |
for (h = m1->ncols; h--; ) |
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); |
771 |
} |
772 |
|
773 |
/* Element-wise multiplication (or division) of m2 into m1 */ |
774 |
int |
775 |
rmx_elemult(RMATRIX *m1, const RMATRIX *m2, int divide) |
776 |
{ |
777 |
int zeroDivides = 0; |
778 |
int i, j, k; |
779 |
|
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); |
785 |
i = rmx_newtype(m1->dtype, m2->dtype); |
786 |
if (i) |
787 |
m1->dtype = i; |
788 |
else |
789 |
rmx_addinfo(m1, rmx_mismatch_warn); |
790 |
for (i = m1->nrows; i--; ) |
791 |
for (j = m1->ncols; j--; ) |
792 |
if (divide) { |
793 |
rmx_dtype d; |
794 |
if (m2->ncomp == 1) { |
795 |
d = rmx_val(m2,i,j)[0]; |
796 |
if (d == 0) { |
797 |
++zeroDivides; |
798 |
for (k = m1->ncomp; k--; ) |
799 |
rmx_lval(m1,i,j)[k] = 0; |
800 |
} else { |
801 |
d = 1./d; |
802 |
for (k = m1->ncomp; k--; ) |
803 |
rmx_lval(m1,i,j)[k] *= d; |
804 |
} |
805 |
} else |
806 |
for (k = m1->ncomp; k--; ) { |
807 |
d = rmx_val(m2,i,j)[k]; |
808 |
if (d == 0) { |
809 |
++zeroDivides; |
810 |
rmx_lval(m1,i,j)[k] = 0; |
811 |
} else |
812 |
rmx_lval(m1,i,j)[k] /= d; |
813 |
} |
814 |
} else { |
815 |
if (m2->ncomp == 1) { |
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_val(m2,i,j)[k]; |
822 |
} |
823 |
if (zeroDivides) { |
824 |
rmx_addinfo(m1, "WARNING: zero divide(s) corrupted results\n"); |
825 |
errno = ERANGE; |
826 |
} |
827 |
return(1); |
828 |
} |
829 |
|
830 |
/* Sum second matrix into first, applying scale factor beforehand */ |
831 |
int |
832 |
rmx_sum(RMATRIX *msum, const RMATRIX *madd, const double sf[]) |
833 |
{ |
834 |
double *mysf = NULL; |
835 |
int i, j, k; |
836 |
|
837 |
if (!msum | !madd || !msum->mtx | !madd->mtx | |
838 |
(msum->nrows != madd->nrows) | |
839 |
(msum->ncols != madd->ncols) | |
840 |
(msum->ncomp != madd->ncomp)) |
841 |
return(0); |
842 |
if (!sf) { |
843 |
mysf = (double *)malloc(sizeof(double)*msum->ncomp); |
844 |
if (!mysf) |
845 |
return(0); |
846 |
for (k = msum->ncomp; k--; ) |
847 |
mysf[k] = 1; |
848 |
sf = mysf; |
849 |
} |
850 |
i = rmx_newtype(msum->dtype, madd->dtype); |
851 |
if (i) |
852 |
msum->dtype = i; |
853 |
else |
854 |
rmx_addinfo(msum, rmx_mismatch_warn); |
855 |
for (i = msum->nrows; i--; ) |
856 |
for (j = msum->ncols; 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 |
} |
862 |
if (mysf) |
863 |
free(mysf); |
864 |
return(1); |
865 |
} |
866 |
|
867 |
/* Scale the given matrix by the indicated scalar component vector */ |
868 |
int |
869 |
rmx_scale(RMATRIX *rm, const double sf[]) |
870 |
{ |
871 |
int i, j, k; |
872 |
|
873 |
if (!rm | !sf || !rm->mtx) |
874 |
return(0); |
875 |
for (i = rm->nrows; i--; ) |
876 |
for (j = rm->ncols; 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 |
|
887 |
/* Allocate new matrix and apply component transformation */ |
888 |
RMATRIX * |
889 |
rmx_transform(const RMATRIX *msrc, int n, const double cmat[]) |
890 |
{ |
891 |
int i, j, ks, kd; |
892 |
RMATRIX *dnew; |
893 |
|
894 |
if (!msrc | (n <= 0) | !cmat || !msrc->mtx) |
895 |
return(NULL); |
896 |
dnew = rmx_alloc(msrc->nrows, msrc->ncols, n); |
897 |
if (!dnew) |
898 |
return(NULL); |
899 |
if (msrc->info) { |
900 |
char buf[128]; |
901 |
sprintf(buf, "Applied %dx%d component transform\n", |
902 |
dnew->ncomp, msrc->ncomp); |
903 |
rmx_addinfo(dnew, msrc->info); |
904 |
rmx_addinfo(dnew, buf); |
905 |
} |
906 |
dnew->dtype = msrc->dtype; |
907 |
for (i = dnew->nrows; i--; ) |
908 |
for (j = dnew->ncols; j--; ) { |
909 |
const rmx_dtype *ds = rmx_val(msrc,i,j); |
910 |
for (kd = dnew->ncomp; kd--; ) { |
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; |
915 |
} |
916 |
} |
917 |
return(dnew); |
918 |
} |
919 |
|
920 |
/* Convert a color matrix to newly allocated RMATRIX buffer */ |
921 |
RMATRIX * |
922 |
rmx_from_cmatrix(const CMATRIX *cm) |
923 |
{ |
924 |
int i, j; |
925 |
RMATRIX *dnew; |
926 |
|
927 |
if (!cm) |
928 |
return(NULL); |
929 |
dnew = rmx_alloc(cm->nrows, cm->ncols, 3); |
930 |
if (!dnew) |
931 |
return(NULL); |
932 |
dnew->dtype = DTfloat; |
933 |
for (i = dnew->nrows; i--; ) |
934 |
for (j = dnew->ncols; j--; ) { |
935 |
const COLORV *cv = cm_lval(cm,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]; |
940 |
} |
941 |
return(dnew); |
942 |
} |
943 |
|
944 |
/* Convert general matrix to newly allocated CMATRIX buffer */ |
945 |
CMATRIX * |
946 |
cm_from_rmatrix(const RMATRIX *rm) |
947 |
{ |
948 |
int i, j; |
949 |
CMATRIX *cnew; |
950 |
|
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 rmx_dtype *dp = rmx_val(rm,i,j); |
959 |
COLORV *cv = cm_lval(cnew,i,j); |
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 |
} |