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 |
> |
buf = (char *)malloc(ncols*elsiz); |
102 |
> |
if (!buf) |
103 |
> |
goto memerr; |
104 |
> |
for (y = nrows; y-- > 0; ) { |
105 |
> |
if (getbinary(buf, elsiz, ncols, fp) != ncols) |
106 |
> |
goto readerr; |
107 |
> |
if (putbinary(buf, elsiz, ncols, stdout) != ncols) |
108 |
> |
goto writerr; |
109 |
> |
if (y && fseek(fp, skip_len, SEEK_CUR) < 0) { |
110 |
> |
fputs(progname, stderr); |
111 |
> |
fputs(": unexpected seek error on input\n", stderr); |
112 |
> |
return(0); |
113 |
> |
} |
114 |
> |
} |
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) |
122 |
> |
goto memerr; |
123 |
|
/* skip rows as requested */ |
124 |
< |
if (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0) { |
124 |
> |
if (skip_len > skip_thresh || |
125 |
> |
(rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0)) |
126 |
|
for (y = 0; y < rmin; y++) |
127 |
|
if (getbinary(buf, elsiz, width, fp) != width) |
128 |
|
goto readerr; |
109 |
– |
} |
129 |
|
for (y = 0; y < nrows; y++) { /* copy portion */ |
130 |
|
if (getbinary(buf, elsiz, width, fp) != width) |
131 |
|
goto readerr; |
143 |
|
fputs(progname, stderr); |
144 |
|
fputs(": error reading binary data\n", stderr); |
145 |
|
return(0); |
146 |
+ |
memerr: |
147 |
+ |
fputs(progname, stderr); |
148 |
+ |
fputs(": out of memory!\n", stderr); |
149 |
+ |
return(0); |
150 |
|
} |
151 |
|
|
152 |
|
/* Read (and copy) specified number of white-space-separated words */ |
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) { |
305 |
< |
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); |
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); |
329 |
|
asiz = sizeof(float); |
330 |
|
} else if (!strcmp(fmt, "double")) { |
331 |
|
asiz = sizeof(double); |
332 |
+ |
} else if (!strcmp(fmt, "32-bit_encoded_normal")) { |
333 |
+ |
asiz = 4; |
334 |
+ |
ncomp = 1; |
335 |
+ |
} else if (!strcmp(fmt, "16-bit_encoded_depth")) { |
336 |
+ |
asiz = 2; |
337 |
+ |
ncomp = 1; |
338 |
|
} else if (globmatch(PICFMT, fmt)) { |
339 |
|
asiz = -1; |
340 |
|
if (!ncomp) ncomp = 3; |
341 |
+ |
else ncomp *= (ncomp == 3); |
342 |
|
} else if (strcasecmp(fmt, "ascii")) { |
343 |
|
fputs(progname, stderr); |
344 |
|
fputs(": unsupported format - ", stderr); |
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); |