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.5 by greg, Fri Aug 7 11:05:30 1992 UTC vs.
Revision 2.21 by greg, Fri Jun 4 17:03:29 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12   #include <fcntl.h>
13 +
14 + #ifndef F_SETLKW
15 +
16 + main(argc, argv)
17 + int argc;
18 + char *argv[];
19 + {
20 +        fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
21 +        exit(1);
22 + }
23 +
24 + #else
25 +
26 + #include <signal.h>
27   #include "color.h"
28   #include "view.h"
29   #include "resolu.h"
30  
31 + #ifndef NFS
32 + #define  NFS                    1
33 + #endif
34 +                                /* set the following to 0 to forgo forking */
35 + #ifndef MAXFORK
36 + #if NFS
37 + #define  MAXFORK                3       /* allotment of duped processes */
38 + #else
39 + #define  MAXFORK                0
40 + #endif
41 + #endif
42 +                                        /* protection from SYSV signals(!) */
43 + #if defined(sgi) || defined(hpux)
44 + #define guard_io()      sighold(SIGALRM)
45 + #define unguard()       sigrelse(SIGALRM)
46 + #endif
47 + #ifndef guard_io
48 + #define guard_io()      0
49 + #define unguard()       0
50 + #endif
51                                  /* rpict command */
52   char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
53   int  rpargc = 9;
54   int  rpd[3];
55   FILE  *torp, *fromrp;
56 < COLR  *scanline;
56 > COLR  *pbuf;
57                                  /* our view parameters */
58   VIEW  ourview = STDVIEW;
59   double  pixaspect = 1.0;
# Line 29 | Line 63 | char  *outfile = NULL;
63   int  outfd;
64   long  scanorig;
65   int  syncfd = -1;               /* lock file descriptor */
66 + int  nforked = 0;
67  
68   char  *progname;
69   int  verbose = 0;
70  
71   extern long  lseek(), ftell();
72  
73 + int  gotalrm = 0;
74 + int  onalrm() { gotalrm++; }
75  
76 +
77   main(argc, argv)
78   int  argc;
79   char  *argv[];
# Line 92 | Line 130 | char  *argv[];
130                          case 'F':               /* syncronization file */
131                                  if (argv[i][2])
132                                          break;
133 <                                if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
133 >                                if ((syncfd = open(argv[++i],
134 >                                                O_RDWR|O_CREAT, 0666)) < 0) {
135                                          fprintf(stderr, "%s: cannot open\n",
136                                                          argv[i]);
137                                          exit(1);
138                                  }
139                                  continue;
140 +                        case 'z':               /* z-file ist verbotten */
141 +                                fprintf(stderr, "%s: -z option not allowed\n",
142 +                                                argv[0]);
143 +                                exit(1);
144                          case 'o':               /* output file */
145                                  if (argv[i][2])
146                                          break;
# Line 113 | Line 156 | char  *argv[];
156          }
157          init(argc, argv);
158          rpiece();
159 <        rval = cleanup();
117 <        exit(rval);
159 >        exit(cleanup(0));
160   }
161  
162  
# Line 154 | Line 196 | char  **av;
196          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
197                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
198                          goto filerr;
199 <                getheader(fp, NULL);            /* skip header */
200 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
199 >                getheader(fp, NULL, NULL);      /* skip header */
200 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
201                                  hr != hres*hmult || vr != vres*vmult) {
202 <                        fprintf(stderr, "%s: picture resolution mismatch\n",
203 <                                        outfile);
202 >                        fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
203 >                                        progname, outfile);
204                          exit(1);
205                  }
206          } else {
207 <                fprintf(stderr, "%s: cannot open\n", outfile);
207 >                fprintf(stderr, "%s: cannot open file \"%s\"\n",
208 >                                progname, outfile);
209                  exit(1);
210          }
211          scanorig = ftell(fp);           /* record position of first scanline */
212          if (fclose(fp) == -1)           /* done with stream i/o */
213                  goto filerr;
214 <        sync();                         /* avoid NFS buffering */
214 > #if NFS
215 >        sync();                         /* flush NFS buffers */
216 > #endif
217                                          /* start rpict process */
218          if (open_process(rpd, rpargv) <= 0) {
219                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 180 | Line 225 | char  **av;
225                                  progname, rpargv[0]);
226                  exit(1);
227          }
228 <        if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
228 >        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
229                  fprintf(stderr, "%s: out of memory\n", progname);
230                  exit(1);
231          }
232 +        signal(SIGALRM, onalrm);
233          return;
234   filerr:
235 <        fprintf(stderr, "%s: file i/o error\n", outfile);
235 >        fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
236          exit(1);
237   }
238  
# Line 195 | Line 241 | int
241   nextpiece(xp, yp)               /* get next piece assignment */
242   int  *xp, *yp;
243   {
198        extern char  *fgets();
244          struct flock  fls;
245          char  buf[64];
246  
247 <        if (syncfd != -1) {             /* using sync file */
247 >        if (gotalrm)                    /* someone wants us to quit */
248 >                return(0);
249 >        if (syncfd != -1) {             /* use sync file */
250                  fls.l_type = F_WRLCK;           /* gain exclusive access */
251                  fls.l_whence = 0;
252                  fls.l_start = 0L;
# Line 208 | Line 255 | int  *xp, *yp;
255                  lseek(syncfd, 0L, 0);
256                  buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
257                  if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
211                        *xp = hmult;
212                        *yp = vmult-1;
213                }
214                if (--(*xp) < 0) {              /* decrement position */
258                          *xp = hmult-1;
259 <                        if (--(*yp) < 0) {      /* all done! */
259 >                        *yp = vmult;
260 >                }
261 >                if (--(*yp) < 0) {              /* decrement position */
262 >                        *yp = vmult-1;
263 >                        if (--(*xp) < 0) {      /* all done! */
264                                  close(syncfd);
265                                  return(0);
266                          }
267                  }
268 <                sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
268 >                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
269                  lseek(syncfd, 0L, 0);           /* write new position */
270                  write(syncfd, buf, strlen(buf));
271                  fls.l_type = F_UNLCK;           /* release sync file */
272                  fcntl(syncfd, F_SETLKW, &fls);
273                  return(1);
274          }
275 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* using stdin */
275 >        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
276                  return(0);
277          if (sscanf(buf, "%d %d", xp, yp) == 2)
278                  return(1);
279          fprintf(stderr, "%s: input format error\n", progname);
280 <        exit(1);
280 >        exit(cleanup(1));
281   }
282  
283  
284   int
285 < cleanup()                       /* close rpict process and clean up */
285 > cleanup(rstat)                  /* close rpict process and clean up */
286 > int  rstat;
287   {
288 <        register int  rpstat;
288 >        int  status;
289  
290 <        free((char *)scanline);
290 >        free((char *)pbuf);
291          fclose(torp);
292          fclose(fromrp);
293 <        rpstat = close_process(rpd);
294 <        if (rpstat == -1)
295 <                rpstat = 0;
296 <        return(rpstat);
293 >        while (wait(&status) != -1)
294 >                if (rstat == 0)
295 >                        rstat = status>>8 & 0xff;
296 >        return(rstat);
297   }
298  
299  
# Line 253 | Line 301 | rpiece()                       /* render picture piece by piece */
301   {
302          VIEW  pview;
303          int  xorg, yorg;
304 <        
304 >                                        /* compute view parameters */
305 >        copystruct(&pview, &ourview);
306 >        switch (ourview.type) {
307 >        case VT_PER:
308 >                pview.horiz = 2.*180./PI*atan(
309 >                                tan(PI/180./2.*ourview.horiz)/hmult );
310 >                pview.vert = 2.*180./PI*atan(
311 >                                tan(PI/180./2.*ourview.vert)/vmult );
312 >                break;
313 >        case VT_PAR:
314 >        case VT_ANG:
315 >                pview.horiz = ourview.horiz / hmult;
316 >                pview.vert = ourview.vert / vmult;
317 >                break;
318 >        case VT_HEM:
319 >                pview.horiz = 2.*180./PI*asin(
320 >                                sin(PI/180./2.*ourview.horiz)/hmult );
321 >                pview.vert = 2.*180./PI*asin(
322 >                                sin(PI/180./2.*ourview.vert)/vmult );
323 >                break;
324 >        default:
325 >                fprintf(stderr, "%s: unknown view type '-vt%c'\n",
326 >                                progname, ourview.type);
327 >                exit(cleanup(1));
328 >        }
329 >                                        /* render each piece */
330          while (nextpiece(&xorg, &yorg)) {
331 <                copystruct(&pview, &ourview);   /* compute view for piece */
332 <                switch (ourview.type) {
260 <                case VT_PER:
261 <                        pview.horiz = 2.*180./PI*atan(
262 <                                        tan(PI/180./2.*ourview.horiz)/hmult );
263 <                        pview.vert = 2.*180./PI*atan(
264 <                                        tan(PI/180./2.*ourview.vert)/vmult );
265 <                        break;
266 <                case VT_PAR:
267 <                case VT_ANG:
268 <                        pview.horiz = ourview.horiz / hmult;
269 <                        pview.vert = ourview.vert / vmult;
270 <                        break;
271 <                case VT_HEM:
272 <                        pview.horiz = 2.*180./PI*asin(
273 <                                        sin(PI/180./2.*ourview.horiz)/hmult );
274 <                        pview.vert = 2.*180./PI*asin(
275 <                                        sin(PI/180./2.*ourview.vert)/vmult );
276 <                        break;
277 <                default:
278 <                        fprintf(stderr, "%s: unknown view type '-vt%c'\n",
279 <                                        progname, ourview.type);
280 <                        exit(1);
281 <                }
282 <                pview.hoff += xorg - 0.5*(hmult-1);
283 <                pview.voff += yorg - 0.5*(vmult-1);
331 >                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
332 >                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
333                  fputs(VIEWSTR, torp);
334                  fprintview(&pview, torp);
335                  putc('\n', torp);
336 <                fflush(torp);                   /* assign piece to rpict */
336 >                fflush(torp);                   /* assigns piece to rpict */
337                  putpiece(xorg, yorg);           /* place piece in output */
289                if (verbose) {                  /* notify caller */
290                        printf("%d %d done\n", xorg, yorg);
291                        fflush(stdout);
292                }
338          }
339   }
340  
341  
342 + int
343   putpiece(xpos, ypos)            /* get next piece from rpict */
344   int  xpos, ypos;
345   {
346 +        struct flock  fls;
347 +        int  pid, status;
348          int  hr, vr;
349 <        int  y;
350 <
349 >        register int  y;
350 >                                /* check bounds */
351          if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
352                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
353                                  progname, xpos, ypos);
354 <                exit(1);
354 >                exit(cleanup(1));
355          }
356 <        getheader(fromrp, NULL);        /* discard header info. */
357 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
358 <                        hr != hres || vr != vres) {
356 >                                /* check header from rpict */
357 >        guard_io();
358 >        getheader(fromrp, NULL, NULL);
359 >        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
360                  fprintf(stderr, "%s: resolution mismatch from %s\n",
361                                  progname, rpargv[0]);
362 <                exit(1);
362 >                exit(cleanup(1));
363          }
364 <        for (y = 0; y < vr; y++) {      /* transfer scanlines */
365 <                if (freadcolrs(scanline, hr, fromrp) < 0) {
364 >        unguard();
365 >                                /* load new piece into buffer */
366 >        for (y = 0; y < vr; y++) {
367 >                guard_io();
368 >                if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
369                          fprintf(stderr, "%s: read error from %s\n",
370                                          progname, rpargv[0]);
371 <                        exit(1);
371 >                        exit(cleanup(1));
372                  }
373 <                if ((y == 0 || hmult != 1) && lseek(outfd,
374 < scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
375 <                                0) == -1) {
376 <                        fprintf(stderr, "%s: seek error\n", outfile);
377 <                        exit(1);
373 >                unguard();
374 >        }
375 > #if MAXFORK
376 >                                /* fork so we don't slow rpict down */
377 >        if ((pid = fork()) > 0) {
378 >                if (++nforked >= MAXFORK) {
379 >                        wait(&status);          /* reap a child */
380 >                        if (status)
381 >                                exit(cleanup(status>>8 & 0xff));
382 >                        nforked--;
383                  }
384 <                if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
385 <                                hr*sizeof(COLR)) {
386 <                        fprintf(stderr, "%s: write error\n", outfile);
387 <                        exit(1);
384 >                return(pid);
385 >        }
386 > #else
387 >        pid = -1;               /* no forking */
388 > #endif
389 >        fls.l_start = scanorig +
390 >                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
391 > #if NFS
392 >        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
393 >                                /* lock file section so NFS doesn't mess up */
394 >        fls.l_whence = 0;
395 >        fls.l_type = F_WRLCK;
396 >        fcntl(outfd, F_SETLKW, &fls);
397 > #endif
398 >                                /* write new piece to file */
399 >        if (lseek(outfd, fls.l_start, 0) == -1)
400 >                goto seekerr;
401 >        if (hmult == 1) {
402 >                if (writebuf(outfd, (char *)pbuf,
403 >                                vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
404 >                        goto writerr;
405 >        } else
406 >                for (y = 0; y < vr; y++) {
407 >                        if (writebuf(outfd, (char *)(pbuf+y*hr),
408 >                                        hr*sizeof(COLR)) != hr*sizeof(COLR))
409 >                                goto writerr;
410 >                        if (y < vr-1 && lseek(outfd,
411 >                                        (long)(hmult-1)*hr*sizeof(COLR),
412 >                                        1) == -1)
413 >                                goto seekerr;
414                  }
415 +        if (verbose) {                          /* notify caller */
416 +                printf("%d %d done\n", xpos, ypos);
417 +                fflush(stdout);
418          }
419 +        if (pid == -1) {        /* didn't fork or fork failed */
420 + #if NFS
421 +                fls.l_type = F_UNLCK;           /* release lock */
422 +                fcntl(outfd, F_SETLKW, &fls);
423 + #endif
424 +                return(0);
425 +        }
426 +        _exit(0);               /* else exit child process (releasing lock) */
427 + seekerr:
428 +        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
429 +        _exit(1);
430 + writerr:
431 +        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
432 +        _exit(1);
433   }
434 +
435 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines