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