ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 2.31
Committed: Thu Nov 18 09:22:55 1993 UTC (30 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +8 -6 lines
Log Message:
fixed time declaration and use

File Contents

# User Rev Content
1 greg 2.8 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * rpict.c - routines and variables for picture generation.
9     *
10     * 8/14/85
11     */
12    
13     #include "ray.h"
14    
15 greg 2.30 #ifndef NIX
16 greg 1.1 #ifdef BSD
17     #include <sys/time.h>
18     #include <sys/resource.h>
19 greg 2.30 #else
20     #include <sys/types.h>
21     #include <sys/times.h>
22     #include <limits.h>
23 greg 2.31 extern time_t time();
24 greg 1.34 #endif
25 greg 2.31 #else
26     extern unsigned long time();
27 greg 2.30 #endif
28 greg 1.34
29 greg 1.14 #include <signal.h>
30 greg 1.1
31     #include "view.h"
32    
33 greg 1.36 #include "resolu.h"
34    
35 greg 1.1 #include "random.h"
36    
37 greg 2.12 #include "paths.h"
38    
39 greg 2.21 #define RFTEMPLATE "rfXXXXXX"
40    
41 greg 2.22 #ifndef SIGCONT
42     #define SIGCONT SIGIO
43     #endif
44    
45 greg 1.25 int dimlist[MAXDIM]; /* sampling dimensions */
46     int ndims = 0; /* number of sampling dimensions */
47     int samplendx; /* sample index number */
48    
49 greg 1.12 VIEW ourview = STDVIEW; /* view parameters */
50     int hresolu = 512; /* horizontal resolution */
51     int vresolu = 512; /* vertical resolution */
52 greg 2.14 double pixaspect = 1.0; /* pixel aspect ratio */
53 greg 1.1
54     int psample = 4; /* pixel sample size */
55 greg 2.14 double maxdiff = .05; /* max. difference for interpolation */
56     double dstrpix = 0.67; /* square pixel distribution */
57 greg 1.1
58 greg 2.14 double dstrsrc = 0.0; /* square source distribution */
59     double shadthresh = .05; /* shadow threshold */
60     double shadcert = .5; /* shadow certainty */
61 greg 1.35 int directrelay = 1; /* number of source relays */
62 greg 1.29 int vspretest = 512; /* virtual source pretest density */
63 greg 2.20 int directvis = 1; /* sources visible? */
64 greg 2.14 double srcsizerat = .25; /* maximum ratio source size/dist. */
65 greg 1.1
66 greg 2.14 double specthresh = .15; /* specular sampling threshold */
67     double specjitter = 1.; /* specular sampling jitter */
68 greg 2.5
69 greg 1.1 int maxdepth = 6; /* maximum recursion depth */
70 greg 2.14 double minweight = 5e-3; /* minimum ray weight */
71 greg 1.1
72     COLOR ambval = BLKCOLOR; /* ambient value */
73 greg 2.14 double ambacc = 0.2; /* ambient accuracy */
74 greg 1.6 int ambres = 32; /* ambient resolution */
75 greg 1.1 int ambdiv = 128; /* ambient divisions */
76     int ambssamp = 0; /* ambient super-samples */
77     int ambounce = 0; /* ambient bounces */
78     char *amblist[128]; /* ambient include/exclude list */
79     int ambincl = -1; /* include == 1, exclude == 0 */
80    
81 greg 2.16 #ifdef MSDOS
82     int ralrm = 60; /* seconds between reports */
83     #else
84 greg 1.1 int ralrm = 0; /* seconds between reports */
85 greg 2.16 #endif
86 greg 1.1
87 greg 2.14 double pctdone = 0.0; /* percentage done */
88 greg 1.1
89 greg 2.31 unsigned long tlastrept = 0L; /* time at last report */
90 greg 1.24
91 greg 2.31 extern unsigned long tstart; /* starting time */
92 greg 1.24
93 greg 2.24 extern unsigned long nrays; /* number of rays traced */
94 greg 1.1
95 greg 2.14 #define MAXDIV 16 /* maximum sample size */
96 greg 1.1
97 greg 2.14 #define pixjitter() (.5+dstrpix*(.5-frandom()))
98 greg 1.1
99 greg 2.8 static int hres, vres; /* resolution for this frame */
100    
101     extern char *mktemp();
102    
103 greg 2.14 double pixvalue();
104 greg 1.1
105 greg 2.27 #ifdef NIX
106     #define file_exists(f) (access(f,F_OK)==0)
107     #else
108     #include <sys/types.h>
109     #include <sys/stat.h>
110     int
111     file_exists(fname) /* ordinary file exists? */
112     char *fname;
113     {
114     struct stat sbuf;
115     if (stat(fname, &sbuf) < 0) return(0);
116     return((sbuf.st_mode & S_IFREG) != 0);
117     }
118     #endif
119 greg 1.11
120 greg 2.27
121 greg 1.1 quit(code) /* quit program */
122     int code;
123     {
124 greg 2.8 if (code) /* report status */
125 greg 1.1 report();
126 greg 2.21 headclean(); /* delete header file */
127     pfclean(); /* clean up persist files */
128 greg 1.1 exit(code);
129     }
130    
131    
132 greg 2.30 #ifndef NIX
133 greg 1.1 report() /* report progress */
134     {
135 greg 2.30 char hostname[128];
136     double u, s;
137     #ifdef BSD
138 greg 1.1 struct rusage rubuf;
139 greg 2.30 #else
140     struct tms tbuf;
141     #endif
142 greg 1.1
143 greg 2.31 tlastrept = time((unsigned long *)NULL);
144 greg 2.30 #ifdef BSD
145 greg 1.1 getrusage(RUSAGE_SELF, &rubuf);
146 greg 2.30 u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
147     s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
148 greg 1.1 getrusage(RUSAGE_CHILDREN, &rubuf);
149 greg 2.30 u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
150     s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
151     #else
152     times(&tbuf);
153     u = ( tbuf.tms_utime + tbuf.tms_cutime ) / CLK_TCK;
154     s = ( tbuf.tms_stime + tbuf.tms_cstime ) / CLK_TCK;
155     #endif
156     gethostname(hostname, sizeof(hostname));
157 greg 1.1
158 greg 2.30 sprintf(errmsg,
159     "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
160     nrays, pctdone, u/3600., s/3600.,
161     (tlastrept-tstart)/3600., hostname);
162 greg 1.24 eputs(errmsg);
163     }
164 greg 1.1 #else
165 greg 1.24 report() /* report progress */
166     {
167 greg 2.31 tlastrept = time((unsigned long *)NULL);
168 greg 2.30 sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
169 greg 1.24 nrays, pctdone, (tlastrept-tstart)/3600.0);
170 greg 1.1 eputs(errmsg);
171 greg 2.22 signal(SIGCONT, report);
172 greg 1.1 }
173 greg 1.24 #endif
174 greg 1.1
175    
176 greg 2.8 rpict(seq, pout, zout, prvr) /* generate image(s) */
177     int seq;
178     char *pout, *zout, *prvr;
179     /*
180     * If seq is greater than zero, then we will render a sequence of
181     * images based on view parameter strings read from the standard input.
182     * If pout is NULL, then all images will be sent to the standard ouput.
183     * If seq is greater than zero and prvr is an integer, then it is the
184     * frame number at which rendering should begin. Preceeding view parameter
185     * strings will be skipped in the input.
186     * If pout and prvr are the same, prvr is renamed to avoid overwriting.
187     * Note that pout and zout should contain %d format specifications for
188     * sequenced file naming.
189     */
190     {
191 greg 2.21 extern char *rindex(), *strncpy(), *strcat(), *strcpy();
192 greg 2.9 char fbuf[128], fbuf2[128];
193 greg 2.26 int npicts;
194 greg 2.9 register char *cp;
195 greg 2.14 RESOLU rs;
196     double pa;
197 greg 2.8 /* 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 greg 2.9 register int rn; /* skip to specified view */
211 greg 2.8 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 greg 2.18 if (pout != NULL & prvr != NULL) {
221 greg 2.8 sprintf(fbuf, pout, seq);
222 greg 2.18 if (!strcmp(prvr, fbuf)) { /* rename */
223     strcpy(fbuf2, fbuf);
224     for (cp = fbuf2; *cp; cp++)
225     ;
226     while (cp > fbuf2 && !ISDIRSEP(cp[-1]))
227     cp--;
228     strcpy(cp, RFTEMPLATE);
229 greg 2.8 prvr = mktemp(fbuf2);
230 greg 2.28 if (rename(fbuf, prvr) < 0)
231     if (errno == ENOENT) { /* ghost file */
232     sprintf(errmsg,
233     "new output file \"%s\"",
234     fbuf);
235     error(WARNING, errmsg);
236     prvr = NULL;
237     } else { /* serious error */
238     sprintf(errmsg,
239 greg 2.8 "cannot rename \"%s\" to \"%s\"",
240     fbuf, prvr);
241 greg 2.28 error(SYSTEM, errmsg);
242     }
243 greg 2.8 }
244     }
245 greg 2.26 npicts = 0; /* render sequence */
246 greg 2.8 do {
247     if (seq && nextview(stdin) == EOF)
248     break;
249 greg 2.25 pctdone = 0.0;
250 greg 2.8 if (pout != NULL) {
251     sprintf(fbuf, pout, seq);
252 greg 2.27 if (file_exists(fbuf)) {
253     if (prvr != NULL || !strcmp(fbuf, pout)) {
254     sprintf(errmsg,
255     "output file \"%s\" exists",
256     fbuf);
257     error(USER, errmsg);
258     }
259 greg 2.26 continue; /* don't clobber */
260 greg 2.27 }
261 greg 2.8 if (freopen(fbuf, "w", stdout) == NULL) {
262     sprintf(errmsg,
263     "cannot open output file \"%s\"", fbuf);
264     error(SYSTEM, errmsg);
265     }
266 greg 2.17 #ifdef MSDOS
267     setmode(fileno(stdout), O_BINARY);
268     #endif
269 greg 2.8 dupheader();
270     }
271     hres = hresolu; vres = vresolu; pa = pixaspect;
272     if (prvr != NULL)
273     if (viewfile(prvr, &ourview, &rs) <= 0
274     || rs.or != PIXSTANDARD) {
275     sprintf(errmsg,
276     "cannot recover view parameters from \"%s\"", prvr);
277     error(WARNING, errmsg);
278     } else {
279     pa = 0.0;
280     hres = scanlen(&rs);
281     vres = numscans(&rs);
282     }
283 greg 2.9 if ((cp = setview(&ourview)) != NULL)
284     error(USER, cp);
285 greg 2.8 normaspect(viewaspect(&ourview), &pa, &hres, &vres);
286     if (seq) {
287     if (ralrm > 0) {
288 greg 2.11 fprintf(stderr, "FRAME %d:", seq);
289     fprintview(&ourview, stderr);
290     putc('\n', stderr);
291     fflush(stderr);
292 greg 2.8 }
293     printf("FRAME=%d\n", seq);
294     }
295     fputs(VIEWSTR, stdout);
296     fprintview(&ourview, stdout);
297     putchar('\n');
298     if (pa < .99 || pa > 1.01)
299     fputaspect(pa, stdout);
300     fputformat(COLRFMT, stdout);
301     putchar('\n');
302     if (zout != NULL)
303 greg 2.9 sprintf(cp=fbuf, zout, seq);
304 greg 2.8 else
305 greg 2.9 cp = NULL;
306     render(cp, prvr);
307 greg 2.8 prvr = NULL;
308 greg 2.26 npicts++;
309 greg 2.8 } while (seq++);
310 greg 2.26 /* check that we did something */
311     if (npicts == 0)
312     error(WARNING, "no output produced");
313 greg 2.8 }
314    
315    
316     nextview(fp) /* get next view from fp */
317     FILE *fp;
318     {
319 greg 2.9 char linebuf[256];
320 greg 2.8
321     while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
322 greg 2.9 if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
323 greg 2.8 return(0);
324     return(EOF);
325     }
326    
327    
328 greg 1.11 render(zfile, oldfile) /* render the scene */
329     char *zfile, *oldfile;
330 greg 1.1 {
331 greg 1.16 extern long lseek();
332 greg 1.1 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
333 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
334 greg 2.2 char *sampdens; /* previous sample density */
335 greg 1.1 int ypos; /* current scanline */
336 greg 1.15 int ystep; /* current y step size */
337 greg 1.33 int hstep; /* h step size */
338 greg 1.16 int zfd;
339 greg 1.1 COLOR *colptr;
340 greg 1.11 float *zptr;
341 greg 1.1 register int i;
342 greg 1.9 /* allocate scanlines */
343 greg 1.1 for (i = 0; i <= psample; i++) {
344 greg 2.8 scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
345 greg 1.1 if (scanbar[i] == NULL)
346 greg 1.11 goto memerr;
347 greg 1.1 }
348 greg 2.2 hstep = (psample*140+49)/99; /* quincunx sampling */
349     ystep = (psample*99+70)/140;
350     if (hstep > 2) {
351 greg 2.8 i = hres/hstep + 2;
352 greg 2.2 if ((sampdens = malloc(i)) == NULL)
353     goto memerr;
354     while (i--)
355     sampdens[i] = hstep;
356     } else
357     sampdens = NULL;
358 greg 2.26 /* open z-file */
359 greg 1.11 if (zfile != NULL) {
360 greg 1.16 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
361 greg 2.26 sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
362 greg 1.11 error(SYSTEM, errmsg);
363     }
364 greg 2.17 #ifdef MSDOS
365     setmode(zfd, O_BINARY);
366     #endif
367 greg 1.11 for (i = 0; i <= psample; i++) {
368 greg 2.8 zbar[i] = (float *)malloc(hres*sizeof(float));
369 greg 1.11 if (zbar[i] == NULL)
370     goto memerr;
371     }
372     } else {
373 greg 1.16 zfd = -1;
374 greg 1.11 for (i = 0; i <= psample; i++)
375     zbar[i] = NULL;
376     }
377 greg 1.1 /* write out boundaries */
378 greg 2.8 fprtresolu(hres, vres, stdout);
379 greg 1.10 /* recover file and compute first */
380 greg 1.11 i = salvage(oldfile);
381 greg 1.23 if (zfd != -1 && i > 0 &&
382 greg 2.8 lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
383 greg 2.26 error(SYSTEM, "z-file seek error in render");
384 greg 2.8 pctdone = 100.0*i/vres;
385 greg 1.24 if (ralrm > 0) /* report init stats */
386     report();
387 greg 2.14 #ifndef BSD
388 greg 1.32 else
389     #endif
390 greg 2.22 signal(SIGCONT, report);
391 greg 2.8 ypos = vres-1 - i;
392     fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
393 greg 1.10 /* compute scanlines */
394 greg 1.15 for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
395     /* bottom adjust? */
396     if (ypos < 0) {
397     ystep += ypos;
398     ypos = 0;
399     }
400     colptr = scanbar[ystep]; /* move base to top */
401     scanbar[ystep] = scanbar[0];
402 greg 1.1 scanbar[0] = colptr;
403 greg 1.15 zptr = zbar[ystep];
404     zbar[ystep] = zbar[0];
405 greg 1.11 zbar[0] = zptr;
406 greg 1.10 /* fill base line */
407 greg 2.2 fillscanline(scanbar[0], zbar[0], sampdens,
408 greg 2.8 hres, ypos, hstep);
409 greg 1.10 /* fill bar */
410 greg 2.8 fillscanbar(scanbar, zbar, hres, ypos, ystep);
411 greg 1.10 /* write it out */
412 greg 2.14 #ifndef BSD
413 greg 2.22 signal(SIGCONT, SIG_IGN); /* don't interrupt writes */
414 greg 1.32 #endif
415 greg 1.15 for (i = ystep; i > 0; i--) {
416 greg 1.17 if (zfd != -1 && write(zfd, (char *)zbar[i],
417 greg 2.8 hres*sizeof(float))
418     < hres*sizeof(float))
419 greg 1.1 goto writerr;
420 greg 2.8 if (fwritescan(scanbar[i], hres, stdout) < 0)
421 greg 1.11 goto writerr;
422     }
423 greg 1.1 if (fflush(stdout) == EOF)
424     goto writerr;
425 greg 1.24 /* record progress */
426 greg 2.8 pctdone = 100.0*(vres-1-ypos)/vres;
427 greg 2.31 if (ralrm > 0 && time((unsigned long *)NULL) >= tlastrept+ralrm)
428 greg 1.24 report();
429 greg 2.14 #ifndef BSD
430 greg 1.32 else
431 greg 2.22 signal(SIGCONT, report);
432 greg 1.32 #endif
433 greg 1.1 }
434 greg 1.11 /* clean up */
435 greg 2.22 signal(SIGCONT, SIG_IGN);
436 greg 1.16 if (zfd != -1) {
437 greg 2.8 if (write(zfd, (char *)zbar[0], hres*sizeof(float))
438     < hres*sizeof(float))
439 greg 1.11 goto writerr;
440 greg 1.20 if (close(zfd) == -1)
441     goto writerr;
442 greg 1.11 for (i = 0; i <= psample; i++)
443     free((char *)zbar[i]);
444     }
445 greg 2.8 fwritescan(scanbar[0], hres, stdout);
446 greg 1.10 if (fflush(stdout) == EOF)
447     goto writerr;
448 greg 1.1 for (i = 0; i <= psample; i++)
449     free((char *)scanbar[i]);
450 greg 2.2 if (sampdens != NULL)
451     free(sampdens);
452 greg 1.11 pctdone = 100.0;
453 greg 2.13 if (ralrm > 0)
454     report();
455 greg 2.23 signal(SIGCONT, SIG_DFL);
456 greg 1.1 return;
457     writerr:
458     error(SYSTEM, "write error in render");
459 greg 1.11 memerr:
460     error(SYSTEM, "out of memory in render");
461 greg 1.1 }
462    
463    
464 greg 2.2 fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */
465 greg 2.14 register COLOR *scanline;
466     register float *zline;
467 greg 2.2 register char *sd;
468 greg 1.9 int xres, y, xstep;
469 greg 1.1 {
470 greg 1.33 static int nc = 0; /* number of calls */
471 greg 2.2 int bl = xstep, b = xstep;
472 greg 2.14 double z;
473 greg 1.1 register int i;
474    
475 greg 1.11 z = pixvalue(scanline[0], 0, y);
476     if (zline) zline[0] = z;
477 greg 1.33 /* zig-zag start for quincunx pattern */
478 greg 2.2 for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) {
479 greg 1.15 if (i >= xres) {
480     xstep += xres-1-i;
481     i = xres-1;
482     }
483 greg 1.11 z = pixvalue(scanline[i], i, y);
484     if (zline) zline[i] = z;
485 greg 2.2 if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1];
486 greg 2.3 if (i <= xstep)
487     b = fillsample(scanline, zline, 0, y, i, 0, b/2);
488     else
489     b = fillsample(scanline+i-xstep,
490     zline ? zline+i-xstep : NULL,
491     i-xstep, y, xstep, 0, b/2);
492 greg 2.2 if (sd) *sd++ = nc & 1 ? bl : b;
493     bl = b;
494 greg 1.1 }
495 greg 2.2 if (sd && nc & 1) *sd = bl;
496 greg 1.1 }
497    
498    
499 greg 1.11 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
500 greg 2.14 register COLOR *scanbar[];
501     register float *zbar[];
502 greg 1.9 int xres, y, ysize;
503 greg 1.1 {
504     COLOR vline[MAXDIV+1];
505 greg 1.11 float zline[MAXDIV+1];
506 greg 1.1 int b = ysize;
507     register int i, j;
508    
509 greg 1.8 for (i = 0; i < xres; i++) {
510 greg 1.1
511     copycolor(vline[0], scanbar[0][i]);
512     copycolor(vline[ysize], scanbar[ysize][i]);
513 greg 1.11 if (zbar[0]) {
514     zline[0] = zbar[0][i];
515     zline[ysize] = zbar[ysize][i];
516     }
517 greg 1.1
518 greg 1.11 b = fillsample(vline, zbar[0] ? zline : NULL,
519     i, y, 0, ysize, b/2);
520 greg 1.1
521     for (j = 1; j < ysize; j++)
522     copycolor(scanbar[j][i], vline[j]);
523 greg 1.11 if (zbar[0])
524     for (j = 1; j < ysize; j++)
525     zbar[j][i] = zline[j];
526 greg 1.1 }
527     }
528    
529    
530     int
531 greg 2.14 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
532     register COLOR *colline;
533     register float *zline;
534 greg 1.1 int x, y;
535     int xlen, ylen;
536     int b;
537     {
538 greg 2.14 double ratio;
539     double z;
540 greg 1.1 COLOR ctmp;
541     int ncut;
542     register int len;
543    
544     if (xlen > 0) /* x or y length is zero */
545     len = xlen;
546     else
547     len = ylen;
548    
549     if (len <= 1) /* limit recursion */
550     return(0);
551    
552 greg 1.11 if (b > 0
553     || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
554     || bigdiff(colline[0], colline[len], maxdiff)) {
555 greg 1.1
556 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
557     if (zline) zline[len>>1] = z;
558 greg 1.1 ncut = 1;
559    
560     } else { /* interpolate */
561    
562     copycolor(colline[len>>1], colline[len]);
563     ratio = (double)(len>>1) / len;
564     scalecolor(colline[len>>1], ratio);
565 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
566     ratio = 1.0 - ratio;
567 greg 1.1 copycolor(ctmp, colline[0]);
568     scalecolor(ctmp, ratio);
569     addcolor(colline[len>>1], ctmp);
570 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
571 greg 1.1 ncut = 0;
572     }
573     /* recurse */
574 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
575 greg 1.1
576 greg 1.11 ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
577     x+(xlen>>1), y+(ylen>>1),
578     xlen-(xlen>>1), ylen-(ylen>>1), b/2);
579 greg 1.1
580     return(ncut);
581     }
582    
583    
584 greg 1.11 double
585 greg 1.1 pixvalue(col, x, y) /* compute pixel value */
586     COLOR col; /* returned color */
587     int x, y; /* pixel position */
588     {
589 greg 1.25 static RAY thisray;
590 greg 1.1
591 greg 1.22 if (viewray(thisray.rorg, thisray.rdir, &ourview,
592 greg 2.8 (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) {
593 greg 1.22 setcolor(col, 0.0, 0.0, 0.0);
594     return(0.0);
595     }
596 greg 1.1
597     rayorigin(&thisray, NULL, PRIMARY, 1.0);
598 greg 1.25
599 greg 2.8 samplendx = pixnumber(x,y,hres,vres); /* set pixel index */
600 greg 1.25
601 greg 1.1 rayvalue(&thisray); /* trace ray */
602    
603     copycolor(col, thisray.rcol); /* return color */
604 greg 1.11
605 greg 1.20 return(thisray.rt); /* return distance */
606 greg 1.1 }
607    
608    
609     int
610     salvage(oldfile) /* salvage scanlines from killed program */
611     char *oldfile;
612     {
613     COLR *scanline;
614     FILE *fp;
615     int x, y;
616    
617     if (oldfile == NULL)
618     return(0);
619 greg 1.9
620     if ((fp = fopen(oldfile, "r")) == NULL) {
621 greg 1.1 sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
622     error(WARNING, errmsg);
623     return(0);
624     }
625 greg 2.17 #ifdef MSDOS
626     setmode(fileno(fp), O_BINARY);
627     #endif
628 greg 1.1 /* discard header */
629 greg 2.19 getheader(fp, NULL, NULL);
630 greg 1.1 /* get picture size */
631 greg 1.36 if (!fscnresolu(&x, &y, fp)) {
632 greg 2.29 sprintf(errmsg, "bad recover file \"%s\" - not removed",
633     oldfile);
634 greg 1.2 error(WARNING, errmsg);
635 greg 1.1 fclose(fp);
636     return(0);
637     }
638    
639 greg 2.8 if (x != hres || y != vres) {
640 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
641     oldfile);
642     error(USER, errmsg);
643     }
644    
645 greg 2.8 scanline = (COLR *)malloc(hres*sizeof(COLR));
646 greg 1.1 if (scanline == NULL)
647     error(SYSTEM, "out of memory in salvage");
648 greg 2.8 for (y = 0; y < vres; y++) {
649     if (freadcolrs(scanline, hres, fp) < 0)
650 greg 1.1 break;
651 greg 2.8 if (fwritecolrs(scanline, hres, stdout) < 0)
652 greg 1.1 goto writerr;
653     }
654     if (fflush(stdout) == EOF)
655     goto writerr;
656     free((char *)scanline);
657     fclose(fp);
658     unlink(oldfile);
659     return(y);
660     writerr:
661 greg 2.9 sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
662     error(SYSTEM, errmsg);
663 greg 1.31 }
664    
665    
666     int
667     pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */
668     register int x, y;
669     int xres, yres;
670     {
671     x -= y;
672     while (x < 0)
673     x += xres;
674     return((((x>>2)*yres + y) << 2) + (x & 3));
675 greg 1.1 }