ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.36
Committed: Mon Apr 16 19:40:37 2018 UTC (6 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.35: +7 -2 lines
Log Message:
Added call to getc_unlocked() to speed up reading of stream remainder

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.36 static const char RCSid[] = "$Id: pcompos.c,v 2.35 2016/02/02 18:02:32 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * pcompos.c - program to composite pictures.
6     *
7     * 6/30/87
8     */
9    
10     #include <stdio.h>
11 greg 2.16 #include <math.h>
12 schorsch 2.22 #include <time.h>
13 schorsch 2.23 #include <string.h>
14 greg 2.16
15 schorsch 2.22 #include "copyright.h"
16 greg 2.21
17 schorsch 2.22 #include "platform.h"
18 greg 2.35 #include "paths.h"
19 schorsch 2.29 #include "rterror.h"
20 greg 1.1 #include "color.h"
21 greg 1.10 #include "resolu.h"
22 greg 1.1
23 greg 2.36 #ifdef getc_unlocked /* avoid horrendous overhead of flockfile */
24     #undef getc
25     #define getc getc_unlocked
26     #endif
27    
28 greg 2.33 #define MAXFILE 1024
29 greg 1.1
30 greg 2.17 #define HASMIN 1
31     #define HASMAX 2
32    
33 greg 1.1 /* output picture size */
34     int xsiz = 0;
35     int ysiz = 0;
36     /* input dimensions */
37     int xmin = 0;
38     int ymin = 0;
39     int xmax = 0;
40     int ymax = 0;
41    
42     COLR bgcolr = BLKCOLR; /* background color */
43    
44 greg 1.9 int labelht = 24; /* label height */
45    
46 greg 1.1 int checkthresh = 0; /* check threshold value */
47    
48 greg 2.28 char StandardInput[] = "<stdin>";
49 greg 2.14 char Command[] = "<Command>";
50     char Label[] = "<Label>";
51    
52 greg 1.1 char *progname;
53    
54     struct {
55 greg 1.7 char *name; /* file or command name */
56 greg 1.1 FILE *fp; /* stream pointer */
57     int xres, yres; /* picture size */
58     int xloc, yloc; /* anchor point */
59 greg 2.17 int flags; /* HASMIN, HASMAX */
60 greg 2.31 double thmin, thmax; /* thresholds */
61 greg 1.1 } input[MAXFILE]; /* our input files */
62    
63     int nfile; /* number of files */
64    
65 greg 2.32 int echoheader = 1;
66 greg 2.19 char ourfmt[LPICFMT+1] = PICFMT;
67 greg 1.6 int wrongformat = 0;
68 greg 1.1
69 greg 1.7
70 greg 2.32 static gethfunc headline;
71 schorsch 2.29 static void compos(void);
72 greg 2.31 static int cmpcolr(COLR c1, double lv2);
73 schorsch 2.29 static FILE * lblopen(char *s, int *xp, int *yp);
74    
75 greg 2.15
76 schorsch 2.27
77     static int
78 greg 2.32 headline( /* print line preceded by a tab */
79 schorsch 2.27 char *s,
80     void *p
81     )
82 greg 1.1 {
83 greg 1.6 char fmt[32];
84    
85 greg 2.18 if (isheadid(s))
86 gwlarson 2.20 return(0);
87 greg 2.19 if (formatval(fmt, s)) {
88     if (globmatch(ourfmt, fmt)) {
89     wrongformat = 0;
90     strcpy(ourfmt, fmt);
91     } else
92     wrongformat = 1;
93 greg 2.32 } else if (echoheader) {
94 greg 2.36 fputc('\t', stdout);
95 greg 1.6 fputs(s, stdout);
96     }
97 gwlarson 2.20 return(0);
98 greg 1.1 }
99    
100    
101 schorsch 2.29 int
102     main(
103     int argc,
104     char *argv[]
105     )
106 greg 1.1 {
107 greg 1.7 int ncolumns = 0;
108 greg 1.9 int autolabel = 0;
109 greg 2.11 int curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0;
110 greg 2.17 int xsgn, ysgn;
111 greg 1.9 char *thislabel;
112 greg 1.1 int an;
113 schorsch 2.22 SET_DEFAULT_BINARY();
114     SET_FILE_BINARY(stdin);
115     SET_FILE_BINARY(stdout);
116 greg 1.1 progname = argv[0];
117    
118     for (an = 1; an < argc && argv[an][0] == '-'; an++)
119     switch (argv[an][1]) {
120 greg 2.32 case 'h':
121     echoheader = !echoheader;
122     break;
123 greg 1.1 case 'x':
124 greg 2.11 xsiz = atoi(argv[++an]);
125 greg 1.1 break;
126     case 'y':
127 greg 2.11 ysiz = atoi(argv[++an]);
128 greg 1.1 break;
129     case 'b':
130     setcolr(bgcolr, atof(argv[an+1]),
131     atof(argv[an+2]),
132     atof(argv[an+3]));
133     an += 3;
134     break;
135 greg 1.7 case 'a':
136     ncolumns = atoi(argv[++an]);
137     break;
138 greg 2.11 case 's':
139     spacing = atoi(argv[++an]);
140     break;
141     case 'o':
142     curx = x0 = atoi(argv[++an]);
143     cury = atoi(argv[++an]);
144     break;
145 greg 1.7 case 'l':
146 greg 1.9 switch (argv[an][2]) {
147     case 'a':
148     autolabel++;
149     break;
150     case 'h':
151     labelht = atoi(argv[++an]);
152     break;
153     case '\0':
154     goto dofiles;
155     default:
156     goto userr;
157     }
158 greg 1.7 break;
159 greg 1.1 case '\0':
160     case 't':
161     goto dofiles;
162     default:
163     goto userr;
164     }
165     dofiles:
166 greg 2.18 newheader("RADIANCE", stdout);
167 greg 2.21 fputnow(stdout);
168 greg 1.1 for (nfile = 0; an < argc; nfile++) {
169 greg 1.9 if (nfile >= MAXFILE)
170     goto toomany;
171 greg 2.6 thislabel = NULL;
172 greg 2.17 input[nfile].flags = 0;
173     xsgn = ysgn = '-';
174     while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'
175     || argv[an][0] == '=')) {
176 greg 1.1 switch (argv[an][1]) {
177     case 't':
178     checkthresh = 1;
179     if (argv[an][0] == '-') {
180 greg 2.17 input[nfile].flags |= HASMIN;
181 greg 2.31 input[nfile].thmin = atof(argv[an+1]);
182 greg 2.17 } else if (argv[an][0] == '+') {
183     input[nfile].flags |= HASMAX;
184 greg 2.31 input[nfile].thmax = atof(argv[an+1]);
185 greg 2.17 } else
186     goto userr;
187     an++;
188 greg 1.1 break;
189 greg 1.9 case 'l':
190     if (strcmp(argv[an], "-l"))
191     goto userr;
192 greg 2.17 thislabel = argv[++an];
193 greg 1.9 break;
194 greg 2.17 case '+':
195     case '-':
196     case '0':
197     if (argv[an][0] != '=')
198     goto userr;
199     xsgn = argv[an][1];
200     ysgn = argv[an][2];
201     if (ysgn != '+' && ysgn != '-' && ysgn != '0')
202     goto userr;
203     break;
204 greg 1.1 case '\0':
205     if (argv[an][0] == '-')
206     goto getfile;
207 greg 1.9 goto userr;
208 greg 1.1 default:
209     goto userr;
210     }
211 greg 2.17 an++;
212     }
213 greg 1.1 getfile:
214 greg 1.7 if (argc-an < (ncolumns ? 1 : 3))
215 greg 1.1 goto userr;
216 greg 2.6 if (autolabel && thislabel == NULL)
217     thislabel = argv[an];
218 greg 1.1 if (!strcmp(argv[an], "-")) {
219 greg 2.28 input[nfile].name = StandardInput;
220 greg 1.1 input[nfile].fp = stdin;
221     } else {
222 greg 2.4 if (argv[an][0] == '!') {
223 greg 2.14 input[nfile].name = Command;
224 greg 2.4 input[nfile].fp = popen(argv[an]+1, "r");
225     } else {
226     input[nfile].name = argv[an];
227     input[nfile].fp = fopen(argv[an], "r");
228     }
229     if (input[nfile].fp == NULL) {
230 greg 1.5 perror(argv[an]);
231 greg 1.1 quit(1);
232     }
233     }
234     an++;
235     /* get header */
236 greg 2.32 if (echoheader)
237     printf("%s:\n", input[nfile].name);
238     getheader(input[nfile].fp, headline, NULL);
239 greg 1.6 if (wrongformat) {
240 greg 2.21 fprintf(stderr, "%s: incompatible input format\n",
241 greg 1.6 input[nfile].name);
242     quit(1);
243     }
244 greg 1.1 /* get picture size */
245 greg 1.4 if (fgetresolu(&input[nfile].xres, &input[nfile].yres,
246 greg 1.10 input[nfile].fp) < 0) {
247 greg 1.1 fprintf(stderr, "%s: bad picture size\n",
248     input[nfile].name);
249     quit(1);
250     }
251 greg 2.34 if (ncolumns) {
252     if (curcol >= abs(ncolumns)) {
253 greg 2.11 cury = ymax + spacing;
254     curx = x0;
255 greg 1.7 curcol = 0;
256     }
257     input[nfile].xloc = curx;
258     input[nfile].yloc = cury;
259 greg 2.11 curx += input[nfile].xres + spacing;
260 greg 1.7 curcol++;
261     } else {
262     input[nfile].xloc = atoi(argv[an++]);
263 greg 2.17 if (xsgn == '+')
264     input[nfile].xloc -= input[nfile].xres;
265     else if (xsgn == '0')
266     input[nfile].xloc -= input[nfile].xres/2;
267 greg 1.7 input[nfile].yloc = atoi(argv[an++]);
268 greg 2.17 if (ysgn == '+')
269     input[nfile].yloc -= input[nfile].yres;
270     else if (ysgn == '0')
271     input[nfile].yloc -= input[nfile].yres/2;
272 greg 1.7 }
273 greg 1.1 if (input[nfile].xloc < xmin)
274     xmin = input[nfile].xloc;
275     if (input[nfile].yloc < ymin)
276     ymin = input[nfile].yloc;
277     if (input[nfile].xloc+input[nfile].xres > xmax)
278     xmax = input[nfile].xloc+input[nfile].xres;
279     if (input[nfile].yloc+input[nfile].yres > ymax)
280     ymax = input[nfile].yloc+input[nfile].yres;
281 greg 1.9 if (thislabel != NULL) {
282     if (++nfile >= MAXFILE)
283     goto toomany;
284 greg 2.14 input[nfile].name = Label;
285 greg 2.17 input[nfile].flags = 0;
286 greg 2.7 input[nfile].xres = input[nfile-1].xres;
287     input[nfile].yres = labelht;
288 greg 1.9 if ((input[nfile].fp = lblopen(thislabel,
289     &input[nfile].xres,
290     &input[nfile].yres)) == NULL)
291 greg 1.7 goto labelerr;
292     input[nfile].xloc = input[nfile-1].xloc;
293     input[nfile].yloc = input[nfile-1].yloc +
294 greg 1.9 input[nfile-1].yres-input[nfile].yres;
295 greg 1.7 }
296 greg 1.1 }
297     if (xsiz <= 0)
298     xsiz = xmax;
299 greg 2.11 else if (xsiz > xmax)
300     xmax = xsiz;
301 greg 1.1 if (ysiz <= 0)
302     ysiz = ymax;
303 greg 2.11 else if (ysiz > ymax)
304     ymax = ysiz;
305 greg 2.34 if (ncolumns < 0) { /* reverse rows if requested */
306     int i = nfile;
307     while (i--)
308     input[i].yloc = ymax - input[i].yres - input[i].yloc;
309     }
310 greg 1.1 /* add new header info. */
311     printargs(argc, argv, stdout);
312 greg 2.19 if (strcmp(ourfmt, PICFMT))
313     fputformat(ourfmt, stdout); /* print format if known */
314 greg 1.10 putchar('\n');
315     fprtresolu(xsiz, ysiz, stdout);
316 greg 1.1
317     compos();
318    
319     quit(0);
320     userr:
321 greg 2.11 fprintf(stderr,
322 greg 2.32 "Usage: %s [-h][-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ",
323 greg 1.8 progname);
324 greg 2.17 fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n");
325 greg 1.7 quit(1);
326 greg 1.9 toomany:
327     fprintf(stderr, "%s: only %d files and labels allowed\n",
328     progname, MAXFILE);
329     quit(1);
330 greg 1.7 labelerr:
331     fprintf(stderr, "%s: error opening label\n", progname);
332 greg 1.1 quit(1);
333 schorsch 2.29 return 1; /* pro forma return */
334 greg 1.1 }
335    
336    
337 schorsch 2.29 static void
338     compos(void) /* composite pictures */
339 greg 1.1 {
340     COLR *scanin, *scanout;
341     int y;
342     register int x, i;
343    
344 greg 1.3 scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR));
345 greg 1.1 if (scanin == NULL)
346     goto memerr;
347     scanin -= xmin;
348     if (checkthresh) {
349     scanout = (COLR *)malloc(xsiz*sizeof(COLR));
350     if (scanout == NULL)
351     goto memerr;
352     } else
353     scanout = scanin;
354     for (y = ymax-1; y >= 0; y--) {
355     for (x = 0; x < xsiz; x++)
356     copycolr(scanout[x], bgcolr);
357     for (i = 0; i < nfile; i++) {
358     if (input[i].yloc > y ||
359     input[i].yloc+input[i].yres <= y)
360     continue;
361     if (freadcolrs(scanin+input[i].xloc,
362     input[i].xres, input[i].fp) < 0) {
363 greg 1.2 fprintf(stderr, "%s: read error (y==%d)\n",
364     input[i].name,
365     y-input[i].yloc);
366 greg 1.1 quit(1);
367     }
368     if (y >= ysiz)
369     continue;
370     if (checkthresh) {
371     x = input[i].xloc+input[i].xres;
372     if (x > xsiz)
373     x = xsiz;
374     for (x--; x >= 0 && x >= input[i].xloc; x--) {
375 greg 2.17 if (input[i].flags & HASMIN &&
376 greg 1.1 cmpcolr(scanin[x], input[i].thmin) <= 0)
377     continue;
378 greg 2.17 if (input[i].flags & HASMAX &&
379 greg 1.1 cmpcolr(scanin[x], input[i].thmax) >= 0)
380     continue;
381     copycolr(scanout[x], scanin[x]);
382     }
383     }
384     }
385     if (y >= ysiz)
386     continue;
387     if (fwritecolrs(scanout, xsiz, stdout) < 0) {
388 greg 1.5 perror(progname);
389 greg 1.1 quit(1);
390     }
391     }
392 greg 2.28 /* read remainders from streams */
393     for (i = 0; i < nfile; i++)
394     if (input[i].name[0] == '<')
395     while (getc(input[i].fp) != EOF)
396     ;
397 greg 1.1 return;
398     memerr:
399 greg 1.5 perror(progname);
400 greg 1.1 quit(1);
401     }
402    
403    
404 schorsch 2.29 static int
405 greg 2.31 cmpcolr( /* compare COLR to luminance */
406 schorsch 2.29 register COLR c1,
407 greg 2.31 double lv2
408 schorsch 2.29 )
409 greg 1.1 {
410 greg 2.31 double lv1 = .0;
411    
412     if (c1[EXP])
413     lv1 = ldexp((double)normbright(c1), (int)c1[EXP]-(COLXS+8));
414     if (lv1 < lv2) return(-1);
415     if (lv1 > lv2) return(1);
416 greg 1.1 return(0);
417 greg 1.9 }
418    
419    
420 schorsch 2.29 static FILE *
421     lblopen( /* open pipe to label generator */
422     char *s,
423     int *xp,
424     int *yp
425     )
426 greg 1.9 {
427 schorsch 2.26 char com[PATH_MAX];
428 greg 1.9 FILE *fp;
429    
430 greg 2.7 sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s);
431 greg 1.9 if ((fp = popen(com, "r")) == NULL)
432     return(NULL);
433     if (checkheader(fp, COLRFMT, NULL) < 0)
434     goto err;
435 greg 1.10 if (fgetresolu(xp, yp, fp) < 0)
436 greg 1.9 goto err;
437     return(fp);
438     err:
439     pclose(fp);
440     return(NULL);
441 greg 1.1 }
442    
443    
444 greg 2.21 void
445 greg 1.1 quit(code) /* exit gracefully */
446     int code;
447     {
448 greg 2.13 register int i;
449     /* close input files */
450     for (i = 0; i < nfile; i++)
451 greg 2.14 if (input[i].name == Command || input[i].name == Label)
452     pclose(input[i].fp);
453     else
454     fclose(input[i].fp);
455 greg 1.1 exit(code);
456     }