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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines