1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rcrop.c,v 1.11 2022/03/16 16:48:39 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* rcrop.c - crop a Radiance picture or matrix data |
6 |
*/ |
7 |
|
8 |
#include <ctype.h> |
9 |
#include "rtio.h" |
10 |
#include "platform.h" |
11 |
#include "color.h" |
12 |
#include "fvect.h" |
13 |
#include "view.h" |
14 |
|
15 |
char *progname; /* global argv[0] */ |
16 |
|
17 |
VIEW vw = STDVIEW; |
18 |
int gotvw = 0; |
19 |
char fmt[MAXFMTLEN] = "ascii"; /* assumed when unspecified */ |
20 |
int ncomp = 0; |
21 |
RESOLU res; |
22 |
int rmin, cmin, nrows, ncols; |
23 |
|
24 |
/* Process header line, copying to stdout when appropriate */ |
25 |
static int |
26 |
headline(char *s, void *p) |
27 |
{ |
28 |
if (formatval(fmt, s)) |
29 |
return(0); |
30 |
if (!strncmp(s, "NCOMP=", 6)) { |
31 |
ncomp = atoi(s+6); |
32 |
return(-(ncomp <= 0)); |
33 |
} |
34 |
if (!strncmp(s, "NROWS=", 6)) { |
35 |
res.rt = PIXSTANDARD; |
36 |
res.yr = atoi(s+6); |
37 |
return(-(res.yr <= 0)); |
38 |
} |
39 |
if (!strncmp(s, "NCOLS=", 6)) { |
40 |
res.rt = PIXSTANDARD; |
41 |
res.xr = atoi(s+6); |
42 |
return(-(res.xr <= 0)); |
43 |
} |
44 |
if (isview(s)) { |
45 |
gotvw += sscanview(&vw, s); |
46 |
return(0); |
47 |
} |
48 |
fputs(s, stdout); /* copy other header info. */ |
49 |
return(0); |
50 |
} |
51 |
|
52 |
/* Copy routine for COLR data */ |
53 |
static int |
54 |
colr_copyf(FILE *fp) |
55 |
{ |
56 |
const int width = scanlen(&res); |
57 |
COLR *scan = (COLR *)malloc(sizeof(COLR)*width); |
58 |
int y; |
59 |
|
60 |
if (!scan) { |
61 |
fputs(progname, stderr); |
62 |
fputs(": out of memory!\n", stderr); |
63 |
return(0); |
64 |
} |
65 |
for (y = 0; y < rmin; y++) /* initial skip */ |
66 |
if (freadcolrs(scan, width, fp) < 0) |
67 |
goto readerr; |
68 |
/* scanlines to copy */ |
69 |
for (y = 0; y < nrows; y++) { |
70 |
if (freadcolrs(scan, width, fp) < 0) |
71 |
goto readerr; |
72 |
if (fwritecolrs(scan+cmin, ncols, stdout) < 0) |
73 |
goto writerr; |
74 |
} |
75 |
free(scan); |
76 |
if (fflush(stdout) == 0) |
77 |
return(1); |
78 |
writerr: |
79 |
fputs(progname, stderr); |
80 |
fputs(": error writing picture\n", stderr); |
81 |
return(0); |
82 |
readerr: |
83 |
fputs(progname, stderr); |
84 |
fputs(": error reading picture\n", stderr); |
85 |
return(0); |
86 |
} |
87 |
|
88 |
/* Copy routine for binary data (asize = sizeof(type)) */ |
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 |
const long skip_len = (width-ncols)*elsiz; |
96 |
char *buf; |
97 |
int y; |
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 (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; |
129 |
for (y = 0; y < nrows; y++) { /* copy portion */ |
130 |
if (getbinary(buf, elsiz, width, fp) != width) |
131 |
goto readerr; |
132 |
if (putbinary(buf+cmin*elsiz, elsiz, ncols, stdout) != ncols) |
133 |
goto writerr; |
134 |
} |
135 |
free(buf); /* we're done */ |
136 |
if (fflush(stdout) == 0) |
137 |
return(1); |
138 |
writerr: |
139 |
fputs(progname, stderr); |
140 |
fputs(": error writing binary data\n", stderr); |
141 |
return(0); |
142 |
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 */ |
153 |
static int |
154 |
readwords(FILE *finp, int nwords, FILE *fout) |
155 |
{ |
156 |
while (nwords-- > 0) { |
157 |
int c; |
158 |
do { |
159 |
c = getc(finp); |
160 |
} while (isspace(c)); |
161 |
if (c == EOF) |
162 |
return(-1); |
163 |
if (fout && fputc(' ', fout) == EOF) |
164 |
return(-1); |
165 |
do { |
166 |
if (fout) |
167 |
putc(c, fout); |
168 |
} while ((c = getc(finp)) != EOF && !isspace(c)); |
169 |
} |
170 |
return(0); |
171 |
} |
172 |
|
173 |
/* Copy routine for ascii data */ |
174 |
static int |
175 |
ascii_copyf(FILE *fp) |
176 |
{ |
177 |
const int width = scanlen(&res); |
178 |
int x, y; |
179 |
|
180 |
SET_FILE_TEXT(fp); /* started as binary */ |
181 |
SET_FILE_TEXT(stdout); |
182 |
/* skip rows as requested */ |
183 |
if (readwords(fp, rmin*width*ncomp, NULL) < 0) |
184 |
goto io_err; |
185 |
for (y = 0; y < nrows; y++) { /* copy part */ |
186 |
if (readwords(fp, cmin*ncomp, NULL) < 0) |
187 |
goto io_err; |
188 |
if (readwords(fp, ncols*ncomp, stdout) < 0) |
189 |
goto io_err; |
190 |
fputc('\n', stdout); /* newline per row */ |
191 |
if (readwords(fp, (width-ncols-cmin)*ncomp, NULL) < 0) |
192 |
goto io_err; |
193 |
} |
194 |
if (fflush(stdout) == 0) |
195 |
return(1); |
196 |
io_err: |
197 |
fputs(progname, stderr); |
198 |
fputs(": error copying ascii data\n", stderr); |
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[]) |
244 |
{ |
245 |
FILE *fp = stdin; |
246 |
int asiz = 0; |
247 |
int gotdims; |
248 |
|
249 |
progname = argv[0]; |
250 |
/* get input and output */ |
251 |
if ((argc < 5) | (argc > 7)) |
252 |
goto usage; |
253 |
if (!isint(argv[1]) | !isint(argv[2]) | |
254 |
!isint(argv[3]) | !isint(argv[4])) |
255 |
goto usage; |
256 |
rmin = atoi(argv[1]); |
257 |
cmin = atoi(argv[2]); |
258 |
nrows = atoi(argv[3]); |
259 |
ncols = atoi(argv[4]); |
260 |
if ((rmin < 0) | (cmin < 0) | (nrows < 0) | (ncols < 0)) |
261 |
goto usage; |
262 |
if (argc <= 5) |
263 |
SET_FILE_BINARY(fp); |
264 |
else if (!(fp = fopen(argv[5], "rb"))) { |
265 |
fputs(argv[5], stderr); |
266 |
fputs(": cannot open for reading\n", stderr); |
267 |
return(1); |
268 |
} |
269 |
if (argc <= 6) |
270 |
SET_FILE_BINARY(stdout); |
271 |
else if (!freopen(argv[6], "wb", stdout)) { |
272 |
fputs(argv[6], stderr); |
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); |
283 |
fputs(": bad input header\n", stderr); |
284 |
return(1); |
285 |
} |
286 |
gotdims = (res.rt == PIXSTANDARD) & (res.xr > 0) & (res.yr > 0); |
287 |
if (!gotdims && !fgetsresolu(&res, fp)) { |
288 |
fputs(progname, stderr); |
289 |
fputs(": missing input dimensions\n", stderr); |
290 |
return(1); |
291 |
} |
292 |
if (!nrows) |
293 |
nrows = numscans(&res) - rmin; |
294 |
if (!ncols) |
295 |
ncols = scanlen(&res) - cmin; |
296 |
if ((nrows <= 0) | (ncols <= 0) | |
297 |
(rmin+nrows > numscans(&res)) | |
298 |
(cmin+ncols > scanlen(&res))) { |
299 |
fputs(progname, stderr); |
300 |
fputs(": illegal crop\n", stderr); |
301 |
return(1); |
302 |
} |
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) /* dimensions + format */ |
310 |
printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols); |
311 |
if (ncomp) |
312 |
printf("NCOMP=%d\n", ncomp); |
313 |
fputformat(fmt, stdout); /* will align bytes if it can */ |
314 |
fputc('\n', stdout); /* end of new header */ |
315 |
if (!gotdims) { /* add resolution string? */ |
316 |
RESOLU newres; |
317 |
if (res.rt & YMAJOR) { |
318 |
newres.xr = ncols; |
319 |
newres.yr = nrows; |
320 |
} else { |
321 |
newres.xr = nrows; |
322 |
newres.yr = ncols; |
323 |
} |
324 |
newres.rt = res.rt; |
325 |
fputsresolu(&newres, stdout); |
326 |
} |
327 |
/* call appropriate processor */ |
328 |
if (!strcmp(fmt, "float")) { |
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); |
345 |
fputs(fmt, stderr); |
346 |
fputc('\n', stderr); |
347 |
return(1); |
348 |
} |
349 |
if (ncomp <= 0) { |
350 |
fputs(progname, stderr); |
351 |
fputs(": illegal number of components\n", stderr); |
352 |
return(1); |
353 |
} |
354 |
if (!(asiz < 0 ? colr_copyf(fp) : |
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); |
365 |
fputs(progname, stderr); |
366 |
fputs(" row0 col0 nrows ncols [input [output]]\n", stderr); |
367 |
return(1); |
368 |
} |