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.2 by greg, Thu Nov 21 10:10:46 1991 UTC vs.
Revision 2.17 by greg, Thu Oct 15 21:13:17 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 59 | Line 63 | int  ambounce = 0;                     /* ambient bounces */
63   char  *amblist[128];                    /* ambient include/exclude list */
64   int  ambincl = -1;                      /* include == 1, exclude == 0 */
65  
66 + #ifdef MSDOS
67 + int  ralrm = 60;                        /* seconds between reports */
68 + #else
69   int  ralrm = 0;                         /* seconds between reports */
70 + #endif
71  
72 < double  pctdone = 0.0;                  /* percentage done */
72 > double  pctdone = 0.0;                  /* percentage done */
73  
74   long  tlastrept = 0L;                   /* time at last report */
75  
# Line 70 | Line 78 | extern long  tstart;                   /* starting time */
78  
79   extern long  nrays;                     /* number of rays traced */
80  
81 < #define  MAXDIV         16              /* maximum sample size */
81 > #define  MAXDIV         16              /* maximum sample size */
82  
83 < #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
83 > #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
84  
85 < double  pixvalue();
85 > #define  RFTEMPLATE     "rfXXXXXX"
86 > #define  HFTEMPLATE     TEMPLATE
87  
88 + static char  *hfname = NULL;            /* header file name */
89 + static FILE  *hfp = NULL;               /* header file pointer */
90  
91 + static int  hres, vres;                 /* resolution for this frame */
92 +
93 + extern char  *mktemp();
94 +
95 + double  pixvalue();
96 +
97 +
98   quit(code)                      /* quit program */
99   int  code;
100   {
101 <        if (code || ralrm > 0)          /* report status */
101 >        if (code)                       /* report status */
102                  report();
103 <
103 >        if (hfname != NULL) {           /* delete header file */
104 >                if (hfp != NULL)
105 >                        fclose(hfp);
106 >                unlink(hfname);
107 >        }
108          exit(code);
109   }
110  
# Line 91 | Line 113 | int  code;
113   report()                /* report progress */
114   {
115          struct rusage  rubuf;
116 <        double  t;
116 >        double  t;
117  
118          getrusage(RUSAGE_SELF, &rubuf);
119          t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6;
# Line 108 | Line 130 | report()               /* report progress */
130   #else
131   report()                /* report progress */
132   {
111        signal(SIGALRM, report);
133          tlastrept = time((long *)0);
134          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n",
135                          nrays, pctdone, (tlastrept-tstart)/3600.0);
136          eputs(errmsg);
137 +        signal(SIGALRM, report);
138   }
139   #endif
140  
141  
142 + openheader()                    /* save standard output to header file */
143 + {
144 +        hfname = mktemp(HFTEMPLATE);
145 +        if (freopen(hfname, "w", stdout) == NULL) {
146 +                sprintf(errmsg, "cannot open header file \"%s\"", hfname);
147 +                error(SYSTEM, errmsg);
148 +        }
149 + }
150 +
151 +
152 + closeheader()                   /* done with header output */
153 + {
154 +        if (hfname == NULL)
155 +                return;
156 +        if (fflush(stdout) == EOF || (hfp = fopen(hfname, "r")) == NULL)
157 +                error(SYSTEM, "error reopening header file");
158 + #ifdef MSDOS
159 +        setmode(fileno(hfp), O_BINARY);
160 + #endif
161 + }
162 +
163 +
164 + dupheader()                     /* repeat header on standard output */
165 + {
166 +        register int  c;
167 +
168 +        if (fseek(hfp, 0L, 0) < 0)
169 +                error(SYSTEM, "seek error on header file");
170 +        while ((c = getc(hfp)) != EOF)
171 +                putchar(c);
172 + }
173 +
174 +
175 + rpict(seq, pout, zout, prvr)                    /* generate image(s) */
176 + int  seq;
177 + char  *pout, *zout, *prvr;
178 + /*
179 + * If seq is greater than zero, then we will render a sequence of
180 + * images based on view parameter strings read from the standard input.
181 + * If pout is NULL, then all images will be sent to the standard ouput.
182 + * If seq is greater than zero and prvr is an integer, then it is the
183 + * frame number at which rendering should begin.  Preceeding view parameter
184 + * strings will be skipped in the input.
185 + * If pout and prvr are the same, prvr is renamed to avoid overwriting.
186 + * Note that pout and zout should contain %d format specifications for
187 + * sequenced file naming.
188 + */
189 + {
190 +        extern char  *rindex(), *strncpy(), *strcat();
191 +        char  fbuf[128], fbuf2[128];
192 +        register char  *cp;
193 +        RESOLU  rs;
194 +        double  pa;
195 +                                        /* finished writing header */
196 +        closeheader();
197 +                                        /* check sampling */
198 +        if (psample < 1)
199 +                psample = 1;
200 +        else if (psample > MAXDIV) {
201 +                sprintf(errmsg, "pixel sampling reduced from %d to %d",
202 +                                psample, MAXDIV);
203 +                error(WARNING, errmsg);
204 +                psample = MAXDIV;
205 +        }
206 +                                        /* get starting frame */
207 +        if (seq <= 0)
208 +                seq = 0;
209 +        else if (prvr != NULL && isint(prvr)) {
210 +                register int  rn;               /* skip to specified view */
211 +                if ((rn = atoi(prvr)) < seq)
212 +                        error(USER, "recover frame less than start frame");
213 +                if (pout == NULL)
214 +                        error(USER, "missing output file specification");
215 +                for ( ; seq < rn; seq++)
216 +                        if (nextview(stdin) == EOF)
217 +                                error(USER, "unexpected EOF on view input");
218 +                prvr = fbuf;                    /* mark for renaming */
219 +        }
220 +        if (pout != NULL) {
221 +                sprintf(fbuf, pout, seq);
222 +                if (prvr != NULL && !strcmp(prvr, fbuf)) {      /* rename */
223 +                        fbuf2[0] = '\0';
224 +                        if ((cp = rindex(fbuf, '/')) != NULL)
225 +                                strncpy(fbuf2, fbuf, cp-fbuf+1);
226 +                        strcat(fbuf2, RFTEMPLATE);
227 +                        prvr = mktemp(fbuf2);
228 +                        if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
229 +                                sprintf(errmsg,
230 +                                        "cannot rename \"%s\" to \"%s\"",
231 +                                                fbuf, prvr);
232 +                                error(SYSTEM, errmsg);
233 +                        }
234 +                }
235 +        }
236 +                                        /* render sequence */
237 +        do {
238 +                if (seq && nextview(stdin) == EOF)
239 +                        break;
240 +                if (pout != NULL) {
241 +                        sprintf(fbuf, pout, seq);
242 +                        if (freopen(fbuf, "w", stdout) == NULL) {
243 +                                sprintf(errmsg,
244 +                                        "cannot open output file \"%s\"", fbuf);
245 +                                error(SYSTEM, errmsg);
246 +                        }
247 + #ifdef MSDOS
248 +                        setmode(fileno(stdout), O_BINARY);
249 + #endif
250 +                        dupheader();
251 +                }
252 +                hres = hresolu; vres = vresolu; pa = pixaspect;
253 +                if (prvr != NULL)
254 +                        if (viewfile(prvr, &ourview, &rs) <= 0
255 +                                        || rs.or != PIXSTANDARD) {
256 +                                sprintf(errmsg,
257 +                        "cannot recover view parameters from \"%s\"", prvr);
258 +                                error(WARNING, errmsg);
259 +                        } else {
260 +                                pa = 0.0;
261 +                                hres = scanlen(&rs);
262 +                                vres = numscans(&rs);
263 +                        }
264 +                if ((cp = setview(&ourview)) != NULL)
265 +                        error(USER, cp);
266 +                normaspect(viewaspect(&ourview), &pa, &hres, &vres);
267 +                if (seq) {
268 +                        if (ralrm > 0) {
269 +                                fprintf(stderr, "FRAME %d:", seq);
270 +                                fprintview(&ourview, stderr);
271 +                                putc('\n', stderr);
272 +                                fflush(stderr);
273 +                        }
274 +                        printf("FRAME=%d\n", seq);
275 +                }
276 +                fputs(VIEWSTR, stdout);
277 +                fprintview(&ourview, stdout);
278 +                putchar('\n');
279 +                if (pa < .99 || pa > 1.01)
280 +                        fputaspect(pa, stdout);
281 +                fputformat(COLRFMT, stdout);
282 +                putchar('\n');
283 +                if (zout != NULL)
284 +                        sprintf(cp=fbuf, zout, seq);
285 +                else
286 +                        cp = NULL;
287 +                render(cp, prvr);
288 +                prvr = NULL;
289 +        } while (seq++);
290 + }
291 +
292 +
293 + nextview(fp)                            /* get next view from fp */
294 + FILE  *fp;
295 + {
296 +        char  linebuf[256];
297 +
298 +        while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
299 +                if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
300 +                        return(0);
301 +        return(EOF);
302 + }      
303 +
304 +
305   render(zfile, oldfile)                          /* render the scene */
306   char  *zfile, *oldfile;
307   {
# Line 131 | Line 316 | char  *zfile, *oldfile;
316          COLOR  *colptr;
317          float  *zptr;
318          register int  i;
134                                        /* check sampling */
135        if (psample < 1)
136                psample = 1;
137        else if (psample > MAXDIV) {
138                sprintf(errmsg, "pixel sampling reduced from %d to %d",
139                                psample, MAXDIV);
140                error(WARNING, errmsg);
141                psample = MAXDIV;
142        }
319                                          /* allocate scanlines */
320          for (i = 0; i <= psample; i++) {
321 <                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
321 >                scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
322                  if (scanbar[i] == NULL)
323                          goto memerr;
324          }
325          hstep = (psample*140+49)/99;            /* quincunx sampling */
326          ystep = (psample*99+70)/140;
327          if (hstep > 2) {
328 <                i = hresolu/hstep + 2;
328 >                i = hres/hstep + 2;
329                  if ((sampdens = malloc(i)) == NULL)
330                          goto memerr;
331                  while (i--)
# Line 162 | Line 338 | char  *zfile, *oldfile;
338                          sprintf(errmsg, "cannot open z file \"%s\"", zfile);
339                          error(SYSTEM, errmsg);
340                  }
341 + #ifdef MSDOS
342 +                setmode(zfd, O_BINARY);
343 + #endif
344                  for (i = 0; i <= psample; i++) {
345 <                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
345 >                        zbar[i] = (float *)malloc(hres*sizeof(float));
346                          if (zbar[i] == NULL)
347                                  goto memerr;
348                  }
# Line 173 | Line 352 | char  *zfile, *oldfile;
352                          zbar[i] = NULL;
353          }
354                                          /* write out boundaries */
355 <        fprtresolu(hresolu, vresolu, stdout);
355 >        fprtresolu(hres, vres, stdout);
356                                          /* recover file and compute first */
357          i = salvage(oldfile);
358          if (zfd != -1 && i > 0 &&
359 <                        lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
359 >                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
360                  error(SYSTEM, "z file seek error in render");
361 <        pctdone = 100.0*i/vresolu;
361 >        pctdone = 100.0*i/vres;
362          if (ralrm > 0)                  /* report init stats */
363                  report();
364 < #ifndef  BSD
364 > #ifndef  BSD
365          else
366   #endif
367          signal(SIGALRM, report);
368 <        ypos = vresolu-1 - i;
369 <        fillscanline(scanbar[0], zbar[0], sampdens, hresolu, ypos, hstep);
368 >        ypos = vres-1 - i;
369 >        fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
370                                                  /* compute scanlines */
371          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
372                                                          /* bottom adjust? */
# Line 203 | Line 382 | char  *zfile, *oldfile;
382                  zbar[0] = zptr;
383                                                          /* fill base line */
384                  fillscanline(scanbar[0], zbar[0], sampdens,
385 <                                hresolu, ypos, hstep);
385 >                                hres, ypos, hstep);
386                                                          /* fill bar */
387 <                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
387 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
388                                                          /* write it out */
389 < #ifndef  BSD
389 > #ifndef  BSD
390                  signal(SIGALRM, SIG_IGN);       /* don't interrupt writes */
391   #endif
392                  for (i = ystep; i > 0; i--) {
393                          if (zfd != -1 && write(zfd, (char *)zbar[i],
394 <                                        hresolu*sizeof(float))
395 <                                        < hresolu*sizeof(float))
394 >                                        hres*sizeof(float))
395 >                                        < hres*sizeof(float))
396                                  goto writerr;
397 <                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
397 >                        if (fwritescan(scanbar[i], hres, stdout) < 0)
398                                  goto writerr;
399                  }
400                  if (fflush(stdout) == EOF)
401                          goto writerr;
402                                                          /* record progress */
403 <                pctdone = 100.0*(vresolu-1-ypos)/vresolu;
403 >                pctdone = 100.0*(vres-1-ypos)/vres;
404                  if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
405                          report();
406 < #ifndef  BSD
406 > #ifndef  BSD
407                  else
408                          signal(SIGALRM, report);
409   #endif
410          }
411                                                  /* clean up */
412 +        signal(SIGALRM, SIG_IGN);
413          if (zfd != -1) {
414 <                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
415 <                                < hresolu*sizeof(float))
414 >                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
415 >                                < hres*sizeof(float))
416                          goto writerr;
417                  if (close(zfd) == -1)
418                          goto writerr;
419                  for (i = 0; i <= psample; i++)
420                          free((char *)zbar[i]);
421          }
422 <        fwritescan(scanbar[0], hresolu, stdout);
422 >        fwritescan(scanbar[0], hres, stdout);
423          if (fflush(stdout) == EOF)
424                  goto writerr;
425          for (i = 0; i <= psample; i++)
# Line 247 | Line 427 | char  *zfile, *oldfile;
427          if (sampdens != NULL)
428                  free(sampdens);
429          pctdone = 100.0;
430 +        if (ralrm > 0)
431 +                report();
432          return;
433   writerr:
434          error(SYSTEM, "write error in render");
# Line 256 | Line 438 | memerr:
438  
439  
440   fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
441 < register COLOR  *scanline;
442 < register float  *zline;
441 > register COLOR  *scanline;
442 > register float  *zline;
443   register char  *sd;
444   int  xres, y, xstep;
445   {
446          static int  nc = 0;             /* number of calls */
447          int  bl = xstep, b = xstep;
448 <        double  z;
448 >        double  z;
449          register int  i;
450          
451          z = pixvalue(scanline[0], 0, y);
# Line 277 | Line 459 | int  xres, y, xstep;
459                  z = pixvalue(scanline[i], i, y);
460                  if (zline) zline[i] = z;
461                  if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
462 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
463 <                                i-xstep, y, xstep, 0, b/2);
462 >                if (i <= xstep)
463 >                        b = fillsample(scanline, zline, 0, y, i, 0, b/2);
464 >                else
465 >                        b = fillsample(scanline+i-xstep,
466 >                                        zline ? zline+i-xstep : NULL,
467 >                                        i-xstep, y, xstep, 0, b/2);
468                  if (sd) *sd++ = nc & 1 ? bl : b;
469                  bl = b;
470          }
# Line 287 | Line 473 | int  xres, y, xstep;
473  
474  
475   fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
476 < register COLOR  *scanbar[];
477 < register float  *zbar[];
476 > register COLOR  *scanbar[];
477 > register float  *zbar[];
478   int  xres, y, ysize;
479   {
480          COLOR  vline[MAXDIV+1];
# Line 318 | Line 504 | int  xres, y, ysize;
504  
505  
506   int
507 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
508 < register COLOR  *colline;
509 < register float  *zline;
507 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
508 > register COLOR  *colline;
509 > register float  *zline;
510   int  x, y;
511   int  xlen, ylen;
512   int  b;
513   {
514 <        extern double  fabs();
515 <        double  ratio;
330 <        double  z;
514 >        double  ratio;
515 >        double  z;
516          COLOR  ctmp;
517          int  ncut;
518          register int  len;
# Line 380 | Line 565 | int  x, y;                     /* pixel position */
565          static RAY  thisray;
566  
567          if (viewray(thisray.rorg, thisray.rdir, &ourview,
568 <                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
568 >                        (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
569                  setcolor(col, 0.0, 0.0, 0.0);
570                  return(0.0);
571          }
572  
573          rayorigin(&thisray, NULL, PRIMARY, 1.0);
574  
575 <        samplendx = pixnumber(x,y,hresolu,vresolu);     /* set pixel index */
575 >        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
576  
577          rayvalue(&thisray);                     /* trace ray */
578  
# Line 413 | Line 598 | char  *oldfile;
598                  error(WARNING, errmsg);
599                  return(0);
600          }
601 + #ifdef MSDOS
602 +        setmode(fileno(fp), O_BINARY);
603 + #endif
604                                  /* discard header */
605          getheader(fp, NULL);
606                                  /* get picture size */
# Line 423 | Line 611 | char  *oldfile;
611                  return(0);
612          }
613  
614 <        if (x != hresolu || y != vresolu) {
614 >        if (x != hres || y != vres) {
615                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
616                                  oldfile);
617                  error(USER, errmsg);
618          }
619  
620 <        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
620 >        scanline = (COLR *)malloc(hres*sizeof(COLR));
621          if (scanline == NULL)
622                  error(SYSTEM, "out of memory in salvage");
623 <        for (y = 0; y < vresolu; y++) {
624 <                if (freadcolrs(scanline, hresolu, fp) < 0)
623 >        for (y = 0; y < vres; y++) {
624 >                if (freadcolrs(scanline, hres, fp) < 0)
625                          break;
626 <                if (fwritecolrs(scanline, hresolu, stdout) < 0)
626 >                if (fwritecolrs(scanline, hres, stdout) < 0)
627                          goto writerr;
628          }
629          if (fflush(stdout) == EOF)
# Line 445 | Line 633 | char  *oldfile;
633          unlink(oldfile);
634          return(y);
635   writerr:
636 <        error(SYSTEM, "write error in salvage");
636 >        sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
637 >        error(SYSTEM, errmsg);
638   }
639  
640  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines