ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcrop.c
Revision: 1.11
Committed: Wed Mar 16 16:48:39 2022 UTC (2 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +10 -11 lines
Log Message:
refactor(rcrop): Other minor code clean-ups

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcrop.c,v 1.10 2022/03/16 15:50:24 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); /* success! */
116 return(1);
117 } /* else need to read it all... */
118 buf = (char *)malloc(width*elsiz);
119 if (!buf)
120 goto memerr;
121 /* skip rows as requested */
122 if (skip_len > skip_thresh ||
123 (rmin && fseek(fp, rmin*width*elsiz, SEEK_CUR) < 0))
124 for (y = 0; y < rmin; y++)
125 if (getbinary(buf, elsiz, width, fp) != width)
126 goto readerr;
127 for (y = 0; y < nrows; y++) { /* copy portion */
128 if (getbinary(buf, elsiz, width, fp) != width)
129 goto readerr;
130 if (putbinary(buf+cmin*elsiz, elsiz, ncols, stdout) != ncols)
131 goto writerr;
132 }
133 free(buf); /* we're done */
134 if (fflush(stdout) == 0)
135 return(1);
136 writerr:
137 fputs(progname, stderr);
138 fputs(": error writing binary data\n", stderr);
139 return(0);
140 readerr:
141 fputs(progname, stderr);
142 fputs(": error reading binary data\n", stderr);
143 return(0);
144 memerr:
145 fputs(progname, stderr);
146 fputs(": out of memory!\n", stderr);
147 return(0);
148 }
149
150 /* Read (and copy) specified number of white-space-separated words */
151 static int
152 readwords(FILE *finp, int nwords, FILE *fout)
153 {
154 while (nwords-- > 0) {
155 int c;
156 do {
157 c = getc(finp);
158 } while (isspace(c));
159 if (c == EOF)
160 return(-1);
161 if (fout && fputc(' ', fout) == EOF)
162 return(-1);
163 do {
164 if (fout)
165 putc(c, fout);
166 } while ((c = getc(finp)) != EOF && !isspace(c));
167 }
168 return(0);
169 }
170
171 /* Copy routine for ascii data */
172 static int
173 ascii_copyf(FILE *fp)
174 {
175 const int width = scanlen(&res);
176 int x, y;
177
178 SET_FILE_TEXT(fp); /* started as binary */
179 SET_FILE_TEXT(stdout);
180 /* skip rows as requested */
181 if (readwords(fp, rmin*width*ncomp, NULL) < 0)
182 goto io_err;
183 for (y = 0; y < nrows; y++) { /* copy part */
184 if (readwords(fp, cmin*ncomp, NULL) < 0)
185 goto io_err;
186 if (readwords(fp, ncols*ncomp, stdout) < 0)
187 goto io_err;
188 fputc('\n', stdout); /* newline per row */
189 if (readwords(fp, (width-ncols-cmin)*ncomp, NULL) < 0)
190 goto io_err;
191 }
192 if (fflush(stdout) == 0)
193 return(1);
194 io_err:
195 fputs(progname, stderr);
196 fputs(": error copying ascii data\n", stderr);
197 return(0);
198 }
199
200 /* Adjust (crop) our view */
201 static int
202 adjust_view(void)
203 {
204 double p0[2], p1[2];
205 const char *err;
206
207 if (res.rt & YMAJOR) {
208 p0[0] = cmin/(double)res.xr;
209 p0[1] = rmin/(double)res.yr;
210 p1[0] = (cmin+ncols)/(double)res.xr;
211 p1[1] = (rmin+nrows)/(double)res.yr;
212 } else {
213 p0[0] = rmin/(double)res.xr;
214 p0[1] = cmin/(double)res.yr;
215 p1[0] = (rmin+nrows)/(double)res.xr;
216 p1[1] = (cmin+ncols)/(double)res.yr;
217 }
218 if (res.rt & XDECR) {
219 p0[0] = 1. - p0[0];
220 p1[0] = 1. - p1[0];
221 }
222 if (res.rt & YDECR) {
223 p0[1] = 1. - p0[1];
224 p1[1] = 1. - p1[1];
225 }
226 err = cropview(&vw, p0[0], p0[1], p1[0], p1[1]);
227
228 if (!err)
229 return(1); /* success! */
230
231 fputs(progname, stderr);
232 fputs(": view error - ", stderr);
233 fputs(err, stderr);
234 fputc('\n', stderr);
235 return(0); /* something went wrong */
236 }
237
238
239 /* Main routine -- load header and call processor */
240 int
241 main(int argc, char *argv[])
242 {
243 FILE *fp = stdin;
244 int asiz = 0;
245 int gotdims;
246
247 progname = argv[0];
248 /* get input and output */
249 if ((argc < 5) | (argc > 7))
250 goto usage;
251 if (!isint(argv[1]) | !isint(argv[2]) |
252 !isint(argv[3]) | !isint(argv[4]))
253 goto usage;
254 rmin = atoi(argv[1]);
255 cmin = atoi(argv[2]);
256 nrows = atoi(argv[3]);
257 ncols = atoi(argv[4]);
258 if ((rmin < 0) | (cmin < 0) | (nrows < 0) | (ncols < 0))
259 goto usage;
260 if (argc <= 5)
261 SET_FILE_BINARY(fp);
262 else if (!(fp = fopen(argv[5], "rb"))) {
263 fputs(argv[5], stderr);
264 fputs(": cannot open for reading\n", stderr);
265 return(1);
266 }
267 if (argc <= 6)
268 SET_FILE_BINARY(stdout);
269 else if (!freopen(argv[6], "wb", stdout)) {
270 fputs(argv[6], stderr);
271 fputs(": cannot open for writing\n", stderr);
272 return(1);
273 }
274 #ifdef getc_unlocked /* avoid stupid semaphores */
275 flockfile(fp);
276 flockfile(stdout);
277 #endif
278 /* process information header */
279 if (getheader(fp, headline, NULL) < 0) {
280 fputs(progname, stderr);
281 fputs(": bad input header\n", stderr);
282 return(1);
283 }
284 gotdims = (res.rt == PIXSTANDARD) & (res.xr > 0) & (res.yr > 0);
285 if (!gotdims && !fgetsresolu(&res, fp)) {
286 fputs(progname, stderr);
287 fputs(": missing input dimensions\n", stderr);
288 return(1);
289 }
290 if (!nrows)
291 nrows = numscans(&res) - rmin;
292 if (!ncols)
293 ncols = scanlen(&res) - cmin;
294 if ((nrows <= 0) | (ncols <= 0) |
295 (rmin+nrows > numscans(&res)) |
296 (cmin+ncols > scanlen(&res))) {
297 fputs(progname, stderr);
298 fputs(": illegal crop\n", stderr);
299 return(1);
300 }
301 printargs(5, argv, stdout); /* add to header */
302 if (gotvw && adjust_view()) {
303 fputs(VIEWSTR, stdout); /* write adjusted view */
304 fprintview(&vw, stdout);
305 fputc('\n', stdout);
306 }
307 if (gotdims) /* dimensions + format */
308 printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols);
309 if (ncomp)
310 printf("NCOMP=%d\n", ncomp);
311 fputformat(fmt, stdout); /* will align bytes if it can */
312 fputc('\n', stdout); /* end of new header */
313 if (!gotdims) { /* add resolution string? */
314 RESOLU newres;
315 if (res.rt & YMAJOR) {
316 newres.xr = ncols;
317 newres.yr = nrows;
318 } else {
319 newres.xr = nrows;
320 newres.yr = ncols;
321 }
322 newres.rt = res.rt;
323 fputsresolu(&newres, stdout);
324 }
325 /* call appropriate processor */
326 if (!strcmp(fmt, "float")) {
327 asiz = sizeof(float);
328 } else if (!strcmp(fmt, "double")) {
329 asiz = sizeof(double);
330 } else if (!strcmp(fmt, "32-bit_encoded_normal")) {
331 asiz = 4;
332 ncomp = 1;
333 } else if (!strcmp(fmt, "16-bit_encoded_depth")) {
334 asiz = 2;
335 ncomp = 1;
336 } else if (globmatch(PICFMT, fmt)) {
337 asiz = -1;
338 if (!ncomp) ncomp = 3;
339 else ncomp *= (ncomp == 3);
340 } else if (strcasecmp(fmt, "ascii")) {
341 fputs(progname, stderr);
342 fputs(": unsupported format - ", stderr);
343 fputs(fmt, stderr);
344 fputc('\n', stderr);
345 return(1);
346 }
347 if (ncomp <= 0) {
348 fputs(progname, stderr);
349 fputs(": illegal number of components\n", stderr);
350 return(1);
351 }
352 if (!(asiz < 0 ? colr_copyf(fp) :
353 asiz ? binary_copyf(fp, asiz) : ascii_copyf(fp)))
354 return(1);
355 /* need to consume the rest? */
356 if (fp == stdin && rmin+nrows < numscans(&res) &&
357 fseek(fp, 0L, SEEK_END) < 0)
358 while (getc(fp) != EOF)
359 ;
360 return(0);
361 usage:
362 fputs("Usage: ", stderr);
363 fputs(progname, stderr);
364 fputs(" row0 col0 nrows ncols [input [output]]\n", stderr);
365 return(1);
366 }