ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcomb.c
Revision: 2.9
Committed: Sun May 19 15:32:24 2024 UTC (20 hours, 38 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.8: +2 -3 lines
Log Message:
feat: Added facility for app-managed matrix memory

File Contents

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