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 2.30 by greg, Thu Oct 28 15:24:19 1993 UTC vs.
Revision 2.106 by greg, Sat Jan 18 03:49:00 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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  
15 #ifndef NIX
16 #ifdef BSD
17 #include  <sys/time.h>
18 #include  <sys/resource.h>
19 #else
10   #include  <sys/types.h>
11 < #include  <sys/times.h>
12 < #include  <limits.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
24 #endif
26  
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"
29
30 #include  "resolu.h"
31
34   #include  "random.h"
33
35   #include  "paths.h"
36 + #include  "hilbert.h"
37 + #include  "pmapbias.h"
38 + #include  "pmapdiag.h"
39  
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 + extern RGBPRIMP  out_prims;             /* output color primitives (NULL if spectral) */
52 +
53   int  dimlist[MAXDIM];                   /* sampling dimensions */
54   int  ndims = 0;                         /* number of sampling dimensions */
55   int  samplendx;                         /* sample index number */
56  
57 + void  (*addobjnotify[])() = {ambnotify, NULL};
58 +
59   VIEW  ourview = STDVIEW;                /* view parameters */
60   int  hresolu = 512;                     /* horizontal resolution */
61   int  vresolu = 512;                     /* vertical resolution */
# Line 52 | Line 65 | int  psample = 4;                      /* pixel sample size */
65   double  maxdiff = .05;                  /* max. difference for interpolation */
66   double  dstrpix = 0.67;                 /* square pixel distribution */
67  
68 + double  mblur = 0.;                     /* motion blur parameter */
69 +
70 + double  dblur = 0.;                     /* depth-of-field blur parameter */
71 +
72 + void  (*trace)() = NULL;                /* trace call */
73 +
74 + int  do_irrad = 0;                      /* compute irradiance? */
75 +
76 + int  rand_samp = 0;                     /* pure Monte Carlo sampling? */
77 +
78   double  dstrsrc = 0.0;                  /* square source distribution */
79   double  shadthresh = .05;               /* shadow threshold */
80   double  shadcert = .5;                  /* shadow certainty */
# Line 60 | Line 83 | int  vspretest = 512;                  /* virtual source pretest dens
83   int  directvis = 1;                     /* sources visible? */
84   double  srcsizerat = .25;               /* maximum ratio source size/dist. */
85  
86 + COLOR  cextinction = BLKCOLOR;          /* global extinction coefficient */
87 + COLOR  salbedo = BLKCOLOR;              /* global scattering albedo */
88 + double  seccg = 0.;                     /* global scattering eccentricity */
89 + double  ssampdist = 0.;                 /* scatter sampling distance */
90 +
91   double  specthresh = .15;               /* specular sampling threshold */
92   double  specjitter = 1.;                /* specular sampling jitter */
93  
94 < int  maxdepth = 6;                      /* maximum recursion depth */
67 < double  minweight = 5e-3;               /* minimum ray weight */
94 > int  backvis = 1;                       /* back face visibility */
95  
96 + int  maxdepth = 7;                      /* maximum recursion depth */
97 + double  minweight = 1e-4;               /* minimum ray weight */
98 +
99 + char  *ambfile = NULL;                  /* ambient file name */
100   COLOR  ambval = BLKCOLOR;               /* ambient value */
101 + int  ambvwt = 0;                        /* initial weight for ambient value */
102   double  ambacc = 0.2;                   /* ambient accuracy */
103 < int  ambres = 32;                       /* ambient resolution */
104 < int  ambdiv = 128;                      /* ambient divisions */
105 < int  ambssamp = 0;                      /* ambient super-samples */
103 > int  ambres = 64;                       /* ambient resolution */
104 > int  ambdiv = 512;                      /* ambient divisions */
105 > int  ambssamp = 128;                    /* ambient super-samples */
106   int  ambounce = 0;                      /* ambient bounces */
107 < char  *amblist[128];                    /* ambient include/exclude list */
107 > char  *amblist[AMBLLEN];                /* ambient include/exclude list */
108   int  ambincl = -1;                      /* include == 1, exclude == 0 */
109  
78 #ifdef MSDOS
79 int  ralrm = 60;                        /* seconds between reports */
80 #else
110   int  ralrm = 0;                         /* seconds between reports */
82 #endif
111  
112   double  pctdone = 0.0;                  /* percentage done */
113 + time_t  tlastrept = 0L;                 /* time at last report */
114 + time_t  tstart;                         /* starting time */
115  
86 long  tlastrept = 0L;                   /* time at last report */
87
88 extern long  time();
89 extern long  tstart;                    /* starting time */
90
91 extern unsigned long  nrays;            /* number of rays traced */
92
116   #define  MAXDIV         16              /* maximum sample size */
117  
118   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
119  
120 < static int  hres, vres;                 /* resolution for this frame */
120 > int  hres, vres;                        /* resolution for this frame */
121  
122 < extern char  *mktemp();
122 > extern void     sskip_ray(RAY *r, double h, double v);
123  
124 < double  pixvalue();
124 > static VIEW     lastview;               /* the previous view input */
125  
126 < #ifdef NIX
127 < #define  file_exists(f) (access(f,F_OK)==0)
128 < #else
129 < #include  <sys/types.h>
130 < #include  <sys/stat.h>
131 < int
132 < file_exists(fname)                              /* ordinary file exists? */
133 < char  *fname;
134 < {
135 <        struct stat  sbuf;
136 <        if (stat(fname, &sbuf) < 0) return(0);
137 <        return((sbuf.st_mode & S_IFREG) != 0);
115 < }
116 < #endif
126 > static void report(int);
127 > static int nextview(FILE *fp);
128 > static void render(char *zfile, char *oldfile);
129 > static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres,
130 >                int y, int xstep);
131 > static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres,
132 >                int y, int ysize);
133 > static int fillsample(COLOR *colline, float *zline, int x, int y,
134 >                int xlen, int ylen, int b);
135 > static double pixvalue(COLOR  col, int  x, int  y);
136 > static int salvage(char  *oldfile);
137 > static int pixnumber(int  x, int  y, int  xres, int  yres);
138  
139  
140 < quit(code)                      /* quit program */
141 < int  code;
140 >
141 > void
142 > quit(int code)                  /* quit program */
143   {
144          if (code)                       /* report status */
145 <                report();
145 >                report(0);
146 > #ifndef NON_POSIX
147          headclean();                    /* delete header file */
148          pfclean();                      /* clean up persist files */
149 + #endif
150          exit(code);
151   }
152  
153  
154 < #ifndef NIX
155 < report()                /* report progress */
154 > #ifndef NON_POSIX
155 > static void
156 > report(int dummy)               /* report progress */
157   {
158 <        char  hostname[128];
159 <        double  u, s;
158 >        char                    bcStat [128];
159 >        double          u, s;
160   #ifdef BSD
161 <        struct rusage  rubuf;
161 >        struct rusage   rubuf;
162   #else
163 <        struct tms  tbuf;
163 >        double          period = 1.0 / 60.0;
164 >        struct tms      tbuf;
165   #endif
166  
167 <        tlastrept = time((long *)0);
167 >        tlastrept = time((time_t *)NULL);
168   #ifdef BSD
169          getrusage(RUSAGE_SELF, &rubuf);
170 <        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
171 <        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
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          getrusage(RUSAGE_CHILDREN, &rubuf);
173 <        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
174 <        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
173 >        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
174 >        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
175   #else
176          times(&tbuf);
177 <        u = ( tbuf.tms_utime + tbuf.tms_cutime ) / CLK_TCK;
178 <        s = ( tbuf.tms_stime + tbuf.tms_cstime ) / CLK_TCK;
177 > #ifdef _SC_CLK_TCK
178 >        period = 1.0 / sysconf(_SC_CLK_TCK);
179   #endif
180 <        gethostname(hostname, sizeof(hostname));
180 >        u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
181 >        s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
182 > #endif
183  
184 +        /* PMAP: Get photon map bias compensation statistics */
185 +        pmapBiasCompReport(bcStat);
186 +        
187          sprintf(errmsg,
188 <                "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
189 <                        nrays, pctdone, u/3600., s/3600.,
190 <                        (tlastrept-tstart)/3600., hostname);
188 >                        "%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n",
189 >                        nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.),
190 >                        (tlastrept-tstart)*(1./3600.), myhostname(), getpid());
191          eputs(errmsg);
192 + #ifdef SIGCONT
193 +        signal(SIGCONT, report);
194 + #endif
195   }
196   #else
197 < report()                /* report progress */
197 > static void
198 > report(int dummy)               /* report progress */
199   {
200 <        tlastrept = time((long *)0);
201 <        sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
202 <                        nrays, pctdone, (tlastrept-tstart)/3600.0);
200 >        char    bcStat [128];
201 >        
202 >        tlastrept = time((time_t *)NULL);
203 >
204 >        /* PMAP: Get photon map bias compensation statistics */
205 >        pmapBiasCompReport(bcStat);
206 >        
207 >        sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n",
208 >                        nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0);
209          eputs(errmsg);
169        signal(SIGCONT, report);
210   }
211   #endif
212  
213  
214 < rpict(seq, pout, zout, prvr)                    /* generate image(s) */
215 < int  seq;
216 < char  *pout, *zout, *prvr;
214 > void
215 > rpict(                  /* generate image(s) */
216 >        int  seq,
217 >        char  *pout,
218 >        char  *zout,
219 >        char  *prvr
220 > )
221   /*
222   * If seq is greater than zero, then we will render a sequence of
223   * images based on view parameter strings read from the standard input.
# Line 186 | Line 230 | char  *pout, *zout, *prvr;
230   * sequenced file naming.
231   */
232   {
189        extern char  *rindex(), *strncpy(), *strcat(), *strcpy();
233          char  fbuf[128], fbuf2[128];
234          int  npicts;
235 <        register char  *cp;
235 >        char  *cp;
236          RESOLU  rs;
237          double  pa;
238                                          /* check sampling */
# Line 205 | Line 248 | char  *pout, *zout, *prvr;
248          if (seq <= 0)
249                  seq = 0;
250          else if (prvr != NULL && isint(prvr)) {
251 <                register int  rn;               /* skip to specified view */
251 >                int  rn;                /* skip to specified view */
252                  if ((rn = atoi(prvr)) < seq)
253                          error(USER, "recover frame less than start frame");
254                  if (pout == NULL)
# Line 213 | Line 256 | char  *pout, *zout, *prvr;
256                  for ( ; seq < rn; seq++)
257                          if (nextview(stdin) == EOF)
258                                  error(USER, "unexpected EOF on view input");
259 +                setview(&ourview);
260                  prvr = fbuf;                    /* mark for renaming */
261          }
262 <        if (pout != NULL & prvr != NULL) {
262 >        if ((pout != NULL) & (prvr != NULL)) {
263                  sprintf(fbuf, pout, seq);
264                  if (!strcmp(prvr, fbuf)) {      /* rename */
265                          strcpy(fbuf2, fbuf);
# Line 225 | Line 269 | char  *pout, *zout, *prvr;
269                                  cp--;
270                          strcpy(cp, RFTEMPLATE);
271                          prvr = mktemp(fbuf2);
272 <                        if (rename(fbuf, prvr) < 0)
272 >                        if (rename(fbuf, prvr) < 0) {
273                                  if (errno == ENOENT) {  /* ghost file */
274                                          sprintf(errmsg,
275                                                  "new output file \"%s\"",
# Line 238 | Line 282 | char  *pout, *zout, *prvr;
282                                                  fbuf, prvr);
283                                          error(SYSTEM, errmsg);
284                                  }
285 +                        }
286                  }
287          }
288          npicts = 0;                     /* render sequence */
# Line 246 | Line 291 | char  *pout, *zout, *prvr;
291                          break;
292                  pctdone = 0.0;
293                  if (pout != NULL) {
294 +                        int     myfd;
295 +                        close(1);                       /* reassign stdout */
296                          sprintf(fbuf, pout, seq);
297 <                        if (file_exists(fbuf)) {
298 <                                if (prvr != NULL || !strcmp(fbuf, pout)) {
297 >                tryagain:
298 >                        errno = 0;                      /* exclusive open */
299 >                        if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
300 >                                if ((errno != EEXIST) | (prvr != NULL) ||
301 >                                                !strcmp(fbuf, pout)) {
302                                          sprintf(errmsg,
303 <                                                "output file \"%s\" exists",
303 >                                                "cannot open output file \"%s\"",
304                                                  fbuf);
305 <                                        error(USER, errmsg);
305 >                                        error(SYSTEM, errmsg);
306                                  }
307 +                                setview(&ourview);
308                                  continue;               /* don't clobber */
309                          }
310 <                        if (freopen(fbuf, "w", stdout) == NULL) {
311 <                                sprintf(errmsg,
312 <                                        "cannot open output file \"%s\"", fbuf);
262 <                                error(SYSTEM, errmsg);
310 >                        if (myfd != 1) {
311 >                                unlink(fbuf);
312 >                                goto tryagain;          /* leave it open */
313                          }
314 < #ifdef MSDOS
265 <                        setmode(fileno(stdout), O_BINARY);
266 < #endif
314 >                        SET_FD_BINARY(1);
315                          dupheader();
316                  }
317                  hres = hresolu; vres = vresolu; pa = pixaspect;
318 <                if (prvr != NULL)
319 <                        if (viewfile(prvr, &ourview, &rs) <= 0
272 <                                        || rs.or != PIXSTANDARD) {
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);
# Line 278 | Line 325 | char  *pout, *zout, *prvr;
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);
# Line 293 | Line 341 | char  *pout, *zout, *prvr;
341                  fputs(VIEWSTR, stdout);
342                  fprintview(&ourview, stdout);
343                  putchar('\n');
344 <                if (pa < .99 || pa > 1.01)
344 >                if ((pa < .99) | (pa > 1.01))
345                          fputaspect(pa, stdout);
346 <                fputformat(COLRFMT, stdout);
347 <                putchar('\n');
346 >                fputnow(stdout);
347 >                if (out_prims == xyzprims) {
348 >                        fputformat(CIEFMT, stdout);
349 >                } else {
350 >                        fputprims(out_prims, stdout);
351 >                        fputformat(COLRFMT, stdout);
352 >                }
353 >                putchar('\n');          /* close header */
354                  if (zout != NULL)
355                          sprintf(cp=fbuf, zout, seq);
356                  else
# Line 311 | Line 365 | char  *pout, *zout, *prvr;
365   }
366  
367  
368 < nextview(fp)                            /* get next view from fp */
369 < FILE  *fp;
368 > static int
369 > nextview(                               /* get next view from fp */
370 >        FILE  *fp
371 > )
372   {
373          char  linebuf[256];
374  
375 +        lastview = ourview;
376          while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
377                  if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
378                          return(0);
# Line 323 | Line 380 | FILE  *fp;
380   }      
381  
382  
383 < render(zfile, oldfile)                          /* render the scene */
384 < char  *zfile, *oldfile;
383 > static void
384 > render(                         /* render the scene */
385 >        char  *zfile,
386 >        char  *oldfile
387 > )
388   {
389 <        extern long  lseek();
389 >        const int  srcdrawing =         /* manually draw tiny light sources? */
390 >                (directvis && dblur <= FTINY && (mblur <= FTINY) | !lastview.type);
391          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
392          float  *zbar[MAXDIV+1];         /* z values */
393          char  *sampdens;                /* previous sample density */
# Line 336 | Line 397 | char  *zfile, *oldfile;
397          int  zfd;
398          COLOR  *colptr;
399          float  *zptr;
400 <        register int  i;
400 >        int  i;
401 >                                        /* check for empty image */
402 >        if ((hres <= 0) | (vres <= 0)) {
403 >                error(WARNING, "empty output picture");
404 >                fprtresolu(0, 0, stdout);
405 >                return;
406 >        }
407                                          /* allocate scanlines */
408          for (i = 0; i <= psample; i++) {
409                  scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
# Line 347 | Line 414 | char  *zfile, *oldfile;
414          ystep = (psample*99+70)/140;
415          if (hstep > 2) {
416                  i = hres/hstep + 2;
417 <                if ((sampdens = malloc(i)) == NULL)
417 >                if ((sampdens = (char *)malloc(i)) == NULL)
418                          goto memerr;
419                  while (i--)
420                          sampdens[i] = hstep;
# Line 359 | Line 426 | char  *zfile, *oldfile;
426                          sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
427                          error(SYSTEM, errmsg);
428                  }
429 < #ifdef MSDOS
363 <                setmode(zfd, O_BINARY);
364 < #endif
429 >                SET_FD_BINARY(zfd);
430                  for (i = 0; i <= psample; i++) {
431                          zbar[i] = (float *)malloc(hres*sizeof(float));
432                          if (zbar[i] == NULL)
# Line 376 | Line 441 | char  *zfile, *oldfile;
441          fprtresolu(hres, vres, stdout);
442                                          /* recover file and compute first */
443          i = salvage(oldfile);
444 <        if (zfd != -1 && i > 0 &&
445 <                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
444 >        if (i >= vres)
445 >                goto alldone;
446 >        if ((zfd != -1) & (i > 0) &&
447 >                        lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
448                  error(SYSTEM, "z-file seek error in render");
449          pctdone = 100.0*i/vres;
450          if (ralrm > 0)                  /* report init stats */
451 <                report();
452 < #ifndef  BSD
451 >                report(0);
452 > #ifdef SIGCONT
453          else
387 #endif
454          signal(SIGCONT, report);
455 <        ypos = vres-1 - i;
455 > #endif
456 >        ypos = vres-1 - i;                      /* initialize sampling */
457 >        if (srcdrawing)
458 >                init_drawsources(psample);
459          fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
460                                                  /* compute scanlines */
461          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
# Line 406 | Line 475 | char  *zfile, *oldfile;
475                                  hres, ypos, hstep);
476                                                          /* fill bar */
477                  fillscanbar(scanbar, zbar, hres, ypos, ystep);
478 +                if (srcdrawing)                         /* add bitty sources */
479 +                        drawsources((COLORV **)scanbar, out_prims, zbar, 0, hres, ypos, ystep);
480                                                          /* write it out */
481 < #ifndef  BSD
481 > #ifdef SIGCONT
482                  signal(SIGCONT, SIG_IGN);       /* don't interrupt writes */
483   #endif
484                  for (i = ystep; i > 0; i--) {
# Line 422 | Line 493 | char  *zfile, *oldfile;
493                          goto writerr;
494                                                          /* record progress */
495                  pctdone = 100.0*(vres-1-ypos)/vres;
496 <                if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
497 <                        report();
498 < #ifndef  BSD
496 >                if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
497 >                        report(0);
498 > #ifdef SIGCONT
499                  else
500                          signal(SIGCONT, report);
501   #endif
502          }
503                                                  /* clean up */
504 + #ifdef SIGCONT
505          signal(SIGCONT, SIG_IGN);
506 <        if (zfd != -1) {
507 <                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
506 > #endif
507 >        if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
508                                  < hres*sizeof(float))
509 <                        goto writerr;
509 >                goto writerr;
510 >        fwritescan(scanbar[0], hres, stdout);
511 >        if (fflush(stdout) == EOF)
512 >                goto writerr;
513 > alldone:
514 >        if (zfd != -1) {
515                  if (close(zfd) == -1)
516                          goto writerr;
517                  for (i = 0; i <= psample; i++)
518 <                        free((char *)zbar[i]);
518 >                        free((void *)zbar[i]);
519          }
443        fwritescan(scanbar[0], hres, stdout);
444        if (fflush(stdout) == EOF)
445                goto writerr;
520          for (i = 0; i <= psample; i++)
521 <                free((char *)scanbar[i]);
521 >                free((void *)scanbar[i]);
522          if (sampdens != NULL)
523                  free(sampdens);
524          pctdone = 100.0;
525          if (ralrm > 0)
526 <                report();
526 >                report(0);
527 > #ifdef SIGCONT
528          signal(SIGCONT, SIG_DFL);
529 + #endif
530          return;
531   writerr:
532          error(SYSTEM, "write error in render");
# Line 459 | Line 535 | memerr:
535   }
536  
537  
538 < fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
539 < register COLOR  *scanline;
540 < register float  *zline;
541 < register char  *sd;
542 < int  xres, y, xstep;
538 > static void
539 > fillscanline(   /* fill scan at y */
540 >        COLOR   *scanline,
541 >        float   *zline,
542 >        char  *sd,
543 >        int  xres,
544 >        int  y,
545 >        int  xstep
546 > )
547   {
548          static int  nc = 0;             /* number of calls */
549          int  bl = xstep, b = xstep;
550          double  z;
551 <        register int  i;
552 <        
551 >        int  i;
552 >
553          z = pixvalue(scanline[0], 0, y);
554          if (zline) zline[0] = z;
555                                  /* zig-zag start for quincunx pattern */
# Line 485 | Line 565 | int  xres, y, xstep;
565                          b = fillsample(scanline, zline, 0, y, i, 0, b/2);
566                  else
567                          b = fillsample(scanline+i-xstep,
568 <                                        zline ? zline+i-xstep : NULL,
568 >                                        zline ? zline+i-xstep : (float *)NULL,
569                                          i-xstep, y, xstep, 0, b/2);
570                  if (sd) *sd++ = nc & 1 ? bl : b;
571                  bl = b;
# Line 494 | Line 574 | int  xres, y, xstep;
574   }
575  
576  
577 < fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
578 < register COLOR  *scanbar[];
579 < register float  *zbar[];
580 < int  xres, y, ysize;
577 > static void
578 > fillscanbar(    /* fill interior */
579 >        COLOR   *scanbar[],
580 >        float   *zbar[],
581 >        int  xres,
582 >        int  y,
583 >        int  ysize
584 > )
585   {
586          COLOR  vline[MAXDIV+1];
587          float  zline[MAXDIV+1];
588          int  b = ysize;
589 <        register int  i, j;
590 <        
589 >        int  i, j;
590 >
591          for (i = 0; i < xres; i++) {
508                
592                  copycolor(vline[0], scanbar[0][i]);
593                  copycolor(vline[ysize], scanbar[ysize][i]);
594                  if (zbar[0]) {
595                          zline[0] = zbar[0][i];
596                          zline[ysize] = zbar[ysize][i];
597                  }
598 <                
516 <                b = fillsample(vline, zbar[0] ? zline : NULL,
598 >                b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
599                                  i, y, 0, ysize, b/2);
600 <                
600 >
601                  for (j = 1; j < ysize; j++)
602                          copycolor(scanbar[j][i], vline[j]);
603                  if (zbar[0])
# Line 525 | Line 607 | int  xres, y, ysize;
607   }
608  
609  
610 < int
611 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
612 < register COLOR  *colline;
613 < register float  *zline;
614 < int  x, y;
615 < int  xlen, ylen;
616 < int  b;
610 > static int
611 > fillsample( /* fill interior points */
612 >        COLOR   *colline,
613 >        float   *zline,
614 >        int  x,
615 >        int  y,
616 >        int  xlen,
617 >        int  ylen,
618 >        int  b
619 > )
620   {
621          double  ratio;
622          double  z;
623          COLOR  ctmp;
624          int  ncut;
625 <        register int  len;
626 <        
625 >        int  len;
626 >
627          if (xlen > 0)                   /* x or y length is zero */
628                  len = xlen;
629          else
630                  len = ylen;
631 <                
631 >
632          if (len <= 1)                   /* limit recursion */
633                  return(0);
634 <        
635 <        if (b > 0
636 <        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
634 >
635 >        if (b > 0 ||
636 >        (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
637                          || bigdiff(colline[0], colline[len], maxdiff)) {
638 <        
638 >
639                  z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
640                  if (zline) zline[len>>1] = z;
641                  ncut = 1;
557                
642          } else {                                        /* interpolate */
559        
643                  copycolor(colline[len>>1], colline[len]);
644                  ratio = (double)(len>>1) / len;
645                  scalecolor(colline[len>>1], ratio);
# Line 570 | Line 653 | int  b;
653          }
654                                                          /* recurse */
655          ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
656 <        
657 <        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
656 >
657 >        ncut += fillsample(colline+(len>>1),
658 >                        zline ? zline+(len>>1) : (float *)NULL,
659                          x+(xlen>>1), y+(ylen>>1),
660                          xlen-(xlen>>1), ylen-(ylen>>1), b/2);
661  
# Line 579 | Line 663 | int  b;
663   }
664  
665  
666 < double
667 < pixvalue(col, x, y)             /* compute pixel value */
668 < COLOR  col;                     /* returned color */
669 < int  x, y;                      /* pixel position */
666 > static double
667 > pixvalue(               /* compute pixel value */
668 >        COLOR  col,                     /* returned color */
669 >        int  x,                 /* pixel position */
670 >        int  y
671 > )
672   {
673 <        static RAY  thisray;
674 <
675 <        if (viewray(thisray.rorg, thisray.rdir, &ourview,
676 <                        (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
677 <                setcolor(col, 0.0, 0.0, 0.0);
673 >        RAY  thisray;
674 >        FVECT   lorg, ldir;
675 >        double  hpos, vpos, lmax;
676 >        int     i;
677 >                                                /* compute view ray */
678 >        setcolor(col, 0.0, 0.0, 0.0);
679 >        hpos = (x+pixjitter())/hres;
680 >        vpos = (y+pixjitter())/vres;
681 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
682 >                                        &ourview, hpos, vpos)) < -FTINY)
683                  return(0.0);
684 +                                                /* set pixel index */
685 +        samplendx = pixnumber(x,y,hres,vres);
686 +                                                /* optional motion blur */
687 +        if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
688 +                                        &lastview, hpos, vpos)) >= -FTINY) {
689 +                double  d = mblur*(.5-urand(281+samplendx));
690 +
691 +                thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
692 +                for (i = 3; i--; ) {
693 +                        thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i];
694 +                        thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i];
695 +                }
696 +                if (normalize(thisray.rdir) == 0.0)
697 +                        return(0.0);
698          }
699 +                                                /* depth-of-field */
700 +        if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur))
701 +                return(0.0);
702  
703 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
703 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
704  
597        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
598
705          rayvalue(&thisray);                     /* trace ray */
706 +                                                /* -> color */
707 +        scolor_out(col, out_prims, thisray.rcol);
708  
709 <        copycolor(col, thisray.rcol);           /* return color */
602 <        
603 <        return(thisray.rt);                     /* return distance */
709 >        return(raydistance(&thisray));          /* return distance */
710   }
711  
712  
713 < int
714 < salvage(oldfile)                /* salvage scanlines from killed program */
715 < char  *oldfile;
713 > static int
714 > salvage(                /* salvage scanlines from killed program */
715 >        char  *oldfile
716 > )
717   {
718          COLR  *scanline;
719          FILE  *fp;
720          int  x, y;
721  
722          if (oldfile == NULL)
723 <                return(0);
724 <        
723 >                goto gotzip;
724 >
725          if ((fp = fopen(oldfile, "r")) == NULL) {
726                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
727                  error(WARNING, errmsg);
728 <                return(0);
728 >                goto gotzip;
729          }
730 < #ifdef MSDOS
624 <        setmode(fileno(fp), O_BINARY);
625 < #endif
730 >        SET_FILE_BINARY(fp);
731                                  /* discard header */
732          getheader(fp, NULL, NULL);
733                                  /* get picture size */
# Line 631 | Line 736 | char  *oldfile;
736                                  oldfile);
737                  error(WARNING, errmsg);
738                  fclose(fp);
739 <                return(0);
739 >                goto gotzip;
740          }
741  
742 <        if (x != hres || y != vres) {
742 >        if ((x != hres) | (y != vres)) {
743                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
744                                  oldfile);
745                  error(USER, errmsg);
# Line 651 | Line 756 | char  *oldfile;
756          }
757          if (fflush(stdout) == EOF)
758                  goto writerr;
759 <        free((char *)scanline);
759 >        free((void *)scanline);
760          fclose(fp);
761          unlink(oldfile);
762          return(y);
763 + gotzip:
764 +        if (fflush(stdout) == EOF)
765 +                error(SYSTEM, "error writing picture header");
766 +        return(0);
767   writerr:
768          sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
769          error(SYSTEM, errmsg);
770 +        return -1; /* pro forma return */
771   }
772  
773 <
774 < int
775 < pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
776 < register int  x, y;
777 < int  xres, yres;
773 > static int
774 > pixnumber(              /* compute pixel index (screen door) */
775 >        int  x,
776 >        int  y,
777 >        int  xres,
778 >        int  yres
779 > )
780   {
781 <        x -= y;
782 <        while (x < 0)
783 <                x += xres;
784 <        return((((x>>2)*yres + y) << 2) + (x & 3));
781 >        unsigned        nbits = 0;
782 >        bitmask_t       coord[2];
783 >
784 >        if (xres < yres) xres = yres;
785 >        while (xres > 0) {
786 >                xres >>= 1;
787 >                ++nbits;
788 >        }
789 >        coord[0] = x; coord[1] = y;
790 >        return ((int)hilbert_c2i(2, nbits, coord));
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines