ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.77
Committed: Wed Apr 5 06:22:56 2006 UTC (18 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R8
Changes since 2.76: +3 -7 lines
Log Message:
Made -u+ option truly random

File Contents

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