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.7 by greg, Tue Sep 12 12:28:26 1989 UTC vs.
Revision 2.70 by greg, Tue Sep 28 17:54:19 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines