ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
Revision: 2.31
Committed: Tue Jun 6 19:13:00 2006 UTC (17 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +13 -19 lines
Log Message:
Fixed long-standing inaccuracy of +/-t threshold options

File Contents

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