| 1 |
greg |
2.1 |
#ifndef lint |
| 2 |
greg |
2.34 |
static const char RCSid[] = "$Id: rcomb.c,v 2.33 2025/08/02 20:37:52 greg Exp $"; |
| 3 |
greg |
2.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* General component matrix combiner, operating on a row at a time. |
| 6 |
greg |
2.18 |
* |
| 7 |
|
|
* Multi-processing mode under Unix creates children that each work |
| 8 |
greg |
2.19 |
* on one input row at a time, fed by the original process. Final conversion |
| 9 |
greg |
2.20 |
* and output to stdout is sorted by last child while its siblings send it |
| 10 |
greg |
2.19 |
* their record calculations. |
| 11 |
greg |
2.1 |
*/ |
| 12 |
|
|
|
| 13 |
|
|
#include <math.h> |
| 14 |
|
|
#include "platform.h" |
| 15 |
greg |
2.10 |
#include "rtprocess.h" |
| 16 |
greg |
2.1 |
#include "rtio.h" |
| 17 |
|
|
#include "rmatrix.h" |
| 18 |
|
|
#include "calcomp.h" |
| 19 |
|
|
|
| 20 |
|
|
#ifndef M_PI |
| 21 |
|
|
#define M_PI 3.14159265358979323846 |
| 22 |
|
|
#endif |
| 23 |
|
|
|
| 24 |
|
|
/* Unary matrix operation(s) */ |
| 25 |
|
|
typedef struct { |
| 26 |
|
|
double cmat[MAXCOMP*MAXCOMP]; /* component transformation */ |
| 27 |
|
|
double sca[MAXCOMP]; /* scalar coefficients */ |
| 28 |
|
|
const char *csym; /* symbolic coefficients */ |
| 29 |
|
|
short clen; /* number of coefficients */ |
| 30 |
|
|
short nsf; /* number of scalars */ |
| 31 |
|
|
} RUNARYOP; |
| 32 |
|
|
|
| 33 |
|
|
/* Input matrix */ |
| 34 |
|
|
typedef struct { |
| 35 |
|
|
const char *inspec; /* input specification */ |
| 36 |
|
|
RUNARYOP preop; /* transform operation */ |
| 37 |
|
|
RMATRIX imx; /* input matrix header info */ |
| 38 |
|
|
RMATRIX *rmp; /* active single-row matrix */ |
| 39 |
|
|
FILE *infp; /* open input stream */ |
| 40 |
|
|
} ROPMAT; |
| 41 |
|
|
|
| 42 |
|
|
ROPMAT *mop = NULL; /* allocated input array */ |
| 43 |
|
|
int nall = 0; /* number allocated */ |
| 44 |
|
|
int nmats = 0; /* number of actual inputs */ |
| 45 |
|
|
|
| 46 |
|
|
RMATRIX *mcat = NULL; /* final concatenation */ |
| 47 |
|
|
int mcat_last = 0; /* goes after trailing ops? */ |
| 48 |
|
|
|
| 49 |
greg |
2.2 |
int in_nrows; /* number of input rows (or 0) */ |
| 50 |
|
|
#define in_ncols (mop[0].rmp->ncols) /* number of input columns */ |
| 51 |
greg |
2.1 |
#define in_ncomp (mop[0].rmp->ncomp) /* input #components */ |
| 52 |
|
|
|
| 53 |
|
|
extern int nowarn; /* turn off warnings? */ |
| 54 |
|
|
|
| 55 |
|
|
int cur_row; /* current input/output row */ |
| 56 |
|
|
int cur_col; /* current input/output column */ |
| 57 |
|
|
int cur_chan; /* if we're looping channels */ |
| 58 |
|
|
|
| 59 |
greg |
2.10 |
SUBPROC *cproc = NULL; /* child process array */ |
| 60 |
|
|
int nchildren = 0; /* # of child processes */ |
| 61 |
|
|
int inchild = -1; /* our child ID (-1: parent) */ |
| 62 |
|
|
|
| 63 |
greg |
2.22 |
extern int checksymbolic(ROPMAT *rop); |
| 64 |
greg |
2.1 |
|
| 65 |
greg |
2.34 |
/* Split input matrices to allow for certain operations */ |
| 66 |
greg |
2.22 |
int |
| 67 |
greg |
2.1 |
split_input(ROPMAT *rop) |
| 68 |
|
|
{ |
| 69 |
|
|
if (rop->rmp == &rop->imx && !(rop->rmp = rmx_copy(&rop->imx))) { |
| 70 |
|
|
fputs("Out of memory in split_input()\n", stderr); |
| 71 |
|
|
return(0); |
| 72 |
|
|
} |
| 73 |
|
|
rmx_reset(rop->rmp); |
| 74 |
|
|
return(1); |
| 75 |
|
|
} |
| 76 |
|
|
|
| 77 |
|
|
/* Check/set transform based on a reference input file */ |
| 78 |
greg |
2.22 |
int |
| 79 |
greg |
2.1 |
checkreffile(ROPMAT *rop) |
| 80 |
|
|
{ |
| 81 |
|
|
static const char *curRF = NULL; |
| 82 |
|
|
static RMATRIX refm; |
| 83 |
|
|
const int nc = rop->imx.ncomp; |
| 84 |
|
|
int i; |
| 85 |
|
|
|
| 86 |
|
|
if (!curRF || strcmp(rop->preop.csym, curRF)) { |
| 87 |
|
|
FILE *fp = fopen(rop->preop.csym, "rb"); |
| 88 |
|
|
if (!rmx_load_header(&refm, fp)) { |
| 89 |
|
|
fprintf(stderr, "%s: cannot read info header\n", |
| 90 |
|
|
rop->preop.csym); |
| 91 |
|
|
curRF = NULL; |
| 92 |
|
|
if (fp) fclose(fp); |
| 93 |
|
|
return(0); |
| 94 |
|
|
} |
| 95 |
|
|
fclose(fp); |
| 96 |
|
|
curRF = rop->preop.csym; |
| 97 |
|
|
} |
| 98 |
|
|
if (refm.ncomp == 3) { |
| 99 |
|
|
rop->preop.csym = (refm.dtype == DTxyze) ? "XYZ" : "RGB"; |
| 100 |
|
|
return(checksymbolic(rop)); |
| 101 |
|
|
} |
| 102 |
|
|
if (refm.ncomp == 2) { |
| 103 |
|
|
fprintf(stderr, "%s: cannot convert to 2 components\n", |
| 104 |
|
|
curRF); |
| 105 |
|
|
return(0); |
| 106 |
|
|
} |
| 107 |
|
|
if (refm.ncomp == 1) { |
| 108 |
|
|
rop->preop.csym = "Y"; /* XXX big assumption */ |
| 109 |
|
|
return(checksymbolic(rop)); |
| 110 |
|
|
} |
| 111 |
|
|
if (refm.ncomp == nc && |
| 112 |
|
|
!memcmp(refm.wlpart, rop->imx.wlpart, sizeof(refm.wlpart))) |
| 113 |
|
|
return(1); /* nothing to do */ |
| 114 |
|
|
|
| 115 |
|
|
if ((nc <= 3) | (nc > MAXCSAMP) | (refm.ncomp > MAXCSAMP)) { |
| 116 |
|
|
fprintf(stderr, "%s: cannot resample from %d to %d components\n", |
| 117 |
|
|
curRF, nc, refm.ncomp); |
| 118 |
|
|
return(0); |
| 119 |
|
|
} |
| 120 |
|
|
if (!split_input(rop)) /* get our own struct */ |
| 121 |
|
|
return(0); |
| 122 |
|
|
rop->preop.clen = refm.ncomp * nc; /* compute spec to ref */ |
| 123 |
|
|
|
| 124 |
|
|
for (i = 0; i < nc; i++) { |
| 125 |
|
|
SCOLOR scstim, scresp; |
| 126 |
|
|
int j; |
| 127 |
|
|
memset(scstim, 0, sizeof(COLORV)*nc); |
| 128 |
|
|
scstim[i] = 1.f; |
| 129 |
|
|
convertscolor(scresp, refm.ncomp, refm.wlpart[0], refm.wlpart[3], |
| 130 |
|
|
scstim, nc, rop->imx.wlpart[0], rop->imx.wlpart[3]); |
| 131 |
|
|
for (j = refm.ncomp; j-- > 0; ) |
| 132 |
|
|
rop->preop.cmat[j*nc + i] = scresp[j]; |
| 133 |
|
|
} |
| 134 |
|
|
/* remember new spectral params */ |
| 135 |
|
|
memcpy(rop->rmp->wlpart, refm.wlpart, sizeof(rop->rmp->wlpart)); |
| 136 |
|
|
rop->rmp->ncomp = refm.ncomp; |
| 137 |
|
|
return(1); |
| 138 |
|
|
} |
| 139 |
|
|
|
| 140 |
|
|
/* Compute conversion row from spectrum to one channel of RGB */ |
| 141 |
greg |
2.22 |
void |
| 142 |
greg |
2.1 |
rgbrow(ROPMAT *rop, int r, int p) |
| 143 |
|
|
{ |
| 144 |
|
|
const int nc = rop->imx.ncomp; |
| 145 |
|
|
const float * wlp = rop->imx.wlpart; |
| 146 |
|
|
int i; |
| 147 |
|
|
|
| 148 |
|
|
for (i = nc; i--; ) { |
| 149 |
|
|
int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc; |
| 150 |
|
|
int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc; |
| 151 |
|
|
COLOR crgb; |
| 152 |
|
|
spec_rgb(crgb, nmStart, nmEnd); |
| 153 |
|
|
rop->preop.cmat[r*nc+i] = crgb[p]; |
| 154 |
|
|
} |
| 155 |
|
|
} |
| 156 |
|
|
|
| 157 |
|
|
/* Compute conversion row from spectrum to one channel of XYZ */ |
| 158 |
greg |
2.22 |
void |
| 159 |
greg |
2.1 |
xyzrow(ROPMAT *rop, int r, int p) |
| 160 |
|
|
{ |
| 161 |
|
|
const int nc = rop->imx.ncomp; |
| 162 |
|
|
const float * wlp = rop->imx.wlpart; |
| 163 |
|
|
int i; |
| 164 |
|
|
|
| 165 |
|
|
for (i = nc; i--; ) { |
| 166 |
|
|
int nmEnd = wlp[0] + (wlp[3] - wlp[0])*i/nc; |
| 167 |
|
|
int nmStart = wlp[0] + (wlp[3] - wlp[0])*(i+1)/nc; |
| 168 |
|
|
COLOR cxyz; |
| 169 |
|
|
spec_cie(cxyz, nmStart, nmEnd); |
| 170 |
|
|
rop->preop.cmat[r*nc+i] = cxyz[p]; |
| 171 |
|
|
} |
| 172 |
|
|
} |
| 173 |
|
|
|
| 174 |
|
|
/* Use the spectral sensitivity function to compute matrix coefficients */ |
| 175 |
greg |
2.22 |
void |
| 176 |
greg |
2.25 |
sensrow(ROPMAT *rop, int r, double (*sf)(const SCOLOR sc, int ncs, const float wlpt[4])) |
| 177 |
greg |
2.1 |
{ |
| 178 |
|
|
const int nc = rop->imx.ncomp; |
| 179 |
|
|
int i; |
| 180 |
|
|
|
| 181 |
|
|
for (i = nc; i--; ) { |
| 182 |
|
|
SCOLOR sclr; |
| 183 |
|
|
memset(sclr, 0, sizeof(COLORV)*nc); |
| 184 |
|
|
sclr[i] = 1.f; |
| 185 |
|
|
rop->preop.cmat[r*nc+i] = (*sf)(sclr, nc, rop->imx.wlpart); |
| 186 |
|
|
} |
| 187 |
|
|
} |
| 188 |
|
|
|
| 189 |
|
|
/* Check/set symbolic transform */ |
| 190 |
greg |
2.22 |
int |
| 191 |
greg |
2.1 |
checksymbolic(ROPMAT *rop) |
| 192 |
|
|
{ |
| 193 |
|
|
const int nc = rop->imx.ncomp; |
| 194 |
|
|
const int dt = rop->imx.dtype; |
| 195 |
greg |
2.3 |
double cf = 1; |
| 196 |
greg |
2.1 |
int i, j; |
| 197 |
|
|
/* check suffix => reference file */ |
| 198 |
greg |
2.33 |
if (strchr(rop->preop.csym, '.') != NULL) |
| 199 |
greg |
2.1 |
return(checkreffile(rop)); |
| 200 |
|
|
|
| 201 |
|
|
if (nc < 3) { |
| 202 |
|
|
fprintf(stderr, "%s: -c '%s' requires at least 3 components\n", |
| 203 |
|
|
rop->inspec, rop->preop.csym); |
| 204 |
|
|
return(0); |
| 205 |
|
|
} |
| 206 |
|
|
rop->preop.clen = strlen(rop->preop.csym) * nc; |
| 207 |
|
|
if (rop->preop.clen > MAXCOMP*MAXCOMP) { |
| 208 |
|
|
fprintf(stderr, "%s: -c '%s' results in too many components\n", |
| 209 |
|
|
rop->inspec, rop->preop.csym); |
| 210 |
|
|
return(0); |
| 211 |
|
|
} |
| 212 |
|
|
for (j = 0; rop->preop.csym[j]; j++) { |
| 213 |
|
|
int comp = 0; |
| 214 |
|
|
switch (rop->preop.csym[j]) { |
| 215 |
|
|
case 'B': |
| 216 |
greg |
2.3 |
case 'b': |
| 217 |
greg |
2.1 |
++comp; |
| 218 |
|
|
/* fall through */ |
| 219 |
|
|
case 'G': |
| 220 |
greg |
2.3 |
case 'g': |
| 221 |
greg |
2.1 |
++comp; |
| 222 |
|
|
/* fall through */ |
| 223 |
|
|
case 'R': |
| 224 |
greg |
2.3 |
case 'r': |
| 225 |
|
|
if (rop->preop.csym[j] <= 'Z') |
| 226 |
|
|
cf = 1./WHTEFFICACY; |
| 227 |
greg |
2.1 |
if (dt == DTxyze) { |
| 228 |
|
|
for (i = 3; i--; ) |
| 229 |
greg |
2.3 |
rop->preop.cmat[j*nc+i] = cf*xyz2rgbmat[comp][i]; |
| 230 |
greg |
2.1 |
} else if (nc == 3) |
| 231 |
|
|
rop->preop.cmat[j*nc+comp] = 1.; |
| 232 |
|
|
else |
| 233 |
|
|
rgbrow(rop, j, comp); |
| 234 |
|
|
break; |
| 235 |
|
|
case 'Z': |
| 236 |
greg |
2.3 |
case 'z': |
| 237 |
greg |
2.1 |
++comp; |
| 238 |
|
|
/* fall through */ |
| 239 |
|
|
case 'Y': |
| 240 |
greg |
2.3 |
case 'y': |
| 241 |
greg |
2.1 |
++comp; |
| 242 |
|
|
/* fall through */ |
| 243 |
|
|
case 'X': |
| 244 |
greg |
2.3 |
case 'x': |
| 245 |
|
|
if ((rop->preop.csym[j] <= 'Z') & (dt != DTxyze)) |
| 246 |
|
|
cf = WHTEFFICACY; |
| 247 |
greg |
2.1 |
if (dt == DTxyze) { |
| 248 |
|
|
rop->preop.cmat[j*nc+comp] = 1.; |
| 249 |
|
|
} else if (nc == 3) { |
| 250 |
|
|
for (i = 3; i--; ) |
| 251 |
|
|
rop->preop.cmat[j*nc+i] = |
| 252 |
|
|
rgb2xyzmat[comp][i]; |
| 253 |
|
|
} else if (comp == CIEY) |
| 254 |
|
|
sensrow(rop, j, scolor2photopic); |
| 255 |
|
|
else |
| 256 |
|
|
xyzrow(rop, j, comp); |
| 257 |
|
|
|
| 258 |
greg |
2.3 |
for (i = nc*(cf != 1); i--; ) |
| 259 |
|
|
rop->preop.cmat[j*nc+i] *= cf; |
| 260 |
greg |
2.1 |
break; |
| 261 |
|
|
case 'S': /* scotopic (il)luminance */ |
| 262 |
greg |
2.3 |
cf = WHTSCOTOPIC; |
| 263 |
|
|
/* fall through */ |
| 264 |
|
|
case 's': |
| 265 |
greg |
2.1 |
sensrow(rop, j, scolor2scotopic); |
| 266 |
greg |
2.3 |
for (i = nc*(cf != 1); i--; ) |
| 267 |
|
|
rop->preop.cmat[j*nc+i] *= cf; |
| 268 |
greg |
2.1 |
break; |
| 269 |
|
|
case 'M': /* melanopic (il)luminance */ |
| 270 |
greg |
2.3 |
cf = WHTMELANOPIC; |
| 271 |
|
|
/* fall through */ |
| 272 |
|
|
case 'm': |
| 273 |
greg |
2.1 |
sensrow(rop, j, scolor2melanopic); |
| 274 |
greg |
2.3 |
for (i = nc*(cf != 1); i--; ) |
| 275 |
|
|
rop->preop.cmat[j*nc+i] *= cf; |
| 276 |
greg |
2.1 |
break; |
| 277 |
|
|
case 'A': /* average component */ |
| 278 |
greg |
2.3 |
case 'a': |
| 279 |
greg |
2.1 |
for (i = nc; i--; ) |
| 280 |
|
|
rop->preop.cmat[j*nc+i] = 1./(double)nc; |
| 281 |
|
|
break; |
| 282 |
|
|
default: |
| 283 |
|
|
fprintf(stderr, "%s: -c '%c' unsupported\n", |
| 284 |
|
|
rop->inspec, rop->preop.csym[j]); |
| 285 |
|
|
return(0); |
| 286 |
|
|
} |
| 287 |
|
|
} |
| 288 |
|
|
if (!split_input(rop)) /* get our own struct */ |
| 289 |
|
|
return(0); |
| 290 |
|
|
memcpy(rop->rmp->wlpart, WLPART, sizeof(rop->rmp->wlpart)); |
| 291 |
|
|
rop->rmp->ncomp = rop->preop.clen / nc; |
| 292 |
|
|
/* decide on output type */ |
| 293 |
greg |
2.4 |
if (!strcasecmp(rop->preop.csym, "XYZ")) { |
| 294 |
greg |
2.1 |
if (dt <= DTspec) |
| 295 |
|
|
rop->rmp->dtype = DTxyze; |
| 296 |
greg |
2.4 |
} else if (!strcasecmp(rop->preop.csym, "RGB")) { |
| 297 |
greg |
2.1 |
if (dt <= DTspec) |
| 298 |
|
|
rop->rmp->dtype = DTrgbe; |
| 299 |
|
|
} else if (rop->rmp->dtype == DTspec) |
| 300 |
|
|
rop->rmp->dtype = DTfloat; |
| 301 |
|
|
return(1); |
| 302 |
|
|
} |
| 303 |
|
|
|
| 304 |
greg |
2.34 |
/* Set up color transform for matrix */ |
| 305 |
greg |
2.22 |
int |
| 306 |
greg |
2.1 |
get_component_xfm(ROPMAT *rop) |
| 307 |
|
|
{ |
| 308 |
|
|
int i, j; |
| 309 |
|
|
|
| 310 |
|
|
if (rop->rmp != &rop->imx) { /* reset destination matrix */ |
| 311 |
|
|
rmx_free(rop->rmp); |
| 312 |
|
|
rop->rmp = &rop->imx; |
| 313 |
|
|
} |
| 314 |
|
|
if (rop->preop.csym && /* symbolic transform? */ |
| 315 |
|
|
!checksymbolic(rop)) |
| 316 |
|
|
return(0); |
| 317 |
|
|
/* undo exposure? */ |
| 318 |
|
|
if (fabs(1. - bright(rop->rmp->cexp)) > .025) { |
| 319 |
|
|
if (rop->rmp->ncomp == 1) |
| 320 |
|
|
rop->rmp->cexp[RED] = rop->rmp->cexp[GRN] = |
| 321 |
|
|
rop->rmp->cexp[BLU] = bright(rop->rmp->cexp); |
| 322 |
|
|
if (rop->preop.nsf <= 0) { |
| 323 |
|
|
rop->preop.nsf = i = rop->rmp->ncomp; |
| 324 |
|
|
while (i--) |
| 325 |
|
|
rop->preop.sca[i] = 1.; |
| 326 |
|
|
} |
| 327 |
|
|
if (rop->preop.nsf == 1) { |
| 328 |
|
|
if (rop->rmp->ncomp == 3) { |
| 329 |
|
|
rop->preop.sca[2] = rop->preop.sca[1] = |
| 330 |
|
|
rop->preop.sca[0]; |
| 331 |
|
|
rop->preop.nsf = 3; |
| 332 |
|
|
} else |
| 333 |
|
|
rop->preop.sca[0] /= bright(rop->rmp->cexp); |
| 334 |
|
|
} |
| 335 |
|
|
if (rop->preop.nsf == 3) { |
| 336 |
|
|
opcolor(rop->preop.sca, /=, rop->rmp->cexp); |
| 337 |
|
|
} else if (rop->preop.nsf > 3) { /* punt */ |
| 338 |
|
|
double mult = 1./bright(rop->rmp->cexp); |
| 339 |
|
|
for (i = rop->preop.nsf; i--; ) |
| 340 |
|
|
rop->preop.sca[i] *= mult; |
| 341 |
|
|
} |
| 342 |
|
|
setcolor(rop->rmp->cexp, 1., 1., 1.); |
| 343 |
|
|
} |
| 344 |
|
|
if (rop->preop.clen > 0) { /* use component transform? */ |
| 345 |
|
|
if (rop->preop.clen % rop->imx.ncomp) { |
| 346 |
|
|
fprintf(stderr, "%s: -c must have N x %d coefficients\n", |
| 347 |
|
|
rop->inspec, rop->imx.ncomp); |
| 348 |
|
|
return(0); |
| 349 |
|
|
} |
| 350 |
|
|
if (rop->preop.nsf > 0) { /* scale transform, instead */ |
| 351 |
|
|
if (rop->preop.nsf == 1) { |
| 352 |
|
|
for (i = rop->preop.clen; i--; ) |
| 353 |
|
|
rop->preop.cmat[i] *= rop->preop.sca[0]; |
| 354 |
|
|
} else if (rop->preop.nsf*rop->imx.ncomp != rop->preop.clen) { |
| 355 |
|
|
fprintf(stderr, "%s: -s must have one or %d factors\n", |
| 356 |
|
|
rop->inspec, |
| 357 |
|
|
rop->preop.clen/rop->imx.ncomp); |
| 358 |
|
|
return(0); |
| 359 |
|
|
} else { |
| 360 |
|
|
for (i = rop->preop.nsf; i--; ) |
| 361 |
|
|
for (j = rop->imx.ncomp; j--; ) |
| 362 |
|
|
rop->preop.cmat[i*rop->imx.ncomp+j] |
| 363 |
|
|
*= rop->preop.sca[i]; |
| 364 |
|
|
} |
| 365 |
|
|
} |
| 366 |
|
|
rop->preop.nsf = 0; /* now folded in */ |
| 367 |
|
|
if (!split_input(rop)) /* get our own struct */ |
| 368 |
|
|
return(0); |
| 369 |
|
|
rop->rmp->ncomp = rop->preop.clen / rop->imx.ncomp; |
| 370 |
|
|
if ((rop->rmp->ncomp > 3) & (rop->rmp->dtype <= DTspec)) { |
| 371 |
|
|
rop->rmp->dtype = DTfloat; /* probably not actual spectrum */ |
| 372 |
|
|
memcpy(rop->rmp->wlpart, WLPART, sizeof(rop->rmp->wlpart)); |
| 373 |
|
|
} |
| 374 |
|
|
} else if (rop->preop.nsf > 0) { /* else use scalar(s)? */ |
| 375 |
|
|
if (rop->preop.nsf == 1) { |
| 376 |
|
|
for (i = rop->rmp->ncomp; --i; ) |
| 377 |
|
|
rop->preop.sca[i] = rop->preop.sca[0]; |
| 378 |
|
|
rop->preop.nsf = rop->rmp->ncomp; |
| 379 |
|
|
} else if (rop->preop.nsf != rop->rmp->ncomp) { |
| 380 |
|
|
fprintf(stderr, "%s: -s must have one or %d factors\n", |
| 381 |
|
|
rop->inspec, rop->rmp->ncomp); |
| 382 |
|
|
return(0); |
| 383 |
|
|
} |
| 384 |
|
|
} |
| 385 |
|
|
return(1); |
| 386 |
|
|
} |
| 387 |
|
|
|
| 388 |
greg |
2.34 |
/* Apply the given color transform and/or scaling operation */ |
| 389 |
greg |
2.22 |
int |
| 390 |
greg |
2.1 |
apply_op(RMATRIX *dst, const RMATRIX *src, const RUNARYOP *ro) |
| 391 |
|
|
{ |
| 392 |
|
|
if (ro->clen > 0) { |
| 393 |
|
|
RMATRIX *res = rmx_transform(src, dst->ncomp, ro->cmat); |
| 394 |
|
|
if (!res) { |
| 395 |
|
|
fputs("Error in call to rmx_transform()\n", stderr); |
| 396 |
|
|
return(0); |
| 397 |
|
|
} |
| 398 |
|
|
if (!rmx_transfer_data(dst, res, 0)) |
| 399 |
|
|
return(0); |
| 400 |
|
|
rmx_free(res); |
| 401 |
|
|
} else if (dst != src) |
| 402 |
greg |
2.9 |
memcpy(dst->mtx, src->mtx, rmx_array_size(dst)); |
| 403 |
greg |
2.1 |
if (ro->nsf == dst->ncomp) |
| 404 |
|
|
rmx_scale(dst, ro->sca); |
| 405 |
|
|
return(1); |
| 406 |
|
|
} |
| 407 |
|
|
|
| 408 |
greg |
2.34 |
/* Open the associated input file and load/check header */ |
| 409 |
greg |
2.22 |
int |
| 410 |
greg |
2.1 |
open_input(ROPMAT *rop) |
| 411 |
|
|
{ |
| 412 |
|
|
int outtype; |
| 413 |
|
|
|
| 414 |
|
|
if (!rop || !rop->inspec || !rop->inspec[0]) |
| 415 |
|
|
return(0); |
| 416 |
|
|
if (rop->inspec == stdin_name) |
| 417 |
|
|
rop->infp = stdin; |
| 418 |
|
|
else if (rop->inspec[0] == '!') |
| 419 |
|
|
rop->infp = popen(rop->inspec+1, "r"); |
| 420 |
|
|
else |
| 421 |
|
|
rop->infp = fopen(rop->inspec, "rb"); |
| 422 |
|
|
|
| 423 |
|
|
if (!rmx_load_header(&rop->imx, rop->infp)) { |
| 424 |
|
|
fprintf(stderr, "Bad header from: %s\n", rop->inspec); |
| 425 |
|
|
return(0); |
| 426 |
|
|
} |
| 427 |
|
|
return(get_component_xfm(rop)); |
| 428 |
|
|
} |
| 429 |
|
|
|
| 430 |
|
|
/* Return nominal wavelength associated with input component (return nm) */ |
| 431 |
greg |
2.22 |
double |
| 432 |
greg |
2.1 |
l_wavelength(char *nam) |
| 433 |
|
|
{ |
| 434 |
|
|
double comp = argument(1); |
| 435 |
|
|
|
| 436 |
|
|
if ((comp < -.5) | (comp >= in_ncomp+.5)) { |
| 437 |
|
|
errno = EDOM; |
| 438 |
|
|
return(.0); |
| 439 |
|
|
} |
| 440 |
|
|
if (comp < .5) /* asking for #components? */ |
| 441 |
|
|
return(in_ncomp); |
| 442 |
|
|
|
| 443 |
|
|
if (in_ncomp == 3) { /* special case for RGB */ |
| 444 |
|
|
const int w0 = (int)(comp - .5); |
| 445 |
|
|
return(mop[0].rmp->wlpart[w0] + |
| 446 |
|
|
(comp-.5)*(mop[0].rmp->wlpart[w0+1] - |
| 447 |
|
|
mop[0].rmp->wlpart[w0])); |
| 448 |
|
|
} |
| 449 |
|
|
return(mop[0].rmp->wlpart[0] + /* general case, even div. */ |
| 450 |
|
|
(comp-.5)/(double)in_ncomp * |
| 451 |
|
|
(mop[0].rmp->wlpart[3] - mop[0].rmp->wlpart[0])); |
| 452 |
|
|
} |
| 453 |
|
|
|
| 454 |
|
|
/* Return ith input with optional channel selector */ |
| 455 |
greg |
2.22 |
double |
| 456 |
greg |
2.1 |
l_chanin(char *nam) |
| 457 |
|
|
{ |
| 458 |
|
|
double inp = argument(1); |
| 459 |
|
|
int mi, chan; |
| 460 |
|
|
|
| 461 |
|
|
if ((mi = (int)(inp-.5)) < 0 || mi >= nmats) { |
| 462 |
|
|
errno = EDOM; |
| 463 |
|
|
return(.0); |
| 464 |
|
|
} |
| 465 |
|
|
if (inp < .5) /* asking for #inputs? */ |
| 466 |
|
|
return(nmats); |
| 467 |
|
|
|
| 468 |
|
|
if (nargum() >= 2) { |
| 469 |
|
|
double cval = argument(2); |
| 470 |
|
|
if (cval < .5 || (chan = (int)(cval-.5)) >= in_ncomp) { |
| 471 |
|
|
errno = EDOM; |
| 472 |
|
|
return(.0); |
| 473 |
|
|
} |
| 474 |
|
|
} else |
| 475 |
|
|
chan = cur_chan; |
| 476 |
|
|
|
| 477 |
|
|
return(mop[mi].rmp->mtx[cur_col*in_ncomp + chan]); |
| 478 |
|
|
} |
| 479 |
|
|
|
| 480 |
greg |
2.34 |
/* Set up our operations and check consistency */ |
| 481 |
greg |
2.22 |
int |
| 482 |
greg |
2.1 |
initialize(RMATRIX *imp) |
| 483 |
|
|
{ |
| 484 |
|
|
int i; |
| 485 |
|
|
/* XXX struct is zeroed coming in */ |
| 486 |
|
|
setcolor(imp->cexp, 1.f, 1.f, 1.f); |
| 487 |
|
|
for (i = 0; i < nmats; i++) { /* open each input */ |
| 488 |
|
|
int restype; |
| 489 |
|
|
if (!open_input(&mop[i])) |
| 490 |
|
|
return(0); |
| 491 |
|
|
restype = mop[i].rmp->dtype; |
| 492 |
|
|
if (!imp->dtype || (restype = rmx_newtype(restype, imp->dtype)) > 0) |
| 493 |
|
|
imp->dtype = restype; |
| 494 |
greg |
2.24 |
else if (!nowarn) |
| 495 |
greg |
2.1 |
fprintf(stderr, "%s: warning - data type mismatch\n", |
| 496 |
|
|
mop[i].inspec); |
| 497 |
|
|
if (!i) { |
| 498 |
|
|
imp->ncols = mop[0].rmp->ncols; |
| 499 |
|
|
imp->ncomp = mop[0].rmp->ncomp; |
| 500 |
|
|
memcpy(imp->wlpart, mop[0].rmp->wlpart, sizeof(imp->wlpart)); |
| 501 |
greg |
2.2 |
} else if ((mop[i].rmp->ncols != imp->ncols) | |
| 502 |
|
|
(mop[i].rmp->ncomp != imp->ncomp) | |
| 503 |
|
|
((in_nrows > 0) & (mop[i].rmp->nrows > 0) & |
| 504 |
|
|
(mop[i].rmp->nrows != in_nrows))) { |
| 505 |
greg |
2.1 |
fprintf(stderr, "%s: mismatch in size or #components\n", |
| 506 |
|
|
mop[i].inspec); |
| 507 |
|
|
return(0); |
| 508 |
|
|
} /* XXX should check wlpart? */ |
| 509 |
greg |
2.2 |
if (in_nrows <= 0) |
| 510 |
|
|
in_nrows = imp->nrows = mop[i].rmp->nrows; |
| 511 |
greg |
2.1 |
} /* set up .cal environment */ |
| 512 |
|
|
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
| 513 |
|
|
esupport &= ~(E_OUTCHAN|E_INCHAN); |
| 514 |
|
|
varset("PI", ':', M_PI); |
| 515 |
|
|
varset("nfiles", ':', nmats); |
| 516 |
|
|
varset("nrows", ':', in_nrows); |
| 517 |
|
|
varset("ncols", ':', in_ncols); |
| 518 |
|
|
varset("ncomp", ':', in_ncomp); |
| 519 |
|
|
varset("R", ':', 1.); |
| 520 |
|
|
varset("G", ':', 2.); |
| 521 |
|
|
varset("B", ':', 3.); |
| 522 |
|
|
funset("wl", 1, ':', l_wavelength); |
| 523 |
|
|
funset("ci", 1, '=', l_chanin); |
| 524 |
|
|
scompile("ri(i)=ci(i,R);gi(i)=ci(i,G);bi(i)=ci(i,B)", NULL, 0); |
| 525 |
|
|
return(1); |
| 526 |
|
|
} |
| 527 |
|
|
|
| 528 |
greg |
2.34 |
/* Copy input header information to output header, indented */ |
| 529 |
greg |
2.22 |
void |
| 530 |
greg |
2.1 |
output_headinfo(FILE *fp) |
| 531 |
|
|
{ |
| 532 |
|
|
int i; |
| 533 |
|
|
|
| 534 |
|
|
for (i = 0; i < nmats; i++) { |
| 535 |
|
|
const char *cp = mop[i].imx.info; |
| 536 |
|
|
fputs(mop[i].inspec, fp); |
| 537 |
|
|
fputs(":\n", fp); |
| 538 |
|
|
if (!cp) continue; |
| 539 |
|
|
while (*cp) { |
| 540 |
|
|
if (*cp == '\n') { |
| 541 |
|
|
cp++; /* avoid inadvertant terminus */ |
| 542 |
|
|
continue; |
| 543 |
|
|
} |
| 544 |
|
|
fputc('\t', fp); /* indent this input's info */ |
| 545 |
|
|
do |
| 546 |
|
|
putc(*cp, fp); |
| 547 |
|
|
while (*cp++ != '\n'); |
| 548 |
|
|
} |
| 549 |
|
|
} |
| 550 |
|
|
} |
| 551 |
|
|
|
| 552 |
greg |
2.34 |
/* Spawn the indicated number of children and return 1 in parent */ |
| 553 |
greg |
2.22 |
int |
| 554 |
greg |
2.10 |
spawned_children(int np) |
| 555 |
greg |
2.1 |
{ |
| 556 |
greg |
2.10 |
int i, rv; |
| 557 |
|
|
|
| 558 |
|
|
#if defined(_WIN32) || defined(_WIN64) |
| 559 |
|
|
if (np > 1) { |
| 560 |
greg |
2.24 |
if (!nowarn) |
| 561 |
|
|
fputs("Warning: only one process under Windows\n", stderr); |
| 562 |
greg |
2.10 |
np = 1; |
| 563 |
|
|
} else |
| 564 |
|
|
#endif |
| 565 |
greg |
2.18 |
if ((in_nrows > 0) & (np*4 > in_nrows)) |
| 566 |
|
|
np = in_nrows/4; |
| 567 |
greg |
2.10 |
/* we'll be doing a row at a time */ |
| 568 |
greg |
2.1 |
for (i = 0; i < nmats; i++) { |
| 569 |
greg |
2.10 |
mop[i].imx.nrows = 1; |
| 570 |
greg |
2.1 |
if (!rmx_prepare(&mop[i].imx)) |
| 571 |
|
|
goto memerror; |
| 572 |
|
|
if (mop[i].rmp != &mop[i].imx) { |
| 573 |
|
|
mop[i].rmp->nrows = 1; |
| 574 |
|
|
if (!rmx_prepare(mop[i].rmp)) |
| 575 |
|
|
goto memerror; |
| 576 |
|
|
} |
| 577 |
|
|
} |
| 578 |
greg |
2.20 |
/* prep output row buffer(s) */ |
| 579 |
greg |
2.23 |
if (mop[nmats].preop.clen > 0) { |
| 580 |
greg |
2.10 |
if (!split_input(&mop[nmats])) /* need separate buffer */ |
| 581 |
greg |
2.23 |
goto memerror; |
| 582 |
|
|
mop[nmats].rmp->ncomp = mop[nmats].preop.clen / |
| 583 |
|
|
mop[nmats].imx.ncomp; |
| 584 |
greg |
2.1 |
} |
| 585 |
greg |
2.10 |
mop[nmats].imx.nrows = 1; |
| 586 |
|
|
if (!rmx_prepare(&mop[nmats].imx)) |
| 587 |
|
|
goto memerror; |
| 588 |
greg |
2.20 |
if (mop[nmats].rmp != &mop[nmats].imx) { |
| 589 |
|
|
mop[nmats].rmp->nrows = 1; |
| 590 |
|
|
if (!rmx_prepare(mop[nmats].rmp)) |
| 591 |
|
|
goto memerror; |
| 592 |
|
|
} |
| 593 |
greg |
2.18 |
if (np <= 1) { /* single process return */ |
| 594 |
greg |
2.10 |
#ifdef getc_unlocked |
| 595 |
|
|
for (i = 0; i < nmats; i++) |
| 596 |
|
|
flockfile(mop[i].infp); |
| 597 |
|
|
flockfile(stdout); |
| 598 |
|
|
#endif |
| 599 |
|
|
return(0); |
| 600 |
|
|
} |
| 601 |
|
|
fflush(stdout); /* flush header & spawn children */ |
| 602 |
greg |
2.20 |
nchildren = np + 1; /* extra child to sequence output */ |
| 603 |
greg |
2.19 |
cproc = (SUBPROC *)malloc(sizeof(SUBPROC)*nchildren); |
| 604 |
greg |
2.10 |
if (!cproc) |
| 605 |
|
|
goto memerror; |
| 606 |
greg |
2.20 |
for (i = nchildren; i--; ) cproc[i] = sp_inactive; |
| 607 |
|
|
cproc[nchildren-1].flags |= PF_FILT_OUT; |
| 608 |
greg |
2.23 |
/* start each child from parent */ |
| 609 |
|
|
for (i = 0; i < nchildren; i++) |
| 610 |
|
|
if ((rv = open_process(&cproc[i], NULL)) <= 0) |
| 611 |
|
|
break; /* child breaks here */ |
| 612 |
greg |
2.10 |
if (rv < 0) { |
| 613 |
greg |
2.23 |
perror("fork"); /* WTH? */ |
| 614 |
greg |
2.17 |
close_processes(cproc, i); |
| 615 |
greg |
2.10 |
exit(1); |
| 616 |
|
|
} |
| 617 |
greg |
2.23 |
if (i != nchildren-1) { /* last child is sole reader */ |
| 618 |
|
|
int j = i; |
| 619 |
|
|
while (j-- > 0) { |
| 620 |
|
|
close(cproc[j].r); |
| 621 |
|
|
cproc[j].r = -1; |
| 622 |
greg |
2.21 |
} |
| 623 |
greg |
2.23 |
} |
| 624 |
|
|
if (rv > 0) |
| 625 |
greg |
2.20 |
return(1); /* parent return value */ |
| 626 |
greg |
2.23 |
|
| 627 |
|
|
inchild = i; /* else set our child index */ |
| 628 |
greg |
2.20 |
while (i-- > 0) /* only parent writes siblings */ |
| 629 |
greg |
2.11 |
close(cproc[i].w); |
| 630 |
greg |
2.20 |
|
| 631 |
greg |
2.23 |
i = nmats; /* close matrix streams (carefully) */ |
| 632 |
|
|
while (i-- > 0) { |
| 633 |
|
|
if (mop[i].infp != stdin) { |
| 634 |
|
|
close(fileno(mop[i].infp)); /* avoid lseek() */ |
| 635 |
|
|
fclose(mop[i].infp); /* ! pclose() */ |
| 636 |
|
|
} |
| 637 |
|
|
mop[i].infp = NULL; |
| 638 |
|
|
} |
| 639 |
|
|
fpurge(stdin); /* discard previously buffered input */ |
| 640 |
|
|
|
| 641 |
greg |
2.22 |
if (inchild == nchildren-1) |
| 642 |
|
|
return(-1); /* output process return value */ |
| 643 |
greg |
2.20 |
|
| 644 |
greg |
2.23 |
i = nmats; /* get matrix rows from parent */ |
| 645 |
greg |
2.20 |
while (i-- > 0) { |
| 646 |
|
|
mop[i].infp = stdin; |
| 647 |
|
|
mop[i].imx.dtype = DTrmx_native; |
| 648 |
|
|
mop[i].imx.pflags &= ~RMF_SWAPIN; |
| 649 |
|
|
} |
| 650 |
greg |
2.10 |
#ifdef getc_unlocked |
| 651 |
|
|
flockfile(stdin); |
| 652 |
|
|
#endif |
| 653 |
greg |
2.20 |
mop[nmats].rmp->dtype = DTrmx_native; |
| 654 |
|
|
return(0); /* worker child return value */ |
| 655 |
greg |
2.10 |
memerror: |
| 656 |
|
|
fputs("Out of memory in spawned_children()\n", stderr); |
| 657 |
|
|
exit(1); |
| 658 |
|
|
} |
| 659 |
|
|
|
| 660 |
greg |
2.34 |
/* Run parental feeder loop */ |
| 661 |
greg |
2.22 |
int |
| 662 |
greg |
2.19 |
parent_loop(void) |
| 663 |
greg |
2.10 |
{ |
| 664 |
|
|
int i; |
| 665 |
|
|
|
| 666 |
greg |
2.20 |
rmx_reset(&mop[nmats].imx); /* not touching output side */ |
| 667 |
|
|
if (mop[nmats].rmp != &mop[nmats].imx) { |
| 668 |
|
|
rmx_free(mop[nmats].rmp); |
| 669 |
|
|
mop[nmats].rmp = &mop[nmats].imx; |
| 670 |
|
|
} |
| 671 |
greg |
2.10 |
#ifdef getc_unlocked |
| 672 |
greg |
2.20 |
for (i = 0; i < nmats; i++) /* we handle matrix inputs */ |
| 673 |
greg |
2.10 |
flockfile(mop[i].infp); |
| 674 |
|
|
#endif |
| 675 |
greg |
2.19 |
/* load & send rows to kids */ |
| 676 |
greg |
2.10 |
for (cur_row = 0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row++) { |
| 677 |
greg |
2.20 |
int wfd = cproc[cur_row % (nchildren-1)].w; |
| 678 |
greg |
2.11 |
for (i = 0; i < nmats; i++) |
| 679 |
greg |
2.10 |
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 680 |
|
|
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 681 |
|
|
break; |
| 682 |
greg |
2.27 |
fprintf(stderr, "%s: load error at row %d\n", |
| 683 |
greg |
2.10 |
mop[i].inspec, cur_row); |
| 684 |
|
|
return(0); |
| 685 |
|
|
} |
| 686 |
greg |
2.11 |
if (i < nmats) |
| 687 |
|
|
break; |
| 688 |
|
|
for (i = 0; i < nmats; i++) |
| 689 |
greg |
2.19 |
if (writebuf(wfd, mop[i].imx.mtx, rmx_array_size(&mop[i].imx)) |
| 690 |
greg |
2.23 |
!= rmx_array_size(&mop[i].imx)) { |
| 691 |
greg |
2.27 |
fprintf(stderr, "%s: write error at row %d\n", |
| 692 |
greg |
2.23 |
mop[i].inspec, cur_row); |
| 693 |
greg |
2.19 |
return(0); |
| 694 |
greg |
2.23 |
} |
| 695 |
greg |
2.10 |
} |
| 696 |
greg |
2.23 |
i = close_processes(cproc, nchildren); /* collect family */ |
| 697 |
greg |
2.20 |
free(cproc); cproc = NULL; nchildren = 0; |
| 698 |
greg |
2.10 |
if (i < 0) { |
| 699 |
greg |
2.24 |
if (!nowarn) |
| 700 |
greg |
2.27 |
fputs("Warning: lost child process\n", stderr); |
| 701 |
greg |
2.10 |
return(1); |
| 702 |
|
|
} |
| 703 |
|
|
if (i > 0) { |
| 704 |
|
|
fprintf(stderr, "Child exited with status %d\n", i); |
| 705 |
|
|
return(0); |
| 706 |
|
|
} |
| 707 |
greg |
2.20 |
return(1); /* return success! */ |
| 708 |
greg |
2.10 |
} |
| 709 |
|
|
|
| 710 |
greg |
2.34 |
/* Main operation loop, may be run in each child */ |
| 711 |
greg |
2.22 |
int |
| 712 |
greg |
2.19 |
combine_input(void) |
| 713 |
greg |
2.10 |
{ |
| 714 |
greg |
2.20 |
const int row0 = (inchild >= 0)*inchild; |
| 715 |
|
|
const int rstep = nchildren ? nchildren-1 : 1; |
| 716 |
greg |
2.10 |
ROPMAT *res = &mop[nmats]; |
| 717 |
|
|
int set_r, set_c; |
| 718 |
|
|
RMATRIX *tmp = NULL; |
| 719 |
|
|
int co_set; |
| 720 |
|
|
int i; |
| 721 |
|
|
|
| 722 |
greg |
2.23 |
if (mcat_last && !(tmp = rmx_alloc(1, res->imx.ncols, res->rmp->ncomp))) { |
| 723 |
|
|
fputs("Out of buffer space in combine_input()\n", stderr); |
| 724 |
|
|
return(0); |
| 725 |
|
|
} |
| 726 |
greg |
2.1 |
/* figure out what the user set */ |
| 727 |
|
|
co_set = fundefined("co"); |
| 728 |
|
|
if (!co_set) |
| 729 |
|
|
co_set = -vardefined("co"); |
| 730 |
|
|
if (!co_set & (in_ncomp == 3) && vardefined("ro") && |
| 731 |
|
|
vardefined("go") && vardefined("bo")) { |
| 732 |
|
|
scompile("co(p)=select(p,ro,go,bo)", NULL, 0); |
| 733 |
|
|
co_set = 1; |
| 734 |
|
|
} |
| 735 |
|
|
if (co_set) { /* set if user wants, didn't set */ |
| 736 |
|
|
set_r = varlookup("r") != NULL && !vardefined("r"); |
| 737 |
|
|
set_c = varlookup("c") != NULL && !vardefined("c"); |
| 738 |
|
|
} else /* save a little time */ |
| 739 |
|
|
set_r = set_c = 0; |
| 740 |
greg |
2.16 |
/* read/process row-by-row */ |
| 741 |
greg |
2.10 |
for (cur_row = row0; (in_nrows <= 0) | (cur_row < in_nrows); cur_row += rstep) { |
| 742 |
greg |
2.1 |
RMATRIX *mres = NULL; |
| 743 |
greg |
2.14 |
for (i = 0; i < nmats; i++) |
| 744 |
greg |
2.1 |
if (!rmx_load_row(mop[i].imx.mtx, &mop[i].imx, mop[i].infp)) { |
| 745 |
greg |
2.5 |
if (cur_row > in_nrows) /* unknown #input rows? */ |
| 746 |
greg |
2.10 |
break; |
| 747 |
greg |
2.27 |
fprintf(stderr, "%s: load error at row %d\n", |
| 748 |
greg |
2.1 |
mop[i].inspec, cur_row); |
| 749 |
|
|
return(0); |
| 750 |
|
|
} |
| 751 |
greg |
2.10 |
if (i < nmats) |
| 752 |
|
|
break; |
| 753 |
|
|
for (i = 0; i < nmats; i++) |
| 754 |
greg |
2.1 |
if (!apply_op(mop[i].rmp, &mop[i].imx, &mop[i].preop)) |
| 755 |
|
|
return(0); |
| 756 |
|
|
if (set_r) varset("r", '=', cur_row); |
| 757 |
|
|
for (cur_col = 0; cur_col < in_ncols; cur_col++) { |
| 758 |
|
|
if (set_c) varset("c", '=', cur_col); |
| 759 |
|
|
for (cur_chan = 0; cur_chan < in_ncomp; cur_chan++) { |
| 760 |
|
|
const int ndx = cur_col*in_ncomp + cur_chan; |
| 761 |
|
|
eclock++; |
| 762 |
|
|
if (!co_set) { /* just summing elements? */ |
| 763 |
|
|
res->imx.mtx[ndx] = 0; |
| 764 |
|
|
for (i = nmats; i--; ) |
| 765 |
|
|
res->imx.mtx[ndx] += mop[i].rmp->mtx[ndx]; |
| 766 |
|
|
} else if (co_set > 0) { |
| 767 |
|
|
double dchan = cur_chan+1; |
| 768 |
|
|
res->imx.mtx[ndx] = funvalue("co", 1, &dchan); |
| 769 |
|
|
} else |
| 770 |
|
|
res->imx.mtx[ndx] = varvalue("co"); |
| 771 |
|
|
} |
| 772 |
|
|
} /* final conversions */ |
| 773 |
|
|
if (!mcat) { |
| 774 |
|
|
if (!apply_op(res->rmp, &res->imx, &res->preop)) |
| 775 |
|
|
return(0); |
| 776 |
|
|
} else if (mcat_last) { |
| 777 |
|
|
if (!apply_op(tmp, &res->imx, &res->preop)) |
| 778 |
|
|
return(0); |
| 779 |
|
|
mres = rmx_multiply(tmp, mcat); |
| 780 |
|
|
if (!mres) |
| 781 |
|
|
goto multerror; |
| 782 |
|
|
if (!rmx_transfer_data(res->rmp, mres, 0)) |
| 783 |
|
|
return(0); |
| 784 |
|
|
} else /* mcat && !mcat_last */ { |
| 785 |
|
|
mres = rmx_multiply(&res->imx, mcat); |
| 786 |
|
|
if (!mres) |
| 787 |
|
|
goto multerror; |
| 788 |
|
|
if (!apply_op(res->rmp, mres, &res->preop)) |
| 789 |
|
|
return(0); |
| 790 |
|
|
} |
| 791 |
|
|
rmx_free(mres); mres = NULL; |
| 792 |
|
|
if (!rmx_write_data(res->rmp->mtx, res->rmp->ncomp, |
| 793 |
greg |
2.23 |
res->rmp->ncols, res->rmp->dtype, stdout) || |
| 794 |
|
|
(inchild >= 0 && fflush(stdout) == EOF)) { |
| 795 |
greg |
2.27 |
fprintf(stderr, "Conversion/write error at row %d\n", |
| 796 |
greg |
2.23 |
cur_row); |
| 797 |
greg |
2.16 |
return(0); |
| 798 |
greg |
2.23 |
} |
| 799 |
greg |
2.1 |
} |
| 800 |
greg |
2.20 |
return(inchild >= 0 || fflush(stdout) != EOF); |
| 801 |
greg |
2.1 |
multerror: |
| 802 |
greg |
2.27 |
fputs("Unexpected matrix multiply error\n", stderr); |
| 803 |
greg |
2.1 |
return(0); |
| 804 |
|
|
} |
| 805 |
|
|
|
| 806 |
greg |
2.34 |
/* Run output process loop when #processes > 1 */ |
| 807 |
greg |
2.22 |
int |
| 808 |
|
|
output_loop(void) |
| 809 |
|
|
{ |
| 810 |
|
|
const size_t row_size = rmx_array_size(mop[nmats].rmp); |
| 811 |
greg |
2.23 |
int cur_child = 0; |
| 812 |
greg |
2.22 |
int i = nmats; |
| 813 |
|
|
|
| 814 |
greg |
2.23 |
while (i-- > 0) { /* free input buffers */ |
| 815 |
greg |
2.22 |
rmx_reset(&mop[i].imx); |
| 816 |
|
|
if (mop[i].rmp != &mop[i].imx) { |
| 817 |
|
|
rmx_free(mop[i].rmp); |
| 818 |
|
|
mop[i].rmp = &mop[i].imx; |
| 819 |
|
|
} |
| 820 |
|
|
} |
| 821 |
greg |
2.23 |
if (mop[nmats].rmp != &mop[nmats].imx) /* output is split? */ |
| 822 |
|
|
rmx_reset(&mop[nmats].imx); |
| 823 |
greg |
2.22 |
#ifdef getc_unlocked |
| 824 |
|
|
flockfile(stdout); /* we own this, now */ |
| 825 |
|
|
#endif |
| 826 |
|
|
for ( ; ; ) { /* loop until no more */ |
| 827 |
|
|
ssize_t rv; |
| 828 |
|
|
rv = readbuf(cproc[cur_child].r, mop[nmats].rmp->mtx, row_size); |
| 829 |
|
|
if (!rv) /* out of rows? */ |
| 830 |
|
|
break; |
| 831 |
|
|
if (rv != row_size) { |
| 832 |
greg |
2.27 |
fputs("Read error\n", stderr); |
| 833 |
greg |
2.22 |
return(0); |
| 834 |
|
|
} /* do final conversion */ |
| 835 |
|
|
if (!rmx_write_data(mop[nmats].rmp->mtx, mop[nmats].rmp->ncomp, |
| 836 |
|
|
mop[nmats].rmp->ncols, mop[nmats].rmp->dtype, stdout)) { |
| 837 |
greg |
2.27 |
fputs("Conversion/write error\n", stderr); |
| 838 |
greg |
2.22 |
return(0); |
| 839 |
|
|
} |
| 840 |
|
|
cur_child++; |
| 841 |
greg |
2.23 |
cur_child *= (cur_child < inchild); /* loop over workers */ |
| 842 |
greg |
2.22 |
} |
| 843 |
|
|
return(fflush(stdout) != EOF); |
| 844 |
|
|
} |
| 845 |
|
|
|
| 846 |
greg |
2.34 |
/* Check/convert floating-point arguments following this */ |
| 847 |
greg |
2.22 |
int |
| 848 |
greg |
2.1 |
get_factors(double da[], int n, char *av[]) |
| 849 |
|
|
{ |
| 850 |
|
|
int ac; |
| 851 |
|
|
|
| 852 |
|
|
for (ac = 0; ac < n && isflt(av[ac]); ac++) |
| 853 |
|
|
da[ac] = atof(av[ac]); |
| 854 |
|
|
return(ac); |
| 855 |
|
|
} |
| 856 |
|
|
|
| 857 |
greg |
2.34 |
/* Resize/reallocate the input array as requested */ |
| 858 |
greg |
2.22 |
void |
| 859 |
greg |
2.1 |
resize_inparr(int n2alloc) |
| 860 |
|
|
{ |
| 861 |
|
|
int i; |
| 862 |
|
|
|
| 863 |
greg |
2.6 |
if (n2alloc == nall) |
| 864 |
|
|
return; |
| 865 |
greg |
2.26 |
for (i = nall; i-- > n2alloc; ) { |
| 866 |
greg |
2.1 |
rmx_reset(&mop[i].imx); |
| 867 |
|
|
if (mop[i].rmp != &mop[i].imx) |
| 868 |
|
|
rmx_free(mop[i].rmp); |
| 869 |
|
|
} |
| 870 |
|
|
mop = (ROPMAT *)realloc(mop, n2alloc*sizeof(ROPMAT)); |
| 871 |
|
|
if (mop == NULL) { |
| 872 |
|
|
fputs("Out of memory in resize_inparr()\n", stderr); |
| 873 |
|
|
exit(1); |
| 874 |
|
|
} |
| 875 |
greg |
2.6 |
if (n2alloc > nall) |
| 876 |
|
|
memset(mop+nall, 0, (n2alloc-nall)*sizeof(ROPMAT)); |
| 877 |
greg |
2.1 |
nall = n2alloc; |
| 878 |
|
|
} |
| 879 |
|
|
|
| 880 |
|
|
/* Load one or more matrices and operate on them, sending results to stdout */ |
| 881 |
|
|
int |
| 882 |
|
|
main(int argc, char *argv[]) |
| 883 |
|
|
{ |
| 884 |
|
|
|
| 885 |
|
|
int outfmt = DTfromHeader; |
| 886 |
|
|
const char *defCsym = NULL; |
| 887 |
|
|
int echoheader = 1; |
| 888 |
|
|
int stdin_used = 0; |
| 889 |
greg |
2.10 |
int nproc = 1; |
| 890 |
greg |
2.1 |
const char *mcat_spec = NULL; |
| 891 |
greg |
2.32 |
int transpose_mcat = 0; |
| 892 |
greg |
2.1 |
int n2comp = 0; |
| 893 |
|
|
uby8 comp_ndx[128]; |
| 894 |
|
|
int i; |
| 895 |
|
|
/* get starting input array */ |
| 896 |
|
|
mop = (ROPMAT *)calloc(nall=2, sizeof(ROPMAT)); |
| 897 |
|
|
/* get options and arguments */ |
| 898 |
|
|
for (i = 1; i < argc; i++) |
| 899 |
|
|
if (argv[i][0] != '-' || !argv[i][1]) { |
| 900 |
|
|
if (argv[i][0] == '-') { |
| 901 |
|
|
if (stdin_used++) goto stdin_error; |
| 902 |
|
|
mop[nmats].inspec = stdin_name; |
| 903 |
|
|
} else |
| 904 |
|
|
mop[nmats].inspec = argv[i]; |
| 905 |
|
|
if (!mop[nmats].preop.csym) |
| 906 |
|
|
mop[nmats].preop.csym = defCsym; |
| 907 |
|
|
if (++nmats >= nall) |
| 908 |
|
|
resize_inparr(nmats + (nmats>>2) + 2); |
| 909 |
|
|
} else { |
| 910 |
|
|
int n = argc-1 - i; |
| 911 |
|
|
switch (argv[i][1]) { /* get option */ |
| 912 |
|
|
case 'w': |
| 913 |
|
|
nowarn = !nowarn; |
| 914 |
|
|
break; |
| 915 |
|
|
case 'h': |
| 916 |
|
|
echoheader = !echoheader; |
| 917 |
|
|
break; |
| 918 |
greg |
2.10 |
case 'n': |
| 919 |
|
|
nproc = atoi(argv[++i]); |
| 920 |
|
|
if (nproc <= 0) |
| 921 |
|
|
goto userr; |
| 922 |
|
|
break; |
| 923 |
greg |
2.1 |
case 'e': |
| 924 |
|
|
if (!n) goto userr; |
| 925 |
|
|
comp_ndx[n2comp++] = i++; |
| 926 |
|
|
break; |
| 927 |
|
|
case 'f': |
| 928 |
|
|
switch (argv[i][2]) { |
| 929 |
|
|
case '\0': |
| 930 |
|
|
if (!n) goto userr; |
| 931 |
|
|
comp_ndx[n2comp++] = i++; |
| 932 |
|
|
break; |
| 933 |
|
|
case 'd': |
| 934 |
|
|
outfmt = DTdouble; |
| 935 |
|
|
break; |
| 936 |
|
|
case 'f': |
| 937 |
|
|
outfmt = DTfloat; |
| 938 |
|
|
break; |
| 939 |
|
|
case 'a': |
| 940 |
|
|
outfmt = DTascii; |
| 941 |
|
|
break; |
| 942 |
|
|
case 'c': |
| 943 |
|
|
outfmt = DTrgbe; |
| 944 |
|
|
break; |
| 945 |
|
|
default: |
| 946 |
|
|
goto userr; |
| 947 |
|
|
} |
| 948 |
|
|
break; |
| 949 |
|
|
case 's': |
| 950 |
|
|
if (n > MAXCOMP) n = MAXCOMP; |
| 951 |
|
|
i += mop[nmats].preop.nsf = |
| 952 |
|
|
get_factors(mop[nmats].preop.sca, |
| 953 |
|
|
n, argv+i+1); |
| 954 |
|
|
if (mop[nmats].preop.nsf <= 0) { |
| 955 |
|
|
fprintf(stderr, "%s: -s missing arguments\n", |
| 956 |
|
|
argv[0]); |
| 957 |
|
|
goto userr; |
| 958 |
|
|
} |
| 959 |
|
|
break; |
| 960 |
|
|
case 'C': |
| 961 |
greg |
2.23 |
mcat_last = 0; |
| 962 |
greg |
2.1 |
if (!n || isflt(argv[i+1])) |
| 963 |
|
|
goto userr; |
| 964 |
|
|
defCsym = mop[nmats].preop.csym = argv[++i]; |
| 965 |
|
|
mop[nmats].preop.clen = 0; |
| 966 |
|
|
break; |
| 967 |
|
|
case 'c': |
| 968 |
greg |
2.23 |
mcat_last = 0; |
| 969 |
greg |
2.1 |
if (n && !isflt(argv[i+1])) { |
| 970 |
|
|
mop[nmats].preop.csym = argv[++i]; |
| 971 |
|
|
mop[nmats].preop.clen = 0; |
| 972 |
|
|
break; |
| 973 |
|
|
} |
| 974 |
|
|
if (n > MAXCOMP*MAXCOMP) n = MAXCOMP*MAXCOMP; |
| 975 |
|
|
i += mop[nmats].preop.clen = |
| 976 |
|
|
get_factors(mop[nmats].preop.cmat, |
| 977 |
|
|
n, argv+i+1); |
| 978 |
|
|
if (mop[nmats].preop.clen <= 0) { |
| 979 |
|
|
fprintf(stderr, "%s: -c missing arguments\n", |
| 980 |
|
|
argv[0]); |
| 981 |
|
|
goto userr; |
| 982 |
|
|
} |
| 983 |
|
|
mop[nmats].preop.csym = NULL; |
| 984 |
|
|
break; |
| 985 |
|
|
case 'm': |
| 986 |
greg |
2.32 |
if (!n) goto userr; |
| 987 |
greg |
2.23 |
mcat_last = 1; |
| 988 |
greg |
2.32 |
transpose_mcat = (argv[i][2] == 't'); |
| 989 |
greg |
2.1 |
if (argv[++i][0] == '-' && !argv[i][1]) { |
| 990 |
|
|
if (stdin_used++) goto stdin_error; |
| 991 |
|
|
mcat_spec = stdin_name; |
| 992 |
|
|
} else |
| 993 |
|
|
mcat_spec = argv[i]; |
| 994 |
|
|
break; |
| 995 |
|
|
default: |
| 996 |
|
|
fprintf(stderr, "%s: unknown option '%s'\n", |
| 997 |
|
|
argv[0], argv[i]); |
| 998 |
|
|
goto userr; |
| 999 |
|
|
} |
| 1000 |
|
|
} |
| 1001 |
|
|
if (!nmats) { |
| 1002 |
|
|
fprintf(stderr, "%s: need at least one input matrix\n", argv[0]); |
| 1003 |
|
|
goto userr; |
| 1004 |
|
|
} |
| 1005 |
|
|
resize_inparr(nmats+1); /* extra matrix at end for result */ |
| 1006 |
|
|
mop[nmats].inspec = "trailing_ops"; |
| 1007 |
greg |
2.32 |
|
| 1008 |
|
|
if (mcat_spec) { /* load final concatenation matrix? */ |
| 1009 |
|
|
mcat = rmx_load(mcat_spec); |
| 1010 |
|
|
if (!mcat) { |
| 1011 |
|
|
fprintf(stderr, "%s: error loading concatenation matrix: %s\n", |
| 1012 |
|
|
argv[0], mcat_spec); |
| 1013 |
|
|
return(1); |
| 1014 |
|
|
} |
| 1015 |
|
|
if (transpose_mcat && !rmx_transpose(mcat)) { |
| 1016 |
|
|
fprintf(stderr, "%s: error transposing concatenation matrix: %s\n", |
| 1017 |
|
|
argv[0], mcat_spec); |
| 1018 |
|
|
return(1); |
| 1019 |
|
|
} |
| 1020 |
greg |
2.1 |
} |
| 1021 |
|
|
/* get/check inputs, set constants */ |
| 1022 |
|
|
if (!initialize(&mop[nmats].imx)) |
| 1023 |
|
|
return(1); |
| 1024 |
|
|
|
| 1025 |
|
|
for (i = 0; i < n2comp; i++) /* user .cal files and expressions */ |
| 1026 |
|
|
if (argv[comp_ndx[i]][1] == 'f') { |
| 1027 |
|
|
char *fpath = getpath(argv[comp_ndx[i]+1], |
| 1028 |
|
|
getrlibpath(), 0); |
| 1029 |
|
|
if (fpath == NULL) { |
| 1030 |
|
|
fprintf(stderr, "%s: cannot find file '%s'\n", |
| 1031 |
|
|
argv[0], argv[comp_ndx[i]+1]); |
| 1032 |
|
|
return(1); |
| 1033 |
|
|
} |
| 1034 |
|
|
fcompile(fpath); |
| 1035 |
|
|
} else /* (argv[comp_ndx[i]][1] == 'e') */ |
| 1036 |
|
|
scompile(argv[comp_ndx[i]+1], NULL, 0); |
| 1037 |
|
|
|
| 1038 |
|
|
/* get trailing color transform */ |
| 1039 |
|
|
if (!get_component_xfm(&mop[nmats])) |
| 1040 |
|
|
return(1); |
| 1041 |
|
|
/* adjust output dimensions and #components */ |
| 1042 |
|
|
if (mcat) { |
| 1043 |
|
|
if (mop[nmats].imx.ncols != mcat->nrows) { |
| 1044 |
|
|
fprintf(stderr, |
| 1045 |
|
|
"%s: number of input columns does not match number of rows in '%s'\n", |
| 1046 |
|
|
argv[0], mcat_spec); |
| 1047 |
|
|
return(1); |
| 1048 |
|
|
} |
| 1049 |
|
|
if (mcat->ncomp != (mcat_last ? mop[nmats].rmp->ncomp : mop[nmats].imx.ncomp)) { |
| 1050 |
|
|
fprintf(stderr, |
| 1051 |
|
|
"%s: number of components does not match those in '%s'\n", |
| 1052 |
|
|
argv[0], mcat_spec); |
| 1053 |
|
|
return(1); |
| 1054 |
|
|
} |
| 1055 |
|
|
if (!split_input(&mop[nmats])) |
| 1056 |
|
|
return(1); |
| 1057 |
|
|
mop[nmats].rmp->ncols = mcat->ncols; |
| 1058 |
|
|
} |
| 1059 |
greg |
2.30 |
#if DTrmx_native==DTfloat |
| 1060 |
|
|
if (outfmt == DTdouble) |
| 1061 |
|
|
fprintf(stderr, "%s: warning - writing float result as double\n", argv[0]); |
| 1062 |
|
|
#endif |
| 1063 |
greg |
2.1 |
newheader("RADIANCE", stdout); /* write output header */ |
| 1064 |
|
|
if (echoheader) |
| 1065 |
|
|
output_headinfo(stdout); |
| 1066 |
|
|
printargs(argc, argv, stdout); |
| 1067 |
|
|
fputnow(stdout); |
| 1068 |
|
|
mop[nmats].rmp->dtype = rmx_write_header(mop[nmats].rmp, outfmt, stdout); |
| 1069 |
|
|
if (!mop[nmats].rmp->dtype) { |
| 1070 |
|
|
fprintf(stderr, "%s: unsupported output format\n", argv[0]); |
| 1071 |
|
|
return(1); |
| 1072 |
|
|
} |
| 1073 |
greg |
2.7 |
doptimize(1); /* optimize definitions */ |
| 1074 |
greg |
2.22 |
i = spawned_children(nproc); /* create multiple processes if requested */ |
| 1075 |
|
|
if (i > 0) /* running in parent process? */ |
| 1076 |
greg |
2.10 |
return(parent_loop() ? 0 : 1); |
| 1077 |
greg |
2.22 |
if (i < 0) /* running in output process? */ |
| 1078 |
|
|
return(output_loop() ? 0 : 1); |
| 1079 |
|
|
/* else we are a worker process */ |
| 1080 |
greg |
2.10 |
return(combine_input() ? 0 : 1); |
| 1081 |
greg |
2.1 |
stdin_error: |
| 1082 |
|
|
fprintf(stderr, "%s: %s used for more than one input\n", |
| 1083 |
|
|
argv[0], stdin_name); |
| 1084 |
|
|
return(1); |
| 1085 |
|
|
userr: |
| 1086 |
|
|
fprintf(stderr, |
| 1087 |
greg |
2.10 |
"Usage: %s [-h][-f{adfc}][-n nproc][-e expr][-f file][-s sf .. | -c ce ..] m1 .. -m mcat > mres\n", |
| 1088 |
greg |
2.1 |
argv[0]); |
| 1089 |
|
|
return(1); |
| 1090 |
|
|
} |