ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
(Generate patch)

Comparing ray/src/px/pinterp.c (file contents):
Revision 1.21 by greg, Wed Jan 17 15:12:37 1990 UTC vs.
Revision 2.35 by schorsch, Mon Jun 30 14:59:12 2003 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
4
4   /*
5   * Interpolate and extrapolate pictures with different view parameters.
6   *
7   *      Greg Ward       09Dec89
8   */
9  
10 < #include "standard.h"
10 > #include "copyright.h"
11  
12 < #include <sys/fcntl.h>
12 > #include <ctype.h>
13 > #include <string.h>
14  
15 + #include "standard.h"
16 + #include "rtprocess.h" /* Windows: must come before color.h */
17   #include "view.h"
16
18   #include "color.h"
19  
20 < #ifndef BSD
20 < #define vfork           fork
21 < #endif
20 > #define LOG2            0.69314718055994530942
21  
22   #define pscan(y)        (ourpict+(y)*hresolu)
23 + #define sscan(y)        (ourspict+(y)*hresolu)
24 + #define wscan(y)        (ourweigh+(y)*hresolu)
25   #define zscan(y)        (ourzbuf+(y)*hresolu)
26 + #define bscan(y)        (ourbpict+(y)*hresolu)
27 + #define averaging       (ourweigh != NULL)
28 + #define blurring        (ourbpict != NULL)
29 + #define usematrix       (hasmatrix & !averaging)
30 + #define zisnorm         (!usematrix | ourview.type != VT_PER)
31  
32 + #define MAXWT           1000.           /* maximum pixel weight (averaging) */
33 +
34   #define F_FORE          1               /* fill foreground */
35   #define F_BACK          2               /* fill background */
36  
37 < #define PACKSIZ         42              /* calculation packet size */
37 > #define PACKSIZ         256             /* max. calculation packet size */
38  
39 < #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
39 > #define RTCOM           "rtrace -h- -ovl -fff -ld- -i- -I- "
40  
41   #define ABS(x)          ((x)>0?(x):-(x))
42  
43   struct position {int x,y; float z;};
44  
45 + #define NSTEPS          64              /* number steps in overlap prescan */
46 + #define MINSTEP         4               /* minimum worthwhile preview step */
47 +
48 + struct bound {int min,max;};
49 +
50   VIEW    ourview = STDVIEW;              /* desired view */
51   int     hresolu = 512;                  /* horizontal resolution */
52   int     vresolu = 512;                  /* vertical resolution */
# Line 41 | Line 54 | double pixaspect = 1.0;                /* pixel aspect ratio */
54  
55   double  zeps = .02;                     /* allowed z epsilon */
56  
57 < COLR    *ourpict;                       /* output picture */
57 > COLR    *ourpict;                       /* output picture (COLR's) */
58 > COLOR   *ourspict;                      /* output pixel sums (averaging) */
59 > float   *ourweigh = NULL;               /* output pixel weights (averaging) */
60   float   *ourzbuf;                       /* corresponding z-buffer */
61 + COLOR   *ourbpict = NULL;               /* blurred picture (view averaging) */
62  
63 + VIEW    avgview;                        /* average view for -B option */
64 + int     nvavg;                          /* number of views averaged */
65 +
66   char    *progname;
67  
68 < int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
68 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
69 > int     fillsamp = 0;                   /* sample separation (0 == inf) */
70   extern int      backfill(), rcalfill(); /* fill functions */
71 < int     (*deffill)() = backfill;        /* selected fill function */
71 > int     (*fillfunc)() = backfill;       /* selected fill function */
72   COLR    backcolr = BLKCOLR;             /* background color */
73 + COLOR   backcolor = BLKCOLOR;           /* background color (float) */
74   double  backz = 0.0;                    /* background z value */
75 < int     normdist = 1;                   /* normalized distance? */
76 < double  ourexp = -1;                    /* output picture exposure */
75 > int     normdist = 1;                   /* i/o normalized distance? */
76 > char    ourfmt[LPICFMT+1] = PICFMT;     /* original picture format */
77 > double  ourexp = -1;                    /* original picture exposure */
78 > int     expadj = 0;                     /* exposure adjustment (f-stops) */
79 > double  rexpadj = 1;                    /* real exposure adjustment */
80  
81 < VIEW    theirview = STDVIEW;            /* input view */
81 > VIEW    theirview;                      /* input view */
82   int     gotview;                        /* got input view? */
83 < int     thresolu, tvresolu;             /* input resolution */
83 > int     wrongformat = 0;                /* input in another format? */
84 > RESOLU  tresolu;                        /* input resolution */
85   double  theirexp;                       /* input picture exposure */
86 < double  theirs2ours[4][4];              /* transformation matrix */
86 > MAT4    theirs2ours;                    /* transformation matrix */
87 > int     hasmatrix = 0;                  /* has transformation matrix */
88  
89 < int     childpid = -1;                  /* id of fill process */
90 < FILE    *psend, *precv;                 /* pipes to/from fill calculation */
91 < int     queue[PACKSIZ][2];              /* pending pixels */
92 < int     queuesiz;                       /* number of pixels pending */
89 > static SUBPROC PDesc = SP_INACTIVE; /* rtrace process descriptor */
90 > unsigned short  queue[PACKSIZ][2];      /* pending pixels */
91 > int     packsiz;                        /* actual packet size */
92 > int     queuesiz = 0;                   /* number of pixels pending */
93  
94 + extern double   movepixel();
95  
96 +
97   main(argc, argv)                        /* interpolate pictures */
98   int     argc;
99   char    *argv[];
100   {
101 < #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
102 <        extern double   atof();
101 > #define  check(ol,al)           if (argv[an][ol] || \
102 >                                badarg(argc-an-1,argv+an+1,al)) \
103 >                                goto badopt
104          int     gotvfile = 0;
105 +        int     doavg = -1;
106 +        int     doblur = 0;
107          char    *zfile = NULL;
108 <        char    *err;
109 <        int     i, rval;
108 >        char    *expcomp = NULL;
109 >        int     i, an, rval;
110  
111          progname = argv[0];
112  
113 <        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
114 <                rval = getviewopt(&ourview, argc-i, argv+i);
113 >        for (an = 1; an < argc && argv[an][0] == '-'; an++) {
114 >                rval = getviewopt(&ourview, argc-an, argv+an);
115                  if (rval >= 0) {
116 <                        i += rval;
116 >                        an += rval;
117                          continue;
118                  }
119 <                switch (argv[i][1]) {
119 >                switch (argv[an][1]) {
120 >                case 'e':                               /* exposure */
121 >                        check(2,"f");
122 >                        expcomp = argv[++an];
123 >                        break;
124                  case 't':                               /* threshold */
125 <                        check(2,1);
126 <                        zeps = atof(argv[++i]);
125 >                        check(2,"f");
126 >                        zeps = atof(argv[++an]);
127                          break;
128 +                case 'a':                               /* average */
129 +                        check(2,NULL);
130 +                        doavg = 1;
131 +                        break;
132 +                case 'B':                               /* blur views */
133 +                        check(2,NULL);
134 +                        doblur = 1;
135 +                        break;
136 +                case 'q':                               /* quick (no avg.) */
137 +                        check(2,NULL);
138 +                        doavg = 0;
139 +                        break;
140                  case 'n':                               /* dist. normalized? */
141 <                        check(2,0);
141 >                        check(2,NULL);
142                          normdist = !normdist;
143                          break;
144                  case 'f':                               /* fill type */
145 <                        switch (argv[i][2]) {
145 >                        switch (argv[an][2]) {
146                          case '0':                               /* none */
147 <                                check(3,0);
148 <                                fill = 0;
147 >                                check(3,NULL);
148 >                                fillo = 0;
149                                  break;
150                          case 'f':                               /* foreground */
151 <                                check(3,0);
152 <                                fill = F_FORE;
151 >                                check(3,NULL);
152 >                                fillo = F_FORE;
153                                  break;
154                          case 'b':                               /* background */
155 <                                check(3,0);
156 <                                fill = F_BACK;
155 >                                check(3,NULL);
156 >                                fillo = F_BACK;
157                                  break;
158                          case 'a':                               /* all */
159 <                                check(3,0);
160 <                                fill = F_FORE|F_BACK;
159 >                                check(3,NULL);
160 >                                fillo = F_FORE|F_BACK;
161                                  break;
162 +                        case 's':                               /* sample */
163 +                                check(3,"i");
164 +                                fillsamp = atoi(argv[++an]);
165 +                                break;
166                          case 'c':                               /* color */
167 <                                check(3,3);
168 <                                deffill = backfill;
169 <                                setcolr(backcolr, atof(argv[i+1]),
170 <                                        atof(argv[i+2]), atof(argv[i+3]));
171 <                                i += 3;
167 >                                check(3,"fff");
168 >                                fillfunc = backfill;
169 >                                setcolor(backcolor, atof(argv[an+1]),
170 >                                        atof(argv[an+2]), atof(argv[an+3]));
171 >                                setcolr(backcolr, colval(backcolor,RED),
172 >                                                colval(backcolor,GRN),
173 >                                                colval(backcolor,BLU));
174 >                                an += 3;
175                                  break;
176                          case 'z':                               /* z value */
177 <                                check(3,1);
178 <                                deffill = backfill;
179 <                                backz = atof(argv[++i]);
177 >                                check(3,"f");
178 >                                fillfunc = backfill;
179 >                                backz = atof(argv[++an]);
180                                  break;
181                          case 'r':                               /* rtrace */
182 <                                check(3,1);
183 <                                deffill = rcalfill;
184 <                                calstart(RTCOM, argv[++i]);
182 >                                check(3,"s");
183 >                                fillfunc = rcalfill;
184 >                                calstart(RTCOM, argv[++an]);
185                                  break;
186                          default:
187                                  goto badopt;
188                          }
189                          break;
190                  case 'z':                               /* z file */
191 <                        check(2,1);
192 <                        zfile = argv[++i];
191 >                        check(2,"s");
192 >                        zfile = argv[++an];
193                          break;
194                  case 'x':                               /* x resolution */
195 <                        check(2,1);
196 <                        hresolu = atoi(argv[++i]);
195 >                        check(2,"i");
196 >                        hresolu = atoi(argv[++an]);
197                          break;
198                  case 'y':                               /* y resolution */
199 <                        check(2,1);
200 <                        vresolu = atoi(argv[++i]);
199 >                        check(2,"i");
200 >                        vresolu = atoi(argv[++an]);
201                          break;
202                  case 'p':                               /* pixel aspect */
203 <                        check(2,1);
204 <                        pixaspect = atof(argv[++i]);
203 >                        if (argv[an][2] != 'a')
204 >                                goto badopt;
205 >                        check(3,"f");
206 >                        pixaspect = atof(argv[++an]);
207                          break;
208                  case 'v':                               /* view file */
209 <                        if (argv[i][2] != 'f')
209 >                        if (argv[an][2] != 'f')
210                                  goto badopt;
211 <                        check(3,1);
212 <                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
213 <                        if (gotvfile < 0) {
214 <                                perror(argv[i]);
215 <                                exit(1);
160 <                        } else if (gotvfile == 0) {
211 >                        check(3,"s");
212 >                        gotvfile = viewfile(argv[++an], &ourview, NULL);
213 >                        if (gotvfile < 0)
214 >                                syserror(argv[an]);
215 >                        else if (gotvfile == 0) {
216                                  fprintf(stderr, "%s: bad view file\n",
217 <                                                argv[i]);
217 >                                                argv[an]);
218                                  exit(1);
219                          }
220                          break;
221                  default:
222                  badopt:
223                          fprintf(stderr, "%s: command line error at '%s'\n",
224 <                                        progname, argv[i]);
224 >                                        progname, argv[an]);
225                          goto userr;
226                  }
227          }
228                                                  /* check arguments */
229 <        if ((argc-i)%2)
229 >        if ((argc-an)%2)
230                  goto userr;
231 +        if (fillsamp == 1)
232 +                fillo &= ~F_BACK;
233 +        if (doavg < 0)
234 +                doavg = (argc-an) > 2;
235 +        if (expcomp != NULL)
236 +                if (expcomp[0] == '+' | expcomp[0] == '-') {
237 +                        expadj = atof(expcomp) + (expcomp[0]=='+' ? .5 : -.5);
238 +                        if (doavg | doblur)
239 +                                rexpadj = pow(2.0, atof(expcomp));
240 +                        else
241 +                                rexpadj = pow(2.0, (double)expadj);
242 +                } else {
243 +                        if (!isflt(expcomp))
244 +                                goto userr;
245 +                        rexpadj = atof(expcomp);
246 +                        expadj = log(rexpadj)/LOG2 + (rexpadj>1 ? .5 : -.5);
247 +                        if (!(doavg | doblur))
248 +                                rexpadj = pow(2.0, (double)expadj);
249 +                }
250                                                  /* set view */
251 <        if (err = setview(&ourview)) {
252 <                fprintf(stderr, "%s: %s\n", progname, err);
251 >        if (nextview(doblur ? stdin : (FILE *)NULL) == EOF) {
252 >                fprintf(stderr, "%s: no view on standard input!\n",
253 >                                progname);
254                  exit(1);
255          }
256          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
257                                                  /* allocate frame */
258 <        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
259 <        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
260 <        if (ourpict == NULL || ourzbuf == NULL)
261 <                syserror();
262 <                                                        /* get input */
263 <        for ( ; i < argc; i += 2)
264 <                addpicture(argv[i], argv[i+1]);
265 <                                                        /* fill in spaces */
266 <        if (fill&F_BACK)
267 <                backpicture();
268 <        else
269 <                fillpicture();
258 >        if (doavg) {
259 >                ourspict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR));
260 >                ourweigh = (float *)bmalloc(hresolu*vresolu*sizeof(float));
261 >                if (ourspict == NULL | ourweigh == NULL)
262 >                        syserror(progname);
263 >        } else {
264 >                ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
265 >                if (ourpict == NULL)
266 >                        syserror(progname);
267 >        }
268 >        if (doblur) {
269 >                ourbpict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR));
270 >                if (ourbpict == NULL)
271 >                        syserror(progname);
272 >        }
273 >        ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
274 >        if (ourzbuf == NULL)
275 >                syserror(progname);
276 >                                                        /* new header */
277 >        newheader("RADIANCE", stdout);
278 >        fputnow(stdout);
279 >                                                        /* run pictures */
280 >        do {
281 >                memset((char *)ourzbuf, '\0', hresolu*vresolu*sizeof(float));
282 >                for (i = an; i < argc; i += 2)
283 >                        addpicture(argv[i], argv[i+1]);
284 >                if (fillo&F_BACK)                       /* fill in spaces */
285 >                        backpicture(fillfunc, fillsamp);
286 >                else
287 >                        fillpicture(fillfunc);
288 >                                                        /* aft clipping */
289 >                clipaft();
290 >        } while (addblur() && nextview(stdin) != EOF);
291                                                          /* close calculation */
292          caldone();
293                                                          /* add to header */
294          printargs(argc, argv, stdout);
295 <        if (gotvfile) {
296 <                printf(VIEWSTR);
297 <                fprintview(&ourview, stdout);
298 <                printf("\n");
295 >        compavgview();
296 >        if (doblur | gotvfile) {
297 >                fputs(VIEWSTR, stdout);
298 >                fprintview(&avgview, stdout);
299 >                putc('\n', stdout);
300          }
301 <        if (pixaspect < .99 || pixaspect > 1.01)
301 >        if (pixaspect < .99 | pixaspect > 1.01)
302                  fputaspect(pixaspect, stdout);
303 <        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
303 >        if (ourexp > 0)
304 >                ourexp *= rexpadj;
305 >        else
306 >                ourexp = rexpadj;
307 >        if (ourexp < .995 | ourexp > 1.005)
308                  fputexpos(ourexp, stdout);
309 <        printf("\n");
309 >        if (strcmp(ourfmt, PICFMT))             /* print format if known */
310 >                fputformat(ourfmt, stdout);
311 >        putc('\n', stdout);
312                                                          /* write picture */
313          writepicture();
314                                                          /* write z file */
# Line 215 | Line 318 | char   *argv[];
318          exit(0);
319   userr:
320          fprintf(stderr,
321 <        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
321 >        "Usage: %s [view opts][-t eps][-z zout][-e spec][-B][-a|-q][-fT][-n] pfile zspec ..\n",
322                          progname);
323          exit(1);
324   #undef check
325   }
326  
327  
328 + int
329   headline(s)                             /* process header string */
330   char    *s;
331   {
332 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
229 <        register char   **an;
332 >        char    fmt[32];
333  
334 <        printf("\t%s", s);
335 <
334 >        if (isheadid(s))
335 >                return(0);
336 >        if (formatval(fmt, s)) {
337 >                if (globmatch(ourfmt, fmt)) {
338 >                        wrongformat = 0;
339 >                        strcpy(ourfmt, fmt);
340 >                } else
341 >                        wrongformat = 1;
342 >                return(0);
343 >        }
344 >        if (nvavg < 2) {
345 >                putc('\t', stdout);
346 >                fputs(s, stdout);
347 >        }
348          if (isexpos(s)) {
349                  theirexp *= exposval(s);
350 +                return(0);
351 +        }
352 +        if (isview(s) && sscanview(&theirview, s) > 0)
353 +                gotview++;
354 +        return(0);
355 + }
356 +
357 +
358 + nextview(fp)                            /* get and set next view */
359 + FILE    *fp;
360 + {
361 +        char    linebuf[256];
362 +        char    *err;
363 +        register int    i;
364 +
365 +        if (fp != NULL) {
366 +                do                      /* get new view */
367 +                        if (fgets(linebuf, sizeof(linebuf), fp) == NULL)
368 +                                return(EOF);
369 +                while (!isview(linebuf) || !sscanview(&ourview, linebuf));
370 +        }
371 +                                        /* set new view */
372 +        if ((err = setview(&ourview)) != NULL) {
373 +                fprintf(stderr, "%s: %s\n", progname, err);
374 +                exit(1);
375 +        }
376 +        if (!nvavg) {                   /* first view */
377 +                copystruct(&avgview, &ourview);
378 +                return(nvavg++);
379 +        }
380 +                                        /* add to average view */
381 +        for (i = 0; i < 3; i++) {
382 +                avgview.vp[i] += ourview.vp[i];
383 +                avgview.vdir[i] += ourview.vdir[i];
384 +                avgview.vup[i] += ourview.vup[i];
385 +        }
386 +        avgview.horiz += ourview.horiz;
387 +        avgview.vert += ourview.vert;
388 +        avgview.hoff += ourview.hoff;
389 +        avgview.voff += ourview.voff;
390 +        avgview.vfore += ourview.vfore;
391 +        avgview.vaft += ourview.vaft;
392 +        return(nvavg++);
393 + }
394 +
395 +
396 + compavgview()                           /* compute average view */
397 + {
398 +        register int    i;
399 +        double  f;
400 +
401 +        if (nvavg < 2)
402                  return;
403 +        f = 1.0/nvavg;
404 +        for (i = 0; i < 3; i++) {
405 +                avgview.vp[i] *= f;
406 +                avgview.vdir[i] *= f;
407 +                avgview.vup[i] *= f;
408          }
409 <        for (an = altname; *an != NULL; an++)
410 <                if (!strncmp(*an, s, strlen(*an))) {
411 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
412 <                                gotview++;
413 <                        break;
414 <                }
409 >        avgview.horiz *= f;
410 >        avgview.vert *= f;
411 >        avgview.hoff *= f;
412 >        avgview.voff *= f;
413 >        avgview.vfore *= f;
414 >        avgview.vaft *= f;
415 >        if (setview(&avgview) != NULL)          /* in case of emergency... */
416 >                copystruct(&avgview, &ourview);
417 >        pixaspect = viewaspect(&avgview) * hresolu / vresolu;
418   }
419  
420  
421   addpicture(pfile, zspec)                /* add picture to output */
422   char    *pfile, *zspec;
423   {
249        extern double   atof();
424          FILE    *pfp;
425          int     zfd;
426          char    *err;
427          COLR    *scanin;
428          float   *zin;
429          struct position *plast;
430 +        struct bound    *xlim, ylim;
431          int     y;
432                                          /* open picture file */
433 <        if ((pfp = fopen(pfile, "r")) == NULL) {
434 <                perror(pfile);
260 <                exit(1);
261 <        }
433 >        if ((pfp = fopen(pfile, "r")) == NULL)
434 >                syserror(pfile);
435                                          /* get header with exposure and view */
436          theirexp = 1.0;
437 +        copystruct(&theirview, &stdview);
438          gotview = 0;
439 <        printf("%s:\n", pfile);
440 <        getheader(pfp, headline);
441 <        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
442 <                        != (YMAJOR|YDECR)) {
443 <                fprintf(stderr, "%s: picture view error\n", pfile);
439 >        if (nvavg < 2)
440 >                printf("%s:\n", pfile);
441 >        getheader(pfp, headline, NULL);
442 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
443 >                fprintf(stderr, "%s: picture format error\n", pfile);
444                  exit(1);
445          }
446          if (ourexp <= 0)
# Line 278 | Line 452 | char   *pfile, *zspec;
452                  exit(1);
453          }
454                                          /* compute transformation */
455 <        pixform(theirs2ours, &theirview, &ourview);
282 <                                        /* allocate scanlines */
283 <        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
284 <        zin = (float *)malloc(thresolu*sizeof(float));
285 <        plast = (struct position *)calloc(thresolu, sizeof(struct position));
286 <        if (scanin == NULL || zin == NULL || plast == NULL)
287 <                syserror();
455 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
456                                          /* get z specification or file */
457 +        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
458 +        if (zin == NULL)
459 +                syserror(progname);
460          if ((zfd = open(zspec, O_RDONLY)) == -1) {
461                  double  zvalue;
462                  register int    x;
463 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
464 <                        perror(zspec);
465 <                        exit(1);
295 <                }
296 <                for (x = 0; x < thresolu; x++)
463 >                if (!isflt(zspec) || (zvalue = atof(zspec)) <= 0.0)
464 >                        syserror(zspec);
465 >                for (x = scanlen(&tresolu); x-- > 0; )
466                          zin[x] = zvalue;
467          }
468 <                                        /* load image */
469 <        for (y = tvresolu-1; y >= 0; y--) {
470 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
468 >                                        /* compute transferrable perimeter */
469 >        xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound));
470 >        if (xlim == NULL)
471 >                syserror(progname);
472 >        if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */
473 >                free((void *)zin);
474 >                free((void *)xlim);
475 >                if (zfd != -1)
476 >                        close(zfd);
477 >                fclose(pfp);
478 >                return;
479 >        }
480 >                                        /* allocate scanlines */
481 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
482 >        plast = (struct position *)calloc(scanlen(&tresolu),
483 >                        sizeof(struct position));
484 >        if (scanin == NULL | plast == NULL)
485 >                syserror(progname);
486 >                                        /* skip to starting point */
487 >        for (y = 0; y < ylim.min; y++)
488 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
489                          fprintf(stderr, "%s: read error\n", pfile);
490                          exit(1);
491                  }
492 <                if (zfd != -1 && read(zfd,zin,thresolu*sizeof(float))
493 <                                < thresolu*sizeof(float)) {
494 <                        fprintf(stderr, "%s: read error\n", zspec);
492 >        if (zfd != -1 && lseek(zfd,
493 >                        (off_t)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0)
494 >                syserror(zspec);
495 >                                        /* load image */
496 >        for (y = ylim.min; y <= ylim.max; y++) {
497 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
498 >                        fprintf(stderr, "%s: read error\n", pfile);
499                          exit(1);
500                  }
501 <                addscanline(y, scanin, zin, plast);
501 >                if (zfd != -1 && read(zfd, (char *)zin,
502 >                                scanlen(&tresolu)*sizeof(float))
503 >                                < scanlen(&tresolu)*sizeof(float))
504 >                        syserror(zspec);
505 >                addscanline(xlim+y, y, scanin, zin, plast);
506          }
507                                          /* clean up */
508 <        free((char *)scanin);
509 <        free((char *)zin);
510 <        free((char *)plast);
508 >        free((void *)xlim);
509 >        free((void *)scanin);
510 >        free((void *)zin);
511 >        free((void *)plast);
512          fclose(pfp);
513          if (zfd != -1)
514                  close(zfd);
# Line 320 | Line 516 | char   *pfile, *zspec;
516  
517  
518   pixform(xfmat, vw1, vw2)                /* compute view1 to view2 matrix */
519 < register double xfmat[4][4];
519 > register MAT4   xfmat;
520   register VIEW   *vw1, *vw2;
521   {
522          double  m4t[4][4];
523  
524 +        if (vw1->type != VT_PER & vw1->type != VT_PAR)
525 +                return(0);
526 +        if (vw2->type != VT_PER & vw2->type != VT_PAR)
527 +                return(0);
528          setident4(xfmat);
529          xfmat[0][0] = vw1->hvec[0];
530          xfmat[0][1] = vw1->hvec[1];
# Line 352 | Line 552 | register VIEW  *vw1, *vw2;
552          m4t[2][2] = vw2->vdir[2];
553          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
554          multmat4(xfmat, xfmat, m4t);
555 +        return(1);
556   }
557  
558  
559 < addscanline(y, pline, zline, lasty)     /* add scanline to output */
559 > addscanline(xl, y, pline, zline, lasty) /* add scanline to output */
560 > struct bound    *xl;
561   int     y;
562   COLR    *pline;
563   float   *zline;
564   struct position *lasty;         /* input/output */
565   {
566 <        extern double   sqrt();
365 <        double  pos[3];
566 >        FVECT   pos;
567          struct position lastx, newpos;
568 +        double  wt;
569          register int    x;
570  
571          lastx.z = 0;
572 <        for (x = thresolu-1; x >= 0; x--) {
573 <                pos[0] = (x+.5)/thresolu + theirview.hoff - .5;
372 <                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
572 >        for (x = xl->max; x >= xl->min; x--) {
573 >                pix2loc(pos, &tresolu, x, y);
574                  pos[2] = zline[x];
575 <                if (theirview.type == VT_PER) {
375 <                        if (normdist)   /* adjust for eye-ray distance */
376 <                                pos[2] /= sqrt( 1.
377 <                                        + pos[0]*pos[0]*theirview.hn2
378 <                                        + pos[1]*pos[1]*theirview.vn2 );
379 <                        pos[0] *= pos[2];
380 <                        pos[1] *= pos[2];
381 <                }
382 <                multp3(pos, pos, theirs2ours);
383 <                if (pos[2] <= 0) {
575 >                if ((wt = movepixel(pos)) <= FTINY) {
576                          lasty[x].z = lastx.z = 0;       /* mark invalid */
577                          continue;
578                  }
579 <                if (ourview.type == VT_PER) {
388 <                        pos[0] /= pos[2];
389 <                        pos[1] /= pos[2];
390 <                }
391 <                pos[0] += .5 - ourview.hoff;
392 <                pos[1] += .5 - ourview.voff;
579 >                                        /* add pixel to our image */
580                  newpos.x = pos[0] * hresolu;
581                  newpos.y = pos[1] * vresolu;
582                  newpos.z = zline[x];
583 <                                        /* add pixel to our image */
584 <                if (pos[0] >= 0 && newpos.x < hresolu
585 <                                && pos[1] >= 0 && newpos.y < vresolu) {
586 <                        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 */
583 >                addpixel(&newpos, &lastx, &lasty[x], pline[x], wt, pos[2]);
584 >                lasty[x].x = lastx.x = newpos.x;
585 >                lasty[x].y = lastx.y = newpos.y;
586 >                lasty[x].z = lastx.z = newpos.z;
587          }
588   }
589  
590  
591 < addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
591 > addpixel(p0, p1, p2, pix, w, z)         /* fill in pixel parallelogram */
592   struct position *p0, *p1, *p2;
593   COLR    pix;
594 + double  w;
595   double  z;
596   {
597          double  zt = 2.*zeps*p0->z;             /* threshold */
598 +        COLOR   pval;                           /* converted+weighted pixel */
599          int     s1x, s1y, s2x, s2y;             /* step sizes */
600          int     l1, l2, c1, c2;                 /* side lengths and counters */
601          int     p1isy;                          /* p0p1 along y? */
# Line 419 | Line 603 | double z;
603          register int    x, y;                   /* final position */
604  
605                                          /* compute vector p0p1 */
606 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
606 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
607                  s1x = p1->x - p0->x;
608                  s1y = p1->y - p0->y;
609                  l1 = ABS(s1x);
610                  if (p1isy = (ABS(s1y) > l1))
611                          l1 = ABS(s1y);
612 +                else if (l1 < 1)
613 +                        l1 = 1;
614          } else {
615                  l1 = s1x = s1y = 1;
616                  p1isy = -1;
617          }
618                                          /* compute vector p0p2 */
619 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
619 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
620                  s2x = p2->x - p0->x;
621                  s2y = p2->y - p0->y;
622                  if (p1isy == 1)
# Line 440 | Line 626 | double z;
626                          if (p1isy != 0 && ABS(s2x) > l2)
627                                  l2 = ABS(s2x);
628                  }
629 +                if (l2 < 1)
630 +                        l2 = 1;
631          } else
632                  l2 = s2x = s2y = 1;
633                                          /* fill the parallelogram */
634 +        if (averaging) {
635 +                colr_color(pval, pix);
636 +                scalecolor(pval, w);
637 +        }
638          for (c1 = l1; c1-- > 0; ) {
639                  x1 = p0->x + c1*s1x/l1;
640                  y1 = p0->y + c1*s1y/l1;
641                  for (c2 = l2; c2-- > 0; ) {
642                          x = x1 + c2*s2x/l2;
643 +                        if (x < 0 | x >= hresolu)
644 +                                continue;
645                          y = y1 + c2*s2y/l2;
646 <                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
646 >                        if (y < 0 | y >= vresolu)
647 >                                continue;
648 >                        if (averaging) {
649 >                                if (zscan(y)[x] <= 0 || zscan(y)[x]-z
650                                                  > zeps*zscan(y)[x]) {
651 <                                zscan(y)[x] = z;
651 >                                        copycolor(sscan(y)[x], pval);
652 >                                        wscan(y)[x] = w;
653 >                                        zscan(y)[x] = z;
654 >                                } else if (z-zscan(y)[x] <= zeps*zscan(y)[x]) {
655 >                                        addcolor(sscan(y)[x], pval);
656 >                                        wscan(y)[x] += w;
657 >                                }
658 >                        } else if (zscan(y)[x] <= 0 || zscan(y)[x]-z
659 >                                                > zeps*zscan(y)[x]) {
660                                  copycolr(pscan(y)[x], pix);
661 +                                zscan(y)[x] = z;
662                          }
663                  }
664          }
665   }
666  
667  
668 < backpicture()                           /* background fill algorithm */
668 > double
669 > movepixel(pos)                          /* reposition image point */
670 > register FVECT  pos;
671   {
672 +        FVECT   pt, tdir, odir;
673 +        double  d;
674 +        
675 +        if (pos[2] <= 0)                /* empty pixel */
676 +                return(0);
677 +        if (usematrix) {
678 +                pos[0] += theirview.hoff - .5;
679 +                pos[1] += theirview.voff - .5;
680 +                if (normdist & theirview.type == VT_PER)
681 +                        d = sqrt(1. + pos[0]*pos[0]*theirview.hn2
682 +                                        + pos[1]*pos[1]*theirview.vn2);
683 +                else
684 +                        d = 1.;
685 +                pos[2] += d*theirview.vfore;
686 +                if (theirview.type == VT_PER) {
687 +                        pos[2] /= d;
688 +                        pos[0] *= pos[2];
689 +                        pos[1] *= pos[2];
690 +                }
691 +                multp3(pos, pos, theirs2ours);
692 +                if (pos[2] <= ourview.vfore)
693 +                        return(0);
694 +                if (ourview.type == VT_PER) {
695 +                        pos[0] /= pos[2];
696 +                        pos[1] /= pos[2];
697 +                }
698 +                pos[0] += .5 - ourview.hoff;
699 +                pos[1] += .5 - ourview.voff;
700 +                pos[2] -= ourview.vfore;
701 +        } else {
702 +                if (viewray(pt, tdir, &theirview, pos[0], pos[1]) < -FTINY)
703 +                        return(0);
704 +                if (!normdist & theirview.type == VT_PER)       /* adjust */
705 +                        pos[2] *= sqrt(1. + pos[0]*pos[0]*theirview.hn2
706 +                                        + pos[1]*pos[1]*theirview.vn2);
707 +                pt[0] += tdir[0]*pos[2];
708 +                pt[1] += tdir[1]*pos[2];
709 +                pt[2] += tdir[2]*pos[2];
710 +                viewloc(pos, &ourview, pt);
711 +                if (pos[2] <= 0)
712 +                        return(0);
713 +        }
714 +        if (pos[0] < 0 | pos[0] >= 1-FTINY | pos[1] < 0 | pos[1] >= 1-FTINY)
715 +                return(0);
716 +        if (!averaging)
717 +                return(1);
718 +                                                /* compute pixel weight */
719 +        if (ourview.type == VT_PAR) {
720 +                d = DOT(ourview.vdir,tdir);
721 +                d = 1. - d*d;
722 +        } else {
723 +                VSUB(odir, pt, ourview.vp);
724 +                d = DOT(odir,tdir);
725 +                d = 1. - d*d/DOT(odir,odir);
726 +        }
727 +        if (d <= 1./MAXWT/MAXWT)
728 +                return(MAXWT);          /* clip to maximum weight */
729 +        return(1./sqrt(d));
730 + }
731 +
732 +
733 + getperim(xl, yl, zline, zfd)            /* compute overlapping image area */
734 + register struct bound   *xl;
735 + struct bound    *yl;
736 + float   *zline;
737 + int     zfd;
738 + {
739 +        int     step;
740 +        FVECT   pos;
741 +        register int    x, y;
742 +                                                /* set up step size */
743 +        if (scanlen(&tresolu) < numscans(&tresolu))
744 +                step = scanlen(&tresolu)/NSTEPS;
745 +        else
746 +                step = numscans(&tresolu)/NSTEPS;
747 +        if (step < MINSTEP) {                   /* not worth cropping? */
748 +                yl->min = 0;
749 +                yl->max = numscans(&tresolu) - 1;
750 +                x = scanlen(&tresolu) - 1;
751 +                for (y = numscans(&tresolu); y--; ) {
752 +                        xl[y].min = 0;
753 +                        xl[y].max = x;
754 +                }
755 +                return(1);
756 +        }
757 +        yl->min = 32000; yl->max = 0;           /* search for points on image */
758 +        for (y = step - 1; y < numscans(&tresolu); y += step) {
759 +                if (zfd != -1) {
760 +                        if (lseek(zfd, (off_t)y*scanlen(&tresolu)*sizeof(float),
761 +                                        0) < 0)
762 +                                syserror("lseek");
763 +                        if (read(zfd, (char *)zline,
764 +                                        scanlen(&tresolu)*sizeof(float))
765 +                                        < scanlen(&tresolu)*sizeof(float))
766 +                                syserror("read");
767 +                }
768 +                xl[y].min = 32000; xl[y].max = 0;               /* x max */
769 +                for (x = scanlen(&tresolu); (x -= step) > 0; ) {
770 +                        pix2loc(pos, &tresolu, x, y);
771 +                        pos[2] = zline[x];
772 +                        if (movepixel(pos) > FTINY) {
773 +                                xl[y].max = x + step - 1;
774 +                                xl[y].min = x - step + 1;       /* x min */
775 +                                if (xl[y].min < 0)
776 +                                        xl[y].min = 0;
777 +                                for (x = step - 1; x < xl[y].max; x += step) {
778 +                                        pix2loc(pos, &tresolu, x, y);
779 +                                        pos[2] = zline[x];
780 +                                        if (movepixel(pos) > FTINY) {
781 +                                                xl[y].min = x - step + 1;
782 +                                                break;
783 +                                        }
784 +                                }
785 +                                if (y < yl->min)                /* y limits */
786 +                                        yl->min = y - step + 1;
787 +                                yl->max = y + step - 1;
788 +                                break;
789 +                        }
790 +                }
791 +                                                        /* fill in between */
792 +                if (y < step) {
793 +                        xl[y-1].min = xl[y].min;
794 +                        xl[y-1].max = xl[y].max;
795 +                } else {
796 +                        if (xl[y].min < xl[y-step].min)
797 +                                xl[y-1].min = xl[y].min;
798 +                        else
799 +                                xl[y-1].min = xl[y-step].min;
800 +                        if (xl[y].max > xl[y-step].max)
801 +                                xl[y-1].max = xl[y].max;
802 +                        else
803 +                                xl[y-1].max = xl[y-step].max;
804 +                }
805 +                for (x = 2; x < step; x++)
806 +                        copystruct(xl+y-x, xl+y-1);
807 +        }
808 +        if (yl->max >= numscans(&tresolu))
809 +                yl->max = numscans(&tresolu) - 1;
810 +        y -= step;
811 +        for (x = numscans(&tresolu) - 1; x > y; x--)    /* fill bottom rows */
812 +                copystruct(xl+x, xl+y);
813 +        return(yl->max >= yl->min);
814 + }
815 +
816 +
817 + backpicture(fill, samp)                 /* background fill algorithm */
818 + int     (*fill)();
819 + int     samp;
820 + {
821          int     *yback, xback;
822          int     y;
466        COLR    pfill;
823          register int    x, i;
824                                                          /* get back buffer */
825          yback = (int *)malloc(hresolu*sizeof(int));
826          if (yback == NULL)
827 <                syserror();
827 >                syserror(progname);
828          for (x = 0; x < hresolu; x++)
829                  yback[x] = -2;
830          /*
# Line 510 | Line 866 | backpicture()                          /* background fill algorithm */
866                                                  xback = x-1;
867                                  }
868                                  /*
869 <                                 * Check to see if we have no background for
870 <                                 * this pixel.  If not, use background color.
869 >                                 * If we have no background for this pixel,
870 >                                 * use the given fill function.
871                                   */
872 <                                if (xback < 0 && yback[x] < 0) {
873 <                                        (*deffill)(x,y);
518 <                                        continue;
519 <                                }
872 >                                if (xback < 0 && yback[x] < 0)
873 >                                        goto fillit;
874                                  /*
875                                   * Compare, and use the background that is
876                                   * farther, unless one of them is next to us.
877 +                                 * If the background is too distant, call
878 +                                 * the fill function.
879                                   */
880                                  if ( yback[x] < 0
881                                          || (xback >= 0 && ABS(x-xback) <= 1)
882                                          || ( ABS(y-yback[x]) > 1
883                                                  && zscan(yback[x])[x]
884                                                  < zscan(y)[xback] ) ) {
885 <                                        copycolr(pscan(y)[x],pscan(y)[xback]);
885 >                                        if (samp > 0 && ABS(x-xback) >= samp)
886 >                                                goto fillit;
887 >                                        if (averaging) {
888 >                                                copycolor(sscan(y)[x],
889 >                                                        sscan(y)[xback]);
890 >                                                wscan(y)[x] = wscan(y)[xback];
891 >                                        } else
892 >                                                copycolr(pscan(y)[x],
893 >                                                        pscan(y)[xback]);
894                                          zscan(y)[x] = zscan(y)[xback];
895                                  } else {
896 <                                        copycolr(pscan(y)[x],pscan(yback[x])[x]);
896 >                                        if (samp > 0 && ABS(y-yback[x]) > samp)
897 >                                                goto fillit;
898 >                                        if (averaging) {
899 >                                                copycolor(sscan(y)[x],
900 >                                                        sscan(yback[x])[x]);
901 >                                                wscan(y)[x] =
902 >                                                        wscan(yback[x])[x];
903 >                                        } else
904 >                                                copycolr(pscan(y)[x],
905 >                                                        pscan(yback[x])[x]);
906                                          zscan(y)[x] = zscan(yback[x])[x];
907                                  }
908 +                                continue;
909 +                        fillit:
910 +                                (*fill)(x,y);
911 +                                if (fill == rcalfill) {         /* use it */
912 +                                        clearqueue();
913 +                                        xback = x;
914 +                                        yback[x] = y;
915 +                                }
916                          } else {                                /* full pixel */
917                                  yback[x] = -2;
918                                  xback = -2;
919                          }
920          }
921 <        free((char *)yback);
921 >        free((void *)yback);
922   }
923  
924  
925 < fillpicture()                           /* paint in empty pixels with default */
925 > fillpicture(fill)               /* paint in empty pixels using fill */
926 > int     (*fill)();
927   {
928          register int    x, y;
929  
930          for (y = 0; y < vresolu; y++)
931                  for (x = 0; x < hresolu; x++)
932                          if (zscan(y)[x] <= 0)
933 <                                (*deffill)(x,y);
933 >                                (*fill)(x,y);
934 >        if (fill == rcalfill)
935 >                clearqueue();
936   }
937  
938  
939 < writepicture()                          /* write out picture */
939 > clipaft()                       /* perform aft clipping as indicated */
940   {
941 +        register int    x, y;
942 +        int     adjtest = ourview.type == VT_PER & zisnorm;
943 +        double  tstdist;
944 +        double  yzn2, vx;
945 +
946 +        if (ourview.vaft <= FTINY)
947 +                return(0);
948 +        tstdist = ourview.vaft - ourview.vfore;
949 +        for (y = 0; y < vresolu; y++) {
950 +                if (adjtest) {                          /* adjust test */
951 +                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
952 +                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
953 +                        tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2);
954 +                }
955 +                for (x = 0; x < hresolu; x++)
956 +                        if (zscan(y)[x] > tstdist) {
957 +                                if (adjtest) {
958 +                                        vx = (x+.5)/hresolu + ourview.hoff - .5;
959 +                                        if (zscan(y)[x] <= (ourview.vaft -
960 +                                                        ourview.vfore) *
961 +                                                sqrt(vx*vx*ourview.hn2 + yzn2))
962 +                                                continue;
963 +                                }
964 +                                if (averaging)
965 +                                        memset(sscan(y)[x], '\0', sizeof(COLOR));
966 +                                else
967 +                                        memset(pscan(y)[x], '\0', sizeof(COLR));
968 +                                zscan(y)[x] = 0.0;
969 +                        }
970 +        }
971 +        return(1);
972 + }
973 +
974 +
975 + addblur()                               /* add to blurred picture */
976 + {
977 +        COLOR   cval;
978 +        double  d;
979 +        register int    i;
980 +
981 +        if (!blurring)
982 +                return(0);
983 +        i = hresolu*vresolu;
984 +        if (nvavg < 2)
985 +                if (averaging)
986 +                        while (i--) {
987 +                                copycolor(ourbpict[i], ourspict[i]);
988 +                                d = 1.0/ourweigh[i];
989 +                                scalecolor(ourbpict[i], d);
990 +                        }
991 +                else
992 +                        while (i--)
993 +                                colr_color(ourbpict[i], ourpict[i]);
994 +        else
995 +                if (averaging)
996 +                        while (i--) {
997 +                                copycolor(cval, ourspict[i]);
998 +                                d = 1.0/ourweigh[i];
999 +                                scalecolor(cval, d);
1000 +                                addcolor(ourbpict[i], cval);
1001 +                        }
1002 +                else
1003 +                        while (i--) {
1004 +                                colr_color(cval, ourpict[i]);
1005 +                                addcolor(ourbpict[i], cval);
1006 +                        }
1007 +                                /* print view */
1008 +        printf("VIEW%d:", nvavg);
1009 +        fprintview(&ourview, stdout);
1010 +        putchar('\n');
1011 +        return(1);
1012 + }
1013 +
1014 +
1015 + writepicture()                          /* write out picture (alters buffer) */
1016 + {
1017          int     y;
1018 +        register int    x;
1019 +        double  d;
1020  
1021 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
1021 >        fprtresolu(hresolu, vresolu, stdout);
1022          for (y = vresolu-1; y >= 0; y--)
1023 <                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
1024 <                        syserror();
1023 >                if (blurring) {
1024 >                        for (x = 0; x < hresolu; x++) { /* compute avg. */
1025 >                                d = rexpadj/nvavg;
1026 >                                scalecolor(bscan(y)[x], d);
1027 >                        }
1028 >                        if (fwritescan(bscan(y), hresolu, stdout) < 0)
1029 >                                syserror(progname);
1030 >                } else if (averaging) {
1031 >                        for (x = 0; x < hresolu; x++) { /* average pixels */
1032 >                                d = rexpadj/wscan(y)[x];
1033 >                                scalecolor(sscan(y)[x], d);
1034 >                        }
1035 >                        if (fwritescan(sscan(y), hresolu, stdout) < 0)
1036 >                                syserror(progname);
1037 >                } else {
1038 >                        if (expadj)
1039 >                                shiftcolrs(pscan(y), hresolu, expadj);
1040 >                        if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
1041 >                                syserror(progname);
1042 >                }
1043   }
1044  
1045  
1046 < writedistance(fname)                    /* write out z file */
1046 > writedistance(fname)                    /* write out z file (alters buffer) */
1047   char    *fname;
1048   {
1049 <        extern double   sqrt();
1050 <        int     donorm = normdist && ourview.type == VT_PER;
1049 >        int     donorm = normdist & !zisnorm ? 1 :
1050 >                        ourview.type == VT_PER & !normdist & zisnorm ? -1 : 0;
1051          int     fd;
1052          int     y;
573        float   *zout;
1053  
1054 <        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
1055 <                perror(fname);
577 <                exit(1);
578 <        }
579 <        if (donorm
580 <        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
581 <                syserror();
1054 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
1055 >                syserror(fname);
1056          for (y = vresolu-1; y >= 0; y--) {
1057                  if (donorm) {
1058 <                        double  vx, yzn2;
1058 >                        double  vx, yzn2, d;
1059                          register int    x;
1060 <                        yzn2 = y - .5*(vresolu-1);
1060 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
1061                          yzn2 = 1. + yzn2*yzn2*ourview.vn2;
1062                          for (x = 0; x < hresolu; x++) {
1063 <                                vx = x - .5*(hresolu-1);
1064 <                                zout[x] = zscan(y)[x]
1065 <                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
1063 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
1064 >                                d = sqrt(vx*vx*ourview.hn2 + yzn2);
1065 >                                if (donorm > 0)
1066 >                                        zscan(y)[x] *= d;
1067 >                                else
1068 >                                        zscan(y)[x] /= d;
1069                          }
593                } else
594                        zout = zscan(y);
595                if (write(fd, zout, hresolu*sizeof(float))
596                                < hresolu*sizeof(float)) {
597                        perror(fname);
598                        exit(1);
1070                  }
1071 +                if (write(fd, (char *)zscan(y), hresolu*sizeof(float))
1072 +                                < hresolu*sizeof(float))
1073 +                        syserror(fname);
1074          }
601        if (donorm)
602                free((char *)zout);
1075          close(fd);
1076   }
1077  
1078  
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 }
616
617
1079   backfill(x, y)                          /* fill pixel with background */
1080   int     x, y;
1081   {
1082 <        register BYTE   *dest = pscan(y)[x];
1083 <
1084 <        copycolr(dest, backcolr);
1082 >        if (averaging) {
1083 >                copycolor(sscan(y)[x], backcolor);
1084 >                wscan(y)[x] = 1;
1085 >        } else
1086 >                copycolr(pscan(y)[x], backcolr);
1087          zscan(y)[x] = backz;
1088   }
1089  
1090  
1091 < calstart(prog, args)                    /* start fill calculation */
1091 > calstart(prog, args)                    /* start fill calculation */
1092   char    *prog, *args;
1093   {
1094          char    combuf[512];
1095 <        int     p0[2], p1[2];
1095 >        char    *argv[64];
1096 >        int     rval;
1097 >        register char   **wp, *cp;
1098  
1099 <        if (childpid != -1) {
1099 >        if (PDesc.running) {
1100                  fprintf(stderr, "%s: too many calculations\n", progname);
1101                  exit(1);
1102          }
1103 <        sprintf(combuf, prog, PACKSIZ, args);
1104 <        if (pipe(p0) < 0 || pipe(p1) < 0)
1105 <                syserror();
1106 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
1107 <                close(p0[1]);
1108 <                close(p1[0]);
1109 <                if (p0[0] != 0) {
1110 <                        dup2(p0[0], 0);
1111 <                        close(p0[0]);
1112 <                }
1113 <                if (p1[1] != 1) {
1114 <                        dup2(p1[1], 1);
650 <                        close(p1[1]);
651 <                }
652 <                execl("/bin/sh", "sh", "-c", combuf, 0);
653 <                perror("/bin/sh");
654 <                _exit(127);
1103 >        strcpy(combuf, prog);
1104 >        strcat(combuf, args);
1105 >        cp = combuf;
1106 >        wp = argv;
1107 >        for ( ; ; ) {
1108 >                while (isspace(*cp))    /* nullify spaces */
1109 >                        *cp++ = '\0';
1110 >                if (!*cp)               /* all done? */
1111 >                        break;
1112 >                *wp++ = cp;             /* add argument to list */
1113 >                while (*++cp && !isspace(*cp))
1114 >                        ;
1115          }
1116 <        if (childpid == -1)
1117 <                syserror();
1118 <        close(p0[0]);
1119 <        close(p1[1]);
1120 <        if ((psend = fdopen(p0[1], "w")) == NULL)
1121 <                syserror();
1122 <        if ((precv = fdopen(p1[0], "r")) == NULL)
1123 <                syserror();
1116 >        *wp = NULL;
1117 >                                                /* start process */
1118 >        if ((rval = open_process(&PDesc, argv)) < 0)
1119 >                syserror(progname);
1120 >        if (rval == 0) {
1121 >                fprintf(stderr, "%s: command not found\n", argv[0]);
1122 >                exit(1);
1123 >        }
1124 >        packsiz = rval/(6*sizeof(float)) - 1;
1125 >        if (packsiz > PACKSIZ)
1126 >                packsiz = PACKSIZ;
1127          queuesiz = 0;
1128   }
1129  
1130  
1131 < caldone()                               /* done with calculation */
1131 > caldone()                               /* done with calculation */
1132   {
1133 <        int     pid;
671 <
672 <        if (childpid == -1)
1133 >        if (!PDesc.running)
1134                  return;
674        if (fclose(psend) == EOF)
675                syserror();
1135          clearqueue();
1136 <        fclose(precv);
678 <        while ((pid = wait(0)) != -1 && pid != childpid)
679 <                ;
680 <        childpid = -1;
1136 >        close_process(&PDesc);
1137   }
1138  
1139  
1140   rcalfill(x, y)                          /* fill with ray-calculated pixel */
1141   int     x, y;
1142   {
1143 <        FVECT   orig, dir;
688 <        float   outbuf[6];
689 <
690 <        if (queuesiz >= PACKSIZ) {      /* flush queue */
691 <                if (fflush(psend) == EOF)
692 <                        syserror();
1143 >        if (queuesiz >= packsiz)        /* flush queue if needed */
1144                  clearqueue();
1145 <        }
695 <                                        /* send new ray */
696 <        viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu);
697 <        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
698 <        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
699 <        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
700 <                syserror();
701 <                                        /* remember it */
1145 >                                        /* add position to queue */
1146          queue[queuesiz][0] = x;
1147          queue[queuesiz][1] = y;
1148          queuesiz++;
1149   }
1150  
1151  
1152 < clearqueue()                            /* get results from queue */
1152 > clearqueue()                            /* process queue */
1153   {
1154 <        float   inbuf[4];
1154 >        FVECT   orig, dir;
1155 >        float   fbuf[6*(PACKSIZ+1)];
1156 >        register float  *fbp;
1157          register int    i;
1158 +        double  vx, vy;
1159  
1160 +        if (queuesiz == 0)
1161 +                return;
1162 +        fbp = fbuf;
1163          for (i = 0; i < queuesiz; i++) {
1164 <                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
1165 <                        fprintf(stderr, "%s: read error in clearqueue\n",
1166 <                                        progname);
1167 <                        exit(1);
1168 <                }
1164 >                viewray(orig, dir, &ourview,
1165 >                                (queue[i][0]+.5)/hresolu,
1166 >                                (queue[i][1]+.5)/vresolu);
1167 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
1168 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
1169 >        }
1170 >                                        /* mark end and get results */
1171 >        memset((char *)fbp, '\0', 6*sizeof(float));
1172 >        if (process(&PDesc, (char *)fbuf, (char *)fbuf,
1173 >                        4*sizeof(float)*(queuesiz+1),
1174 >                        6*sizeof(float)*(queuesiz+1)) !=
1175 >                        4*sizeof(float)*(queuesiz+1)) {
1176 >                fprintf(stderr, "%s: error reading from rtrace process\n",
1177 >                                progname);
1178 >                exit(1);
1179 >        }
1180 >        fbp = fbuf;
1181 >        for (i = 0; i < queuesiz; i++) {
1182                  if (ourexp > 0 && ourexp != 1.0) {
1183 <                        inbuf[0] *= ourexp;
1184 <                        inbuf[1] *= ourexp;
1185 <                        inbuf[2] *= ourexp;
1183 >                        fbp[0] *= ourexp;
1184 >                        fbp[1] *= ourexp;
1185 >                        fbp[2] *= ourexp;
1186                  }
1187 <                setcolr(pscan(queue[i][1])[queue[i][0]],
1188 <                                inbuf[0], inbuf[1], inbuf[2]);
1189 <                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
1187 >                if (averaging) {
1188 >                        setcolor(sscan(queue[i][1])[queue[i][0]],
1189 >                                        fbp[0], fbp[1], fbp[2]);
1190 >                        wscan(queue[i][1])[queue[i][0]] = 1;
1191 >                } else
1192 >                        setcolr(pscan(queue[i][1])[queue[i][0]],
1193 >                                        fbp[0], fbp[1], fbp[2]);
1194 >                if (zisnorm)
1195 >                        zscan(queue[i][1])[queue[i][0]] = fbp[3];
1196 >                else {
1197 >                        vx = (queue[i][0]+.5)/hresolu + ourview.hoff - .5;
1198 >                        vy = (queue[i][1]+.5)/vresolu + ourview.voff - .5;
1199 >                        zscan(queue[i][1])[queue[i][0]] = fbp[3] / sqrt(1. +
1200 >                                        vx*vx*ourview.hn2 + vy*vy*ourview.vn2);
1201 >                }
1202 >                fbp += 4;
1203          }
1204          queuesiz = 0;
1205   }
1206  
1207  
1208 < syserror()                      /* report error and exit */
1208 > syserror(s)                     /* report error and exit */
1209 > char    *s;
1210   {
1211 <        perror(progname);
1211 >        perror(s);
1212          exit(1);
1213   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines