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; |
112 |
|
return(0); |
113 |
|
} |
114 |
|
} |
115 |
< |
free(buf); /* success! */ |
116 |
< |
return(1); |
115 |
> |
free(buf); |
116 |
> |
if (fflush(stdout) == EOF) |
117 |
> |
goto writerr; |
118 |
> |
return(1); /* success! */ |
119 |
|
} /* else need to read it all... */ |
120 |
|
buf = (char *)malloc(width*elsiz); |
121 |
|
if (!buf) |
199 |
|
return(0); |
200 |
|
} |
201 |
|
|
202 |
+ |
/* Adjust (crop) our view */ |
203 |
+ |
static int |
204 |
+ |
adjust_view(void) |
205 |
+ |
{ |
206 |
+ |
double p0[2], p1[2]; |
207 |
+ |
const char *err; |
208 |
+ |
|
209 |
+ |
if (res.rt & YMAJOR) { |
210 |
+ |
p0[0] = cmin/(double)res.xr; |
211 |
+ |
p0[1] = rmin/(double)res.yr; |
212 |
+ |
p1[0] = (cmin+ncols)/(double)res.xr; |
213 |
+ |
p1[1] = (rmin+nrows)/(double)res.yr; |
214 |
+ |
} else { |
215 |
+ |
p0[0] = rmin/(double)res.xr; |
216 |
+ |
p0[1] = cmin/(double)res.yr; |
217 |
+ |
p1[0] = (rmin+nrows)/(double)res.xr; |
218 |
+ |
p1[1] = (cmin+ncols)/(double)res.yr; |
219 |
+ |
} |
220 |
+ |
if (res.rt & XDECR) { |
221 |
+ |
p0[0] = 1. - p0[0]; |
222 |
+ |
p1[0] = 1. - p1[0]; |
223 |
+ |
} |
224 |
+ |
if (res.rt & YDECR) { |
225 |
+ |
p0[1] = 1. - p0[1]; |
226 |
+ |
p1[1] = 1. - p1[1]; |
227 |
+ |
} |
228 |
+ |
err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]); |
229 |
+ |
|
230 |
+ |
if (!err) |
231 |
+ |
return(1); /* success! */ |
232 |
+ |
|
233 |
+ |
fputs(progname, stderr); |
234 |
+ |
fputs(": view error - ", stderr); |
235 |
+ |
fputs(err, stderr); |
236 |
+ |
fputc('\n', stderr); |
237 |
+ |
return(0); /* something went wrong */ |
238 |
+ |
} |
239 |
+ |
|
240 |
+ |
|
241 |
|
/* Main routine -- load header and call processor */ |
242 |
|
int |
243 |
|
main(int argc, char *argv[]) |
273 |
|
fputs(": cannot open for writing\n", stderr); |
274 |
|
return(1); |
275 |
|
} |
276 |
+ |
#ifdef getc_unlocked /* avoid stupid semaphores */ |
277 |
+ |
flockfile(fp); |
278 |
+ |
flockfile(stdout); |
279 |
+ |
#endif |
280 |
|
/* process information header */ |
281 |
|
if (getheader(fp, headline, NULL) < 0) { |
282 |
|
fputs(progname, stderr); |
300 |
|
fputs(": illegal crop\n", stderr); |
301 |
|
return(1); |
302 |
|
} |
303 |
< |
printargs(argc, argv, stdout); |
304 |
< |
if (gotvw) { /* adjust view? */ |
305 |
< |
double p0[2], p1[2]; |
306 |
< |
const char *err; |
307 |
< |
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 |
< |
} |
303 |
> |
printargs(5, argv, stdout); /* add to header */ |
304 |
> |
if (gotvw && adjust_view()) { |
305 |
> |
fputs(VIEWSTR, stdout); /* write adjusted view */ |
306 |
> |
fprintview(&vw, stdout); |
307 |
> |
fputc('\n', stdout); |
308 |
|
} |
309 |
< |
if (gotdims) |
309 |
> |
if (gotdims) /* dimensions + format */ |
310 |
|
printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols); |
311 |
|
if (ncomp) |
312 |
|
printf("NCOMP=%d\n", ncomp); |
352 |
|
return(1); |
353 |
|
} |
354 |
|
if (!(asiz < 0 ? colr_copyf(fp) : |
355 |
< |
!asiz ? ascii_copyf(fp) : binary_copyf(fp, asiz))) |
355 |
> |
asiz ? binary_copyf(fp, asiz) : ascii_copyf(fp))) |
356 |
|
return(1); |
357 |
+ |
/* need to consume the rest? */ |
358 |
+ |
if (fp == stdin && rmin+nrows < numscans(&res) && |
359 |
+ |
fseek(fp, 0L, SEEK_END) < 0) |
360 |
+ |
while (getc(fp) != EOF) |
361 |
+ |
; |
362 |
|
return(0); |
363 |
|
usage: |
364 |
|
fputs("Usage: ", stderr); |