ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.40
Committed: Mon Nov 20 21:44:29 2023 UTC (5 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.39: +2 -2 lines
Log Message:
refactor: Eliminated LPICFMT macro

File Contents

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