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.18 by greg, Wed Feb 21 12:39:37 1990 UTC vs.
Revision 2.48 by gregl, Mon Oct 20 16:46:37 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ LBL";
6  
7   /*
8   *  rpict.c - routines and variables for picture generation.
9 *
10 *     8/14/85
9   */
10  
11   #include  "ray.h"
12  
13 + #include  <sys/types.h>
14 +
15 + #ifndef NIX
16   #ifdef BSD
17   #include  <sys/time.h>
18   #include  <sys/resource.h>
19   #else
20 < #include  <signal.h>
20 > #include  <sys/times.h>
21 > #include  <unistd.h>
22   #endif
23 < #include  <fcntl.h>
23 > #endif
24  
25 + extern time_t   time();
26 +
27 + #include  <signal.h>
28 +
29   #include  "view.h"
30  
31 + #include  "resolu.h"
32 +
33   #include  "random.h"
34  
35 + #include  "paths.h"
36 +
37 + #define  RFTEMPLATE     "rfXXXXXX"
38 +
39 + #ifndef SIGCONT
40 + #define SIGCONT         SIGIO
41 + #endif
42 +
43 + int  dimlist[MAXDIM];                   /* sampling dimensions */
44 + int  ndims = 0;                         /* number of sampling dimensions */
45 + int  samplendx;                         /* sample index number */
46 +
47   VIEW  ourview = STDVIEW;                /* view parameters */
48   int  hresolu = 512;                     /* horizontal resolution */
49   int  vresolu = 512;                     /* vertical resolution */
50 < double  pixaspect = 1.0;                /* pixel aspect ratio */
50 > double  pixaspect = 1.0;                /* pixel aspect ratio */
51  
52   int  psample = 4;                       /* pixel sample size */
53 < double  maxdiff = .05;                  /* max. difference for interpolation */
54 < double  dstrpix = 0.67;                 /* square pixel distribution */
53 > double  maxdiff = .05;                  /* max. difference for interpolation */
54 > double  dstrpix = 0.67;                 /* square pixel distribution */
55  
56 < double  dstrsrc = 0.0;                  /* square source distribution */
57 < double  shadthresh = .05;               /* shadow threshold */
58 < double  shadcert = .5;                  /* shadow certainty */
56 > double  dstrsrc = 0.0;                  /* square source distribution */
57 > double  shadthresh = .05;               /* shadow threshold */
58 > double  shadcert = .5;                  /* shadow certainty */
59 > int  directrelay = 1;                   /* number of source relays */
60 > int  vspretest = 512;                   /* virtual source pretest density */
61 > int  directvis = 1;                     /* sources visible? */
62 > double  srcsizerat = .25;               /* maximum ratio source size/dist. */
63  
64 + COLOR  cextinction = BLKCOLOR;          /* global extinction coefficient */
65 + COLOR  salbedo = BLKCOLOR;              /* global scattering albedo */
66 + double  seccg = 0.;                     /* global scattering eccentricity */
67 + double  ssampdist = 0.;                 /* scatter sampling distance */
68 +
69 + double  specthresh = .15;               /* specular sampling threshold */
70 + double  specjitter = 1.;                /* specular sampling jitter */
71 +
72 + int  backvis = 1;                       /* back face visibility */
73 +
74   int  maxdepth = 6;                      /* maximum recursion depth */
75 < double  minweight = 5e-3;               /* minimum ray weight */
75 > double  minweight = 5e-3;               /* minimum ray weight */
76  
77   COLOR  ambval = BLKCOLOR;               /* ambient value */
78 < double  ambacc = 0.2;                   /* ambient accuracy */
78 > int  ambvwt = 0;                        /* initial weight for ambient value */
79 > double  ambacc = 0.2;                   /* ambient accuracy */
80   int  ambres = 32;                       /* ambient resolution */
81   int  ambdiv = 128;                      /* ambient divisions */
82   int  ambssamp = 0;                      /* ambient super-samples */
# Line 49 | Line 84 | int  ambounce = 0;                     /* ambient bounces */
84   char  *amblist[128];                    /* ambient include/exclude list */
85   int  ambincl = -1;                      /* include == 1, exclude == 0 */
86  
87 + #ifdef MSDOS
88 + int  ralrm = 60;                        /* seconds between reports */
89 + #else
90   int  ralrm = 0;                         /* seconds between reports */
91 + #endif
92  
93 < double  pctdone = 0.0;                  /* percentage done */
93 > double  pctdone = 0.0;                  /* percentage done */
94  
95 < extern long  nrays;                     /* number of rays traced */
95 > time_t  tlastrept = 0L;                 /* time at last report */
96  
97 < #define  MAXDIV         32              /* maximum sample size */
97 > extern time_t  tstart;                  /* starting time */
98  
99 < #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
99 > extern unsigned long  nrays;            /* number of rays traced */
100  
101 < double  pixvalue();
101 > #define  MAXDIV         16              /* maximum sample size */
102  
103 + #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
104  
105 + int  hres, vres;                        /* resolution for this frame */
106 +
107 + extern char  *mktemp();
108 +
109 + double  pixvalue();
110 +
111 + #ifdef NIX
112 + #define  file_exists(f) (access(f,F_OK)==0)
113 + #else
114 + #include  <sys/types.h>
115 + #include  <sys/stat.h>
116 + int
117 + file_exists(fname)                              /* ordinary file exists? */
118 + char  *fname;
119 + {
120 +        struct stat  sbuf;
121 +        if (stat(fname, &sbuf) < 0) return(0);
122 +        return((sbuf.st_mode & S_IFREG) != 0);
123 + }
124 + #endif
125 +
126 +
127   quit(code)                      /* quit program */
128   int  code;
129   {
130 <        if (code || ralrm > 0)          /* report status */
130 >        if (code)                       /* report status */
131                  report();
132 <
132 >        headclean();                    /* delete header file */
133 >        pfclean();                      /* clean up persist files */
134          exit(code);
135   }
136  
137  
138 + #ifndef NIX
139   report()                /* report progress */
140   {
141 +        extern char  *myhostname();
142 +        double  u, s;
143   #ifdef BSD
144          struct rusage  rubuf;
145 <        double  t;
145 > #else
146 >        struct tms  tbuf;
147 >        double  period;
148 > #endif
149  
150 +        tlastrept = time((time_t *)NULL);
151 + #ifdef BSD
152          getrusage(RUSAGE_SELF, &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;
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          getrusage(RUSAGE_CHILDREN, &rubuf);
156 <        t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
157 <        t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec;
87 <
88 <        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
89 <                        nrays, pctdone, t/3600.0);
156 >        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
157 >        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
158   #else
159 <        signal(SIGALRM, report);
160 <        sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
159 >        times(&tbuf);
160 > #ifdef _SC_CLK_TCK
161 >        period = 1.0 / sysconf(_SC_CLK_TCK);
162 > #else
163 >        period = 1.0 / 60.0;
164   #endif
165 +        u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
166 +        s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
167 + #endif
168 +
169 +        sprintf(errmsg,
170 +                "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
171 +                        nrays, pctdone, u/3600., s/3600.,
172 +                        (tlastrept-tstart)/3600., myhostname());
173          eputs(errmsg);
174 + #ifndef BSD
175 +        signal(SIGCONT, report);
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          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;
113                                        /* check sampling */
114        if (psample < 1)
115                psample = 1;
116        else if (psample > MAXDIV)
117                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 ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
374 <                        sprintf(errmsg, "cannot open z file \"%s\"", zfile);
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                  }
# Line 138 | Line 388 | char  *zfile, *oldfile;
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 (zfd != -1 && lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
395 <                error(SYSTEM, "z file seek error in render");
396 <        ypos = vresolu-1 - i;
397 <        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
398 <        ystep = psample;
394 >        if (i >= vres)
395 >                goto alldone;
396 >        if (zfd != -1 && i > 0 &&
397 >                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
398 >                error(SYSTEM, "z-file seek error in render");
399 >        pctdone = 100.0*i/vres;
400 >        if (ralrm > 0)                  /* report init stats */
401 >                report();
402 > #ifndef  BSD
403 >        else
404 > #endif
405 >        signal(SIGCONT, report);
406 >        ypos = vres-1 - i;                      /* initialize sampling */
407 >        if (directvis)
408 >                init_drawsources(psample);
409 >        fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
410                                                  /* compute scanlines */
411          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
151                                                        /* record progress */
152                pctdone = 100.0*(vresolu-ypos-ystep)/vresolu;
412                                                          /* bottom adjust? */
413                  if (ypos < 0) {
414                          ystep += ypos;
# Line 162 | Line 421 | char  *zfile, *oldfile;
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, ystep);
427 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
428 >                if (directvis)                          /* add bitty sources */
429 >                        drawsources(scanbar, zbar, 0, hres, ypos, ystep);
430                                                          /* write it out */
431 + #ifndef  BSD
432 +                signal(SIGCONT, SIG_IGN);       /* don't interrupt writes */
433 + #endif
434                  for (i = ystep; i > 0; i--) {
435                          if (zfd != -1 && write(zfd, (char *)zbar[i],
436 <                                        hresolu*sizeof(float))
437 <                                        < hresolu*sizeof(float))
436 >                                        hres*sizeof(float))
437 >                                        < hres*sizeof(float))
438                                  goto writerr;
439 <                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
439 >                        if (fwritescan(scanbar[i], hres, stdout) < 0)
440                                  goto writerr;
441                  }
442                  if (fflush(stdout) == EOF)
443                          goto writerr;
444 +                                                        /* record progress */
445 +                pctdone = 100.0*(vres-1-ypos)/vres;
446 +                if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
447 +                        report();
448 + #ifndef  BSD
449 +                else
450 +                        signal(SIGCONT, report);
451 + #endif
452          }
453                                                  /* clean up */
454 +        signal(SIGCONT, SIG_IGN);
455 +        if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
456 +                                < hres*sizeof(float))
457 +                goto writerr;
458 +        fwritescan(scanbar[0], hres, stdout);
459 +        if (fflush(stdout) == EOF)
460 +                goto writerr;
461 + alldone:
462          if (zfd != -1) {
463 <                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
183 <                                < hresolu*sizeof(float))
463 >                if (close(zfd) == -1)
464                          goto writerr;
185                close(zfd);
465                  for (i = 0; i <= psample; i++)
466                          free((char *)zbar[i]);
467          }
189        fwritescan(scanbar[0], hresolu, stdout);
190        if (fflush(stdout) == EOF)
191                goto writerr;
468          for (i = 0; i <= psample; i++)
469                  free((char *)scanbar[i]);
470 +        if (sampdens != NULL)
471 +                free(sampdens);
472          pctdone = 100.0;
473 +        if (ralrm > 0)
474 +                report();
475 +        signal(SIGCONT, SIG_DFL);
476          return;
477   writerr:
478          error(SYSTEM, "write error in render");
# Line 200 | Line 481 | memerr:
481   }
482  
483  
484 < fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
485 < register COLOR  *scanline;
486 < register float  *zline;
484 > fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
485 > register COLOR  *scanline;
486 > register float  *zline;
487 > register char  *sd;
488   int  xres, y, xstep;
489   {
490 <        int  b = xstep;
491 <        double  z;
490 >        static int  nc = 0;             /* number of calls */
491 >        int  bl = xstep, b = xstep;
492 >        double  z;
493          register int  i;
494 <        
494 >
495          z = pixvalue(scanline[0], 0, y);
496          if (zline) zline[0] = z;
497 <
498 <        for (i = xstep; i < xres-1+xstep; i += xstep) {
497 >                                /* zig-zag start for quincunx pattern */
498 >        for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
499                  if (i >= xres) {
500                          xstep += xres-1-i;
501                          i = xres-1;
502                  }
503                  z = pixvalue(scanline[i], i, y);
504                  if (zline) zline[i] = z;
505 <                
506 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
507 <                                i-xstep, y, xstep, 0, b/2);
505 >                if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
506 >                if (i <= xstep)
507 >                        b = fillsample(scanline, zline, 0, y, i, 0, b/2);
508 >                else
509 >                        b = fillsample(scanline+i-xstep,
510 >                                        zline ? zline+i-xstep : (float *)NULL,
511 >                                        i-xstep, y, xstep, 0, b/2);
512 >                if (sd) *sd++ = nc & 1 ? bl : b;
513 >                bl = b;
514          }
515 +        if (sd && nc & 1) *sd = bl;
516   }
517  
518  
519   fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
520 < register COLOR  *scanbar[];
521 < register float  *zbar[];
520 > register COLOR  *scanbar[];
521 > register float  *zbar[];
522   int  xres, y, ysize;
523   {
524          COLOR  vline[MAXDIV+1];
525          float  zline[MAXDIV+1];
526          int  b = ysize;
237        double  z;
527          register int  i, j;
528 <        
528 >
529          for (i = 0; i < xres; i++) {
241                
530                  copycolor(vline[0], scanbar[0][i]);
531                  copycolor(vline[ysize], scanbar[ysize][i]);
532                  if (zbar[0]) {
533                          zline[0] = zbar[0][i];
534                          zline[ysize] = zbar[ysize][i];
535                  }
536 <                
249 <                b = fillsample(vline, zbar[0] ? zline : NULL,
536 >                b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
537                                  i, y, 0, ysize, b/2);
538 <                
538 >
539                  for (j = 1; j < ysize; j++)
540                          copycolor(scanbar[j][i], vline[j]);
541                  if (zbar[0])
# Line 259 | 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;
271 <        double  z;
556 >        double  ratio;
557 >        double  z;
558          COLOR  ctmp;
559          int  ncut;
560          register int  len;
561 <        
561 >
562          if (xlen > 0)                   /* x or y length is zero */
563                  len = xlen;
564          else
565                  len = ylen;
566 <                
566 >
567          if (len <= 1)                   /* limit recursion */
568                  return(0);
569 <        
570 <        if (b > 0
571 <        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
569 >
570 >        if (b > 0 ||
571 >        (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
572                          || bigdiff(colline[0], colline[len], maxdiff)) {
573 <        
573 >
574                  z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
575                  if (zline) zline[len>>1] = z;
576                  ncut = 1;
291                
577          } else {                                        /* interpolate */
293        
578                  copycolor(colline[len>>1], colline[len]);
579                  ratio = (double)(len>>1) / len;
580                  scalecolor(colline[len>>1], ratio);
# Line 304 | Line 588 | int  b;
588          }
589                                                          /* recurse */
590          ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
591 <        
592 <        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
591 >
592 >        ncut += fillsample(colline+(len>>1),
593 >                        zline ? zline+(len>>1) : (float *)NULL,
594                          x+(xlen>>1), y+(ylen>>1),
595                          xlen-(xlen>>1), ylen-(ylen>>1), b/2);
596  
# Line 318 | Line 603 | pixvalue(col, x, y)            /* compute pixel value */
603   COLOR  col;                     /* returned color */
604   int  x, y;                      /* pixel position */
605   {
606 <        static RAY  thisray;    /* our ray for this pixel */
606 >        static RAY  thisray;
607  
608 <        viewray(thisray.rorg, thisray.rdir, &ourview,
609 <                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
608 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
609 >                        (x+pixjitter())/hres, (y+pixjitter())/vres)) < -FTINY) {
610 >                setcolor(col, 0.0, 0.0, 0.0);
611 >                return(0.0);
612 >        }
613  
614          rayorigin(&thisray, NULL, PRIMARY, 1.0);
615 <        
615 >
616 >        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
617 >
618          rayvalue(&thisray);                     /* trace ray */
619  
620          copycolor(col, thisray.rcol);           /* return color */
621 <        
622 <        return(thisray.rot);                    /* return distance */
621 >
622 >        return(thisray.rt);                     /* return distance */
623   }
624  
625  
# Line 342 | Line 632 | char  *oldfile;
632          int  x, y;
633  
634          if (oldfile == NULL)
635 <                return(0);
636 <        
635 >                goto gotzip;
636 >
637          if ((fp = fopen(oldfile, "r")) == NULL) {
638                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
639                  error(WARNING, errmsg);
640 <                return(0);
640 >                goto gotzip;
641          }
642 + #ifdef MSDOS
643 +        setmode(fileno(fp), O_BINARY);
644 + #endif
645                                  /* discard header */
646 <        getheader(fp, NULL);
646 >        getheader(fp, NULL, NULL);
647                                  /* get picture size */
648 <        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
649 <                sprintf(errmsg, "bad recover file \"%s\"", oldfile);
648 >        if (!fscnresolu(&x, &y, fp)) {
649 >                sprintf(errmsg, "bad recover file \"%s\" - not removed",
650 >                                oldfile);
651                  error(WARNING, errmsg);
652                  fclose(fp);
653 <                return(0);
653 >                goto gotzip;
654          }
655  
656 <        if (x != hresolu || y != vresolu) {
656 >        if (x != hres || y != vres) {
657                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
658                                  oldfile);
659                  error(USER, errmsg);
660          }
661  
662 <        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
662 >        scanline = (COLR *)malloc(hres*sizeof(COLR));
663          if (scanline == NULL)
664                  error(SYSTEM, "out of memory in salvage");
665 <        for (y = 0; y < vresolu; y++) {
666 <                if (freadcolrs(scanline, hresolu, fp) < 0)
665 >        for (y = 0; y < vres; y++) {
666 >                if (freadcolrs(scanline, hres, fp) < 0)
667                          break;
668 <                if (fwritecolrs(scanline, hresolu, stdout) < 0)
668 >                if (fwritecolrs(scanline, hres, stdout) < 0)
669                          goto writerr;
670          }
671          if (fflush(stdout) == EOF)
# Line 380 | Line 674 | char  *oldfile;
674          fclose(fp);
675          unlink(oldfile);
676          return(y);
677 + gotzip:
678 +        if (fflush(stdout) == EOF)
679 +                error(SYSTEM, "error writing picture header");
680 +        return(0);
681   writerr:
682 <        error(SYSTEM, "write error in salvage");
682 >        sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
683 >        error(SYSTEM, errmsg);
684 > }
685 >
686 >
687 > int
688 > pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
689 > register int  x, y;
690 > int  xres, yres;
691 > {
692 >        x -= y;
693 >        while (x < 0)
694 >                x += xres;
695 >        return((((x>>2)*yres + y) << 2) + (x & 3));
696   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines