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

Comparing ray/src/util/rpiece.c (file contents):
Revision 2.8 by greg, Sun Aug 9 12:12:48 1992 UTC vs.
Revision 2.14 by greg, Wed Dec 2 17:07:00 1992 UTC

# Line 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   #include "view.h"
16   #include "resolu.h"
17  
18 + #ifndef NFS
19 + #define  NFS                    1
20 + #endif
21 +                                /* set the following to 0 to forgo forking */
22 + #ifndef MAXFORK
23 + #if NFS
24 + #define  MAXFORK                3       /* allotment of duped processes */
25 + #else
26 + #define  MAXFORK                0
27 + #endif
28 + #endif
29 +
30                                  /* rpict command */
31   char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
32   int  rpargc = 9;
# Line 30 | Line 42 | char  *outfile = NULL;
42   int  outfd;
43   long  scanorig;
44   int  syncfd = -1;               /* lock file descriptor */
45 + int  nforked = 0;
46  
47   char  *progname;
48   int  verbose = 0;
# Line 96 | Line 109 | char  *argv[];
109                          case 'F':               /* syncronization file */
110                                  if (argv[i][2])
111                                          break;
112 <                                if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
112 >                                if ((syncfd = open(argv[++i],
113 >                                                O_RDWR|O_CREAT, 0666)) < 0) {
114                                          fprintf(stderr, "%s: cannot open\n",
115                                                          argv[i]);
116                                          exit(1);
# Line 117 | Line 131 | char  *argv[];
131          }
132          init(argc, argv);
133          rpiece();
134 <        rval = cleanup();
134 >        rval = cleanup(0);
135          exit(rval);
136   }
137  
# Line 158 | Line 172 | char  **av;
172          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
173                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
174                          goto filerr;
175 <                getheader(fp, NULL);            /* skip header */
175 >                getheader(fp, NULL, NULL);      /* skip header */
176                  if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
177                                  hr != hres*hmult || vr != vres*vmult) {
178                          fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
# Line 173 | Line 187 | char  **av;
187          scanorig = ftell(fp);           /* record position of first scanline */
188          if (fclose(fp) == -1)           /* done with stream i/o */
189                  goto filerr;
190 + #if NFS
191          sync();                         /* flush NFS buffers */
192 + #endif
193                                          /* start rpict process */
194          if (open_process(rpd, rpargv) <= 0) {
195                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 238 | Line 254 | int  *xp, *yp;
254          if (sscanf(buf, "%d %d", xp, yp) == 2)
255                  return(1);
256          fprintf(stderr, "%s: input format error\n", progname);
257 <        exit(1);
257 >        exit(cleanup(1));
258   }
259  
260  
261   int
262 < cleanup()                       /* close rpict process and clean up */
262 > cleanup(rstat)                  /* close rpict process and clean up */
263 > int  rstat;
264   {
265 <        register int  rpstat;
265 >        int  status;
266  
267          free((char *)pbuf);
268          fclose(torp);
269          fclose(fromrp);
270 <        rpstat = close_process(rpd);
271 <        if (rpstat == -1)
272 <                rpstat = 0;
273 <        return(rpstat);
270 >        while (wait(&status) != -1)
271 >                if (rstat == 0)
272 >                        rstat = status>>8 & 0xff;
273 >        return(rstat);
274   }
275  
276  
# Line 261 | Line 278 | rpiece()                       /* render picture piece by piece */
278   {
279          VIEW  pview;
280          int  xorg, yorg;
281 <        
281 >                                        /* compute view parameters */
282 >        copystruct(&pview, &ourview);
283 >        switch (ourview.type) {
284 >        case VT_PER:
285 >                pview.horiz = 2.*180./PI*atan(
286 >                                tan(PI/180./2.*ourview.horiz)/hmult );
287 >                pview.vert = 2.*180./PI*atan(
288 >                                tan(PI/180./2.*ourview.vert)/vmult );
289 >                break;
290 >        case VT_PAR:
291 >        case VT_ANG:
292 >                pview.horiz = ourview.horiz / hmult;
293 >                pview.vert = ourview.vert / vmult;
294 >                break;
295 >        case VT_HEM:
296 >                pview.horiz = 2.*180./PI*asin(
297 >                                sin(PI/180./2.*ourview.horiz)/hmult );
298 >                pview.vert = 2.*180./PI*asin(
299 >                                sin(PI/180./2.*ourview.vert)/vmult );
300 >                break;
301 >        default:
302 >                fprintf(stderr, "%s: unknown view type '-vt%c'\n",
303 >                                progname, ourview.type);
304 >                exit(cleanup(1));
305 >        }
306 >                                        /* render each piece */
307          while (nextpiece(&xorg, &yorg)) {
308 <                copystruct(&pview, &ourview);   /* compute view for piece */
309 <                switch (ourview.type) {
268 <                case VT_PER:
269 <                        pview.horiz = 2.*180./PI*atan(
270 <                                        tan(PI/180./2.*ourview.horiz)/hmult );
271 <                        pview.vert = 2.*180./PI*atan(
272 <                                        tan(PI/180./2.*ourview.vert)/vmult );
273 <                        break;
274 <                case VT_PAR:
275 <                case VT_ANG:
276 <                        pview.horiz = ourview.horiz / hmult;
277 <                        pview.vert = ourview.vert / vmult;
278 <                        break;
279 <                case VT_HEM:
280 <                        pview.horiz = 2.*180./PI*asin(
281 <                                        sin(PI/180./2.*ourview.horiz)/hmult );
282 <                        pview.vert = 2.*180./PI*asin(
283 <                                        sin(PI/180./2.*ourview.vert)/vmult );
284 <                        break;
285 <                default:
286 <                        fprintf(stderr, "%s: unknown view type '-vt%c'\n",
287 <                                        progname, ourview.type);
288 <                        exit(1);
289 <                }
290 <                pview.hoff += xorg - 0.5*(hmult-1);
291 <                pview.voff += yorg - 0.5*(vmult-1);
308 >                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
309 >                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
310                  fputs(VIEWSTR, torp);
311                  fprintview(&pview, torp);
312                  putc('\n', torp);
313 <                fflush(torp);                   /* assign piece to rpict */
313 >                fflush(torp);                   /* assigns piece to rpict */
314                  putpiece(xorg, yorg);           /* place piece in output */
315                  if (verbose) {                  /* notify caller */
316                          printf("%d %d done\n", xorg, yorg);
# Line 302 | Line 320 | rpiece()                       /* render picture piece by piece */
320   }
321  
322  
323 + int
324   putpiece(xpos, ypos)            /* get next piece from rpict */
325   int  xpos, ypos;
326   {
327          struct flock  fls;
328 +        int  pid, status;
329          int  hr, vr;
330          register int  y;
331                                  /* check bounds */
332          if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
333                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
334                                  progname, xpos, ypos);
335 <                exit(1);
335 >                exit(cleanup(1));
336          }
337                                  /* check header from rpict */
338 <        getheader(fromrp, NULL);
339 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) {
338 >        getheader(fromrp, NULL, NULL);
339 >        if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) {
340                  fprintf(stderr, "%s: resolution mismatch from %s\n",
341                                  progname, rpargv[0]);
342 <                exit(1);
342 >                exit(cleanup(1));
343          }
344                                  /* load new piece into buffer */
345          for (y = 0; y < vr; y++)
346                  if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
347                          fprintf(stderr, "%s: read error from %s\n",
348                                          progname, rpargv[0]);
349 <                        exit(1);
349 >                        exit(cleanup(1));
350                  }
351 + #if MAXFORK
352 +                                /* fork so we don't slow rpict down */
353 +        if ((pid = fork()) > 0) {
354 +                if (++nforked >= MAXFORK) {
355 +                        wait(&status);          /* reap a child */
356 +                        if (status)
357 +                                exit(cleanup(status>>8 & 0xff));
358 +                        nforked--;
359 +                }
360 +                return(pid);
361 +        }
362 + #else
363 +        pid = -1;               /* no forking */
364 + #endif
365 + #if NFS
366                                  /* lock file section so NFS doesn't mess up */
367          fls.l_whence = 0;
368          fls.l_len = (long)vres*hmult*hres*sizeof(COLR);
369          fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len;
370          fls.l_type = F_WRLCK;
371          fcntl(outfd, F_SETLKW, &fls);
372 + #endif
373                                  /* write new piece to file */
374          if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1)
375                  goto seekerr;
376 <        for (y = 0; y < vr; y++) {
377 <                if (writebuf(outfd, (char *)(pbuf+y*hr), hr*sizeof(COLR)) !=
378 <                                hr*sizeof(COLR)) {
379 <                        fprintf(stderr, "%s: write error on file \"%s\"\n",
380 <                                        progname, outfile);
381 <                        exit(1);
376 >        if (hmult == 1) {
377 >                if (writebuf(outfd, (char *)pbuf,
378 >                                vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
379 >                        goto writerr;
380 >        } else
381 >                for (y = 0; y < vr; y++) {
382 >                        if (writebuf(outfd, (char *)(pbuf+y*hr),
383 >                                        hr*sizeof(COLR)) != hr*sizeof(COLR))
384 >                                goto writerr;
385 >                        if (y < vr-1 && lseek(outfd,
386 >                                        (long)(hmult-1)*hr*sizeof(COLR),
387 >                                        1) == -1)
388 >                                goto seekerr;
389                  }
390 <                if (hmult != 1 && y < vr-1 && lseek(outfd,
391 <                                (long)(hmult-1)*hr*sizeof(COLR), 1) == -1)
392 <                        goto seekerr;
390 >        if (pid == -1) {        /* fork failed */
391 > #if NFS
392 >                fls.l_type = F_UNLCK;           /* release lock */
393 >                fcntl(outfd, F_SETLKW, &fls);
394 > #endif
395 >                return(0);
396          }
397 <                                /* unlock file section */
352 <        fls.l_type = F_UNLCK;
353 <        fcntl(outfd, F_SETLKW, &fls);
354 <        return;
397 >        _exit(0);               /* else exit child process (releasing lock) */
398   seekerr:
399          fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
400 <        exit(1);
400 >        _exit(1);
401 > writerr:
402 >        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
403 >        _exit(1);
404   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines