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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines