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.41 by greg, Fri Dec 8 18:49:20 1995 UTC vs.
Revision 2.88 by greg, Fri May 9 23:28:57 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines