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 1.25 by greg, Tue May 21 17:41:35 1991 UTC vs.
Revision 2.16 by greg, Tue Oct 6 12:30:07 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 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   #ifdef BSD
16   #include  <sys/time.h>
17   #include  <sys/resource.h>
18 #else
19 #include  <signal.h>
18   #endif
21 #include  <fcntl.h>
19  
20 + #include  <signal.h>
21 +
22   #include  "view.h"
23  
24 + #include  "resolu.h"
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 31 | 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 */
39 < int  psuper = 2;                        /* pixel super-sampling rate */
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. */
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 54 | 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 65 | Line 78 | extern long  tstart;                   /* starting time */
78  
79   extern long  nrays;                     /* number of rays traced */
80  
81 < #define  MAXDIV         32              /* 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 86 | 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 103 | Line 130 | report()               /* report progress */
130   #else
131   report()                /* report progress */
132   {
106        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 + }
159 +
160 +
161 + dupheader()                     /* repeat header on standard output */
162 + {
163 +        register int  c;
164 +
165 +        if (fseek(hfp, 0L, 0) < 0)
166 +                error(SYSTEM, "seek error on header file");
167 +        while ((c = getc(hfp)) != EOF)
168 +                putchar(c);
169 + }
170 +
171 +
172 + rpict(seq, pout, zout, prvr)                    /* generate image(s) */
173 + int  seq;
174 + char  *pout, *zout, *prvr;
175 + /*
176 + * If seq is greater than zero, then we will render a sequence of
177 + * images based on view parameter strings read from the standard input.
178 + * If pout is NULL, then all images will be sent to the standard ouput.
179 + * If seq is greater than zero and prvr is an integer, then it is the
180 + * frame number at which rendering should begin.  Preceeding view parameter
181 + * strings will be skipped in the input.
182 + * If pout and prvr are the same, prvr is renamed to avoid overwriting.
183 + * Note that pout and zout should contain %d format specifications for
184 + * sequenced file naming.
185 + */
186 + {
187 +        extern char  *rindex(), *strncpy(), *strcat();
188 +        char  fbuf[128], fbuf2[128];
189 +        register char  *cp;
190 +        RESOLU  rs;
191 +        double  pa;
192 +                                        /* finished writing header */
193 +        closeheader();
194 +                                        /* check sampling */
195 +        if (psample < 1)
196 +                psample = 1;
197 +        else if (psample > MAXDIV) {
198 +                sprintf(errmsg, "pixel sampling reduced from %d to %d",
199 +                                psample, MAXDIV);
200 +                error(WARNING, errmsg);
201 +                psample = MAXDIV;
202 +        }
203 +                                        /* get starting frame */
204 +        if (seq <= 0)
205 +                seq = 0;
206 +        else if (prvr != NULL && isint(prvr)) {
207 +                register int  rn;               /* skip to specified view */
208 +                if ((rn = atoi(prvr)) < seq)
209 +                        error(USER, "recover frame less than start frame");
210 +                if (pout == NULL)
211 +                        error(USER, "missing output file specification");
212 +                for ( ; seq < rn; seq++)
213 +                        if (nextview(stdin) == EOF)
214 +                                error(USER, "unexpected EOF on view input");
215 +                prvr = fbuf;                    /* mark for renaming */
216 +        }
217 +        if (pout != NULL) {
218 +                sprintf(fbuf, pout, seq);
219 +                if (prvr != NULL && !strcmp(prvr, fbuf)) {      /* rename */
220 +                        fbuf2[0] = '\0';
221 +                        if ((cp = rindex(fbuf, '/')) != NULL)
222 +                                strncpy(fbuf2, fbuf, cp-fbuf+1);
223 +                        strcat(fbuf2, RFTEMPLATE);
224 +                        prvr = mktemp(fbuf2);
225 +                        if (rename(fbuf, prvr) < 0 && errno != ENOENT) {
226 +                                sprintf(errmsg,
227 +                                        "cannot rename \"%s\" to \"%s\"",
228 +                                                fbuf, prvr);
229 +                                error(SYSTEM, errmsg);
230 +                        }
231 +                }
232 +        }
233 +                                        /* render sequence */
234 +        do {
235 +                if (seq && nextview(stdin) == EOF)
236 +                        break;
237 +                if (pout != NULL) {
238 +                        sprintf(fbuf, pout, seq);
239 +                        if (freopen(fbuf, "w", stdout) == NULL) {
240 +                                sprintf(errmsg,
241 +                                        "cannot open output file \"%s\"", fbuf);
242 +                                error(SYSTEM, errmsg);
243 +                        }
244 +                        dupheader();
245 +                }
246 +                hres = hresolu; vres = vresolu; pa = pixaspect;
247 +                if (prvr != NULL)
248 +                        if (viewfile(prvr, &ourview, &rs) <= 0
249 +                                        || rs.or != PIXSTANDARD) {
250 +                                sprintf(errmsg,
251 +                        "cannot recover view parameters from \"%s\"", prvr);
252 +                                error(WARNING, errmsg);
253 +                        } else {
254 +                                pa = 0.0;
255 +                                hres = scanlen(&rs);
256 +                                vres = numscans(&rs);
257 +                        }
258 +                if ((cp = setview(&ourview)) != NULL)
259 +                        error(USER, cp);
260 +                normaspect(viewaspect(&ourview), &pa, &hres, &vres);
261 +                if (seq) {
262 +                        if (ralrm > 0) {
263 +                                fprintf(stderr, "FRAME %d:", seq);
264 +                                fprintview(&ourview, stderr);
265 +                                putc('\n', stderr);
266 +                                fflush(stderr);
267 +                        }
268 +                        printf("FRAME=%d\n", seq);
269 +                }
270 +                fputs(VIEWSTR, stdout);
271 +                fprintview(&ourview, stdout);
272 +                putchar('\n');
273 +                if (pa < .99 || pa > 1.01)
274 +                        fputaspect(pa, stdout);
275 +                fputformat(COLRFMT, stdout);
276 +                putchar('\n');
277 +                if (zout != NULL)
278 +                        sprintf(cp=fbuf, zout, seq);
279 +                else
280 +                        cp = NULL;
281 +                render(cp, prvr);
282 +                prvr = NULL;
283 +        } while (seq++);
284 + }
285 +
286 +
287 + nextview(fp)                            /* get next view from fp */
288 + FILE  *fp;
289 + {
290 +        char  linebuf[256];
291 +
292 +        while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
293 +                if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
294 +                        return(0);
295 +        return(EOF);
296 + }      
297 +
298 +
299   render(zfile, oldfile)                          /* render the scene */
300   char  *zfile, *oldfile;
301   {
302          extern long  lseek();
303          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
304          float  *zbar[MAXDIV+1];         /* z values */
305 +        char  *sampdens;                /* previous sample density */
306          int  ypos;                      /* current scanline */
307          int  ystep;                     /* current y step size */
308 +        int  hstep;                     /* h step size */
309          int  zfd;
310          COLOR  *colptr;
311          float  *zptr;
312          register int  i;
127                                        /* check sampling */
128        if (psample < 1)
129                psample = 1;
130        else if (psample > MAXDIV)
131                psample = MAXDIV;
313                                          /* allocate scanlines */
314          for (i = 0; i <= psample; i++) {
315 <                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
315 >                scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
316                  if (scanbar[i] == NULL)
317                          goto memerr;
318          }
319 +        hstep = (psample*140+49)/99;            /* quincunx sampling */
320 +        ystep = (psample*99+70)/140;
321 +        if (hstep > 2) {
322 +                i = hres/hstep + 2;
323 +                if ((sampdens = malloc(i)) == NULL)
324 +                        goto memerr;
325 +                while (i--)
326 +                        sampdens[i] = hstep;
327 +        } else
328 +                sampdens = NULL;
329                                          /* open z file */
330          if (zfile != NULL) {
331                  if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
# Line 142 | Line 333 | char  *zfile, *oldfile;
333                          error(SYSTEM, errmsg);
334                  }
335                  for (i = 0; i <= psample; i++) {
336 <                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
336 >                        zbar[i] = (float *)malloc(hres*sizeof(float));
337                          if (zbar[i] == NULL)
338                                  goto memerr;
339                  }
# Line 152 | Line 343 | char  *zfile, *oldfile;
343                          zbar[i] = NULL;
344          }
345                                          /* write out boundaries */
346 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
346 >        fprtresolu(hres, vres, stdout);
347                                          /* recover file and compute first */
348          i = salvage(oldfile);
349          if (zfd != -1 && i > 0 &&
350 <                        lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
350 >                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
351                  error(SYSTEM, "z file seek error in render");
352 <        pctdone = 100.0*i/vresolu;
352 >        pctdone = 100.0*i/vres;
353          if (ralrm > 0)                  /* report init stats */
354                  report();
355 <        ypos = vresolu-1 - i;
356 <        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
357 <        ystep = psample;
355 > #ifndef  BSD
356 >        else
357 > #endif
358 >        signal(SIGALRM, report);
359 >        ypos = vres-1 - i;
360 >        fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
361                                                  /* compute scanlines */
362          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
363                                                          /* bottom adjust? */
# Line 178 | Line 372 | char  *zfile, *oldfile;
372                  zbar[ystep] = zbar[0];
373                  zbar[0] = zptr;
374                                                          /* fill base line */
375 <                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
375 >                fillscanline(scanbar[0], zbar[0], sampdens,
376 >                                hres, ypos, hstep);
377                                                          /* fill bar */
378 <                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
378 >                fillscanbar(scanbar, zbar, hres, ypos, ystep);
379                                                          /* write it out */
380 + #ifndef  BSD
381 +                signal(SIGALRM, SIG_IGN);       /* don't interrupt writes */
382 + #endif
383                  for (i = ystep; i > 0; i--) {
384                          if (zfd != -1 && write(zfd, (char *)zbar[i],
385 <                                        hresolu*sizeof(float))
386 <                                        < hresolu*sizeof(float))
385 >                                        hres*sizeof(float))
386 >                                        < hres*sizeof(float))
387                                  goto writerr;
388 <                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
388 >                        if (fwritescan(scanbar[i], hres, stdout) < 0)
389                                  goto writerr;
390                  }
391                  if (fflush(stdout) == EOF)
392                          goto writerr;
393                                                          /* record progress */
394 <                pctdone = 100.0*(vresolu-1-ypos)/vresolu;
394 >                pctdone = 100.0*(vres-1-ypos)/vres;
395                  if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
396                          report();
397 + #ifndef  BSD
398 +                else
399 +                        signal(SIGALRM, report);
400 + #endif
401          }
402                                                  /* clean up */
403 +        signal(SIGALRM, SIG_IGN);
404          if (zfd != -1) {
405 <                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
406 <                                < hresolu*sizeof(float))
405 >                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
406 >                                < hres*sizeof(float))
407                          goto writerr;
408                  if (close(zfd) == -1)
409                          goto writerr;
410                  for (i = 0; i <= psample; i++)
411                          free((char *)zbar[i]);
412          }
413 <        fwritescan(scanbar[0], hresolu, stdout);
413 >        fwritescan(scanbar[0], hres, stdout);
414          if (fflush(stdout) == EOF)
415                  goto writerr;
416          for (i = 0; i <= psample; i++)
417                  free((char *)scanbar[i]);
418 +        if (sampdens != NULL)
419 +                free(sampdens);
420          pctdone = 100.0;
421 +        if (ralrm > 0)
422 +                report();
423          return;
424   writerr:
425          error(SYSTEM, "write error in render");
# Line 221 | Line 428 | memerr:
428   }
429  
430  
431 < fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
432 < register COLOR  *scanline;
433 < register float  *zline;
431 > fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
432 > register COLOR  *scanline;
433 > register float  *zline;
434 > register char  *sd;
435   int  xres, y, xstep;
436   {
437 <        int  b = xstep;
438 <        double  z;
437 >        static int  nc = 0;             /* number of calls */
438 >        int  bl = xstep, b = xstep;
439 >        double  z;
440          register int  i;
441          
442          z = pixvalue(scanline[0], 0, y);
443          if (zline) zline[0] = z;
444 <
445 <        for (i = xstep; i < xres-1+xstep; i += xstep) {
444 >                                /* zig-zag start for quincunx pattern */
445 >        for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
446                  if (i >= xres) {
447                          xstep += xres-1-i;
448                          i = xres-1;
449                  }
450                  z = pixvalue(scanline[i], i, y);
451                  if (zline) zline[i] = z;
452 <                
453 <                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
454 <                                i-xstep, y, xstep, 0, b/2);
452 >                if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
453 >                if (i <= xstep)
454 >                        b = fillsample(scanline, zline, 0, y, i, 0, b/2);
455 >                else
456 >                        b = fillsample(scanline+i-xstep,
457 >                                        zline ? zline+i-xstep : NULL,
458 >                                        i-xstep, y, xstep, 0, b/2);
459 >                if (sd) *sd++ = nc & 1 ? bl : b;
460 >                bl = b;
461          }
462 +        if (sd && nc & 1) *sd = bl;
463   }
464  
465  
466   fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
467 < register COLOR  *scanbar[];
468 < register float  *zbar[];
467 > register COLOR  *scanbar[];
468 > register float  *zbar[];
469   int  xres, y, ysize;
470   {
471          COLOR  vline[MAXDIV+1];
# Line 279 | Line 495 | int  xres, y, ysize;
495  
496  
497   int
498 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
499 < register COLOR  *colline;
500 < register float  *zline;
498 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
499 > register COLOR  *colline;
500 > register float  *zline;
501   int  x, y;
502   int  xlen, ylen;
503   int  b;
504   {
505 <        extern double  fabs();
506 <        double  ratio;
291 <        double  z;
505 >        double  ratio;
506 >        double  z;
507          COLOR  ctmp;
508          int  ncut;
509          register int  len;
# Line 341 | Line 556 | int  x, y;                     /* pixel position */
556          static RAY  thisray;
557  
558          if (viewray(thisray.rorg, thisray.rdir, &ourview,
559 <                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) {
559 >                        (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
560                  setcolor(col, 0.0, 0.0, 0.0);
561                  return(0.0);
562          }
563  
564          rayorigin(&thisray, NULL, PRIMARY, 1.0);
565  
566 <        samplendx = 3*y + x;                    /* set pixel index */
566 >        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
567  
568          rayvalue(&thisray);                     /* trace ray */
569  
# Line 377 | Line 592 | char  *oldfile;
592                                  /* discard header */
593          getheader(fp, NULL);
594                                  /* get picture size */
595 <        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
595 >        if (!fscnresolu(&x, &y, fp)) {
596                  sprintf(errmsg, "bad recover file \"%s\"", oldfile);
597                  error(WARNING, errmsg);
598                  fclose(fp);
599                  return(0);
600          }
601  
602 <        if (x != hresolu || y != vresolu) {
602 >        if (x != hres || y != vres) {
603                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
604                                  oldfile);
605                  error(USER, errmsg);
606          }
607  
608 <        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
608 >        scanline = (COLR *)malloc(hres*sizeof(COLR));
609          if (scanline == NULL)
610                  error(SYSTEM, "out of memory in salvage");
611 <        for (y = 0; y < vresolu; y++) {
612 <                if (freadcolrs(scanline, hresolu, fp) < 0)
611 >        for (y = 0; y < vres; y++) {
612 >                if (freadcolrs(scanline, hres, fp) < 0)
613                          break;
614 <                if (fwritecolrs(scanline, hresolu, stdout) < 0)
614 >                if (fwritecolrs(scanline, hres, stdout) < 0)
615                          goto writerr;
616          }
617          if (fflush(stdout) == EOF)
# Line 406 | Line 621 | char  *oldfile;
621          unlink(oldfile);
622          return(y);
623   writerr:
624 <        error(SYSTEM, "write error in salvage");
624 >        sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
625 >        error(SYSTEM, errmsg);
626 > }
627 >
628 >
629 > int
630 > pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
631 > register int  x, y;
632 > int  xres, yres;
633 > {
634 >        x -= y;
635 >        while (x < 0)
636 >                x += xres;
637 >        return((((x>>2)*yres + y) << 2) + (x & 3));
638   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines