ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
(Generate patch)

Comparing ray/src/rt/rpict.c (file contents):
Revision 1.4 by greg, Wed Jun 7 21:00:57 1989 UTC vs.
Revision 2.36 by greg, Sun Dec 12 13:56:08 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 12 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include  "ray.h"
14  
15 + #include  <sys/types.h>
16 +
17 + #ifndef NIX
18   #ifdef BSD
19   #include  <sys/time.h>
20   #include  <sys/resource.h>
21 + #else
22 + #include  <sys/times.h>
23 + #include  <sys/utsname.h>
24 + #include  <unistd.h>
25   #endif
26 + #endif
27  
28 + extern time_t   time();
29 +
30 + #include  <signal.h>
31 +
32   #include  "view.h"
33  
34 + #include  "resolu.h"
35 +
36   #include  "random.h"
37  
38 < VIEW  ourview = STDVIEW(512);           /* view parameters */
38 > #include  "paths.h"
39  
40 + #define  RFTEMPLATE     "rfXXXXXX"
41 +
42 + #ifndef SIGCONT
43 + #define SIGCONT         SIGIO
44 + #endif
45 +
46 + int  dimlist[MAXDIM];                   /* sampling dimensions */
47 + int  ndims = 0;                         /* number of sampling dimensions */
48 + int  samplendx;                         /* sample index number */
49 +
50 + VIEW  ourview = STDVIEW;                /* view parameters */
51 + int  hresolu = 512;                     /* horizontal resolution */
52 + int  vresolu = 512;                     /* vertical resolution */
53 + double  pixaspect = 1.0;                /* pixel aspect ratio */
54 +
55   int  psample = 4;                       /* pixel sample size */
56 < double  maxdiff = .05;                  /* max. difference for interpolation */
57 < double  dstrpix = 0.67;                 /* square pixel distribution */
56 > double  maxdiff = .05;                  /* max. difference for interpolation */
57 > double  dstrpix = 0.67;                 /* square pixel distribution */
58  
59 < double  dstrsrc = 0.0;                  /* square source distribution */
60 < double  shadthresh = .05;               /* shadow threshold */
59 > double  dstrsrc = 0.0;                  /* square source distribution */
60 > double  shadthresh = .05;               /* shadow threshold */
61 > double  shadcert = .5;                  /* shadow certainty */
62 > int  directrelay = 1;                   /* number of source relays */
63 > int  vspretest = 512;                   /* virtual source pretest density */
64 > int  directvis = 1;                     /* sources visible? */
65 > double  srcsizerat = .25;               /* maximum ratio source size/dist. */
66  
67 + double  specthresh = .15;               /* specular sampling threshold */
68 + double  specjitter = 1.;                /* specular sampling jitter */
69 +
70   int  maxdepth = 6;                      /* maximum recursion depth */
71 < double  minweight = 5e-3;               /* minimum ray weight */
71 > double  minweight = 5e-3;               /* minimum ray weight */
72  
73   COLOR  ambval = BLKCOLOR;               /* ambient value */
74 < double  ambacc = 0.2;                   /* ambient accuracy */
75 < int  ambres = 128;                      /* ambient resolution */
74 > double  ambacc = 0.2;                   /* ambient accuracy */
75 > int  ambres = 32;                       /* ambient resolution */
76   int  ambdiv = 128;                      /* ambient divisions */
77   int  ambssamp = 0;                      /* ambient super-samples */
78   int  ambounce = 0;                      /* ambient bounces */
79   char  *amblist[128];                    /* ambient include/exclude list */
80   int  ambincl = -1;                      /* include == 1, exclude == 0 */
81  
82 + #ifdef MSDOS
83 + int  ralrm = 60;                        /* seconds between reports */
84 + #else
85   int  ralrm = 0;                         /* seconds between reports */
86 + #endif
87  
88 < double  pctdone = 0.0;                  /* percentage done */
88 > double  pctdone = 0.0;                  /* percentage done */
89  
90 < extern long  nrays;                     /* number of rays traced */
90 > time_t  tlastrept = 0L;                 /* time at last report */
91  
92 < #define  MAXDIV         32              /* maximum sample size */
92 > extern time_t  tstart;                  /* starting time */
93  
94 < #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
94 > extern unsigned long  nrays;            /* number of rays traced */
95  
96 + #define  MAXDIV         16              /* maximum sample size */
97  
98 + #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
99 +
100 + static int  hres, vres;                 /* resolution for this frame */
101 +
102 + extern char  *mktemp();
103 +
104 + double  pixvalue();
105 +
106 + #ifdef NIX
107 + #define  file_exists(f) (access(f,F_OK)==0)
108 + #else
109 + #include  <sys/types.h>
110 + #include  <sys/stat.h>
111 + int
112 + file_exists(fname)                              /* ordinary file exists? */
113 + char  *fname;
114 + {
115 +        struct stat  sbuf;
116 +        if (stat(fname, &sbuf) < 0) return(0);
117 +        return((sbuf.st_mode & S_IFREG) != 0);
118 + }
119 + #endif
120 +
121 +
122   quit(code)                      /* quit program */
123   int  code;
124   {
125 <        if (code || ralrm > 0)          /* report status */
125 >        if (code)                       /* report status */
126                  report();
127 <
127 >        headclean();                    /* delete header file */
128 >        pfclean();                      /* clean up persist files */
129          exit(code);
130   }
131  
132  
133 + #ifndef NIX
134   report()                /* report progress */
135   {
136 +        double  u, s;
137   #ifdef BSD
138 +        char  hostname[257];
139          struct rusage  rubuf;
140 <        double  t;
140 > #else
141 >        struct tms  tbuf;
142 >        struct utsname  nambuf;
143 >        double  period;
144 > #define hostname  nambuf.nodename
145 > #endif
146  
147 +        tlastrept = time((time_t *)NULL);
148 + #ifdef BSD
149          getrusage(RUSAGE_SELF, &rubuf);
150 <        t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
151 <        t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
150 >        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
151 >        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
152          getrusage(RUSAGE_CHILDREN, &rubuf);
153 <        t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
154 <        t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
155 <
79 <        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
80 <                        nrays, pctdone, t/3600.0);
153 >        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
154 >        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
155 >        gethostname(hostname, sizeof(hostname));
156   #else
157 <        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
157 >        times(&tbuf);
158 > #ifdef _SC_CLK_TCK
159 >        period = 1.0 / sysconf(_SC_CLK_TCK);
160 > #else
161 >        period = 1.0 / 60.0;
162   #endif
163 +        u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
164 +        s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
165 +        uname(&nambuf);
166 + #endif
167 +
168 +        sprintf(errmsg,
169 +                "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
170 +                        nrays, pctdone, u/3600., s/3600.,
171 +                        (tlastrept-tstart)/3600., hostname);
172          eputs(errmsg);
173 + #ifndef BSD
174 +        signal(SIGCONT, report);
175 + #undef hostname
176 + #endif
177 + }
178 + #else
179 + report()                /* report progress */
180 + {
181 +        tlastrept = time((time_t *)NULL);
182 +        sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
183 +                        nrays, pctdone, (tlastrept-tstart)/3600.0);
184 +        eputs(errmsg);
185 + }
186 + #endif
187  
188 <        if (ralrm > 0)
189 <                alarm(ralrm);
188 >
189 > rpict(seq, pout, zout, prvr)                    /* generate image(s) */
190 > int  seq;
191 > char  *pout, *zout, *prvr;
192 > /*
193 > * If seq is greater than zero, then we will render a sequence of
194 > * images based on view parameter strings read from the standard input.
195 > * If pout is NULL, then all images will be sent to the standard ouput.
196 > * If seq is greater than zero and prvr is an integer, then it is the
197 > * frame number at which rendering should begin.  Preceeding view parameter
198 > * strings will be skipped in the input.
199 > * If pout and prvr are the same, prvr is renamed to avoid overwriting.
200 > * Note that pout and zout should contain %d format specifications for
201 > * sequenced file naming.
202 > */
203 > {
204 >        extern char  *rindex(), *strncpy(), *strcat(), *strcpy();
205 >        char  fbuf[128], fbuf2[128];
206 >        int  npicts;
207 >        register char  *cp;
208 >        RESOLU  rs;
209 >        double  pa;
210 >                                        /* check sampling */
211 >        if (psample < 1)
212 >                psample = 1;
213 >        else if (psample > MAXDIV) {
214 >                sprintf(errmsg, "pixel sampling reduced from %d to %d",
215 >                                psample, MAXDIV);
216 >                error(WARNING, errmsg);
217 >                psample = MAXDIV;
218 >        }
219 >                                        /* get starting frame */
220 >        if (seq <= 0)
221 >                seq = 0;
222 >        else if (prvr != NULL && isint(prvr)) {
223 >                register int  rn;               /* skip to specified view */
224 >                if ((rn = atoi(prvr)) < seq)
225 >                        error(USER, "recover frame less than start frame");
226 >                if (pout == NULL)
227 >                        error(USER, "missing output file specification");
228 >                for ( ; seq < rn; seq++)
229 >                        if (nextview(stdin) == EOF)
230 >                                error(USER, "unexpected EOF on view input");
231 >                prvr = fbuf;                    /* mark for renaming */
232 >        }
233 >        if (pout != NULL & prvr != NULL) {
234 >                sprintf(fbuf, pout, seq);
235 >                if (!strcmp(prvr, fbuf)) {      /* rename */
236 >                        strcpy(fbuf2, fbuf);
237 >                        for (cp = fbuf2; *cp; cp++)
238 >                                ;
239 >                        while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
240 >                                cp--;
241 >                        strcpy(cp, RFTEMPLATE);
242 >                        prvr = mktemp(fbuf2);
243 >                        if (rename(fbuf, prvr) < 0)
244 >                                if (errno == ENOENT) {  /* ghost file */
245 >                                        sprintf(errmsg,
246 >                                                "new output file \"%s\"",
247 >                                                fbuf);
248 >                                        error(WARNING, errmsg);
249 >                                        prvr = NULL;
250 >                                } else {                /* serious error */
251 >                                        sprintf(errmsg,
252 >                                        "cannot rename \"%s\" to \"%s\"",
253 >                                                fbuf, prvr);
254 >                                        error(SYSTEM, errmsg);
255 >                                }
256 >                }
257 >        }
258 >        npicts = 0;                     /* render sequence */
259 >        do {
260 >                if (seq && nextview(stdin) == EOF)
261 >                        break;
262 >                pctdone = 0.0;
263 >                if (pout != NULL) {
264 >                        sprintf(fbuf, pout, seq);
265 >                        if (file_exists(fbuf)) {
266 >                                if (prvr != NULL || !strcmp(fbuf, pout)) {
267 >                                        sprintf(errmsg,
268 >                                                "output file \"%s\" exists",
269 >                                                fbuf);
270 >                                        error(USER, errmsg);
271 >                                }
272 >                                continue;               /* don't clobber */
273 >                        }
274 >                        if (freopen(fbuf, "w", stdout) == NULL) {
275 >                                sprintf(errmsg,
276 >                                        "cannot open output file \"%s\"", fbuf);
277 >                                error(SYSTEM, errmsg);
278 >                        }
279 > #ifdef MSDOS
280 >                        setmode(fileno(stdout), O_BINARY);
281 > #endif
282 >                        dupheader();
283 >                }
284 >                hres = hresolu; vres = vresolu; pa = pixaspect;
285 >                if (prvr != NULL)
286 >                        if (viewfile(prvr, &ourview, &rs) <= 0
287 >                                        || rs.or != PIXSTANDARD) {
288 >                                sprintf(errmsg,
289 >                        "cannot recover view parameters from \"%s\"", prvr);
290 >                                error(WARNING, errmsg);
291 >                        } else {
292 >                                pa = 0.0;
293 >                                hres = scanlen(&rs);
294 >                                vres = numscans(&rs);
295 >                        }
296 >                if ((cp = setview(&ourview)) != NULL)
297 >                        error(USER, cp);
298 >                normaspect(viewaspect(&ourview), &pa, &hres, &vres);
299 >                if (seq) {
300 >                        if (ralrm > 0) {
301 >                                fprintf(stderr, "FRAME %d:", seq);
302 >                                fprintview(&ourview, stderr);
303 >                                putc('\n', stderr);
304 >                                fflush(stderr);
305 >                        }
306 >                        printf("FRAME=%d\n", seq);
307 >                }
308 >                fputs(VIEWSTR, stdout);
309 >                fprintview(&ourview, stdout);
310 >                putchar('\n');
311 >                if (pa < .99 || pa > 1.01)
312 >                        fputaspect(pa, stdout);
313 >                fputformat(COLRFMT, stdout);
314 >                putchar('\n');
315 >                if (zout != NULL)
316 >                        sprintf(cp=fbuf, zout, seq);
317 >                else
318 >                        cp = NULL;
319 >                render(cp, prvr);
320 >                prvr = NULL;
321 >                npicts++;
322 >        } while (seq++);
323 >                                        /* check that we did something */
324 >        if (npicts == 0)
325 >                error(WARNING, "no output produced");
326   }
327  
328  
329 < render(oldfile)                         /* render the scene */
330 < char  *oldfile;
329 > nextview(fp)                            /* get next view from fp */
330 > FILE  *fp;
331   {
332 +        char  linebuf[256];
333 +
334 +        while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
335 +                if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
336 +                        return(0);
337 +        return(EOF);
338 + }      
339 +
340 +
341 + render(zfile, oldfile)                          /* render the scene */
342 + char  *zfile, *oldfile;
343 + {
344 +        extern long  lseek();
345          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
346 +        float  *zbar[MAXDIV+1];         /* z values */
347 +        char  *sampdens;                /* previous sample density */
348          int  ypos;                      /* current scanline */
349 +        int  ystep;                     /* current y step size */
350 +        int  hstep;                     /* h step size */
351 +        int  zfd;
352          COLOR  *colptr;
353 +        float  *zptr;
354          register int  i;
355 <
99 <        if (psample < 1)
100 <                psample = 1;
101 <        else if (psample > MAXDIV)
102 <                psample = MAXDIV;
103 <
104 <        ourview.hresolu -= ourview.hresolu % psample;
105 <        ourview.vresolu -= ourview.vresolu % psample;
106 <                
355 >                                        /* allocate scanlines */
356          for (i = 0; i <= psample; i++) {
357 <                scanbar[i] = (COLOR *)malloc((ourview.hresolu+1)*sizeof(COLOR));
357 >                scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
358                  if (scanbar[i] == NULL)
359 <                        error(SYSTEM, "out of memory in render");
359 >                        goto memerr;
360          }
361 <        
361 >        hstep = (psample*140+49)/99;            /* quincunx sampling */
362 >        ystep = (psample*99+70)/140;
363 >        if (hstep > 2) {
364 >                i = hres/hstep + 2;
365 >                if ((sampdens = malloc(i)) == NULL)
366 >                        goto memerr;
367 >                while (i--)
368 >                        sampdens[i] = hstep;
369 >        } else
370 >                sampdens = NULL;
371 >                                        /* open z-file */
372 >        if (zfile != NULL) {
373 >                if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
374 >                        sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
375 >                        error(SYSTEM, errmsg);
376 >                }
377 > #ifdef MSDOS
378 >                setmode(zfd, O_BINARY);
379 > #endif
380 >                for (i = 0; i <= psample; i++) {
381 >                        zbar[i] = (float *)malloc(hres*sizeof(float));
382 >                        if (zbar[i] == NULL)
383 >                                goto memerr;
384 >                }
385 >        } else {
386 >                zfd = -1;
387 >                for (i = 0; i <= psample; i++)
388 >                        zbar[i] = NULL;
389 >        }
390                                          /* write out boundaries */
391 <        printf("-Y %d +X %d\n", ourview.vresolu, ourview.hresolu);
392 <
393 <        ypos = ourview.vresolu - salvage(oldfile);      /* find top line */
394 <        fillscanline(scanbar[0], ypos, psample);        /* top scan */
395 <
396 <        for (ypos -= psample; ypos > -psample; ypos -= psample) {
397 <        
398 <                colptr = scanbar[psample];              /* get last scanline */
399 <                scanbar[psample] = scanbar[0];
391 >        fprtresolu(hres, vres, stdout);
392 >                                        /* recover file and compute first */
393 >        i = salvage(oldfile);
394 >        if (zfd != -1 && i > 0 &&
395 >                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
396 >                error(SYSTEM, "z-file seek error in render");
397 >        pctdone = 100.0*i/vres;
398 >        if (ralrm > 0)                  /* report init stats */
399 >                report();
400 > #ifndef  BSD
401 >        else
402 > #endif
403 >        signal(SIGCONT, report);
404 >        ypos = vres-1 - i;
405 >        fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
406 >                                                /* compute scanlines */
407 >        for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
408 >                                                        /* bottom adjust? */
409 >                if (ypos < 0) {
410 >                        ystep += ypos;
411 >                        ypos = 0;
412 >                }
413 >                colptr = scanbar[ystep];                /* move base to top */
414 >                scanbar[ystep] = scanbar[0];
415                  scanbar[0] = colptr;
416 <
417 <                fillscanline(scanbar[0], ypos, psample);        /* base scan */
418 <        
419 <                fillscanbar(scanbar, ypos, psample);
420 <                
421 <                for (i = psample-1; i >= 0; i--)
422 <                        if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
416 >                zptr = zbar[ystep];
417 >                zbar[ystep] = zbar[0];
418 >                zbar[0] = zptr;
419 >                                                        /* fill base line */
420 >                fillscanline(scanbar[0], zbar[0], sampdens,
421 >                                hres, ypos, hstep);
422 >                                                        /* fill bar */
423 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
424 >                                                        /* write it out */
425 > #ifndef  BSD
426 >                signal(SIGCONT, SIG_IGN);       /* don't interrupt writes */
427 > #endif
428 >                for (i = ystep; i > 0; i--) {
429 >                        if (zfd != -1 && write(zfd, (char *)zbar[i],
430 >                                        hres*sizeof(float))
431 >                                        < hres*sizeof(float))
432                                  goto writerr;
433 +                        if (fwritescan(scanbar[i], hres, stdout) < 0)
434 +                                goto writerr;
435 +                }
436                  if (fflush(stdout) == EOF)
437                          goto writerr;
438 <                pctdone = 100.0*(ourview.vresolu-ypos)/ourview.vresolu;
438 >                                                        /* record progress */
439 >                pctdone = 100.0*(vres-1-ypos)/vres;
440 >                if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
441 >                        report();
442 > #ifndef  BSD
443 >                else
444 >                        signal(SIGCONT, report);
445 > #endif
446          }
447 <                
447 >                                                /* clean up */
448 >        signal(SIGCONT, SIG_IGN);
449 >        if (zfd != -1) {
450 >                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
451 >                                < hres*sizeof(float))
452 >                        goto writerr;
453 >                if (close(zfd) == -1)
454 >                        goto writerr;
455 >                for (i = 0; i <= psample; i++)
456 >                        free((char *)zbar[i]);
457 >        }
458 >        fwritescan(scanbar[0], hres, stdout);
459 >        if (fflush(stdout) == EOF)
460 >                goto writerr;
461          for (i = 0; i <= psample; i++)
462                  free((char *)scanbar[i]);
463 +        if (sampdens != NULL)
464 +                free(sampdens);
465 +        pctdone = 100.0;
466 +        if (ralrm > 0)
467 +                report();
468 +        signal(SIGCONT, SIG_DFL);
469          return;
470   writerr:
471          error(SYSTEM, "write error in render");
472 + memerr:
473 +        error(SYSTEM, "out of memory in render");
474   }
475  
476  
477 < fillscanline(scanline, y, xstep)                /* fill scan line at y */
478 < register COLOR  *scanline;
479 < int  y, xstep;
477 > fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
478 > register COLOR  *scanline;
479 > register float  *zline;
480 > register char  *sd;
481 > int  xres, y, xstep;
482   {
483 <        int  b = xstep;
483 >        static int  nc = 0;             /* number of calls */
484 >        int  bl = xstep, b = xstep;
485 >        double  z;
486          register int  i;
487          
488 <        pixvalue(scanline[0], 0, y);
489 <
490 <        for (i = xstep; i <= ourview.hresolu; i += xstep) {
491 <        
492 <                pixvalue(scanline[i], i, y);
493 <                
494 <                b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2);
488 >        z = pixvalue(scanline[0], 0, y);
489 >        if (zline) zline[0] = z;
490 >                                /* zig-zag start for quincunx pattern */
491 >        for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
492 >                if (i >= xres) {
493 >                        xstep += xres-1-i;
494 >                        i = xres-1;
495 >                }
496 >                z = pixvalue(scanline[i], i, y);
497 >                if (zline) zline[i] = z;
498 >                if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
499 >                if (i <= xstep)
500 >                        b = fillsample(scanline, zline, 0, y, i, 0, b/2);
501 >                else
502 >                        b = fillsample(scanline+i-xstep,
503 >                                        zline ? zline+i-xstep : NULL,
504 >                                        i-xstep, y, xstep, 0, b/2);
505 >                if (sd) *sd++ = nc & 1 ? bl : b;
506 >                bl = b;
507          }
508 +        if (sd && nc & 1) *sd = bl;
509   }
510  
511  
512 < fillscanbar(scanbar, y, ysize)          /* fill interior */
513 < register COLOR  *scanbar[];
514 < int  y, ysize;
512 > fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
513 > register COLOR  *scanbar[];
514 > register float  *zbar[];
515 > int  xres, y, ysize;
516   {
517          COLOR  vline[MAXDIV+1];
518 +        float  zline[MAXDIV+1];
519          int  b = ysize;
520          register int  i, j;
521          
522 <        for (i = 0; i < ourview.hresolu; i++) {
522 >        for (i = 0; i < xres; i++) {
523                  
524                  copycolor(vline[0], scanbar[0][i]);
525                  copycolor(vline[ysize], scanbar[ysize][i]);
526 +                if (zbar[0]) {
527 +                        zline[0] = zbar[0][i];
528 +                        zline[ysize] = zbar[ysize][i];
529 +                }
530                  
531 <                b = fillsample(vline, i, y, 0, ysize, b/2);
531 >                b = fillsample(vline, zbar[0] ? zline : NULL,
532 >                                i, y, 0, ysize, b/2);
533                  
534                  for (j = 1; j < ysize; j++)
535                          copycolor(scanbar[j][i], vline[j]);
536 +                if (zbar[0])
537 +                        for (j = 1; j < ysize; j++)
538 +                                zbar[j][i] = zline[j];
539          }
540   }
541  
542  
543   int
544 < fillsample(colline, x, y, xlen, ylen, b)        /* fill interior points */
545 < register COLOR  *colline;
544 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
545 > register COLOR  *colline;
546 > register float  *zline;
547   int  x, y;
548   int  xlen, ylen;
549   int  b;
550   {
551 <        double  ratio;
551 >        double  ratio;
552 >        double  z;
553          COLOR  ctmp;
554          int  ncut;
555          register int  len;
# Line 201 | Line 562 | int  b;
562          if (len <= 1)                   /* limit recursion */
563                  return(0);
564          
565 <        if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) {
565 >        if (b > 0
566 >        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
567 >                        || bigdiff(colline[0], colline[len], maxdiff)) {
568          
569 <                pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
569 >                z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
570 >                if (zline) zline[len>>1] = z;
571                  ncut = 1;
572                  
573          } else {                                        /* interpolate */
# Line 211 | Line 575 | int  b;
575                  copycolor(colline[len>>1], colline[len]);
576                  ratio = (double)(len>>1) / len;
577                  scalecolor(colline[len>>1], ratio);
578 <                copycolor(ctmp, colline[0]);
578 >                if (zline) zline[len>>1] = zline[len] * ratio;
579                  ratio = 1.0 - ratio;
580 +                copycolor(ctmp, colline[0]);
581                  scalecolor(ctmp, ratio);
582                  addcolor(colline[len>>1], ctmp);
583 +                if (zline) zline[len>>1] += zline[0] * ratio;
584                  ncut = 0;
585          }
586                                                          /* recurse */
587 <        ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2);
587 >        ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
588          
589 <        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1),
590 <                                xlen - (xlen>>1), ylen - (ylen>>1), b/2);
589 >        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
590 >                        x+(xlen>>1), y+(ylen>>1),
591 >                        xlen-(xlen>>1), ylen-(ylen>>1), b/2);
592  
593          return(ncut);
594   }
595  
596  
597 + double
598   pixvalue(col, x, y)             /* compute pixel value */
599   COLOR  col;                     /* returned color */
600   int  x, y;                      /* pixel position */
601   {
602 <        static RAY  thisray;    /* our ray for this pixel */
602 >        static RAY  thisray;
603  
604 <        rayview(thisray.rorg, thisray.rdir, &ourview,
605 <                        x + pixjitter(), y + pixjitter());
604 >        if (viewray(thisray.rorg, thisray.rdir, &ourview,
605 >                        (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
606 >                setcolor(col, 0.0, 0.0, 0.0);
607 >                return(0.0);
608 >        }
609  
610          rayorigin(&thisray, NULL, PRIMARY, 1.0);
611 <        
611 >
612 >        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
613 >
614          rayvalue(&thisray);                     /* trace ray */
615  
616          copycolor(col, thisray.rcol);           /* return color */
617 +        
618 +        return(thisray.rt);                     /* return distance */
619   }
620  
621  
# Line 254 | Line 629 | char  *oldfile;
629  
630          if (oldfile == NULL)
631                  return(0);
632 <        else if ((fp = fopen(oldfile, "r")) == NULL) {
632 >        
633 >        if ((fp = fopen(oldfile, "r")) == NULL) {
634                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
635                  error(WARNING, errmsg);
636                  return(0);
637          }
638 + #ifdef MSDOS
639 +        setmode(fileno(fp), O_BINARY);
640 + #endif
641                                  /* discard header */
642 <        getheader(fp, NULL);
642 >        getheader(fp, NULL, NULL);
643                                  /* get picture size */
644 <        if (fscanf(fp, "-Y %d +X %d\n", &y, &x) != 2) {
645 <                sprintf(errmsg, "bad recover file \"%s\"", oldfile);
644 >        if (!fscnresolu(&x, &y, fp)) {
645 >                sprintf(errmsg, "bad recover file \"%s\" - not removed",
646 >                                oldfile);
647                  error(WARNING, errmsg);
648                  fclose(fp);
649                  return(0);
650          }
651  
652 <        if (x != ourview.hresolu || y != ourview.vresolu) {
652 >        if (x != hres || y != vres) {
653                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
654                                  oldfile);
655                  error(USER, errmsg);
276                return(0);
656          }
657  
658 <        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR));
658 >        scanline = (COLR *)malloc(hres*sizeof(COLR));
659          if (scanline == NULL)
660                  error(SYSTEM, "out of memory in salvage");
661 <        for (y = 0; y < ourview.vresolu; y++) {
662 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
661 >        for (y = 0; y < vres; y++) {
662 >                if (freadcolrs(scanline, hres, fp) < 0)
663                          break;
664 <                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0)
664 >                if (fwritecolrs(scanline, hres, stdout) < 0)
665                          goto writerr;
666          }
667          if (fflush(stdout) == EOF)
# Line 292 | Line 671 | char  *oldfile;
671          unlink(oldfile);
672          return(y);
673   writerr:
674 <        error(SYSTEM, "write error in salvage");
674 >        sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
675 >        error(SYSTEM, errmsg);
676 > }
677 >
678 >
679 > int
680 > pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
681 > register int  x, y;
682 > int  xres, yres;
683 > {
684 >        x -= y;
685 >        while (x < 0)
686 >                x += xres;
687 >        return((((x>>2)*yres + y) << 2) + (x & 3));
688   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines