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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines