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; |
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 |
> |
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; |
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) |
134 |
|
goto memerr; |
135 |
|
/* skip rows as requested */ |
136 |
|
if (skip_len > skip_thresh || |
137 |
< |
(rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0)) |
137 |
> |
(rmin && fseek(fp, (long)rmin*width*elsiz, SEEK_CUR) < 0)) |
138 |
|
for (y = 0; y < rmin; y++) |
139 |
|
if (getbinary(buf, elsiz, width, fp) != width) |
140 |
|
goto readerr; |
163 |
|
|
164 |
|
/* Read (and copy) specified number of white-space-separated words */ |
165 |
|
static int |
166 |
< |
readwords(FILE *finp, int nwords, FILE *fout) |
166 |
> |
readwords(FILE *finp, long nwords, FILE *fout) |
167 |
|
{ |
168 |
|
while (nwords-- > 0) { |
169 |
|
int c; |
192 |
|
SET_FILE_TEXT(fp); /* started as binary */ |
193 |
|
SET_FILE_TEXT(stdout); |
194 |
|
/* skip rows as requested */ |
195 |
< |
if (readwords(fp, rmin*width*ncomp, NULL) < 0) |
195 |
> |
if (readwords(fp, (long)rmin*width*ncomp, NULL) < 0) |
196 |
|
goto io_err; |
197 |
|
for (y = 0; y < nrows; y++) { /* copy part */ |
198 |
|
if (readwords(fp, cmin*ncomp, NULL) < 0) |
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[]) |
269 |
|
cmin = atoi(argv[2]); |
270 |
|
nrows = atoi(argv[3]); |
271 |
|
ncols = atoi(argv[4]); |
272 |
< |
if ((rmin < 0) | (cmin < 0) | (nrows < 0) | (ncols < 0)) |
272 |
> |
if ((rmin < 0) | (cmin < 0)) |
273 |
|
goto usage; |
274 |
|
if (argc <= 5) |
275 |
|
SET_FILE_BINARY(fp); |
301 |
|
fputs(": missing input dimensions\n", stderr); |
302 |
|
return(1); |
303 |
|
} |
304 |
< |
if (!nrows) |
305 |
< |
nrows = numscans(&res) - rmin; |
306 |
< |
if (!ncols) |
307 |
< |
ncols = scanlen(&res) - cmin; |
304 |
> |
if (nrows <= 0 ) |
305 |
> |
nrows += numscans(&res) - rmin; |
306 |
> |
if (ncols <= 0) |
307 |
> |
ncols += scanlen(&res) - cmin; |
308 |
|
if ((nrows <= 0) | (ncols <= 0) | |
309 |
|
(rmin+nrows > numscans(&res)) | |
310 |
|
(cmin+ncols > scanlen(&res))) { |
312 |
|
fputs(": illegal crop\n", stderr); |
313 |
|
return(1); |
314 |
|
} |
315 |
< |
printargs(5, argv, stdout); |
316 |
< |
if (gotvw) { /* adjust view? */ |
317 |
< |
double p0[2], p1[2]; |
318 |
< |
const char *err; |
319 |
< |
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[0] = rmin/(double)res.xr; |
275 |
< |
p0[1] = cmin/(double)res.yr; |
276 |
< |
p1[0] = (rmin+nrows)/(double)res.xr; |
277 |
< |
p1[1] = (cmin+ncols)/(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 |
< |
} |
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); |
324 |
> |
fputncomp(ncomp, stdout); |
325 |
|
fputformat(fmt, stdout); /* will align bytes if it can */ |
326 |
|
fputc('\n', stdout); /* end of new header */ |
327 |
|
if (!gotdims) { /* add resolution string? */ |
351 |
|
asiz = -1; |
352 |
|
if (!ncomp) ncomp = 3; |
353 |
|
else ncomp *= (ncomp == 3); |
354 |
+ |
} else if (!strcmp(fmt, SPECFMT)) { |
355 |
+ |
asiz = ncomp+1; |
356 |
+ |
ncomp = 1; /* XXX assumes uncompressed */ |
357 |
|
} else if (strcasecmp(fmt, "ascii")) { |
358 |
|
fputs(progname, stderr); |
359 |
|
fputs(": unsupported format - ", stderr); |
367 |
|
return(1); |
368 |
|
} |
369 |
|
if (!(asiz < 0 ? colr_copyf(fp) : |
370 |
< |
!asiz ? ascii_copyf(fp) : binary_copyf(fp, asiz))) |
370 |
> |
asiz ? binary_copyf(fp, asiz) : ascii_copyf(fp))) |
371 |
|
return(1); |
372 |
|
/* need to consume the rest? */ |
373 |
|
if (fp == stdin && rmin+nrows < numscans(&res) && |