ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 1.16
Committed: Sat Jan 6 22:27:10 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.15: +23 -6 lines
Log Message:
added code to deal with picture exposure

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2     static char SCCSid[] = "$SunId$ LBL";
3     #endif
4    
5     /*
6     * Interpolate and extrapolate pictures with different view parameters.
7     *
8     * Greg Ward 09Dec89
9     */
10    
11     #include "standard.h"
12    
13     #include "view.h"
14    
15     #include "color.h"
16    
17     #define pscan(y) (ourpict+(y)*ourview.hresolu)
18     #define zscan(y) (ourzbuf+(y)*ourview.hresolu)
19    
20 greg 1.13 #define F_FORE 1 /* fill foreground */
21     #define F_BACK 2 /* fill background */
22 greg 1.11
23 greg 1.13 #define PACKSIZ 42 /* calculation packet size */
24    
25     #define RTCOM "rtrace -h -ovl -fff -x %d %s"
26    
27 greg 1.3 #define ABS(x) ((x)>0?(x):-(x))
28    
29 greg 1.15 struct position {int x,y; float z;};
30    
31 greg 1.1 VIEW ourview = STDVIEW(512); /* desired view */
32    
33 greg 1.8 double zeps = .02; /* allowed z epsilon */
34 greg 1.1
35     COLR *ourpict; /* output picture */
36     float *ourzbuf; /* corresponding z-buffer */
37    
38     char *progname;
39    
40 greg 1.12 int fill = F_FORE|F_BACK; /* selected fill algorithm */
41 greg 1.14 extern int backfill(), rcalfill(); /* fill functions */
42 greg 1.12 int (*deffill)() = backfill; /* selected fill function */
43 greg 1.11 COLR backcolr = BLKCOLR; /* background color */
44 greg 1.13 double backz = 0.0; /* background z value */
45 greg 1.16 int normdist = 1; /* normalized distance? */
46     double ourexp = -1; /* output picture exposure */
47 greg 1.11
48 greg 1.16 VIEW theirview = STDVIEW(512); /* input view */
49     int gotview; /* got input view? */
50 greg 1.5 double theirs2ours[4][4]; /* transformation matrix */
51 greg 1.16 double theirexp; /* input picture exposure */
52 greg 1.1
53 greg 1.14 int childpid = -1; /* id of fill process */
54 greg 1.13 FILE *psend, *precv; /* pipes to/from fill calculation */
55     int queue[PACKSIZ][2]; /* pending pixels */
56     int queuesiz; /* number of pixels pending */
57 greg 1.5
58 greg 1.13
59 greg 1.1 main(argc, argv) /* interpolate pictures */
60     int argc;
61     char *argv[];
62     {
63     #define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt
64 greg 1.11 extern double atof();
65 greg 1.1 int gotvfile = 0;
66 greg 1.11 char *zfile = NULL;
67 greg 1.1 char *err;
68     int i;
69    
70     progname = argv[0];
71    
72     for (i = 1; i < argc && argv[i][0] == '-'; i++)
73     switch (argv[i][1]) {
74     case 't': /* threshold */
75     check(2,1);
76     zeps = atof(argv[++i]);
77     break;
78 greg 1.11 case 'n': /* dist. normalized? */
79 greg 1.10 check(2,0);
80 greg 1.11 normdist = !normdist;
81 greg 1.10 break;
82 greg 1.11 case 'f': /* fill type */
83     switch (argv[i][2]) {
84     case '0': /* none */
85     check(3,0);
86     fill = 0;
87     break;
88     case 'f': /* foreground */
89     check(3,0);
90     fill = F_FORE;
91     break;
92     case 'b': /* background */
93     check(3,0);
94     fill = F_BACK;
95     break;
96     case 'a': /* all */
97     check(3,0);
98     fill = F_FORE|F_BACK;
99     break;
100     case 'c': /* color */
101     check(3,3);
102 greg 1.12 deffill = backfill;
103 greg 1.11 setcolr(backcolr, atof(argv[i+1]),
104     atof(argv[i+2]), atof(argv[i+3]));
105     i += 3;
106     break;
107 greg 1.13 case 'z': /* z value */
108     check(3,1);
109     deffill = backfill;
110     backz = atof(argv[++i]);
111     break;
112     case 'r': /* rtrace */
113     check(3,1);
114 greg 1.14 deffill = rcalfill;
115 greg 1.13 calstart(RTCOM, argv[++i]);
116     break;
117 greg 1.11 default:
118     goto badopt;
119     }
120     break;
121     case 'z': /* z file */
122     check(2,1);
123     zfile = argv[++i];
124     break;
125 greg 1.6 case 'x': /* x resolution */
126     check(2,1);
127     ourview.hresolu = atoi(argv[++i]);
128     break;
129     case 'y': /* y resolution */
130     check(2,1);
131     ourview.vresolu = atoi(argv[++i]);
132     break;
133 greg 1.1 case 'v': /* view */
134     switch (argv[i][2]) {
135     case 't': /* type */
136     check(4,0);
137     ourview.type = argv[i][3];
138     break;
139     case 'p': /* point */
140     check(3,3);
141     ourview.vp[0] = atof(argv[++i]);
142     ourview.vp[1] = atof(argv[++i]);
143     ourview.vp[2] = atof(argv[++i]);
144     break;
145     case 'd': /* direction */
146     check(3,3);
147     ourview.vdir[0] = atof(argv[++i]);
148     ourview.vdir[1] = atof(argv[++i]);
149     ourview.vdir[2] = atof(argv[++i]);
150     break;
151     case 'u': /* up */
152     check(3,3);
153     ourview.vup[0] = atof(argv[++i]);
154     ourview.vup[1] = atof(argv[++i]);
155     ourview.vup[2] = atof(argv[++i]);
156     break;
157     case 'h': /* horizontal */
158     check(3,1);
159     ourview.horiz = atof(argv[++i]);
160     break;
161     case 'v': /* vertical */
162     check(3,1);
163     ourview.vert = atof(argv[++i]);
164     break;
165     case 'f': /* file */
166     check(3,1);
167     gotvfile = viewfile(argv[++i], &ourview);
168     if (gotvfile < 0) {
169     perror(argv[i]);
170     exit(1);
171     } else if (gotvfile == 0) {
172     fprintf(stderr, "%s: bad view file\n",
173     argv[i]);
174     exit(1);
175     }
176     break;
177     default:
178     goto badopt;
179     }
180     break;
181     default:
182     badopt:
183 greg 1.10 fprintf(stderr, "%s: command line error at '%s'\n",
184 greg 1.1 progname, argv[i]);
185 greg 1.10 goto userr;
186 greg 1.1 }
187     /* check arguments */
188 greg 1.13 if ((argc-i)%2)
189 greg 1.10 goto userr;
190 greg 1.1 /* set view */
191     if (err = setview(&ourview)) {
192     fprintf(stderr, "%s: %s\n", progname, err);
193     exit(1);
194     }
195     /* allocate frame */
196 greg 1.12 ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
197 greg 1.1 ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
198 greg 1.15 if (ourpict == NULL || ourzbuf == NULL)
199     syserror();
200 greg 1.1 /* get input */
201     for ( ; i < argc; i += 2)
202     addpicture(argv[i], argv[i+1]);
203     /* fill in spaces */
204 greg 1.11 if (fill&F_BACK)
205 greg 1.12 backpicture();
206     else
207 greg 1.11 fillpicture();
208 greg 1.13 /* close calculation */
209 greg 1.14 caldone();
210 greg 1.1 /* add to header */
211     printargs(argc, argv, stdout);
212     if (gotvfile) {
213     printf(VIEWSTR);
214     fprintview(&ourview, stdout);
215     printf("\n");
216     }
217 greg 1.16 if (ourexp > 0 && ourexp != 1.0)
218     fputexpos(ourexp, stdout);
219 greg 1.1 printf("\n");
220 greg 1.11 /* write picture */
221 greg 1.1 writepicture();
222 greg 1.11 /* write z file */
223     if (zfile != NULL)
224     writedistance(zfile);
225 greg 1.1
226     exit(0);
227 greg 1.10 userr:
228     fprintf(stderr,
229 greg 1.11 "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
230 greg 1.10 progname);
231     exit(1);
232 greg 1.1 #undef check
233     }
234    
235    
236     headline(s) /* process header string */
237     char *s;
238     {
239     static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
240     register char **an;
241    
242     printf("\t%s", s);
243    
244 greg 1.16 if (isexpos(s)) {
245     theirexp *= exposval(s);
246     return;
247     }
248 greg 1.1 for (an = altname; *an != NULL; an++)
249     if (!strncmp(*an, s, strlen(*an))) {
250     if (sscanview(&theirview, s+strlen(*an)) == 0)
251     gotview++;
252     break;
253     }
254     }
255    
256    
257 greg 1.10 addpicture(pfile, zspec) /* add picture to output */
258     char *pfile, *zspec;
259 greg 1.1 {
260 greg 1.10 extern double atof();
261 greg 1.1 FILE *pfp, *zfp;
262 greg 1.8 char *err;
263 greg 1.1 COLR *scanin;
264 greg 1.15 float *zin;
265     struct position *plast;
266 greg 1.1 int y;
267 greg 1.10 /* open picture file */
268 greg 1.1 if ((pfp = fopen(pfile, "r")) == NULL) {
269     perror(pfile);
270     exit(1);
271     }
272 greg 1.16 /* get header with exposure and view */
273     theirexp = 1.0;
274     gotview = 0;
275 greg 1.1 printf("%s:\n", pfile);
276     getheader(pfp, headline);
277 greg 1.9 if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
278     != (YMAJOR|YDECR)) {
279 greg 1.1 fprintf(stderr, "%s: picture view error\n", pfile);
280     exit(1);
281     }
282 greg 1.16 if (ourexp <= 0)
283     ourexp = theirexp;
284     else if (ABS(theirexp-ourexp) > .01*ourexp)
285     fprintf(stderr, "%s: different exposure (warning)\n", pfile);
286 greg 1.1 if (err = setview(&theirview)) {
287     fprintf(stderr, "%s: %s\n", pfile, err);
288     exit(1);
289     }
290 greg 1.5 /* compute transformation */
291     pixform(theirs2ours, &theirview, &ourview);
292 greg 1.1 /* allocate scanlines */
293 greg 1.9 scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
294     zin = (float *)malloc(theirview.hresolu*sizeof(float));
295 greg 1.15 plast = (struct position *)calloc(theirview.hresolu,
296     sizeof(struct position));
297     if (scanin == NULL || zin == NULL || plast == NULL)
298     syserror();
299 greg 1.10 /* get z specification or file */
300     if ((zfp = fopen(zspec, "r")) == NULL) {
301     double zvalue;
302     register int x;
303     if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
304     perror(zspec);
305     exit(1);
306     }
307     for (x = 0; x < theirview.hresolu; x++)
308     zin[x] = zvalue;
309     }
310 greg 1.1 /* load image */
311 greg 1.9 for (y = theirview.vresolu-1; y >= 0; y--) {
312     if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
313 greg 1.1 fprintf(stderr, "%s: read error\n", pfile);
314     exit(1);
315     }
316 greg 1.10 if (zfp != NULL
317     && fread(zin,sizeof(float),theirview.hresolu,zfp)
318 greg 1.9 < theirview.hresolu) {
319 greg 1.10 fprintf(stderr, "%s: read error\n", zspec);
320 greg 1.1 exit(1);
321     }
322 greg 1.15 addscanline(y, scanin, zin, plast);
323 greg 1.1 }
324     /* clean up */
325     free((char *)scanin);
326     free((char *)zin);
327 greg 1.9 free((char *)plast);
328 greg 1.1 fclose(pfp);
329 greg 1.10 if (zfp != NULL)
330     fclose(zfp);
331 greg 1.1 }
332    
333    
334 greg 1.5 pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */
335     register double xfmat[4][4];
336     register VIEW *vw1, *vw2;
337     {
338     double m4t[4][4];
339    
340     setident4(xfmat);
341     xfmat[0][0] = vw1->vhinc[0];
342     xfmat[0][1] = vw1->vhinc[1];
343     xfmat[0][2] = vw1->vhinc[2];
344     xfmat[1][0] = vw1->vvinc[0];
345     xfmat[1][1] = vw1->vvinc[1];
346     xfmat[1][2] = vw1->vvinc[2];
347     xfmat[2][0] = vw1->vdir[0];
348     xfmat[2][1] = vw1->vdir[1];
349     xfmat[2][2] = vw1->vdir[2];
350     xfmat[3][0] = vw1->vp[0];
351     xfmat[3][1] = vw1->vp[1];
352     xfmat[3][2] = vw1->vp[2];
353     setident4(m4t);
354     m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
355     m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
356     m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
357     m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
358     m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
359     m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
360     m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
361     m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
362     m4t[0][2] = vw2->vdir[0];
363     m4t[1][2] = vw2->vdir[1];
364     m4t[2][2] = vw2->vdir[2];
365     m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
366     multmat4(xfmat, xfmat, m4t);
367     }
368    
369    
370 greg 1.15 addscanline(y, pline, zline, lasty) /* add scanline to output */
371 greg 1.1 int y;
372     COLR *pline;
373     float *zline;
374 greg 1.15 struct position *lasty; /* input/output */
375 greg 1.1 {
376 greg 1.11 extern double sqrt();
377 greg 1.5 double pos[3];
378 greg 1.15 struct position lastx, newpos;
379 greg 1.1 register int x;
380    
381 greg 1.8 for (x = theirview.hresolu-1; x >= 0; x--) {
382 greg 1.5 pos[0] = x - .5*(theirview.hresolu-1);
383     pos[1] = y - .5*(theirview.vresolu-1);
384     pos[2] = zline[x];
385     if (theirview.type == VT_PER) {
386 greg 1.11 if (normdist) /* adjust for eye-ray distance */
387 greg 1.10 pos[2] /= sqrt( 1.
388 greg 1.5 + pos[0]*pos[0]*theirview.vhn2
389     + pos[1]*pos[1]*theirview.vvn2 );
390     pos[0] *= pos[2];
391     pos[1] *= pos[2];
392     }
393     multp3(pos, pos, theirs2ours);
394 greg 1.15 if (pos[2] <= 0) {
395     lasty[x].z = lastx.z = 0; /* mark invalid */
396 greg 1.1 continue;
397 greg 1.15 }
398 greg 1.5 if (ourview.type == VT_PER) {
399     pos[0] /= pos[2];
400     pos[1] /= pos[2];
401     }
402     pos[0] += .5*ourview.hresolu;
403     pos[1] += .5*ourview.vresolu;
404 greg 1.15 newpos.x = pos[0];
405     newpos.y = pos[1];
406     newpos.z = zline[x];
407 greg 1.8 /* add pixel to our image */
408 greg 1.15 if (pos[0] >= 0 && newpos.x < ourview.hresolu
409     && pos[1] >= 0 && newpos.y < ourview.vresolu) {
410     addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
411     lasty[x].x = lastx.x = newpos.x;
412     lasty[x].y = lastx.y = newpos.y;
413     lasty[x].z = lastx.z = newpos.z;
414     } else
415     lasty[x].z = lastx.z = 0; /* mark invalid */
416 greg 1.1 }
417     }
418    
419    
420 greg 1.15 addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */
421     struct position *p0, *p1, *p2;
422 greg 1.8 COLR pix;
423     double z;
424     {
425 greg 1.15 double zt = 2.*zeps*p0->z; /* threshold */
426     int s1x, s1y, s2x, s2y; /* step sizes */
427     int l1, l2, c1, c2; /* side lengths and counters */
428     int p1isy; /* p0p1 along y? */
429     int x1, y1; /* p1 position */
430     register int x, y; /* final position */
431    
432     /* compute vector p0p1 */
433     if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
434     s1x = p1->x - p0->x;
435     s1y = p1->y - p0->y;
436     l1 = ABS(s1x);
437     if (p1isy = (ABS(s1y) > l1))
438     l1 = ABS(s1y);
439     } else {
440     l1 = s1x = s1y = 1;
441     p1isy = -1;
442     }
443     /* compute vector p0p2 */
444     if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
445     s2x = p2->x - p0->x;
446     s2y = p2->y - p0->y;
447     if (p1isy == 1)
448     l2 = ABS(s2x);
449     else {
450     l2 = ABS(s2y);
451     if (p1isy != 0 && ABS(s2x) > l2)
452     l2 = ABS(s2x);
453     }
454     } else
455     l2 = s2x = s2y = 1;
456     /* fill the parallelogram */
457     for (c1 = l1; c1-- > 0; ) {
458     x1 = p0->x + c1*s1x/l1;
459     y1 = p0->y + c1*s1y/l1;
460     for (c2 = l2; c2-- > 0; ) {
461     x = x1 + c2*s2x/l2;
462     y = y1 + c2*s2y/l2;
463     if (zscan(y)[x] <= 0 || zscan(y)[x]-z
464     > zeps*zscan(y)[x]) {
465 greg 1.8 zscan(y)[x] = z;
466     copycolr(pscan(y)[x], pix);
467     }
468 greg 1.15 }
469     }
470 greg 1.8 }
471    
472    
473 greg 1.12 backpicture() /* background fill algorithm */
474 greg 1.1 {
475 greg 1.2 int *yback, xback;
476 greg 1.1 int y;
477 greg 1.2 COLR pfill;
478     register int x, i;
479     /* get back buffer */
480     yback = (int *)malloc(ourview.hresolu*sizeof(int));
481 greg 1.15 if (yback == NULL)
482     syserror();
483 greg 1.2 for (x = 0; x < ourview.hresolu; x++)
484 greg 1.4 yback[x] = -2;
485 greg 1.7 /*
486     * Xback and yback are the pixel locations of suitable
487     * background values in each direction.
488     * A value of -2 means unassigned, and -1 means
489     * that there is no suitable background in this direction.
490     */
491 greg 1.2 /* fill image */
492 greg 1.4 for (y = 0; y < ourview.vresolu; y++) {
493     xback = -2;
494 greg 1.1 for (x = 0; x < ourview.hresolu; x++)
495 greg 1.8 if (zscan(y)[x] <= 0) { /* empty pixel */
496 greg 1.7 /*
497     * First, find background from above or below.
498     * (farthest assigned pixel)
499     */
500 greg 1.4 if (yback[x] == -2) {
501     for (i = y+1; i < ourview.vresolu; i++)
502 greg 1.8 if (zscan(i)[x] > 0)
503 greg 1.4 break;
504     if (i < ourview.vresolu
505 greg 1.2 && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
506 greg 1.4 yback[x] = i;
507     else
508     yback[x] = y-1;
509 greg 1.2 }
510 greg 1.7 /*
511     * Next, find background from left or right.
512     */
513 greg 1.4 if (xback == -2) {
514 greg 1.11 for (i = x+1; i < ourview.hresolu; i++)
515 greg 1.8 if (zscan(y)[i] > 0)
516 greg 1.4 break;
517     if (i < ourview.hresolu
518     && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
519     xback = i;
520     else
521     xback = x-1;
522     }
523 greg 1.7 /*
524 greg 1.11 * Check to see if we have no background for
525     * this pixel. If not, use background color.
526     */
527     if (xback < 0 && yback[x] < 0) {
528 greg 1.12 (*deffill)(x,y);
529 greg 1.11 continue;
530     }
531     /*
532 greg 1.7 * Compare, and use the background that is
533     * farther, unless one of them is next to us.
534     */
535 greg 1.11 if ( yback[x] < 0
536     || (xback >= 0 && ABS(x-xback) <= 1)
537 greg 1.4 || ( ABS(y-yback[x]) > 1
538 greg 1.11 && zscan(yback[x])[x]
539 greg 1.13 < zscan(y)[xback] ) ) {
540 greg 1.4 copycolr(pscan(y)[x],pscan(y)[xback]);
541 greg 1.13 zscan(y)[x] = zscan(y)[xback];
542     } else {
543 greg 1.4 copycolr(pscan(y)[x],pscan(yback[x])[x]);
544 greg 1.13 zscan(y)[x] = zscan(yback[x])[x];
545     }
546 greg 1.4 } else { /* full pixel */
547     yback[x] = -2;
548     xback = -2;
549     }
550     }
551 greg 1.2 free((char *)yback);
552 greg 1.1 }
553    
554    
555 greg 1.12 fillpicture() /* paint in empty pixels with default */
556 greg 1.11 {
557     register int x, y;
558    
559     for (y = 0; y < ourview.vresolu; y++)
560     for (x = 0; x < ourview.hresolu; x++)
561     if (zscan(y)[x] <= 0)
562 greg 1.12 (*deffill)(x,y);
563 greg 1.11 }
564    
565    
566 greg 1.1 writepicture() /* write out picture */
567     {
568     int y;
569    
570     fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
571     for (y = ourview.vresolu-1; y >= 0; y--)
572 greg 1.15 if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0)
573     syserror();
574 greg 1.11 }
575    
576    
577     writedistance(fname) /* write out z file */
578     char *fname;
579     {
580     extern double sqrt();
581     int donorm = normdist && ourview.type == VT_PER;
582     FILE *fp;
583     int y;
584     float *zout;
585    
586     if ((fp = fopen(fname, "w")) == NULL) {
587     perror(fname);
588     exit(1);
589     }
590     if (donorm
591 greg 1.15 && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL)
592     syserror();
593 greg 1.11 for (y = ourview.vresolu-1; y >= 0; y--) {
594     if (donorm) {
595     double vx, yzn2;
596     register int x;
597     yzn2 = y - .5*(ourview.vresolu-1);
598     yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
599     for (x = 0; x < ourview.hresolu; x++) {
600     vx = x - .5*(ourview.hresolu-1);
601     zout[x] = zscan(y)[x]
602     * sqrt(vx*vx*ourview.vhn2 + yzn2);
603     }
604     } else
605     zout = zscan(y);
606     if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
607     < ourview.hresolu) {
608     perror(fname);
609     exit(1);
610     }
611     }
612     if (donorm)
613     free((char *)zout);
614     fclose(fp);
615 greg 1.10 }
616    
617    
618     isfloat(s) /* see if string is floating number */
619     register char *s;
620     {
621     for ( ; *s; s++)
622     if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
623     && *s != 'e' && *s != 'E' && *s != '+')
624     return(0);
625     return(1);
626 greg 1.12 }
627    
628    
629     backfill(x, y) /* fill pixel with background */
630     int x, y;
631     {
632     register BYTE *dest = pscan(y)[x];
633    
634     copycolr(dest, backcolr);
635 greg 1.13 zscan(y)[x] = backz;
636     }
637    
638    
639     calstart(prog, args) /* start fill calculation */
640     char *prog, *args;
641     {
642     char combuf[512];
643     int p0[2], p1[2];
644    
645 greg 1.14 if (childpid != -1) {
646     fprintf(stderr, "%s: too many calculations\n", progname);
647     exit(1);
648     }
649 greg 1.13 sprintf(combuf, prog, PACKSIZ, args);
650     if (pipe(p0) < 0 || pipe(p1) < 0)
651 greg 1.14 syserror();
652 greg 1.13 if ((childpid = vfork()) == 0) { /* fork calculation */
653     close(p0[1]);
654     close(p1[0]);
655     if (p0[0] != 0) {
656     dup2(p0[0], 0);
657     close(p0[0]);
658     }
659     if (p1[1] != 1) {
660     dup2(p1[1], 1);
661     close(p1[1]);
662     }
663     execl("/bin/sh", "sh", "-c", combuf, 0);
664     perror("/bin/sh");
665     _exit(127);
666     }
667 greg 1.14 if (childpid == -1)
668     syserror();
669 greg 1.13 close(p0[0]);
670     close(p1[1]);
671     if ((psend = fdopen(p0[1], "w")) == NULL)
672 greg 1.14 syserror();
673 greg 1.13 if ((precv = fdopen(p1[0], "r")) == NULL)
674 greg 1.14 syserror();
675 greg 1.13 queuesiz = 0;
676     }
677    
678    
679     caldone() /* done with calculation */
680     {
681     int pid;
682    
683 greg 1.14 if (childpid == -1)
684     return;
685     if (fclose(psend) == EOF)
686     syserror();
687 greg 1.13 clearqueue();
688     fclose(precv);
689     while ((pid = wait(0)) != -1 && pid != childpid)
690     ;
691 greg 1.14 childpid = -1;
692 greg 1.13 }
693    
694    
695 greg 1.14 rcalfill(x, y) /* fill with ray-calculated pixel */
696 greg 1.13 int x, y;
697     {
698     FVECT orig, dir;
699     float outbuf[6];
700    
701     if (queuesiz >= PACKSIZ) { /* flush queue */
702 greg 1.14 if (fflush(psend) == EOF)
703     syserror();
704 greg 1.13 clearqueue();
705     }
706     /* send new ray */
707     rayview(orig, dir, &ourview, x+.5, y+.5);
708     outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
709     outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
710 greg 1.14 if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
711     syserror();
712 greg 1.13 /* remember it */
713     queue[queuesiz][0] = x;
714     queue[queuesiz][1] = y;
715     queuesiz++;
716     }
717    
718    
719     clearqueue() /* get results from queue */
720     {
721     float inbuf[4];
722     register int i;
723    
724     for (i = 0; i < queuesiz; i++) {
725 greg 1.14 if (fread(inbuf, sizeof(float), 4, precv) < 4) {
726     fprintf(stderr, "%s: read error in clearqueue\n",
727     progname);
728     exit(1);
729 greg 1.16 }
730     if (ourexp > 0 && ourexp != 1.0) {
731     inbuf[0] *= ourexp;
732     inbuf[1] *= ourexp;
733     inbuf[2] *= ourexp;
734 greg 1.14 }
735 greg 1.13 setcolr(pscan(queue[i][1])[queue[i][0]],
736     inbuf[0], inbuf[1], inbuf[2]);
737     zscan(queue[i][1])[queue[i][0]] = inbuf[3];
738     }
739     queuesiz = 0;
740 greg 1.14 }
741    
742    
743     syserror() /* report error and exit */
744     {
745     perror(progname);
746     exit(1);
747 greg 1.1 }