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.17 by greg, Wed Jun 5 17:30:56 2024 UTC

# Line 12 | Line 12 | static const char RCSid[] = "$Id$";
12   #include "fvect.h"
13   #include "view.h"
14  
15 #define MAXWORD         64      /* maximum word (number) length */
16
15   char    *progname;              /* global argv[0] */
16  
17   VIEW    vw = STDVIEW;
18   int     gotvw = 0;
19 < char    fmt[MAXFMTLEN] = "Unknown";
19 > char    fmt[MAXFMTLEN] = "ascii";       /* assumed when unspecified */
20   int     ncomp = 0;
21   RESOLU  res;
22   int     rmin, cmin, nrows, ncols;
# Line 29 | Line 27 | headline(char *s, void *p)
27   {
28          if (formatval(fmt, s))
29                  return(0);
30 <        if (!strncmp(s, "NCOMP=", 6)) {
31 <                ncomp = atoi(s+6);
30 >        if (isncomp(s)) {
31 >                ncomp = ncompval(s);
32                  return(-(ncomp <= 0));
33          }
34          if (!strncmp(s, "NROWS=", 6)) {
# Line 79 | Line 77 | colr_copyf(FILE *fp)
77                  return(1);
78   writerr:
79          fputs(progname, stderr);
80 <        fputs(": error writing scanline\n", stderr);
80 >        fputs(": error writing picture\n", stderr);
81          return(0);
82   readerr:
83          fputs(progname, stderr);
84 <        fputs(": error reading scanline\n", stderr);
84 >        fputs(": error reading picture\n", stderr);
85          return(0);
86   }
87  
# Line 91 | Line 89 | readerr:
89   static int
90   binary_copyf(FILE *fp, int asize)
91   {
92 +        const int       skip_thresh = 8192;
93          const size_t    elsiz = asize*ncomp;
94          const int       width = scanlen(&res);
95 <        char            *buf = (char *)malloc(elsiz*width);
95 >        const long      skip_len = (width-ncols)*elsiz;
96 >        char            *buf;
97          int             y;
98 <
99 <        if (!buf) {
100 <                fputs(progname, stderr);
101 <                fputs(": out of memory!\n", stderr);
102 <                return(0);
103 <        }
98 >                                        /* check if fseek() useful */
99 >        if (skip_len > skip_thresh &&
100 >                        fseek(fp, ((long)rmin*width + cmin)*elsiz, SEEK_CUR) == 0) {
101 >                int     fd;
102 >                off_t   curpos;
103 >                buf = (char *)malloc(ncols*elsiz);
104 >                if (!buf)
105 >                        goto memerr;
106 > #ifdef NON_POSIX
107 >                for (y = nrows; y-- > 0; ) {
108 >                        if (getbinary(buf, elsiz, ncols, fp) != ncols)
109 >                                goto readerr;
110 >                        if (putbinary(buf, elsiz, ncols, stdout) != ncols)
111 >                                goto writerr;
112 >                        if (y && fseek(fp, skip_len, SEEK_CUR) < 0) {
113 >                                fputs(progname, stderr);
114 >                                fputs(": unexpected seek error on input\n", stderr);
115 >                                return(0);
116 >                        }
117 >                }
118 > #else
119 >                fd = fileno(fp);
120 >                curpos = ftello(fp);
121 >                for (y = nrows; y-- > 0; curpos += width*elsiz) {
122 >                        if (pread(fd, buf, ncols*elsiz,
123 >                                                curpos) != ncols*elsiz)
124 >                                goto readerr;
125 >                        if (putbinary(buf, elsiz, ncols, stdout) != ncols)
126 >                                goto writerr;
127 >                }
128 > #endif
129 >                free(buf);
130 >                if (fflush(stdout) == EOF)
131 >                        goto writerr;
132 >                return(1);              /* success! */
133 >        }                               /* else need to read it all... */
134 >        buf = (char *)malloc(width*elsiz);
135 >        if (!buf)
136 >                goto memerr;
137                                          /* skip rows as requested */
138 <        if (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0) {
138 >        if (skip_len > skip_thresh ||
139 >                        (rmin && fseek(fp, (long)rmin*width*elsiz, SEEK_CUR) < 0))
140                  for (y = 0; y < rmin; y++)
141                          if (getbinary(buf, elsiz, width, fp) != width)
142                                  goto readerr;
109        }
143          for (y = 0; y < nrows; y++) {   /* copy portion */
144                  if (getbinary(buf, elsiz, width, fp) != width)
145                          goto readerr;
# Line 124 | Line 157 | readerr:
157          fputs(progname, stderr);
158          fputs(": error reading binary data\n", stderr);
159          return(0);
160 + memerr:
161 +        fputs(progname, stderr);
162 +        fputs(": out of memory!\n", stderr);
163 +        return(0);
164   }
165  
166   /* Read (and copy) specified number of white-space-separated words */
167   static int
168 < readwords(FILE *finp, int nwords, FILE *fout)
168 > readwords(FILE *finp, long nwords, FILE *fout)
169   {
170          while (nwords-- > 0) {
171                  int     c;
# Line 157 | Line 194 | ascii_copyf(FILE *fp)
194          SET_FILE_TEXT(fp);              /* started as binary */
195          SET_FILE_TEXT(stdout);
196                                          /* skip rows as requested */
197 <        if (readwords(fp, rmin*width*ncomp, NULL) < 0)
197 >        if (readwords(fp, (long)rmin*width*ncomp, NULL) < 0)
198                  goto io_err;
199          for (y = 0; y < nrows; y++) {   /* copy part */
200                  if (readwords(fp, cmin*ncomp, NULL) < 0)
# Line 176 | Line 213 | io_err:
213          return(0);
214   }
215  
216 + /* Adjust (crop) our view */
217 + static int
218 + adjust_view(void)
219 + {
220 +        double          p0[2], p1[2];
221 +        const char      *err;
222 +
223 +        if (res.rt & YMAJOR) {
224 +                p0[0] = cmin/(double)res.xr;
225 +                p0[1] = rmin/(double)res.yr;
226 +                p1[0] = (cmin+ncols)/(double)res.xr;
227 +                p1[1] = (rmin+nrows)/(double)res.yr;
228 +        } else {
229 +                p0[0] = rmin/(double)res.xr;
230 +                p0[1] = cmin/(double)res.yr;
231 +                p1[0] = (rmin+nrows)/(double)res.xr;
232 +                p1[1] = (cmin+ncols)/(double)res.yr;
233 +        }
234 +        if (res.rt & XDECR) {
235 +                p0[0] = 1. - p0[0];
236 +                p1[0] = 1. - p1[0];
237 +        }
238 +        if (res.rt & YDECR) {
239 +                p0[1] = 1. - p0[1];
240 +                p1[1] = 1. - p1[1];
241 +        }
242 +        err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]);
243 +
244 +        if (!err)
245 +                return(1);      /* success! */
246 +
247 +        fputs(progname, stderr);
248 +        fputs(": view error - ", stderr);
249 +        fputs(err, stderr);
250 +        fputc('\n', stderr);
251 +        return(0);              /* something went wrong */
252 + }
253 +
254 +
255   /* Main routine -- load header and call processor */
256   int
257   main(int argc, char *argv[])
# Line 195 | Line 271 | main(int argc, char *argv[])
271          cmin = atoi(argv[2]);
272          nrows = atoi(argv[3]);
273          ncols = atoi(argv[4]);
274 <        if ((rmin < 0) | (cmin < 0) | (nrows < 0) | (ncols < 0))
274 >        if ((rmin < 0) | (cmin < 0))
275                  goto usage;
276          if (argc <= 5)
277                  SET_FILE_BINARY(fp);
# Line 211 | Line 287 | main(int argc, char *argv[])
287                  fputs(": cannot open for writing\n", stderr);
288                  return(1);
289          }
290 + #ifdef getc_unlocked            /* avoid stupid semaphores */
291 +        flockfile(fp);
292 +        flockfile(stdout);
293 + #endif
294                                  /* process information header */
295          if (getheader(fp, headline, NULL) < 0) {
296                  fputs(progname, stderr);
# Line 223 | Line 303 | main(int argc, char *argv[])
303                  fputs(": missing input dimensions\n", stderr);
304                  return(1);
305          }
306 <        if (!nrows)
307 <                nrows = numscans(&res) - rmin;
308 <        if (!ncols)
309 <                ncols = scanlen(&res) - cmin;
306 >        if (nrows <= 0 )
307 >                nrows += numscans(&res) - rmin;
308 >        if (ncols <= 0)
309 >                ncols += scanlen(&res) - cmin;
310          if ((nrows <= 0) | (ncols <= 0) |
311                          (rmin+nrows > numscans(&res)) |
312                          (cmin+ncols > scanlen(&res))) {
# Line 234 | Line 314 | main(int argc, char *argv[])
314                  fputs(": illegal crop\n", stderr);
315                  return(1);
316          }
317 <        printargs(argc, argv, stdout);
318 <        if (gotvw) {
319 <                double          p0[2], p1[2];
240 <                const char      *err;
241 <                if (res.rt & YMAJOR) {
242 <                        p0[0] = cmin/(double)res.xr;
243 <                        p0[1] = rmin/(double)res.yr;
244 <                        p1[0] = (cmin+ncols)/(double)res.xr;
245 <                        p1[1] = (rmin+nrows)/(double)res.yr;
246 <                } else {
247 <                        p0[1] = cmin/(double)res.xr;
248 <                        p0[0] = rmin/(double)res.yr;
249 <                        p1[1] = (cmin+ncols)/(double)res.xr;
250 <                        p1[0] = (rmin+nrows)/(double)res.yr;
251 <                }
252 <                if (res.rt & XDECR) {
253 <                        p0[0] = 1. - p0[0];
254 <                        p1[0] = 1. - p1[0];
255 <                }
256 <                if (res.rt & YDECR) {
257 <                        p0[1] = 1. - p0[1];
258 <                        p1[1] = 1. - p1[1];
259 <                }
260 <                err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]);
261 <                if (err) {
262 <                        fputs(progname, stderr);
263 <                        fputs(": view error - ", stderr);
264 <                        fputs(err, stderr);
265 <                        fputc('\n', stderr);
266 <                        return(1);
267 <                }
268 <                fputs(VIEWSTR, stdout);
317 >        printargs(5, argv, stdout);     /* add to header */
318 >        if (gotvw && adjust_view()) {
319 >                fputs(VIEWSTR, stdout); /* write adjusted view */
320                  fprintview(&vw, stdout);
321                  fputc('\n', stdout);
322          }
323 <        if (gotdims)
323 >        if (gotdims)                    /* dimensions + format */
324                  printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols);
325          if (ncomp)
326 <                printf("NCOMP=%d\n", ncomp);
326 >                fputncomp(ncomp, stdout);
327          fputformat(fmt, stdout);        /* will align bytes if it can */
328          fputc('\n', stdout);            /* end of new header */
329          if (!gotdims) {                 /* add resolution string? */
# Line 292 | Line 343 | main(int argc, char *argv[])
343                  asiz = sizeof(float);
344          } else if (!strcmp(fmt, "double")) {
345                  asiz = sizeof(double);
346 +        } else if (!strcmp(fmt, "32-bit_encoded_normal")) {
347 +                asiz = 4;
348 +                ncomp = 1;
349 +        } else if (!strcmp(fmt, "16-bit_encoded_depth")) {
350 +                asiz = 2;
351 +                ncomp = 1;
352          } else if (globmatch(PICFMT, fmt)) {
353                  asiz = -1;
354                  if (!ncomp) ncomp = 3;
355 +                else ncomp *= (ncomp == 3);
356 +        } else if (!strcmp(fmt, SPECFMT)) {
357 +                asiz = ncomp+1;
358 +                ncomp = 1;              /* XXX assumes uncompressed */
359          } else if (strcasecmp(fmt, "ascii")) {
360                  fputs(progname, stderr);
361                  fputs(": unsupported format - ", stderr);
# Line 308 | Line 369 | main(int argc, char *argv[])
369                  return(1);
370          }
371          if (!(asiz < 0 ? colr_copyf(fp) :
372 <                        !asiz ? ascii_copyf(fp) : binary_copyf(fp, asiz)))
372 >                        asiz ? binary_copyf(fp, asiz) : ascii_copyf(fp)))
373                  return(1);
374 +                                        /* need to consume the rest? */
375 +        if (fp == stdin && rmin+nrows < numscans(&res) &&
376 +                        fseek(fp, 0L, SEEK_END) < 0)
377 +                while (getc(fp) != EOF)
378 +                        ;
379          return(0);
380   usage:
381          fputs("Usage: ", stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines