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.1 by greg, Thu Feb 2 10:49:23 1989 UTC vs.
Revision 2.42 by greg, Sat Jun 7 05:09:46 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1987 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 "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 + #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
21 + #undef getc
22 + #define getc    getc_unlocked
23 + #endif
24  
25 < #define  MAXFILE        16
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 28 | Line 38 | int  ymax = 0;
38  
39   COLR  bgcolr = BLKCOLR;                 /* background color */
40  
41 + 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 name */
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 < tabputs(s)                      /* print line preceded by a tab */
66 < char  *s;
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 >
73 >
74 > static int
75 > headline(                       /* print line preceded by a tab */
76 >        char    *s,
77 >        void    *p
78 > )
79   {
80 <        putc('\t', stdout);
81 <        fputs(s, stdout);
80 >        char  fmt[MAXFMTLEN];
81 >
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 <        double  atof();
106 >        int  ncolumns = 0;
107 >        int  autolabel = 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 75 | Line 133 | char  *argv[];
133                                          atof(argv[an+3]));
134                          an += 3;
135                          break;
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':
149 +                                autolabel++;
150 +                                break;
151 +                        case 'h':
152 +                                labelht = atoi(argv[++an]);
153 +                                break;
154 +                        case '\0':
155 +                                goto dofiles;
156 +                        default:
157 +                                goto userr;
158 +                        }
159 +                        break;
160                  case '\0':
161                  case 't':
162                          goto dofiles;
# Line 82 | 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 <                        fprintf(stderr, "%s: too many files\n", progname);
172 <                        quit(1);
173 <                }
174 <                input[nfile].hasmin = input[nfile].hasmax = 0;
175 <                while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+'))
170 >                if (nfile >= MAXFILE)
171 >                        goto toomany;
172 >                thislabel = NULL;
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,
104 <                                                        atof(argv[an+1]),
105 <                                                        atof(argv[an+1]),
106 <                                                        atof(argv[an+1]));
107 <                                }
108 <                                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];
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;
208 <                        /* fall through */
208 >                                goto userr;
209                          default:
210                                  goto userr;
211                          }
212 +                        an++;
213 +                }
214   getfile:
215 <                if (argc-an < 3)
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 <                        input[nfile].name = argv[an];
224 <                        if ((input[nfile].fp = fopen(argv[an], "r")) == NULL) {
225 <                                fprintf(stderr, "%s: cannot open\n", argv[an]);
223 >                        if (argv[an][0] == '!') {
224 >                                input[nfile].name = Command;
225 >                                input[nfile].fp = popen(argv[an]+1, "r");
226 >                        } else {
227 >                                input[nfile].name = argv[an];
228 >                                input[nfile].fp = fopen(argv[an], "r");
229 >                        }
230 >                        if (input[nfile].fp == NULL) {
231 >                                perror(argv[an]);
232                                  quit(1);
233                          }
234                  }
235                  an++;
236                                                  /* get header */
237 <                printf("%s:\n", input[nfile].name);
238 <                getheader(input[nfile].fp, tabputs);
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: incompatible input format\n",
248 >                                        input[nfile].name);
249 >                        quit(1);
250 >                }
251                                                  /* get picture size */
252 <                if (fscanf(input[nfile].fp, "-Y %d +X %d\n",
253 <                                &input[nfile].yres, &input[nfile].xres) != 2) {
252 >                if (fgetresolu(&input[nfile].xres, &input[nfile].yres,
253 >                                input[nfile].fp) < 0) {
254                          fprintf(stderr, "%s: bad picture size\n",
255                                          input[nfile].name);
256                          quit(1);
257                  }
258 <                input[nfile].xloc = atoi(argv[an++]);
259 <                input[nfile].yloc = atoi(argv[an++]);
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 + 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;
282                  if (input[nfile].yloc < ymin)
# Line 148 | Line 285 | getfile:
285                          xmax = input[nfile].xloc+input[nfile].xres;
286                  if (input[nfile].yloc+input[nfile].yres > ymax)
287                          ymax = input[nfile].yloc+input[nfile].yres;
288 +                if (thislabel != NULL) {
289 +                        if (++nfile >= MAXFILE)
290 +                                goto toomany;
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,
296 +                                        &input[nfile].xres,
297 +                                        &input[nfile].yres)) == NULL)
298 +                                goto labelerr;
299 +                        input[nfile].xloc = input[nfile-1].xloc;
300 +                        input[nfile].yloc = input[nfile-1].yloc +
301 +                                        input[nfile-1].yres-input[nfile].yres;
302 +                }
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 <        printf("\n-Y %d +X %d\n", ysiz, xsiz);
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  
326          compos();
327          
328          quit(0);
329   userr:
330 <        fprintf(stderr, "Usage: %s [-x xres][-y yres][-b r g b] ", progname);
331 <        fprintf(stderr, "[-t min1][+t max1] file1 x1 y1 ..\n");
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][=SS] pic1 x1 y1 ..\n");
334          quit(1);
335 + toomany:
336 +        fprintf(stderr, "%s: only %d files and labels allowed\n",
337 +                        progname, MAXFILE);
338 +        quit(1);
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;
351          register int  x, i;
352  
353 <        scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR)) - xmin;
177 <        scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR)) - xmin;
353 >        scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR));
354          if (scanin == NULL)
355                  goto memerr;
356          scanin -= xmin;
# Line 193 | Line 369 | compos()                               /* composite pictures */
369                                  continue;
370                          if (freadcolrs(scanin+input[i].xloc,
371                                          input[i].xres, input[i].fp) < 0) {
372 <                                fprintf(stderr, "%s: read error\n",
373 <                                                input[i].name);
372 >                                fprintf(stderr, "%s: read error (y==%d)\n",
373 >                                                input[i].name,
374 >                                                y-input[i].yloc);
375                                  quit(1);
376                          }
377                          if (y >= ysiz)
# Line 204 | 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 217 | Line 394 | compos()                               /* composite pictures */
394                  if (y >= ysiz)
395                          continue;
396                  if (fwritecolrs(scanout, xsiz, stdout) < 0) {
397 <                        fprintf(stderr, "%s: write error\n", progname);
397 >                        perror(progname);
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 <        fprintf(stderr, "%s: out of memory\n", progname);
408 >        perror(progname);
409          quit(1);
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 + static FILE *
430 + lblopen(                /* open pipe to label generator */
431 +        char  *s,
432 +        int  *xp,
433 +        int  *yp
434 + )
435 + {
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);
440 +        if ((fp = popen(com, "r")) == NULL)
441 +                return(NULL);
442 +        if (checkheader(fp, COLRFMT, NULL) < 0)
443 +                goto err;
444 +        if (fgetresolu(xp, yp, fp) < 0)
445 +                goto err;
446 +        return(fp);
447 + err:
448 +        pclose(fp);
449 +        return(NULL);
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