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.14 by greg, Thu Jan 11 08:30:42 1990 UTC vs.
Revision 2.37 by greg, Wed Dec 22 21:03:54 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  <signal.h>
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 + #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 */
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 */
61 < double  shadcert = .5;                  /* shadow certainty */
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 */
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 */
# Line 48 | Line 79 | 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 < double  pixvalue();
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 <
87 <        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
88 <                        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 <        signal(SIGALRM, report);
158 <        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 + 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 <        FILE  *zfp;
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;
110                                        /* check sampling */
111        if (psample < 1)
112                psample = 1;
113        else if (psample > MAXDIV)
114                psample = MAXDIV;
355                                          /* allocate scanlines */
356          for (i = 0; i <= psample; i++) {
357 <                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
357 >                scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
358                  if (scanbar[i] == NULL)
359                          goto memerr;
360          }
361 <                                        /* open z file */
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 ((zfp = fopen(zfile, "a+")) == NULL) {
374 <                        sprintf(errmsg, "cannot open z file \"%s\"", zfile);
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(hresolu*sizeof(float));
381 >                        zbar[i] = (float *)malloc(hres*sizeof(float));
382                          if (zbar[i] == NULL)
383                                  goto memerr;
384                  }
385          } else {
386 <                zfp = NULL;
386 >                zfd = -1;
387                  for (i = 0; i <= psample; i++)
388                          zbar[i] = NULL;
389          }
390                                          /* write out boundaries */
391 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
391 >        fprtresolu(hres, vres, stdout);
392                                          /* recover file and compute first */
393          i = salvage(oldfile);
394 <        if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF)
395 <                error(SYSTEM, "z file seek error in render");
396 <        ypos = vresolu-1 - i;
397 <        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
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 -= psample; ypos >= 0; ypos -= psample) {
408 <        
409 <                pctdone = 100.0*(vresolu-ypos-psample)/vresolu;
410 <
411 <                colptr = scanbar[psample];              /* move base to top */
412 <                scanbar[psample] = scanbar[0];
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 <                zptr = zbar[psample];
417 <                zbar[psample] = zbar[0];
416 >                zptr = zbar[ystep];
417 >                zbar[ystep] = zbar[0];
418                  zbar[0] = zptr;
419                                                          /* fill base line */
420 <                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
420 >                fillscanline(scanbar[0], zbar[0], sampdens,
421 >                                hres, ypos, hstep);
422                                                          /* fill bar */
423 <                fillscanbar(scanbar, zbar, hresolu, ypos, psample);
423 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
424                                                          /* write it out */
425 <                for (i = psample; i > 0; i--) {
426 <                        if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
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],hresolu,stdout) < 0)
433 >                        if (fwritescan(scanbar[i], hres, stdout) < 0)
434                                  goto writerr;
435                  }
167                if (zfp != NULL && fflush(zfp) == EOF)
168                        goto writerr;
436                  if (fflush(stdout) == EOF)
437                          goto writerr;
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          }
172                                                /* compute residual */
173        colptr = scanbar[psample];
174        scanbar[psample] = scanbar[0];
175        scanbar[0] = colptr;
176        zptr = zbar[psample];
177        zbar[psample] = zbar[0];
178        zbar[0] = zptr;
179        if (ypos > -psample) {
180                fillscanline(scanbar[-ypos], zbar[-ypos], hresolu, 0, psample);
181                fillscanbar(scanbar-ypos, zbar-ypos, hresolu, 0, psample+ypos);
182        }
183        for (i = psample; i+ypos >= 0; i--) {
184                if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu)
185                        goto writerr;
186                if (fwritescan(scanbar[i], hresolu, stdout) < 0)
187                        goto writerr;
188        }
447                                                  /* clean up */
448 <        if (zfp != NULL) {
449 <                if (fclose(zfp) == EOF)
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");
# Line 206 | Line 474 | memerr:
474   }
475  
476  
477 < fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
478 < register COLOR  *scanline;
479 < register float  *zline;
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;
484 <        double  z;
483 >        static int  nc = 0;             /* number of calls */
484 >        int  bl = xstep, b = xstep;
485 >        double  z;
486          register int  i;
487          
488          z = pixvalue(scanline[0], 0, y);
489          if (zline) zline[0] = z;
490 <
491 <        for (i = xstep; i < xres; i += xstep) {
492 <        
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 <                
499 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
500 <                                i-xstep, y, xstep, 0, b/2);
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 (i-xstep < xres-1) {
230 <                z = pixvalue(scanline[xres-1], xres-1, y);
231 <                if (zline) zline[xres-1] = z;
232 <                fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
233 <                                i-xstep, y, xres-1-(i-xstep), 0, b/2);
234 <        }
508 >        if (sd && nc & 1) *sd = bl;
509   }
510  
511  
512   fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
513 < register COLOR  *scanbar[];
514 < register float  *zbar[];
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;
246        double  z;
520          register int  i, j;
521          
522          for (i = 0; i < xres; i++) {
# Line 268 | Line 541 | int  xres, y, ysize;
541  
542  
543   int
544 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
545 < register COLOR  *colline;
546 < register float  *zline;
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 <        extern double  fabs();
552 <        double  ratio;
280 <        double  z;
551 >        double  ratio;
552 >        double  z;
553          COLOR  ctmp;
554          int  ncut;
555          register int  len;
# Line 327 | Line 599 | 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 <        viewray(thisray.rorg, thisray.rdir, &ourview,
605 <                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
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.rot);                    /* return distance */
618 >        return(thisray.rt);                     /* return distance */
619   }
620  
621  
# Line 351 | Line 628 | char  *oldfile;
628          int  x, y;
629  
630          if (oldfile == NULL)
631 <                return(0);
632 <        
631 >                goto gotzip;
632 >
633          if ((fp = fopen(oldfile, "r")) == NULL) {
634                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
635                  error(WARNING, errmsg);
636 <                return(0);
636 >                goto gotzip;
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 (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
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);
649 >                goto gotzip;
650          }
651  
652 <        if (x != hresolu || y != vresolu) {
652 >        if (x != hres || y != vres) {
653                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
654                                  oldfile);
655                  error(USER, errmsg);
656          }
657  
658 <        scanline = (COLR *)malloc(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 < vresolu; y++) {
662 <                if (freadcolrs(scanline, hresolu, fp) < 0)
661 >        for (y = 0; y < vres; y++) {
662 >                if (freadcolrs(scanline, hres, fp) < 0)
663                          break;
664 <                if (fwritecolrs(scanline, hresolu, stdout) < 0)
664 >                if (fwritecolrs(scanline, hres, stdout) < 0)
665                          goto writerr;
666          }
667          if (fflush(stdout) == EOF)
# Line 389 | Line 670 | char  *oldfile;
670          fclose(fp);
671          unlink(oldfile);
672          return(y);
673 + gotzip:
674 +        if (fflush(stdout) == EOF)
675 +                error(SYSTEM, "error writing picture header");
676 +        return(0);
677   writerr:
678 <        error(SYSTEM, "write error in salvage");
678 >        sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
679 >        error(SYSTEM, errmsg);
680 > }
681 >
682 >
683 > int
684 > pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
685 > register int  x, y;
686 > int  xres, yres;
687 > {
688 >        x -= y;
689 >        while (x < 0)
690 >                x += xres;
691 >        return((((x>>2)*yres + y) << 2) + (x & 3));
692   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines