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.27 by greg, Wed Jun 19 16:36:34 1991 UTC vs.
Revision 2.83 by greg, Fri Sep 3 21:16:50 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines