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) { |
162 |
|
int maxrow = (nrows > 0 ? nrows : 32000); |
163 |
|
int r, c; |
164 |
|
for (r = 0; r < maxrow; r++) { |
165 |
< |
if (r >= cm->nrows) /* need more space? */ |
165 |
> |
if (r >= cm->nrows) /* need more space? */ |
166 |
|
cm = cm_resize(cm, 2*cm->nrows); |
167 |
|
for (c = 0; c < ncols; c++) { |
168 |
|
COLORV *cv = cm_lval(cm,r,c); |
169 |
|
if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) |
170 |
< |
if ((nrows <= 0) & (r > 0) & (c == 0)) { |
170 |
> |
if ((nrows <= 0) & (r > 0) & !c) { |
171 |
|
cm = cm_resize(cm, maxrow=r); |
172 |
|
break; |
173 |
|
} else |
195 |
|
if (nread == cm->nrows*cm->ncols) |
196 |
|
/* need more space? */ |
197 |
|
cm = cm_resize(cm, 2*cm->nrows); |
198 |
< |
else if (nread % cm->ncols == 0) |
198 |
> |
else if (nread && !(nread % cm->ncols)) |
199 |
|
/* seem to be done */ |
200 |
|
cm = cm_resize(cm, nread/cm->ncols); |
201 |
|
else /* ended mid-row */ |
296 |
|
cm_bsdf(const struct BSDF_data *bsdf) |
297 |
|
{ |
298 |
|
CMATRIX *cm = cm_alloc(bsdf->nout, bsdf->ninc); |
299 |
< |
COLORV *mp = cm->cmem; |
299 |
> |
int nbadohm = 0; |
300 |
> |
int nneg = 0; |
301 |
|
int r, c; |
302 |
|
|
303 |
< |
for (r = 0; r < cm->nrows; r++) |
304 |
< |
for (c = 0; c < cm->ncols; c++, mp += 3) |
305 |
< |
mp[0] = mp[1] = mp[2] = BSDF_value(bsdf,c,r); |
303 |
> |
for (c = 0; c < cm->ncols; c++) { |
304 |
> |
float dom = getBSDF_incohm(bsdf,c); |
305 |
> |
FVECT v; |
306 |
> |
|
307 |
> |
if (dom <= .0) { |
308 |
> |
nbadohm++; |
309 |
> |
continue; |
310 |
> |
} |
311 |
> |
if (!getBSDF_incvec(v,bsdf,c) || v[2] > FTINY) |
312 |
> |
error(USER, "illegal incoming BTDF direction"); |
313 |
> |
dom *= -v[2]; |
314 |
> |
|
315 |
> |
for (r = 0; r < cm->nrows; r++) { |
316 |
> |
float f = BSDF_value(bsdf,c,r); |
317 |
> |
COLORV *mp = cm_lval(cm,r,c); |
318 |
> |
|
319 |
> |
if (f <= .0) { |
320 |
> |
nneg += (f < -FTINY); |
321 |
> |
f = .0f; |
322 |
> |
} |
323 |
> |
mp[0] = mp[1] = mp[2] = f * dom; |
324 |
> |
} |
325 |
> |
} |
326 |
> |
if (nneg || nbadohm) { |
327 |
> |
sprintf(errmsg, |
328 |
> |
"BTDF has %d negatives and %d bad incoming solid angles", |
329 |
> |
nneg, nbadohm); |
330 |
> |
error(WARNING, errmsg); |
331 |
> |
} |
332 |
|
return(cm); |
333 |
|
} |
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; |
399 |
< |
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 |
< |
tvec = cm_load(argv[4], 0, 1, DTascii); /* argv[4]==NULL iff argc==4 */ |
438 |
< |
Dmat = cm_load(argv[3], 0, tvec->nrows, DTfromHeader); |
439 |
< |
btdf = load_BSDF(argv[2]); |
440 |
< |
if (btdf == NULL) |
441 |
< |
return(1); |
442 |
< |
if (btdf->ninc != Dmat->nrows) { |
443 |
< |
sprintf(errmsg, "Incoming BTDF dir (%d) mismatch to D (%d)", |
444 |
< |
btdf->ninc, Dmat->nrows); |
445 |
< |
error(USER, errmsg); |
446 |
< |
} |
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, 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); |