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.1 by greg, Tue Nov 12 17:08:26 1991 UTC vs.
Revision 2.14 by greg, Mon Sep 21 12:07:52 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18   #endif
19  
20   #include  <signal.h>
21 #include  <fcntl.h>
21  
22   #include  "view.h"
23  
# Line 26 | Line 25 | static char SCCSid[] = "$SunId$ LBL";
25  
26   #include  "random.h"
27  
28 + #include  "paths.h"
29 +
30   int  dimlist[MAXDIM];                   /* sampling dimensions */
31   int  ndims = 0;                         /* number of sampling dimensions */
32   int  samplendx;                         /* sample index number */
# Line 33 | Line 34 | int  samplendx;                                /* sample index number */
34   VIEW  ourview = STDVIEW;                /* view parameters */
35   int  hresolu = 512;                     /* horizontal resolution */
36   int  vresolu = 512;                     /* vertical resolution */
37 < double  pixaspect = 1.0;                /* pixel aspect ratio */
37 > double  pixaspect = 1.0;                /* pixel aspect ratio */
38  
39   int  psample = 4;                       /* pixel sample size */
40 < double  maxdiff = .05;                  /* max. difference for interpolation */
41 < double  dstrpix = 0.67;                 /* square pixel distribution */
40 > double  maxdiff = .05;                  /* max. difference for interpolation */
41 > double  dstrpix = 0.67;                 /* square pixel distribution */
42  
43 < double  dstrsrc = 0.0;                  /* square source distribution */
44 < double  shadthresh = .05;               /* shadow threshold */
45 < double  shadcert = .5;                  /* shadow certainty */
43 > double  dstrsrc = 0.0;                  /* square source distribution */
44 > double  shadthresh = .05;               /* shadow threshold */
45 > double  shadcert = .5;                  /* shadow certainty */
46   int  directrelay = 1;                   /* number of source relays */
47   int  vspretest = 512;                   /* virtual source pretest density */
48   int  directinvis = 0;                   /* sources invisible? */
49 < double  srcsizerat = .25;               /* maximum ratio source size/dist. */
49 > double  srcsizerat = .25;               /* maximum ratio source size/dist. */
50  
51 + double  specthresh = .15;               /* specular sampling threshold */
52 + double  specjitter = 1.;                /* specular sampling jitter */
53 +
54   int  maxdepth = 6;                      /* maximum recursion depth */
55 < double  minweight = 5e-3;               /* minimum ray weight */
55 > double  minweight = 5e-3;               /* minimum ray weight */
56  
57   COLOR  ambval = BLKCOLOR;               /* ambient value */
58 < double  ambacc = 0.2;                   /* ambient accuracy */
58 > double  ambacc = 0.2;                   /* ambient accuracy */
59   int  ambres = 32;                       /* ambient resolution */
60   int  ambdiv = 128;                      /* ambient divisions */
61   int  ambssamp = 0;                      /* ambient super-samples */
# Line 61 | Line 65 | int  ambincl = -1;                     /* include == 1, exclude == 0 */
65  
66   int  ralrm = 0;                         /* seconds between reports */
67  
68 < double  pctdone = 0.0;                  /* percentage done */
68 > double  pctdone = 0.0;                  /* percentage done */
69  
70   long  tlastrept = 0L;                   /* time at last report */
71  
# Line 70 | Line 74 | extern long  tstart;                   /* starting time */
74  
75   extern long  nrays;                     /* number of rays traced */
76  
77 < #define  MAXDIV         15              /* maximum sample size */
77 > #define  MAXDIV         16              /* maximum sample size */
78  
79 < #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
79 > #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
80  
81 < double  pixvalue();
81 > #define  RFTEMPLATE     "rfXXXXXX"
82 > #define  HFTEMPLATE     TEMPLATE
83  
84 + static char  *hfname = NULL;            /* header file name */
85 + static FILE  *hfp = NULL;               /* header file pointer */
86  
87 + static int  hres, vres;                 /* resolution for this frame */
88 +
89 + extern char  *mktemp();
90 +
91 + double  pixvalue();
92 +
93 +
94   quit(code)                      /* quit program */
95   int  code;
96   {
97 <        if (code || ralrm > 0)          /* report status */
97 >        if (code)                       /* report status */
98                  report();
99 <
99 >        if (hfname != NULL) {           /* delete header file */
100 >                if (hfp != NULL)
101 >                        fclose(hfp);
102 >                unlink(hfname);
103 >        }
104          exit(code);
105   }
106  
# Line 91 | Line 109 | int  code;
109   report()                /* report progress */
110   {
111          struct rusage  rubuf;
112 <        double  t;
112 >        double  t;
113  
114          getrusage(RUSAGE_SELF, &rubuf);
115          t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
# Line 108 | Line 126 | report()               /* report progress */
126   #else
127   report()                /* report progress */
128   {
111        signal(SIGALRM, report);
129          tlastrept = time((long *)0);
130          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
131                          nrays, pctdone, (tlastrept-tstart)/3600.0);
132          eputs(errmsg);
133 +        signal(SIGALRM, report);
134   }
135   #endif
136  
137  
138 + openheader()                    /* save standard output to header file */
139 + {
140 +        hfname = mktemp(HFTEMPLATE);
141 +        if (freopen(hfname, "w", stdout) == NULL) {
142 +                sprintf(errmsg, "cannot open header file \"%s\"", hfname);
143 +                error(SYSTEM, errmsg);
144 +        }
145 + }
146 +
147 +
148 + closeheader()                   /* done with header output */
149 + {
150 +        if (hfname == NULL)
151 +                return;
152 +        if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL)
153 +                error(SYSTEM, "error reopening header file");
154 + }
155 +
156 +
157 + dupheader()                     /* repeat header on standard output */
158 + {
159 +        register int  c;
160 +
161 +        if (fseek(hfp, 0L, 0) < 0)
162 +                error(SYSTEM, "seek error on header file");
163 +        while ((c = getc(hfp)) != EOF)
164 +                putchar(c);
165 + }
166 +
167 +
168 + rpict(seq, pout, zout, prvr)                    /* generate image(s) */
169 + int  seq;
170 + char  *pout, *zout, *prvr;
171 + /*
172 + * If seq is greater than zero, then we will render a sequence of
173 + * images based on view parameter strings read from the standard input.
174 + * If pout is NULL, then all images will be sent to the standard ouput.
175 + * If seq is greater than zero and prvr is an integer, then it is the
176 + * frame number at which rendering should begin.  Preceeding view parameter
177 + * strings will be skipped in the input.
178 + * If pout and prvr are the same, prvr is renamed to avoid overwriting.
179 + * Note that pout and zout should contain %d format specifications for
180 + * sequenced file naming.
181 + */
182 + {
183 +        extern char  *rindex(), *strncpy(), *strcat();
184 +        char  fbuf[128], fbuf2[128];
185 +        register char  *cp;
186 +        RESOLU  rs;
187 +        double  pa;
188 +                                        /* finished writing header */
189 +        closeheader();
190 +                                        /* check sampling */
191 +        if (psample < 1)
192 +                psample = 1;
193 +        else if (psample > MAXDIV) {
194 +                sprintf(errmsg, "pixel sampling reduced from %d to %d",
195 +                                psample, MAXDIV);
196 +                error(WARNING, errmsg);
197 +                psample = MAXDIV;
198 +        }
199 +                                        /* get starting frame */
200 +        if (seq <= 0)
201 +                seq = 0;
202 +        else if (prvr != NULL && isint(prvr)) {
203 +                register int  rn;               /* skip to specified view */
204 +                if ((rn = atoi(prvr)) < seq)
205 +                        error(USER, "recover frame less than start frame");
206 +                if (pout == NULL)
207 +                        error(USER, "missing output file specification");
208 +                for ( ; seq < rn; seq++)
209 +                        if (nextview(stdin) == EOF)
210 +                                error(USER, "unexpected EOF on view input");
211 +                prvr = fbuf;                    /* mark for renaming */
212 +        }
213 +        if (pout != NULL) {
214 +                sprintf(fbuf, pout, seq);
215 +                if (prvr != NULL && !strcmp(prvr, fbuf)) {      /* rename */
216 +                        fbuf2[0] = '\0';
217 +                        if ((cp = rindex(fbuf, '/')) != NULL)
218 +                                strncpy(fbuf2, fbuf, cp-fbuf+1);
219 +                        strcat(fbuf2, RFTEMPLATE);
220 +                        prvr = mktemp(fbuf2);
221 +                        if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
222 +                                sprintf(errmsg,
223 +                                        "cannot rename \"%s\" to \"%s\"",
224 +                                                fbuf, prvr);
225 +                                error(SYSTEM, errmsg);
226 +                        }
227 +                }
228 +        }
229 +                                        /* render sequence */
230 +        do {
231 +                if (seq && nextview(stdin) == EOF)
232 +                        break;
233 +                if (pout != NULL) {
234 +                        sprintf(fbuf, pout, seq);
235 +                        if (freopen(fbuf, "w", stdout) == NULL) {
236 +                                sprintf(errmsg,
237 +                                        "cannot open output file \"%s\"", fbuf);
238 +                                error(SYSTEM, errmsg);
239 +                        }
240 +                        dupheader();
241 +                }
242 +                hres = hresolu; vres = vresolu; pa = pixaspect;
243 +                if (prvr != NULL)
244 +                        if (viewfile(prvr, &ourview, &rs) <= 0
245 +                                        || rs.or != PIXSTANDARD) {
246 +                                sprintf(errmsg,
247 +                        "cannot recover view parameters from \"%s\"", prvr);
248 +                                error(WARNING, errmsg);
249 +                        } else {
250 +                                pa = 0.0;
251 +                                hres = scanlen(&rs);
252 +                                vres = numscans(&rs);
253 +                        }
254 +                if ((cp = setview(&ourview)) != NULL)
255 +                        error(USER, cp);
256 +                normaspect(viewaspect(&ourview), &pa, &hres, &vres);
257 +                if (seq) {
258 +                        if (ralrm > 0) {
259 +                                fprintf(stderr, "FRAME %d:", seq);
260 +                                fprintview(&ourview, stderr);
261 +                                putc('\n', stderr);
262 +                                fflush(stderr);
263 +                        }
264 +                        printf("FRAME=%d\n", seq);
265 +                }
266 +                fputs(VIEWSTR, stdout);
267 +                fprintview(&ourview, stdout);
268 +                putchar('\n');
269 +                if (pa < .99 || pa > 1.01)
270 +                        fputaspect(pa, stdout);
271 +                fputformat(COLRFMT, stdout);
272 +                putchar('\n');
273 +                if (zout != NULL)
274 +                        sprintf(cp=fbuf, zout, seq);
275 +                else
276 +                        cp = NULL;
277 +                render(cp, prvr);
278 +                prvr = NULL;
279 +        } while (seq++);
280 + }
281 +
282 +
283 + nextview(fp)                            /* get next view from fp */
284 + FILE  *fp;
285 + {
286 +        char  linebuf[256];
287 +
288 +        while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
289 +                if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
290 +                        return(0);
291 +        return(EOF);
292 + }      
293 +
294 +
295   render(zfile, oldfile)                          /* render the scene */
296   char  *zfile, *oldfile;
297   {
298          extern long  lseek();
299          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
300          float  *zbar[MAXDIV+1];         /* z values */
301 +        char  *sampdens;                /* previous sample density */
302          int  ypos;                      /* current scanline */
303          int  ystep;                     /* current y step size */
304          int  hstep;                     /* h step size */
# Line 130 | Line 306 | char  *zfile, *oldfile;
306          COLOR  *colptr;
307          float  *zptr;
308          register int  i;
133                                        /* check sampling */
134        if (psample < 1)
135                psample = 1;
136        else if (psample > MAXDIV) {
137                sprintf(errmsg, "pixel sampling reduced from %d to %d",
138                                psample, MAXDIV);
139                error(WARNING, errmsg);
140                psample = MAXDIV;
141        }
309                                          /* allocate scanlines */
310          for (i = 0; i <= psample; i++) {
311 <                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
311 >                scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
312                  if (scanbar[i] == NULL)
313                          goto memerr;
314          }
315 +        hstep = (psample*140+49)/99;            /* quincunx sampling */
316 +        ystep = (psample*99+70)/140;
317 +        if (hstep > 2) {
318 +                i = hres/hstep + 2;
319 +                if ((sampdens = malloc(i)) == NULL)
320 +                        goto memerr;
321 +                while (i--)
322 +                        sampdens[i] = hstep;
323 +        } else
324 +                sampdens = NULL;
325                                          /* open z file */
326          if (zfile != NULL) {
327                  if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
# Line 152 | Line 329 | char  *zfile, *oldfile;
329                          error(SYSTEM, errmsg);
330                  }
331                  for (i = 0; i <= psample; i++) {
332 <                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
332 >                        zbar[i] = (float *)malloc(hres*sizeof(float));
333                          if (zbar[i] == NULL)
334                                  goto memerr;
335                  }
# Line 162 | Line 339 | char  *zfile, *oldfile;
339                          zbar[i] = NULL;
340          }
341                                          /* write out boundaries */
342 <        fprtresolu(hresolu, vresolu, stdout);
342 >        fprtresolu(hres, vres, stdout);
343                                          /* recover file and compute first */
344          i = salvage(oldfile);
345          if (zfd != -1 && i > 0 &&
346 <                        lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
346 >                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
347                  error(SYSTEM, "z file seek error in render");
348 <        pctdone = 100.0*i/vresolu;
348 >        pctdone = 100.0*i/vres;
349          if (ralrm > 0)                  /* report init stats */
350                  report();
351 < #ifndef  BSD
351 > #ifndef  BSD
352          else
353   #endif
354          signal(SIGALRM, report);
355 <        ypos = vresolu-1 - i;
356 <        hstep = (psample*140+49)/99;            /* quincunx sampling */
180 <        ystep = (psample*99+70)/140;
181 <        fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep);
355 >        ypos = vres-1 - i;
356 >        fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
357                                                  /* compute scanlines */
358          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
359                                                          /* bottom adjust? */
# Line 193 | Line 368 | char  *zfile, *oldfile;
368                  zbar[ystep] = zbar[0];
369                  zbar[0] = zptr;
370                                                          /* fill base line */
371 <                fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep);
371 >                fillscanline(scanbar[0], zbar[0], sampdens,
372 >                                hres, ypos, hstep);
373                                                          /* fill bar */
374 <                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
374 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
375                                                          /* write it out */
376 < #ifndef  BSD
376 > #ifndef  BSD
377                  signal(SIGALRM, SIG_IGN);       /* don't interrupt writes */
378   #endif
379                  for (i = ystep; i > 0; i--) {
380                          if (zfd != -1 && write(zfd, (char *)zbar[i],
381 <                                        hresolu*sizeof(float))
382 <                                        < hresolu*sizeof(float))
381 >                                        hres*sizeof(float))
382 >                                        < hres*sizeof(float))
383                                  goto writerr;
384 <                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
384 >                        if (fwritescan(scanbar[i], hres, stdout) < 0)
385                                  goto writerr;
386                  }
387                  if (fflush(stdout) == EOF)
388                          goto writerr;
389                                                          /* record progress */
390 <                pctdone = 100.0*(vresolu-1-ypos)/vresolu;
390 >                pctdone = 100.0*(vres-1-ypos)/vres;
391                  if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
392                          report();
393 < #ifndef  BSD
393 > #ifndef  BSD
394                  else
395                          signal(SIGALRM, report);
396   #endif
397          }
398                                                  /* clean up */
399 +        signal(SIGALRM, SIG_IGN);
400          if (zfd != -1) {
401 <                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
402 <                                < hresolu*sizeof(float))
401 >                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
402 >                                < hres*sizeof(float))
403                          goto writerr;
404                  if (close(zfd) == -1)
405                          goto writerr;
406                  for (i = 0; i <= psample; i++)
407                          free((char *)zbar[i]);
408          }
409 <        fwritescan(scanbar[0], hresolu, stdout);
409 >        fwritescan(scanbar[0], hres, stdout);
410          if (fflush(stdout) == EOF)
411                  goto writerr;
412          for (i = 0; i <= psample; i++)
413                  free((char *)scanbar[i]);
414 +        if (sampdens != NULL)
415 +                free(sampdens);
416          pctdone = 100.0;
417 +        if (ralrm > 0)
418 +                report();
419          return;
420   writerr:
421          error(SYSTEM, "write error in render");
# Line 243 | Line 424 | memerr:
424   }
425  
426  
427 < fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
428 < register COLOR  *scanline;
429 < register float  *zline;
427 > fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
428 > register COLOR  *scanline;
429 > register float  *zline;
430 > register char  *sd;
431   int  xres, y, xstep;
432   {
433          static int  nc = 0;             /* number of calls */
434 <        int  b = xstep;
435 <        double  z;
434 >        int  bl = xstep, b = xstep;
435 >        double  z;
436          register int  i;
437          
438          z = pixvalue(scanline[0], 0, y);
439          if (zline) zline[0] = z;
440                                  /* zig-zag start for quincunx pattern */
441 <        for (i = nc++ & 1 ? xstep/2 : xstep; i < xres-1+xstep; i += xstep) {
441 >        for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
442                  if (i >= xres) {
443                          xstep += xres-1-i;
444                          i = xres-1;
445                  }
446                  z = pixvalue(scanline[i], i, y);
447                  if (zline) zline[i] = z;
448 <                
449 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
450 <                                i-xstep, y, xstep, 0, b/2);
448 >                if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
449 >                if (i <= xstep)
450 >                        b = fillsample(scanline, zline, 0, y, i, 0, b/2);
451 >                else
452 >                        b = fillsample(scanline+i-xstep,
453 >                                        zline ? zline+i-xstep : NULL,
454 >                                        i-xstep, y, xstep, 0, b/2);
455 >                if (sd) *sd++ = nc & 1 ? bl : b;
456 >                bl = b;
457          }
458 +        if (sd && nc & 1) *sd = bl;
459   }
460  
461  
462   fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
463 < register COLOR  *scanbar[];
464 < register float  *zbar[];
463 > register COLOR  *scanbar[];
464 > register float  *zbar[];
465   int  xres, y, ysize;
466   {
467          COLOR  vline[MAXDIV+1];
# Line 302 | Line 491 | int  xres, y, ysize;
491  
492  
493   int
494 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
495 < register COLOR  *colline;
496 < register float  *zline;
494 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
495 > register COLOR  *colline;
496 > register float  *zline;
497   int  x, y;
498   int  xlen, ylen;
499   int  b;
500   {
501          extern double  fabs();
502 <        double  ratio;
503 <        double  z;
502 >        double  ratio;
503 >        double  z;
504          COLOR  ctmp;
505          int  ncut;
506          register int  len;
# Line 364 | Line 553 | int  x, y;                     /* pixel position */
553          static RAY  thisray;
554  
555          if (viewray(thisray.rorg, thisray.rdir, &ourview,
556 <                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
556 >                        (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
557                  setcolor(col, 0.0, 0.0, 0.0);
558                  return(0.0);
559          }
560  
561          rayorigin(&thisray, NULL, PRIMARY, 1.0);
562  
563 <        samplendx = pixnumber(x,y,hresolu,vresolu);     /* set pixel index */
563 >        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
564  
565          rayvalue(&thisray);                     /* trace ray */
566  
# Line 407 | Line 596 | char  *oldfile;
596                  return(0);
597          }
598  
599 <        if (x != hresolu || y != vresolu) {
599 >        if (x != hres || y != vres) {
600                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
601                                  oldfile);
602                  error(USER, errmsg);
603          }
604  
605 <        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
605 >        scanline = (COLR *)malloc(hres*sizeof(COLR));
606          if (scanline == NULL)
607                  error(SYSTEM, "out of memory in salvage");
608 <        for (y = 0; y < vresolu; y++) {
609 <                if (freadcolrs(scanline, hresolu, fp) < 0)
608 >        for (y = 0; y < vres; y++) {
609 >                if (freadcolrs(scanline, hres, fp) < 0)
610                          break;
611 <                if (fwritecolrs(scanline, hresolu, stdout) < 0)
611 >                if (fwritecolrs(scanline, hres, stdout) < 0)
612                          goto writerr;
613          }
614          if (fflush(stdout) == EOF)
# Line 429 | Line 618 | char  *oldfile;
618          unlink(oldfile);
619          return(y);
620   writerr:
621 <        error(SYSTEM, "write error in salvage");
621 >        sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
622 >        error(SYSTEM, errmsg);
623   }
624  
625  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines