38 |
|
CMATRIX *cm; |
39 |
|
|
40 |
|
if ((nrows <= 0) | (ncols <= 0)) |
41 |
< |
return(NULL); |
41 |
> |
error(USER, "attempt to create empty matrix"); |
42 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
43 |
|
3*sizeof(COLORV)*(nrows*ncols - 1)); |
44 |
|
if (cm == NULL) |
108 |
|
CMATRIX *cm; |
109 |
|
FILE *fp = stdin; |
110 |
|
|
111 |
+ |
if (ncols <= 0) |
112 |
+ |
error(USER, "Non-positive number of columns"); |
113 |
|
if (fname == NULL) |
114 |
|
fname = "<stdin>"; |
115 |
|
else if ((fp = fopen(fname, "r")) == NULL) { |
334 |
|
|
335 |
|
/* Sum together a set of images and write result to stdout */ |
336 |
|
static int |
337 |
< |
sum_images(const char *fspec, const CMATRIX *cv) |
337 |
> |
sum_images(const char *fspec, const CMATRIX *cv, FILE *fp) |
338 |
|
{ |
339 |
|
int myDT = DTfromHeader; |
340 |
|
CMATRIX *pmat; |
372 |
|
pmat = cm_alloc(myYR, myXR); |
373 |
|
memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR); |
374 |
|
/* finish header */ |
375 |
< |
fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, stdout); |
376 |
< |
fputc('\n', stdout); |
377 |
< |
fprtresolu(myXR, myYR, stdout); |
378 |
< |
fflush(stdout); |
375 |
> |
fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fp); |
376 |
> |
fputc('\n', fp); |
377 |
> |
fprtresolu(myXR, myYR, fp); |
378 |
> |
fflush(fp); |
379 |
|
} else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) { |
380 |
|
sprintf(errmsg, "picture '%s' format/size mismatch", |
381 |
|
fname); |
400 |
|
free(scanline); |
401 |
|
/* write scanlines */ |
402 |
|
for (y = 0; y < myYR; y++) |
403 |
< |
if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, stdout) < 0) |
403 |
> |
if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fp) < 0) |
404 |
|
return(0); |
405 |
|
cm_free(pmat); /* all done */ |
406 |
< |
return(fflush(stdout) == 0); |
406 |
> |
return(fflush(fp) == 0); |
407 |
|
} |
408 |
|
|
409 |
< |
/* check to see if a string contains a %d specification */ |
410 |
< |
int |
411 |
< |
hasDecimalSpec(const char *s) |
409 |
> |
/* check to see if a string contains a %d or %o specification */ |
410 |
> |
static int |
411 |
> |
hasNumberFormat(const char *s) |
412 |
|
{ |
413 |
|
while (*s && *s != '%') |
414 |
|
s++; |
418 |
|
++s; |
419 |
|
while (isdigit(*s)); |
420 |
|
|
421 |
< |
return(*s == 'd'); |
421 |
> |
return(*s == 'd' | *s == 'i' | *s == 'o' | *s == 'x' | *s == 'X'); |
422 |
|
} |
423 |
|
|
424 |
|
int |
425 |
|
main(int argc, char *argv[]) |
426 |
|
{ |
427 |
< |
CMATRIX *tvec, *Dmat, *Tmat, *ivec, *cvec; |
426 |
< |
struct BSDF_data *btdf; |
427 |
> |
CMATRIX *cvec; |
428 |
|
|
429 |
|
progname = argv[0]; |
430 |
|
|
431 |
< |
if ((argc < 4) | (argc > 5)) { |
432 |
< |
fprintf(stderr, "Usage: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n", |
433 |
< |
progname); |
431 |
> |
if ((argc < 2) | (argc > 5)) { |
432 |
> |
fprintf(stderr, "Usage: %s DCspec [tregvec]\n", progname); |
433 |
> |
fprintf(stderr, " or: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n", |
434 |
> |
progname); |
435 |
|
return(1); |
436 |
|
} |
437 |
< |
/* load Tregenza vector */ |
438 |
< |
tvec = cm_load(argv[4], 0, 1, DTascii); /* argv[4]==NULL iff argc==4 */ |
439 |
< |
/* load BTDF */ |
440 |
< |
btdf = load_BSDF(argv[2]); |
441 |
< |
if (btdf == NULL) |
442 |
< |
return(1); |
437 |
> |
|
438 |
> |
if (argc > 3) { /* need to compute DC matrix? */ |
439 |
> |
CMATRIX *svec, *Dmat, *Tmat, *ivec; |
440 |
> |
struct BSDF_data *btdf; |
441 |
> |
/* get sky vector */ |
442 |
> |
svec = cm_load(argv[4], 0, 1, DTascii); |
443 |
> |
/* load BSDF */ |
444 |
> |
btdf = load_BSDF(argv[2]); |
445 |
> |
if (btdf == NULL) |
446 |
> |
return(1); |
447 |
|
/* load Daylight matrix */ |
448 |
< |
Dmat = cm_load(argv[3], btdf->ninc, tvec->nrows, DTfromHeader); |
448 |
> |
Dmat = cm_load(argv[3], btdf->ninc, svec->nrows, DTfromHeader); |
449 |
|
/* multiply vector through */ |
450 |
< |
ivec = cm_multiply(Dmat, tvec); |
451 |
< |
cm_free(Dmat); cm_free(tvec); |
452 |
< |
Tmat = cm_bsdf(btdf); /* convert BTDF to matrix */ |
453 |
< |
free_BSDF(btdf); |
454 |
< |
cvec = cm_multiply(Tmat, ivec); /* cvec = component vector */ |
455 |
< |
cm_free(Tmat); cm_free(ivec); |
456 |
< |
if (hasDecimalSpec(argv[1])) { /* generating image */ |
450 |
> |
ivec = cm_multiply(Dmat, svec); |
451 |
> |
cm_free(Dmat); cm_free(svec); |
452 |
> |
Tmat = cm_bsdf(btdf); /* convert BTDF to matrix */ |
453 |
> |
free_BSDF(btdf); |
454 |
> |
cvec = cm_multiply(Tmat, ivec); /* cvec = component vector */ |
455 |
> |
cm_free(Tmat); cm_free(ivec); |
456 |
> |
} else { /* else just use sky vector */ |
457 |
> |
cvec = cm_load(argv[2], 0, 1, DTascii); |
458 |
> |
} |
459 |
> |
|
460 |
> |
if (hasNumberFormat(argv[1])) { /* generating image */ |
461 |
|
SET_FILE_BINARY(stdout); |
462 |
|
newheader("RADIANCE", stdout); |
463 |
|
printargs(argc, argv, stdout); |
464 |
|
fputnow(stdout); |
465 |
< |
if (!sum_images(argv[1], cvec)) |
465 |
> |
if (!sum_images(argv[1], cvec, stdout)) |
466 |
|
return(1); |
467 |
|
} else { /* generating vector */ |
468 |
|
CMATRIX *Vmat = cm_load(argv[1], 0, cvec->nrows, DTfromHeader); |