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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines