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.11 by greg, Wed Mar 16 16:48:39 2022 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 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, (rmin*width + cmin)*elsiz, SEEK_CUR) == 0) {
101 >                buf = (char *)malloc(ncols*elsiz);
102 >                if (!buf)
103 >                        goto memerr;
104 >                for (y = nrows; y-- > 0; ) {
105 >                        if (getbinary(buf, elsiz, ncols, fp) != ncols)
106 >                                goto readerr;
107 >                        if (putbinary(buf, elsiz, ncols, stdout) != ncols)
108 >                                goto writerr;
109 >                        if (y && fseek(fp, skip_len, SEEK_CUR) < 0) {
110 >                                fputs(progname, stderr);
111 >                                fputs(": unexpected seek error on input\n", stderr);
112 >                                return(0);
113 >                        }
114 >                }
115 >                free(buf);              /* success! */
116 >                return(1);
117 >        }                               /* else need to read it all... */
118 >        buf = (char *)malloc(width*elsiz);
119 >        if (!buf)
120 >                goto memerr;
121                                          /* skip rows as requested */
122 <        if (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0) {
122 >        if (skip_len > skip_thresh ||
123 >                        (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0))
124                  for (y = 0; y < rmin; y++)
125                          if (getbinary(buf, elsiz, width, fp) != width)
126                                  goto readerr;
109        }
127          for (y = 0; y < nrows; y++) {   /* copy portion */
128                  if (getbinary(buf, elsiz, width, fp) != width)
129                          goto readerr;
# Line 124 | Line 141 | readerr:
141          fputs(progname, stderr);
142          fputs(": error reading binary data\n", stderr);
143          return(0);
144 + memerr:
145 +        fputs(progname, stderr);
146 +        fputs(": out of memory!\n", stderr);
147 +        return(0);
148   }
149  
150   /* Read (and copy) specified number of white-space-separated words */
# Line 176 | Line 197 | io_err:
197          return(0);
198   }
199  
200 + /* Adjust (crop) our view */
201 + static int
202 + adjust_view(void)
203 + {
204 +        double          p0[2], p1[2];
205 +        const char      *err;
206 +
207 +        if (res.rt & YMAJOR) {
208 +                p0[0] = cmin/(double)res.xr;
209 +                p0[1] = rmin/(double)res.yr;
210 +                p1[0] = (cmin+ncols)/(double)res.xr;
211 +                p1[1] = (rmin+nrows)/(double)res.yr;
212 +        } else {
213 +                p0[0] = rmin/(double)res.xr;
214 +                p0[1] = cmin/(double)res.yr;
215 +                p1[0] = (rmin+nrows)/(double)res.xr;
216 +                p1[1] = (cmin+ncols)/(double)res.yr;
217 +        }
218 +        if (res.rt & XDECR) {
219 +                p0[0] = 1. - p0[0];
220 +                p1[0] = 1. - p1[0];
221 +        }
222 +        if (res.rt & YDECR) {
223 +                p0[1] = 1. - p0[1];
224 +                p1[1] = 1. - p1[1];
225 +        }
226 +        err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]);
227 +
228 +        if (!err)
229 +                return(1);      /* success! */
230 +
231 +        fputs(progname, stderr);
232 +        fputs(": view error - ", stderr);
233 +        fputs(err, stderr);
234 +        fputc('\n', stderr);
235 +        return(0);              /* something went wrong */
236 + }
237 +
238 +
239   /* Main routine -- load header and call processor */
240   int
241   main(int argc, char *argv[])
# Line 211 | Line 271 | main(int argc, char *argv[])
271                  fputs(": cannot open for writing\n", stderr);
272                  return(1);
273          }
274 + #ifdef getc_unlocked            /* avoid stupid semaphores */
275 +        flockfile(fp);
276 +        flockfile(stdout);
277 + #endif
278                                  /* process information header */
279          if (getheader(fp, headline, NULL) < 0) {
280                  fputs(progname, stderr);
# Line 234 | Line 298 | main(int argc, char *argv[])
298                  fputs(": illegal crop\n", stderr);
299                  return(1);
300          }
301 <        printargs(argc, argv, stdout);
302 <        if (gotvw) {
303 <                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);
301 >        printargs(5, argv, stdout);     /* add to header */
302 >        if (gotvw && adjust_view()) {
303 >                fputs(VIEWSTR, stdout); /* write adjusted view */
304                  fprintview(&vw, stdout);
305                  fputc('\n', stdout);
306          }
307 <        if (gotdims)
307 >        if (gotdims)                    /* dimensions + format */
308                  printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols);
309          if (ncomp)
310                  printf("NCOMP=%d\n", ncomp);
# Line 292 | Line 327 | main(int argc, char *argv[])
327                  asiz = sizeof(float);
328          } else if (!strcmp(fmt, "double")) {
329                  asiz = sizeof(double);
330 +        } else if (!strcmp(fmt, "32-bit_encoded_normal")) {
331 +                asiz = 4;
332 +                ncomp = 1;
333 +        } else if (!strcmp(fmt, "16-bit_encoded_depth")) {
334 +                asiz = 2;
335 +                ncomp = 1;
336          } else if (globmatch(PICFMT, fmt)) {
337                  asiz = -1;
338                  if (!ncomp) ncomp = 3;
339 +                else ncomp *= (ncomp == 3);
340          } else if (strcasecmp(fmt, "ascii")) {
341                  fputs(progname, stderr);
342                  fputs(": unsupported format - ", stderr);
# Line 308 | Line 350 | main(int argc, char *argv[])
350                  return(1);
351          }
352          if (!(asiz < 0 ? colr_copyf(fp) :
353 <                        !asiz ? ascii_copyf(fp) : binary_copyf(fp, asiz)))
353 >                        asiz ? binary_copyf(fp, asiz) : ascii_copyf(fp)))
354                  return(1);
355 +                                        /* need to consume the rest? */
356 +        if (fp == stdin && rmin+nrows < numscans(&res) &&
357 +                        fseek(fp, 0L, SEEK_END) < 0)
358 +                while (getc(fp) != EOF)
359 +                        ;
360          return(0);
361   usage:
362          fputs("Usage: ", stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines