26 |
|
int i, y; |
27 |
|
|
28 |
|
if (cv->ncols != 1) |
29 |
< |
error(INTERNAL, "expected vector in sum_images()"); |
29 |
> |
error(INTERNAL, "expected matrix in sum_images()"); |
30 |
|
for (i = 0; i < cv->nrows; i++) { |
31 |
|
const COLORV *scv = cv_lval(cv,i); |
32 |
|
char fname[1024]; |
33 |
|
FILE *fp; |
34 |
|
int dt, xr, yr; |
35 |
|
COLORV *psp; |
36 |
+ |
char *err; |
37 |
|
/* check for zero */ |
38 |
|
if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) && |
39 |
|
(myDT != DTfromHeader) | (i < cv->nrows-1)) |
40 |
|
continue; |
41 |
|
/* open next picture */ |
42 |
|
sprintf(fname, fspec, i); |
43 |
< |
if ((fp = fopen(fname, "r")) == NULL) { |
43 |
> |
if ((fp = fopen(fname, "rb")) == NULL) { |
44 |
|
sprintf(errmsg, "cannot open picture '%s'", fname); |
45 |
|
error(SYSTEM, errmsg); |
46 |
|
} |
47 |
< |
SET_FILE_BINARY(fp); |
48 |
< |
dt = getDTfromHeader(fp); |
47 |
> |
dt = DTfromHeader; |
48 |
> |
if ((err = cm_getheader(&dt, NULL, NULL, fp)) != NULL) |
49 |
> |
error(USER, err); |
50 |
|
if ((dt != DTrgbe) & (dt != DTxyze) || |
51 |
|
!fscnresolu(&xr, &yr, fp)) { |
52 |
|
sprintf(errmsg, "file '%s' not a picture", fname); |
61 |
|
pmat = cm_alloc(myYR, myXR); |
62 |
|
memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR); |
63 |
|
/* finish header */ |
64 |
< |
fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fout); |
64 |
> |
fputformat((char *)cm_fmt_id[myDT], fout); |
65 |
|
fputc('\n', fout); |
64 |
– |
fprtresolu(myXR, myYR, fout); |
66 |
|
fflush(fout); |
67 |
|
} else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) { |
68 |
|
sprintf(errmsg, "picture '%s' format/size mismatch", |
86 |
|
fclose(fp); /* done this picture */ |
87 |
|
} |
88 |
|
free(scanline); |
89 |
< |
/* write scanlines */ |
90 |
< |
for (y = 0; y < myYR; y++) |
91 |
< |
if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fout) < 0) |
91 |
< |
return(0); |
92 |
< |
cm_free(pmat); /* all done */ |
93 |
< |
return(fflush(fout) == 0); |
89 |
> |
i = cm_write(pmat, myDT, fout); /* write picture */ |
90 |
> |
cm_free(pmat); /* free data */ |
91 |
> |
return(i); |
92 |
|
} |
93 |
|
|
94 |
|
/* check to see if a string contains a %d or %o specification */ |
120 |
|
main(int argc, char *argv[]) |
121 |
|
{ |
122 |
|
int skyfmt = DTascii; |
123 |
+ |
int outfmt = DTascii; |
124 |
|
int nsteps = 1; |
125 |
|
char *ofspec = NULL; |
126 |
|
FILE *ofp = stdout; |
134 |
|
switch (argv[a][1]) { |
135 |
|
case 'n': |
136 |
|
nsteps = atoi(argv[++a]); |
137 |
< |
if (nsteps <= 0) |
137 |
> |
if (nsteps < 0) |
138 |
|
goto userr; |
139 |
|
break; |
141 |
– |
case 'o': |
142 |
– |
ofspec = argv[++a]; |
143 |
– |
break; |
140 |
|
case 'i': |
141 |
|
switch (argv[a][2]) { |
142 |
|
case 'f': |
148 |
|
case 'a': |
149 |
|
skyfmt = DTascii; |
150 |
|
break; |
151 |
+ |
case 'h': |
152 |
+ |
skyfmt = DTfromHeader; |
153 |
+ |
break; |
154 |
|
default: |
155 |
|
goto userr; |
156 |
|
} |
157 |
|
break; |
158 |
+ |
case 'o': |
159 |
+ |
switch (argv[a][2]) { |
160 |
+ |
case '\0': /* output specification (not format) */ |
161 |
+ |
ofspec = argv[++a]; |
162 |
+ |
break; |
163 |
+ |
case 'f': |
164 |
+ |
outfmt = DTfloat; |
165 |
+ |
break; |
166 |
+ |
case 'd': |
167 |
+ |
outfmt = DTdouble; |
168 |
+ |
break; |
169 |
+ |
case 'a': |
170 |
+ |
outfmt = DTascii; |
171 |
+ |
break; |
172 |
+ |
default: |
173 |
+ |
goto userr; |
174 |
+ |
} |
175 |
+ |
break; |
176 |
|
default: |
177 |
|
goto userr; |
178 |
|
} |
181 |
|
|
182 |
|
if (argc-a > 2) { /* VTDs expression */ |
183 |
|
CMATRIX *smtx, *Dmat, *Tmat, *imtx; |
167 |
– |
COLOR tLamb; |
184 |
|
/* get sky vector/matrix */ |
185 |
|
smtx = cm_load(argv[a+3], 0, nsteps, skyfmt); |
186 |
|
/* load BSDF */ |
187 |
< |
Tmat = cm_loadBSDF(argv[a+1], tLamb); |
187 |
> |
Tmat = cm_loadBTDF(argv[a+1]); |
188 |
|
/* load Daylight matrix */ |
189 |
|
Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols, |
190 |
|
smtx->nrows, DTfromHeader); |
191 |
|
/* multiply vector through */ |
192 |
|
imtx = cm_multiply(Dmat, smtx); |
193 |
|
cm_free(Dmat); cm_free(smtx); |
194 |
< |
if (Tmat == NULL) { /* diffuse only */ |
195 |
< |
cmtx = cm_scale(imtx, tLamb); |
180 |
< |
} else { /* else apply BTDF matrix */ |
181 |
< |
cmtx = cm_multiply(Tmat, imtx); |
182 |
< |
cm_free(Tmat); |
183 |
< |
} |
194 |
> |
cmtx = cm_multiply(Tmat, imtx); |
195 |
> |
cm_free(Tmat); |
196 |
|
cm_free(imtx); |
197 |
|
} else { /* sky vector/matrix only */ |
198 |
|
cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt); |
252 |
|
CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader); |
253 |
|
CMATRIX *rmtx = cm_multiply(Vmat, cmtx); |
254 |
|
cm_free(Vmat); |
255 |
< |
if (ofspec != NULL) /* multiple vector files? */ |
255 |
> |
if (ofspec != NULL) { /* multiple vector files? */ |
256 |
> |
const char *wtype = (outfmt==DTascii) ? "w" : "wb"; |
257 |
|
for (i = 0; i < nsteps; i++) { |
258 |
|
CMATRIX *rvec = cm_column(rmtx, i); |
259 |
|
sprintf(fnbuf, ofspec, i+1); |
260 |
< |
if ((ofp = fopen(fnbuf, "w")) == NULL) { |
260 |
> |
if ((ofp = fopen(fnbuf, wtype)) == NULL) { |
261 |
|
fprintf(stderr, |
262 |
|
"%s: cannot open '%s' for output\n", |
263 |
|
progname, fnbuf); |
264 |
|
return(1); |
265 |
|
} |
266 |
< |
cm_print(rvec, ofp); |
266 |
> |
#ifdef getc_unlocked |
267 |
> |
flockfile(ofp); |
268 |
> |
#endif |
269 |
> |
cm_write(rvec, outfmt, ofp); |
270 |
|
if (fclose(ofp) == EOF) { |
271 |
|
fprintf(stderr, |
272 |
|
"%s: error writing to '%s'\n", |
276 |
|
ofp = stdout; |
277 |
|
cm_free(rvec); |
278 |
|
} |
279 |
< |
else |
280 |
< |
cm_print(rmtx, ofp); |
279 |
> |
} else { |
280 |
> |
#ifdef getc_unlocked |
281 |
> |
flockfile(ofp); |
282 |
> |
#endif |
283 |
> |
if (outfmt != DTascii) |
284 |
> |
SET_FILE_BINARY(ofp); |
285 |
> |
if (rmtx->ncols > 1) { /* header if actual matrix */ |
286 |
> |
newheader("RADIANCE", ofp); |
287 |
> |
printargs(argc, argv, ofp); |
288 |
> |
fputnow(ofp); |
289 |
> |
fprintf(ofp, "NROWS=%d\n", rmtx->nrows); |
290 |
> |
fprintf(ofp, "NCOLS=%d\n", rmtx->ncols); |
291 |
> |
fputs("NCOMP=3\n", ofp); |
292 |
> |
fputformat((char *)cm_fmt_id[outfmt], ofp); |
293 |
> |
fputc('\n', ofp); |
294 |
> |
} |
295 |
> |
cm_write(rmtx, outfmt, ofp); |
296 |
> |
} |
297 |
|
cm_free(rmtx); |
298 |
|
} |
299 |
|
if (fflush(ofp) == EOF) { /* final clean-up */ |
303 |
|
cm_free(cmtx); |
304 |
|
return(0); |
305 |
|
userr: |
306 |
< |
fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}] DCspec [skyf]\n", |
306 |
> |
fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] DCspec [skyf]\n", |
307 |
|
progname); |
308 |
< |
fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n", |
308 |
> |
fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n", |
309 |
|
progname); |
310 |
|
return(1); |
311 |
|
} |