14 |
|
#include "platform.h" |
15 |
|
#include "resolu.h" |
16 |
|
|
17 |
– |
char *progname; /* global argv[0] */ |
18 |
– |
|
17 |
|
/* Sum together a set of images and write result to fout */ |
18 |
|
static int |
19 |
|
sum_images(const char *fspec, const CMATRIX *cv, FILE *fout) |
20 |
|
{ |
21 |
+ |
static int runcnt = 0; |
22 |
|
int myDT = DTfromHeader; |
23 |
|
COLR *scanline = NULL; |
24 |
|
CMATRIX *pmat = NULL; |
27 |
|
|
28 |
|
if (cv->ncols != 1) |
29 |
|
error(INTERNAL, "expected vector in sum_images()"); |
30 |
< |
for (i = 0; i < cv->nrows; i++) { |
31 |
< |
const COLORV *scv = cv_lval(cv,i); |
30 |
> |
for (i = cv->nrows; i-- > 0; ) { |
31 |
> |
const int r = runcnt&1 ? i : cv->nrows-1 - i; |
32 |
> |
const COLORV *scv = cv_lval(cv,r); |
33 |
|
int flat_file = 0; |
34 |
|
char fname[1024]; |
35 |
|
FILE *fp; |
39 |
|
char *err; |
40 |
|
/* check for zero */ |
41 |
|
if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) && |
42 |
< |
(myDT != DTfromHeader) | (i < cv->nrows-1)) |
42 |
> |
(myDT != DTfromHeader) | (i > 0)) |
43 |
|
continue; |
44 |
|
/* open next picture */ |
45 |
< |
sprintf(fname, fspec, i); |
45 |
> |
sprintf(fname, fspec, r); |
46 |
|
if ((fp = fopen(fname, "rb")) == NULL) { |
47 |
|
sprintf(errmsg, "cannot open picture '%s'", fname); |
48 |
|
error(SYSTEM, errmsg); |
49 |
|
} |
50 |
+ |
#ifdef getc_unlocked |
51 |
+ |
flockfile(fp); |
52 |
+ |
#endif |
53 |
|
dt = DTfromHeader; |
54 |
|
if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL) |
55 |
|
error(USER, err); |
77 |
|
} |
78 |
|
/* flat file check */ |
79 |
|
if ((data_start = ftell(fp)) > 0 && fseek(fp, 0L, SEEK_END) == 0) { |
80 |
< |
flat_file = (ftell(fp) == data_start + sizeof(COLR)*xr*yr); |
80 |
> |
flat_file = (ftell(fp) >= data_start + sizeof(COLR)*xr*yr); |
81 |
|
if (fseek(fp, data_start, SEEK_SET) < 0) { |
82 |
|
sprintf(errmsg, "cannot seek on picture '%s'", fname); |
83 |
|
error(SYSTEM, errmsg); |
107 |
|
free(scanline); |
108 |
|
i = cm_write(pmat, myDT, fout); /* write picture */ |
109 |
|
cm_free(pmat); /* free data */ |
110 |
+ |
++runcnt; /* for zig-zagging */ |
111 |
|
return(i); |
112 |
|
} |
113 |
|
|
114 |
+ |
/* adjust matrix dimensions according to user size(s) */ |
115 |
+ |
static int |
116 |
+ |
alt_dim(CMATRIX *cm, int nr, int nc) |
117 |
+ |
{ |
118 |
+ |
if ((nr <= 0) & (nc <= 0)) |
119 |
+ |
return(0); |
120 |
+ |
if ((nr == cm->nrows) & (nc == cm->ncols)) |
121 |
+ |
return(0); |
122 |
+ |
if (nr > 0) { |
123 |
+ |
if (nc <= 0) |
124 |
+ |
nc = cm->nrows*cm->ncols/nr; |
125 |
+ |
if (nr*nc != cm->nrows*cm->ncols) { |
126 |
+ |
fprintf(stderr, "Bad dimensions: %dx%d != %dx%d\n", |
127 |
+ |
nr, nc, cm->nrows, cm->ncols); |
128 |
+ |
return(-1); |
129 |
+ |
} |
130 |
+ |
} else /* nc > 0 */ { |
131 |
+ |
nr = cm->nrows*cm->ncols/nc; |
132 |
+ |
if (nc*nr != cm->nrows*cm->ncols) { |
133 |
+ |
fprintf(stderr, "Bad dimensions: %d does not divide %dx%d evenly\n", |
134 |
+ |
nc, cm->nrows, cm->ncols); |
135 |
+ |
return(-1); |
136 |
+ |
} |
137 |
+ |
} |
138 |
+ |
cm->nrows = nr; |
139 |
+ |
cm->ncols = nc; |
140 |
+ |
return(1); |
141 |
+ |
} |
142 |
+ |
|
143 |
|
/* check to see if a string contains a %d or %o specification */ |
144 |
|
static int |
145 |
|
hasNumberFormat(const char *s) |
178 |
|
CMATRIX *cmtx; /* component vector/matrix result */ |
179 |
|
char fnbuf[256]; |
180 |
|
int a, i; |
181 |
< |
|
182 |
< |
progname = argv[0]; |
181 |
> |
/* set global progname */ |
182 |
> |
fixargv0(argv[0]); |
183 |
|
/* get options */ |
184 |
|
for (a = 1; a < argc && argv[a][0] == '-'; a++) |
185 |
|
switch (argv[a][1]) { |
248 |
|
nsteps = smtx->ncols; |
249 |
|
/* load BSDF */ |
250 |
|
if (argv[a+1][0] != '!' && |
251 |
< |
(ccp = strrchr(argv[a+1], '.')) != NULL && |
251 |
> |
(ccp = strrchr(argv[a+1], '.')) > argv[a+1] && |
252 |
|
!strcasecmp(ccp+1, "XML")) |
253 |
|
Tmat = cm_loadBTDF(argv[a+1]); |
254 |
|
else |
268 |
|
} |
269 |
|
/* prepare output stream */ |
270 |
|
if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) { |
271 |
< |
sprintf(fnbuf, ofspec, 1); |
271 |
> |
sprintf(fnbuf, ofspec, 0); |
272 |
|
ofspec = fnbuf; |
273 |
|
} |
274 |
|
if (ofspec != NULL && !hasNumberFormat(ofspec)) { |
279 |
|
} |
280 |
|
ofspec = NULL; /* only need to open once */ |
281 |
|
} |
282 |
< |
if (hasNumberFormat(argv[a])) { /* generating image(s) */ |
282 |
> |
if (hasNumberFormat(argv[a])) { /* loading image vector(s) */ |
283 |
|
if (outfmt != DTrgbe) { |
284 |
|
error(WARNING, "changing output type to -oc"); |
285 |
|
outfmt = DTrgbe; |
321 |
|
} |
322 |
|
else if (!sum_images(argv[a], cmtx, ofp)) |
323 |
|
return(1); |
324 |
< |
} else { /* generating vector/matrix */ |
324 |
> |
} else { /* loading view matrix */ |
325 |
|
CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader); |
326 |
|
CMATRIX *rmtx = cm_multiply(Vmat, cmtx); |
327 |
|
cm_free(Vmat); |
329 |
|
const char *wtype = (outfmt==DTascii) ? "w" : "wb"; |
330 |
|
for (i = 0; i < nsteps; i++) { |
331 |
|
CMATRIX *rvec = cm_column(rmtx, i); |
332 |
< |
if (yres > 0) { |
333 |
< |
if (xres <= 0) |
301 |
< |
xres = rvec->nrows/yres; |
302 |
< |
if (xres*yres != rvec->nrows) { |
303 |
< |
fprintf(stderr, "Bad resolution: %d != %dx%d\n", |
304 |
< |
rvec->nrows, xres, yres); |
305 |
< |
return(1); |
306 |
< |
} |
307 |
< |
rvec->nrows = yres; |
308 |
< |
rvec->ncols = xres; |
309 |
< |
} else if (xres > 0) { |
310 |
< |
yres = rvec->nrows/xres; |
311 |
< |
if (xres*yres != rvec->nrows) { |
312 |
< |
fprintf(stderr, |
313 |
< |
"Bad resolution: %d does not divide %d evenly\n", |
314 |
< |
xres, rvec->nrows); |
315 |
< |
return(1); |
316 |
< |
} |
317 |
< |
rvec->nrows = yres; |
318 |
< |
rvec->ncols = xres; |
319 |
< |
} |
332 |
> |
if (alt_dim(rvec, yres, xres) < 0) |
333 |
> |
return(1); |
334 |
|
sprintf(fnbuf, ofspec, i); |
335 |
|
if ((ofp = fopen(fnbuf, wtype)) == NULL) { |
336 |
|
fprintf(stderr, |
349 |
|
if ((outfmt != DTrgbe) & (outfmt != DTxyze)) { |
350 |
|
fprintf(ofp, "NROWS=%d\n", rvec->nrows); |
351 |
|
fprintf(ofp, "NCOLS=%d\n", rvec->ncols); |
352 |
< |
fputs("NCOMP=3\n", ofp); |
352 |
> |
fputncomp(3, ofp); |
353 |
|
} |
354 |
|
if ((outfmt == DTfloat) | (outfmt == DTdouble)) |
355 |
|
fputendian(ofp); |
372 |
|
#endif |
373 |
|
if (outfmt != DTascii) |
374 |
|
SET_FILE_BINARY(ofp); |
375 |
+ |
if (alt_dim(rmtx, yres, xres) < 0) |
376 |
+ |
return(1); |
377 |
|
if (headout) { /* header output */ |
378 |
|
newheader("RADIANCE", ofp); |
379 |
|
printargs(argc, argv, ofp); |
381 |
|
if ((outfmt != DTrgbe) & (outfmt != DTxyze)) { |
382 |
|
fprintf(ofp, "NROWS=%d\n", rmtx->nrows); |
383 |
|
fprintf(ofp, "NCOLS=%d\n", rmtx->ncols); |
384 |
< |
fputs("NCOMP=3\n", ofp); |
384 |
> |
fputncomp(3, ofp); |
385 |
|
} |
386 |
|
if ((outfmt == DTfloat) | (outfmt == DTdouble)) |
387 |
|
fputendian(ofp); |