ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.34
Committed: Thu Jun 4 01:19:30 2015 UTC (8 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.33: +8 -3 lines
Log Message:
Added Roland and Andreas' request to place images using -a from top-left

File Contents

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