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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines