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 2.5 by greg, Tue Jun 16 16:48:41 1992 UTC vs.
Revision 2.36 by greg, Mon Apr 16 19:40:37 2018 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  "color.h"
15 > #include "copyright.h"
16  
17 + #include  "platform.h"
18 + #include  "paths.h"
19 + #include  "rterror.h"
20 + #include  "color.h"
21   #include  "resolu.h"
22  
23 < #define  MAXFILE        32
23 > #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
24 > #undef getc
25 > #define getc    getc_unlocked
26 > #endif
27  
28 + #define  MAXFILE        1024
29 +
30 + #define  HASMIN         1
31 + #define  HASMAX         2
32 +
33                                          /* output picture size */
34   int  xsiz = 0;
35   int  ysiz = 0;
# Line 33 | Line 45 | int  labelht = 24;                     /* label height */
45  
46   int  checkthresh = 0;                   /* check threshold value */
47  
48 + char  StandardInput[] = "<stdin>";
49 + char  Command[] = "<Command>";
50 + char  Label[] = "<Label>";
51 +
52   char  *progname;
53  
54   struct {
# Line 40 | Line 56 | struct {
56          FILE  *fp;                      /* stream pointer */
57          int  xres, yres;                /* picture size */
58          int  xloc, yloc;                /* anchor point */
59 <        int  hasmin, hasmax;            /* has threshold values */
60 <        COLR  thmin, thmax;             /* thresholds */
59 >        int  flags;                     /* HASMIN, HASMAX */
60 >        double  thmin, thmax;           /* thresholds */
61   } input[MAXFILE];               /* our input files */
62  
63   int  nfile;                     /* number of files */
64  
65 + int  echoheader = 1;
66 + char  ourfmt[LPICFMT+1] = PICFMT;
67   int  wrongformat = 0;
68  
51 FILE  *popen(), *lblopen();
69  
70 + static gethfunc headline;
71 + static void compos(void);
72 + static int cmpcolr(COLR  c1, double lv2);
73 + static FILE * lblopen(char  *s, int  *xp, int  *yp);
74  
75 < tabputs(s)                      /* print line preceded by a tab */
76 < char  *s;
75 >
76 >
77 > static int
78 > headline(                       /* print line preceded by a tab */
79 >        char    *s,
80 >        void    *p
81 > )
82   {
83          char  fmt[32];
84  
85 <        if (isformat(s)) {
86 <                formatval(fmt, s);
87 <                wrongformat = strcmp(fmt, COLRFMT);
88 <        } else {
89 <                putc('\t', stdout);
85 >        if (isheadid(s))
86 >                return(0);
87 >        if (formatval(fmt, s)) {
88 >                if (globmatch(ourfmt, fmt)) {
89 >                        wrongformat = 0;
90 >                        strcpy(ourfmt, fmt);
91 >                } else
92 >                        wrongformat = 1;
93 >        } else if (echoheader) {
94 >                fputc('\t', stdout);
95                  fputs(s, stdout);
96          }
97 +        return(0);
98   }
99  
100  
101 < main(argc, argv)
102 < int  argc;
103 < char  *argv[];
101 > int
102 > main(
103 >        int  argc,
104 >        char  *argv[]
105 > )
106   {
107          int  ncolumns = 0;
108          int  autolabel = 0;
109 <        int  curcol = 0, curx = 0, cury = 0;
109 >        int  curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0;
110 >        int  xsgn, ysgn;
111          char  *thislabel;
112          int  an;
113 <
113 >        SET_DEFAULT_BINARY();
114 >        SET_FILE_BINARY(stdin);
115 >        SET_FILE_BINARY(stdout);
116          progname = argv[0];
117  
118          for (an = 1; an < argc && argv[an][0] == '-'; an++)
119                  switch (argv[an][1]) {
120 +                case 'h':
121 +                        echoheader = !echoheader;
122 +                        break;
123                  case 'x':
124 <                        xmax = xsiz = atoi(argv[++an]);
124 >                        xsiz = atoi(argv[++an]);
125                          break;
126                  case 'y':
127 <                        ymax = ysiz = atoi(argv[++an]);
127 >                        ysiz = atoi(argv[++an]);
128                          break;
129                  case 'b':
130                          setcolr(bgcolr, atof(argv[an+1]),
# Line 95 | Line 135 | char  *argv[];
135                  case 'a':
136                          ncolumns = atoi(argv[++an]);
137                          break;
138 +                case 's':
139 +                        spacing = atoi(argv[++an]);
140 +                        break;
141 +                case 'o':
142 +                        curx = x0 = atoi(argv[++an]);
143 +                        cury = atoi(argv[++an]);
144 +                        break;
145                  case 'l':
146                          switch (argv[an][2]) {
147                          case 'a':
# Line 116 | Line 163 | char  *argv[];
163                          goto userr;
164                  }
165   dofiles:
166 +        newheader("RADIANCE", stdout);
167 +        fputnow(stdout);
168          for (nfile = 0; an < argc; nfile++) {
169                  if (nfile >= MAXFILE)
170                          goto toomany;
171 <                if (autolabel)
172 <                        thislabel = argv[an];
173 <                else
174 <                        thislabel = NULL;
175 <                input[nfile].hasmin = input[nfile].hasmax = 0;
127 <                while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'))
171 >                thislabel = NULL;
172 >                input[nfile].flags = 0;
173 >                xsgn = ysgn = '-';
174 >                while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'
175 >                                || argv[an][0] == '=')) {
176                          switch (argv[an][1]) {
177                          case 't':
178                                  checkthresh = 1;
179                                  if (argv[an][0] == '-') {
180 <                                        input[nfile].hasmin = 1;
181 <                                        setcolr(input[nfile].thmin,
182 <                                                        atof(argv[an+1]),
183 <                                                        atof(argv[an+1]),
184 <                                                        atof(argv[an+1]));
185 <                                } else {
186 <                                        input[nfile].hasmax = 1;
187 <                                        setcolr(input[nfile].thmax,
140 <                                                        atof(argv[an+1]),
141 <                                                        atof(argv[an+1]),
142 <                                                        atof(argv[an+1]));
143 <                                }
144 <                                an += 2;
180 >                                        input[nfile].flags |= HASMIN;
181 >                                        input[nfile].thmin = atof(argv[an+1]);
182 >                                } else if (argv[an][0] == '+') {
183 >                                        input[nfile].flags |= HASMAX;
184 >                                        input[nfile].thmax = atof(argv[an+1]);
185 >                                } else
186 >                                        goto userr;
187 >                                an++;
188                                  break;
189                          case 'l':
190                                  if (strcmp(argv[an], "-l"))
191                                          goto userr;
192 <                                thislabel = argv[an+1];
150 <                                an += 2;
192 >                                thislabel = argv[++an];
193                                  break;
194 +                        case '+':
195 +                        case '-':
196 +                        case '0':
197 +                                if (argv[an][0] != '=')
198 +                                        goto userr;
199 +                                xsgn = argv[an][1];
200 +                                ysgn = argv[an][2];
201 +                                if (ysgn != '+' && ysgn != '-' && ysgn != '0')
202 +                                        goto userr;
203 +                                break;
204                          case '\0':
205                                  if (argv[an][0] == '-')
206                                          goto getfile;
# Line 156 | Line 208 | dofiles:
208                          default:
209                                  goto userr;
210                          }
211 +                        an++;
212 +                }
213   getfile:
214                  if (argc-an < (ncolumns ? 1 : 3))
215                          goto userr;
216 +                if (autolabel && thislabel == NULL)
217 +                        thislabel = argv[an];
218                  if (!strcmp(argv[an], "-")) {
219 <                        input[nfile].name = "<stdin>";
219 >                        input[nfile].name = StandardInput;
220                          input[nfile].fp = stdin;
221                  } else {
222                          if (argv[an][0] == '!') {
223 <                                input[nfile].name = "<Command>";
223 >                                input[nfile].name = Command;
224                                  input[nfile].fp = popen(argv[an]+1, "r");
225                          } else {
226                                  input[nfile].name = argv[an];
# Line 177 | Line 233 | getfile:
233                  }
234                  an++;
235                                                  /* get header */
236 <                printf("%s:\n", input[nfile].name);
237 <                getheader(input[nfile].fp, tabputs, NULL);
236 >                if (echoheader)
237 >                        printf("%s:\n", input[nfile].name);
238 >                getheader(input[nfile].fp, headline, NULL);
239                  if (wrongformat) {
240 <                        fprintf(stderr, "%s: not a Radiance picture\n",
240 >                        fprintf(stderr, "%s: incompatible input format\n",
241                                          input[nfile].name);
242                          quit(1);
243                  }
# Line 191 | Line 248 | getfile:
248                                          input[nfile].name);
249                          quit(1);
250                  }
251 <                if (ncolumns > 0) {
252 <                        if (curcol >= ncolumns) {
253 <                                cury = ymax;
254 <                                curx = 0;
251 >                if (ncolumns) {
252 >                        if (curcol >= abs(ncolumns)) {
253 >                                cury = ymax + spacing;
254 >                                curx = x0;
255                                  curcol = 0;
256                          }
257                          input[nfile].xloc = curx;
258                          input[nfile].yloc = cury;
259 <                        curx += input[nfile].xres;
259 >                        curx += input[nfile].xres + spacing;
260                          curcol++;
261                  } else {
262                          input[nfile].xloc = atoi(argv[an++]);
263 +                        if (xsgn == '+')
264 +                                input[nfile].xloc -= input[nfile].xres;
265 +                        else if (xsgn == '0')
266 +                                input[nfile].xloc -= input[nfile].xres/2;
267                          input[nfile].yloc = atoi(argv[an++]);
268 +                        if (ysgn == '+')
269 +                                input[nfile].yloc -= input[nfile].yres;
270 +                        else if (ysgn == '0')
271 +                                input[nfile].yloc -= input[nfile].yres/2;
272                  }
273                  if (input[nfile].xloc < xmin)
274                          xmin = input[nfile].xloc;
# Line 216 | Line 281 | getfile:
281                  if (thislabel != NULL) {
282                          if (++nfile >= MAXFILE)
283                                  goto toomany;
284 <                        input[nfile].name = "<Label>";
285 <                        input[nfile].hasmin = input[nfile].hasmax = 0;
284 >                        input[nfile].name = Label;
285 >                        input[nfile].flags = 0;
286 >                        input[nfile].xres = input[nfile-1].xres;
287 >                        input[nfile].yres = labelht;
288                          if ((input[nfile].fp = lblopen(thislabel,
289                                          &input[nfile].xres,
290                                          &input[nfile].yres)) == NULL)
# Line 229 | Line 296 | getfile:
296          }
297          if (xsiz <= 0)
298                  xsiz = xmax;
299 +        else if (xsiz > xmax)
300 +                xmax = xsiz;
301          if (ysiz <= 0)
302                  ysiz = ymax;
303 +        else if (ysiz > ymax)
304 +                ymax = ysiz;
305 +        if (ncolumns < 0) {             /* reverse rows if requested */
306 +                int     i = nfile;
307 +                while (i--)
308 +                        input[i].yloc = ymax - input[i].yres - input[i].yloc;
309 +        }
310                                          /* add new header info. */
311          printargs(argc, argv, stdout);
312 <        fputformat(COLRFMT, stdout);
312 >        if (strcmp(ourfmt, PICFMT))
313 >                fputformat(ourfmt, stdout);     /* print format if known */
314          putchar('\n');
315          fprtresolu(xsiz, ysiz, stdout);
316  
# Line 241 | Line 318 | getfile:
318          
319          quit(0);
320   userr:
321 <        fprintf(stderr, "Usage: %s [-x xr][-y yr][-b r g b][-a n][-la][-lh h] ",
321 >        fprintf(stderr,
322 >        "Usage: %s [-h][-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ",
323                          progname);
324 <        fprintf(stderr, "[-t min1][+t max1][-l lab] pic1 x1 y1 ..\n");
324 >        fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n");
325          quit(1);
326   toomany:
327          fprintf(stderr, "%s: only %d files and labels allowed\n",
# Line 252 | Line 330 | toomany:
330   labelerr:
331          fprintf(stderr, "%s: error opening label\n", progname);
332          quit(1);
333 +        return 1; /* pro forma return */
334   }
335  
336  
337 < compos()                                /* composite pictures */
337 > static void
338 > compos(void)                            /* composite pictures */
339   {
340          COLR  *scanin, *scanout;
341          int  y;
# Line 292 | Line 372 | compos()                               /* composite pictures */
372                                  if (x > xsiz)
373                                          x = xsiz;
374                                  for (x--; x >= 0 && x >= input[i].xloc; x--) {
375 <                                        if (input[i].hasmin &&
375 >                                        if (input[i].flags & HASMIN &&
376                                          cmpcolr(scanin[x], input[i].thmin) <= 0)
377                                                  continue;
378 <                                        if (input[i].hasmax &&
378 >                                        if (input[i].flags & HASMAX &&
379                                          cmpcolr(scanin[x], input[i].thmax) >= 0)
380                                                  continue;
381                                          copycolr(scanout[x], scanin[x]);
# Line 309 | Line 389 | compos()                               /* composite pictures */
389                          quit(1);
390                  }
391          }
392 +                                        /* read remainders from streams */
393 +        for (i = 0; i < nfile; i++)
394 +                if (input[i].name[0] == '<')
395 +                        while (getc(input[i].fp) != EOF)
396 +                                ;
397          return;
398   memerr:
399          perror(progname);
# Line 316 | Line 401 | memerr:
401   }
402  
403  
404 < int
405 < cmpcolr(c1, c2)                 /* compare two colr's (improvisation) */
406 < register COLR  c1, c2;
404 > static int
405 > cmpcolr(                        /* compare COLR to luminance */
406 >        register COLR  c1,
407 >        double lv2
408 > )
409   {
410 <        register int  i, j;
411 <
412 <        j = 4;                          /* check exponents first! */
413 <        while (j--)
414 <                if (i = c1[j] - c2[j])
415 <                        return(i);
410 >        double  lv1 = .0;
411 >        
412 >        if (c1[EXP])
413 >                lv1 = ldexp((double)normbright(c1), (int)c1[EXP]-(COLXS+8));
414 >        if (lv1 < lv2) return(-1);
415 >        if (lv1 > lv2) return(1);
416          return(0);
417   }
418  
419  
420 < FILE *
421 < lblopen(s, xp, yp)              /* open pipe to label generator */
422 < char  *s;
423 < int  *xp, *yp;
420 > static FILE *
421 > lblopen(                /* open pipe to label generator */
422 >        char  *s,
423 >        int  *xp,
424 >        int  *yp
425 > )
426   {
427 <        char  com[128];
427 >        char  com[PATH_MAX];
428          FILE  *fp;
429  
430 <        sprintf(com, "psign -s -.2 -h %d '%.30s'", labelht, s);
430 >        sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s);
431          if ((fp = popen(com, "r")) == NULL)
432                  return(NULL);
433          if (checkheader(fp, COLRFMT, NULL) < 0)
# Line 352 | Line 441 | err:
441   }
442  
443  
444 + void
445   quit(code)              /* exit gracefully */
446   int  code;
447   {
448 +        register int  i;
449 +                                /* close input files */
450 +        for (i = 0; i < nfile; i++)
451 +                if (input[i].name == Command || input[i].name == Label)
452 +                        pclose(input[i].fp);
453 +                else
454 +                        fclose(input[i].fp);
455          exit(code);
456   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines