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.2 by greg, Tue Mar 15 01:40:17 2022 UTC vs.
Revision 1.13 by greg, Thu Apr 7 16:58:30 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 = 1024;
92 >        const int       skip_thresh = 8192;
93          const size_t    elsiz = asize*ncomp;
94          const int       width = scanlen(&res);
95          const long      skip_len = (width-ncols)*elsiz;
# Line 100 | Line 98 | binary_copyf(FILE *fp, int asize)
98                                          /* check if fseek() useful */
99          if (skip_len > skip_thresh &&
100                          fseek(fp, (rmin*width + cmin)*elsiz, SEEK_CUR) == 0) {
101 +                off_t   curpos;
102                  buf = (char *)malloc(ncols*elsiz);
103                  if (!buf)
104                          goto memerr;
105 + #ifdef NON_POSIX
106                  for (y = nrows; y-- > 0; ) {
107                          if (getbinary(buf, elsiz, ncols, fp) != ncols)
108                                  goto readerr;
# Line 114 | Line 114 | binary_copyf(FILE *fp, int asize)
114                                  return(0);
115                          }
116                  }
117 <                free(buf);              /* success! */
118 <                return(1);
117 > #else
118 >                curpos = ftello(fp);
119 >                for (y = nrows; y-- > 0; curpos += width*elsiz) {
120 >                        if (pread(fileno(fp), buf, ncols*elsiz,
121 >                                                curpos) != ncols*elsiz)
122 >                                goto readerr;
123 >                        if (putbinary(buf, elsiz, ncols, stdout) != ncols)
124 >                                goto writerr;
125 >                }
126 > #endif
127 >                free(buf);
128 >                if (fflush(stdout) == EOF)
129 >                        goto writerr;
130 >                return(1);              /* success! */
131          }                               /* else need to read it all... */
132          buf = (char *)malloc(width*elsiz);
133          if (!buf)
# Line 199 | Line 211 | io_err:
211          return(0);
212   }
213  
214 + /* Adjust (crop) our view */
215 + static int
216 + adjust_view(void)
217 + {
218 +        double          p0[2], p1[2];
219 +        const char      *err;
220 +
221 +        if (res.rt & YMAJOR) {
222 +                p0[0] = cmin/(double)res.xr;
223 +                p0[1] = rmin/(double)res.yr;
224 +                p1[0] = (cmin+ncols)/(double)res.xr;
225 +                p1[1] = (rmin+nrows)/(double)res.yr;
226 +        } else {
227 +                p0[0] = rmin/(double)res.xr;
228 +                p0[1] = cmin/(double)res.yr;
229 +                p1[0] = (rmin+nrows)/(double)res.xr;
230 +                p1[1] = (cmin+ncols)/(double)res.yr;
231 +        }
232 +        if (res.rt & XDECR) {
233 +                p0[0] = 1. - p0[0];
234 +                p1[0] = 1. - p1[0];
235 +        }
236 +        if (res.rt & YDECR) {
237 +                p0[1] = 1. - p0[1];
238 +                p1[1] = 1. - p1[1];
239 +        }
240 +        err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]);
241 +
242 +        if (!err)
243 +                return(1);      /* success! */
244 +
245 +        fputs(progname, stderr);
246 +        fputs(": view error - ", stderr);
247 +        fputs(err, stderr);
248 +        fputc('\n', stderr);
249 +        return(0);              /* something went wrong */
250 + }
251 +
252 +
253   /* Main routine -- load header and call processor */
254   int
255   main(int argc, char *argv[])
# Line 234 | Line 285 | main(int argc, char *argv[])
285                  fputs(": cannot open for writing\n", stderr);
286                  return(1);
287          }
288 + #ifdef getc_unlocked            /* avoid stupid semaphores */
289 +        flockfile(fp);
290 +        flockfile(stdout);
291 + #endif
292                                  /* process information header */
293          if (getheader(fp, headline, NULL) < 0) {
294                  fputs(progname, stderr);
# Line 257 | Line 312 | main(int argc, char *argv[])
312                  fputs(": illegal crop\n", stderr);
313                  return(1);
314          }
315 <        printargs(argc, argv, stdout);
316 <        if (gotvw) {
317 <                double          p0[2], p1[2];
263 <                const char      *err;
264 <                if (res.rt & YMAJOR) {
265 <                        p0[0] = cmin/(double)res.xr;
266 <                        p0[1] = rmin/(double)res.yr;
267 <                        p1[0] = (cmin+ncols)/(double)res.xr;
268 <                        p1[1] = (rmin+nrows)/(double)res.yr;
269 <                } else {
270 <                        p0[1] = cmin/(double)res.xr;
271 <                        p0[0] = rmin/(double)res.yr;
272 <                        p1[1] = (cmin+ncols)/(double)res.xr;
273 <                        p1[0] = (rmin+nrows)/(double)res.yr;
274 <                }
275 <                if (res.rt & XDECR) {
276 <                        p0[0] = 1. - p0[0];
277 <                        p1[0] = 1. - p1[0];
278 <                }
279 <                if (res.rt & YDECR) {
280 <                        p0[1] = 1. - p0[1];
281 <                        p1[1] = 1. - p1[1];
282 <                }
283 <                err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]);
284 <                if (err) {
285 <                        fputs(progname, stderr);
286 <                        fputs(": view error - ", stderr);
287 <                        fputs(err, stderr);
288 <                        fputc('\n', stderr);
289 <                        return(1);
290 <                }
291 <                fputs(VIEWSTR, stdout);
315 >        printargs(5, argv, stdout);     /* add to header */
316 >        if (gotvw && adjust_view()) {
317 >                fputs(VIEWSTR, stdout); /* write adjusted view */
318                  fprintview(&vw, stdout);
319                  fputc('\n', stdout);
320          }
321 <        if (gotdims)
321 >        if (gotdims)                    /* dimensions + format */
322                  printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols);
323          if (ncomp)
324                  printf("NCOMP=%d\n", ncomp);
# Line 315 | Line 341 | main(int argc, char *argv[])
341                  asiz = sizeof(float);
342          } else if (!strcmp(fmt, "double")) {
343                  asiz = sizeof(double);
344 +        } else if (!strcmp(fmt, "32-bit_encoded_normal")) {
345 +                asiz = 4;
346 +                ncomp = 1;
347 +        } else if (!strcmp(fmt, "16-bit_encoded_depth")) {
348 +                asiz = 2;
349 +                ncomp = 1;
350          } else if (globmatch(PICFMT, fmt)) {
351                  asiz = -1;
352                  if (!ncomp) ncomp = 3;
353 +                else ncomp *= (ncomp == 3);
354          } else if (strcasecmp(fmt, "ascii")) {
355                  fputs(progname, stderr);
356                  fputs(": unsupported format - ", stderr);
# Line 331 | Line 364 | main(int argc, char *argv[])
364                  return(1);
365          }
366          if (!(asiz < 0 ? colr_copyf(fp) :
367 <                        !asiz ? ascii_copyf(fp) : binary_copyf(fp, asiz)))
367 >                        asiz ? binary_copyf(fp, asiz) : ascii_copyf(fp)))
368                  return(1);
369 +                                        /* need to consume the rest? */
370 +        if (fp == stdin && rmin+nrows < numscans(&res) &&
371 +                        fseek(fp, 0L, SEEK_END) < 0)
372 +                while (getc(fp) != EOF)
373 +                        ;
374          return(0);
375   usage:
376          fputs("Usage: ", stderr);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines