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.12 by greg, Mon Jan 8 13:38:01 1990 UTC vs.
Revision 2.44 by greg, Wed Feb 14 15:18:05 1996 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines