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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines