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; |
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 |
|
|
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 |
> |
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; |
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 |
> |
#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) |
134 |
> |
goto memerr; |
135 |
|
/* skip rows as requested */ |
136 |
< |
if (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0) { |
136 |
> |
if (skip_len > skip_thresh || |
137 |
> |
(rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0)) |
138 |
|
for (y = 0; y < rmin; y++) |
139 |
|
if (getbinary(buf, elsiz, width, fp) != width) |
140 |
|
goto readerr; |
109 |
– |
} |
141 |
|
for (y = 0; y < nrows; y++) { /* copy portion */ |
142 |
|
if (getbinary(buf, elsiz, width, fp) != width) |
143 |
|
goto readerr; |
155 |
|
fputs(progname, stderr); |
156 |
|
fputs(": error reading binary data\n", stderr); |
157 |
|
return(0); |
158 |
+ |
memerr: |
159 |
+ |
fputs(progname, stderr); |
160 |
+ |
fputs(": out of memory!\n", stderr); |
161 |
+ |
return(0); |
162 |
|
} |
163 |
|
|
164 |
|
/* Read (and copy) specified number of white-space-separated words */ |
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[]) |
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); |
312 |
|
fputs(": illegal crop\n", stderr); |
313 |
|
return(1); |
314 |
|
} |
315 |
< |
printargs(argc, argv, stdout); |
316 |
< |
if (gotvw) { |
317 |
< |
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); |
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); |
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); |
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); |