ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.42
Committed: Sat Jun 7 05:09:46 2025 UTC (12 days, 5 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.41: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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