ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rcrop.c
Revision: 1.7
Committed: Tue Mar 15 20:21:18 2022 UTC (2 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +5 -1 lines
Log Message:
perf(rcrop): added flockfile() call to avoid semaphores in fread() and fwrite()

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rcrop.c,v 1.6 2022/03/15 15:01:22 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 #define MAXWORD 64 /* maximum word (number) length */
16
17 char *progname; /* global argv[0] */
18
19 VIEW vw = STDVIEW;
20 int gotvw = 0;
21 char fmt[MAXFMTLEN] = "Unknown";
22 int ncomp = 0;
23 RESOLU res;
24 int rmin, cmin, nrows, ncols;
25
26 /* Process header line, copying to stdout when appropriate */
27 static int
28 headline(char *s, void *p)
29 {
30 if (formatval(fmt, s))
31 return(0);
32 if (!strncmp(s, "NCOMP=", 6)) {
33 ncomp = atoi(s+6);
34 return(-(ncomp <= 0));
35 }
36 if (!strncmp(s, "NROWS=", 6)) {
37 res.rt = PIXSTANDARD;
38 res.yr = atoi(s+6);
39 return(-(res.yr <= 0));
40 }
41 if (!strncmp(s, "NCOLS=", 6)) {
42 res.rt = PIXSTANDARD;
43 res.xr = atoi(s+6);
44 return(-(res.xr <= 0));
45 }
46 if (isview(s)) {
47 gotvw += sscanview(&vw, s);
48 return(0);
49 }
50 fputs(s, stdout); /* copy other header info. */
51 return(0);
52 }
53
54 /* Copy routine for COLR data */
55 static int
56 colr_copyf(FILE *fp)
57 {
58 const int width = scanlen(&res);
59 COLR *scan = (COLR *)malloc(sizeof(COLR)*width);
60 int y;
61
62 if (!scan) {
63 fputs(progname, stderr);
64 fputs(": out of memory!\n", stderr);
65 return(0);
66 }
67 for (y = 0; y < rmin; y++) /* initial skip */
68 if (freadcolrs(scan, width, fp) < 0)
69 goto readerr;
70 /* scanlines to copy */
71 for (y = 0; y < nrows; y++) {
72 if (freadcolrs(scan, width, fp) < 0)
73 goto readerr;
74 if (fwritecolrs(scan+cmin, ncols, stdout) < 0)
75 goto writerr;
76 }
77 free(scan);
78 if (fflush(stdout) == 0)
79 return(1);
80 writerr:
81 fputs(progname, stderr);
82 fputs(": error writing picture\n", stderr);
83 return(0);
84 readerr:
85 fputs(progname, stderr);
86 fputs(": error reading picture\n", stderr);
87 return(0);
88 }
89
90 /* Copy routine for binary data (asize = sizeof(type)) */
91 static int
92 binary_copyf(FILE *fp, int asize)
93 {
94 const int skip_thresh = 8192;
95 const size_t elsiz = asize*ncomp;
96 const int width = scanlen(&res);
97 const long skip_len = (width-ncols)*elsiz;
98 char *buf;
99 int y;
100 /* check if fseek() useful */
101 if (skip_len > skip_thresh &&
102 fseek(fp, (rmin*width + cmin)*elsiz, SEEK_CUR) == 0) {
103 buf = (char *)malloc(ncols*elsiz);
104 if (!buf)
105 goto memerr;
106 for (y = nrows; y-- > 0; ) {
107 if (getbinary(buf, elsiz, ncols, fp) != ncols)
108 goto readerr;
109 if (putbinary(buf, elsiz, ncols, stdout) != ncols)
110 goto writerr;
111 if (y && fseek(fp, skip_len, SEEK_CUR) < 0) {
112 fputs(progname, stderr);
113 fputs(": unexpected seek error on input\n", stderr);
114 return(0);
115 }
116 }
117 free(buf); /* success! */
118 return(1);
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 /* Main routine -- load header and call processor */
203 int
204 main(int argc, char *argv[])
205 {
206 FILE *fp = stdin;
207 int asiz = 0;
208 int gotdims;
209
210 progname = argv[0];
211 /* get input and output */
212 if ((argc < 5) | (argc > 7))
213 goto usage;
214 if (!isint(argv[1]) | !isint(argv[2]) |
215 !isint(argv[3]) | !isint(argv[4]))
216 goto usage;
217 rmin = atoi(argv[1]);
218 cmin = atoi(argv[2]);
219 nrows = atoi(argv[3]);
220 ncols = atoi(argv[4]);
221 if ((rmin < 0) | (cmin < 0) | (nrows < 0) | (ncols < 0))
222 goto usage;
223 if (argc <= 5)
224 SET_FILE_BINARY(fp);
225 else if (!(fp = fopen(argv[5], "rb"))) {
226 fputs(argv[5], stderr);
227 fputs(": cannot open for reading\n", stderr);
228 return(1);
229 }
230 if (argc <= 6)
231 SET_FILE_BINARY(stdout);
232 else if (!freopen(argv[6], "wb", stdout)) {
233 fputs(argv[6], stderr);
234 fputs(": cannot open for writing\n", stderr);
235 return(1);
236 }
237 #ifdef getc_unlocked /* avoid stupid semaphores */
238 flockfile(fp);
239 flockfile(stdout);
240 #endif
241 /* process information header */
242 if (getheader(fp, headline, NULL) < 0) {
243 fputs(progname, stderr);
244 fputs(": bad input header\n", stderr);
245 return(1);
246 }
247 gotdims = (res.rt == PIXSTANDARD) & (res.xr > 0) & (res.yr > 0);
248 if (!gotdims && !fgetsresolu(&res, fp)) {
249 fputs(progname, stderr);
250 fputs(": missing input dimensions\n", stderr);
251 return(1);
252 }
253 if (!nrows)
254 nrows = numscans(&res) - rmin;
255 if (!ncols)
256 ncols = scanlen(&res) - cmin;
257 if ((nrows <= 0) | (ncols <= 0) |
258 (rmin+nrows > numscans(&res)) |
259 (cmin+ncols > scanlen(&res))) {
260 fputs(progname, stderr);
261 fputs(": illegal crop\n", stderr);
262 return(1);
263 }
264 printargs(argc, argv, stdout);
265 if (gotvw) { /* adjust view? */
266 double p0[2], p1[2];
267 const char *err;
268 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[1] = cmin/(double)res.xr;
275 p0[0] = rmin/(double)res.yr;
276 p1[1] = (cmin+ncols)/(double)res.xr;
277 p1[0] = (rmin+nrows)/(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 }
299 }
300 if (gotdims)
301 printf("NROWS=%d\nNCOLS=%d\n", nrows, ncols);
302 if (ncomp)
303 printf("NCOMP=%d\n", ncomp);
304 fputformat(fmt, stdout); /* will align bytes if it can */
305 fputc('\n', stdout); /* end of new header */
306 if (!gotdims) { /* add resolution string? */
307 RESOLU newres;
308 if (res.rt & YMAJOR) {
309 newres.xr = ncols;
310 newres.yr = nrows;
311 } else {
312 newres.xr = nrows;
313 newres.yr = ncols;
314 }
315 newres.rt = res.rt;
316 fputsresolu(&newres, stdout);
317 }
318 /* call appropriate processor */
319 if (!strcmp(fmt, "float")) {
320 asiz = sizeof(float);
321 } else if (!strcmp(fmt, "double")) {
322 asiz = sizeof(double);
323 } else if (!strcmp(fmt, "32-bit_encoded_normal")) {
324 asiz = 4;
325 ncomp = 1;
326 } else if (!strcmp(fmt, "16-bit_encoded_depth")) {
327 asiz = 2;
328 ncomp = 1;
329 } else if (globmatch(PICFMT, fmt)) {
330 asiz = -1;
331 if (!ncomp) ncomp = 3;
332 else ncomp *= (ncomp == 3);
333 } else if (strcasecmp(fmt, "ascii")) {
334 fputs(progname, stderr);
335 fputs(": unsupported format - ", stderr);
336 fputs(fmt, stderr);
337 fputc('\n', stderr);
338 return(1);
339 }
340 if (ncomp <= 0) {
341 fputs(progname, stderr);
342 fputs(": illegal number of components\n", stderr);
343 return(1);
344 }
345 if (!(asiz < 0 ? colr_copyf(fp) :
346 !asiz ? ascii_copyf(fp) : binary_copyf(fp, asiz)))
347 return(1);
348 /* need to consume the rest? */
349 if (fp == stdin && rmin+nrows < numscans(&res) &&
350 fseek(fp, 0L, SEEK_END) < 0)
351 while (getc(fp) != EOF)
352 ;
353 return(0);
354 usage:
355 fputs("Usage: ", stderr);
356 fputs(progname, stderr);
357 fputs(" row0 col0 nrows ncols [input [output]]\n", stderr);
358 return(1);
359 }