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.36 by greg, Sun Dec 12 13:56:08 1993 UTC vs.
Revision 2.90 by rschregle, Wed Apr 22 20:28:16 2015 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 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 + #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   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 */
# Line 56 | Line 63 | 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  mblur = 0.;                     /* motion blur parameter */
67 +
68 + double  dblur = 0.;                     /* depth-of-field blur parameter */
69 +
70 + void  (*trace)() = NULL;                /* trace call */
71 +
72 + int  do_irrad = 0;                      /* compute irradiance? */
73 +
74 + int  rand_samp = 0;                     /* pure Monte Carlo sampling? */
75 +
76   double  dstrsrc = 0.0;                  /* square source distribution */
77   double  shadthresh = .05;               /* shadow threshold */
78   double  shadcert = .5;                  /* shadow certainty */
# Line 64 | Line 81 | int  vspretest = 512;                  /* virtual source pretest dens
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  maxdepth = 6;                      /* maximum recursion depth */
71 < double  minweight = 5e-3;               /* minimum ray weight */
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 + int  ambvwt = 0;                        /* initial weight for ambient value */
100   double  ambacc = 0.2;                   /* ambient accuracy */
101 < int  ambres = 32;                       /* ambient resolution */
102 < int  ambdiv = 128;                      /* ambient divisions */
103 < int  ambssamp = 0;                      /* ambient super-samples */
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  
82 #ifdef MSDOS
83 int  ralrm = 60;                        /* seconds between reports */
84 #else
108   int  ralrm = 0;                         /* seconds between reports */
86 #endif
109  
110   double  pctdone = 0.0;                  /* percentage done */
89
111   time_t  tlastrept = 0L;                 /* time at last report */
112 + time_t  tstart;                         /* starting time */
113  
92 extern time_t  tstart;                  /* starting time */
93
94 extern unsigned long  nrays;            /* number of rays traced */
95
114   #define  MAXDIV         16              /* maximum sample size */
115  
116   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
117  
118 < static int  hres, vres;                 /* resolution for this frame */
118 > int  hres, vres;                        /* resolution for this frame */
119  
120 < extern char  *mktemp();
120 > static VIEW     lastview;               /* the previous view input */
121  
122 < double  pixvalue();
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 < #ifdef NIX
136 < #define  file_exists(f) (access(f,F_OK)==0)
137 < #else
135 >
136 >
137 > #ifdef RHAS_STAT
138   #include  <sys/types.h>
139   #include  <sys/stat.h>
140   int
# Line 116 | Line 145 | char  *fname;
145          if (stat(fname, &sbuf) < 0) return(0);
146          return((sbuf.st_mode & S_IFREG) != 0);
147   }
148 + #else
149 + #define  file_exists(f) (access(f,F_OK)==0)
150   #endif
151  
152  
153 + void
154   quit(code)                      /* quit program */
155   int  code;
156   {
157          if (code)                       /* report status */
158 <                report();
158 >                report(0);
159 > #ifndef NON_POSIX
160          headclean();                    /* delete header file */
161          pfclean();                      /* clean up persist files */
162 + #endif
163          exit(code);
164   }
165  
166  
167 < #ifndef NIX
168 < report()                /* report progress */
167 > #ifndef NON_POSIX
168 > static void
169 > report(int dummy)               /* report progress */
170   {
171 <        double  u, s;
171 >        char                    bcStat [128];
172 >        double          u, s;
173   #ifdef BSD
174 <        char  hostname[257];
139 <        struct rusage  rubuf;
174 >        struct rusage   rubuf;
175   #else
176 <        struct tms  tbuf;
177 <        struct utsname  nambuf;
143 <        double  period;
144 < #define hostname  nambuf.nodename
176 >        double          period = 1.0 / 60.0;
177 >        struct tms      tbuf;
178   #endif
179  
180          tlastrept = time((time_t *)NULL);
181   #ifdef BSD
182          getrusage(RUSAGE_SELF, &rubuf);
183 <        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
184 <        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
183 >        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
184 >        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
185          getrusage(RUSAGE_CHILDREN, &rubuf);
186 <        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
187 <        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
155 <        gethostname(hostname, sizeof(hostname));
186 >        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
187 >        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
188   #else
189          times(&tbuf);
190   #ifdef _SC_CLK_TCK
191          period = 1.0 / sysconf(_SC_CLK_TCK);
160 #else
161        period = 1.0 / 60.0;
192   #endif
193          u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
194          s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
165        uname(&nambuf);
195   #endif
196  
197 +        /* PMAP: Get photon map bias compensation statistics */
198 +        pmapBiasCompReport(bcStat);
199 +        
200          sprintf(errmsg,
201 <                "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
202 <                        nrays, pctdone, u/3600., s/3600.,
203 <                        (tlastrept-tstart)/3600., hostname);
201 >                        "%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n",
202 >                        nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.),
203 >                        (tlastrept-tstart)*(1./3600.), myhostname(), getpid());
204          eputs(errmsg);
205 < #ifndef BSD
205 > #ifdef SIGCONT
206          signal(SIGCONT, report);
175 #undef hostname
207   #endif
208   }
209   #else
210 < report()                /* report progress */
210 > static void
211 > report(int dummy)               /* report progress */
212   {
213 +        char    bcStat [128];
214 +        
215          tlastrept = time((time_t *)NULL);
216 <        sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
217 <                        nrays, pctdone, (tlastrept-tstart)/3600.0);
216 >
217 >        /* PMAP: Get photon map bias compensation statistics */
218 >        pmapBiasCompReport(bcStat);
219 >        
220 >        sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n",
221 >                        nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0);
222          eputs(errmsg);
223   }
224   #endif
225  
226  
227 < rpict(seq, pout, zout, prvr)                    /* generate image(s) */
228 < int  seq;
229 < char  *pout, *zout, *prvr;
227 > void
228 > rpict(                  /* generate image(s) */
229 >        int  seq,
230 >        char  *pout,
231 >        char  *zout,
232 >        char  *prvr
233 > )
234   /*
235   * If seq is greater than zero, then we will render a sequence of
236   * images based on view parameter strings read from the standard input.
# Line 201 | Line 243 | char  *pout, *zout, *prvr;
243   * sequenced file naming.
244   */
245   {
204        extern char  *rindex(), *strncpy(), *strcat(), *strcpy();
246          char  fbuf[128], fbuf2[128];
247          int  npicts;
248 <        register char  *cp;
248 >        char  *cp;
249          RESOLU  rs;
250          double  pa;
251                                          /* check sampling */
# Line 220 | Line 261 | char  *pout, *zout, *prvr;
261          if (seq <= 0)
262                  seq = 0;
263          else if (prvr != NULL && isint(prvr)) {
264 <                register int  rn;               /* skip to specified view */
264 >                int  rn;                /* skip to specified view */
265                  if ((rn = atoi(prvr)) < seq)
266                          error(USER, "recover frame less than start frame");
267                  if (pout == NULL)
# Line 228 | Line 269 | char  *pout, *zout, *prvr;
269                  for ( ; seq < rn; seq++)
270                          if (nextview(stdin) == EOF)
271                                  error(USER, "unexpected EOF on view input");
272 +                setview(&ourview);
273                  prvr = fbuf;                    /* mark for renaming */
274          }
275 <        if (pout != NULL & prvr != NULL) {
275 >        if ((pout != NULL) & (prvr != NULL)) {
276                  sprintf(fbuf, pout, seq);
277                  if (!strcmp(prvr, fbuf)) {      /* rename */
278                          strcpy(fbuf2, fbuf);
# Line 240 | Line 282 | char  *pout, *zout, *prvr;
282                                  cp--;
283                          strcpy(cp, RFTEMPLATE);
284                          prvr = mktemp(fbuf2);
285 <                        if (rename(fbuf, prvr) < 0)
285 >                        if (rename(fbuf, prvr) < 0) {
286                                  if (errno == ENOENT) {  /* ghost file */
287                                          sprintf(errmsg,
288                                                  "new output file \"%s\"",
# Line 253 | Line 295 | char  *pout, *zout, *prvr;
295                                                  fbuf, prvr);
296                                          error(SYSTEM, errmsg);
297                                  }
298 +                        }
299                  }
300          }
301          npicts = 0;                     /* render sequence */
# Line 269 | Line 312 | char  *pout, *zout, *prvr;
312                                                  fbuf);
313                                          error(USER, errmsg);
314                                  }
315 +                                setview(&ourview);
316                                  continue;               /* don't clobber */
317                          }
318                          if (freopen(fbuf, "w", stdout) == NULL) {
# Line 276 | Line 320 | char  *pout, *zout, *prvr;
320                                          "cannot open output file \"%s\"", fbuf);
321                                  error(SYSTEM, errmsg);
322                          }
323 < #ifdef MSDOS
280 <                        setmode(fileno(stdout), O_BINARY);
281 < #endif
323 >                        SET_FILE_BINARY(stdout);
324                          dupheader();
325                  }
326                  hres = hresolu; vres = vresolu; pa = pixaspect;
327 <                if (prvr != NULL)
328 <                        if (viewfile(prvr, &ourview, &rs) <= 0
287 <                                        || rs.or != PIXSTANDARD) {
327 >                if (prvr != NULL) {
328 >                        if (viewfile(prvr, &ourview, &rs) <= 0) {
329                                  sprintf(errmsg,
330                          "cannot recover view parameters from \"%s\"", prvr);
331                                  error(WARNING, errmsg);
# Line 293 | Line 334 | char  *pout, *zout, *prvr;
334                                  hres = scanlen(&rs);
335                                  vres = numscans(&rs);
336                          }
337 +                }
338                  if ((cp = setview(&ourview)) != NULL)
339                          error(USER, cp);
340                  normaspect(viewaspect(&ourview), &pa, &hres, &vres);
# Line 308 | Line 350 | char  *pout, *zout, *prvr;
350                  fputs(VIEWSTR, stdout);
351                  fprintview(&ourview, stdout);
352                  putchar('\n');
353 <                if (pa < .99 || pa > 1.01)
353 >                if ((pa < .99) | (pa > 1.01))
354                          fputaspect(pa, stdout);
355 +                fputnow(stdout);
356                  fputformat(COLRFMT, stdout);
357                  putchar('\n');
358                  if (zout != NULL)
# Line 326 | Line 369 | char  *pout, *zout, *prvr;
369   }
370  
371  
372 < nextview(fp)                            /* get next view from fp */
373 < FILE  *fp;
372 > static int
373 > nextview(                               /* get next view from fp */
374 >        FILE  *fp
375 > )
376   {
377          char  linebuf[256];
378  
379 +        lastview = ourview;
380          while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
381                  if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
382                          return(0);
# Line 338 | Line 384 | FILE  *fp;
384   }      
385  
386  
387 < render(zfile, oldfile)                          /* render the scene */
388 < char  *zfile, *oldfile;
387 > static void
388 > render(                         /* render the scene */
389 >        char  *zfile,
390 >        char  *oldfile
391 > )
392   {
344        extern long  lseek();
393          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
394          float  *zbar[MAXDIV+1];         /* z values */
395          char  *sampdens;                /* previous sample density */
# Line 351 | Line 399 | char  *zfile, *oldfile;
399          int  zfd;
400          COLOR  *colptr;
401          float  *zptr;
402 <        register int  i;
402 >        int  i;
403 >                                        /* check for empty image */
404 >        if ((hres <= 0) | (vres <= 0)) {
405 >                error(WARNING, "empty output picture");
406 >                fprtresolu(0, 0, stdout);
407 >                return;
408 >        }
409                                          /* allocate scanlines */
410          for (i = 0; i <= psample; i++) {
411                  scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
# Line 362 | Line 416 | char  *zfile, *oldfile;
416          ystep = (psample*99+70)/140;
417          if (hstep > 2) {
418                  i = hres/hstep + 2;
419 <                if ((sampdens = malloc(i)) == NULL)
419 >                if ((sampdens = (char *)malloc(i)) == NULL)
420                          goto memerr;
421                  while (i--)
422                          sampdens[i] = hstep;
# Line 374 | Line 428 | char  *zfile, *oldfile;
428                          sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
429                          error(SYSTEM, errmsg);
430                  }
431 < #ifdef MSDOS
378 <                setmode(zfd, O_BINARY);
379 < #endif
431 >                SET_FD_BINARY(zfd);
432                  for (i = 0; i <= psample; i++) {
433                          zbar[i] = (float *)malloc(hres*sizeof(float));
434                          if (zbar[i] == NULL)
# Line 391 | Line 443 | char  *zfile, *oldfile;
443          fprtresolu(hres, vres, stdout);
444                                          /* recover file and compute first */
445          i = salvage(oldfile);
446 <        if (zfd != -1 && i > 0 &&
447 <                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
446 >        if (i >= vres)
447 >                goto alldone;
448 >        if ((zfd != -1) & (i > 0) &&
449 >                        lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
450                  error(SYSTEM, "z-file seek error in render");
451          pctdone = 100.0*i/vres;
452          if (ralrm > 0)                  /* report init stats */
453 <                report();
454 < #ifndef  BSD
453 >                report(0);
454 > #ifdef SIGCONT
455          else
402 #endif
456          signal(SIGCONT, report);
457 <        ypos = vres-1 - i;
457 > #endif
458 >        ypos = vres-1 - i;                      /* initialize sampling */
459 >        if (directvis)
460 >                init_drawsources(psample);
461          fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
462                                                  /* compute scanlines */
463          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
# Line 421 | Line 477 | char  *zfile, *oldfile;
477                                  hres, ypos, hstep);
478                                                          /* fill bar */
479                  fillscanbar(scanbar, zbar, hres, ypos, ystep);
480 +                if (directvis)                          /* add bitty sources */
481 +                        drawsources(scanbar, zbar, 0, hres, ypos, ystep);
482                                                          /* write it out */
483 < #ifndef  BSD
483 > #ifdef SIGCONT
484                  signal(SIGCONT, SIG_IGN);       /* don't interrupt writes */
485   #endif
486                  for (i = ystep; i > 0; i--) {
# Line 438 | Line 496 | char  *zfile, *oldfile;
496                                                          /* record progress */
497                  pctdone = 100.0*(vres-1-ypos)/vres;
498                  if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
499 <                        report();
500 < #ifndef  BSD
499 >                        report(0);
500 > #ifdef SIGCONT
501                  else
502                          signal(SIGCONT, report);
503   #endif
504          }
505                                                  /* clean up */
506 + #ifdef SIGCONT
507          signal(SIGCONT, SIG_IGN);
508 <        if (zfd != -1) {
509 <                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
508 > #endif
509 >        if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
510                                  < hres*sizeof(float))
511 <                        goto writerr;
511 >                goto writerr;
512 >        fwritescan(scanbar[0], hres, stdout);
513 >        if (fflush(stdout) == EOF)
514 >                goto writerr;
515 > alldone:
516 >        if (zfd != -1) {
517                  if (close(zfd) == -1)
518                          goto writerr;
519                  for (i = 0; i <= psample; i++)
520 <                        free((char *)zbar[i]);
520 >                        free((void *)zbar[i]);
521          }
458        fwritescan(scanbar[0], hres, stdout);
459        if (fflush(stdout) == EOF)
460                goto writerr;
522          for (i = 0; i <= psample; i++)
523 <                free((char *)scanbar[i]);
523 >                free((void *)scanbar[i]);
524          if (sampdens != NULL)
525                  free(sampdens);
526          pctdone = 100.0;
527          if (ralrm > 0)
528 <                report();
528 >                report(0);
529 > #ifdef SIGCONT
530          signal(SIGCONT, SIG_DFL);
531 + #endif
532          return;
533   writerr:
534          error(SYSTEM, "write error in render");
# Line 474 | Line 537 | memerr:
537   }
538  
539  
540 < fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
541 < register COLOR  *scanline;
542 < register float  *zline;
543 < register char  *sd;
544 < int  xres, y, xstep;
540 > static void
541 > fillscanline(   /* fill scan at y */
542 >        COLOR   *scanline,
543 >        float   *zline,
544 >        char  *sd,
545 >        int  xres,
546 >        int  y,
547 >        int  xstep
548 > )
549   {
550          static int  nc = 0;             /* number of calls */
551          int  bl = xstep, b = xstep;
552          double  z;
553 <        register int  i;
554 <        
553 >        int  i;
554 >
555          z = pixvalue(scanline[0], 0, y);
556          if (zline) zline[0] = z;
557                                  /* zig-zag start for quincunx pattern */
# Line 500 | Line 567 | int  xres, y, xstep;
567                          b = fillsample(scanline, zline, 0, y, i, 0, b/2);
568                  else
569                          b = fillsample(scanline+i-xstep,
570 <                                        zline ? zline+i-xstep : NULL,
570 >                                        zline ? zline+i-xstep : (float *)NULL,
571                                          i-xstep, y, xstep, 0, b/2);
572                  if (sd) *sd++ = nc & 1 ? bl : b;
573                  bl = b;
# Line 509 | Line 576 | int  xres, y, xstep;
576   }
577  
578  
579 < fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
580 < register COLOR  *scanbar[];
581 < register float  *zbar[];
582 < int  xres, y, ysize;
579 > static void
580 > fillscanbar(    /* fill interior */
581 >        COLOR   *scanbar[],
582 >        float   *zbar[],
583 >        int  xres,
584 >        int  y,
585 >        int  ysize
586 > )
587   {
588          COLOR  vline[MAXDIV+1];
589          float  zline[MAXDIV+1];
590          int  b = ysize;
591 <        register int  i, j;
592 <        
591 >        int  i, j;
592 >
593          for (i = 0; i < xres; i++) {
523                
594                  copycolor(vline[0], scanbar[0][i]);
595                  copycolor(vline[ysize], scanbar[ysize][i]);
596                  if (zbar[0]) {
597                          zline[0] = zbar[0][i];
598                          zline[ysize] = zbar[ysize][i];
599                  }
600 <                
531 <                b = fillsample(vline, zbar[0] ? zline : NULL,
600 >                b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
601                                  i, y, 0, ysize, b/2);
602 <                
602 >
603                  for (j = 1; j < ysize; j++)
604                          copycolor(scanbar[j][i], vline[j]);
605                  if (zbar[0])
# Line 540 | Line 609 | int  xres, y, ysize;
609   }
610  
611  
612 < int
613 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
614 < register COLOR  *colline;
615 < register float  *zline;
616 < int  x, y;
617 < int  xlen, ylen;
618 < int  b;
612 > static int
613 > fillsample( /* fill interior points */
614 >        COLOR   *colline,
615 >        float   *zline,
616 >        int  x,
617 >        int  y,
618 >        int  xlen,
619 >        int  ylen,
620 >        int  b
621 > )
622   {
623          double  ratio;
624          double  z;
625          COLOR  ctmp;
626          int  ncut;
627 <        register int  len;
628 <        
627 >        int  len;
628 >
629          if (xlen > 0)                   /* x or y length is zero */
630                  len = xlen;
631          else
632                  len = ylen;
633 <                
633 >
634          if (len <= 1)                   /* limit recursion */
635                  return(0);
636 <        
637 <        if (b > 0
638 <        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
636 >
637 >        if (b > 0 ||
638 >        (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
639                          || bigdiff(colline[0], colline[len], maxdiff)) {
640 <        
640 >
641                  z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
642                  if (zline) zline[len>>1] = z;
643                  ncut = 1;
572                
644          } else {                                        /* interpolate */
574        
645                  copycolor(colline[len>>1], colline[len]);
646                  ratio = (double)(len>>1) / len;
647                  scalecolor(colline[len>>1], ratio);
# Line 585 | Line 655 | int  b;
655          }
656                                                          /* recurse */
657          ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
658 <        
659 <        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
658 >
659 >        ncut += fillsample(colline+(len>>1),
660 >                        zline ? zline+(len>>1) : (float *)NULL,
661                          x+(xlen>>1), y+(ylen>>1),
662                          xlen-(xlen>>1), ylen-(ylen>>1), b/2);
663  
# Line 594 | Line 665 | int  b;
665   }
666  
667  
668 < double
669 < pixvalue(col, x, y)             /* compute pixel value */
670 < COLOR  col;                     /* returned color */
671 < int  x, y;                      /* pixel position */
668 > static double
669 > pixvalue(               /* compute pixel value */
670 >        COLOR  col,                     /* returned color */
671 >        int  x,                 /* pixel position */
672 >        int  y
673 > )
674   {
675 <        static RAY  thisray;
676 <
677 <        if (viewray(thisray.rorg, thisray.rdir, &ourview,
678 <                        (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
679 <                setcolor(col, 0.0, 0.0, 0.0);
675 >        extern void  SDsquare2disk(double ds[2], double seedx, double seedy);
676 >        RAY  thisray;
677 >        FVECT   lorg, ldir;
678 >        double  hpos, vpos, vdist, lmax;
679 >        int     i;
680 >                                                /* compute view ray */
681 >        setcolor(col, 0.0, 0.0, 0.0);
682 >        hpos = (x+pixjitter())/hres;
683 >        vpos = (y+pixjitter())/vres;
684 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
685 >                                        &ourview, hpos, vpos)) < -FTINY)
686                  return(0.0);
608        }
687  
688 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
688 >        vdist = ourview.vdist;
689 >                                                /* set pixel index */
690 >        samplendx = pixnumber(x,y,hres,vres);
691 >                                                /* optional motion blur */
692 >        if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
693 >                                        &lastview, hpos, vpos)) >= -FTINY) {
694 >                double  d = mblur*(.5-urand(281+samplendx));
695  
696 <        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
696 >                thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
697 >                for (i = 3; i--; ) {
698 >                        thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i];
699 >                        thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i];
700 >                }
701 >                if (normalize(thisray.rdir) == 0.0)
702 >                        return(0.0);
703 >                vdist = (1.-d)*vdist + d*lastview.vdist;
704 >        }
705 >                                                /* optional depth-of-field */
706 >        if (dblur > FTINY) {
707 >                double  vc, df[2];
708 >                                                /* random point on disk */
709 >                SDsquare2disk(df, frandom(), frandom());
710 >                df[0] *= .5*dblur;
711 >                df[1] *= .5*dblur;
712 >                if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) {
713 >                        double  adj = 1.0;
714 >                        if (ourview.type == VT_PER)
715 >                                adj /= DOT(thisray.rdir, ourview.vdir);
716 >                        df[0] /= sqrt(ourview.hn2);
717 >                        df[1] /= sqrt(ourview.vn2);
718 >                        for (i = 3; i--; ) {
719 >                                vc = ourview.vp[i] + adj*vdist*thisray.rdir[i];
720 >                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
721 >                                                        df[1]*ourview.vvec[i] ;
722 >                                thisray.rdir[i] = vc - thisray.rorg[i];
723 >                        }
724 >                } else {                        /* non-standard view case */
725 >                        double  dfd = PI/4.*dblur*(.5 - frandom());
726 >                        if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) {
727 >                                if (ourview.type != VT_CYL)
728 >                                        df[0] /= sqrt(ourview.hn2);
729 >                                df[1] /= sqrt(ourview.vn2);
730 >                        }
731 >                        for (i = 3; i--; ) {
732 >                                vc = ourview.vp[i] + vdist*thisray.rdir[i];
733 >                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
734 >                                                        df[1]*ourview.vvec[i] +
735 >                                                        dfd*ourview.vdir[i] ;
736 >                                thisray.rdir[i] = vc - thisray.rorg[i];
737 >                        }
738 >                }
739 >                if (normalize(thisray.rdir) == 0.0)
740 >                        return(0.0);
741 >        }
742  
743 +        rayorigin(&thisray, PRIMARY, NULL, NULL);
744 +
745          rayvalue(&thisray);                     /* trace ray */
746  
747          copycolor(col, thisray.rcol);           /* return color */
748 <        
748 >
749          return(thisray.rt);                     /* return distance */
750   }
751  
752  
753 < int
754 < salvage(oldfile)                /* salvage scanlines from killed program */
755 < char  *oldfile;
753 > static int
754 > salvage(                /* salvage scanlines from killed program */
755 >        char  *oldfile
756 > )
757   {
758          COLR  *scanline;
759          FILE  *fp;
760          int  x, y;
761  
762          if (oldfile == NULL)
763 <                return(0);
764 <        
763 >                goto gotzip;
764 >
765          if ((fp = fopen(oldfile, "r")) == NULL) {
766                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
767                  error(WARNING, errmsg);
768 <                return(0);
768 >                goto gotzip;
769          }
770 < #ifdef MSDOS
639 <        setmode(fileno(fp), O_BINARY);
640 < #endif
770 >        SET_FILE_BINARY(fp);
771                                  /* discard header */
772          getheader(fp, NULL, NULL);
773                                  /* get picture size */
# Line 646 | Line 776 | char  *oldfile;
776                                  oldfile);
777                  error(WARNING, errmsg);
778                  fclose(fp);
779 <                return(0);
779 >                goto gotzip;
780          }
781  
782 <        if (x != hres || y != vres) {
782 >        if ((x != hres) | (y != vres)) {
783                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
784                                  oldfile);
785                  error(USER, errmsg);
# Line 666 | Line 796 | char  *oldfile;
796          }
797          if (fflush(stdout) == EOF)
798                  goto writerr;
799 <        free((char *)scanline);
799 >        free((void *)scanline);
800          fclose(fp);
801          unlink(oldfile);
802          return(y);
803 + gotzip:
804 +        if (fflush(stdout) == EOF)
805 +                error(SYSTEM, "error writing picture header");
806 +        return(0);
807   writerr:
808          sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
809          error(SYSTEM, errmsg);
810 +        return -1; /* pro forma return */
811   }
812  
813 <
814 < int
815 < pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
816 < register int  x, y;
817 < int  xres, yres;
813 > static int
814 > pixnumber(              /* compute pixel index (screen door) */
815 >        int  x,
816 >        int  y,
817 >        int  xres,
818 >        int  yres
819 > )
820   {
821 <        x -= y;
822 <        while (x < 0)
823 <                x += xres;
824 <        return((((x>>2)*yres + y) << 2) + (x & 3));
821 >        unsigned        nbits = 0;
822 >        bitmask_t       coord[2];
823 >
824 >        if (xres < yres) xres = yres;
825 >        while (xres > 0) {
826 >                xres >>= 1;
827 >                ++nbits;
828 >        }
829 >        coord[0] = x; coord[1] = y;
830 >        return ((int)hilbert_c2i(2, nbits, coord));
831   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines