ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rmtxcomb.c
Revision: 2.3
Committed: Tue Dec 5 21:13:38 2023 UTC (5 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +5 -3 lines
Log Message:
fix(rmtxcomb): Additional bug fixes

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rmtxcomb.c,v 2.2 2023/12/05 20:05:36 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; /* input row count */
49 #define in_ncols (mop[0].rmp->ncols) /* input column count */
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) & (refm.dtype != DTspec)) {
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 } else if (rop->preop.nsf > 0) { /* else use scalar(s)? */
349 if (rop->preop.nsf == 1) {
350 for (i = rop->rmp->ncomp; --i; )
351 rop->preop.sca[i] = rop->preop.sca[0];
352 rop->preop.nsf = rop->rmp->ncomp;
353 } else if (rop->preop.nsf != rop->rmp->ncomp) {
354 fprintf(stderr, "%s: -s must have one or %d factors\n",
355 rop->inspec, rop->rmp->ncomp);
356 return(0);
357 }
358 }
359 return(1);
360 }
361
362 static int
363 apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro)
364 {
365 /*
366 if (!dst | !src | !ro || (dst->nrows != src->nrows) |
367 (dst->ncols != src->ncols))
368 return(0);
369 */
370 if (ro->clen > 0) {
371 RMATRIX *res = rmx_transform(src, dst->ncomp, ro->cmat);
372 if (!res) {
373 fputs("Error in call to rmx_transform()\n", stderr);
374 return(0);
375 }
376 if (dst->mtx) free(dst->mtx);
377 dst->mtx = res->mtx; res->mtx = NULL;
378 rmx_free(res);
379 } else if (dst != src)
380 memcpy(dst->mtx, src->mtx,
381 sizeof(double)*dst->ncomp*dst->ncols*dst->nrows);
382 if (ro->nsf == dst->ncomp)
383 rmx_scale(dst, ro->sca);
384 return(1);
385 }
386
387 static int
388 open_input(ROPMAT *rop)
389 {
390 int outtype;
391
392 if (!rop || !rop->inspec || !rop->inspec[0])
393 return(0);
394 if (rop->inspec == stdin_name)
395 rop->infp = stdin;
396 else if (rop->inspec[0] == '!')
397 rop->infp = popen(rop->inspec+1, "r");
398 else
399 rop->infp = fopen(rop->inspec, "rb");
400
401 if (!rmx_load_header(&rop->imx, rop->infp)) {
402 fprintf(stderr, "Bad header from: %s\n", rop->inspec);
403 return(0);
404 }
405 return(get_component_xfm(rop));
406 }
407
408 /* Return nominal wavelength associated with input component (return nm) */
409 static double
410 l_wavelength(char *nam)
411 {
412 double comp = argument(1);
413
414 if ((comp < -.5) | (comp >= in_ncomp+.5)) {
415 errno = EDOM;
416 return(.0);
417 }
418 if (comp < .5) /* asking for #components? */
419 return(in_ncomp);
420
421 if (in_ncomp == 3) { /* special case for RGB */
422 const int w0 = (int)(comp - .5);
423 return(mop[0].rmp->wlpart[w0] +
424 (comp-.5)*(mop[0].rmp->wlpart[w0+1] -
425 mop[0].rmp->wlpart[w0]));
426 }
427 return(mop[0].rmp->wlpart[0] + /* general case, even div. */
428 (comp-.5)/(double)in_ncomp *
429 (mop[0].rmp->wlpart[3] - mop[0].rmp->wlpart[0]));
430 }
431
432 /* Return ith input with optional channel selector */
433 static double
434 l_chanin(char *nam)
435 {
436 double inp = argument(1);
437 int mi, chan;
438
439 if ((mi = (int)(inp-.5)) < 0 || mi >= nmats) {
440 errno = EDOM;
441 return(.0);
442 }
443 if (inp < .5) /* asking for #inputs? */
444 return(nmats);
445
446 if (nargum() >= 2) {
447 double cval = argument(2);
448 if (cval < .5 || (chan = (int)(cval-.5)) >= in_ncomp) {
449 errno = EDOM;
450 return(.0);
451 }
452 } else
453 chan = cur_chan;
454
455 return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]);
456 }
457
458 static int
459 initialize(RMATRIX *imp)
460 {
461 int i;
462 /* XXX struct is zeroed coming in */
463 setcolor(imp->cexp, 1.f, 1.f, 1.f);
464 for (i = 0; i < nmats; i++) { /* open each input */
465 int restype;
466 if (!open_input(&mop[i]))
467 return(0);
468 restype = mop[i].rmp->dtype;
469 if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0)
470 imp->dtype = restype;
471 else
472 fprintf(stderr, "%s: warning - data type mismatch\n",
473 mop[i].inspec);
474 if (!i) {
475 imp->nrows = in_nrows = mop[0].rmp->nrows;
476 imp->ncols = mop[0].rmp->ncols;
477 imp->ncomp = mop[0].rmp->ncomp;
478 memcpy(imp->wlpart, mop[0].rmp->wlpart, sizeof(imp->wlpart));
479 } else if ((mop[i].rmp->nrows != imp->nrows) |
480 (mop[i].rmp->ncols != imp->ncols) |
481 (mop[i].rmp->ncomp != imp->ncomp)) {
482 fprintf(stderr, "%s: mismatch in size or #components\n",
483 mop[i].inspec);
484 return(0);
485 } /* XXX should check wlpart? */
486 } /* set up .cal environment */
487 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
488 esupport &= ~(E_OUTCHAN|E_INCHAN);
489 varset("PI", ':', M_PI);
490 varset("nfiles", ':', nmats);
491 varset("nrows", ':', in_nrows);
492 varset("ncols", ':', in_ncols);
493 varset("ncomp", ':', in_ncomp);
494 varset("R", ':', 1.);
495 varset("G", ':', 2.);
496 varset("B", ':', 3.);
497 funset("wl", 1, ':', l_wavelength);
498 funset("ci", 1, '=', l_chanin);
499 scompile("ri(i)=ci(i,R);gi(i)=ci(i,G);bi(i)=ci(i,B)", NULL, 0);
500 return(1);
501 }
502
503 static void
504 output_headinfo(FILE *fp)
505 {
506 int i;
507
508 for (i = 0; i < nmats; i++) {
509 const char *cp = mop[i].imx.info;
510 fputs(mop[i].inspec, fp);
511 fputs(":\n", fp);
512 if (!cp) continue;
513 while (*cp) {
514 if (*cp == '\n') {
515 cp++; /* avoid inadvertant terminus */
516 continue;
517 }
518 fputc('\t', fp); /* indent this input's info */
519 do
520 putc(*cp, fp);
521 while (*cp++ != '\n');
522 }
523 }
524 }
525
526 static int
527 combine_input(ROPMAT *res, FILE *fout)
528 {
529 int set_r, set_c;
530 RMATRIX *tmp = NULL;
531 int co_set;
532 int i;
533 /* allocate input row buffers */
534 for (i = 0; i < nmats; i++) {
535 mop[i].imx.nrows = 1; /* we'll be doing a row at a time */
536 if (!rmx_prepare(&mop[i].imx))
537 goto memerror;
538 if (mop[i].rmp != &mop[i].imx) {
539 mop[i].rmp->nrows = 1;
540 if (!rmx_prepare(mop[i].rmp))
541 goto memerror;
542 }
543 }
544 /* prep output row buffers */
545 if (mcat || res->preop.clen > 0) {
546 if (!split_input(res)) /* need separate buffer */
547 return(0);
548 if (res->preop.clen > 0)
549 res->rmp->ncomp = res->preop.clen / res->imx.ncomp;
550 res->rmp->nrows = 1;
551 if (!mcat | !mcat_last && !rmx_prepare(res->rmp))
552 goto memerror;
553 }
554 if (mcat && mcat_last &&
555 !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp)))
556 goto memerror;
557 res->imx.nrows = 1;
558 if (!rmx_prepare(&res->imx))
559 goto memerror;
560 /* figure out what the user set */
561 co_set = fundefined("co");
562 if (!co_set)
563 co_set = -vardefined("co");
564 if (!co_set & (in_ncomp == 3) && vardefined("ro") &&
565 vardefined("go") && vardefined("bo")) {
566 scompile("co(p)=select(p,ro,go,bo)", NULL, 0);
567 co_set = 1;
568 }
569 if (co_set) { /* don't override user */
570 set_r = !vardefined("r");
571 set_c = !vardefined("c");
572 } else /* save a little time */
573 set_r = set_c = 0;
574 /* read/process row-by-row */
575 for (cur_row = 0; cur_row < in_nrows; cur_row++) {
576 RMATRIX *mres = NULL;
577 for (i = 0; i < nmats; i++) {
578 if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) {
579 fprintf(stderr, "%s: read error at row %d\n",
580 mop[i].inspec, cur_row);
581 return(0);
582 }
583 if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop))
584 return(0);
585 }
586 if (set_r) varset("r", '=', cur_row);
587 for (cur_col = 0; cur_col < in_ncols; cur_col++) {
588 if (set_c) varset("c", '=', cur_col);
589 for (cur_chan = 0; cur_chan < in_ncomp; cur_chan++) {
590 const int ndx = cur_col*in_ncomp + cur_chan;
591 eclock++;
592 if (!co_set) { /* just summing elements? */
593 res->imx.mtx[ndx] = 0;
594 for (i = nmats; i--; )
595 res->imx.mtx[ndx] += mop[i].rmp->mtx[ndx];
596 } else if (co_set > 0) {
597 double dchan = cur_chan+1;
598 res->imx.mtx[ndx] = funvalue("co", 1, &dchan);
599 } else
600 res->imx.mtx[ndx] = varvalue("co");
601 }
602 } /* final conversions */
603 if (!mcat) {
604 if (!apply_op(res->rmp, &res->imx, &res->preop))
605 return(0);
606 } else if (mcat_last) {
607 if (!apply_op(tmp, &res->imx, &res->preop))
608 return(0);
609 mres = rmx_multiply(tmp, mcat);
610 if (!mres)
611 goto multerror;
612 if (res->rmp->mtx) free(res->rmp->mtx);
613 res->rmp->mtx = mres->mtx; mres->mtx = NULL;
614 } else /* mcat && !mcat_last */ {
615 mres = rmx_multiply(&res->imx, mcat);
616 if (!mres)
617 goto multerror;
618 if (!apply_op(res->rmp, mres, &res->preop))
619 return(0);
620 }
621 rmx_free(mres); mres = NULL;
622 if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp,
623 res->rmp->ncols, res->rmp->dtype, fout))
624 return(0);
625 }
626 #if 0 /* we're about to exit, so who cares? */
627 rmx_free(tmp); /* clean up */
628 rmx_reset(res->rmp);
629 rmx_reset(&res->imx);
630 for (i = 0; i < nmats; i++) {
631 rmx_reset(mop[i].rmp);
632 rmx_reset(&mop[i].imx);
633 if (mop[i].inspec[0] == '!')
634 pclose(mop[i].infp);
635 else if (mop[i].inspec != stdin_name)
636 fclose(mop[i].infp);
637 mop[i].infp = NULL;
638 }
639 #endif
640 return(fflush(fout) != EOF);
641 memerror:
642 fputs("Out of buffer space in combine_input()\n", stderr);
643 return(0);
644 multerror:
645 fputs("Unexpected matrix multiply error in combine_input()\n", stderr);
646 return(0);
647 }
648
649 static int
650 get_factors(double da[], int n, char *av[])
651 {
652 int ac;
653
654 for (ac = 0; ac < n && isflt(av[ac]); ac++)
655 da[ac] = atof(av[ac]);
656 return(ac);
657 }
658
659 static void
660 resize_inparr(int n2alloc)
661 {
662 int i;
663
664 for (i = nmats; i > n2alloc; i--) {
665 rmx_reset(&mop[i].imx);
666 if (mop[i].rmp != &mop[i].imx)
667 rmx_free(mop[i].rmp);
668 }
669 mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT));
670 if (mop == NULL) {
671 fputs("Out of memory in resize_inparr()\n", stderr);
672 exit(1);
673 }
674 if (n2alloc > nmats)
675 memset(mop+nmats, 0, (n2alloc-nmats)*sizeof(ROPMAT));
676 nall = n2alloc;
677 }
678
679 /* Load one or more matrices and operate on them, sending results to stdout */
680 int
681 main(int argc, char *argv[])
682 {
683
684 int outfmt = DTfromHeader;
685 const char *defCsym = NULL;
686 int echoheader = 1;
687 int stdin_used = 0;
688 const char *mcat_spec = NULL;
689 int n2comp = 0;
690 uby8 comp_ndx[128];
691 int i;
692 /* get starting input array */
693 mop = (ROPMAT *)calloc(nall=2, sizeof(ROPMAT));
694 /* get options and arguments */
695 for (i = 1; i < argc; i++)
696 if (argv[i][0] != '-' || !argv[i][1]) {
697 if (argv[i][0] == '-') {
698 if (stdin_used++) goto stdin_error;
699 mop[nmats].inspec = stdin_name;
700 } else
701 mop[nmats].inspec = argv[i];
702 if (!mop[nmats].preop.csym)
703 mop[nmats].preop.csym = defCsym;
704 if (++nmats >= nall)
705 resize_inparr(nmats + (nmats>>2) + 2);
706 } else {
707 int n = argc-1 - i;
708 switch (argv[i][1]) { /* get option */
709 case 'w':
710 nowarn = !nowarn;
711 break;
712 case 'h':
713 echoheader = !echoheader;
714 break;
715 case 'e':
716 if (!n) goto userr;
717 comp_ndx[n2comp++] = i++;
718 break;
719 case 'f':
720 switch (argv[i][2]) {
721 case '\0':
722 if (!n) goto userr;
723 comp_ndx[n2comp++] = i++;
724 break;
725 case 'd':
726 outfmt = DTdouble;
727 break;
728 case 'f':
729 outfmt = DTfloat;
730 break;
731 case 'a':
732 outfmt = DTascii;
733 break;
734 case 'c':
735 outfmt = DTrgbe;
736 break;
737 default:
738 goto userr;
739 }
740 break;
741 case 's':
742 if (n > MAXCOMP) n = MAXCOMP;
743 i += mop[nmats].preop.nsf =
744 get_factors(mop[nmats].preop.sca,
745 n, argv+i+1);
746 if (mop[nmats].preop.nsf <= 0) {
747 fprintf(stderr, "%s: -s missing arguments\n",
748 argv[0]);
749 goto userr;
750 }
751 break;
752 case 'C':
753 if (!n || isflt(argv[i+1]))
754 goto userr;
755 defCsym = mop[nmats].preop.csym = argv[++i];
756 mop[nmats].preop.clen = 0;
757 mcat_last = 0;
758 break;
759 case 'c':
760 if (n && !isflt(argv[i+1])) {
761 mop[nmats].preop.csym = argv[++i];
762 mop[nmats].preop.clen = 0;
763 break;
764 }
765 if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP;
766 i += mop[nmats].preop.clen =
767 get_factors(mop[nmats].preop.cmat,
768 n, argv+i+1);
769 if (mop[nmats].preop.clen <= 0) {
770 fprintf(stderr, "%s: -c missing arguments\n",
771 argv[0]);
772 goto userr;
773 }
774 mop[nmats].preop.csym = NULL;
775 mcat_last = 0;
776 break;
777 case 'm':
778 if (!n) goto userr;
779 if (argv[++i][0] == '-' && !argv[i][1]) {
780 if (stdin_used++) goto stdin_error;
781 mcat_spec = stdin_name;
782 } else
783 mcat_spec = argv[i];
784 mcat_last = 1;
785 break;
786 default:
787 fprintf(stderr, "%s: unknown option '%s'\n",
788 argv[0], argv[i]);
789 goto userr;
790 }
791 }
792 if (!nmats) {
793 fprintf(stderr, "%s: need at least one input matrix\n", argv[0]);
794 goto userr;
795 }
796 resize_inparr(nmats+1); /* extra matrix at end for result */
797 mop[nmats].inspec = "trailing_ops";
798 /* load final concatenation matrix */
799 if (mcat_spec && !(mcat = rmx_load(mcat_spec, RMPnone))) {
800 fprintf(stderr, "%s: error loading concatenation matrix: %s\n",
801 argv[0], mcat_spec);
802 return(1);
803 }
804 /* get/check inputs, set constants */
805 if (!initialize(&mop[nmats].imx))
806 return(1);
807
808 for (i = 0; i < n2comp; i++) /* user .cal files and expressions */
809 if (argv[comp_ndx[i]][1] == 'f') {
810 char *fpath = getpath(argv[comp_ndx[i]+1],
811 getrlibpath(), 0);
812 if (fpath == NULL) {
813 fprintf(stderr, "%s: cannot find file '%s'\n",
814 argv[0], argv[comp_ndx[i]+1]);
815 return(1);
816 }
817 fcompile(fpath);
818 } else /* (argv[comp_ndx[i]][1] == 'e') */
819 scompile(argv[comp_ndx[i]+1], NULL, 0);
820
821 /* get trailing color transform */
822 if (!get_component_xfm(&mop[nmats]))
823 return(1);
824 /* adjust output dimensions and #components */
825 if (mcat) {
826 if (mop[nmats].imx.ncols != mcat->nrows) {
827 fprintf(stderr,
828 "%s: number of input columns does not match number of rows in '%s'\n",
829 argv[0], mcat_spec);
830 return(1);
831 }
832 if (mcat->ncomp != (mcat_last ? mop[nmats].rmp->ncomp : mop[nmats].imx.ncomp)) {
833 fprintf(stderr,
834 "%s: number of components does not match those in '%s'\n",
835 argv[0], mcat_spec);
836 return(1);
837 }
838 if (!split_input(&mop[nmats]))
839 return(1);
840 mop[nmats].rmp->ncols = mcat->ncols;
841 }
842 newheader("RADIANCE", stdout); /* write output header */
843 if (echoheader)
844 output_headinfo(stdout);
845 printargs(argc, argv, stdout);
846 fputnow(stdout);
847 mop[nmats].rmp->dtype = rmx_write_header(mop[nmats].rmp, outfmt, stdout);
848 if (!mop[nmats].rmp->dtype) {
849 fprintf(stderr, "%s: unsupported output format\n", argv[0]);
850 return(1);
851 }
852 /* process & write rows */
853 return(combine_input(&mop[nmats], stdout) ? 0 : 1);
854 stdin_error:
855 fprintf(stderr, "%s: %s used for more than one input\n",
856 argv[0], stdin_name);
857 return(1);
858 userr:
859 fprintf(stderr,
860 "Usage: %s [-h][-f{adfc}][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n",
861 argv[0]);
862 return(1);
863 }