ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 2.15
Committed: Thu Dec 22 19:11:01 1994 UTC (29 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.14: +39 -9 lines
Log Message:
made aft clipping work(?) and fixed bug in movepixel()

File Contents

# User Rev Content
1 greg 2.14 /* Copyright (c) 1994 Regents of the University of California */
2 greg 1.34
3 greg 1.1 #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Interpolate and extrapolate pictures with different view parameters.
9     *
10     * Greg Ward 09Dec89
11     */
12    
13     #include "standard.h"
14    
15 greg 2.3 #include <ctype.h>
16    
17 greg 1.1 #include "view.h"
18    
19     #include "color.h"
20    
21 greg 1.34 #include "resolu.h"
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 2.3 #define PACKSIZ 256 /* max. calculation packet size */
30 greg 1.13
31 greg 2.4 #define RTCOM "rtrace -h- -ovl -fff "
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.34 RESOLU tresolu; /* input resolution */
62 greg 1.17 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 2.3 int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */
67     #define childpid (PDesc[2])
68     unsigned short queue[PACKSIZ][2]; /* pending pixels */
69     int packsiz; /* actual packet size */
70 greg 1.13 int queuesiz; /* number of pixels pending */
71 greg 1.5
72 greg 1.13
73 greg 1.1 main(argc, argv) /* interpolate pictures */
74     int argc;
75     char *argv[];
76     {
77 greg 2.3 #define check(ol,al) if (argv[i][ol] || \
78     badarg(argc-i-1,argv+i+1,al)) \
79     goto badopt
80 greg 1.1 int gotvfile = 0;
81 greg 1.11 char *zfile = NULL;
82 greg 1.1 char *err;
83 greg 1.17 int i, rval;
84 greg 1.1
85     progname = argv[0];
86    
87 greg 1.17 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
88     rval = getviewopt(&ourview, argc-i, argv+i);
89     if (rval >= 0) {
90     i += rval;
91     continue;
92     }
93 greg 1.1 switch (argv[i][1]) {
94     case 't': /* threshold */
95 greg 2.3 check(2,"f");
96 greg 1.1 zeps = atof(argv[++i]);
97     break;
98 greg 1.11 case 'n': /* dist. normalized? */
99 greg 2.3 check(2,NULL);
100 greg 1.11 normdist = !normdist;
101 greg 1.10 break;
102 greg 1.11 case 'f': /* fill type */
103     switch (argv[i][2]) {
104     case '0': /* none */
105 greg 2.3 check(3,NULL);
106 greg 1.25 fillo = 0;
107 greg 1.11 break;
108     case 'f': /* foreground */
109 greg 2.3 check(3,NULL);
110 greg 1.25 fillo = F_FORE;
111 greg 1.11 break;
112     case 'b': /* background */
113 greg 2.3 check(3,NULL);
114 greg 1.25 fillo = F_BACK;
115 greg 1.11 break;
116     case 'a': /* all */
117 greg 2.3 check(3,NULL);
118 greg 1.25 fillo = F_FORE|F_BACK;
119 greg 1.11 break;
120 greg 1.25 case 's': /* sample */
121 greg 2.3 check(3,"i");
122 greg 1.26 fillsamp = atoi(argv[++i]);
123 greg 1.25 break;
124 greg 1.11 case 'c': /* color */
125 greg 2.3 check(3,"fff");
126 greg 1.25 fillfunc = backfill;
127 greg 1.11 setcolr(backcolr, atof(argv[i+1]),
128     atof(argv[i+2]), atof(argv[i+3]));
129     i += 3;
130     break;
131 greg 1.13 case 'z': /* z value */
132 greg 2.3 check(3,"f");
133 greg 1.25 fillfunc = backfill;
134 greg 1.13 backz = atof(argv[++i]);
135     break;
136     case 'r': /* rtrace */
137 greg 2.3 check(3,"s");
138 greg 1.25 fillfunc = rcalfill;
139 greg 1.13 calstart(RTCOM, argv[++i]);
140     break;
141 greg 1.11 default:
142     goto badopt;
143     }
144     break;
145     case 'z': /* z file */
146 greg 2.3 check(2,"s");
147 greg 1.11 zfile = argv[++i];
148     break;
149 greg 1.6 case 'x': /* x resolution */
150 greg 2.3 check(2,"i");
151 greg 1.17 hresolu = atoi(argv[++i]);
152 greg 1.6 break;
153     case 'y': /* y resolution */
154 greg 2.3 check(2,"i");
155 greg 1.17 vresolu = atoi(argv[++i]);
156 greg 1.6 break;
157 greg 1.18 case 'p': /* pixel aspect */
158 greg 2.6 if (argv[i][2] != 'a')
159     goto badopt;
160     check(3,"f");
161 greg 1.18 pixaspect = atof(argv[++i]);
162     break;
163 greg 1.17 case 'v': /* view file */
164     if (argv[i][2] != 'f')
165 greg 1.1 goto badopt;
166 greg 2.3 check(3,"s");
167 greg 1.19 gotvfile = viewfile(argv[++i], &ourview, 0, 0);
168 greg 2.10 if (gotvfile < 0)
169     syserror(argv[i]);
170     else if (gotvfile == 0) {
171 greg 1.17 fprintf(stderr, "%s: bad view file\n",
172     argv[i]);
173     exit(1);
174 greg 1.1 }
175     break;
176     default:
177     badopt:
178 greg 1.10 fprintf(stderr, "%s: command line error at '%s'\n",
179 greg 1.1 progname, argv[i]);
180 greg 1.10 goto userr;
181 greg 1.1 }
182 greg 1.17 }
183 greg 1.1 /* check arguments */
184 greg 1.13 if ((argc-i)%2)
185 greg 1.10 goto userr;
186 greg 2.3 if (fillsamp == 1)
187     fillo &= ~F_BACK;
188 greg 1.1 /* set view */
189 greg 2.15 if ((err = setview(&ourview)) != NULL) {
190 greg 1.1 fprintf(stderr, "%s: %s\n", progname, err);
191     exit(1);
192     }
193 greg 1.18 normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
194 greg 1.1 /* allocate frame */
195 greg 2.7 ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
196     ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
197 greg 1.15 if (ourpict == NULL || ourzbuf == NULL)
198 greg 2.10 syserror(progname);
199 greg 2.7 bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
200 greg 2.13 /* new header */
201     newheader("RADIANCE", stdout);
202 greg 1.1 /* get input */
203     for ( ; i < argc; i += 2)
204     addpicture(argv[i], argv[i+1]);
205     /* fill in spaces */
206 greg 1.25 if (fillo&F_BACK)
207 greg 1.26 backpicture(fillfunc, fillsamp);
208 greg 1.12 else
209 greg 1.25 fillpicture(fillfunc);
210 greg 1.13 /* close calculation */
211 greg 1.14 caldone();
212 greg 2.15 /* aft clipping */
213     clipaft();
214 greg 1.1 /* add to header */
215     printargs(argc, argv, stdout);
216     if (gotvfile) {
217 greg 1.25 fputs(VIEWSTR, stdout);
218 greg 1.1 fprintview(&ourview, stdout);
219 greg 1.25 putc('\n', stdout);
220 greg 1.1 }
221 greg 1.18 if (pixaspect < .99 || pixaspect > 1.01)
222     fputaspect(pixaspect, stdout);
223     if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
224 greg 1.16 fputexpos(ourexp, stdout);
225 greg 1.33 fputformat(COLRFMT, stdout);
226 greg 1.25 putc('\n', stdout);
227 greg 1.11 /* write picture */
228 greg 1.1 writepicture();
229 greg 1.11 /* write z file */
230     if (zfile != NULL)
231     writedistance(zfile);
232 greg 1.1
233     exit(0);
234 greg 1.10 userr:
235     fprintf(stderr,
236 greg 1.17 "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
237 greg 1.10 progname);
238     exit(1);
239 greg 1.1 #undef check
240     }
241    
242    
243     headline(s) /* process header string */
244     char *s;
245     {
246 greg 1.33 char fmt[32];
247 greg 1.1
248 greg 2.13 if (isheadid(s))
249     return;
250     if (formatval(fmt, s)) {
251 greg 1.33 wrongformat = strcmp(fmt, COLRFMT);
252     return;
253     }
254 greg 1.25 putc('\t', stdout);
255     fputs(s, stdout);
256 greg 1.1
257 greg 1.16 if (isexpos(s)) {
258     theirexp *= exposval(s);
259     return;
260     }
261 greg 2.5 if (isview(s) && sscanview(&theirview, s) > 0)
262     gotview++;
263 greg 1.1 }
264    
265    
266 greg 1.10 addpicture(pfile, zspec) /* add picture to output */
267     char *pfile, *zspec;
268 greg 1.1 {
269 greg 1.19 FILE *pfp;
270     int zfd;
271 greg 1.8 char *err;
272 greg 1.1 COLR *scanin;
273 greg 1.15 float *zin;
274     struct position *plast;
275 greg 1.1 int y;
276 greg 1.10 /* open picture file */
277 greg 2.10 if ((pfp = fopen(pfile, "r")) == NULL)
278     syserror(pfile);
279 greg 1.16 /* get header with exposure and view */
280     theirexp = 1.0;
281     gotview = 0;
282 greg 1.1 printf("%s:\n", pfile);
283 greg 1.33 getheader(pfp, headline, NULL);
284 greg 1.34 if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
285 greg 1.33 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.34 scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
300     zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
301     plast = (struct position *)calloc(scanlen(&tresolu),
302     sizeof(struct position));
303 greg 1.15 if (scanin == NULL || zin == NULL || plast == NULL)
304 greg 2.10 syserror(progname);
305 greg 1.10 /* get z specification or file */
306 greg 1.19 if ((zfd = open(zspec, O_RDONLY)) == -1) {
307 greg 1.10 double zvalue;
308     register int x;
309 greg 2.10 if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
310     syserror(zspec);
311 greg 1.34 for (x = scanlen(&tresolu); x-- > 0; )
312 greg 1.10 zin[x] = zvalue;
313     }
314 greg 1.1 /* load image */
315 greg 1.34 for (y = 0; y < numscans(&tresolu); y++) {
316     if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
317 greg 1.1 fprintf(stderr, "%s: read error\n", pfile);
318     exit(1);
319     }
320 greg 1.34 if (zfd != -1 && read(zfd,(char *)zin,
321     scanlen(&tresolu)*sizeof(float))
322     < scanlen(&tresolu)*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.30 FVECT pos;
386 greg 1.15 struct position lastx, newpos;
387 greg 1.1 register int x;
388    
389 greg 1.20 lastx.z = 0;
390 greg 1.34 for (x = scanlen(&tresolu); x-- > 0; ) {
391     pix2loc(pos, &tresolu, x, y);
392 greg 1.5 pos[2] = zline[x];
393 greg 1.30 if (movepixel(pos) < 0) {
394 greg 1.15 lasty[x].z = lastx.z = 0; /* mark invalid */
395 greg 1.1 continue;
396 greg 1.15 }
397 greg 1.17 newpos.x = pos[0] * hresolu;
398     newpos.y = pos[1] * vresolu;
399 greg 1.15 newpos.z = zline[x];
400 greg 1.8 /* add pixel to our image */
401 greg 1.17 if (pos[0] >= 0 && newpos.x < hresolu
402     && pos[1] >= 0 && newpos.y < vresolu) {
403 greg 1.15 addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
404     lasty[x].x = lastx.x = newpos.x;
405     lasty[x].y = lastx.y = newpos.y;
406     lasty[x].z = lastx.z = newpos.z;
407     } else
408     lasty[x].z = lastx.z = 0; /* mark invalid */
409 greg 1.1 }
410     }
411    
412    
413 greg 1.15 addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */
414     struct position *p0, *p1, *p2;
415 greg 1.8 COLR pix;
416     double z;
417     {
418 greg 1.15 double zt = 2.*zeps*p0->z; /* threshold */
419     int s1x, s1y, s2x, s2y; /* step sizes */
420     int l1, l2, c1, c2; /* side lengths and counters */
421     int p1isy; /* p0p1 along y? */
422     int x1, y1; /* p1 position */
423     register int x, y; /* final position */
424    
425     /* compute vector p0p1 */
426 greg 1.25 if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
427 greg 1.15 s1x = p1->x - p0->x;
428     s1y = p1->y - p0->y;
429     l1 = ABS(s1x);
430     if (p1isy = (ABS(s1y) > l1))
431     l1 = ABS(s1y);
432     } else {
433     l1 = s1x = s1y = 1;
434     p1isy = -1;
435     }
436     /* compute vector p0p2 */
437 greg 1.25 if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
438 greg 1.15 s2x = p2->x - p0->x;
439     s2y = p2->y - p0->y;
440     if (p1isy == 1)
441     l2 = ABS(s2x);
442     else {
443     l2 = ABS(s2y);
444     if (p1isy != 0 && ABS(s2x) > l2)
445     l2 = ABS(s2x);
446     }
447     } else
448     l2 = s2x = s2y = 1;
449     /* fill the parallelogram */
450     for (c1 = l1; c1-- > 0; ) {
451     x1 = p0->x + c1*s1x/l1;
452     y1 = p0->y + c1*s1y/l1;
453     for (c2 = l2; c2-- > 0; ) {
454     x = x1 + c2*s2x/l2;
455 greg 1.28 if (x < 0 || x >= hresolu)
456     continue;
457 greg 1.15 y = y1 + c2*s2y/l2;
458 greg 1.28 if (y < 0 || y >= vresolu)
459     continue;
460 greg 1.15 if (zscan(y)[x] <= 0 || zscan(y)[x]-z
461     > zeps*zscan(y)[x]) {
462 greg 1.8 zscan(y)[x] = z;
463     copycolr(pscan(y)[x], pix);
464     }
465 greg 1.15 }
466     }
467 greg 1.30 }
468    
469    
470     movepixel(pos) /* reposition image point */
471     FVECT pos;
472     {
473 greg 2.15 double d0, d1;
474 greg 1.30 FVECT pt, direc;
475    
476 greg 1.32 if (pos[2] <= 0) /* empty pixel */
477     return(-1);
478 greg 2.15 if (normdist && theirview.type == VT_PER) { /* adjust distance */
479     d0 = pos[0] + theirview.hoff - .5;
480     d1 = pos[1] + theirview.voff - .5;
481     pos[2] /= sqrt(1. + d0*d0*theirview.hn2 + d1*d1*theirview.vn2);
482     }
483 greg 1.30 if (hasmatrix) {
484     pos[0] += theirview.hoff - .5;
485     pos[1] += theirview.voff - .5;
486     if (theirview.type == VT_PER) {
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 greg 2.14 if (viewray(pt, direc, &theirview, pos[0], pos[1]) < -FTINY)
502 greg 1.30 return(-1);
503     pt[0] += direc[0]*pos[2];
504     pt[1] += direc[1]*pos[2];
505     pt[2] += direc[2]*pos[2];
506 greg 1.34 viewloc(pos, &ourview, pt);
507 greg 1.30 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 greg 2.10 syserror(progname);
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 2.15 }
618    
619    
620     clipaft() /* perform aft clipping as indicated */
621     {
622     register int x, y;
623     double tstdist;
624     double yzn2, vx;
625    
626     if (ourview.vaft <= FTINY)
627     return;
628     tstdist = ourview.vaft;
629     for (y = 0; y < vresolu; y++) {
630     if (ourview.type == VT_PER) { /* adjust distance */
631     yzn2 = (y+.5)/vresolu + ourview.voff - .5;
632     yzn2 = 1. + yzn2*yzn2*ourview.vn2;
633     tstdist = ourview.vaft * sqrt(yzn2);
634     }
635     for (x = 0; x < hresolu; x++)
636     if (zscan(y)[x] > tstdist) {
637     if (ourview.type == VT_PER) {
638     vx = (x+.5)/hresolu + ourview.hoff - .5;
639     if (zscan(y)[x] <= ourview.vaft *
640     sqrt(vx*vx*ourview.hn2 + yzn2))
641     continue;
642     }
643     bzero(pscan(y)[x], sizeof(COLR));
644     zscan(y)[x] = 0.0;
645     }
646     }
647 greg 1.11 }
648    
649    
650 greg 1.1 writepicture() /* write out picture */
651     {
652     int y;
653    
654 greg 1.34 fprtresolu(hresolu, vresolu, stdout);
655 greg 1.17 for (y = vresolu-1; y >= 0; y--)
656     if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
657 greg 2.10 syserror(progname);
658 greg 1.11 }
659    
660    
661     writedistance(fname) /* write out z file */
662     char *fname;
663     {
664     int donorm = normdist && ourview.type == VT_PER;
665 greg 1.19 int fd;
666 greg 1.11 int y;
667     float *zout;
668    
669 greg 2.10 if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
670     syserror(fname);
671 greg 1.11 if (donorm
672 greg 1.17 && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
673 greg 2.10 syserror(progname);
674 greg 1.17 for (y = vresolu-1; y >= 0; y--) {
675 greg 1.11 if (donorm) {
676     double vx, yzn2;
677     register int x;
678 greg 1.29 yzn2 = (y+.5)/vresolu + ourview.voff - .5;
679 greg 1.17 yzn2 = 1. + yzn2*yzn2*ourview.vn2;
680     for (x = 0; x < hresolu; x++) {
681 greg 1.29 vx = (x+.5)/hresolu + ourview.hoff - .5;
682 greg 1.11 zout[x] = zscan(y)[x]
683 greg 1.17 * sqrt(vx*vx*ourview.hn2 + yzn2);
684 greg 1.11 }
685     } else
686     zout = zscan(y);
687 greg 1.23 if (write(fd, (char *)zout, hresolu*sizeof(float))
688 greg 2.10 < hresolu*sizeof(float))
689     syserror(fname);
690 greg 1.11 }
691     if (donorm)
692     free((char *)zout);
693 greg 1.19 close(fd);
694 greg 1.10 }
695    
696    
697     isfloat(s) /* see if string is floating number */
698     register char *s;
699     {
700     for ( ; *s; s++)
701     if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
702     && *s != 'e' && *s != 'E' && *s != '+')
703     return(0);
704     return(1);
705 greg 1.12 }
706    
707    
708     backfill(x, y) /* fill pixel with background */
709     int x, y;
710     {
711     register BYTE *dest = pscan(y)[x];
712    
713     copycolr(dest, backcolr);
714 greg 1.13 zscan(y)[x] = backz;
715     }
716    
717    
718 greg 2.3 calstart(prog, args) /* start fill calculation */
719 greg 1.13 char *prog, *args;
720     {
721     char combuf[512];
722 greg 2.3 char *argv[64];
723     int rval;
724     register char **wp, *cp;
725 greg 1.13
726 greg 1.14 if (childpid != -1) {
727     fprintf(stderr, "%s: too many calculations\n", progname);
728     exit(1);
729     }
730 greg 2.3 strcpy(combuf, prog);
731     strcat(combuf, args);
732     cp = combuf;
733     wp = argv;
734     for ( ; ; ) {
735 greg 2.11 while (isspace(*cp)) /* nullify spaces */
736     *cp++ = '\0';
737     if (!*cp) /* all done? */
738     break;
739     *wp++ = cp; /* add argument to list */
740     while (*++cp && !isspace(*cp))
741     ;
742 greg 2.3 }
743     *wp = NULL;
744     /* start process */
745     if ((rval = open_process(PDesc, argv)) < 0)
746 greg 2.10 syserror(progname);
747 greg 2.3 if (rval == 0) {
748     fprintf(stderr, "%s: command not found\n", argv[0]);
749     exit(1);
750 greg 1.13 }
751 greg 2.3 packsiz = rval/(6*sizeof(float)) - 1;
752     if (packsiz > PACKSIZ)
753     packsiz = PACKSIZ;
754 greg 1.13 queuesiz = 0;
755     }
756    
757    
758 greg 2.3 caldone() /* done with calculation */
759 greg 1.13 {
760 greg 1.14 if (childpid == -1)
761     return;
762 greg 1.25 clearqueue();
763 greg 2.3 close_process(PDesc);
764 greg 1.14 childpid = -1;
765 greg 1.13 }
766    
767    
768 greg 1.14 rcalfill(x, y) /* fill with ray-calculated pixel */
769 greg 1.13 int x, y;
770     {
771 greg 2.3 if (queuesiz >= packsiz) /* flush queue if needed */
772 greg 1.25 clearqueue();
773 greg 1.22 /* add position to queue */
774 greg 1.13 queue[queuesiz][0] = x;
775     queue[queuesiz][1] = y;
776     queuesiz++;
777     }
778    
779    
780 greg 1.25 clearqueue() /* process queue */
781 greg 1.13 {
782 greg 1.22 FVECT orig, dir;
783 greg 2.3 float fbuf[6*(PACKSIZ+1)];
784     register float *fbp;
785 greg 1.13 register int i;
786    
787 greg 2.10 if (queuesiz == 0)
788     return;
789 greg 2.3 fbp = fbuf;
790 greg 1.13 for (i = 0; i < queuesiz; i++) {
791 greg 1.22 viewray(orig, dir, &ourview,
792     (queue[i][0]+.5)/hresolu,
793     (queue[i][1]+.5)/vresolu);
794 greg 2.3 *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
795     *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
796 greg 1.22 }
797 greg 2.3 /* mark end and get results */
798     bzero((char *)fbp, 6*sizeof(float));
799     if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
800     6*sizeof(float)*(queuesiz+1)) !=
801     4*sizeof(float)*queuesiz) {
802     fprintf(stderr, "%s: error reading from rtrace process\n",
803     progname);
804     exit(1);
805     }
806     fbp = fbuf;
807 greg 1.22 for (i = 0; i < queuesiz; i++) {
808 greg 1.16 if (ourexp > 0 && ourexp != 1.0) {
809 greg 2.3 fbp[0] *= ourexp;
810     fbp[1] *= ourexp;
811     fbp[2] *= ourexp;
812 greg 1.14 }
813 greg 1.13 setcolr(pscan(queue[i][1])[queue[i][0]],
814 greg 2.3 fbp[0], fbp[1], fbp[2]);
815     zscan(queue[i][1])[queue[i][0]] = fbp[3];
816     fbp += 4;
817 greg 1.13 }
818     queuesiz = 0;
819 greg 1.14 }
820    
821    
822 greg 2.10 syserror(s) /* report error and exit */
823     char *s;
824 greg 1.14 {
825 greg 2.10 perror(s);
826 greg 1.14 exit(1);
827 greg 1.1 }