ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.85
Committed: Mon Nov 26 07:04:07 2012 UTC (11 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.84: +33 -30 lines
Log Message:
Fixed bug in depth-of-field with non-zero fore clipping plane

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.85 static const char RCSid[] = "$Id: rpict.c,v 2.84 2011/02/25 06:51:50 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 schorsch 2.65 #include "rtmisc.h" /* myhostname() */
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.30 double u, s;
171     #ifdef BSD
172 greg 1.1 struct rusage rubuf;
173 greg 2.30 #else
174     struct tms tbuf;
175 greg 2.33 double period;
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.30 u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
182     s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
183 greg 1.1 getrusage(RUSAGE_CHILDREN, &rubuf);
184 greg 2.30 u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
185     s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
186     #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 #else
191     period = 1.0 / 60.0;
192     #endif
193 greg 2.33 u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
194     s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
195 greg 2.30 #endif
196 greg 1.1
197 greg 2.30 sprintf(errmsg,
198     "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
199     nrays, pctdone, u/3600., s/3600.,
200 greg 2.43 (tlastrept-tstart)/3600., myhostname());
201 greg 1.24 eputs(errmsg);
202 schorsch 2.56 #ifdef SIGCONT
203 greg 2.35 signal(SIGCONT, report);
204     #endif
205 greg 1.24 }
206 greg 1.1 #else
207 schorsch 2.69 static void
208 schorsch 2.71 report(int dummy) /* report progress */
209 greg 1.24 {
210 greg 2.32 tlastrept = time((time_t *)NULL);
211 greg 2.30 sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
212 greg 1.24 nrays, pctdone, (tlastrept-tstart)/3600.0);
213 greg 1.1 eputs(errmsg);
214     }
215 greg 1.24 #endif
216 greg 1.1
217    
218 schorsch 2.69 extern void
219     rpict( /* generate image(s) */
220     int seq,
221     char *pout,
222     char *zout,
223     char *prvr
224     )
225 greg 2.8 /*
226     * If seq is greater than zero, then we will render a sequence of
227     * images based on view parameter strings read from the standard input.
228     * If pout is NULL, then all images will be sent to the standard ouput.
229     * If seq is greater than zero and prvr is an integer, then it is the
230     * frame number at which rendering should begin. Preceeding view parameter
231     * strings will be skipped in the input.
232     * If pout and prvr are the same, prvr is renamed to avoid overwriting.
233     * Note that pout and zout should contain %d format specifications for
234     * sequenced file naming.
235     */
236     {
237 greg 2.9 char fbuf[128], fbuf2[128];
238 greg 2.26 int npicts;
239 greg 2.85 char *cp;
240 greg 2.14 RESOLU rs;
241     double pa;
242 greg 2.8 /* check sampling */
243     if (psample < 1)
244     psample = 1;
245     else if (psample > MAXDIV) {
246     sprintf(errmsg, "pixel sampling reduced from %d to %d",
247     psample, MAXDIV);
248     error(WARNING, errmsg);
249     psample = MAXDIV;
250     }
251     /* get starting frame */
252     if (seq <= 0)
253     seq = 0;
254     else if (prvr != NULL && isint(prvr)) {
255 greg 2.85 int rn; /* skip to specified view */
256 greg 2.8 if ((rn = atoi(prvr)) < seq)
257     error(USER, "recover frame less than start frame");
258     if (pout == NULL)
259     error(USER, "missing output file specification");
260     for ( ; seq < rn; seq++)
261     if (nextview(stdin) == EOF)
262     error(USER, "unexpected EOF on view input");
263 gwlarson 2.50 setview(&ourview);
264 greg 2.8 prvr = fbuf; /* mark for renaming */
265     }
266 schorsch 2.62 if ((pout != NULL) & (prvr != NULL)) {
267 greg 2.8 sprintf(fbuf, pout, seq);
268 greg 2.18 if (!strcmp(prvr, fbuf)) { /* rename */
269     strcpy(fbuf2, fbuf);
270     for (cp = fbuf2; *cp; cp++)
271     ;
272     while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
273     cp--;
274     strcpy(cp, RFTEMPLATE);
275 greg 2.8 prvr = mktemp(fbuf2);
276 schorsch 2.61 if (rename(fbuf, prvr) < 0) {
277 greg 2.28 if (errno == ENOENT) { /* ghost file */
278     sprintf(errmsg,
279     "new output file \"%s\"",
280     fbuf);
281     error(WARNING, errmsg);
282     prvr = NULL;
283     } else { /* serious error */
284     sprintf(errmsg,
285 greg 2.8 "cannot rename \"%s\" to \"%s\"",
286     fbuf, prvr);
287 greg 2.28 error(SYSTEM, errmsg);
288     }
289 schorsch 2.61 }
290 greg 2.8 }
291     }
292 greg 2.26 npicts = 0; /* render sequence */
293 greg 2.8 do {
294     if (seq && nextview(stdin) == EOF)
295     break;
296 greg 2.25 pctdone = 0.0;
297 greg 2.8 if (pout != NULL) {
298     sprintf(fbuf, pout, seq);
299 greg 2.27 if (file_exists(fbuf)) {
300     if (prvr != NULL || !strcmp(fbuf, pout)) {
301     sprintf(errmsg,
302     "output file \"%s\" exists",
303     fbuf);
304     error(USER, errmsg);
305     }
306 gwlarson 2.50 setview(&ourview);
307 greg 2.26 continue; /* don't clobber */
308 greg 2.27 }
309 greg 2.8 if (freopen(fbuf, "w", stdout) == NULL) {
310     sprintf(errmsg,
311     "cannot open output file \"%s\"", fbuf);
312     error(SYSTEM, errmsg);
313     }
314 schorsch 2.55 SET_FILE_BINARY(stdout);
315 greg 2.8 dupheader();
316     }
317     hres = hresolu; vres = vresolu; pa = pixaspect;
318 schorsch 2.61 if (prvr != NULL) {
319 greg 2.66 if (viewfile(prvr, &ourview, &rs) <= 0) {
320 greg 2.8 sprintf(errmsg,
321     "cannot recover view parameters from \"%s\"", prvr);
322     error(WARNING, errmsg);
323     } else {
324     pa = 0.0;
325     hres = scanlen(&rs);
326     vres = numscans(&rs);
327     }
328 schorsch 2.61 }
329 greg 2.9 if ((cp = setview(&ourview)) != NULL)
330     error(USER, cp);
331 greg 2.8 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
332     if (seq) {
333     if (ralrm > 0) {
334 greg 2.11 fprintf(stderr, "FRAME %d:", seq);
335     fprintview(&ourview, stderr);
336     putc('\n', stderr);
337     fflush(stderr);
338 greg 2.8 }
339     printf("FRAME=%d\n", seq);
340     }
341     fputs(VIEWSTR, stdout);
342     fprintview(&ourview, stdout);
343     putchar('\n');
344 greg 2.85 if ((pa < .99) | (pa > 1.01))
345 greg 2.8 fputaspect(pa, stdout);
346 schorsch 2.60 fputnow(stdout);
347 greg 2.8 fputformat(COLRFMT, stdout);
348     putchar('\n');
349     if (zout != NULL)
350 greg 2.9 sprintf(cp=fbuf, zout, seq);
351 greg 2.8 else
352 greg 2.9 cp = NULL;
353     render(cp, prvr);
354 greg 2.8 prvr = NULL;
355 greg 2.26 npicts++;
356 greg 2.8 } while (seq++);
357 greg 2.26 /* check that we did something */
358     if (npicts == 0)
359     error(WARNING, "no output produced");
360 greg 2.8 }
361    
362    
363 schorsch 2.69 static int
364     nextview( /* get next view from fp */
365     FILE *fp
366     )
367 greg 2.8 {
368 greg 2.9 char linebuf[256];
369 greg 2.8
370 schorsch 2.61 lastview = ourview;
371 greg 2.8 while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
372 greg 2.9 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
373 greg 2.8 return(0);
374     return(EOF);
375     }
376    
377    
378 schorsch 2.69 static void
379     render( /* render the scene */
380     char *zfile,
381     char *oldfile
382     )
383 greg 1.1 {
384     COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
385 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
386 greg 2.2 char *sampdens; /* previous sample density */
387 greg 1.1 int ypos; /* current scanline */
388 greg 1.15 int ystep; /* current y step size */
389 greg 1.33 int hstep; /* h step size */
390 greg 1.16 int zfd;
391 greg 1.1 COLOR *colptr;
392 greg 1.11 float *zptr;
393 greg 2.85 int i;
394 greg 2.52 /* check for empty image */
395 greg 2.85 if ((hres <= 0) | (vres <= 0)) {
396 greg 2.52 error(WARNING, "empty output picture");
397     fprtresolu(0, 0, stdout);
398     return;
399     }
400 greg 1.9 /* allocate scanlines */
401 greg 1.1 for (i = 0; i <= psample; i++) {
402 greg 2.8 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
403 greg 1.1 if (scanbar[i] == NULL)
404 greg 1.11 goto memerr;
405 greg 1.1 }
406 greg 2.2 hstep = (psample*140+49)/99; /* quincunx sampling */
407     ystep = (psample*99+70)/140;
408     if (hstep > 2) {
409 greg 2.8 i = hres/hstep + 2;
410 greg 2.52 if ((sampdens = (char *)malloc(i)) == NULL)
411 greg 2.2 goto memerr;
412     while (i--)
413     sampdens[i] = hstep;
414     } else
415     sampdens = NULL;
416 greg 2.26 /* open z-file */
417 greg 1.11 if (zfile != NULL) {
418 greg 1.16 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
419 greg 2.26 sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
420 greg 1.11 error(SYSTEM, errmsg);
421     }
422 schorsch 2.55 SET_FD_BINARY(zfd);
423 greg 1.11 for (i = 0; i <= psample; i++) {
424 greg 2.8 zbar[i] = (float *)malloc(hres*sizeof(float));
425 greg 1.11 if (zbar[i] == NULL)
426     goto memerr;
427     }
428     } else {
429 greg 1.16 zfd = -1;
430 greg 1.11 for (i = 0; i <= psample; i++)
431     zbar[i] = NULL;
432     }
433 greg 1.1 /* write out boundaries */
434 greg 2.8 fprtresolu(hres, vres, stdout);
435 greg 1.10 /* recover file and compute first */
436 greg 1.11 i = salvage(oldfile);
437 greg 2.42 if (i >= vres)
438     goto alldone;
439 greg 2.85 if ((zfd != -1) & (i > 0) &&
440 greg 2.64 lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
441 greg 2.26 error(SYSTEM, "z-file seek error in render");
442 greg 2.8 pctdone = 100.0*i/vres;
443 greg 1.24 if (ralrm > 0) /* report init stats */
444 schorsch 2.69 report(0);
445 schorsch 2.56 #ifdef SIGCONT
446 greg 1.32 else
447 schorsch 2.56 signal(SIGCONT, report);
448 greg 1.32 #endif
449 greg 2.47 ypos = vres-1 - i; /* initialize sampling */
450 gregl 2.48 if (directvis)
451     init_drawsources(psample);
452 greg 2.8 fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
453 greg 1.10 /* compute scanlines */
454 greg 1.15 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
455     /* bottom adjust? */
456     if (ypos < 0) {
457     ystep += ypos;
458     ypos = 0;
459     }
460     colptr = scanbar[ystep]; /* move base to top */
461     scanbar[ystep] = scanbar[0];
462 greg 1.1 scanbar[0] = colptr;
463 greg 1.15 zptr = zbar[ystep];
464     zbar[ystep] = zbar[0];
465 greg 1.11 zbar[0] = zptr;
466 greg 1.10 /* fill base line */
467 greg 2.2 fillscanline(scanbar[0], zbar[0], sampdens,
468 greg 2.8 hres, ypos, hstep);
469 greg 1.10 /* fill bar */
470 greg 2.8 fillscanbar(scanbar, zbar, hres, ypos, ystep);
471 gregl 2.48 if (directvis) /* add bitty sources */
472     drawsources(scanbar, zbar, 0, hres, ypos, ystep);
473 greg 1.10 /* write it out */
474 schorsch 2.56 #ifdef SIGCONT
475 greg 2.22 signal(SIGCONT, SIG_IGN); /* don't interrupt writes */
476 greg 1.32 #endif
477 greg 1.15 for (i = ystep; i > 0; i--) {
478 greg 1.17 if (zfd != -1 && write(zfd, (char *)zbar[i],
479 greg 2.8 hres*sizeof(float))
480     < hres*sizeof(float))
481 greg 1.1 goto writerr;
482 greg 2.8 if (fwritescan(scanbar[i], hres, stdout) < 0)
483 greg 1.11 goto writerr;
484     }
485 greg 1.1 if (fflush(stdout) == EOF)
486     goto writerr;
487 greg 1.24 /* record progress */
488 greg 2.8 pctdone = 100.0*(vres-1-ypos)/vres;
489 greg 2.32 if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
490 schorsch 2.69 report(0);
491 schorsch 2.56 #ifdef SIGCONT
492 greg 1.32 else
493 greg 2.22 signal(SIGCONT, report);
494 greg 1.32 #endif
495 greg 1.1 }
496 greg 1.11 /* clean up */
497 schorsch 2.56 #ifdef SIGCONT
498 greg 2.22 signal(SIGCONT, SIG_IGN);
499 schorsch 2.56 #endif
500 greg 2.42 if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
501     < hres*sizeof(float))
502     goto writerr;
503     fwritescan(scanbar[0], hres, stdout);
504     if (fflush(stdout) == EOF)
505     goto writerr;
506     alldone:
507 greg 1.16 if (zfd != -1) {
508 greg 1.20 if (close(zfd) == -1)
509     goto writerr;
510 greg 1.11 for (i = 0; i <= psample; i++)
511 greg 2.52 free((void *)zbar[i]);
512 greg 1.11 }
513 greg 1.1 for (i = 0; i <= psample; i++)
514 greg 2.52 free((void *)scanbar[i]);
515 greg 2.2 if (sampdens != NULL)
516     free(sampdens);
517 greg 1.11 pctdone = 100.0;
518 greg 2.13 if (ralrm > 0)
519 schorsch 2.69 report(0);
520 schorsch 2.56 #ifdef SIGCONT
521 greg 2.23 signal(SIGCONT, SIG_DFL);
522 schorsch 2.56 #endif
523 greg 1.1 return;
524     writerr:
525     error(SYSTEM, "write error in render");
526 greg 1.11 memerr:
527     error(SYSTEM, "out of memory in render");
528 greg 1.1 }
529    
530    
531 schorsch 2.69 static void
532     fillscanline( /* fill scan at y */
533 greg 2.85 COLOR *scanline,
534     float *zline,
535     char *sd,
536 schorsch 2.69 int xres,
537     int y,
538     int xstep
539     )
540 greg 1.1 {
541 greg 1.33 static int nc = 0; /* number of calls */
542 greg 2.2 int bl = xstep, b = xstep;
543 greg 2.14 double z;
544 greg 2.85 int i;
545 greg 2.45
546 greg 1.11 z = pixvalue(scanline[0], 0, y);
547     if (zline) zline[0] = z;
548 greg 1.33 /* zig-zag start for quincunx pattern */
549 greg 2.2 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
550 greg 1.15 if (i >= xres) {
551     xstep += xres-1-i;
552     i = xres-1;
553     }
554 greg 1.11 z = pixvalue(scanline[i], i, y);
555     if (zline) zline[i] = z;
556 greg 2.2 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
557 greg 2.3 if (i <= xstep)
558     b = fillsample(scanline, zline, 0, y, i, 0, b/2);
559     else
560     b = fillsample(scanline+i-xstep,
561 greg 2.38 zline ? zline+i-xstep : (float *)NULL,
562 greg 2.3 i-xstep, y, xstep, 0, b/2);
563 greg 2.2 if (sd) *sd++ = nc & 1 ? bl : b;
564     bl = b;
565 greg 1.1 }
566 greg 2.2 if (sd && nc & 1) *sd = bl;
567 greg 1.1 }
568    
569    
570 schorsch 2.69 static void
571     fillscanbar( /* fill interior */
572 greg 2.85 COLOR *scanbar[],
573     float *zbar[],
574 schorsch 2.69 int xres,
575     int y,
576     int ysize
577     )
578 greg 1.1 {
579     COLOR vline[MAXDIV+1];
580 greg 1.11 float zline[MAXDIV+1];
581 greg 1.1 int b = ysize;
582 greg 2.85 int i, j;
583 greg 2.45
584 greg 1.8 for (i = 0; i < xres; i++) {
585 greg 1.1 copycolor(vline[0], scanbar[0][i]);
586     copycolor(vline[ysize], scanbar[ysize][i]);
587 greg 1.11 if (zbar[0]) {
588     zline[0] = zbar[0][i];
589     zline[ysize] = zbar[ysize][i];
590     }
591 greg 2.38 b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
592 greg 1.11 i, y, 0, ysize, b/2);
593 greg 2.45
594 greg 1.1 for (j = 1; j < ysize; j++)
595     copycolor(scanbar[j][i], vline[j]);
596 greg 1.11 if (zbar[0])
597     for (j = 1; j < ysize; j++)
598     zbar[j][i] = zline[j];
599 greg 1.1 }
600     }
601    
602    
603 schorsch 2.69 static int
604     fillsample( /* fill interior points */
605 greg 2.85 COLOR *colline,
606     float *zline,
607 schorsch 2.69 int x,
608     int y,
609     int xlen,
610     int ylen,
611     int b
612     )
613 greg 1.1 {
614 greg 2.14 double ratio;
615     double z;
616 greg 1.1 COLOR ctmp;
617     int ncut;
618 greg 2.85 int len;
619 greg 2.45
620 greg 1.1 if (xlen > 0) /* x or y length is zero */
621     len = xlen;
622     else
623     len = ylen;
624 greg 2.45
625 greg 1.1 if (len <= 1) /* limit recursion */
626     return(0);
627 greg 2.45
628     if (b > 0 ||
629     (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
630 greg 1.11 || bigdiff(colline[0], colline[len], maxdiff)) {
631 greg 2.45
632 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
633     if (zline) zline[len>>1] = z;
634 greg 1.1 ncut = 1;
635     } else { /* interpolate */
636     copycolor(colline[len>>1], colline[len]);
637     ratio = (double)(len>>1) / len;
638     scalecolor(colline[len>>1], ratio);
639 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
640     ratio = 1.0 - ratio;
641 greg 1.1 copycolor(ctmp, colline[0]);
642     scalecolor(ctmp, ratio);
643     addcolor(colline[len>>1], ctmp);
644 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
645 greg 1.1 ncut = 0;
646     }
647     /* recurse */
648 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
649 greg 2.45
650 greg 2.38 ncut += fillsample(colline+(len>>1),
651     zline ? zline+(len>>1) : (float *)NULL,
652 greg 1.11 x+(xlen>>1), y+(ylen>>1),
653     xlen-(xlen>>1), ylen-(ylen>>1), b/2);
654 greg 1.1
655     return(ncut);
656     }
657    
658    
659 schorsch 2.69 static double
660     pixvalue( /* compute pixel value */
661     COLOR col, /* returned color */
662     int x, /* pixel position */
663     int y
664     )
665 greg 1.1 {
666 gwlarson 2.49 RAY thisray;
667     FVECT lorg, ldir;
668 greg 2.73 double hpos, vpos, vdist, lmax;
669 greg 2.85 int i;
670 gwlarson 2.49 /* compute view ray */
671 greg 2.85 setcolor(col, 0.0, 0.0, 0.0);
672 gwlarson 2.49 hpos = (x+pixjitter())/hres;
673     vpos = (y+pixjitter())/vres;
674     if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
675 greg 2.85 &ourview, hpos, vpos)) < -FTINY)
676 greg 1.22 return(0.0);
677 greg 2.85
678 greg 2.73 vdist = ourview.vdist;
679 greg 2.77 /* set pixel index */
680     samplendx = pixnumber(x,y,hres,vres);
681 gwlarson 2.49 /* optional motion blur */
682     if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
683     &lastview, hpos, vpos)) >= -FTINY) {
684 greg 2.85 double d = mblur*(.5-urand(281+samplendx));
685 gwlarson 2.49
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 greg 2.85 if (dblur > FTINY) {
697 greg 2.74 double vc, dfh, dfv;
698 greg 2.81 /* square/circle conv. */
699 greg 2.82 dfh = vc = 1. - 2.*frandom();
700     dfv = 1. - 2.*frandom();
701 greg 2.81 dfh *= .5*dblur*sqrt(1. - .5*dfv*dfv);
702     dfv *= .5*dblur*sqrt(1. - .5*vc*vc);
703 greg 2.85 if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) {
704     double adj = 1.0;
705     if (ourview.type == VT_PER)
706     adj /= DOT(thisray.rdir, ourview.vdir);
707 greg 2.74 dfh /= sqrt(ourview.hn2);
708     dfv /= sqrt(ourview.vn2);
709     for (i = 3; i--; ) {
710 greg 2.85 vc = ourview.vp[i] + adj*vdist*thisray.rdir[i];
711 greg 2.74 thisray.rorg[i] += dfh*ourview.hvec[i] +
712     dfv*ourview.vvec[i] ;
713     thisray.rdir[i] = vc - thisray.rorg[i];
714     }
715     } else { /* non-standard view case */
716     double dfd = PI/4.*dblur*(.5 - frandom());
717 greg 2.85 if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) {
718 greg 2.74 if (ourview.type != VT_CYL)
719     dfh /= sqrt(ourview.hn2);
720     dfv /= sqrt(ourview.vn2);
721     }
722     for (i = 3; i--; ) {
723 greg 2.85 vc = ourview.vp[i] + vdist*thisray.rdir[i];
724 greg 2.74 thisray.rorg[i] += dfh*ourview.hvec[i] +
725     dfv*ourview.vvec[i] +
726     dfd*ourview.vdir[i] ;
727     thisray.rdir[i] = vc - thisray.rorg[i];
728     }
729 greg 2.73 }
730     if (normalize(thisray.rdir) == 0.0)
731     return(0.0);
732 gwlarson 2.49 }
733    
734 greg 2.75 rayorigin(&thisray, PRIMARY, NULL, NULL);
735 greg 1.25
736 greg 1.1 rayvalue(&thisray); /* trace ray */
737    
738     copycolor(col, thisray.rcol); /* return color */
739 greg 2.45
740 greg 1.20 return(thisray.rt); /* return distance */
741 greg 1.1 }
742    
743    
744 schorsch 2.69 static int
745     salvage( /* salvage scanlines from killed program */
746     char *oldfile
747     )
748 greg 1.1 {
749     COLR *scanline;
750     FILE *fp;
751     int x, y;
752    
753     if (oldfile == NULL)
754 greg 2.37 goto gotzip;
755    
756 greg 1.9 if ((fp = fopen(oldfile, "r")) == NULL) {
757 greg 1.1 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
758     error(WARNING, errmsg);
759 greg 2.37 goto gotzip;
760 greg 1.1 }
761 schorsch 2.55 SET_FILE_BINARY(fp);
762 greg 1.1 /* discard header */
763 greg 2.19 getheader(fp, NULL, NULL);
764 greg 1.1 /* get picture size */
765 greg 1.36 if (!fscnresolu(&x, &y, fp)) {
766 greg 2.29 sprintf(errmsg, "bad recover file \"%s\" - not removed",
767     oldfile);
768 greg 1.2 error(WARNING, errmsg);
769 greg 1.1 fclose(fp);
770 greg 2.37 goto gotzip;
771 greg 1.1 }
772    
773 greg 2.85 if ((x != hres) | (y != vres)) {
774 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
775     oldfile);
776     error(USER, errmsg);
777     }
778    
779 greg 2.8 scanline = (COLR *)malloc(hres*sizeof(COLR));
780 greg 1.1 if (scanline == NULL)
781     error(SYSTEM, "out of memory in salvage");
782 greg 2.8 for (y = 0; y < vres; y++) {
783     if (freadcolrs(scanline, hres, fp) < 0)
784 greg 1.1 break;
785 greg 2.8 if (fwritecolrs(scanline, hres, stdout) < 0)
786 greg 1.1 goto writerr;
787     }
788     if (fflush(stdout) == EOF)
789     goto writerr;
790 greg 2.52 free((void *)scanline);
791 greg 1.1 fclose(fp);
792     unlink(oldfile);
793     return(y);
794 greg 2.37 gotzip:
795     if (fflush(stdout) == EOF)
796     error(SYSTEM, "error writing picture header");
797     return(0);
798 greg 1.1 writerr:
799 greg 2.9 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
800     error(SYSTEM, errmsg);
801 schorsch 2.69 return -1; /* pro forma return */
802 greg 1.31 }
803    
804 schorsch 2.69 static int
805     pixnumber( /* compute pixel index (brushed) */
806 greg 2.85 int x,
807     int y,
808 schorsch 2.69 int xres,
809     int yres
810     )
811 greg 1.31 {
812     x -= y;
813     while (x < 0)
814     x += xres;
815     return((((x>>2)*yres + y) << 2) + (x & 3));
816 greg 1.1 }