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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines