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.8 by greg, Mon Jul 13 15:04:15 1992 UTC vs.
Revision 2.42 by greg, Sat Jun 7 05:09:46 2025 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   *
7   *     6/30/87
8   */
9  
10 < #include  <stdio.h>
10 > #include  <math.h>
11  
12 < #include  "color.h"
12 > #include "copyright.h"
13  
14 + #include  "rtio.h"
15 + #include  "platform.h"
16 + #include  "rterror.h"
17 + #include  "color.h"
18   #include  "resolu.h"
19  
20 < #define  MAXFILE        64
20 > #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
21 > #undef getc
22 > #define getc    getc_unlocked
23 > #endif
24  
25 + #define  MAXFILE        1024
26 +
27 + #define  HASMIN         1
28 + #define  HASMAX         2
29 +
30                                          /* output picture size */
31   int  xsiz = 0;
32   int  ysiz = 0;
# Line 33 | Line 42 | int  labelht = 24;                     /* label height */
42  
43   int  checkthresh = 0;                   /* check threshold value */
44  
45 < char  *progname;
45 > char  StandardInput[] = "<stdin>";
46 > char  Command[] = "<Command>";
47 > char  Label[] = "<Label>";
48  
49   struct {
50          char  *name;                    /* file or command name */
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[MAXFMTLEN] = PICFMT;
62   int  wrongformat = 0;
63 + double  common_expos = 1.;
64  
65 < FILE  *popen(), *lblopen();
65 > double  this_expos;
66  
67 + static gethfunc headline;
68 + static void compos(void);
69 + static int cmpcolr(COLR  c1, double lv2);
70 + static FILE * lblopen(char  *s, int  *xp, int  *yp);
71  
72 < tabputs(s)                      /* print line preceded by a tab */
73 < char  *s;
72 >
73 >
74 > static int
75 > headline(                       /* print line preceded by a tab */
76 >        char    *s,
77 >        void    *p
78 > )
79   {
80 <        char  fmt[32];
80 >        char  fmt[MAXFMTLEN];
81  
82 <        if (isformat(s)) {
83 <                formatval(fmt, s);
84 <                wrongformat = strcmp(fmt, COLRFMT);
85 <        } else {
86 <                putc('\t', stdout);
82 >        if (isheadid(s))
83 >                return(0);
84 >        if (isexpos(s))
85 >                this_expos *= exposval(s);
86 >        if (formatval(fmt, s)) {
87 >                if (globmatch(ourfmt, fmt)) {
88 >                        wrongformat = 0;
89 >                        strcpy(ourfmt, fmt);
90 >                } else
91 >                        wrongformat = 1;
92 >        } else if (echoheader) {
93 >                fputc('\t', stdout);
94                  fputs(s, stdout);
95          }
96 +        return(0);
97   }
98  
99  
100 < main(argc, argv)
101 < int  argc;
102 < char  *argv[];
100 > int
101 > main(
102 >        int  argc,
103 >        char  *argv[]
104 > )
105   {
106          int  ncolumns = 0;
107          int  autolabel = 0;
108 <        int  curcol = 0, curx = 0, cury = 0;
108 >        int  curcol = 0, x0 = 0, curx = 0, cury = 0, spacing = 0;
109 >        int  xsgn, ysgn;
110          char  *thislabel;
111          int  an;
112  
113 <        progname = argv[0];
113 >        SET_DEFAULT_BINARY();
114 >        SET_FILE_BINARY(stdin);
115 >        SET_FILE_BINARY(stdout);
116  
117 +        fixargv0(argv[0]);              /* sets global progname */
118 +
119          for (an = 1; an < argc && argv[an][0] == '-'; an++)
120                  switch (argv[an][1]) {
121 +                case 'h':
122 +                        echoheader = !echoheader;
123 +                        break;
124                  case 'x':
125 <                        xmax = xsiz = atoi(argv[++an]);
125 >                        xsiz = atoi(argv[++an]);
126                          break;
127                  case 'y':
128 <                        ymax = ysiz = atoi(argv[++an]);
128 >                        ysiz = atoi(argv[++an]);
129                          break;
130                  case 'b':
131                          setcolr(bgcolr, atof(argv[an+1]),
# Line 95 | Line 136 | char  *argv[];
136                  case 'a':
137                          ncolumns = atoi(argv[++an]);
138                          break;
139 +                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                  case 'l':
147                          switch (argv[an][2]) {
148                          case 'a':
# Line 116 | Line 164 | char  *argv[];
164                          goto userr;
165                  }
166   dofiles:
167 +        newheader("RADIANCE", stdout);
168 +        fputnow(stdout);
169          for (nfile = 0; an < argc; nfile++) {
170                  if (nfile >= MAXFILE)
171                          goto toomany;
172                  thislabel = NULL;
173 <                input[nfile].hasmin = input[nfile].hasmax = 0;
174 <                while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'))
173 >                input[nfile].flags = 0;
174 >                xsgn = ysgn = '-';
175 >                while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'
176 >                                || argv[an][0] == '=')) {
177                          switch (argv[an][1]) {
178                          case 't':
179                                  checkthresh = 1;
180                                  if (argv[an][0] == '-') {
181 <                                        input[nfile].hasmin = 1;
182 <                                        setcolr(input[nfile].thmin,
183 <                                                        atof(argv[an+1]),
184 <                                                        atof(argv[an+1]),
185 <                                                        atof(argv[an+1]));
186 <                                } else {
187 <                                        input[nfile].hasmax = 1;
188 <                                        setcolr(input[nfile].thmax,
137 <                                                        atof(argv[an+1]),
138 <                                                        atof(argv[an+1]),
139 <                                                        atof(argv[an+1]));
140 <                                }
141 <                                an += 2;
181 >                                        input[nfile].flags |= HASMIN;
182 >                                        input[nfile].thmin = atof(argv[an+1]);
183 >                                } else if (argv[an][0] == '+') {
184 >                                        input[nfile].flags |= HASMAX;
185 >                                        input[nfile].thmax = atof(argv[an+1]);
186 >                                } else
187 >                                        goto userr;
188 >                                an++;
189                                  break;
190                          case 'l':
191                                  if (strcmp(argv[an], "-l"))
192                                          goto userr;
193 <                                thislabel = argv[an+1];
147 <                                an += 2;
193 >                                thislabel = argv[++an];
194                                  break;
195 +                        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                          case '\0':
206                                  if (argv[an][0] == '-')
207                                          goto getfile;
# Line 153 | Line 209 | dofiles:
209                          default:
210                                  goto userr;
211                          }
212 +                        an++;
213 +                }
214   getfile:
215                  if (argc-an < (ncolumns ? 1 : 3))
216                          goto userr;
217                  if (autolabel && thislabel == NULL)
218                          thislabel = argv[an];
219                  if (!strcmp(argv[an], "-")) {
220 <                        input[nfile].name = "<stdin>";
220 >                        input[nfile].name = StandardInput;
221                          input[nfile].fp = stdin;
222                  } else {
223                          if (argv[an][0] == '!') {
224 <                                input[nfile].name = "<Command>";
224 >                                input[nfile].name = Command;
225                                  input[nfile].fp = popen(argv[an]+1, "r");
226                          } else {
227                                  input[nfile].name = argv[an];
# Line 176 | Line 234 | getfile:
234                  }
235                  an++;
236                                                  /* get header */
237 <                printf("%s:\n", input[nfile].name);
238 <                getheader(input[nfile].fp, tabputs, NULL);
237 >                if (echoheader)
238 >                        printf("%s:\n", input[nfile].name);
239 >                this_expos = 1;
240 >                getheader(input[nfile].fp, headline, NULL);
241 >                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                  if (wrongformat) {
247 <                        fprintf(stderr, "%s: not a Radiance picture\n",
247 >                        fprintf(stderr, "%s: incompatible input format\n",
248                                          input[nfile].name);
249                          quit(1);
250                  }
# Line 190 | Line 255 | getfile:
255                                          input[nfile].name);
256                          quit(1);
257                  }
258 <                if (ncolumns > 0) {
259 <                        if (curcol >= ncolumns) {
260 <                                cury = ymax;
261 <                                curx = 0;
258 >                if (ncolumns) {
259 >                        if (curcol >= abs(ncolumns)) {
260 >                                cury = ymax + spacing;
261 >                                curx = x0;
262                                  curcol = 0;
263                          }
264                          input[nfile].xloc = curx;
265                          input[nfile].yloc = cury;
266 <                        curx += input[nfile].xres;
266 >                        curx += input[nfile].xres + spacing;
267                          curcol++;
268                  } else {
269                          input[nfile].xloc = atoi(argv[an++]);
270 +                        if (xsgn == '+')
271 +                                input[nfile].xloc -= input[nfile].xres;
272 +                        else if (xsgn == '0')
273 +                                input[nfile].xloc -= input[nfile].xres/2;
274                          input[nfile].yloc = atoi(argv[an++]);
275 +                        if (ysgn == '+')
276 +                                input[nfile].yloc -= input[nfile].yres;
277 +                        else if (ysgn == '0')
278 +                                input[nfile].yloc -= input[nfile].yres/2;
279                  }
280                  if (input[nfile].xloc < xmin)
281                          xmin = input[nfile].xloc;
# Line 215 | Line 288 | getfile:
288                  if (thislabel != NULL) {
289                          if (++nfile >= MAXFILE)
290                                  goto toomany;
291 <                        input[nfile].name = "<Label>";
292 <                        input[nfile].hasmin = input[nfile].hasmax = 0;
291 >                        input[nfile].name = Label;
292 >                        input[nfile].flags = 0;
293                          input[nfile].xres = input[nfile-1].xres;
294                          input[nfile].yres = labelht;
295                          if ((input[nfile].fp = lblopen(thislabel,
# Line 230 | Line 303 | getfile:
303          }
304          if (xsiz <= 0)
305                  xsiz = xmax;
306 +        else if (xsiz > xmax)
307 +                xmax = xsiz;
308          if (ysiz <= 0)
309                  ysiz = ymax;
310 +        else if (ysiz > ymax)
311 +                ymax = ysiz;
312 +        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                                          /* add new header info. */
318          printargs(argc, argv, stdout);
319 <        fputformat(COLRFMT, stdout);
319 >        if (common_expos > 0)           /* print exposure if shared */
320 >                fputexpos(common_expos, stdout);
321 >        if (strcmp(ourfmt, PICFMT))
322 >                fputformat(ourfmt, stdout);     /* print format if known */
323          putchar('\n');
324          fprtresolu(xsiz, ysiz, stdout);
325  
# Line 242 | Line 327 | getfile:
327          
328          quit(0);
329   userr:
330 <        fprintf(stderr, "Usage: %s [-x xr][-y yr][-b r g b][-a n][-la][-lh h] ",
330 >        fprintf(stderr,
331 >        "Usage: %s [-h][-x xr][-y yr][-b r g b][-a n][-s p][-o x0 y0][-la][-lh h] ",
332                          progname);
333 <        fprintf(stderr, "[-t min1][+t max1][-l lab] pic1 x1 y1 ..\n");
333 >        fprintf(stderr, "[-t min1][+t max1][-l lab][=SS] pic1 x1 y1 ..\n");
334          quit(1);
335   toomany:
336          fprintf(stderr, "%s: only %d files and labels allowed\n",
# Line 253 | Line 339 | toomany:
339   labelerr:
340          fprintf(stderr, "%s: error opening label\n", progname);
341          quit(1);
342 +        return 1; /* pro forma return */
343   }
344  
345  
346 < compos()                                /* composite pictures */
346 > static void
347 > compos(void)                            /* composite pictures */
348   {
349          COLR  *scanin, *scanout;
350          int  y;
# Line 293 | Line 381 | compos()                               /* composite pictures */
381                                  if (x > xsiz)
382                                          x = xsiz;
383                                  for (x--; x >= 0 && x >= input[i].xloc; x--) {
384 <                                        if (input[i].hasmin &&
384 >                                        if (input[i].flags & HASMIN &&
385                                          cmpcolr(scanin[x], input[i].thmin) <= 0)
386                                                  continue;
387 <                                        if (input[i].hasmax &&
387 >                                        if (input[i].flags & HASMAX &&
388                                          cmpcolr(scanin[x], input[i].thmax) >= 0)
389                                                  continue;
390                                          copycolr(scanout[x], scanin[x]);
# Line 310 | Line 398 | compos()                               /* composite pictures */
398                          quit(1);
399                  }
400          }
401 +                                        /* 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          return;
407   memerr:
408          perror(progname);
# Line 317 | Line 410 | memerr:
410   }
411  
412  
413 < int
414 < cmpcolr(c1, c2)                 /* compare two colr's (improvisation) */
415 < register COLR  c1, c2;
413 > static int
414 > cmpcolr(                        /* compare COLR to luminance */
415 >        register COLR  c1,
416 >        double lv2
417 > )
418   {
419 <        register int  i, j;
420 <
421 <        j = 4;                          /* check exponents first! */
422 <        while (j--)
423 <                if (i = c1[j] - c2[j])
424 <                        return(i);
419 >        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          return(0);
426   }
427  
428  
429 < FILE *
430 < lblopen(s, xp, yp)              /* open pipe to label generator */
431 < char  *s;
432 < int  *xp, *yp;
429 > static FILE *
430 > lblopen(                /* open pipe to label generator */
431 >        char  *s,
432 >        int  *xp,
433 >        int  *yp
434 > )
435   {
436 <        char  com[128];
436 >        char  com[PATH_MAX];
437          FILE  *fp;
438  
439          sprintf(com, "psign -s -.15 -a 2 -x %d -y %d '%.90s'", *xp, *yp, s);
# Line 353 | Line 450 | err:
450   }
451  
452  
453 + void
454   quit(code)              /* exit gracefully */
455   int  code;
456   {
457 +        register int  i;
458 +                                /* close input files */
459 +        for (i = 0; i < nfile; i++)
460 +                if (input[i].name == Command || input[i].name == Label)
461 +                        pclose(input[i].fp);
462 +                else
463 +                        fclose(input[i].fp);
464          exit(code);
465   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines