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