ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcompos.c
(Generate patch)

Comparing ray/src/px/pcompos.c (file contents):
Revision 1.7 by greg, Thu May 30 09:44:36 1991 UTC vs.
Revision 2.33 by greg, Mon Aug 10 04:26:38 2009 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  pcompos.c - program to composite pictures.
6   *
# Line 11 | Line 8 | static char SCCSid[] = "$SunId$ LBL";
8   */
9  
10   #include  <stdio.h>
11 + #include  <math.h>
12 + #include  <time.h>
13 + #include  <string.h>
14  
15 + #include "copyright.h"
16 +
17 + #include  "platform.h"
18 + #include  "rtprocess.h"
19 + #include  "rterror.h"
20   #include  "color.h"
21 + #include  "resolu.h"
22  
23 + #define  MAXFILE        1024
24  
25 < #define  MAXFILE        32
25 > #define  HASMIN         1
26 > #define  HASMAX         2
27  
20 #define  SIGNHT         24
21 #define  setpscom(b,n)  sprintf(b, "psign -h %d '%.30s'|pfilt -1 -x /2 -y /2",\
22                                2*SIGNHT, n)
23
28                                          /* output picture size */
29   int  xsiz = 0;
30   int  ysiz = 0;
# Line 32 | Line 36 | int  ymax = 0;
36  
37   COLR  bgcolr = BLKCOLR;                 /* background color */
38  
39 + int  labelht = 24;                      /* label height */
40 +
41   int  checkthresh = 0;                   /* check threshold value */
42  
43 + char  StandardInput[] = "<stdin>";
44 + char  Command[] = "<Command>";
45 + char  Label[] = "<Label>";
46 +
47   char  *progname;
48  
49   struct {
# Line 41 | Line 51 | struct {
51          FILE  *fp;                      /* stream pointer */
52          int  xres, yres;                /* picture size */
53          int  xloc, yloc;                /* anchor point */
54 <        int  hasmin, hasmax;            /* has threshold values */
55 <        COLR  thmin, thmax;             /* thresholds */
54 >        int  flags;                     /* HASMIN, HASMAX */
55 >        double  thmin, thmax;           /* thresholds */
56   } input[MAXFILE];               /* our input files */
57  
58   int  nfile;                     /* number of files */
59  
60 + int  echoheader = 1;
61 + char  ourfmt[LPICFMT+1] = PICFMT;
62   int  wrongformat = 0;
63  
52 char  tmpbuf[128];
64  
65 < FILE  *popen();
65 > static gethfunc headline;
66 > static void compos(void);
67 > static int cmpcolr(COLR  c1, double lv2);
68 > static FILE * lblopen(char  *s, int  *xp, int  *yp);
69  
70  
71 < tabputs(s)                      /* print line preceded by a tab */
72 < char  *s;
71 >
72 > static int
73 > headline(                       /* print line preceded by a tab */
74 >        char    *s,
75 >        void    *p
76 > )
77   {
78          char  fmt[32];
79  
80 <        if (isformat(s)) {
81 <                formatval(fmt, s);
82 <                wrongformat = strcmp(fmt, COLRFMT);
83 <        } else {
80 >        if (isheadid(s))
81 >                return(0);
82 >        if (formatval(fmt, s)) {
83 >                if (globmatch(ourfmt, fmt)) {
84 >                        wrongformat = 0;
85 >                        strcpy(ourfmt, fmt);
86 >                } else
87 >                        wrongformat = 1;
88 >        } else if (echoheader) {
89                  putc('\t', stdout);
90                  fputs(s, stdout);
91          }
92 +        return(0);
93   }
94  
95  
96 < main(argc, argv)
97 < int  argc;
98 < char  *argv[];
96 > int
97 > main(
98 >        int  argc,
99 >        char  *argv[]
100 > )
101   {
76        double  atof();
102          int  ncolumns = 0;
103 <        int  dolabels = 0;
104 <        int  curcol = 0, curx = 0, cury = 0;
103 >        int  autolabel = 0;
104 >        int  curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0;
105 >        int  xsgn, ysgn;
106 >        char  *thislabel;
107          int  an;
108 <
108 >        SET_DEFAULT_BINARY();
109 >        SET_FILE_BINARY(stdin);
110 >        SET_FILE_BINARY(stdout);
111          progname = argv[0];
112  
113          for (an = 1; an < argc && argv[an][0] == '-'; an++)
114                  switch (argv[an][1]) {
115 +                case 'h':
116 +                        echoheader = !echoheader;
117 +                        break;
118                  case 'x':
119 <                        xmax = xsiz = atoi(argv[++an]);
119 >                        xsiz = atoi(argv[++an]);
120                          break;
121                  case 'y':
122 <                        ymax = ysiz = atoi(argv[++an]);
122 >                        ysiz = atoi(argv[++an]);
123                          break;
124                  case 'b':
125                          setcolr(bgcolr, atof(argv[an+1]),
# Line 98 | Line 130 | char  *argv[];
130                  case 'a':
131                          ncolumns = atoi(argv[++an]);
132                          break;
133 +                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                  case 'l':
141 <                        dolabels++;
141 >                        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                          break;
154                  case '\0':
155                  case 't':
# Line 108 | Line 158 | char  *argv[];
158                          goto userr;
159                  }
160   dofiles:
161 +        newheader("RADIANCE", stdout);
162 +        fputnow(stdout);
163          for (nfile = 0; an < argc; nfile++) {
164 <                if (dolabels) {
165 <                        if (nfile >= MAXFILE-1) {
166 <                                fprintf(stderr,
167 <                                "%s: only %d files allowed with labels\n",
168 <                                        progname, MAXFILE/2);
169 <                        }
170 <                } else {
119 <                        if (nfile >= MAXFILE) {
120 <                                fprintf(stderr, "%s: only %d files allowed\n",
121 <                                                progname, MAXFILE);
122 <                                quit(1);
123 <                        }
124 <                }
125 <                if (nfile >= (dolabels ? MAXFILE-1 : MAXFILE)) {
126 <                        fprintf(stderr, "%s: too many files\n", progname);
127 <                        quit(1);
128 <                }
129 <                input[nfile].hasmin = input[nfile].hasmax = 0;
130 <                while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'))
164 >                if (nfile >= MAXFILE)
165 >                        goto toomany;
166 >                thislabel = NULL;
167 >                input[nfile].flags = 0;
168 >                xsgn = ysgn = '-';
169 >                while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'
170 >                                || argv[an][0] == '=')) {
171                          switch (argv[an][1]) {
172                          case 't':
173                                  checkthresh = 1;
174                                  if (argv[an][0] == '-') {
175 <                                        input[nfile].hasmin = 1;
176 <                                        setcolr(input[nfile].thmin,
177 <                                                        atof(argv[an+1]),
178 <                                                        atof(argv[an+1]),
179 <                                                        atof(argv[an+1]));
180 <                                } else {
181 <                                        input[nfile].hasmax = 1;
182 <                                        setcolr(input[nfile].thmax,
143 <                                                        atof(argv[an+1]),
144 <                                                        atof(argv[an+1]),
145 <                                                        atof(argv[an+1]));
146 <                                }
147 <                                an += 2;
175 >                                        input[nfile].flags |= HASMIN;
176 >                                        input[nfile].thmin = atof(argv[an+1]);
177 >                                } else if (argv[an][0] == '+') {
178 >                                        input[nfile].flags |= HASMAX;
179 >                                        input[nfile].thmax = atof(argv[an+1]);
180 >                                } else
181 >                                        goto userr;
182 >                                an++;
183                                  break;
184 +                        case 'l':
185 +                                if (strcmp(argv[an], "-l"))
186 +                                        goto userr;
187 +                                thislabel = argv[++an];
188 +                                break;
189 +                        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                          case '\0':
200                                  if (argv[an][0] == '-')
201                                          goto getfile;
202 <                        /* fall through */
202 >                                goto userr;
203                          default:
204                                  goto userr;
205                          }
206 +                        an++;
207 +                }
208   getfile:
209                  if (argc-an < (ncolumns ? 1 : 3))
210                          goto userr;
211 +                if (autolabel && thislabel == NULL)
212 +                        thislabel = argv[an];
213                  if (!strcmp(argv[an], "-")) {
214 <                        input[nfile].name = "<stdin>";
214 >                        input[nfile].name = StandardInput;
215                          input[nfile].fp = stdin;
216                  } else {
217 <                        input[nfile].name = argv[an];
218 <                        if ((input[nfile].fp = argv[an][0] == '!' ?
219 <                                        popen(argv[an]+1, "r") :
220 <                                        fopen(argv[an], "r")) == NULL) {
217 >                        if (argv[an][0] == '!') {
218 >                                input[nfile].name = Command;
219 >                                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                                  perror(argv[an]);
226                                  quit(1);
227                          }
228                  }
229                  an++;
230                                                  /* get header */
231 <                printf("%s:\n", input[nfile].name);
232 <                getheader(input[nfile].fp, tabputs, NULL);
231 >                if (echoheader)
232 >                        printf("%s:\n", input[nfile].name);
233 >                getheader(input[nfile].fp, headline, NULL);
234                  if (wrongformat) {
235 <                        fprintf(stderr, "%s: not a Radiance picture\n",
235 >                        fprintf(stderr, "%s: incompatible input format\n",
236                                          input[nfile].name);
237                          quit(1);
238                  }
239                                                  /* get picture size */
240                  if (fgetresolu(&input[nfile].xres, &input[nfile].yres,
241 <                                input[nfile].fp) != (YMAJOR|YDECR)) {
241 >                                input[nfile].fp) < 0) {
242                          fprintf(stderr, "%s: bad picture size\n",
243                                          input[nfile].name);
244                          quit(1);
245                  }
246                  if (ncolumns > 0) {
247                          if (curcol >= ncolumns) {
248 <                                cury = ymax;
249 <                                curx = 0;
248 >                                cury = ymax + spacing;
249 >                                curx = x0;
250                                  curcol = 0;
251                          }
252                          input[nfile].xloc = curx;
253                          input[nfile].yloc = cury;
254 <                        curx += input[nfile].xres;
254 >                        curx += input[nfile].xres + spacing;
255                          curcol++;
256                  } else {
257                          input[nfile].xloc = atoi(argv[an++]);
258 +                        if (xsgn == '+')
259 +                                input[nfile].xloc -= input[nfile].xres;
260 +                        else if (xsgn == '0')
261 +                                input[nfile].xloc -= input[nfile].xres/2;
262                          input[nfile].yloc = atoi(argv[an++]);
263 +                        if (ysgn == '+')
264 +                                input[nfile].yloc -= input[nfile].yres;
265 +                        else if (ysgn == '0')
266 +                                input[nfile].yloc -= input[nfile].yres/2;
267                  }
268                  if (input[nfile].xloc < xmin)
269                          xmin = input[nfile].xloc;
# Line 206 | Line 273 | getfile:
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 <                if (dolabels) {
277 <                        input[++nfile].name = "<Label>";
278 <                        setpscom(tmpbuf, input[nfile-1].name);
279 <                        if ((input[nfile].fp = popen(tmpbuf, "r")) == NULL)
276 >                if (thislabel != NULL) {
277 >                        if (++nfile >= MAXFILE)
278 >                                goto toomany;
279 >                        input[nfile].name = Label;
280 >                        input[nfile].flags = 0;
281 >                        input[nfile].xres = input[nfile-1].xres;
282 >                        input[nfile].yres = labelht;
283 >                        if ((input[nfile].fp = lblopen(thislabel,
284 >                                        &input[nfile].xres,
285 >                                        &input[nfile].yres)) == NULL)
286                                  goto labelerr;
214                        if (checkheader(input[nfile].fp, COLRFMT, NULL) < 0)
215                                goto labelerr;
216                        if (fgetresolu(&input[nfile].xres, &input[nfile].yres,
217                                        input[nfile].fp) != (YMAJOR|YDECR))
218                                goto labelerr;
287                          input[nfile].xloc = input[nfile-1].xloc;
288                          input[nfile].yloc = input[nfile-1].yloc +
289 <                                        input[nfile-1].yres - SIGNHT;
222 <                        input[nfile].hasmin = input[nfile].hasmax = 0;
289 >                                        input[nfile-1].yres-input[nfile].yres;
290                  }
291          }
292          if (xsiz <= 0)
293                  xsiz = xmax;
294 +        else if (xsiz > xmax)
295 +                xmax = xsiz;
296          if (ysiz <= 0)
297                  ysiz = ymax;
298 +        else if (ysiz > ymax)
299 +                ymax = ysiz;
300                                          /* add new header info. */
301          printargs(argc, argv, stdout);
302 <        fputformat(COLRFMT, stdout);
303 <        printf("\n-Y %d +X %d\n", ysiz, xsiz);
302 >        if (strcmp(ourfmt, PICFMT))
303 >                fputformat(ourfmt, stdout);     /* print format if known */
304 >        putchar('\n');
305 >        fprtresolu(xsiz, ysiz, stdout);
306  
307          compos();
308          
309          quit(0);
310   userr:
311 <        fprintf(stderr, "Usage: %s [-x xres][-y yres][-b r g b] ", progname);
312 <        fprintf(stderr, "[-t min1][+t max1] file1 x1 y1 ..\n");
311 >        fprintf(stderr,
312 >        "Usage: %s [-h][-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ",
313 >                        progname);
314 >        fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n");
315          quit(1);
316 + toomany:
317 +        fprintf(stderr, "%s: only %d files and labels allowed\n",
318 +                        progname, MAXFILE);
319 +        quit(1);
320   labelerr:
321          fprintf(stderr, "%s: error opening label\n", progname);
322          quit(1);
323 +        return 1; /* pro forma return */
324   }
325  
326  
327 < compos()                                /* composite pictures */
327 > static void
328 > compos(void)                            /* composite pictures */
329   {
330          COLR  *scanin, *scanout;
331          int  y;
# Line 281 | Line 362 | compos()                               /* composite pictures */
362                                  if (x > xsiz)
363                                          x = xsiz;
364                                  for (x--; x >= 0 && x >= input[i].xloc; x--) {
365 <                                        if (input[i].hasmin &&
365 >                                        if (input[i].flags & HASMIN &&
366                                          cmpcolr(scanin[x], input[i].thmin) <= 0)
367                                                  continue;
368 <                                        if (input[i].hasmax &&
368 >                                        if (input[i].flags & HASMAX &&
369                                          cmpcolr(scanin[x], input[i].thmax) >= 0)
370                                                  continue;
371                                          copycolr(scanout[x], scanin[x]);
# Line 298 | Line 379 | compos()                               /* composite pictures */
379                          quit(1);
380                  }
381          }
382 +                                        /* read remainders from streams */
383 +        for (i = 0; i < nfile; i++)
384 +                if (input[i].name[0] == '<')
385 +                        while (getc(input[i].fp) != EOF)
386 +                                ;
387          return;
388   memerr:
389          perror(progname);
# Line 305 | Line 391 | memerr:
391   }
392  
393  
394 < int
395 < cmpcolr(c1, c2)                 /* compare two colr's (improvisation) */
396 < register COLR  c1, c2;
394 > static int
395 > cmpcolr(                        /* compare COLR to luminance */
396 >        register COLR  c1,
397 >        double lv2
398 > )
399   {
400 <        register int  i, j;
401 <
402 <        j = 4;                          /* check exponents first! */
403 <        while (j--)
404 <                if (i = c1[j] - c2[j])
405 <                        return(i);
400 >        double  lv1 = .0;
401 >        
402 >        if (c1[EXP])
403 >                lv1 = ldexp((double)normbright(c1), (int)c1[EXP]-(COLXS+8));
404 >        if (lv1 < lv2) return(-1);
405 >        if (lv1 > lv2) return(1);
406          return(0);
407   }
408  
409  
410 + static FILE *
411 + lblopen(                /* open pipe to label generator */
412 +        char  *s,
413 +        int  *xp,
414 +        int  *yp
415 + )
416 + {
417 +        char  com[PATH_MAX];
418 +        FILE  *fp;
419 +
420 +        sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s);
421 +        if ((fp = popen(com, "r")) == NULL)
422 +                return(NULL);
423 +        if (checkheader(fp, COLRFMT, NULL) < 0)
424 +                goto err;
425 +        if (fgetresolu(xp, yp, fp) < 0)
426 +                goto err;
427 +        return(fp);
428 + err:
429 +        pclose(fp);
430 +        return(NULL);
431 + }
432 +
433 +
434 + void
435   quit(code)              /* exit gracefully */
436   int  code;
437   {
438 +        register int  i;
439 +                                /* close input files */
440 +        for (i = 0; i < nfile; i++)
441 +                if (input[i].name == Command || input[i].name == Label)
442 +                        pclose(input[i].fp);
443 +                else
444 +                        fclose(input[i].fp);
445          exit(code);
446   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines