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.2 by greg, Mon Apr 10 09:32:55 1989 UTC vs.
Revision 2.93 by greg, Sat May 4 00:32:47 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 */
65  
66 < double  maxdiff = .05;                  /* max. difference for interpolation */
66 > double  mblur = 0.;                     /* motion blur parameter */
67  
68 < double  dstrpix = 0.67;                 /* square pixel distribution */
31 < double  dstrsrc = 0.0;                  /* square source distribution */
68 > double  dblur = 0.;                     /* depth-of-field blur parameter */
69  
70 < int  maxdepth = 6;                      /* maximum recursion depth */
34 < double  minweight = 5e-3;               /* minimum ray weight */
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;
78 <
79 <        sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
80 <                        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 +                        int     myfd;
292 +                        close(1);                       /* reassign stdout */
293 +                        sprintf(fbuf, pout, seq);
294 +                tryagain:
295 +                        errno = 0;                      /* exclusive open */
296 +                        if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
297 +                                if ((errno != EEXIST) | (prvr != NULL) ||
298 +                                                !strcmp(fbuf, pout)) {
299 +                                        sprintf(errmsg,
300 +                                                "cannot open output file \"%s\"",
301 +                                                fbuf);
302 +                                        error(SYSTEM, errmsg);
303 +                                }
304 +                                setview(&ourview);
305 +                                continue;               /* don't clobber */
306 +                        }
307 +                        if (myfd != 1) {
308 +                                unlink(fbuf);
309 +                                goto tryagain;          /* leave it open */
310 +                        }
311 +                        SET_FD_BINARY(1);
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 <        ourview.hresolu -= ourview.hresolu % psample;
360 <        ourview.vresolu -= ourview.vresolu % psample;
361 <                
359 >
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 >        int  i;
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 <        printf("-Y %d +X %d\n", ourview.vresolu, ourview.hresolu);
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 >        COLOR   *scanline,
531 >        float   *zline,
532 >        char  *sd,
533 >        int  xres,
534 >        int  y,
535 >        int  xstep
536 > )
537   {
538 <        int  b = xstep;
539 <        register int  i;
540 <        
541 <        pixvalue(scanline[0], 0, y);
538 >        static int  nc = 0;             /* number of calls */
539 >        int  bl = xstep, b = xstep;
540 >        double  z;
541 >        int  i;
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 >        COLOR   *scanbar[],
570 >        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++) {
172 <                
579 >        int  i, j;
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 >        COLOR   *colline,
603 >        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 <        
615 >        int  len;
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;
208                
632          } else {                                        /* interpolate */
210        
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);
222 <        
223 <        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1),
224 <                                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 >        extern void  SDsquare2disk(double ds[2], double seedx, double seedy);
664 >        RAY  thisray;
665 >        FVECT   lorg, ldir;
666 >        double  hpos, vpos, vdist, lmax;
667 >        int     i;
668 >                                                /* compute view ray */
669 >        setcolor(col, 0.0, 0.0, 0.0);
670 >        hpos = (x+pixjitter())/hres;
671 >        vpos = (y+pixjitter())/vres;
672 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
673 >                                        &ourview, hpos, vpos)) < -FTINY)
674 >                return(0.0);
675  
676 <        rayview(thisray.rorg, thisray.rdir, &ourview,
677 <                        x + pixjitter(), y + pixjitter());
676 >        vdist = ourview.vdist;
677 >                                                /* set pixel index */
678 >        samplendx = pixnumber(x,y,hres,vres);
679 >                                                /* optional motion blur */
680 >        if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
681 >                                        &lastview, hpos, vpos)) >= -FTINY) {
682 >                double  d = mblur*(.5-urand(281+samplendx));
683  
684 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
685 <        
684 >                thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
685 >                for (i = 3; i--; ) {
686 >                        thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i];
687 >                        thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i];
688 >                }
689 >                if (normalize(thisray.rdir) == 0.0)
690 >                        return(0.0);
691 >                vdist = (1.-d)*vdist + d*lastview.vdist;
692 >        }
693 >                                                /* optional depth-of-field */
694 >        if (dblur > FTINY) {
695 >                double  vc, df[2];
696 >                                                /* random point on disk */
697 >                SDsquare2disk(df, frandom(), frandom());
698 >                df[0] *= .5*dblur;
699 >                df[1] *= .5*dblur;
700 >                if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) {
701 >                        double  adj = 1.0;
702 >                        if (ourview.type == VT_PER)
703 >                                adj /= DOT(thisray.rdir, ourview.vdir);
704 >                        df[0] /= sqrt(ourview.hn2);
705 >                        df[1] /= sqrt(ourview.vn2);
706 >                        for (i = 3; i--; ) {
707 >                                vc = ourview.vp[i] + adj*vdist*thisray.rdir[i];
708 >                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
709 >                                                        df[1]*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 >                                        df[0] /= sqrt(ourview.hn2);
717 >                                df[1] /= sqrt(ourview.vn2);
718 >                        }
719 >                        for (i = 3; i--; ) {
720 >                                vc = ourview.vp[i] + vdist*thisray.rdir[i];
721 >                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
722 >                                                        df[1]*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 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
732 >
733          rayvalue(&thisray);                     /* trace ray */
734  
735          copycolor(col, thisray.rcol);           /* return color */
736 +
737 +        return(raydistance(&thisray));          /* 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 <        else if ((fp = fopen(oldfile, "r")) == NULL) {
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 (fscanf(fp, "-Y %d +X %d\n", &y, &x) != 2) {
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 != ourview.hresolu || y != ourview.vresolu) {
770 >        if ((x != hres) | (y != vres)) {
771                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
772                                  oldfile);
773                  error(USER, errmsg);
276                return(0);
774          }
775  
776 <        scanline = (COLR *)malloc(ourview.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 < ourview.vresolu; y++) {
780 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
779 >        for (y = 0; y < vres; y++) {
780 >                if (freadcolrs(scanline, hres, fp) < 0)
781                          break;
782 <                if (fwritecolrs(scanline, ourview.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 (screen door) */
803 >        int  x,
804 >        int  y,
805 >        int  xres,
806 >        int  yres
807 > )
808 > {
809 >        unsigned        nbits = 0;
810 >        bitmask_t       coord[2];
811 >
812 >        if (xres < yres) xres = yres;
813 >        while (xres > 0) {
814 >                xres >>= 1;
815 >                ++nbits;
816 >        }
817 >        coord[0] = x; coord[1] = y;
818 >        return ((int)hilbert_c2i(2, nbits, coord));
819   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines