ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcomb.c
Revision: 2.2
Committed: Mon Dec 18 23:04:05 2023 UTC (4 months, 4 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +13 -8 lines
Log Message:
fix(rcomb): Fixed handling of matrix inputs where NROWS is unspecified

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcomb.c,v 2.1 2023/12/12 16:31:45 greg Exp $";
3 #endif
4 /*
5 * General component matrix combiner, operating on a row at a time.
6 */
7
8 #include <errno.h>
9 #include <math.h>
10 #include "platform.h"
11 #include "rtio.h"
12 #include "resolu.h"
13 #include "rmatrix.h"
14 #include "calcomp.h"
15 #include "paths.h"
16
17 #ifndef M_PI
18 #define M_PI 3.14159265358979323846
19 #endif
20
21 #define MAXCOMP MAXCSAMP /* #components we support */
22
23 /* Unary matrix operation(s) */
24 typedef struct {
25 double cmat[MAXCOMP*MAXCOMP]; /* component transformation */
26 double sca[MAXCOMP]; /* scalar coefficients */
27 const char *csym; /* symbolic coefficients */
28 short clen; /* number of coefficients */
29 short nsf; /* number of scalars */
30 } RUNARYOP;
31
32 /* Input matrix */
33 typedef struct {
34 const char *inspec; /* input specification */
35 RUNARYOP preop; /* transform operation */
36 RMATRIX imx; /* input matrix header info */
37 RMATRIX *rmp; /* active single-row matrix */
38 FILE *infp; /* open input stream */
39 } ROPMAT;
40
41 ROPMAT *mop = NULL; /* allocated input array */
42 int nall = 0; /* number allocated */
43 int nmats = 0; /* number of actual inputs */
44
45 RMATRIX *mcat = NULL; /* final concatenation */
46 int mcat_last = 0; /* goes after trailing ops? */
47
48 int in_nrows; /* number of input rows (or 0) */
49 #define in_ncols (mop[0].rmp->ncols) /* number of input columns */
50 #define in_ncomp (mop[0].rmp->ncomp) /* input #components */
51
52 extern int nowarn; /* turn off warnings? */
53
54 int cur_row; /* current input/output row */
55 int cur_col; /* current input/output column */
56 int cur_chan; /* if we're looping channels */
57
58 static int checksymbolic(ROPMAT *rop);
59
60 static int
61 split_input(ROPMAT *rop)
62 {
63 if (rop->rmp == &rop->imx && !(rop->rmp = rmx_copy(&rop->imx))) {
64 fputs("Out of memory in split_input()\n", stderr);
65 return(0);
66 }
67 rmx_reset(rop->rmp);
68 return(1);
69 }
70
71 /* Check/set transform based on a reference input file */
72 static int
73 checkreffile(ROPMAT *rop)
74 {
75 static const char *curRF = NULL;
76 static RMATRIX refm;
77 const int nc = rop->imx.ncomp;
78 int i;
79
80 if (!curRF || strcmp(rop->preop.csym, curRF)) {
81 FILE *fp = fopen(rop->preop.csym, "rb");
82 if (!rmx_load_header(&refm, fp)) {
83 fprintf(stderr, "%s: cannot read info header\n",
84 rop->preop.csym);
85 curRF = NULL;
86 if (fp) fclose(fp);
87 return(0);
88 }
89 fclose(fp);
90 curRF = rop->preop.csym;
91 }
92 if (refm.ncomp == 3) {
93 rop->preop.csym = (refm.dtype == DTxyze) ? "XYZ" : "RGB";
94 return(checksymbolic(rop));
95 }
96 if (refm.ncomp == 2) {
97 fprintf(stderr, "%s: cannot convert to 2 components\n",
98 curRF);
99 return(0);
100 }
101 if (refm.ncomp == 1) {
102 rop->preop.csym = "Y"; /* XXX big assumption */
103 return(checksymbolic(rop));
104 }
105 if (refm.ncomp == nc &&
106 !memcmp(refm.wlpart, rop->imx.wlpart, sizeof(refm.wlpart)))
107 return(1); /* nothing to do */
108
109 if ((nc <= 3) | (nc > MAXCSAMP) | (refm.ncomp > MAXCSAMP)) {
110 fprintf(stderr, "%s: cannot resample from %d to %d components\n",
111 curRF, nc, refm.ncomp);
112 return(0);
113 }
114 if (!split_input(rop)) /* get our own struct */
115 return(0);
116 rop->preop.clen = refm.ncomp * nc; /* compute spec to ref */
117
118 for (i = 0; i < nc; i++) {
119 SCOLOR scstim, scresp;
120 int j;
121 memset(scstim, 0, sizeof(COLORV)*nc);
122 scstim[i] = 1.f;
123 convertscolor(scresp, refm.ncomp, refm.wlpart[0], refm.wlpart[3],
124 scstim, nc, rop->imx.wlpart[0], rop->imx.wlpart[3]);
125 for (j = refm.ncomp; j-- > 0; )
126 rop->preop.cmat[j*nc + i] = scresp[j];
127 }
128 /* remember new spectral params */
129 memcpy(rop->rmp->wlpart, refm.wlpart, sizeof(rop->rmp->wlpart));
130 rop->rmp->ncomp = refm.ncomp;
131 return(1);
132 }
133
134 /* Compute conversion row from spectrum to one channel of RGB */
135 static void
136 rgbrow(ROPMAT *rop, int r, int p)
137 {
138 const int nc = rop->imx.ncomp;
139 const float * wlp = rop->imx.wlpart;
140 int i;
141
142 for (i = nc; i--; ) {
143 int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc;
144 int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc;
145 COLOR crgb;
146 spec_rgb(crgb, nmStart, nmEnd);
147 rop->preop.cmat[r*nc+i] = crgb[p];
148 }
149 }
150
151 /* Compute conversion row from spectrum to one channel of XYZ */
152 static void
153 xyzrow(ROPMAT *rop, int r, int p)
154 {
155 const int nc = rop->imx.ncomp;
156 const float * wlp = rop->imx.wlpart;
157 int i;
158
159 for (i = nc; i--; ) {
160 int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc;
161 int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc;
162 COLOR cxyz;
163 spec_cie(cxyz, nmStart, nmEnd);
164 rop->preop.cmat[r*nc+i] = cxyz[p];
165 }
166 }
167
168 /* Use the spectral sensitivity function to compute matrix coefficients */
169 static void
170 sensrow(ROPMAT *rop, int r, double (*sf)(SCOLOR sc, int ncs, const float wlpt[4]))
171 {
172 const int nc = rop->imx.ncomp;
173 int i;
174
175 for (i = nc; i--; ) {
176 SCOLOR sclr;
177 memset(sclr, 0, sizeof(COLORV)*nc);
178 sclr[i] = 1.f;
179 rop->preop.cmat[r*nc+i] = (*sf)(sclr, nc, rop->imx.wlpart);
180 }
181 }
182
183 /* Check/set symbolic transform */
184 static int
185 checksymbolic(ROPMAT *rop)
186 {
187 const int nc = rop->imx.ncomp;
188 const int dt = rop->imx.dtype;
189 int i, j;
190 /* check suffix => reference file */
191 if (strchr(rop->preop.csym, '.') > rop->preop.csym)
192 return(checkreffile(rop));
193
194 if (nc < 3) {
195 fprintf(stderr, "%s: -c '%s' requires at least 3 components\n",
196 rop->inspec, rop->preop.csym);
197 return(0);
198 }
199 rop->preop.clen = strlen(rop->preop.csym) * nc;
200 if (rop->preop.clen > MAXCOMP*MAXCOMP) {
201 fprintf(stderr, "%s: -c '%s' results in too many components\n",
202 rop->inspec, rop->preop.csym);
203 return(0);
204 }
205 for (j = 0; rop->preop.csym[j]; j++) {
206 int comp = 0;
207 switch (rop->preop.csym[j]) {
208 case 'B':
209 ++comp;
210 /* fall through */
211 case 'G':
212 ++comp;
213 /* fall through */
214 case 'R':
215 if (dt == DTxyze) {
216 for (i = 3; i--; )
217 rop->preop.cmat[j*nc+i] = 1./WHTEFFICACY *
218 xyz2rgbmat[comp][i];
219 } else if (nc == 3)
220 rop->preop.cmat[j*nc+comp] = 1.;
221 else
222 rgbrow(rop, j, comp);
223 break;
224 case 'Z':
225 ++comp;
226 /* fall through */
227 case 'Y':
228 ++comp;
229 /* fall through */
230 case 'X':
231 if (dt == DTxyze) {
232 rop->preop.cmat[j*nc+comp] = 1.;
233 } else if (nc == 3) {
234 for (i = 3; i--; )
235 rop->preop.cmat[j*nc+i] =
236 rgb2xyzmat[comp][i];
237 } else if (comp == CIEY)
238 sensrow(rop, j, scolor2photopic);
239 else
240 xyzrow(rop, j, comp);
241
242 for (i = nc*(dt != DTxyze); i--; )
243 rop->preop.cmat[j*nc+i] *= WHTEFFICACY;
244 break;
245 case 'S': /* scotopic (il)luminance */
246 sensrow(rop, j, scolor2scotopic);
247 for (i = nc; i--; )
248 rop->preop.cmat[j*nc+i] *= WHTSCOTOPIC;
249 break;
250 case 'M': /* melanopic (il)luminance */
251 sensrow(rop, j, scolor2melanopic);
252 for (i = nc; i--; )
253 rop->preop.cmat[j*nc+i] *= WHTMELANOPIC;
254 break;
255 case 'A': /* average component */
256 for (i = nc; i--; )
257 rop->preop.cmat[j*nc+i] = 1./(double)nc;
258 break;
259 default:
260 fprintf(stderr, "%s: -c '%c' unsupported\n",
261 rop->inspec, rop->preop.csym[j]);
262 return(0);
263 }
264 }
265 if (!split_input(rop)) /* get our own struct */
266 return(0);
267 memcpy(rop->rmp->wlpart, WLPART, sizeof(rop->rmp->wlpart));
268 rop->rmp->ncomp = rop->preop.clen / nc;
269 /* decide on output type */
270 if (!strcmp(rop->preop.csym, "XYZ")) {
271 if (dt <= DTspec)
272 rop->rmp->dtype = DTxyze;
273 } else if (!strcmp(rop->preop.csym, "RGB")) {
274 if (dt <= DTspec)
275 rop->rmp->dtype = DTrgbe;
276 } else if (rop->rmp->dtype == DTspec)
277 rop->rmp->dtype = DTfloat;
278 return(1);
279 }
280
281 static int
282 get_component_xfm(ROPMAT *rop)
283 {
284 int i, j;
285
286 if (rop->rmp != &rop->imx) { /* reset destination matrix */
287 rmx_free(rop->rmp);
288 rop->rmp = &rop->imx;
289 }
290 if (rop->preop.csym && /* symbolic transform? */
291 !checksymbolic(rop))
292 return(0);
293 /* undo exposure? */
294 if (fabs(1. - bright(rop->rmp->cexp)) > .025) {
295 if (rop->rmp->ncomp == 1)
296 rop->rmp->cexp[RED] = rop->rmp->cexp[GRN] =
297 rop->rmp->cexp[BLU] = bright(rop->rmp->cexp);
298 if (rop->preop.nsf <= 0) {
299 rop->preop.nsf = i = rop->rmp->ncomp;
300 while (i--)
301 rop->preop.sca[i] = 1.;
302 }
303 if (rop->preop.nsf == 1) {
304 if (rop->rmp->ncomp == 3) {
305 rop->preop.sca[2] = rop->preop.sca[1] =
306 rop->preop.sca[0];
307 rop->preop.nsf = 3;
308 } else
309 rop->preop.sca[0] /= bright(rop->rmp->cexp);
310 }
311 if (rop->preop.nsf == 3) {
312 opcolor(rop->preop.sca, /=, rop->rmp->cexp);
313 } else if (rop->preop.nsf > 3) { /* punt */
314 double mult = 1./bright(rop->rmp->cexp);
315 for (i = rop->preop.nsf; i--; )
316 rop->preop.sca[i] *= mult;
317 }
318 setcolor(rop->rmp->cexp, 1., 1., 1.);
319 }
320 if (rop->preop.clen > 0) { /* use component transform? */
321 if (rop->preop.clen % rop->imx.ncomp) {
322 fprintf(stderr, "%s: -c must have N x %d coefficients\n",
323 rop->inspec, rop->imx.ncomp);
324 return(0);
325 }
326 if (rop->preop.nsf > 0) { /* scale transform, instead */
327 if (rop->preop.nsf == 1) {
328 for (i = rop->preop.clen; i--; )
329 rop->preop.cmat[i] *= rop->preop.sca[0];
330 } else if (rop->preop.nsf*rop->imx.ncomp != rop->preop.clen) {
331 fprintf(stderr, "%s: -s must have one or %d factors\n",
332 rop->inspec,
333 rop->preop.clen/rop->imx.ncomp);
334 return(0);
335 } else {
336 for (i = rop->preop.nsf; i--; )
337 for (j = rop->imx.ncomp; j--; )
338 rop->preop.cmat[i*rop->imx.ncomp+j]
339 *= rop->preop.sca[i];
340 }
341 }
342 rop->preop.nsf = 0; /* now folded in */
343 if (!split_input(rop)) /* get our own struct */
344 return(0);
345 rop->rmp->ncomp = rop->preop.clen / rop->imx.ncomp;
346 if ((rop->rmp->ncomp > 3) & (rop->rmp->dtype <= DTspec)) {
347 rop->rmp->dtype = DTfloat; /* probably not actual spectrum */
348 memcpy(rop->rmp->wlpart, WLPART, sizeof(rop->rmp->wlpart));
349 }
350 } else if (rop->preop.nsf > 0) { /* else use scalar(s)? */
351 if (rop->preop.nsf == 1) {
352 for (i = rop->rmp->ncomp; --i; )
353 rop->preop.sca[i] = rop->preop.sca[0];
354 rop->preop.nsf = rop->rmp->ncomp;
355 } else if (rop->preop.nsf != rop->rmp->ncomp) {
356 fprintf(stderr, "%s: -s must have one or %d factors\n",
357 rop->inspec, rop->rmp->ncomp);
358 return(0);
359 }
360 }
361 return(1);
362 }
363
364 static int
365 apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro)
366 {
367 if (ro->clen > 0) {
368 RMATRIX *res = rmx_transform(src, dst->ncomp, ro->cmat);
369 if (!res) {
370 fputs("Error in call to rmx_transform()\n", stderr);
371 return(0);
372 }
373 if (!rmx_transfer_data(dst, res, 0))
374 return(0);
375 rmx_free(res);
376 } else if (dst != src)
377 memcpy(dst->mtx, src->mtx,
378 sizeof(double)*dst->ncomp*dst->ncols*dst->nrows);
379 if (ro->nsf == dst->ncomp)
380 rmx_scale(dst, ro->sca);
381 return(1);
382 }
383
384 static int
385 open_input(ROPMAT *rop)
386 {
387 int outtype;
388
389 if (!rop || !rop->inspec || !rop->inspec[0])
390 return(0);
391 if (rop->inspec == stdin_name)
392 rop->infp = stdin;
393 else if (rop->inspec[0] == '!')
394 rop->infp = popen(rop->inspec+1, "r");
395 else
396 rop->infp = fopen(rop->inspec, "rb");
397
398 if (!rmx_load_header(&rop->imx, rop->infp)) {
399 fprintf(stderr, "Bad header from: %s\n", rop->inspec);
400 return(0);
401 }
402 return(get_component_xfm(rop));
403 }
404
405 /* Return nominal wavelength associated with input component (return nm) */
406 static double
407 l_wavelength(char *nam)
408 {
409 double comp = argument(1);
410
411 if ((comp < -.5) | (comp >= in_ncomp+.5)) {
412 errno = EDOM;
413 return(.0);
414 }
415 if (comp < .5) /* asking for #components? */
416 return(in_ncomp);
417
418 if (in_ncomp == 3) { /* special case for RGB */
419 const int w0 = (int)(comp - .5);
420 return(mop[0].rmp->wlpart[w0] +
421 (comp-.5)*(mop[0].rmp->wlpart[w0+1] -
422 mop[0].rmp->wlpart[w0]));
423 }
424 return(mop[0].rmp->wlpart[0] + /* general case, even div. */
425 (comp-.5)/(double)in_ncomp *
426 (mop[0].rmp->wlpart[3] - mop[0].rmp->wlpart[0]));
427 }
428
429 /* Return ith input with optional channel selector */
430 static double
431 l_chanin(char *nam)
432 {
433 double inp = argument(1);
434 int mi, chan;
435
436 if ((mi = (int)(inp-.5)) < 0 || mi >= nmats) {
437 errno = EDOM;
438 return(.0);
439 }
440 if (inp < .5) /* asking for #inputs? */
441 return(nmats);
442
443 if (nargum() >= 2) {
444 double cval = argument(2);
445 if (cval < .5 || (chan = (int)(cval-.5)) >= in_ncomp) {
446 errno = EDOM;
447 return(.0);
448 }
449 } else
450 chan = cur_chan;
451
452 return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]);
453 }
454
455 static int
456 initialize(RMATRIX *imp)
457 {
458 int i;
459 /* XXX struct is zeroed coming in */
460 setcolor(imp->cexp, 1.f, 1.f, 1.f);
461 for (i = 0; i < nmats; i++) { /* open each input */
462 int restype;
463 if (!open_input(&mop[i]))
464 return(0);
465 restype = mop[i].rmp->dtype;
466 if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0)
467 imp->dtype = restype;
468 else
469 fprintf(stderr, "%s: warning - data type mismatch\n",
470 mop[i].inspec);
471 if (!i) {
472 imp->ncols = mop[0].rmp->ncols;
473 imp->ncomp = mop[0].rmp->ncomp;
474 memcpy(imp->wlpart, mop[0].rmp->wlpart, sizeof(imp->wlpart));
475 } else if ((mop[i].rmp->ncols != imp->ncols) |
476 (mop[i].rmp->ncomp != imp->ncomp) |
477 ((in_nrows > 0) & (mop[i].rmp->nrows > 0) &
478 (mop[i].rmp->nrows != in_nrows))) {
479 fprintf(stderr, "%s: mismatch in size or #components\n",
480 mop[i].inspec);
481 return(0);
482 } /* XXX should check wlpart? */
483 if (in_nrows <= 0)
484 in_nrows = imp->nrows = mop[i].rmp->nrows;
485 } /* set up .cal environment */
486 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
487 esupport &= ~(E_OUTCHAN|E_INCHAN);
488 varset("PI", ':', M_PI);
489 varset("nfiles", ':', nmats);
490 varset("nrows", ':', in_nrows);
491 varset("ncols", ':', in_ncols);
492 varset("ncomp", ':', in_ncomp);
493 varset("R", ':', 1.);
494 varset("G", ':', 2.);
495 varset("B", ':', 3.);
496 funset("wl", 1, ':', l_wavelength);
497 funset("ci", 1, '=', l_chanin);
498 scompile("ri(i)=ci(i,R);gi(i)=ci(i,G);bi(i)=ci(i,B)", NULL, 0);
499 return(1);
500 }
501
502 static void
503 output_headinfo(FILE *fp)
504 {
505 int i;
506
507 for (i = 0; i < nmats; i++) {
508 const char *cp = mop[i].imx.info;
509 fputs(mop[i].inspec, fp);
510 fputs(":\n", fp);
511 if (!cp) continue;
512 while (*cp) {
513 if (*cp == '\n') {
514 cp++; /* avoid inadvertant terminus */
515 continue;
516 }
517 fputc('\t', fp); /* indent this input's info */
518 do
519 putc(*cp, fp);
520 while (*cp++ != '\n');
521 }
522 }
523 }
524
525 static int
526 combine_input(ROPMAT *res, FILE *fout)
527 {
528 int set_r, set_c;
529 RMATRIX *tmp = NULL;
530 int co_set;
531 int i;
532 /* allocate input row buffers */
533 for (i = 0; i < nmats; i++) {
534 mop[i].imx.nrows = 1; /* we'll be doing a row at a time */
535 if (!rmx_prepare(&mop[i].imx))
536 goto memerror;
537 if (mop[i].rmp != &mop[i].imx) {
538 mop[i].rmp->nrows = 1;
539 if (!rmx_prepare(mop[i].rmp))
540 goto memerror;
541 }
542 }
543 /* prep output row buffers */
544 if (mcat || res->preop.clen > 0) {
545 if (!split_input(res)) /* need separate buffer */
546 return(0);
547 if (res->preop.clen > 0)
548 res->rmp->ncomp = res->preop.clen / res->imx.ncomp;
549 res->rmp->nrows = 1;
550 if (!mcat | !mcat_last && !rmx_prepare(res->rmp))
551 goto memerror;
552 }
553 if (mcat && mcat_last &&
554 !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
555 goto memerror;
556 res->imx.nrows = 1;
557 if (!rmx_prepare(&res->imx))
558 goto memerror;
559 /* figure out what the user set */
560 co_set = fundefined("co");
561 if (!co_set)
562 co_set = -vardefined("co");
563 if (!co_set & (in_ncomp == 3) && vardefined("ro") &&
564 vardefined("go") && vardefined("bo")) {
565 scompile("co(p)=select(p,ro,go,bo)", NULL, 0);
566 co_set = 1;
567 }
568 if (co_set) { /* set if user wants, didn't set */
569 set_r = varlookup("r") != NULL && !vardefined("r");
570 set_c = varlookup("c") != NULL && !vardefined("c");
571 } else /* save a little time */
572 set_r = set_c = 0;
573 /* read/process row-by-row */
574 for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) {
575 RMATRIX *mres = NULL;
576 for (i = 0; i < nmats; i++) {
577 if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
578 if (in_nrows <= 0) /* normal end? */
579 goto loop_exit;
580 fprintf(stderr, "%s: read error at row %d\n",
581 mop[i].inspec, cur_row);
582 return(0);
583 }
584 if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop))
585 return(0);
586 }
587 if (set_r) varset("r", '=', cur_row);
588 for (cur_col = 0; cur_col < in_ncols; cur_col++) {
589 if (set_c) varset("c", '=', cur_col);
590 for (cur_chan = 0; cur_chan < in_ncomp; cur_chan++) {
591 const int ndx = cur_col*in_ncomp + cur_chan;
592 eclock++;
593 if (!co_set) { /* just summing elements? */
594 res->imx.mtx[ndx] = 0;
595 for (i = nmats; i--; )
596 res->imx.mtx[ndx] += mop[i].rmp->mtx[ndx];
597 } else if (co_set > 0) {
598 double dchan = cur_chan+1;
599 res->imx.mtx[ndx] = funvalue("co", 1, &dchan);
600 } else
601 res->imx.mtx[ndx] = varvalue("co");
602 }
603 } /* final conversions */
604 if (!mcat) {
605 if (!apply_op(res->rmp, &res->imx, &res->preop))
606 return(0);
607 } else if (mcat_last) {
608 if (!apply_op(tmp, &res->imx, &res->preop))
609 return(0);
610 mres = rmx_multiply(tmp, mcat);
611 if (!mres)
612 goto multerror;
613 if (!rmx_transfer_data(res->rmp, mres, 0))
614 return(0);
615 } else /* mcat && !mcat_last */ {
616 mres = rmx_multiply(&res->imx, mcat);
617 if (!mres)
618 goto multerror;
619 if (!apply_op(res->rmp, mres, &res->preop))
620 return(0);
621 }
622 rmx_free(mres); mres = NULL;
623 if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
624 res->rmp->ncols, res->rmp->dtype, fout))
625 return(0);
626 }
627 loop_exit:
628 #if 0 /* we're about to exit, so who cares? */
629 rmx_free(tmp); /* clean up */
630 rmx_reset(res->rmp);
631 rmx_reset(&res->imx);
632 for (i = 0; i < nmats; i++) {
633 rmx_reset(mop[i].rmp);
634 rmx_reset(&mop[i].imx);
635 if (mop[i].inspec[0] == '!')
636 pclose(mop[i].infp);
637 else if (mop[i].inspec != stdin_name)
638 fclose(mop[i].infp);
639 mop[i].infp = NULL;
640 }
641 #endif
642 return(fflush(fout) != EOF);
643 memerror:
644 fputs("Out of buffer space in combine_input()\n", stderr);
645 return(0);
646 multerror:
647 fputs("Unexpected matrix multiply error in combine_input()\n", stderr);
648 return(0);
649 }
650
651 static int
652 get_factors(double da[], int n, char *av[])
653 {
654 int ac;
655
656 for (ac = 0; ac < n && isflt(av[ac]); ac++)
657 da[ac] = atof(av[ac]);
658 return(ac);
659 }
660
661 static void
662 resize_inparr(int n2alloc)
663 {
664 int i;
665
666 for (i = nmats; i > n2alloc; i--) {
667 rmx_reset(&mop[i].imx);
668 if (mop[i].rmp != &mop[i].imx)
669 rmx_free(mop[i].rmp);
670 }
671 mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
672 if (mop == NULL) {
673 fputs("Out of memory in resize_inparr()\n", stderr);
674 exit(1);
675 }
676 if (n2alloc > nmats)
677 memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT));
678 nall = n2alloc;
679 }
680
681 /* Load one or more matrices and operate on them, sending results to stdout */
682 int
683 main(int argc, char *argv[])
684 {
685
686 int outfmt = DTfromHeader;
687 const char *defCsym = NULL;
688 int echoheader = 1;
689 int stdin_used = 0;
690 const char *mcat_spec = NULL;
691 int n2comp = 0;
692 uby8 comp_ndx[128];
693 int i;
694 /* get starting input array */
695 mop = (ROPMAT *)calloc(nall=2, sizeof(ROPMAT));
696 /* get options and arguments */
697 for (i = 1; i < argc; i++)
698 if (argv[i][0] != '-' || !argv[i][1]) {
699 if (argv[i][0] == '-') {
700 if (stdin_used++) goto stdin_error;
701 mop[nmats].inspec = stdin_name;
702 } else
703 mop[nmats].inspec = argv[i];
704 if (!mop[nmats].preop.csym)
705 mop[nmats].preop.csym = defCsym;
706 if (++nmats >= nall)
707 resize_inparr(nmats + (nmats>>2) + 2);
708 } else {
709 int n = argc-1 - i;
710 switch (argv[i][1]) { /* get option */
711 case 'w':
712 nowarn = !nowarn;
713 break;
714 case 'h':
715 echoheader = !echoheader;
716 break;
717 case 'e':
718 if (!n) goto userr;
719 comp_ndx[n2comp++] = i++;
720 break;
721 case 'f':
722 switch (argv[i][2]) {
723 case '\0':
724 if (!n) goto userr;
725 comp_ndx[n2comp++] = i++;
726 break;
727 case 'd':
728 outfmt = DTdouble;
729 break;
730 case 'f':
731 outfmt = DTfloat;
732 break;
733 case 'a':
734 outfmt = DTascii;
735 break;
736 case 'c':
737 outfmt = DTrgbe;
738 break;
739 default:
740 goto userr;
741 }
742 break;
743 case 's':
744 if (n > MAXCOMP) n = MAXCOMP;
745 i += mop[nmats].preop.nsf =
746 get_factors(mop[nmats].preop.sca,
747 n, argv+i+1);
748 if (mop[nmats].preop.nsf <= 0) {
749 fprintf(stderr, "%s: -s missing arguments\n",
750 argv[0]);
751 goto userr;
752 }
753 break;
754 case 'C':
755 if (!n || isflt(argv[i+1]))
756 goto userr;
757 defCsym = mop[nmats].preop.csym = argv[++i];
758 mop[nmats].preop.clen = 0;
759 mcat_last = 0;
760 break;
761 case 'c':
762 if (n && !isflt(argv[i+1])) {
763 mop[nmats].preop.csym = argv[++i];
764 mop[nmats].preop.clen = 0;
765 break;
766 }
767 if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
768 i += mop[nmats].preop.clen =
769 get_factors(mop[nmats].preop.cmat,
770 n, argv+i+1);
771 if (mop[nmats].preop.clen <= 0) {
772 fprintf(stderr, "%s: -c missing arguments\n",
773 argv[0]);
774 goto userr;
775 }
776 mop[nmats].preop.csym = NULL;
777 mcat_last = 0;
778 break;
779 case 'm':
780 if (!n) goto userr;
781 if (argv[++i][0] == '-' && !argv[i][1]) {
782 if (stdin_used++) goto stdin_error;
783 mcat_spec = stdin_name;
784 } else
785 mcat_spec = argv[i];
786 mcat_last = 1;
787 break;
788 default:
789 fprintf(stderr, "%s: unknown option '%s'\n",
790 argv[0], argv[i]);
791 goto userr;
792 }
793 }
794 if (!nmats) {
795 fprintf(stderr, "%s: need at least one input matrix\n", argv[0]);
796 goto userr;
797 }
798 resize_inparr(nmats+1); /* extra matrix at end for result */
799 mop[nmats].inspec = "trailing_ops";
800 /* load final concatenation matrix */
801 if (mcat_spec && !(mcat = rmx_load(mcat_spec, RMPnone))) {
802 fprintf(stderr, "%s: error loading concatenation matrix: %s\n",
803 argv[0], mcat_spec);
804 return(1);
805 }
806 /* get/check inputs, set constants */
807 if (!initialize(&mop[nmats].imx))
808 return(1);
809
810 for (i = 0; i < n2comp; i++) /* user .cal files and expressions */
811 if (argv[comp_ndx[i]][1] == 'f') {
812 char *fpath = getpath(argv[comp_ndx[i]+1],
813 getrlibpath(), 0);
814 if (fpath == NULL) {
815 fprintf(stderr, "%s: cannot find file '%s'\n",
816 argv[0], argv[comp_ndx[i]+1]);
817 return(1);
818 }
819 fcompile(fpath);
820 } else /* (argv[comp_ndx[i]][1] == 'e') */
821 scompile(argv[comp_ndx[i]+1], NULL, 0);
822
823 /* get trailing color transform */
824 if (!get_component_xfm(&mop[nmats]))
825 return(1);
826 /* adjust output dimensions and #components */
827 if (mcat) {
828 if (mop[nmats].imx.ncols != mcat->nrows) {
829 fprintf(stderr,
830 "%s: number of input columns does not match number of rows in '%s'\n",
831 argv[0], mcat_spec);
832 return(1);
833 }
834 if (mcat->ncomp != (mcat_last ? mop[nmats].rmp->ncomp : mop[nmats].imx.ncomp)) {
835 fprintf(stderr,
836 "%s: number of components does not match those in '%s'\n",
837 argv[0], mcat_spec);
838 return(1);
839 }
840 if (!split_input(&mop[nmats]))
841 return(1);
842 mop[nmats].rmp->ncols = mcat->ncols;
843 }
844 newheader("RADIANCE", stdout); /* write output header */
845 if (echoheader)
846 output_headinfo(stdout);
847 printargs(argc, argv, stdout);
848 fputnow(stdout);
849 mop[nmats].rmp->dtype = rmx_write_header(mop[nmats].rmp, outfmt, stdout);
850 if (!mop[nmats].rmp->dtype) {
851 fprintf(stderr, "%s: unsupported output format\n", argv[0]);
852 return(1);
853 }
854 /* process & write rows */
855 return(combine_input(&mop[nmats], stdout) ? 0 : 1);
856 stdin_error:
857 fprintf(stderr, "%s: %s used for more than one input\n",
858 argv[0], stdin_name);
859 return(1);
860 userr:
861 fprintf(stderr,
862 "Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
863 argv[0]);
864 return(1);
865 }