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.11 by greg, Thu Jan 4 10:12:49 1990 UTC vs.
Revision 1.33 by greg, Thu Apr 18 14:35:26 1991 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12  
13 + #include <fcntl.h>
14 +
15   #include "view.h"
16  
17   #include "color.h"
18  
19 < #define pscan(y)        (ourpict+(y)*ourview.hresolu)
20 < #define zscan(y)        (ourzbuf+(y)*ourview.hresolu)
19 > #ifndef BSD
20 > #define vfork           fork
21 > #endif
22  
23 < #define F_FORE  1                       /* fill foreground */
24 < #define F_BACK  2                       /* fill background */
23 > #define pscan(y)        (ourpict+(y)*hresolu)
24 > #define zscan(y)        (ourzbuf+(y)*hresolu)
25  
26 + #define F_FORE          1               /* fill foreground */
27 + #define F_BACK          2               /* fill background */
28 +
29 + #define PACKSIZ         42              /* calculation packet size */
30 +
31 + #define RTCOM           "rtrace -h -ovl -fff %s"
32 +
33   #define ABS(x)          ((x)>0?(x):-(x))
34  
35 < VIEW    ourview = STDVIEW(512);         /* desired view */
35 > struct position {int x,y; float z;};
36  
37 + VIEW    ourview = STDVIEW;              /* desired view */
38 + int     hresolu = 512;                  /* horizontal resolution */
39 + int     vresolu = 512;                  /* vertical resolution */
40 + double  pixaspect = 1.0;                /* pixel aspect ratio */
41 +
42   double  zeps = .02;                     /* allowed z epsilon */
43  
44   COLR    *ourpict;                       /* output picture */
# Line 31 | Line 46 | float  *ourzbuf;                       /* corresponding z-buffer */
46  
47   char    *progname;
48  
49 < VIEW    theirview = STDVIEW(512);       /* input view */
50 < int     gotview;                        /* got input view? */
51 <
52 < int     fill = F_FORE|F_BACK;           /* fill level */
49 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
50 > int     fillsamp = 0;                   /* sample separation (0 == inf) */
51 > extern int      backfill(), rcalfill(); /* fill functions */
52 > int     (*fillfunc)() = backfill;       /* selected fill function */
53   COLR    backcolr = BLKCOLR;             /* background color */
54 + double  backz = 0.0;                    /* background z value */
55 + int     normdist = 1;                   /* normalized distance? */
56 + double  ourexp = -1;                    /* output picture exposure */
57  
58 + VIEW    theirview = STDVIEW;            /* input view */
59 + int     gotview;                        /* got input view? */
60 + int     wrongformat = 0;                /* input in another format? */
61 + int     thresolu, tvresolu;             /* input resolution */
62 + double  theirexp;                       /* input picture exposure */
63   double  theirs2ours[4][4];              /* transformation matrix */
64 < int     normdist = 1;                   /* normalized distance? */
64 > int     hasmatrix = 0;                  /* has transformation matrix */
65  
66 + int     childpid = -1;                  /* id of fill process */
67 + FILE    *psend, *precv;                 /* pipes to/from fill calculation */
68 + int     queue[PACKSIZ][2];              /* pending pixels */
69 + int     queuesiz;                       /* number of pixels pending */
70  
71 +
72   main(argc, argv)                        /* interpolate pictures */
73   int     argc;
74   char    *argv[];
# Line 50 | Line 78 | char   *argv[];
78          int     gotvfile = 0;
79          char    *zfile = NULL;
80          char    *err;
81 <        int     i;
81 >        int     i, rval;
82  
83          progname = argv[0];
84  
85 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
85 >        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
86 >                rval = getviewopt(&ourview, argc-i, argv+i);
87 >                if (rval >= 0) {
88 >                        i += rval;
89 >                        continue;
90 >                }
91                  switch (argv[i][1]) {
92                  case 't':                               /* threshold */
93                          check(2,1);
# Line 68 | Line 101 | char   *argv[];
101                          switch (argv[i][2]) {
102                          case '0':                               /* none */
103                                  check(3,0);
104 <                                fill = 0;
104 >                                fillo = 0;
105                                  break;
106                          case 'f':                               /* foreground */
107                                  check(3,0);
108 <                                fill = F_FORE;
108 >                                fillo = F_FORE;
109                                  break;
110                          case 'b':                               /* background */
111                                  check(3,0);
112 <                                fill = F_BACK;
112 >                                fillo = F_BACK;
113                                  break;
114                          case 'a':                               /* all */
115                                  check(3,0);
116 <                                fill = F_FORE|F_BACK;
116 >                                fillo = F_FORE|F_BACK;
117                                  break;
118 +                        case 's':                               /* sample */
119 +                                check(3,1);
120 +                                fillsamp = atoi(argv[++i]);
121 +                                break;
122                          case 'c':                               /* color */
123                                  check(3,3);
124 +                                fillfunc = backfill;
125                                  setcolr(backcolr, atof(argv[i+1]),
126                                          atof(argv[i+2]), atof(argv[i+3]));
127                                  i += 3;
128                                  break;
129 +                        case 'z':                               /* z value */
130 +                                check(3,1);
131 +                                fillfunc = backfill;
132 +                                backz = atof(argv[++i]);
133 +                                break;
134 +                        case 'r':                               /* rtrace */
135 +                                check(3,1);
136 +                                fillfunc = rcalfill;
137 +                                calstart(RTCOM, argv[++i]);
138 +                                break;
139                          default:
140                                  goto badopt;
141                          }
# Line 98 | Line 146 | char   *argv[];
146                          break;
147                  case 'x':                               /* x resolution */
148                          check(2,1);
149 <                        ourview.hresolu = atoi(argv[++i]);
149 >                        hresolu = atoi(argv[++i]);
150                          break;
151                  case 'y':                               /* y resolution */
152                          check(2,1);
153 <                        ourview.vresolu = atoi(argv[++i]);
153 >                        vresolu = atoi(argv[++i]);
154                          break;
155 <                case 'v':                               /* view */
156 <                        switch (argv[i][2]) {
157 <                        case 't':                               /* type */
158 <                                check(4,0);
159 <                                ourview.type = argv[i][3];
160 <                                break;
113 <                        case 'p':                               /* point */
114 <                                check(3,3);
115 <                                ourview.vp[0] = atof(argv[++i]);
116 <                                ourview.vp[1] = atof(argv[++i]);
117 <                                ourview.vp[2] = atof(argv[++i]);
118 <                                break;
119 <                        case 'd':                               /* direction */
120 <                                check(3,3);
121 <                                ourview.vdir[0] = atof(argv[++i]);
122 <                                ourview.vdir[1] = atof(argv[++i]);
123 <                                ourview.vdir[2] = atof(argv[++i]);
124 <                                break;
125 <                        case 'u':                               /* up */
126 <                                check(3,3);
127 <                                ourview.vup[0] = atof(argv[++i]);
128 <                                ourview.vup[1] = atof(argv[++i]);
129 <                                ourview.vup[2] = atof(argv[++i]);
130 <                                break;
131 <                        case 'h':                               /* horizontal */
132 <                                check(3,1);
133 <                                ourview.horiz = atof(argv[++i]);
134 <                                break;
135 <                        case 'v':                               /* vertical */
136 <                                check(3,1);
137 <                                ourview.vert = atof(argv[++i]);
138 <                                break;
139 <                        case 'f':                               /* file */
140 <                                check(3,1);
141 <                                gotvfile = viewfile(argv[++i], &ourview);
142 <                                if (gotvfile < 0) {
143 <                                        perror(argv[i]);
144 <                                        exit(1);
145 <                                } else if (gotvfile == 0) {
146 <                                        fprintf(stderr, "%s: bad view file\n",
147 <                                                        argv[i]);
148 <                                        exit(1);
149 <                                }
150 <                                break;
151 <                        default:
155 >                case 'p':                               /* pixel aspect */
156 >                        check(2,1);
157 >                        pixaspect = atof(argv[++i]);
158 >                        break;
159 >                case 'v':                               /* view file */
160 >                        if (argv[i][2] != 'f')
161                                  goto badopt;
162 +                        check(3,1);
163 +                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
164 +                        if (gotvfile < 0) {
165 +                                perror(argv[i]);
166 +                                exit(1);
167 +                        } else if (gotvfile == 0) {
168 +                                fprintf(stderr, "%s: bad view file\n",
169 +                                                argv[i]);
170 +                                exit(1);
171                          }
172                          break;
173                  default:
# Line 158 | Line 176 | char   *argv[];
176                                          progname, argv[i]);
177                          goto userr;
178                  }
179 +        }
180                                                  /* check arguments */
181 <        if (argc-i < 2 || (argc-i)%2)
181 >        if ((argc-i)%2)
182                  goto userr;
183                                                  /* set view */
184          if (err = setview(&ourview)) {
185                  fprintf(stderr, "%s: %s\n", progname, err);
186                  exit(1);
187          }
188 +        normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
189                                                  /* allocate frame */
190 <        ourpict = (COLR *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR));
191 <        ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
192 <        if (ourpict == NULL || ourzbuf == NULL) {
193 <                perror(progname);
174 <                exit(1);
175 <        }
190 >        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
191 >        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
192 >        if (ourpict == NULL || ourzbuf == NULL)
193 >                syserror();
194                                                          /* get input */
195          for ( ; i < argc; i += 2)
196                  addpicture(argv[i], argv[i+1]);
197                                                          /* fill in spaces */
198 <        if (fill&F_BACK)
199 <                fillpicture();
198 >        if (fillo&F_BACK)
199 >                backpicture(fillfunc, fillsamp);
200          else
201 <                backpicture();
201 >                fillpicture(fillfunc);
202 >                                                        /* close calculation */
203 >        caldone();
204                                                          /* add to header */
205          printargs(argc, argv, stdout);
206          if (gotvfile) {
207 <                printf(VIEWSTR);
207 >                fputs(VIEWSTR, stdout);
208                  fprintview(&ourview, stdout);
209 <                printf("\n");
209 >                putc('\n', stdout);
210          }
211 <        printf("\n");
211 >        if (pixaspect < .99 || pixaspect > 1.01)
212 >                fputaspect(pixaspect, stdout);
213 >        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
214 >                fputexpos(ourexp, stdout);
215 >        fputformat(COLRFMT, stdout);
216 >        putc('\n', stdout);
217                                                          /* write picture */
218          writepicture();
219                                                          /* write z file */
# Line 198 | Line 223 | char   *argv[];
223          exit(0);
224   userr:
225          fprintf(stderr,
226 <        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
226 >        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
227                          progname);
228          exit(1);
229   #undef check
# Line 208 | Line 233 | userr:
233   headline(s)                             /* process header string */
234   char    *s;
235   {
236 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
236 >        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
237          register char   **an;
238 +        char    fmt[32];
239  
240 <        printf("\t%s", s);
240 >        if (isformat(s)) {
241 >                formatval(fmt, s);
242 >                wrongformat = strcmp(fmt, COLRFMT);
243 >                return;
244 >        }
245 >        putc('\t', stdout);
246 >        fputs(s, stdout);
247  
248 +        if (isexpos(s)) {
249 +                theirexp *= exposval(s);
250 +                return;
251 +        }
252          for (an = altname; *an != NULL; an++)
253                  if (!strncmp(*an, s, strlen(*an))) {
254 <                        if (sscanview(&theirview, s+strlen(*an)) == 0)
254 >                        if (sscanview(&theirview, s+strlen(*an)) > 0)
255                                  gotview++;
256                          break;
257                  }
# Line 226 | Line 262 | addpicture(pfile, zspec)               /* add picture to output */
262   char    *pfile, *zspec;
263   {
264          extern double   atof();
265 <        FILE    *pfp, *zfp;
265 >        FILE    *pfp;
266 >        int     zfd;
267          char    *err;
268          COLR    *scanin;
269 <        float   *zin, *zlast;
270 <        int     *plast;
269 >        float   *zin;
270 >        struct position *plast;
271          int     y;
272                                          /* open picture file */
273          if ((pfp = fopen(pfile, "r")) == NULL) {
274                  perror(pfile);
275                  exit(1);
276          }
277 <                                        /* get header and view */
278 <        printf("%s:\n", pfile);
277 >                                        /* get header with exposure and view */
278 >        theirexp = 1.0;
279          gotview = 0;
280 <        getheader(pfp, headline);
281 <        if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
282 <                        != (YMAJOR|YDECR)) {
283 <                fprintf(stderr, "%s: picture view error\n", pfile);
280 >        printf("%s:\n", pfile);
281 >        getheader(pfp, headline, NULL);
282 >        if (wrongformat || !gotview ||
283 >                fgetresolu(&thresolu, &tvresolu, pfp) != (YMAJOR|YDECR)) {
284 >
285 >                fprintf(stderr, "%s: picture format error\n", pfile);
286                  exit(1);
287          }
288 +        if (ourexp <= 0)
289 +                ourexp = theirexp;
290 +        else if (ABS(theirexp-ourexp) > .01*ourexp)
291 +                fprintf(stderr, "%s: different exposure (warning)\n", pfile);
292          if (err = setview(&theirview)) {
293                  fprintf(stderr, "%s: %s\n", pfile, err);
294                  exit(1);
295          }
296                                          /* compute transformation */
297 <        pixform(theirs2ours, &theirview, &ourview);
297 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
298                                          /* allocate scanlines */
299 <        scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
300 <        zin = (float *)malloc(theirview.hresolu*sizeof(float));
301 <        plast = (int *)calloc(theirview.hresolu, sizeof(int));
302 <        zlast = (float *)calloc(theirview.hresolu, sizeof(float));
303 <        if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) {
261 <                perror(progname);
262 <                exit(1);
263 <        }
299 >        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
300 >        zin = (float *)malloc(thresolu*sizeof(float));
301 >        plast = (struct position *)calloc(thresolu, sizeof(struct position));
302 >        if (scanin == NULL || zin == NULL || plast == NULL)
303 >                syserror();
304                                          /* get z specification or file */
305 <        if ((zfp = fopen(zspec, "r")) == NULL) {
305 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
306                  double  zvalue;
307                  register int    x;
308                  if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
309                          perror(zspec);
310                          exit(1);
311                  }
312 <                for (x = 0; x < theirview.hresolu; x++)
312 >                for (x = 0; x < thresolu; x++)
313                          zin[x] = zvalue;
314          }
315                                          /* load image */
316 <        for (y = theirview.vresolu-1; y >= 0; y--) {
317 <                if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
316 >        for (y = tvresolu-1; y >= 0; y--) {
317 >                if (freadcolrs(scanin, thresolu, pfp) < 0) {
318                          fprintf(stderr, "%s: read error\n", pfile);
319                          exit(1);
320                  }
321 <                if (zfp != NULL
322 <                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
283 <                                < theirview.hresolu) {
321 >                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
322 >                                < thresolu*sizeof(float)) {
323                          fprintf(stderr, "%s: read error\n", zspec);
324                          exit(1);
325                  }
326 <                addscanline(y, scanin, zin, plast, zlast);
326 >                addscanline(y, scanin, zin, plast);
327          }
328                                          /* clean up */
329          free((char *)scanin);
330          free((char *)zin);
331          free((char *)plast);
293        free((char *)zlast);
332          fclose(pfp);
333 <        if (zfp != NULL)
334 <                fclose(zfp);
333 >        if (zfd != -1)
334 >                close(zfd);
335   }
336  
337  
# Line 303 | Line 341 | register VIEW  *vw1, *vw2;
341   {
342          double  m4t[4][4];
343  
344 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
345 +                return(0);
346 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
347 +                return(0);
348          setident4(xfmat);
349 <        xfmat[0][0] = vw1->vhinc[0];
350 <        xfmat[0][1] = vw1->vhinc[1];
351 <        xfmat[0][2] = vw1->vhinc[2];
352 <        xfmat[1][0] = vw1->vvinc[0];
353 <        xfmat[1][1] = vw1->vvinc[1];
354 <        xfmat[1][2] = vw1->vvinc[2];
349 >        xfmat[0][0] = vw1->hvec[0];
350 >        xfmat[0][1] = vw1->hvec[1];
351 >        xfmat[0][2] = vw1->hvec[2];
352 >        xfmat[1][0] = vw1->vvec[0];
353 >        xfmat[1][1] = vw1->vvec[1];
354 >        xfmat[1][2] = vw1->vvec[2];
355          xfmat[2][0] = vw1->vdir[0];
356          xfmat[2][1] = vw1->vdir[1];
357          xfmat[2][2] = vw1->vdir[2];
# Line 317 | Line 359 | register VIEW  *vw1, *vw2;
359          xfmat[3][1] = vw1->vp[1];
360          xfmat[3][2] = vw1->vp[2];
361          setident4(m4t);
362 <        m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
363 <        m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
364 <        m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
365 <        m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
366 <        m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
367 <        m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
368 <        m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
369 <        m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
362 >        m4t[0][0] = vw2->hvec[0]/vw2->hn2;
363 >        m4t[1][0] = vw2->hvec[1]/vw2->hn2;
364 >        m4t[2][0] = vw2->hvec[2]/vw2->hn2;
365 >        m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
366 >        m4t[0][1] = vw2->vvec[0]/vw2->vn2;
367 >        m4t[1][1] = vw2->vvec[1]/vw2->vn2;
368 >        m4t[2][1] = vw2->vvec[2]/vw2->vn2;
369 >        m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
370          m4t[0][2] = vw2->vdir[0];
371          m4t[1][2] = vw2->vdir[1];
372          m4t[2][2] = vw2->vdir[2];
373          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
374          multmat4(xfmat, xfmat, m4t);
375 +        return(1);
376   }
377  
378  
379 < addscanline(y, pline, zline, lasty, lastyz)     /* add scanline to output */
379 > addscanline(y, pline, zline, lasty)     /* add scanline to output */
380   int     y;
381   COLR    *pline;
382   float   *zline;
383 < int     *lasty;                 /* input/output */
341 < float   *lastyz;                /* input/output */
383 > struct position *lasty;         /* input/output */
384   {
385          extern double   sqrt();
386 <        double  pos[3];
387 <        int     lastx = 0;
346 <        double  lastxz = 0;
347 <        double  zt;
348 <        int     xpos, ypos;
386 >        FVECT   pos;
387 >        struct position lastx, newpos;
388          register int    x;
389  
390 <        for (x = theirview.hresolu-1; x >= 0; x--) {
391 <                pos[0] = x - .5*(theirview.hresolu-1);
392 <                pos[1] = y - .5*(theirview.vresolu-1);
390 >        lastx.z = 0;
391 >        for (x = thresolu-1; x >= 0; x--) {
392 >                pos[0] = (x+.5)/thresolu;
393 >                pos[1] = (y+.5)/tvresolu;
394                  pos[2] = zline[x];
395 <                if (theirview.type == VT_PER) {
396 <                        if (normdist)   /* adjust for eye-ray distance */
357 <                                pos[2] /= sqrt( 1.
358 <                                        + pos[0]*pos[0]*theirview.vhn2
359 <                                        + pos[1]*pos[1]*theirview.vvn2 );
360 <                        pos[0] *= pos[2];
361 <                        pos[1] *= pos[2];
362 <                }
363 <                multp3(pos, pos, theirs2ours);
364 <                if (pos[2] <= 0)
395 >                if (movepixel(pos) < 0) {
396 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
397                          continue;
366                if (ourview.type == VT_PER) {
367                        pos[0] /= pos[2];
368                        pos[1] /= pos[2];
398                  }
399 <                pos[0] += .5*ourview.hresolu;
400 <                pos[1] += .5*ourview.vresolu;
401 <                if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
373 <                        || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
374 <                        continue;
399 >                newpos.x = pos[0] * hresolu;
400 >                newpos.y = pos[1] * vresolu;
401 >                newpos.z = zline[x];
402                                          /* add pixel to our image */
403 <                zt = 2.*zeps*zline[x];
404 <                addpixel(xpos, ypos,
405 <                        (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
406 <                                ? lastx - xpos : 1,
407 <                        (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
408 <                                ? lasty[x] - ypos : 1,
409 <                        pline[x], pos[2]);
410 <                lastx = xpos;
384 <                lasty[x] = ypos;
385 <                lastxz = lastyz[x] = zline[x];
403 >                if (pos[0] >= 0 && newpos.x < hresolu
404 >                                && pos[1] >= 0 && newpos.y < vresolu) {
405 >                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
406 >                        lasty[x].x = lastx.x = newpos.x;
407 >                        lasty[x].y = lastx.y = newpos.y;
408 >                        lasty[x].z = lastx.z = newpos.z;
409 >                } else
410 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
411          }
412   }
413  
414  
415 < addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */
416 < int     xstart, ystart;
392 < int     width, height;
415 > addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
416 > struct position *p0, *p1, *p2;
417   COLR    pix;
418   double  z;
419   {
420 <        register int    x, y;
421 <                                        /* make width and height positive */
422 <        if (width < 0) {
423 <                width = -width;
424 <                xstart = xstart-width+1;
425 <        } else if (width == 0)
426 <                width = 1;
427 <        if (height < 0) {
428 <                height = -height;
429 <                ystart = ystart-height+1;
430 <        } else if (height == 0)
431 <                height = 1;
432 <                                        /* fill pixel(s) within rectangle */
433 <        for (y = ystart; y < ystart+height; y++)
434 <                for (x = xstart; x < xstart+width; x++)
435 <                        if (zscan(y)[x] <= 0
436 <                                        || zscan(y)[x]-z > zeps*zscan(y)[x]) {
420 >        double  zt = 2.*zeps*p0->z;             /* threshold */
421 >        int     s1x, s1y, s2x, s2y;             /* step sizes */
422 >        int     l1, l2, c1, c2;                 /* side lengths and counters */
423 >        int     p1isy;                          /* p0p1 along y? */
424 >        int     x1, y1;                         /* p1 position */
425 >        register int    x, y;                   /* final position */
426 >
427 >                                        /* compute vector p0p1 */
428 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
429 >                s1x = p1->x - p0->x;
430 >                s1y = p1->y - p0->y;
431 >                l1 = ABS(s1x);
432 >                if (p1isy = (ABS(s1y) > l1))
433 >                        l1 = ABS(s1y);
434 >        } else {
435 >                l1 = s1x = s1y = 1;
436 >                p1isy = -1;
437 >        }
438 >                                        /* compute vector p0p2 */
439 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
440 >                s2x = p2->x - p0->x;
441 >                s2y = p2->y - p0->y;
442 >                if (p1isy == 1)
443 >                        l2 = ABS(s2x);
444 >                else {
445 >                        l2 = ABS(s2y);
446 >                        if (p1isy != 0 && ABS(s2x) > l2)
447 >                                l2 = ABS(s2x);
448 >                }
449 >        } else
450 >                l2 = s2x = s2y = 1;
451 >                                        /* fill the parallelogram */
452 >        for (c1 = l1; c1-- > 0; ) {
453 >                x1 = p0->x + c1*s1x/l1;
454 >                y1 = p0->y + c1*s1y/l1;
455 >                for (c2 = l2; c2-- > 0; ) {
456 >                        x = x1 + c2*s2x/l2;
457 >                        if (x < 0 || x >= hresolu)
458 >                                continue;
459 >                        y = y1 + c2*s2y/l2;
460 >                        if (y < 0 || y >= vresolu)
461 >                                continue;
462 >                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
463 >                                                > zeps*zscan(y)[x]) {
464                                  zscan(y)[x] = z;
465                                  copycolr(pscan(y)[x], pix);
466                          }
467 +                }
468 +        }
469   }
470  
471  
472 < fillpicture()                           /* fill in empty spaces */
472 > movepixel(pos)                          /* reposition image point */
473 > FVECT   pos;
474   {
475 +        FVECT   pt, direc;
476 +        
477 +        if (pos[2] <= 0)                /* empty pixel */
478 +                return(-1);
479 +        if (hasmatrix) {
480 +                pos[0] += theirview.hoff - .5;
481 +                pos[1] += theirview.voff - .5;
482 +                if (theirview.type == VT_PER) {
483 +                        if (normdist)   /* adjust for eye-ray distance */
484 +                                pos[2] /= sqrt( 1.
485 +                                        + pos[0]*pos[0]*theirview.hn2
486 +                                        + pos[1]*pos[1]*theirview.vn2 );
487 +                        pos[0] *= pos[2];
488 +                        pos[1] *= pos[2];
489 +                }
490 +                multp3(pos, pos, theirs2ours);
491 +                if (pos[2] <= 0)
492 +                        return(-1);
493 +                if (ourview.type == VT_PER) {
494 +                        pos[0] /= pos[2];
495 +                        pos[1] /= pos[2];
496 +                }
497 +                pos[0] += .5 - ourview.hoff;
498 +                pos[1] += .5 - ourview.voff;
499 +                return(0);
500 +        }
501 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
502 +                return(-1);
503 +        pt[0] += direc[0]*pos[2];
504 +        pt[1] += direc[1]*pos[2];
505 +        pt[2] += direc[2]*pos[2];
506 +        viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt);
507 +        if (pos[2] <= 0)
508 +                return(-1);
509 +        return(0);
510 + }
511 +
512 +
513 + backpicture(fill, samp)                 /* background fill algorithm */
514 + int     (*fill)();
515 + int     samp;
516 + {
517          int     *yback, xback;
518          int     y;
423        COLR    pfill;
519          register int    x, i;
520                                                          /* get back buffer */
521 <        yback = (int *)malloc(ourview.hresolu*sizeof(int));
522 <        if (yback == NULL) {
523 <                perror(progname);
524 <                return;
430 <        }
431 <        for (x = 0; x < ourview.hresolu; x++)
521 >        yback = (int *)malloc(hresolu*sizeof(int));
522 >        if (yback == NULL)
523 >                syserror();
524 >        for (x = 0; x < hresolu; x++)
525                  yback[x] = -2;
526          /*
527           * Xback and yback are the pixel locations of suitable
# Line 437 | Line 530 | fillpicture()                          /* fill in empty spaces */
530           * that there is no suitable background in this direction.
531           */
532                                                          /* fill image */
533 <        for (y = 0; y < ourview.vresolu; y++) {
533 >        for (y = 0; y < vresolu; y++) {
534                  xback = -2;
535 <                for (x = 0; x < ourview.hresolu; x++)
535 >                for (x = 0; x < hresolu; x++)
536                          if (zscan(y)[x] <= 0) {         /* empty pixel */
537                                  /*
538                                   * First, find background from above or below.
539                                   * (farthest assigned pixel)
540                                   */
541                                  if (yback[x] == -2) {
542 <                                        for (i = y+1; i < ourview.vresolu; i++)
542 >                                        for (i = y+1; i < vresolu; i++)
543                                                  if (zscan(i)[x] > 0)
544                                                          break;
545 <                                        if (i < ourview.vresolu
545 >                                        if (i < vresolu
546                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
547                                                  yback[x] = i;
548                                          else
# Line 459 | Line 552 | fillpicture()                          /* fill in empty spaces */
552                                   * Next, find background from left or right.
553                                   */
554                                  if (xback == -2) {
555 <                                        for (i = x+1; i < ourview.hresolu; i++)
555 >                                        for (i = x+1; i < hresolu; i++)
556                                                  if (zscan(y)[i] > 0)
557                                                          break;
558 <                                        if (i < ourview.hresolu
558 >                                        if (i < hresolu
559                                  && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
560                                                  xback = i;
561                                          else
562                                                  xback = x-1;
563                                  }
564                                  /*
565 <                                 * Check to see if we have no background for
566 <                                 * this pixel.  If not, use background color.
565 >                                 * If we have no background for this pixel,
566 >                                 * use the given fill function.
567                                   */
568 <                                if (xback < 0 && yback[x] < 0) {
569 <                                        copycolr(pscan(y)[x], backcolr);
477 <                                        continue;
478 <                                }
568 >                                if (xback < 0 && yback[x] < 0)
569 >                                        goto fillit;
570                                  /*
571                                   * Compare, and use the background that is
572                                   * farther, unless one of them is next to us.
573 +                                 * If the background is too distant, call
574 +                                 * the fill function.
575                                   */
576                                  if ( yback[x] < 0
577                                          || (xback >= 0 && ABS(x-xback) <= 1)
578                                          || ( ABS(y-yback[x]) > 1
579                                                  && zscan(yback[x])[x]
580 <                                                < zscan(y)[xback] ) )
580 >                                                < zscan(y)[xback] ) ) {
581 >                                        if (samp > 0 && ABS(x-xback) >= samp)
582 >                                                goto fillit;
583                                          copycolr(pscan(y)[x],pscan(y)[xback]);
584 <                                else
584 >                                        zscan(y)[x] = zscan(y)[xback];
585 >                                } else {
586 >                                        if (samp > 0 && ABS(y-yback[x]) > samp)
587 >                                                goto fillit;
588                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
589 +                                        zscan(y)[x] = zscan(yback[x])[x];
590 +                                }
591 +                                continue;
592 +                        fillit:
593 +                                (*fill)(x,y);
594 +                                if (fill == rcalfill) {         /* use it */
595 +                                        clearqueue();
596 +                                        xback = x;
597 +                                        yback[x] = y;
598 +                                }
599                          } else {                                /* full pixel */
600                                  yback[x] = -2;
601                                  xback = -2;
# Line 497 | Line 605 | fillpicture()                          /* fill in empty spaces */
605   }
606  
607  
608 < backpicture()                           /* paint in empty pixels */
608 > fillpicture(fill)               /* paint in empty pixels using fill */
609 > int     (*fill)();
610   {
611          register int    x, y;
612  
613 <        for (y = 0; y < ourview.vresolu; y++)
614 <                for (x = 0; x < ourview.hresolu; x++)
613 >        for (y = 0; y < vresolu; y++)
614 >                for (x = 0; x < hresolu; x++)
615                          if (zscan(y)[x] <= 0)
616 <                                copycolr(pscan(y)[x], backcolr);
616 >                                (*fill)(x,y);
617   }
618  
619  
# Line 512 | Line 621 | writepicture()                         /* write out picture */
621   {
622          int     y;
623  
624 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
625 <        for (y = ourview.vresolu-1; y >= 0; y--)
626 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) {
627 <                        perror(progname);
519 <                        exit(1);
520 <                }
624 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
625 >        for (y = vresolu-1; y >= 0; y--)
626 >                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
627 >                        syserror();
628   }
629  
630  
# Line 526 | Line 633 | char   *fname;
633   {
634          extern double   sqrt();
635          int     donorm = normdist && ourview.type == VT_PER;
636 <        FILE    *fp;
636 >        int     fd;
637          int     y;
638          float   *zout;
639  
640 <        if ((fp = fopen(fname, "w")) == NULL) {
640 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
641                  perror(fname);
642                  exit(1);
643          }
644          if (donorm
645 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
646 <                perror(progname);
647 <                exit(1);
541 <        }
542 <        for (y = ourview.vresolu-1; y >= 0; y--) {
645 >        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
646 >                syserror();
647 >        for (y = vresolu-1; y >= 0; y--) {
648                  if (donorm) {
649                          double  vx, yzn2;
650                          register int    x;
651 <                        yzn2 = y - .5*(ourview.vresolu-1);
652 <                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
653 <                        for (x = 0; x < ourview.hresolu; x++) {
654 <                                vx = x - .5*(ourview.hresolu-1);
651 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
652 >                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
653 >                        for (x = 0; x < hresolu; x++) {
654 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
655                                  zout[x] = zscan(y)[x]
656 <                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
656 >                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
657                          }
658                  } else
659                          zout = zscan(y);
660 <                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
661 <                                < ourview.hresolu) {
660 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
661 >                                < hresolu*sizeof(float)) {
662                          perror(fname);
663                          exit(1);
664                  }
665          }
666          if (donorm)
667                  free((char *)zout);
668 <        fclose(fp);
668 >        close(fd);
669   }
670  
671  
# Line 572 | Line 677 | register char  *s;
677                                  && *s != 'e' && *s != 'E' && *s != '+')
678                          return(0);
679          return(1);
680 + }
681 +
682 +
683 + backfill(x, y)                          /* fill pixel with background */
684 + int     x, y;
685 + {
686 +        register BYTE   *dest = pscan(y)[x];
687 +
688 +        copycolr(dest, backcolr);
689 +        zscan(y)[x] = backz;
690 + }
691 +
692 +
693 + calstart(prog, args)                    /* start fill calculation */
694 + char    *prog, *args;
695 + {
696 +        char    combuf[512];
697 +        int     p0[2], p1[2];
698 +
699 +        if (childpid != -1) {
700 +                fprintf(stderr, "%s: too many calculations\n", progname);
701 +                exit(1);
702 +        }
703 +        sprintf(combuf, prog, args);
704 +        if (pipe(p0) < 0 || pipe(p1) < 0)
705 +                syserror();
706 +        if ((childpid = vfork()) == 0) {        /* fork calculation */
707 +                close(p0[1]);
708 +                close(p1[0]);
709 +                if (p0[0] != 0) {
710 +                        dup2(p0[0], 0);
711 +                        close(p0[0]);
712 +                }
713 +                if (p1[1] != 1) {
714 +                        dup2(p1[1], 1);
715 +                        close(p1[1]);
716 +                }
717 +                execl("/bin/sh", "sh", "-c", combuf, 0);
718 +                perror("/bin/sh");
719 +                _exit(127);
720 +        }
721 +        if (childpid == -1)
722 +                syserror();
723 +        close(p0[0]);
724 +        close(p1[1]);
725 +        if ((psend = fdopen(p0[1], "w")) == NULL)
726 +                syserror();
727 +        if ((precv = fdopen(p1[0], "r")) == NULL)
728 +                syserror();
729 +        queuesiz = 0;
730 + }
731 +
732 +
733 + caldone()                               /* done with calculation */
734 + {
735 +        int     pid;
736 +
737 +        if (childpid == -1)
738 +                return;
739 +        clearqueue();
740 +        fclose(psend);
741 +        fclose(precv);
742 +        while ((pid = wait(0)) != -1 && pid != childpid)
743 +                ;
744 +        childpid = -1;
745 + }
746 +
747 +
748 + rcalfill(x, y)                          /* fill with ray-calculated pixel */
749 + int     x, y;
750 + {
751 +        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
752 +                clearqueue();
753 +                                        /* add position to queue */
754 +        queue[queuesiz][0] = x;
755 +        queue[queuesiz][1] = y;
756 +        queuesiz++;
757 + }
758 +
759 +
760 + clearqueue()                            /* process queue */
761 + {
762 +        FVECT   orig, dir;
763 +        float   fbuf[6];
764 +        register int    i;
765 +
766 +        for (i = 0; i < queuesiz; i++) {
767 +                viewray(orig, dir, &ourview,
768 +                                (queue[i][0]+.5)/hresolu,
769 +                                (queue[i][1]+.5)/vresolu);
770 +                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
771 +                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
772 +                fwrite((char *)fbuf, sizeof(float), 6, psend);
773 +        }
774 +                                        /* flush output and get results */
775 +        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
776 +        fwrite((char *)fbuf, sizeof(float), 6, psend);
777 +        if (fflush(psend) == EOF)
778 +                syserror();
779 +        for (i = 0; i < queuesiz; i++) {
780 +                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
781 +                        fprintf(stderr, "%s: read error in clearqueue\n",
782 +                                        progname);
783 +                        exit(1);
784 +                }
785 +                if (ourexp > 0 && ourexp != 1.0) {
786 +                        fbuf[0] *= ourexp;
787 +                        fbuf[1] *= ourexp;
788 +                        fbuf[2] *= ourexp;
789 +                }
790 +                setcolr(pscan(queue[i][1])[queue[i][0]],
791 +                                fbuf[0], fbuf[1], fbuf[2]);
792 +                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
793 +        }
794 +        queuesiz = 0;
795 + }
796 +
797 +
798 + syserror()                      /* report error and exit */
799 + {
800 +        perror(progname);
801 +        exit(1);
802   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines