ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 1.24
Committed: Mon Feb 26 14:01:44 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.23: +1 -1 lines
Log Message:
changed <sys/fcntl.h> to <fcntl.h>

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