ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 1.18
Committed: Tue Jan 9 11:39:17 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +9 -1 lines
Log Message:
added maintenance of pixel aspect ratio

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