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.8 by greg, Tue Mar 15 20:54:49 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;
# 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 99 | Line 97 | binary_copyf(FILE *fp, int asize)
97          int             y;
98                                          /* check if fseek() useful */
99          if (skip_len > skip_thresh &&
100 <                        fseek(fp, (rmin*width + cmin)*elsiz, SEEK_CUR) == 0) {
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;
# Line 114 | Line 115 | binary_copyf(FILE *fp, int asize)
115                                  return(0);
116                          }
117                  }
118 <                free(buf);              /* success! */
119 <                return(1);
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 (skip_len > skip_thresh ||
139 <                        (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0))
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;
# Line 151 | Line 165 | memerr:
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 180 | 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 199 | 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 218 | 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 250 | 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 261 | 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) {            /* adjust view? */
319 <                double          p0[2], p1[2];
320 <                const char      *err;
321 <                if (res.rt & YMAJOR) {
269 <                        p0[0] = cmin/(double)res.xr;
270 <                        p0[1] = rmin/(double)res.yr;
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;
278 <                }
279 <                if (res.rt & XDECR) {
280 <                        p0[0] = 1. - p0[0];
281 <                        p1[0] = 1. - p1[0];
282 <                }
283 <                if (res.rt & YDECR) {
284 <                        p0[1] = 1. - p0[1];
285 <                        p1[1] = 1. - p1[1];
286 <                }
287 <                err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]);
288 <                if (err) {
289 <                        fputs(progname, stderr);
290 <                        fputs(": view error - ", stderr);
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 <                }
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 330 | Line 353 | main(int argc, char *argv[])
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 343 | 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) &&

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines