ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.53
Committed: Thu Mar 27 16:34:23 2025 UTC (5 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.52: +8 -5 lines
Log Message:
perf(dctimestep): Added back-and-forth sweep to improve memory access from picture set

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: dctimestep.c,v 2.52 2025/03/22 01:27:22 greg Exp $";
3 #endif
4 /*
5 * Compute time-step result using Daylight Coefficient method.
6 *
7 * G. Ward
8 */
9
10 #include <ctype.h>
11 #include "platform.h"
12 #include "standard.h"
13 #include "cmatrix.h"
14 #include "platform.h"
15 #include "resolu.h"
16
17 char *progname; /* global argv[0] */
18
19 /* Sum together a set of images and write result to fout */
20 static int
21 sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
22 {
23 static int runcnt = 0;
24 int myDT = DTfromHeader;
25 COLR *scanline = NULL;
26 CMATRIX *pmat = NULL;
27 int myXR=0, myYR=0;
28 int i, y;
29
30 if (cv->ncols != 1)
31 error(INTERNAL, "expected vector in sum_images()");
32 for (i = cv->nrows; i-- > 0; ) {
33 const int r = runcnt&1 ? i : cv->nrows-1 - i;
34 const COLORV *scv = cv_lval(cv,r);
35 int flat_file = 0;
36 char fname[1024];
37 FILE *fp;
38 long data_start;
39 int dt, xr, yr;
40 COLORV *psp;
41 char *err;
42 /* check for zero */
43 if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
44 (myDT != DTfromHeader) | (i > 0))
45 continue;
46 /* open next picture */
47 sprintf(fname, fspec, r);
48 if ((fp = fopen(fname, "rb")) == NULL) {
49 sprintf(errmsg, "cannot open picture '%s'", fname);
50 error(SYSTEM, errmsg);
51 }
52 #ifdef getc_unlocked
53 flockfile(fp);
54 #endif
55 dt = DTfromHeader;
56 if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL)
57 error(USER, err);
58 if ((dt != DTrgbe) & (dt != DTxyze) ||
59 !fscnresolu(&xr, &yr, fp)) {
60 sprintf(errmsg, "file '%s' not a picture", fname);
61 error(USER, errmsg);
62 }
63 if (myDT == DTfromHeader) { /* on first one */
64 myDT = dt;
65 myXR = xr; myYR = yr;
66 scanline = (COLR *)malloc(sizeof(COLR)*myXR);
67 if (scanline == NULL)
68 error(SYSTEM, "out of memory in sum_images()");
69 pmat = cm_alloc(myYR, myXR);
70 memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
71 /* finish header */
72 fputformat(cm_fmt_id[myDT], fout);
73 fputc('\n', fout);
74 fflush(fout);
75 } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
76 sprintf(errmsg, "picture '%s' format/size mismatch",
77 fname);
78 error(USER, errmsg);
79 }
80 /* flat file check */
81 if ((data_start = ftell(fp)) > 0 && fseek(fp, 0L, SEEK_END) == 0) {
82 flat_file = (ftell(fp) >= data_start + sizeof(COLR)*xr*yr);
83 if (fseek(fp, data_start, SEEK_SET) < 0) {
84 sprintf(errmsg, "cannot seek on picture '%s'", fname);
85 error(SYSTEM, errmsg);
86 }
87 }
88 psp = pmat->cmem;
89 for (y = 0; y < yr; y++) { /* read it in */
90 COLOR col;
91 int x;
92 if (flat_file ? getbinary(scanline, sizeof(COLR), xr, fp) != xr :
93 freadcolrs(scanline, xr, fp) < 0) {
94 sprintf(errmsg, "error reading picture '%s'",
95 fname);
96 error(SYSTEM, errmsg);
97 }
98 /* sum in scanline */
99 for (x = 0; x < xr; x++, psp += 3) {
100 if (!scanline[x][EXP])
101 continue; /* skip zeroes */
102 colr_color(col, scanline[x]);
103 multcolor(col, scv);
104 addcolor(psp, col);
105 }
106 }
107 fclose(fp); /* done this picture */
108 }
109 free(scanline);
110 i = cm_write(pmat, myDT, fout); /* write picture */
111 cm_free(pmat); /* free data */
112 ++runcnt; /* for zig-zagging */
113 return(i);
114 }
115
116 /* adjust matrix dimensions according to user size(s) */
117 static int
118 alt_dim(CMATRIX *cm, int nr, int nc)
119 {
120 if ((nr <= 0) & (nc <= 0))
121 return(0);
122 if ((nr == cm->nrows) & (nc == cm->ncols))
123 return(0);
124 if (nr > 0) {
125 if (nc <= 0)
126 nc = cm->nrows*cm->ncols/nr;
127 if (nr*nc != cm->nrows*cm->ncols) {
128 fprintf(stderr, "Bad dimensions: %dx%d != %dx%d\n",
129 nr, nc, cm->nrows, cm->ncols);
130 return(-1);
131 }
132 } else /* nc > 0 */ {
133 nr = cm->nrows*cm->ncols/nc;
134 if (nc*nr != cm->nrows*cm->ncols) {
135 fprintf(stderr, "Bad dimensions: %d does not divide %dx%d evenly\n",
136 nc, cm->nrows, cm->ncols);
137 return(-1);
138 }
139 }
140 cm->nrows = nr;
141 cm->ncols = nc;
142 return(1);
143 }
144
145 /* check to see if a string contains a %d or %o specification */
146 static int
147 hasNumberFormat(const char *s)
148 {
149 if (s == NULL)
150 return(0);
151
152 while (*s) {
153 while (*s != '%')
154 if (!*s++)
155 return(0);
156 if (*++s == '%') { /* ignore "%%" */
157 ++s;
158 continue;
159 }
160 while (isdigit(*s)) /* field length */
161 ++s;
162 /* field we'll use? */
163 if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
164 (*s == 'x') | (*s == 'X'))
165 return(1);
166 }
167 return(0); /* didn't find one */
168 }
169
170 int
171 main(int argc, char *argv[])
172 {
173 int skyfmt = DTfromHeader;
174 int outfmt = DTascii;
175 int headout = 1;
176 int nsteps = 0;
177 char *ofspec = NULL;
178 FILE *ofp = stdout;
179 int xres=0, yres=0;
180 CMATRIX *cmtx; /* component vector/matrix result */
181 char fnbuf[256];
182 int a, i;
183
184 progname = argv[0];
185 /* get options */
186 for (a = 1; a < argc && argv[a][0] == '-'; a++)
187 switch (argv[a][1]) {
188 case 'n':
189 nsteps = atoi(argv[++a]);
190 if (nsteps < 0)
191 goto userr;
192 skyfmt = nsteps ? DTascii : DTfromHeader;
193 break;
194 case 'h':
195 headout = !headout;
196 break;
197 case 'i':
198 switch (argv[a][2]) {
199 case 'f':
200 skyfmt = DTfloat;
201 break;
202 case 'd':
203 skyfmt = DTdouble;
204 break;
205 case 'a':
206 skyfmt = DTascii;
207 break;
208 default:
209 goto userr;
210 }
211 break;
212 case 'o':
213 switch (argv[a][2]) {
214 case '\0': /* output specification (not format) */
215 ofspec = argv[++a];
216 break;
217 case 'f':
218 outfmt = DTfloat;
219 break;
220 case 'd':
221 outfmt = DTdouble;
222 break;
223 case 'a':
224 outfmt = DTascii;
225 break;
226 case 'c':
227 outfmt = DTrgbe;
228 break;
229 default:
230 goto userr;
231 }
232 break;
233 case 'x':
234 xres = atoi(argv[++a]);
235 break;
236 case 'y':
237 yres = atoi(argv[++a]);
238 break;
239 default:
240 goto userr;
241 }
242 if ((argc-a < 1) | (argc-a > 4))
243 goto userr;
244
245 if (argc-a > 2) { /* VTDs expression */
246 CMATRIX *smtx, *Dmat, *Tmat, *imtx;
247 const char *ccp;
248 /* get sky vector/matrix */
249 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
250 nsteps = smtx->ncols;
251 /* load BSDF */
252 if (argv[a+1][0] != '!' &&
253 (ccp = strrchr(argv[a+1], '.')) > argv[a+1] &&
254 !strcasecmp(ccp+1, "XML"))
255 Tmat = cm_loadBTDF(argv[a+1]);
256 else
257 Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader);
258 /* load Daylight matrix */
259 Dmat = cm_load(argv[a+2], Tmat->ncols,
260 smtx->nrows, DTfromHeader);
261 /* multiply vector through */
262 imtx = cm_multiply(Dmat, smtx);
263 cm_free(Dmat); cm_free(smtx);
264 cmtx = cm_multiply(Tmat, imtx);
265 cm_free(Tmat);
266 cm_free(imtx);
267 } else { /* sky vector/matrix only */
268 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
269 nsteps = cmtx->ncols;
270 }
271 /* prepare output stream */
272 if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
273 sprintf(fnbuf, ofspec, 0);
274 ofspec = fnbuf;
275 }
276 if (ofspec != NULL && !hasNumberFormat(ofspec)) {
277 if ((ofp = fopen(ofspec, "w")) == NULL) {
278 fprintf(stderr, "%s: cannot open '%s' for output\n",
279 progname, ofspec);
280 return(1);
281 }
282 ofspec = NULL; /* only need to open once */
283 }
284 if (hasNumberFormat(argv[a])) { /* loading image vector(s) */
285 if (outfmt != DTrgbe) {
286 error(WARNING, "changing output type to -oc");
287 outfmt = DTrgbe;
288 }
289 if (ofspec == NULL) {
290 SET_FILE_BINARY(ofp);
291 newheader("RADIANCE", ofp);
292 printargs(argc, argv, ofp);
293 fputnow(ofp);
294 }
295 if (nsteps > 1) /* multiple output frames? */
296 for (i = 0; i < nsteps; i++) {
297 CMATRIX *cvec = cm_column(cmtx, i);
298 if (ofspec != NULL) {
299 sprintf(fnbuf, ofspec, i);
300 if ((ofp = fopen(fnbuf, "wb")) == NULL) {
301 fprintf(stderr,
302 "%s: cannot open '%s' for output\n",
303 progname, fnbuf);
304 return(1);
305 }
306 newheader("RADIANCE", ofp);
307 printargs(argc, argv, ofp);
308 fputnow(ofp);
309 }
310 fprintf(ofp, "FRAME=%d\n", i);
311 if (!sum_images(argv[a], cvec, ofp))
312 return(1);
313 if (ofspec != NULL) {
314 if (fclose(ofp) == EOF) {
315 fprintf(stderr,
316 "%s: error writing to '%s'\n",
317 progname, fnbuf);
318 return(1);
319 }
320 ofp = stdout;
321 }
322 cm_free(cvec);
323 }
324 else if (!sum_images(argv[a], cmtx, ofp))
325 return(1);
326 } else { /* loading view matrix */
327 CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
328 CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
329 cm_free(Vmat);
330 if (ofspec != NULL) { /* multiple vector files? */
331 const char *wtype = (outfmt==DTascii) ? "w" : "wb";
332 for (i = 0; i < nsteps; i++) {
333 CMATRIX *rvec = cm_column(rmtx, i);
334 if (alt_dim(rvec, yres, xres) < 0)
335 return(1);
336 sprintf(fnbuf, ofspec, i);
337 if ((ofp = fopen(fnbuf, wtype)) == NULL) {
338 fprintf(stderr,
339 "%s: cannot open '%s' for output\n",
340 progname, fnbuf);
341 return(1);
342 }
343 #ifdef getc_unlocked
344 flockfile(ofp);
345 #endif
346 if (headout) { /* header output */
347 newheader("RADIANCE", ofp);
348 printargs(argc, argv, ofp);
349 fputnow(ofp);
350 fprintf(ofp, "FRAME=%d\n", i);
351 if ((outfmt != DTrgbe) & (outfmt != DTxyze)) {
352 fprintf(ofp, "NROWS=%d\n", rvec->nrows);
353 fprintf(ofp, "NCOLS=%d\n", rvec->ncols);
354 fputncomp(3, ofp);
355 }
356 if ((outfmt == DTfloat) | (outfmt == DTdouble))
357 fputendian(ofp);
358 fputformat(cm_fmt_id[outfmt], ofp);
359 fputc('\n', ofp);
360 }
361 cm_write(rvec, outfmt, ofp);
362 if (fclose(ofp) == EOF) {
363 fprintf(stderr,
364 "%s: error writing to '%s'\n",
365 progname, fnbuf);
366 return(1);
367 }
368 ofp = stdout;
369 cm_free(rvec);
370 }
371 } else {
372 #ifdef getc_unlocked
373 flockfile(ofp);
374 #endif
375 if (outfmt != DTascii)
376 SET_FILE_BINARY(ofp);
377 if (alt_dim(rmtx, yres, xres) < 0)
378 return(1);
379 if (headout) { /* header output */
380 newheader("RADIANCE", ofp);
381 printargs(argc, argv, ofp);
382 fputnow(ofp);
383 if ((outfmt != DTrgbe) & (outfmt != DTxyze)) {
384 fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
385 fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
386 fputncomp(3, ofp);
387 }
388 if ((outfmt == DTfloat) | (outfmt == DTdouble))
389 fputendian(ofp);
390 fputformat(cm_fmt_id[outfmt], ofp);
391 fputc('\n', ofp);
392 }
393 cm_write(rmtx, outfmt, ofp);
394 }
395 cm_free(rmtx);
396 }
397 if (fflush(ofp) == EOF) { /* final clean-up */
398 fprintf(stderr, "%s: write error on output\n", progname);
399 return(1);
400 }
401 cm_free(cmtx);
402 return(0);
403 userr:
404 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-x xr][-y yr][-i{f|d|h}][-o{f|d|c}] DCspec [skyf]\n",
405 progname);
406 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-x xr][-y yr][-i{f|d|h}][-o{f|d|c}] Vspec Tbsdf Dmat.dat [skyf]\n",
407 progname);
408 return(1);
409 }