1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rmtxop.c,v 2.29 2023/12/03 03:44:42 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* General component matrix operations. |
6 |
*/ |
7 |
|
8 |
#include <errno.h> |
9 |
#include "rtio.h" |
10 |
#include "resolu.h" |
11 |
#include "rmatrix.h" |
12 |
#include "platform.h" |
13 |
|
14 |
#define MAXCOMP MAXCSAMP /* #components we support */ |
15 |
|
16 |
/* Unary matrix operation(s) */ |
17 |
typedef struct { |
18 |
double cmat[MAXCOMP*MAXCOMP]; /* component transformation */ |
19 |
double sca[MAXCOMP]; /* scalar coefficients */ |
20 |
const char *csym; /* symbolic coefs or file */ |
21 |
short clen; /* number of coefficients */ |
22 |
short nsf; /* number of scalars */ |
23 |
short transpose; /* do transpose? */ |
24 |
} RUNARYOP; |
25 |
|
26 |
/* Matrix input source and requested operation(s) */ |
27 |
typedef struct { |
28 |
const char *inspec; /* input specification */ |
29 |
RMPref rmp; /* matrix preference */ |
30 |
RUNARYOP preop; /* unary operation(s) */ |
31 |
RMATRIX *mtx; /* original matrix if loaded */ |
32 |
int binop; /* binary op with next (or 0) */ |
33 |
} ROPMAT; |
34 |
|
35 |
int verbose = 0; /* verbose reporting? */ |
36 |
|
37 |
/* Load matrix */ |
38 |
static int |
39 |
loadmatrix(ROPMAT *rop) |
40 |
{ |
41 |
if (rop->mtx != NULL) /* already loaded? */ |
42 |
return(0); |
43 |
|
44 |
rop->mtx = rmx_load(rop->inspec, rop->rmp); |
45 |
|
46 |
return(!rop->mtx ? -1 : 1); |
47 |
} |
48 |
|
49 |
static int checksymbolic(ROPMAT *rop); |
50 |
|
51 |
/* Check/set transform based on a reference input file */ |
52 |
static int |
53 |
checkreffile(ROPMAT *rop) |
54 |
{ |
55 |
static const char *curRF = NULL; |
56 |
static RMATRIX refm; |
57 |
const int nc = rop->mtx->ncomp; |
58 |
int i; |
59 |
|
60 |
if (!curRF || strcmp(rop->preop.csym, curRF)) { |
61 |
FILE *fp = fopen(rop->preop.csym, "rb"); |
62 |
if (!rmx_load_header(&refm, fp)) { |
63 |
fprintf(stderr, "%s: cannot read info header\n", |
64 |
rop->preop.csym); |
65 |
curRF = NULL; |
66 |
if (fp) fclose(fp); |
67 |
return(-1); |
68 |
} |
69 |
fclose(fp); |
70 |
curRF = rop->preop.csym; |
71 |
} |
72 |
if (refm.ncomp == 3) { |
73 |
rop->preop.csym = (refm.dtype == DTxyze) ? "XYZ" : "RGB"; |
74 |
return(checksymbolic(rop)); |
75 |
} |
76 |
if (refm.ncomp == 2) { |
77 |
fprintf(stderr, "%s: cannot convert to 2 components\n", |
78 |
curRF); |
79 |
return(-1); |
80 |
} |
81 |
if (refm.ncomp == 1) { |
82 |
rop->preop.csym = "Y"; /* XXX big assumption */ |
83 |
return(checksymbolic(rop)); |
84 |
} |
85 |
if (refm.ncomp == nc && |
86 |
!memcmp(refm.wlpart, rop->mtx->wlpart, sizeof(refm.wlpart))) |
87 |
return(0); /* nothing to do */ |
88 |
|
89 |
if ((nc <= 3) | (nc > MAXCSAMP) | (refm.ncomp > MAXCSAMP)) { |
90 |
fprintf(stderr, "%s: cannot resample from %d to %d components\n", |
91 |
curRF, nc, refm.ncomp); |
92 |
return(-1); |
93 |
} |
94 |
rop->preop.clen = refm.ncomp * nc; /* compute spec to ref */ |
95 |
|
96 |
for (i = 0; i < nc; i++) { |
97 |
SCOLOR scstim, scresp; |
98 |
int j; |
99 |
memset(scstim, 0, sizeof(COLORV)*nc); |
100 |
scstim[i] = 1.f; |
101 |
convertscolor(scresp, refm.ncomp, refm.wlpart[0], refm.wlpart[3], |
102 |
scstim, nc, rop->mtx->wlpart[0], rop->mtx->wlpart[3]); |
103 |
for (j = refm.ncomp; j-- > 0; ) |
104 |
rop->preop.cmat[j*nc + i] = scresp[j]; |
105 |
} |
106 |
memcpy(rop->mtx->wlpart, refm.wlpart, sizeof(rop->mtx->wlpart)); |
107 |
return(0); |
108 |
} |
109 |
|
110 |
/* Compute conversion row from spectrum to one channel of RGB */ |
111 |
static void |
112 |
rgbrow(ROPMAT *rop, int r, int p) |
113 |
{ |
114 |
const int nc = rop->mtx->ncomp; |
115 |
const float * wlp = rop->mtx->wlpart; |
116 |
int i; |
117 |
|
118 |
for (i = nc; i--; ) { |
119 |
int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc; |
120 |
int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc; |
121 |
COLOR crgb; |
122 |
spec_rgb(crgb, nmStart, nmEnd); |
123 |
rop->preop.cmat[r*nc+i] = crgb[p]; |
124 |
} |
125 |
} |
126 |
|
127 |
/* Compute conversion row from spectrum to one channel of XYZ */ |
128 |
static void |
129 |
xyzrow(ROPMAT *rop, int r, int p) |
130 |
{ |
131 |
const int nc = rop->mtx->ncomp; |
132 |
const float * wlp = rop->mtx->wlpart; |
133 |
int i; |
134 |
|
135 |
for (i = nc; i--; ) { |
136 |
int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc; |
137 |
int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc; |
138 |
COLOR cxyz; |
139 |
spec_cie(cxyz, nmStart, nmEnd); |
140 |
rop->preop.cmat[r*nc+i] = cxyz[p]; |
141 |
} |
142 |
} |
143 |
|
144 |
/* Use the spectral sensitivity function to compute matrix coefficients */ |
145 |
static void |
146 |
sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4])) |
147 |
{ |
148 |
const int nc = rop->mtx->ncomp; |
149 |
int i; |
150 |
|
151 |
for (i = nc; i--; ) { |
152 |
SCOLOR sclr; |
153 |
memset(sclr, 0, sizeof(COLORV)*nc); |
154 |
sclr[i] = 1.f; |
155 |
rop->preop.cmat[r*nc+i] = (*sf)(sclr, nc, rop->mtx->wlpart); |
156 |
} |
157 |
} |
158 |
|
159 |
/* Check/set symbolic transform */ |
160 |
static int |
161 |
checksymbolic(ROPMAT *rop) |
162 |
{ |
163 |
const int nc = rop->mtx->ncomp; |
164 |
const int dt = rop->mtx->dtype; |
165 |
int i, j; |
166 |
/* check suffix => reference file */ |
167 |
if (strchr(rop->preop.csym, '.') > rop->preop.csym) |
168 |
return(checkreffile(rop)); |
169 |
|
170 |
if (nc < 3) { |
171 |
fprintf(stderr, "%s: -c '%s' requires at least 3 components\n", |
172 |
rop->inspec, rop->preop.csym); |
173 |
return(-1); |
174 |
} |
175 |
rop->preop.clen = strlen(rop->preop.csym) * nc; |
176 |
if (rop->preop.clen > MAXCOMP*MAXCOMP) { |
177 |
fprintf(stderr, "%s: -c '%s' results in too many components\n", |
178 |
rop->inspec, rop->preop.csym); |
179 |
return(-1); |
180 |
} |
181 |
for (j = 0; rop->preop.csym[j]; j++) { |
182 |
int comp = 0; |
183 |
switch (rop->preop.csym[j]) { |
184 |
case 'B': |
185 |
++comp; |
186 |
/* fall through */ |
187 |
case 'G': |
188 |
++comp; |
189 |
/* fall through */ |
190 |
case 'R': |
191 |
if (dt == DTxyze) { |
192 |
for (i = 3; i--; ) |
193 |
rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY * |
194 |
xyz2rgbmat[comp][i]; |
195 |
} else if (nc == 3) |
196 |
rop->preop.cmat[j*nc+comp] = 1.; |
197 |
else |
198 |
rgbrow(rop, j, comp); |
199 |
break; |
200 |
case 'Z': |
201 |
++comp; |
202 |
/* fall through */ |
203 |
case 'Y': |
204 |
++comp; |
205 |
/* fall through */ |
206 |
case 'X': |
207 |
if (dt == DTxyze) { |
208 |
rop->preop.cmat[j*nc+comp] = 1.; |
209 |
} else if (nc == 3) { |
210 |
for (i = 3; i--; ) |
211 |
rop->preop.cmat[j*nc+i] = |
212 |
rgb2xyzmat[comp][i]; |
213 |
} else if (comp == CIEY) |
214 |
sensrow(rop, j, scolor2photopic); |
215 |
else |
216 |
xyzrow(rop, j, comp); |
217 |
|
218 |
for (i = nc*(dt != DTxyze); i--; ) |
219 |
rop->preop.cmat[j*nc+i] *= WHTEFFICACY; |
220 |
break; |
221 |
case 'S': /* scotopic (il)luminance */ |
222 |
sensrow(rop, j, scolor2scotopic); |
223 |
for (i = nc; i--; ) |
224 |
rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC; |
225 |
break; |
226 |
case 'M': /* melanopic (il)luminance */ |
227 |
sensrow(rop, j, scolor2melanopic); |
228 |
for (i = nc; i--; ) |
229 |
rop->preop.cmat[j*nc+i] *= WHTMELANOPIC; |
230 |
break; |
231 |
case 'A': /* average component */ |
232 |
for (i = nc; i--; ) |
233 |
rop->preop.cmat[j*nc+i] = 1./(double)nc; |
234 |
break; |
235 |
default: |
236 |
fprintf(stderr, "%s: -c '%c' unsupported\n", |
237 |
rop->inspec, rop->preop.csym[j]); |
238 |
return(-1); |
239 |
} |
240 |
} |
241 |
/* return recommended output type */ |
242 |
if (!strcmp(rop->preop.csym, "XYZ")) { |
243 |
if (dt <= DTspec) |
244 |
return(DTxyze); |
245 |
} else if (!strcmp(rop->preop.csym, "RGB")) { |
246 |
if (dt <= DTspec) |
247 |
return(DTrgbe); |
248 |
} else if (dt == DTspec) |
249 |
return(DTfloat); /* probably not actual spectrum */ |
250 |
return(0); |
251 |
} |
252 |
|
253 |
/* Get matrix and perform unary operations */ |
254 |
static RMATRIX * |
255 |
loadop(ROPMAT *rop) |
256 |
{ |
257 |
int outtype = 0; |
258 |
RMATRIX *mres; |
259 |
int i, j; |
260 |
|
261 |
if (loadmatrix(rop) < 0) /* make sure we're loaded */ |
262 |
return(NULL); |
263 |
|
264 |
if (rop->preop.csym && /* symbolic transform? */ |
265 |
(outtype = checksymbolic(rop)) < 0) |
266 |
goto failure; |
267 |
if (rop->preop.clen > 0) { /* apply component transform? */ |
268 |
if (rop->preop.clen % rop->mtx->ncomp) { |
269 |
fprintf(stderr, "%s: -c must have N x %d coefficients\n", |
270 |
rop->inspec, rop->mtx->ncomp); |
271 |
goto failure; |
272 |
} |
273 |
if (rop->preop.nsf > 0) { /* scale transform, first */ |
274 |
if (rop->preop.nsf == 1) { |
275 |
for (i = rop->preop.clen; i--; ) |
276 |
rop->preop.cmat[i] *= rop->preop.sca[0]; |
277 |
} else if (rop->preop.nsf*rop->mtx->ncomp != rop->preop.clen) { |
278 |
fprintf(stderr, "%s: -s must have one or %d factors\n", |
279 |
rop->inspec, |
280 |
rop->preop.clen/rop->mtx->ncomp); |
281 |
goto failure; |
282 |
} else { |
283 |
for (i = rop->preop.nsf; i--; ) |
284 |
for (j = rop->mtx->ncomp; j--; ) |
285 |
rop->preop.cmat[i*rop->mtx->ncomp+j] |
286 |
*= rop->preop.sca[i]; |
287 |
} |
288 |
} |
289 |
mres = rmx_transform(rop->mtx, rop->preop.clen/rop->mtx->ncomp, |
290 |
rop->preop.cmat); |
291 |
if (mres == NULL) { |
292 |
fprintf(stderr, "%s: matrix transform failed\n", |
293 |
rop->inspec); |
294 |
goto failure; |
295 |
} |
296 |
if (verbose) |
297 |
fprintf(stderr, "%s: applied %d x %d transform%s\n", |
298 |
rop->inspec, mres->ncomp, |
299 |
rop->mtx->ncomp, |
300 |
rop->preop.nsf ? " (* scalar)" : ""); |
301 |
rop->preop.nsf = 0; /* now folded in */ |
302 |
if ((mres->ncomp > 3) & (mres->dtype <= DTspec)) |
303 |
outtype = DTfloat; /* probably not actual spectrum */ |
304 |
rmx_free(rop->mtx); |
305 |
rop->mtx = mres; |
306 |
} |
307 |
if (rop->preop.nsf > 0) { /* apply scalar(s)? */ |
308 |
if (rop->preop.nsf == 1) { |
309 |
for (i = rop->mtx->ncomp; --i; ) |
310 |
rop->preop.sca[i] = rop->preop.sca[0]; |
311 |
} else if (rop->preop.nsf != rop->mtx->ncomp) { |
312 |
fprintf(stderr, "%s: -s must have one or %d factors\n", |
313 |
rop->inspec, rop->mtx->ncomp); |
314 |
goto failure; |
315 |
} |
316 |
if (!rmx_scale(rop->mtx, rop->preop.sca)) { |
317 |
fputs(rop->inspec, stderr); |
318 |
fputs(": scalar operation failed\n", stderr); |
319 |
goto failure; |
320 |
} |
321 |
if (verbose) { |
322 |
fputs(rop->inspec, stderr); |
323 |
fputs(": applied scalar (", stderr); |
324 |
for (i = 0; i < rop->preop.nsf; i++) |
325 |
fprintf(stderr, " %f", rop->preop.sca[i]); |
326 |
fputs(" )\n", stderr); |
327 |
} |
328 |
} |
329 |
if (rop->preop.transpose) { /* transpose matrix? */ |
330 |
mres = rmx_transpose(rop->mtx); |
331 |
if (mres == NULL) { |
332 |
fputs(rop->inspec, stderr); |
333 |
fputs(": transpose failed\n", stderr); |
334 |
goto failure; |
335 |
} |
336 |
if (verbose) { |
337 |
fputs(rop->inspec, stderr); |
338 |
fputs(": transposed rows and columns\n", stderr); |
339 |
} |
340 |
rmx_free(rop->mtx); |
341 |
rop->mtx = mres; |
342 |
} |
343 |
mres = rop->mtx; |
344 |
rop->mtx = NULL; |
345 |
if (outtype) |
346 |
mres->dtype = outtype; |
347 |
return(mres); |
348 |
failure: |
349 |
rmx_free(rop->mtx); |
350 |
return(rop->mtx = NULL); |
351 |
} |
352 |
|
353 |
/* Execute binary operation, free matrix arguments and return new result */ |
354 |
static RMATRIX * |
355 |
binaryop(const char *inspec, RMATRIX *mleft, int op, RMATRIX *mright) |
356 |
{ |
357 |
RMATRIX *mres = NULL; |
358 |
int i; |
359 |
|
360 |
if ((mleft == NULL) | (mright == NULL)) |
361 |
return(NULL); |
362 |
switch (op) { |
363 |
case '.': /* concatenate */ |
364 |
if (mleft->ncomp != mright->ncomp) { |
365 |
fputs(inspec, stderr); |
366 |
fputs(": # components do not match\n", stderr); |
367 |
} else if (mleft->ncols != mright->nrows) { |
368 |
fputs(inspec, stderr); |
369 |
fputs(": mismatched dimensions\n", |
370 |
stderr); |
371 |
} else |
372 |
mres = rmx_multiply(mleft, mright); |
373 |
rmx_free(mleft); |
374 |
rmx_free(mright); |
375 |
if (mres == NULL) { |
376 |
fputs(inspec, stderr); |
377 |
fputs(": concatenation failed\n", stderr); |
378 |
return(NULL); |
379 |
} |
380 |
if (verbose) { |
381 |
fputs(inspec, stderr); |
382 |
fputs(": concatenated matrix\n", stderr); |
383 |
} |
384 |
break; |
385 |
case '+': |
386 |
if (!rmx_sum(mleft, mright, NULL)) { |
387 |
fputs(inspec, stderr); |
388 |
fputs(": matrix sum failed\n", stderr); |
389 |
rmx_free(mleft); |
390 |
rmx_free(mright); |
391 |
return(NULL); |
392 |
} |
393 |
if (verbose) { |
394 |
fputs(inspec, stderr); |
395 |
fputs(": added in matrix\n", stderr); |
396 |
} |
397 |
rmx_free(mright); |
398 |
mres = mleft; |
399 |
break; |
400 |
case '*': |
401 |
case '/': { |
402 |
const char * tnam = (op == '/') ? |
403 |
"division" : "multiplication"; |
404 |
errno = 0; |
405 |
if (!rmx_elemult(mleft, mright, (op == '/'))) { |
406 |
fprintf(stderr, "%s: element-wise %s failed\n", |
407 |
inspec, tnam); |
408 |
rmx_free(mleft); |
409 |
rmx_free(mright); |
410 |
return(NULL); |
411 |
} |
412 |
if (errno) |
413 |
fprintf(stderr, |
414 |
"%s: warning - error during element-wise %s\n", |
415 |
inspec, tnam); |
416 |
else if (verbose) |
417 |
fprintf(stderr, "%s: element-wise %s\n", inspec, tnam); |
418 |
rmx_free(mright); |
419 |
mres = mleft; |
420 |
} break; |
421 |
default: |
422 |
fprintf(stderr, "%s: unknown operation '%c'\n", inspec, op); |
423 |
rmx_free(mleft); |
424 |
rmx_free(mright); |
425 |
return(NULL); |
426 |
} |
427 |
return(mres); |
428 |
} |
429 |
|
430 |
/* Perform matrix operations from left to right */ |
431 |
static RMATRIX * |
432 |
op_left2right(ROPMAT *mop) |
433 |
{ |
434 |
RMATRIX *mleft = loadop(mop); |
435 |
|
436 |
while (mop->binop) { |
437 |
if (mleft == NULL) |
438 |
break; |
439 |
mleft = binaryop(mop[1].inspec, |
440 |
mleft, mop->binop, loadop(mop+1)); |
441 |
mop++; |
442 |
} |
443 |
return(mleft); |
444 |
} |
445 |
|
446 |
/* Perform matrix operations from right to left */ |
447 |
static RMATRIX * |
448 |
op_right2left(ROPMAT *mop) |
449 |
{ |
450 |
RMATRIX *mright; |
451 |
int rpos = 0; |
452 |
/* find end of list */ |
453 |
while (mop[rpos].binop) |
454 |
if (mop[rpos++].binop != '.') { |
455 |
fputs( |
456 |
"Right-to-left evaluation only for matrix multiplication!\n", |
457 |
stderr); |
458 |
return(NULL); |
459 |
} |
460 |
mright = loadop(mop+rpos); |
461 |
while (rpos-- > 0) { |
462 |
if (mright == NULL) |
463 |
break; |
464 |
mright = binaryop(mop[rpos+1].inspec, |
465 |
loadop(mop+rpos), mop[rpos].binop, mright); |
466 |
} |
467 |
return(mright); |
468 |
} |
469 |
|
470 |
#define t_nrows(mop) ((mop)->preop.transpose ? (mop)->mtx->ncols \ |
471 |
: (mop)->mtx->nrows) |
472 |
#define t_ncols(mop) ((mop)->preop.transpose ? (mop)->mtx->nrows \ |
473 |
: (mop)->mtx->ncols) |
474 |
|
475 |
/* Should we prefer concatenating from rightmost matrix towards left? */ |
476 |
static int |
477 |
prefer_right2left(ROPMAT *mop) |
478 |
{ |
479 |
int mri = 0; |
480 |
|
481 |
while (mop[mri].binop) /* find rightmost matrix */ |
482 |
if (mop[mri++].binop != '.') |
483 |
return(0); /* pre-empt reversal for other ops */ |
484 |
|
485 |
if (mri <= 1) |
486 |
return(0); /* won't matter */ |
487 |
|
488 |
if (loadmatrix(mop+mri) < 0) /* load rightmost cat */ |
489 |
return(1); /* fail will bail in a moment */ |
490 |
|
491 |
if (t_ncols(mop+mri) == 1) |
492 |
return(1); /* definitely better R->L */ |
493 |
|
494 |
if (t_ncols(mop+mri) >= t_nrows(mop+mri)) |
495 |
return(0); /* ...probably worse */ |
496 |
|
497 |
if (loadmatrix(mop) < 0) /* load leftmost */ |
498 |
return(0); /* fail will bail in a moment */ |
499 |
|
500 |
return(t_ncols(mop+mri) < t_nrows(mop)); |
501 |
} |
502 |
|
503 |
static int |
504 |
get_factors(double da[], int n, char *av[]) |
505 |
{ |
506 |
int ac; |
507 |
|
508 |
for (ac = 0; ac < n && isflt(av[ac]); ac++) |
509 |
da[ac] = atof(av[ac]); |
510 |
return(ac); |
511 |
} |
512 |
|
513 |
static ROPMAT * |
514 |
resize_moparr(ROPMAT *mop, int n2alloc) |
515 |
{ |
516 |
int nmats = 0; |
517 |
int i; |
518 |
|
519 |
while (mop[nmats++].binop) |
520 |
; |
521 |
for (i = nmats; i > n2alloc; i--) |
522 |
rmx_free(mop[i].mtx); |
523 |
mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT)); |
524 |
if (mop == NULL) { |
525 |
fputs("Out of memory in resize_moparr()\n", stderr); |
526 |
exit(1); |
527 |
} |
528 |
if (n2alloc > nmats) |
529 |
memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT)); |
530 |
return(mop); |
531 |
} |
532 |
|
533 |
/* Load one or more matrices and operate on them, sending results to stdout */ |
534 |
int |
535 |
main(int argc, char *argv[]) |
536 |
{ |
537 |
int outfmt = DTfromHeader; |
538 |
const char *defCsym = NULL; |
539 |
int nall = 2; |
540 |
ROPMAT *mop = (ROPMAT *)calloc(nall, sizeof(ROPMAT)); |
541 |
int nmats = 0; |
542 |
RMATRIX *mres = NULL; |
543 |
int stdin_used = 0; |
544 |
int i; |
545 |
/* get options and arguments */ |
546 |
for (i = 1; i < argc; i++) { |
547 |
if (argv[i][0] && !argv[i][1] && |
548 |
strchr(".+*/", argv[i][0]) != NULL) { |
549 |
if (!nmats || mop[nmats-1].binop) { |
550 |
fprintf(stderr, |
551 |
"%s: missing matrix argument before '%c' operation\n", |
552 |
argv[0], argv[i][0]); |
553 |
return(1); |
554 |
} |
555 |
mop[nmats-1].binop = argv[i][0]; |
556 |
} else if (argv[i][0] != '-' || !argv[i][1]) { |
557 |
if (argv[i][0] == '-') { |
558 |
if (stdin_used++) { |
559 |
fprintf(stderr, |
560 |
"%s: standard input used for more than one matrix\n", |
561 |
argv[0]); |
562 |
return(1); |
563 |
} |
564 |
mop[nmats].inspec = stdin_name; |
565 |
} else |
566 |
mop[nmats].inspec = argv[i]; |
567 |
if (!mop[nmats].preop.csym) |
568 |
mop[nmats].preop.csym = defCsym; |
569 |
if (nmats > 0 && !mop[nmats-1].binop) |
570 |
mop[nmats-1].binop = '.'; |
571 |
nmats++; |
572 |
} else { |
573 |
int n = argc-1 - i; |
574 |
switch (argv[i][1]) { /* get option */ |
575 |
case 'v': |
576 |
verbose++; |
577 |
break; |
578 |
case 'f': |
579 |
switch (argv[i][2]) { |
580 |
case 'd': |
581 |
outfmt = DTdouble; |
582 |
break; |
583 |
case 'f': |
584 |
outfmt = DTfloat; |
585 |
break; |
586 |
case 'a': |
587 |
outfmt = DTascii; |
588 |
break; |
589 |
case 'c': |
590 |
outfmt = DTrgbe; |
591 |
break; |
592 |
default: |
593 |
goto userr; |
594 |
} |
595 |
break; |
596 |
case 't': |
597 |
mop[nmats].preop.transpose = 1; |
598 |
break; |
599 |
case 's': |
600 |
if (n > MAXCOMP) n = MAXCOMP; |
601 |
i += mop[nmats].preop.nsf = |
602 |
get_factors(mop[nmats].preop.sca, |
603 |
n, argv+i+1); |
604 |
if (mop[nmats].preop.nsf <= 0) { |
605 |
fprintf(stderr, "%s: -s missing arguments\n", |
606 |
argv[0]); |
607 |
goto userr; |
608 |
} |
609 |
break; |
610 |
case 'C': |
611 |
if (!n || isflt(argv[i+1])) |
612 |
goto userr; |
613 |
defCsym = mop[nmats].preop.csym = argv[++i]; |
614 |
mop[nmats].preop.clen = 0; |
615 |
break; |
616 |
case 'c': |
617 |
if (n && !isflt(argv[i+1])) { |
618 |
mop[nmats].preop.csym = argv[++i]; |
619 |
mop[nmats].preop.clen = 0; |
620 |
break; |
621 |
} |
622 |
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
623 |
i += mop[nmats].preop.clen = |
624 |
get_factors(mop[nmats].preop.cmat, |
625 |
n, argv+i+1); |
626 |
if (mop[nmats].preop.clen <= 0) { |
627 |
fprintf(stderr, "%s: -c missing arguments\n", |
628 |
argv[0]); |
629 |
goto userr; |
630 |
} |
631 |
mop[nmats].preop.csym = NULL; |
632 |
break; |
633 |
case 'r': |
634 |
if (argv[i][2] == 'f') |
635 |
mop[nmats].rmp = RMPreflF; |
636 |
else if (argv[i][2] == 'b') |
637 |
mop[nmats].rmp = RMPreflB; |
638 |
else |
639 |
goto userr; |
640 |
break; |
641 |
default: |
642 |
fprintf(stderr, "%s: unknown operation '%s'\n", |
643 |
argv[0], argv[i]); |
644 |
goto userr; |
645 |
} |
646 |
} |
647 |
if (nmats >= nall) |
648 |
mop = resize_moparr(mop, nall += 2); |
649 |
} |
650 |
if (mop[0].inspec == NULL) /* nothing to do? */ |
651 |
goto userr; |
652 |
if (mop[nmats-1].binop) { |
653 |
fprintf(stderr, |
654 |
"%s: missing matrix argument after '%c' operation\n", |
655 |
argv[0], mop[nmats-1].binop); |
656 |
return(1); |
657 |
} |
658 |
/* favor quicker concatenation */ |
659 |
mop[nmats].mtx = prefer_right2left(mop) ? op_right2left(mop) |
660 |
: op_left2right(mop); |
661 |
if (mop[nmats].mtx == NULL) |
662 |
return(1); |
663 |
/* apply trailing unary operations */ |
664 |
mop[nmats].inspec = "trailing_ops"; |
665 |
mres = loadop(mop+nmats); |
666 |
if (mres == NULL) |
667 |
return(1); |
668 |
if (outfmt == DTfromHeader) /* check data type */ |
669 |
outfmt = mres->dtype; |
670 |
if (outfmt == DTrgbe) { |
671 |
if (mres->ncomp > 3) |
672 |
outfmt = DTspec; |
673 |
else if (mres->dtype == DTxyze) |
674 |
outfmt = DTxyze; |
675 |
} |
676 |
newheader("RADIANCE", stdout); /* write result to stdout */ |
677 |
printargs(argc, argv, stdout); |
678 |
return(rmx_write(mres, outfmt, stdout) ? 0 : 1); |
679 |
userr: |
680 |
fprintf(stderr, |
681 |
"Usage: %s [-v][-f{adfc}][-t][-s sf .. | -c ce ..][-rf|-rb] m1 [.+*/] .. > mres\n", |
682 |
argv[0]); |
683 |
return(1); |
684 |
} |