ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.73
Committed: Tue Jan 18 00:33:16 2005 UTC (19 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.72: +21 -4 lines
Log Message:
Added -pd option for depth-of-field sampling and -vd focal distance entry

File Contents

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