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.24 by greg, Fri Aug 6 12:58:43 1993 UTC vs.
Revision 2.42 by greg, Mon Oct 20 16:01:55 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Generate sections of a picture.
6   */
7  
8 + #include "platform.h"
9   #include "standard.h"
12 #include <fcntl.h>
10  
11   #ifndef F_SETLKW
12  
# Line 24 | Line 21 | char *argv[];
21   #else
22  
23   #include <signal.h>
24 +
25   #include "color.h"
26   #include "view.h"
27 < #include "resolu.h"
27 > #include "rtprocess.h"
28  
29   #ifndef NFS
30   #define  NFS                    1
# Line 40 | Line 38 | char *argv[];
38   #endif
39   #endif
40                                          /* protection from SYSV signals(!) */
41 < #if defined(sgi) || defined(hpux)
41 > #if defined(sgi)
42   #define guard_io()      sighold(SIGALRM)
43   #define unguard()       sigrelse(SIGALRM)
44   #endif
# Line 48 | Line 46 | char *argv[];
46   #define guard_io()      0
47   #define unguard()       0
48   #endif
49 +
50 + extern char  *strerror();
51 +
52                                  /* rpict command */
53   char  *rpargv[128] = {"rpict", "-S", "1"};
54   int  rpargc = 3;
54 int  rpd[3];
55   FILE  *torp, *fromrp;
56   COLR  *pbuf;
57                                  /* our view parameters */
# Line 62 | Line 62 | int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
62   char  *outfile = NULL;
63   int  outfd;
64   long  scanorig;
65 < int  syncfd = -1;               /* lock file descriptor */
65 > FILE  *syncfp = NULL;           /* synchronization file pointer */
66 > int  synclst = F_UNLCK;         /* synchronization file lock status */
67   int  nforked = 0;
68  
69 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
70 +
71   char  *progname;
72   int  verbose = 0;
73 + unsigned  timelim = 0;
74 + int  rvrlim = -1;
75  
71 extern long  lseek(), ftell();
72
76   int  gotalrm = 0;
77 < int  onalrm() { gotalrm++; }
77 > void  onalrm(int i) { gotalrm++; }
78  
79  
80   main(argc, argv)
# Line 83 | Line 86 | char  *argv[];
86          progname = argv[0];
87          for (i = 1; i < argc; i++) {
88                                                  /* expand arguments */
89 <                while (rval = expandarg(&argc, &argv, i))
90 <                        if (rval < 0) {
91 <                                fprintf(stderr, "%s: cannot expand '%s'",
92 <                                                argv[0], argv[i]);
93 <                                exit(1);
94 <                        }
89 >                while ((rval = expandarg(&argc, &argv, i)) > 0)
90 >                        ;
91 >                if (rval < 0) {
92 >                        fprintf(stderr, "%s: cannot expand '%s'",
93 >                                        argv[0], argv[i]);
94 >                        exit(1);
95 >                }
96                  if (argv[i][0] == '-')
97                          switch (argv[i][1]) {
98                          case 'v':
# Line 115 | Line 119 | char  *argv[];
119                          case 'p':               /* pixel aspect ratio? */
120                                  if (argv[i][2] != 'a' || argv[i][3])
121                                          break;
122 <                                pixaspect = atof(argv[i+1]);
122 >                                pixaspect = atof(argv[++i]);
123                                  continue;
124 +                        case 'T':               /* time limit (hours) */
125 +                                if (argv[i][2])
126 +                                        break;
127 +                                timelim = atof(argv[++i])*3600. + .5;
128 +                                break;
129                          case 'x':               /* overall x resolution */
130                                  if (argv[i][2])
131                                          break;
# Line 137 | Line 146 | char  *argv[];
146                                          break;
147                                  vmult = atoi(argv[++i]);
148                                  continue;
149 +                        case 'R':               /* recover */
150 +                                if (argv[i][2])
151 +                                        break;
152 +                                rvrlim = 0;
153 +                        /* fall through */
154                          case 'F':               /* syncronization file */
155                                  if (argv[i][2])
156                                          break;
157 <                                if ((syncfd = open(argv[++i],
158 <                                                O_RDWR|O_CREAT, 0666)) < 0) {
157 >                                if ((syncfp =
158 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
159                                          fprintf(stderr, "%s: cannot open\n",
160                                                          argv[i]);
161                                          exit(1);
# Line 174 | Line 188 | char  *argv[];
188   }
189  
190  
191 + dolock(fd, ltyp)                /* lock or unlock a file */
192 + int  fd;
193 + int  ltyp;
194 + {
195 +        static struct flock  fls;       /* static so initialized to zeroes */
196 +
197 +        fls.l_type = ltyp;
198 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
199 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
200 +                                progname, strerror(errno));
201 +                exit(1);
202 +        }
203 + }
204 +
205 +
206   init(ac, av)                    /* set up output file and start rpict */
207   int  ac;
208   char  **av;
# Line 183 | Line 212 | char  **av;
212          char  *err;
213          FILE  *fp;
214          int  hr, vr;
215 +        SUBPROC  rpd; /* since we don't close_process(), this can be local */
216                                          /* set up view */
217          if ((err = setview(&ourview)) != NULL) {
218                  fprintf(stderr, "%s: %s\n", progname, err);
219                  exit(1);
220          }
221 <        if (syncfd != -1) {
222 <                char  buf[32];
223 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
224 <                sscanf(buf, "%d %d", &hmult, &vmult);
221 >        if (syncfp != NULL) {
222 >                sflock(F_RDLCK);
223 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
224 >                sflock(F_UNLCK);
225          }
226                                          /* compute piece size */
227          hres /= hmult;
# Line 206 | Line 236 | char  **av;
236          rpargv[rpargc] = NULL;
237                                          /* open output file */
238          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
239 +                dolock(outfd, F_WRLCK);
240                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
241                          goto filerr;
242 <                printargs(ac, av, fp);          /* write header */
242 >                newheader("RADIANCE", fp);      /* create header */
243 >                printargs(ac, av, fp);
244                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
245                  fputs(VIEWSTR, fp);
246                  fprintview(&ourview, fp);
# Line 219 | Line 251 | char  **av;
251                  putc('\n', fp);
252                  fprtresolu(hres*hmult, vres*vmult, fp);
253          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
254 +                dolock(outfd, F_RDLCK);
255                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
256                          goto filerr;
257                  getheader(fp, NULL, NULL);      /* skip header */
# Line 236 | Line 269 | char  **av;
269          scanorig = ftell(fp);           /* record position of first scanline */
270          if (fclose(fp) == -1)           /* done with stream i/o */
271                  goto filerr;
272 < #if NFS
240 <        sync();                         /* flush NFS buffers */
241 < #endif
272 >        dolock(outfd, F_UNLCK);
273                                          /* start rpict process */
274 <        if (open_process(rpd, rpargv) <= 0) {
274 >        if (open_process(&rpd, rpargv) <= 0) {
275                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
276                  exit(1);
277          }
278 <        if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
279 <                        (torp = fdopen(rpd[1], "w")) == NULL) {
278 >        if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
279 >                        (torp = fdopen(rpd.w, "w")) == NULL) {
280                  fprintf(stderr, "%s: cannot open stream to %s\n",
281                                  progname, rpargv[0]);
282                  exit(1);
# Line 255 | Line 286 | char  **av;
286                  exit(1);
287          }
288          signal(SIGALRM, onalrm);
289 +        if (timelim)
290 +                alarm(timelim);
291          return;
292   filerr:
293          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 266 | Line 299 | int
299   nextpiece(xp, yp)               /* get next piece assignment */
300   int  *xp, *yp;
301   {
269        struct flock  fls;
270        char  buf[64];
271
302          if (gotalrm)                    /* someone wants us to quit */
303                  return(0);
304 <        if (syncfd != -1) {             /* use sync file */
305 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
306 <                fls.l_whence = 0;
307 <                fls.l_start = 0L;
308 <                fls.l_len = 0L;
309 <                fcntl(syncfd, F_SETLKW, &fls);
310 <                lseek(syncfd, 0L, 0);
311 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
312 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
304 >        if (syncfp != NULL) {           /* use sync file */
305 >                /*
306 >                 * So we don't necessarily have to lock and unlock the file
307 >                 * multiple times (very slow), we establish an exclusive
308 >                 * lock at the beginning on our synchronization file and
309 >                 * maintain it in the subroutine rvrpiece().
310 >                 */
311 >                sflock(F_WRLCK);
312 >                fseek(syncfp, 0L, 0);           /* read position */
313 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
314                          *xp = hmult-1;
315                          *yp = vmult;
316                  }
317 +                if (rvrlim == 0)                /* initialize recovery limit */
318 +                        rvrlim = *xp*vmult + *yp;
319 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
320 +                        sflock(F_UNLCK);
321 +                        return(1);
322 +                }
323                  if (--(*yp) < 0) {              /* decrement position */
324                          *yp = vmult-1;
325 <                        if (--(*xp) < 0) {      /* all done! */
326 <                                close(syncfd);
325 >                        if (--(*xp) < 0) {      /* all done */
326 >                                sflock(F_UNLCK);
327                                  return(0);
328                          }
329                  }
330 <                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
331 <                lseek(syncfd, 0L, 0);           /* write new position */
332 <                write(syncfd, buf, strlen(buf));
333 <                fls.l_type = F_UNLCK;           /* release sync file */
297 <                fcntl(syncfd, F_SETLKW, &fls);
330 >                fseek(syncfp, 0L, 0);           /* write new position */
331 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
332 >                fflush(syncfp);
333 >                sflock(F_UNLCK);                /* release sync file */
334                  return(1);
335          }
336 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
301 <                return(0);
302 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
303 <                return(1);
304 <        fprintf(stderr, "%s: input format error\n", progname);
305 <        exit(cleanup(1));
336 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
337   }
338  
339  
340   int
341 + rvrpiece(xp, yp)                /* check for recoverable pieces */
342 + register int  *xp, *yp;
343 + {
344 +        static char  *pdone = NULL;     /* which pieces are done */
345 +        static long  readpos = -1;      /* how far we've read */
346 +        register int  i;
347 +        /*
348 +         * This routine is called by nextpiece() with an
349 +         * exclusive lock on syncfp and the file pointer at the
350 +         * appropriate position to read in the finished pieces.
351 +         */
352 +        if (rvrlim < 0)
353 +                return(0);              /* only check if asked */
354 +        if (pdone == NULL)              /* first call */
355 +                pdone = calloc(hmult*vmult, sizeof(char));
356 +        if (pdone == NULL) {
357 +                fprintf(stderr, "%s: out of memory\n", progname);
358 +                exit(1);
359 +        }
360 +        if (readpos != -1)              /* mark what's been done */
361 +                fseek(syncfp, readpos, 0);
362 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
363 +                pdone[*xp*vmult+*yp] = 1;
364 +        if (!feof(syncfp)) {
365 +                fprintf(stderr, "%s: format error in sync file\n", progname);
366 +                exit(1);
367 +        }
368 +        readpos = ftell(syncfp);
369 +        i = hmult*vmult;                /* find an unaccounted for piece */
370 +        while (i-- > rvrlim)
371 +                if (!pdone[i]) {
372 +                        *xp = i / vmult;
373 +                        *yp = i % vmult;
374 +                        pdone[i] = 1;   /* consider it done */
375 +                        return(1);
376 +                }
377 +        rvrlim = -1;                    /* nothing left to recover */
378 +        free(pdone);
379 +        pdone = NULL;
380 +        return(0);
381 + }
382 +
383 +
384 + int
385   cleanup(rstat)                  /* close rpict process and clean up */
386   int  rstat;
387   {
# Line 327 | Line 402 | rpiece()                       /* render picture piece by piece */
402          VIEW  pview;
403          int  xorg, yorg;
404                                          /* compute view parameters */
405 <        copystruct(&pview, &ourview);
405 >        pview = ourview;
406          switch (ourview.type) {
407          case VT_PER:
408                  pview.horiz = 2.*180./PI*atan(
# Line 340 | Line 415 | rpiece()                       /* render picture piece by piece */
415                  pview.horiz = ourview.horiz / hmult;
416                  pview.vert = ourview.vert / vmult;
417                  break;
418 +        case VT_CYL:
419 +                pview.horiz = ourview.horiz / hmult;
420 +                pview.vert = 2.*180./PI*atan(
421 +                                tan(PI/180./2.*ourview.vert)/vmult );
422 +                break;
423          case VT_HEM:
424                  pview.horiz = 2.*180./PI*asin(
425                                  sin(PI/180./2.*ourview.horiz)/hmult );
# Line 353 | Line 433 | rpiece()                       /* render picture piece by piece */
433          }
434                                          /* render each piece */
435          while (nextpiece(&xorg, &yorg)) {
436 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
437 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
436 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
437 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
438                  fputs(VIEWSTR, torp);
439                  fprintview(&pview, torp);
440                  putc('\n', torp);
# Line 373 | Line 453 | int  xpos, ypos;
453          int  hr, vr;
454          register int  y;
455                                  /* check bounds */
456 <        if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
456 >        if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) {
457                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
458                                  progname, xpos, ypos);
459                  exit(cleanup(1));
# Line 381 | Line 461 | int  xpos, ypos;
461                                  /* check header from rpict */
462          guard_io();
463          getheader(fromrp, NULL, NULL);
464 <        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
464 >        if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) {
465                  fprintf(stderr, "%s: resolution mismatch from %s\n",
466                                  progname, rpargv[0]);
467                  exit(cleanup(1));
# Line 422 | Line 502 | int  xpos, ypos;
502                                  /* lock file section so NFS doesn't mess up */
503          fls.l_whence = 0;
504          fls.l_type = F_WRLCK;
505 <        fcntl(outfd, F_SETLKW, &fls);
505 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
506 >                filerr("lock");
507   #endif
508                                  /* write new piece to file */
509 <        if (lseek(outfd, fls.l_start, 0) == -1)
510 <                goto seekerr;
509 >        if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0)
510 >                filerr("seek");
511          if (hmult == 1) {
512                  if (writebuf(outfd, (char *)pbuf,
513                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
514 <                        goto writerr;
514 >                        filerr("write");
515          } else
516                  for (y = 0; y < vr; y++) {
517                          if (writebuf(outfd, (char *)(pbuf+y*hr),
518                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
519 <                                goto writerr;
519 >                                filerr("write");
520                          if (y < vr-1 && lseek(outfd,
521 <                                        (long)(hmult-1)*hr*sizeof(COLR),
522 <                                        1) == -1)
523 <                                goto seekerr;
521 >                                        (off_t)(hmult-1)*hr*sizeof(COLR),
522 >                                        SEEK_CUR) < 0)
523 >                                filerr("seek");
524                  }
525 + #if NFS
526 +        fls.l_type = F_UNLCK;           /* release lock */
527 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
528 +                filerr("lock");
529 + #endif
530          if (verbose) {                          /* notify caller */
531                  printf("%d %d done\n", xpos, ypos);
532                  fflush(stdout);
533          }
534 <        if (pid == -1) {        /* didn't fork or fork failed */
535 < #if NFS
536 <                fls.l_type = F_UNLCK;           /* release lock */
537 <                fcntl(outfd, F_SETLKW, &fls);
538 < #endif
539 <                return(0);
534 >        if (syncfp != NULL) {                   /* record what's been done */
535 >                sflock(F_WRLCK);
536 >                fseek(syncfp, 0L, 2);           /* append index */
537 >                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
538 >                fflush(syncfp);
539 >                                /*** Unlock not necessary, since
540 >                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
541          }
542 <        _exit(0);               /* else exit child process (releasing lock) */
543 < seekerr:
544 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
545 <        _exit(1);
546 < writerr:
547 <        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
542 >        if (pid == -1)          /* didn't fork or fork failed */
543 >                return(0);
544 >        _exit(0);               /* else exit child process (releasing locks) */
545 > }
546 >
547 >
548 > filerr(t)                       /* report file error and exit */
549 > char  *t;
550 > {
551 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
552 >                        progname, t, outfile, strerror(errno));
553          _exit(1);
554   }
555  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines