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

Comparing ray/src/px/pinterp.c (file contents):
Revision 1.12 by greg, Thu Jan 4 14:25:56 1990 UTC vs.
Revision 2.2 by greg, Thu Dec 19 14:51:55 1991 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines