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.8 by greg, Sun Aug 9 12:12:48 1992 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12   #include <fcntl.h>
13 + #include <signal.h>
14   #include "color.h"
15   #include "view.h"
16   #include "resolu.h"
# Line 19 | Line 20 | char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512",
20   int  rpargc = 9;
21   int  rpd[3];
22   FILE  *torp, *fromrp;
23 < COLR  *scanline;
23 > COLR  *pbuf;
24                                  /* our view parameters */
25   VIEW  ourview = STDVIEW;
26   double  pixaspect = 1.0;
# Line 35 | Line 36 | int  verbose = 0;
36  
37   extern long  lseek(), ftell();
38  
39 + int  gotalrm = 0;
40 + int  onalrm() { gotalrm++; }
41  
42 +
43   main(argc, argv)
44   int  argc;
45   char  *argv[];
# Line 157 | Line 161 | char  **av;
161                  getheader(fp, NULL);            /* skip header */
162                  if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
163                                  hr != hres*hmult || vr != vres*vmult) {
164 <                        fprintf(stderr, "%s: picture resolution mismatch\n",
165 <                                        outfile);
164 >                        fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
165 >                                        progname, outfile);
166                          exit(1);
167                  }
168          } else {
169 <                fprintf(stderr, "%s: cannot open\n", outfile);
169 >                fprintf(stderr, "%s: cannot open file \"%s\"\n",
170 >                                progname, outfile);
171                  exit(1);
172          }
173          scanorig = ftell(fp);           /* record position of first scanline */
174          if (fclose(fp) == -1)           /* done with stream i/o */
175                  goto filerr;
176 <        sync();                         /* avoid NFS buffering */
176 >        sync();                         /* flush NFS buffers */
177                                          /* start rpict process */
178          if (open_process(rpd, rpargv) <= 0) {
179                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 180 | Line 185 | char  **av;
185                                  progname, rpargv[0]);
186                  exit(1);
187          }
188 <        if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
188 >        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
189                  fprintf(stderr, "%s: out of memory\n", progname);
190                  exit(1);
191          }
192 +        signal(SIGALRM, onalrm);
193          return;
194   filerr:
195 <        fprintf(stderr, "%s: file i/o error\n", outfile);
195 >        fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
196          exit(1);
197   }
198  
# Line 199 | Line 205 | int  *xp, *yp;
205          struct flock  fls;
206          char  buf[64];
207  
208 <        if (syncfd != -1) {             /* using sync file */
208 >        if (gotalrm)                    /* someone wants us to quit */
209 >                return(0);
210 >        if (syncfd != -1) {             /* use sync file */
211                  fls.l_type = F_WRLCK;           /* gain exclusive access */
212                  fls.l_whence = 0;
213                  fls.l_start = 0L;
# Line 208 | Line 216 | int  *xp, *yp;
216                  lseek(syncfd, 0L, 0);
217                  buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
218                  if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
211                        *xp = hmult;
212                        *yp = vmult-1;
213                }
214                if (--(*xp) < 0) {              /* decrement position */
219                          *xp = hmult-1;
220 <                        if (--(*yp) < 0) {      /* all done! */
220 >                        *yp = vmult;
221 >                }
222 >                if (--(*yp) < 0) {              /* decrement position */
223 >                        *yp = vmult-1;
224 >                        if (--(*xp) < 0) {      /* all done! */
225                                  close(syncfd);
226                                  return(0);
227                          }
# Line 225 | Line 233 | int  *xp, *yp;
233                  fcntl(syncfd, F_SETLKW, &fls);
234                  return(1);
235          }
236 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* using stdin */
236 >        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
237                  return(0);
238          if (sscanf(buf, "%d %d", xp, yp) == 2)
239                  return(1);
# Line 239 | Line 247 | cleanup()                      /* close rpict process and clean up */
247   {
248          register int  rpstat;
249  
250 <        free((char *)scanline);
250 >        free((char *)pbuf);
251          fclose(torp);
252          fclose(fromrp);
253          rpstat = close_process(rpd);
# Line 297 | Line 305 | rpiece()                       /* render picture piece by piece */
305   putpiece(xpos, ypos)            /* get next piece from rpict */
306   int  xpos, ypos;
307   {
308 +        struct flock  fls;
309          int  hr, vr;
310 <        int  y;
311 <
310 >        register int  y;
311 >                                /* check bounds */
312          if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
313                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
314                                  progname, xpos, ypos);
315                  exit(1);
316          }
317 <        getheader(fromrp, NULL);        /* discard header info. */
318 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
319 <                        hr != hres || vr != vres) {
317 >                                /* check header from rpict */
318 >        getheader(fromrp, NULL);
319 >        if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) {
320                  fprintf(stderr, "%s: resolution mismatch from %s\n",
321                                  progname, rpargv[0]);
322                  exit(1);
323          }
324 <        for (y = 0; y < vr; y++) {      /* transfer scanlines */
325 <                if (freadcolrs(scanline, hr, fromrp) < 0) {
324 >                                /* load new piece into buffer */
325 >        for (y = 0; y < vr; y++)
326 >                if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
327                          fprintf(stderr, "%s: read error from %s\n",
328                                          progname, rpargv[0]);
329                          exit(1);
330                  }
331 <                if ((y == 0 || hmult != 1) && lseek(outfd,
332 < scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
333 <                                0) == -1) {
334 <                        fprintf(stderr, "%s: seek error\n", outfile);
335 <                        exit(1);
336 <                }
337 <                if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
331 >                                /* lock file section so NFS doesn't mess up */
332 >        fls.l_whence = 0;
333 >        fls.l_len = (long)vres*hmult*hres*sizeof(COLR);
334 >        fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len;
335 >        fls.l_type = F_WRLCK;
336 >        fcntl(outfd, F_SETLKW, &fls);
337 >                                /* write new piece to file */
338 >        if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1)
339 >                goto seekerr;
340 >        for (y = 0; y < vr; y++) {
341 >                if (writebuf(outfd, (char *)(pbuf+y*hr), hr*sizeof(COLR)) !=
342                                  hr*sizeof(COLR)) {
343 <                        fprintf(stderr, "%s: write error\n", outfile);
343 >                        fprintf(stderr, "%s: write error on file \"%s\"\n",
344 >                                        progname, outfile);
345                          exit(1);
346                  }
347 +                if (hmult != 1 && y < vr-1 && lseek(outfd,
348 +                                (long)(hmult-1)*hr*sizeof(COLR), 1) == -1)
349 +                        goto seekerr;
350          }
351 +                                /* unlock file section */
352 +        fls.l_type = F_UNLCK;
353 +        fcntl(outfd, F_SETLKW, &fls);
354 +        return;
355 + seekerr:
356 +        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
357 +        exit(1);
358   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines