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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines