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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines