ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.88
Committed: Fri May 9 23:28:57 2014 UTC (9 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad4R2, rad4R2P1
Changes since 2.87: +13 -15 lines
Log Message:
Added process ID to reports

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.88 static const char RCSid[] = "$Id: rpict.c,v 2.87 2014/02/18 12:13:42 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * rpict.c - routines and variables for picture generation.
6     */
7    
8 greg 2.53 #include "copyright.h"
9 greg 2.52
10 greg 2.32 #include <sys/types.h>
11    
12 schorsch 2.80 #include "platform.h"
13     #ifdef NON_POSIX
14     #ifdef MINGW
15     #include <sys/time.h>
16     #endif
17 schorsch 2.79 #else
18 schorsch 2.80 #ifdef BSD
19     #include <sys/time.h>
20     #include <sys/resource.h>
21     #else
22     #include <sys/times.h>
23     #include <unistd.h>
24     #endif
25 greg 2.30 #endif
26 greg 1.34
27 schorsch 2.55 #include <time.h>
28 greg 1.14 #include <signal.h>
29 greg 1.1
30 schorsch 2.69 #include "ray.h"
31     #include "paths.h"
32     #include "ambient.h"
33 greg 1.1 #include "view.h"
34     #include "random.h"
35 schorsch 2.55 #include "paths.h"
36 greg 2.87 #include "hilbert.h"
37 greg 1.1
38 greg 2.12
39 greg 2.21 #define RFTEMPLATE "rfXXXXXX"
40    
41 greg 2.22 #ifndef SIGCONT
42 schorsch 2.56 #ifdef SIGIO /* XXX can we live without this? */
43 greg 2.22 #define SIGCONT SIGIO
44     #endif
45 schorsch 2.56 #endif
46 greg 2.22
47 greg 2.52 CUBE thescene; /* our scene */
48     OBJECT nsceneobjs; /* number of objects in our scene */
49    
50 greg 1.25 int dimlist[MAXDIM]; /* sampling dimensions */
51     int ndims = 0; /* number of sampling dimensions */
52     int samplendx; /* sample index number */
53    
54 greg 2.52 void (*addobjnotify[])() = {ambnotify, NULL};
55    
56 greg 1.12 VIEW ourview = STDVIEW; /* view parameters */
57     int hresolu = 512; /* horizontal resolution */
58     int vresolu = 512; /* vertical resolution */
59 greg 2.14 double pixaspect = 1.0; /* pixel aspect ratio */
60 greg 1.1
61     int psample = 4; /* pixel sample size */
62 greg 2.14 double maxdiff = .05; /* max. difference for interpolation */
63     double dstrpix = 0.67; /* square pixel distribution */
64 greg 1.1
65 gwlarson 2.49 double mblur = 0.; /* motion blur parameter */
66    
67 greg 2.73 double dblur = 0.; /* depth-of-field blur parameter */
68    
69 greg 2.52 void (*trace)() = NULL; /* trace call */
70    
71     int do_irrad = 0; /* compute irradiance? */
72    
73 greg 2.76 int rand_samp = 0; /* pure Monte Carlo sampling? */
74    
75 greg 2.14 double dstrsrc = 0.0; /* square source distribution */
76     double shadthresh = .05; /* shadow threshold */
77     double shadcert = .5; /* shadow certainty */
78 greg 1.35 int directrelay = 1; /* number of source relays */
79 greg 1.29 int vspretest = 512; /* virtual source pretest density */
80 greg 2.20 int directvis = 1; /* sources visible? */
81 greg 2.14 double srcsizerat = .25; /* maximum ratio source size/dist. */
82 greg 2.41
83     COLOR cextinction = BLKCOLOR; /* global extinction coefficient */
84 greg 2.46 COLOR salbedo = BLKCOLOR; /* global scattering albedo */
85 greg 2.41 double seccg = 0.; /* global scattering eccentricity */
86     double ssampdist = 0.; /* scatter sampling distance */
87 greg 1.1
88 greg 2.14 double specthresh = .15; /* specular sampling threshold */
89     double specjitter = 1.; /* specular sampling jitter */
90 greg 2.5
91 greg 2.40 int backvis = 1; /* back face visibility */
92    
93 greg 2.63 int maxdepth = 7; /* maximum recursion depth */
94 greg 2.84 double minweight = 1e-3; /* minimum ray weight */
95 greg 1.1
96 greg 2.52 char *ambfile = NULL; /* ambient file name */
97 greg 1.1 COLOR ambval = BLKCOLOR; /* ambient value */
98 greg 2.44 int ambvwt = 0; /* initial weight for ambient value */
99 greg 2.68 double ambacc = 0.2; /* ambient accuracy */
100 greg 2.63 int ambres = 64; /* ambient resolution */
101     int ambdiv = 512; /* ambient divisions */
102     int ambssamp = 128; /* ambient super-samples */
103 greg 1.1 int ambounce = 0; /* ambient bounces */
104 greg 2.72 char *amblist[AMBLLEN]; /* ambient include/exclude list */
105 greg 1.1 int ambincl = -1; /* include == 1, exclude == 0 */
106    
107     int ralrm = 0; /* seconds between reports */
108    
109 greg 2.14 double pctdone = 0.0; /* percentage done */
110 greg 2.32 time_t tlastrept = 0L; /* time at last report */
111 greg 2.52 time_t tstart; /* starting time */
112 greg 1.1
113 greg 2.14 #define MAXDIV 16 /* maximum sample size */
114 greg 1.1
115 greg 2.14 #define pixjitter() (.5+dstrpix*(.5-frandom()))
116 greg 1.1
117 greg 2.47 int hres, vres; /* resolution for this frame */
118 greg 2.8
119 gwlarson 2.49 static VIEW lastview; /* the previous view input */
120    
121 schorsch 2.69 static void report(int);
122     static int nextview(FILE *fp);
123     static void render(char *zfile, char *oldfile);
124     static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres,
125     int y, int xstep);
126     static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres,
127     int y, int ysize);
128     static int fillsample(COLOR *colline, float *zline, int x, int y,
129     int xlen, int ylen, int b);
130     static double pixvalue(COLOR col, int x, int y);
131     static int salvage(char *oldfile);
132     static int pixnumber(int x, int y, int xres, int yres);
133 greg 2.8
134 greg 2.52
135 greg 1.1
136 schorsch 2.58 #ifdef RHAS_STAT
137 greg 2.27 #include <sys/types.h>
138     #include <sys/stat.h>
139     int
140     file_exists(fname) /* ordinary file exists? */
141     char *fname;
142     {
143     struct stat sbuf;
144     if (stat(fname, &sbuf) < 0) return(0);
145     return((sbuf.st_mode & S_IFREG) != 0);
146     }
147 schorsch 2.58 #else
148     #define file_exists(f) (access(f,F_OK)==0)
149 greg 2.27 #endif
150 greg 1.11
151 greg 2.27
152 greg 2.52 void
153 greg 1.1 quit(code) /* quit program */
154     int code;
155     {
156 greg 2.8 if (code) /* report status */
157 schorsch 2.69 report(0);
158 schorsch 2.57 #ifndef NON_POSIX
159 greg 2.21 headclean(); /* delete header file */
160     pfclean(); /* clean up persist files */
161 greg 2.52 #endif
162 greg 1.1 exit(code);
163     }
164    
165    
166 schorsch 2.57 #ifndef NON_POSIX
167 schorsch 2.69 static void
168     report(int dummy) /* report progress */
169 greg 1.1 {
170 greg 2.88 double u, s;
171 greg 2.30 #ifdef BSD
172 greg 2.88 struct rusage rubuf;
173 greg 2.30 #else
174 greg 2.88 double period = 1.0 / 60.0;
175     struct tms tbuf;
176 greg 2.30 #endif
177 greg 1.1
178 greg 2.32 tlastrept = time((time_t *)NULL);
179 greg 2.30 #ifdef BSD
180 greg 1.1 getrusage(RUSAGE_SELF, &rubuf);
181 greg 2.88 u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
182     s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
183 greg 1.1 getrusage(RUSAGE_CHILDREN, &rubuf);
184 greg 2.88 u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
185     s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
186 greg 2.30 #else
187     times(&tbuf);
188 greg 2.36 #ifdef _SC_CLK_TCK
189 greg 2.33 period = 1.0 / sysconf(_SC_CLK_TCK);
190 greg 2.36 #endif
191 greg 2.33 u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
192     s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
193 greg 2.30 #endif
194 greg 1.1
195 greg 2.30 sprintf(errmsg,
196 greg 2.88 "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n",
197     nrays, pctdone, u*(1./3600.), s*(1./3600.),
198     (tlastrept-tstart)*(1./3600.), myhostname(), getpid());
199 greg 1.24 eputs(errmsg);
200 schorsch 2.56 #ifdef SIGCONT
201 greg 2.35 signal(SIGCONT, report);
202     #endif
203 greg 1.24 }
204 greg 1.1 #else
205 schorsch 2.69 static void
206 schorsch 2.71 report(int dummy) /* report progress */
207 greg 1.24 {
208 greg 2.32 tlastrept = time((time_t *)NULL);
209 greg 2.30 sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
210 greg 1.24 nrays, pctdone, (tlastrept-tstart)/3600.0);
211 greg 1.1 eputs(errmsg);
212     }
213 greg 1.24 #endif
214 greg 1.1
215    
216 greg 2.88 void
217 schorsch 2.69 rpict( /* generate image(s) */
218     int seq,
219     char *pout,
220     char *zout,
221     char *prvr
222     )
223 greg 2.8 /*
224     * If seq is greater than zero, then we will render a sequence of
225     * images based on view parameter strings read from the standard input.
226     * If pout is NULL, then all images will be sent to the standard ouput.
227     * If seq is greater than zero and prvr is an integer, then it is the
228     * frame number at which rendering should begin. Preceeding view parameter
229     * strings will be skipped in the input.
230     * If pout and prvr are the same, prvr is renamed to avoid overwriting.
231     * Note that pout and zout should contain %d format specifications for
232     * sequenced file naming.
233     */
234     {
235 greg 2.9 char fbuf[128], fbuf2[128];
236 greg 2.26 int npicts;
237 greg 2.85 char *cp;
238 greg 2.14 RESOLU rs;
239     double pa;
240 greg 2.8 /* check sampling */
241     if (psample < 1)
242     psample = 1;
243     else if (psample > MAXDIV) {
244     sprintf(errmsg, "pixel sampling reduced from %d to %d",
245     psample, MAXDIV);
246     error(WARNING, errmsg);
247     psample = MAXDIV;
248     }
249     /* get starting frame */
250     if (seq <= 0)
251     seq = 0;
252     else if (prvr != NULL && isint(prvr)) {
253 greg 2.85 int rn; /* skip to specified view */
254 greg 2.8 if ((rn = atoi(prvr)) < seq)
255     error(USER, "recover frame less than start frame");
256     if (pout == NULL)
257     error(USER, "missing output file specification");
258     for ( ; seq < rn; seq++)
259     if (nextview(stdin) == EOF)
260     error(USER, "unexpected EOF on view input");
261 gwlarson 2.50 setview(&ourview);
262 greg 2.8 prvr = fbuf; /* mark for renaming */
263     }
264 schorsch 2.62 if ((pout != NULL) & (prvr != NULL)) {
265 greg 2.8 sprintf(fbuf, pout, seq);
266 greg 2.18 if (!strcmp(prvr, fbuf)) { /* rename */
267     strcpy(fbuf2, fbuf);
268     for (cp = fbuf2; *cp; cp++)
269     ;
270     while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
271     cp--;
272     strcpy(cp, RFTEMPLATE);
273 greg 2.8 prvr = mktemp(fbuf2);
274 schorsch 2.61 if (rename(fbuf, prvr) < 0) {
275 greg 2.28 if (errno == ENOENT) { /* ghost file */
276     sprintf(errmsg,
277     "new output file \"%s\"",
278     fbuf);
279     error(WARNING, errmsg);
280     prvr = NULL;
281     } else { /* serious error */
282     sprintf(errmsg,
283 greg 2.8 "cannot rename \"%s\" to \"%s\"",
284     fbuf, prvr);
285 greg 2.28 error(SYSTEM, errmsg);
286     }
287 schorsch 2.61 }
288 greg 2.8 }
289     }
290 greg 2.26 npicts = 0; /* render sequence */
291 greg 2.8 do {
292     if (seq && nextview(stdin) == EOF)
293     break;
294 greg 2.25 pctdone = 0.0;
295 greg 2.8 if (pout != NULL) {
296     sprintf(fbuf, pout, seq);
297 greg 2.27 if (file_exists(fbuf)) {
298     if (prvr != NULL || !strcmp(fbuf, pout)) {
299     sprintf(errmsg,
300     "output file \"%s\" exists",
301     fbuf);
302     error(USER, errmsg);
303     }
304 gwlarson 2.50 setview(&ourview);
305 greg 2.26 continue; /* don't clobber */
306 greg 2.27 }
307 greg 2.8 if (freopen(fbuf, "w", stdout) == NULL) {
308     sprintf(errmsg,
309     "cannot open output file \"%s\"", fbuf);
310     error(SYSTEM, errmsg);
311     }
312 schorsch 2.55 SET_FILE_BINARY(stdout);
313 greg 2.8 dupheader();
314     }
315     hres = hresolu; vres = vresolu; pa = pixaspect;
316 schorsch 2.61 if (prvr != NULL) {
317 greg 2.66 if (viewfile(prvr, &ourview, &rs) <= 0) {
318 greg 2.8 sprintf(errmsg,
319     "cannot recover view parameters from \"%s\"", prvr);
320     error(WARNING, errmsg);
321     } else {
322     pa = 0.0;
323     hres = scanlen(&rs);
324     vres = numscans(&rs);
325     }
326 schorsch 2.61 }
327 greg 2.9 if ((cp = setview(&ourview)) != NULL)
328     error(USER, cp);
329 greg 2.8 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
330     if (seq) {
331     if (ralrm > 0) {
332 greg 2.11 fprintf(stderr, "FRAME %d:", seq);
333     fprintview(&ourview, stderr);
334     putc('\n', stderr);
335     fflush(stderr);
336 greg 2.8 }
337     printf("FRAME=%d\n", seq);
338     }
339     fputs(VIEWSTR, stdout);
340     fprintview(&ourview, stdout);
341     putchar('\n');
342 greg 2.85 if ((pa < .99) | (pa > 1.01))
343 greg 2.8 fputaspect(pa, stdout);
344 schorsch 2.60 fputnow(stdout);
345 greg 2.8 fputformat(COLRFMT, stdout);
346     putchar('\n');
347     if (zout != NULL)
348 greg 2.9 sprintf(cp=fbuf, zout, seq);
349 greg 2.8 else
350 greg 2.9 cp = NULL;
351     render(cp, prvr);
352 greg 2.8 prvr = NULL;
353 greg 2.26 npicts++;
354 greg 2.8 } while (seq++);
355 greg 2.26 /* check that we did something */
356     if (npicts == 0)
357     error(WARNING, "no output produced");
358 greg 2.8 }
359    
360    
361 schorsch 2.69 static int
362     nextview( /* get next view from fp */
363     FILE *fp
364     )
365 greg 2.8 {
366 greg 2.9 char linebuf[256];
367 greg 2.8
368 schorsch 2.61 lastview = ourview;
369 greg 2.8 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
370 greg 2.9 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
371 greg 2.8 return(0);
372     return(EOF);
373     }
374    
375    
376 schorsch 2.69 static void
377     render( /* render the scene */
378     char *zfile,
379     char *oldfile
380     )
381 greg 1.1 {
382     COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
383 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
384 greg 2.2 char *sampdens; /* previous sample density */
385 greg 1.1 int ypos; /* current scanline */
386 greg 1.15 int ystep; /* current y step size */
387 greg 1.33 int hstep; /* h step size */
388 greg 1.16 int zfd;
389 greg 1.1 COLOR *colptr;
390 greg 1.11 float *zptr;
391 greg 2.85 int i;
392 greg 2.52 /* check for empty image */
393 greg 2.85 if ((hres <= 0) | (vres <= 0)) {
394 greg 2.52 error(WARNING, "empty output picture");
395     fprtresolu(0, 0, stdout);
396     return;
397     }
398 greg 1.9 /* allocate scanlines */
399 greg 1.1 for (i = 0; i <= psample; i++) {
400 greg 2.8 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
401 greg 1.1 if (scanbar[i] == NULL)
402 greg 1.11 goto memerr;
403 greg 1.1 }
404 greg 2.2 hstep = (psample*140+49)/99; /* quincunx sampling */
405     ystep = (psample*99+70)/140;
406     if (hstep > 2) {
407 greg 2.8 i = hres/hstep + 2;
408 greg 2.52 if ((sampdens = (char *)malloc(i)) == NULL)
409 greg 2.2 goto memerr;
410     while (i--)
411     sampdens[i] = hstep;
412     } else
413     sampdens = NULL;
414 greg 2.26 /* open z-file */
415 greg 1.11 if (zfile != NULL) {
416 greg 1.16 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
417 greg 2.26 sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
418 greg 1.11 error(SYSTEM, errmsg);
419     }
420 schorsch 2.55 SET_FD_BINARY(zfd);
421 greg 1.11 for (i = 0; i <= psample; i++) {
422 greg 2.8 zbar[i] = (float *)malloc(hres*sizeof(float));
423 greg 1.11 if (zbar[i] == NULL)
424     goto memerr;
425     }
426     } else {
427 greg 1.16 zfd = -1;
428 greg 1.11 for (i = 0; i <= psample; i++)
429     zbar[i] = NULL;
430     }
431 greg 1.1 /* write out boundaries */
432 greg 2.8 fprtresolu(hres, vres, stdout);
433 greg 1.10 /* recover file and compute first */
434 greg 1.11 i = salvage(oldfile);
435 greg 2.42 if (i >= vres)
436     goto alldone;
437 greg 2.85 if ((zfd != -1) & (i > 0) &&
438 greg 2.64 lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
439 greg 2.26 error(SYSTEM, "z-file seek error in render");
440 greg 2.8 pctdone = 100.0*i/vres;
441 greg 1.24 if (ralrm > 0) /* report init stats */
442 schorsch 2.69 report(0);
443 schorsch 2.56 #ifdef SIGCONT
444 greg 1.32 else
445 schorsch 2.56 signal(SIGCONT, report);
446 greg 1.32 #endif
447 greg 2.47 ypos = vres-1 - i; /* initialize sampling */
448 gregl 2.48 if (directvis)
449     init_drawsources(psample);
450 greg 2.8 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
451 greg 1.10 /* compute scanlines */
452 greg 1.15 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
453     /* bottom adjust? */
454     if (ypos < 0) {
455     ystep += ypos;
456     ypos = 0;
457     }
458     colptr = scanbar[ystep]; /* move base to top */
459     scanbar[ystep] = scanbar[0];
460 greg 1.1 scanbar[0] = colptr;
461 greg 1.15 zptr = zbar[ystep];
462     zbar[ystep] = zbar[0];
463 greg 1.11 zbar[0] = zptr;
464 greg 1.10 /* fill base line */
465 greg 2.2 fillscanline(scanbar[0], zbar[0], sampdens,
466 greg 2.8 hres, ypos, hstep);
467 greg 1.10 /* fill bar */
468 greg 2.8 fillscanbar(scanbar, zbar, hres, ypos, ystep);
469 gregl 2.48 if (directvis) /* add bitty sources */
470     drawsources(scanbar, zbar, 0, hres, ypos, ystep);
471 greg 1.10 /* write it out */
472 schorsch 2.56 #ifdef SIGCONT
473 greg 2.22 signal(SIGCONT, SIG_IGN); /* don't interrupt writes */
474 greg 1.32 #endif
475 greg 1.15 for (i = ystep; i > 0; i--) {
476 greg 1.17 if (zfd != -1 && write(zfd, (char *)zbar[i],
477 greg 2.8 hres*sizeof(float))
478     < hres*sizeof(float))
479 greg 1.1 goto writerr;
480 greg 2.8 if (fwritescan(scanbar[i], hres, stdout) < 0)
481 greg 1.11 goto writerr;
482     }
483 greg 1.1 if (fflush(stdout) == EOF)
484     goto writerr;
485 greg 1.24 /* record progress */
486 greg 2.8 pctdone = 100.0*(vres-1-ypos)/vres;
487 greg 2.32 if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
488 schorsch 2.69 report(0);
489 schorsch 2.56 #ifdef SIGCONT
490 greg 1.32 else
491 greg 2.22 signal(SIGCONT, report);
492 greg 1.32 #endif
493 greg 1.1 }
494 greg 1.11 /* clean up */
495 schorsch 2.56 #ifdef SIGCONT
496 greg 2.22 signal(SIGCONT, SIG_IGN);
497 schorsch 2.56 #endif
498 greg 2.42 if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
499     < hres*sizeof(float))
500     goto writerr;
501     fwritescan(scanbar[0], hres, stdout);
502     if (fflush(stdout) == EOF)
503     goto writerr;
504     alldone:
505 greg 1.16 if (zfd != -1) {
506 greg 1.20 if (close(zfd) == -1)
507     goto writerr;
508 greg 1.11 for (i = 0; i <= psample; i++)
509 greg 2.52 free((void *)zbar[i]);
510 greg 1.11 }
511 greg 1.1 for (i = 0; i <= psample; i++)
512 greg 2.52 free((void *)scanbar[i]);
513 greg 2.2 if (sampdens != NULL)
514     free(sampdens);
515 greg 1.11 pctdone = 100.0;
516 greg 2.13 if (ralrm > 0)
517 schorsch 2.69 report(0);
518 schorsch 2.56 #ifdef SIGCONT
519 greg 2.23 signal(SIGCONT, SIG_DFL);
520 schorsch 2.56 #endif
521 greg 1.1 return;
522     writerr:
523     error(SYSTEM, "write error in render");
524 greg 1.11 memerr:
525     error(SYSTEM, "out of memory in render");
526 greg 1.1 }
527    
528    
529 schorsch 2.69 static void
530     fillscanline( /* fill scan at y */
531 greg 2.85 COLOR *scanline,
532     float *zline,
533     char *sd,
534 schorsch 2.69 int xres,
535     int y,
536     int xstep
537     )
538 greg 1.1 {
539 greg 1.33 static int nc = 0; /* number of calls */
540 greg 2.2 int bl = xstep, b = xstep;
541 greg 2.14 double z;
542 greg 2.85 int i;
543 greg 2.45
544 greg 1.11 z = pixvalue(scanline[0], 0, y);
545     if (zline) zline[0] = z;
546 greg 1.33 /* zig-zag start for quincunx pattern */
547 greg 2.2 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
548 greg 1.15 if (i >= xres) {
549     xstep += xres-1-i;
550     i = xres-1;
551     }
552 greg 1.11 z = pixvalue(scanline[i], i, y);
553     if (zline) zline[i] = z;
554 greg 2.2 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
555 greg 2.3 if (i <= xstep)
556     b = fillsample(scanline, zline, 0, y, i, 0, b/2);
557     else
558     b = fillsample(scanline+i-xstep,
559 greg 2.38 zline ? zline+i-xstep : (float *)NULL,
560 greg 2.3 i-xstep, y, xstep, 0, b/2);
561 greg 2.2 if (sd) *sd++ = nc & 1 ? bl : b;
562     bl = b;
563 greg 1.1 }
564 greg 2.2 if (sd && nc & 1) *sd = bl;
565 greg 1.1 }
566    
567    
568 schorsch 2.69 static void
569     fillscanbar( /* fill interior */
570 greg 2.85 COLOR *scanbar[],
571     float *zbar[],
572 schorsch 2.69 int xres,
573     int y,
574     int ysize
575     )
576 greg 1.1 {
577     COLOR vline[MAXDIV+1];
578 greg 1.11 float zline[MAXDIV+1];
579 greg 1.1 int b = ysize;
580 greg 2.85 int i, j;
581 greg 2.45
582 greg 1.8 for (i = 0; i < xres; i++) {
583 greg 1.1 copycolor(vline[0], scanbar[0][i]);
584     copycolor(vline[ysize], scanbar[ysize][i]);
585 greg 1.11 if (zbar[0]) {
586     zline[0] = zbar[0][i];
587     zline[ysize] = zbar[ysize][i];
588     }
589 greg 2.38 b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
590 greg 1.11 i, y, 0, ysize, b/2);
591 greg 2.45
592 greg 1.1 for (j = 1; j < ysize; j++)
593     copycolor(scanbar[j][i], vline[j]);
594 greg 1.11 if (zbar[0])
595     for (j = 1; j < ysize; j++)
596     zbar[j][i] = zline[j];
597 greg 1.1 }
598     }
599    
600    
601 schorsch 2.69 static int
602     fillsample( /* fill interior points */
603 greg 2.85 COLOR *colline,
604     float *zline,
605 schorsch 2.69 int x,
606     int y,
607     int xlen,
608     int ylen,
609     int b
610     )
611 greg 1.1 {
612 greg 2.14 double ratio;
613     double z;
614 greg 1.1 COLOR ctmp;
615     int ncut;
616 greg 2.85 int len;
617 greg 2.45
618 greg 1.1 if (xlen > 0) /* x or y length is zero */
619     len = xlen;
620     else
621     len = ylen;
622 greg 2.45
623 greg 1.1 if (len <= 1) /* limit recursion */
624     return(0);
625 greg 2.45
626     if (b > 0 ||
627     (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
628 greg 1.11 || bigdiff(colline[0], colline[len], maxdiff)) {
629 greg 2.45
630 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
631     if (zline) zline[len>>1] = z;
632 greg 1.1 ncut = 1;
633     } else { /* interpolate */
634     copycolor(colline[len>>1], colline[len]);
635     ratio = (double)(len>>1) / len;
636     scalecolor(colline[len>>1], ratio);
637 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
638     ratio = 1.0 - ratio;
639 greg 1.1 copycolor(ctmp, colline[0]);
640     scalecolor(ctmp, ratio);
641     addcolor(colline[len>>1], ctmp);
642 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
643 greg 1.1 ncut = 0;
644     }
645     /* recurse */
646 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
647 greg 2.45
648 greg 2.38 ncut += fillsample(colline+(len>>1),
649     zline ? zline+(len>>1) : (float *)NULL,
650 greg 1.11 x+(xlen>>1), y+(ylen>>1),
651     xlen-(xlen>>1), ylen-(ylen>>1), b/2);
652 greg 1.1
653     return(ncut);
654     }
655    
656    
657 schorsch 2.69 static double
658     pixvalue( /* compute pixel value */
659     COLOR col, /* returned color */
660     int x, /* pixel position */
661     int y
662     )
663 greg 1.1 {
664 greg 2.86 extern void SDsquare2disk(double ds[2], double seedx, double seedy);
665 gwlarson 2.49 RAY thisray;
666     FVECT lorg, ldir;
667 greg 2.73 double hpos, vpos, vdist, lmax;
668 greg 2.85 int i;
669 gwlarson 2.49 /* compute view ray */
670 greg 2.85 setcolor(col, 0.0, 0.0, 0.0);
671 gwlarson 2.49 hpos = (x+pixjitter())/hres;
672     vpos = (y+pixjitter())/vres;
673     if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
674 greg 2.85 &ourview, hpos, vpos)) < -FTINY)
675 greg 1.22 return(0.0);
676 greg 2.85
677 greg 2.73 vdist = ourview.vdist;
678 greg 2.77 /* set pixel index */
679     samplendx = pixnumber(x,y,hres,vres);
680 gwlarson 2.49 /* optional motion blur */
681     if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
682     &lastview, hpos, vpos)) >= -FTINY) {
683 greg 2.85 double d = mblur*(.5-urand(281+samplendx));
684 gwlarson 2.49
685     thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
686     for (i = 3; i--; ) {
687     thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i];
688     thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i];
689     }
690     if (normalize(thisray.rdir) == 0.0)
691     return(0.0);
692 greg 2.73 vdist = (1.-d)*vdist + d*lastview.vdist;
693     }
694     /* optional depth-of-field */
695 greg 2.85 if (dblur > FTINY) {
696 greg 2.86 double vc, df[2];
697     /* random point on disk */
698     SDsquare2disk(df, frandom(), frandom());
699     df[0] *= .5*dblur;
700     df[1] *= .5*dblur;
701 greg 2.85 if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) {
702     double adj = 1.0;
703     if (ourview.type == VT_PER)
704     adj /= DOT(thisray.rdir, ourview.vdir);
705 greg 2.86 df[0] /= sqrt(ourview.hn2);
706     df[1] /= sqrt(ourview.vn2);
707 greg 2.74 for (i = 3; i--; ) {
708 greg 2.85 vc = ourview.vp[i] + adj*vdist*thisray.rdir[i];
709 greg 2.86 thisray.rorg[i] += df[0]*ourview.hvec[i] +
710     df[1]*ourview.vvec[i] ;
711 greg 2.74 thisray.rdir[i] = vc - thisray.rorg[i];
712     }
713     } else { /* non-standard view case */
714     double dfd = PI/4.*dblur*(.5 - frandom());
715 greg 2.85 if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) {
716 greg 2.74 if (ourview.type != VT_CYL)
717 greg 2.86 df[0] /= sqrt(ourview.hn2);
718     df[1] /= sqrt(ourview.vn2);
719 greg 2.74 }
720     for (i = 3; i--; ) {
721 greg 2.85 vc = ourview.vp[i] + vdist*thisray.rdir[i];
722 greg 2.86 thisray.rorg[i] += df[0]*ourview.hvec[i] +
723     df[1]*ourview.vvec[i] +
724 greg 2.74 dfd*ourview.vdir[i] ;
725     thisray.rdir[i] = vc - thisray.rorg[i];
726     }
727 greg 2.73 }
728     if (normalize(thisray.rdir) == 0.0)
729     return(0.0);
730 gwlarson 2.49 }
731    
732 greg 2.75 rayorigin(&thisray, PRIMARY, NULL, NULL);
733 greg 1.25
734 greg 1.1 rayvalue(&thisray); /* trace ray */
735    
736     copycolor(col, thisray.rcol); /* return color */
737 greg 2.45
738 greg 1.20 return(thisray.rt); /* return distance */
739 greg 1.1 }
740    
741    
742 schorsch 2.69 static int
743     salvage( /* salvage scanlines from killed program */
744     char *oldfile
745     )
746 greg 1.1 {
747     COLR *scanline;
748     FILE *fp;
749     int x, y;
750    
751     if (oldfile == NULL)
752 greg 2.37 goto gotzip;
753    
754 greg 1.9 if ((fp = fopen(oldfile, "r")) == NULL) {
755 greg 1.1 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
756     error(WARNING, errmsg);
757 greg 2.37 goto gotzip;
758 greg 1.1 }
759 schorsch 2.55 SET_FILE_BINARY(fp);
760 greg 1.1 /* discard header */
761 greg 2.19 getheader(fp, NULL, NULL);
762 greg 1.1 /* get picture size */
763 greg 1.36 if (!fscnresolu(&x, &y, fp)) {
764 greg 2.29 sprintf(errmsg, "bad recover file \"%s\" - not removed",
765     oldfile);
766 greg 1.2 error(WARNING, errmsg);
767 greg 1.1 fclose(fp);
768 greg 2.37 goto gotzip;
769 greg 1.1 }
770    
771 greg 2.85 if ((x != hres) | (y != vres)) {
772 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
773     oldfile);
774     error(USER, errmsg);
775     }
776    
777 greg 2.8 scanline = (COLR *)malloc(hres*sizeof(COLR));
778 greg 1.1 if (scanline == NULL)
779     error(SYSTEM, "out of memory in salvage");
780 greg 2.8 for (y = 0; y < vres; y++) {
781     if (freadcolrs(scanline, hres, fp) < 0)
782 greg 1.1 break;
783 greg 2.8 if (fwritecolrs(scanline, hres, stdout) < 0)
784 greg 1.1 goto writerr;
785     }
786     if (fflush(stdout) == EOF)
787     goto writerr;
788 greg 2.52 free((void *)scanline);
789 greg 1.1 fclose(fp);
790     unlink(oldfile);
791     return(y);
792 greg 2.37 gotzip:
793     if (fflush(stdout) == EOF)
794     error(SYSTEM, "error writing picture header");
795     return(0);
796 greg 1.1 writerr:
797 greg 2.9 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
798     error(SYSTEM, errmsg);
799 schorsch 2.69 return -1; /* pro forma return */
800 greg 1.31 }
801    
802 schorsch 2.69 static int
803 greg 2.87 pixnumber( /* compute pixel index (screen door) */
804 greg 2.85 int x,
805     int y,
806 schorsch 2.69 int xres,
807     int yres
808     )
809 greg 1.31 {
810 greg 2.87 unsigned nbits = 0;
811     bitmask_t coord[2];
812    
813     if (xres < yres) xres = yres;
814     while (xres > 0) {
815     xres >>= 1;
816     ++nbits;
817     }
818     coord[0] = x; coord[1] = y;
819     return ((int)hilbert_c2i(2, nbits, coord));
820 greg 1.1 }