ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 1.33
Committed: Thu Apr 18 14:35:26 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.32: +13 -4 lines
Log Message:
added format information to headers

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