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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines