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

Comparing ray/src/util/rcrop.c (file contents):
Revision 1.1 by greg, Tue Mar 15 00:25:50 2022 UTC vs.
Revision 1.9 by greg, Wed Mar 16 01:51:16 2022 UTC

# Line 18 | Line 18 | char   *progname;              /* global argv[0] */
18  
19   VIEW    vw = STDVIEW;
20   int     gotvw = 0;
21 < char    fmt[MAXFMTLEN] = "Unknown";
21 > char    fmt[MAXFMTLEN] = "ascii";       /* assumed when unspecified */
22   int     ncomp = 0;
23   RESOLU  res;
24   int     rmin, cmin, nrows, ncols;
# Line 79 | Line 79 | colr_copyf(FILE *fp)
79                  return(1);
80   writerr:
81          fputs(progname, stderr);
82 <        fputs(": error writing scanline\n", stderr);
82 >        fputs(": error writing picture\n", stderr);
83          return(0);
84   readerr:
85          fputs(progname, stderr);
86 <        fputs(": error reading scanline\n", stderr);
86 >        fputs(": error reading picture\n", stderr);
87          return(0);
88   }
89  
# Line 91 | Line 91 | readerr:
91   static int
92   binary_copyf(FILE *fp, int asize)
93   {
94 +        const int       skip_thresh = 8192;
95          const size_t    elsiz = asize*ncomp;
96          const int       width = scanlen(&res);
97 <        char            *buf = (char *)malloc(elsiz*width);
97 >        const long      skip_len = (width-ncols)*elsiz;
98 >        char            *buf;
99          int             y;
100 <
101 <        if (!buf) {
102 <                fputs(progname, stderr);
103 <                fputs(": out of memory!\n", stderr);
104 <                return(0);
105 <        }
100 >                                        /* check if fseek() useful */
101 >        if (skip_len > skip_thresh &&
102 >                        fseek(fp, (rmin*width + cmin)*elsiz, SEEK_CUR) == 0) {
103 >                buf = (char *)malloc(ncols*elsiz);
104 >                if (!buf)
105 >                        goto memerr;
106 >                for (y = nrows; y-- > 0; ) {
107 >                        if (getbinary(buf, elsiz, ncols, fp) != ncols)
108 >                                goto readerr;
109 >                        if (putbinary(buf, elsiz, ncols, stdout) != ncols)
110 >                                goto writerr;
111 >                        if (y && fseek(fp, skip_len, SEEK_CUR) < 0) {
112 >                                fputs(progname, stderr);
113 >                                fputs(": unexpected seek error on input\n", stderr);
114 >                                return(0);
115 >                        }
116 >                }
117 >                free(buf);              /* success! */
118 >                return(1);
119 >        }                               /* else need to read it all... */
120 >        buf = (char *)malloc(width*elsiz);
121 >        if (!buf)
122 >                goto memerr;
123                                          /* skip rows as requested */
124 <        if (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0) {
124 >        if (skip_len > skip_thresh ||
125 >                        (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0))
126                  for (y = 0; y < rmin; y++)
127                          if (getbinary(buf, elsiz, width, fp) != width)
128                                  goto readerr;
109        }
129          for (y = 0; y < nrows; y++) {   /* copy portion */
130                  if (getbinary(buf, elsiz, width, fp) != width)
131                          goto readerr;
# Line 124 | Line 143 | readerr:
143          fputs(progname, stderr);
144          fputs(": error reading binary data\n", stderr);
145          return(0);
146 + memerr:
147 +        fputs(progname, stderr);
148 +        fputs(": out of memory!\n", stderr);
149 +        return(0);
150   }
151  
152   /* Read (and copy) specified number of white-space-separated words */
# Line 211 | Line 234 | main(int argc, char *argv[])
234                  fputs(": cannot open for writing\n", stderr);
235                  return(1);
236          }
237 + #ifdef getc_unlocked            /* avoid stupid semaphores */
238 +        flockfile(fp);
239 +        flockfile(stdout);
240 + #endif
241                                  /* process information header */
242          if (getheader(fp, headline, NULL) < 0) {
243                  fputs(progname, stderr);
# Line 234 | Line 261 | main(int argc, char *argv[])
261                  fputs(": illegal crop\n", stderr);
262                  return(1);
263          }
264 <        printargs(argc, argv, stdout);
265 <        if (gotvw) {
264 >        printargs(5, argv, stdout);
265 >        if (gotvw) {            /* adjust view? */
266                  double          p0[2], p1[2];
267                  const char      *err;
268                  if (res.rt & YMAJOR) {
# Line 244 | Line 271 | main(int argc, char *argv[])
271                          p1[0] = (cmin+ncols)/(double)res.xr;
272                          p1[1] = (rmin+nrows)/(double)res.yr;
273                  } else {
274 <                        p0[1] = cmin/(double)res.xr;
275 <                        p0[0] = rmin/(double)res.yr;
276 <                        p1[1] = (cmin+ncols)/(double)res.xr;
277 <                        p1[0] = (rmin+nrows)/(double)res.yr;
274 >                        p0[0] = rmin/(double)res.xr;
275 >                        p0[1] = cmin/(double)res.yr;
276 >                        p1[0] = (rmin+nrows)/(double)res.xr;
277 >                        p1[1] = (cmin+ncols)/(double)res.yr;
278                  }
279                  if (res.rt & XDECR) {
280                          p0[0] = 1. - p0[0];
# Line 264 | Line 291 | main(int argc, char *argv[])
291                          fputs(err, stderr);
292                          fputc('\n', stderr);
293                          return(1);
294 +                } else {
295 +                        fputs(VIEWSTR, stdout);
296 +                        fprintview(&vw, stdout);
297 +                        fputc('\n', stdout);
298                  }
268                fputs(VIEWSTR, stdout);
269                fprintview(&vw, stdout);
270                fputc('\n', stdout);
299          }
300          if (gotdims)
301                  printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols);
# Line 292 | Line 320 | main(int argc, char *argv[])
320                  asiz = sizeof(float);
321          } else if (!strcmp(fmt, "double")) {
322                  asiz = sizeof(double);
323 +        } else if (!strcmp(fmt, "32-bit_encoded_normal")) {
324 +                asiz = 4;
325 +                ncomp = 1;
326 +        } else if (!strcmp(fmt, "16-bit_encoded_depth")) {
327 +                asiz = 2;
328 +                ncomp = 1;
329          } else if (globmatch(PICFMT, fmt)) {
330                  asiz = -1;
331                  if (!ncomp) ncomp = 3;
332 +                else ncomp *= (ncomp == 3);
333          } else if (strcasecmp(fmt, "ascii")) {
334                  fputs(progname, stderr);
335                  fputs(": unsupported format - ", stderr);
# Line 310 | Line 345 | main(int argc, char *argv[])
345          if (!(asiz < 0 ? colr_copyf(fp) :
346                          !asiz ? ascii_copyf(fp) : binary_copyf(fp, asiz)))
347                  return(1);
348 +                                        /* need to consume the rest? */
349 +        if (fp == stdin && rmin+nrows < numscans(&res) &&
350 +                        fseek(fp, 0L, SEEK_END) < 0)
351 +                while (getc(fp) != EOF)
352 +                        ;
353          return(0);
354   usage:
355          fputs("Usage: ", stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines