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; |
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)) { |
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; |
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; |
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; |
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) |
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[]) |
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); |
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); |
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))) { |
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) { |
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 |
< |
} else { |
291 |
< |
fputs(VIEWSTR, stdout); |
292 |
< |
fprintview(&vw, stdout); |
293 |
< |
fputc('\n', stdout); |
294 |
< |
} |
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? */ |
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); |
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); |