| 10 |
|
#include "rtio.h" |
| 11 |
|
#include "resolu.h" |
| 12 |
|
#include "platform.h" |
| 13 |
< |
#include "paths.h" |
| 13 |
> |
#include "random.h" |
| 14 |
|
#include "rmatrix.h" |
| 15 |
|
#if !defined(_WIN32) && !defined(_WIN64) |
| 16 |
|
#include <sys/mman.h> |
| 17 |
+ |
#include <sys/wait.h> |
| 18 |
|
#endif |
| 19 |
|
|
| 20 |
+ |
#define VIEWSTR "VIEW=" /* borrowed from common/view.h */ |
| 21 |
+ |
#define VIEWSTRL 5 |
| 22 |
+ |
|
| 23 |
|
int nprocs = 1; /* # of calculation processes (Unix) */ |
| 24 |
|
int in_type = DTfromHeader; /* input data type */ |
| 25 |
|
int out_type = DTfromHeader; /* output data type */ |
| 29 |
|
int iswapped = 0; /* input data is byte-swapped? */ |
| 30 |
|
int ncomp = 3; /* # input components */ |
| 31 |
|
int xres=0, yres=0; /* input image dimensions */ |
| 32 |
+ |
char viewspec[128] = ""; /* VIEW= line from first header */ |
| 33 |
+ |
char pixasp[48] = ""; /* PIXASPECT= line from header */ |
| 34 |
|
|
| 35 |
|
RMATRIX *cmtx = NULL; /* coefficient matrix */ |
| 36 |
|
|
| 73 |
|
ncomp = ncompval(s); |
| 74 |
|
return(1); |
| 75 |
|
} |
| 70 |
– |
if (isexpos(s)) { |
| 71 |
– |
if (fabs(1. - exposval(s)) > 0.04) |
| 72 |
– |
fputs("Warning - ignoring EXPOSURE setting\n", stderr); |
| 73 |
– |
return(1); |
| 74 |
– |
} |
| 76 |
|
if (formatval(fmt, s)) { |
| 77 |
|
for (in_type = DTend; --in_type > DTfromHeader; ) |
| 78 |
|
if (!strcmp(fmt, cm_fmt_id[in_type])) |
| 84 |
|
iswapped = (i != nativebigendian()); |
| 85 |
|
return(1); |
| 86 |
|
} |
| 87 |
+ |
if (!strncmp(s, VIEWSTR, VIEWSTRL)) { |
| 88 |
+ |
strcpy(viewspec, s); |
| 89 |
+ |
return(1); |
| 90 |
+ |
} |
| 91 |
+ |
if (isaspect(s)) { |
| 92 |
+ |
strcpy(pixasp, s); |
| 93 |
+ |
return(1); |
| 94 |
+ |
} |
| 95 |
|
return(0); |
| 96 |
|
} |
| 97 |
|
|
| 163 |
|
int |
| 164 |
|
checkline(char *s, void *p) |
| 165 |
|
{ |
| 166 |
< |
int *xyres = (int *)p; |
| 167 |
< |
char fmt[MAXFMTLEN]; |
| 166 |
> |
static int exposWarned = 0; |
| 167 |
> |
int *xyres = (int *)p; |
| 168 |
> |
char fmt[MAXFMTLEN]; |
| 169 |
|
|
| 170 |
|
if (!strncmp(s, "NCOLS=", 6)) { |
| 171 |
|
xyres[0] = atoi(s+6); |
| 185 |
|
return(1); |
| 186 |
|
} |
| 187 |
|
if (isexpos(s)) { |
| 188 |
< |
if (fabs(1. - exposval(s)) > 0.04) |
| 189 |
< |
fputs("Warning - ignoring EXPOSURE setting\n", stderr); |
| 188 |
> |
if (!exposWarned && fabs(1. - exposval(s)) > 0.04) { |
| 189 |
> |
fputs("Warning - ignoring EXPOSURE setting(s)\n", |
| 190 |
> |
stderr); |
| 191 |
> |
exposWarned++; |
| 192 |
> |
} |
| 193 |
|
return(1); |
| 194 |
|
} |
| 195 |
|
if (formatval(fmt, s)) { |
| 240 |
|
if (!ospec) { |
| 241 |
|
ospec = "<stdout>"; |
| 242 |
|
fp = stdout; |
| 230 |
– |
SET_FILE_BINARY(fp); |
| 243 |
|
} else if (ospec[0] == '!') { |
| 244 |
|
if (!(fp = popen(ospec+1, "w"))) { |
| 245 |
|
fprintf(stderr, "Cannot start: %s\n", ospec); |
| 246 |
|
return(NULL); |
| 247 |
|
} |
| 248 |
< |
SET_FILE_BINARY(fp); |
| 237 |
< |
} else if (!(fp = fopen(ospec, "wb"))) { |
| 248 |
> |
} else if (!(fp = fopen(ospec, "w"))) { |
| 249 |
|
fprintf(stderr, "%s: cannot open for writing\n", ospec); |
| 250 |
|
return(NULL); |
| 251 |
|
} |
| 252 |
+ |
SET_FILE_BINARY(fp); |
| 253 |
|
newheader("RADIANCE", fp); |
| 254 |
< |
fputnow(fp); |
| 254 |
> |
if (cmtx->info) /* prepend matrix metadata */ |
| 255 |
> |
fputs(cmtx->info, fp); |
| 256 |
> |
else |
| 257 |
> |
fputnow(fp); |
| 258 |
|
if (fno >= 0) |
| 259 |
|
fprintf(fp, "FRAME=%d\n", fno); |
| 260 |
+ |
if (viewspec[0]) |
| 261 |
+ |
fputs(viewspec, fp); |
| 262 |
+ |
if (pixasp[0]) |
| 263 |
+ |
fputs(pixasp, fp); |
| 264 |
|
switch (out_type) { |
| 265 |
|
case DTfloat: |
| 266 |
|
case DTdouble: |
| 396 |
|
return(0); |
| 397 |
|
} |
| 398 |
|
|
| 399 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
| 400 |
+ |
#define multi_process solo_process |
| 401 |
+ |
#else |
| 402 |
+ |
|
| 403 |
+ |
/* allocate a scrambled index array of the specified length */ |
| 404 |
+ |
int * |
| 405 |
+ |
scramble(int n) |
| 406 |
+ |
{ |
| 407 |
+ |
int *scarr = (int *)malloc(sizeof(int)*n); |
| 408 |
+ |
int i; |
| 409 |
+ |
|
| 410 |
+ |
if (!scarr) { |
| 411 |
+ |
fprintf(stderr, "Out of memory in scramble(%d)\n", n); |
| 412 |
+ |
exit(1); |
| 413 |
+ |
} |
| 414 |
+ |
for (i = n; i--; ) |
| 415 |
+ |
scarr[i] = i; |
| 416 |
+ |
/* perform Fisher-Yates shuffle */ |
| 417 |
+ |
for (i = 0; i < n-1; i++) { |
| 418 |
+ |
int ix = irandom(n-i) + i; |
| 419 |
+ |
int ndx = scarr[i]; |
| 420 |
+ |
scarr[i] = scarr[ix]; |
| 421 |
+ |
scarr[ix] = ndx; |
| 422 |
+ |
} |
| 423 |
+ |
return(scarr); |
| 424 |
+ |
} |
| 425 |
+ |
|
| 426 |
|
/* run calculation on multiple processes, using memory maps and fork() */ |
| 427 |
|
int |
| 428 |
|
multi_process(void) |
| 429 |
|
{ |
| 384 |
– |
#if defined(_WIN32) || defined(_WIN64) |
| 385 |
– |
fputs("Bad call to multi_process()\n", stderr); |
| 386 |
– |
return(0); |
| 387 |
– |
#else |
| 430 |
|
int coff = nprocs; |
| 431 |
|
int odd = 0; |
| 432 |
|
char fbuf[512]; |
| 433 |
|
float *osum; |
| 434 |
+ |
int *syarr; |
| 435 |
|
int c; |
| 436 |
|
/* sanity check */ |
| 437 |
|
if (sizeof(float) != sizeof(COLORV)) { |
| 452 |
|
xres, yres, ncomp); |
| 453 |
|
return(0); |
| 454 |
|
} |
| 455 |
+ |
srandom(113*coff + 5669); /* randomize row access for this process */ |
| 456 |
+ |
syarr = scramble(yres); |
| 457 |
|
/* run through our unique set of columns */ |
| 458 |
|
for (c = coff; c < cmtx->ncols; c += nprocs) { |
| 459 |
|
FILE *fout; |
| 464 |
|
while (rc-- > 0) { /* map & sum each input file */ |
| 465 |
|
const int r = odd ? rc : cmtx->nrows-1 - rc; |
| 466 |
|
const rmx_dtype *cval = rmx_val(cmtx, r, c); |
| 467 |
< |
long dstart, n; |
| 467 |
> |
long dstart; |
| 468 |
|
size_t maplen; |
| 469 |
|
void *imap; |
| 470 |
|
FILE *finp; |
| 471 |
|
float *dst; |
| 472 |
< |
int i; |
| 472 |
> |
int i, x; |
| 473 |
|
for (i = ncomp; i--; ) |
| 474 |
|
if (cval[i] != 0) break; |
| 475 |
|
if (i < 0) /* this coefficient is zero, skip */ |
| 488 |
|
return(0); |
| 489 |
|
} |
| 490 |
|
i = in_type==DTfloat ? ncomp*(int)sizeof(float) : ncomp+1; |
| 491 |
< |
maplen = dstart + yres*xres*i; |
| 491 |
> |
maplen = dstart + (size_t)yres*xres*i; |
| 492 |
|
imap = mmap(NULL, maplen, PROT_READ, |
| 493 |
|
MAP_FILE|MAP_SHARED, fileno(finp), 0); |
| 494 |
< |
fclose(finp); /* will load from map */ |
| 494 |
> |
fclose(finp); /* will read from map (randomly) */ |
| 495 |
|
if (imap == MAP_FAILED) { |
| 496 |
|
fprintf(stderr, "%s: unable to map input file\n", fbuf); |
| 497 |
|
return(0); |
| 498 |
|
} |
| 499 |
< |
dst = osum; /* -> weighted image sum */ |
| 500 |
< |
if (in_type == DTfloat) { |
| 501 |
< |
const float *fvp = (float *)((char *)imap + dstart); |
| 502 |
< |
for (n = (long)yres*xres; n-- > 0; |
| 503 |
< |
dst += ncomp, fvp += ncomp) |
| 504 |
< |
for (i = ncomp; i--; ) |
| 505 |
< |
dst[i] += cval[i]*fvp[i]; |
| 506 |
< |
} else { |
| 462 |
< |
const COLRV *cvp = (COLRV *)((char *)imap + dstart); |
| 463 |
< |
for (n = (long)yres*xres; n-- > 0; |
| 464 |
< |
dst += ncomp, cvp += ncomp+1) { |
| 465 |
< |
const rmx_dtype fe = cxponent[cvp[ncomp]]; |
| 466 |
< |
for (i = ncomp; i--; ) |
| 467 |
< |
dst[i] += cval[i]*(cvp[i]+(rmx_dtype).5)*fe; |
| 499 |
> |
if (in_type == DTfloat) |
| 500 |
> |
for (y = yres; y-- > 0; ) { |
| 501 |
> |
const float *fvp = (float *)((char *)imap + dstart) + |
| 502 |
> |
(size_t)ncomp*xres*syarr[y]; |
| 503 |
> |
dst = osum + (size_t)ncomp*xres*syarr[y]; |
| 504 |
> |
for (x = xres; x-- > 0; dst += ncomp, fvp += ncomp) |
| 505 |
> |
for (i = ncomp; i--; ) |
| 506 |
> |
dst[i] += cval[i]*fvp[i]; |
| 507 |
|
} |
| 508 |
< |
} |
| 508 |
> |
else |
| 509 |
> |
for (y = yres; y-- > 0; ) { |
| 510 |
> |
const COLRV *cvp = (COLRV *)((char *)imap + dstart) + |
| 511 |
> |
(ncomp+1L)*xres*syarr[y]; |
| 512 |
> |
dst = osum + (size_t)ncomp*xres*syarr[y]; |
| 513 |
> |
for (x = xres; x-- > 0; dst += ncomp, cvp += ncomp+1) { |
| 514 |
> |
const rmx_dtype fe = cxponent[cvp[ncomp]]; |
| 515 |
> |
for (i = ncomp; i--; ) |
| 516 |
> |
dst[i] += cval[i]*(cvp[i]+(rmx_dtype).5)*fe; |
| 517 |
> |
} |
| 518 |
> |
} |
| 519 |
|
munmap(imap, maplen); |
| 520 |
< |
} /* write out accumulated column result */ |
| 520 |
> |
} /* write accumulated column picture/matrix */ |
| 521 |
|
sprintf(fbuf, out_spec, c); |
| 522 |
|
fout = open_output(fbuf, c); |
| 523 |
|
if (!fout) |
| 541 |
|
odd = !odd; /* go back & forth to milk page cache */ |
| 542 |
|
} |
| 543 |
|
free(osum); |
| 544 |
< |
if (coff) /* children return here... */ |
| 544 |
> |
free(syarr); |
| 545 |
> |
if (coff) /* child processes return here... */ |
| 546 |
|
return(1); |
| 547 |
|
c = 0; /* ...but parent waits for children */ |
| 548 |
|
while (++coff < nprocs) { |
| 555 |
|
writerr: |
| 556 |
|
fprintf(stderr, "%s: write error\n", fbuf); |
| 557 |
|
return(0); |
| 508 |
– |
#endif |
| 558 |
|
} |
| 559 |
|
|
| 560 |
+ |
#endif /* ! Windows */ |
| 561 |
+ |
|
| 562 |
|
int |
| 563 |
|
main(int argc, char *argv[]) |
| 564 |
|
{ |
| 595 |
|
if ((argc-a < 1) | (argc-a > 2) || argv[a][0] == '-') |
| 596 |
|
goto userr; |
| 597 |
|
in_spec = argv[a]; |
| 598 |
< |
cmtx = rmx_load(argv[a+1], RMPnone); /* may load from stdin */ |
| 598 |
> |
cmtx = rmx_load(argv[a+1]); /* loads from stdin if a+1==argc */ |
| 599 |
|
if (cmtx == NULL) |
| 600 |
|
return(1); /* error reported */ |
| 601 |
+ |
if (nprocs > cmtx->ncols) |
| 602 |
+ |
nprocs = cmtx->ncols; |
| 603 |
|
#if defined(_WIN32) || defined(_WIN64) |
| 604 |
|
if (nprocs > 1) { |
| 605 |
|
fprintf(stderr, "%s: warning - Windows only allows -N 1\n", argv[0]); |
| 606 |
|
nprocs = 1; |
| 607 |
|
} |
| 608 |
|
#else |
| 556 |
– |
if (nprocs > cmtx->ncols) |
| 557 |
– |
nprocs = cmtx->ncols; |
| 609 |
|
if ((nprocs > 1) & !out_spec) { |
| 610 |
|
fprintf(stderr, "%s: multi-processing result cannot go to stdout\n", |
| 611 |
|
argv[0]); |