| 1 | greg | 1.7 | /* Copyright (c) 1991 Regents of the University of California */ | 
| 2 | greg | 1.1 |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | *  pcompos.c - program to composite pictures. | 
| 9 |  |  | * | 
| 10 |  |  | *     6/30/87 | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 |  |  | #include  <stdio.h> | 
| 14 |  |  |  | 
| 15 |  |  | #include  "color.h" | 
| 16 |  |  |  | 
| 17 | greg | 1.10 | #include  "resolu.h" | 
| 18 | greg | 1.1 |  | 
| 19 | greg | 1.5 | #define  MAXFILE        32 | 
| 20 | greg | 1.1 |  | 
| 21 |  |  | /* output picture size */ | 
| 22 |  |  | int  xsiz = 0; | 
| 23 |  |  | int  ysiz = 0; | 
| 24 |  |  | /* input dimensions */ | 
| 25 |  |  | int  xmin = 0; | 
| 26 |  |  | int  ymin = 0; | 
| 27 |  |  | int  xmax = 0; | 
| 28 |  |  | int  ymax = 0; | 
| 29 |  |  |  | 
| 30 |  |  | COLR  bgcolr = BLKCOLR;                 /* background color */ | 
| 31 |  |  |  | 
| 32 | greg | 1.9 | int  labelht = 24;                      /* label height */ | 
| 33 |  |  |  | 
| 34 | greg | 1.1 | int  checkthresh = 0;                   /* check threshold value */ | 
| 35 |  |  |  | 
| 36 |  |  | char  *progname; | 
| 37 |  |  |  | 
| 38 |  |  | struct { | 
| 39 | greg | 1.7 | char  *name;                    /* file or command name */ | 
| 40 | greg | 1.1 | FILE  *fp;                      /* stream pointer */ | 
| 41 |  |  | int  xres, yres;                /* picture size */ | 
| 42 |  |  | int  xloc, yloc;                /* anchor point */ | 
| 43 |  |  | int  hasmin, hasmax;            /* has threshold values */ | 
| 44 |  |  | COLR  thmin, thmax;             /* thresholds */ | 
| 45 |  |  | } input[MAXFILE];               /* our input files */ | 
| 46 |  |  |  | 
| 47 |  |  | int  nfile;                     /* number of files */ | 
| 48 |  |  |  | 
| 49 | greg | 1.6 | int  wrongformat = 0; | 
| 50 | greg | 1.1 |  | 
| 51 | greg | 1.9 | FILE  *popen(), *lblopen(); | 
| 52 | greg | 1.6 |  | 
| 53 | greg | 1.7 |  | 
| 54 | greg | 1.1 | tabputs(s)                      /* print line preceded by a tab */ | 
| 55 |  |  | char  *s; | 
| 56 |  |  | { | 
| 57 | greg | 1.6 | char  fmt[32]; | 
| 58 |  |  |  | 
| 59 |  |  | if (isformat(s)) { | 
| 60 |  |  | formatval(fmt, s); | 
| 61 |  |  | wrongformat = strcmp(fmt, COLRFMT); | 
| 62 |  |  | } else { | 
| 63 |  |  | putc('\t', stdout); | 
| 64 |  |  | fputs(s, stdout); | 
| 65 |  |  | } | 
| 66 | greg | 1.1 | } | 
| 67 |  |  |  | 
| 68 |  |  |  | 
| 69 |  |  | main(argc, argv) | 
| 70 |  |  | int  argc; | 
| 71 |  |  | char  *argv[]; | 
| 72 |  |  | { | 
| 73 |  |  | double  atof(); | 
| 74 | greg | 1.7 | int  ncolumns = 0; | 
| 75 | greg | 1.9 | int  autolabel = 0; | 
| 76 | greg | 1.7 | int  curcol = 0, curx = 0, cury = 0; | 
| 77 | greg | 1.9 | char  *thislabel; | 
| 78 | greg | 1.1 | int  an; | 
| 79 |  |  |  | 
| 80 |  |  | progname = argv[0]; | 
| 81 |  |  |  | 
| 82 |  |  | for (an = 1; an < argc && argv[an][0] == '-'; an++) | 
| 83 |  |  | switch (argv[an][1]) { | 
| 84 |  |  | case 'x': | 
| 85 |  |  | xmax = xsiz = atoi(argv[++an]); | 
| 86 |  |  | break; | 
| 87 |  |  | case 'y': | 
| 88 |  |  | ymax = ysiz = atoi(argv[++an]); | 
| 89 |  |  | break; | 
| 90 |  |  | case 'b': | 
| 91 |  |  | setcolr(bgcolr, atof(argv[an+1]), | 
| 92 |  |  | atof(argv[an+2]), | 
| 93 |  |  | atof(argv[an+3])); | 
| 94 |  |  | an += 3; | 
| 95 |  |  | break; | 
| 96 | greg | 1.7 | case 'a': | 
| 97 |  |  | ncolumns = atoi(argv[++an]); | 
| 98 |  |  | break; | 
| 99 |  |  | case 'l': | 
| 100 | greg | 1.9 | switch (argv[an][2]) { | 
| 101 |  |  | case 'a': | 
| 102 |  |  | autolabel++; | 
| 103 |  |  | break; | 
| 104 |  |  | case 'h': | 
| 105 |  |  | labelht = atoi(argv[++an]); | 
| 106 |  |  | break; | 
| 107 |  |  | case '\0': | 
| 108 |  |  | goto dofiles; | 
| 109 |  |  | default: | 
| 110 |  |  | goto userr; | 
| 111 |  |  | } | 
| 112 | greg | 1.7 | break; | 
| 113 | greg | 1.1 | case '\0': | 
| 114 |  |  | case 't': | 
| 115 |  |  | goto dofiles; | 
| 116 |  |  | default: | 
| 117 |  |  | goto userr; | 
| 118 |  |  | } | 
| 119 |  |  | dofiles: | 
| 120 |  |  | for (nfile = 0; an < argc; nfile++) { | 
| 121 | greg | 1.9 | if (nfile >= MAXFILE) | 
| 122 |  |  | goto toomany; | 
| 123 |  |  | if (autolabel) | 
| 124 |  |  | thislabel = argv[an]; | 
| 125 |  |  | else | 
| 126 |  |  | thislabel = NULL; | 
| 127 | greg | 1.1 | input[nfile].hasmin = input[nfile].hasmax = 0; | 
| 128 |  |  | while (an < argc && (argv[an][0] == '-' || argv[an][0] == '+')) | 
| 129 |  |  | switch (argv[an][1]) { | 
| 130 |  |  | case 't': | 
| 131 |  |  | checkthresh = 1; | 
| 132 |  |  | if (argv[an][0] == '-') { | 
| 133 |  |  | input[nfile].hasmin = 1; | 
| 134 |  |  | setcolr(input[nfile].thmin, | 
| 135 |  |  | atof(argv[an+1]), | 
| 136 |  |  | atof(argv[an+1]), | 
| 137 |  |  | atof(argv[an+1])); | 
| 138 |  |  | } else { | 
| 139 |  |  | input[nfile].hasmax = 1; | 
| 140 |  |  | setcolr(input[nfile].thmax, | 
| 141 |  |  | atof(argv[an+1]), | 
| 142 |  |  | atof(argv[an+1]), | 
| 143 |  |  | atof(argv[an+1])); | 
| 144 |  |  | } | 
| 145 |  |  | an += 2; | 
| 146 |  |  | break; | 
| 147 | greg | 1.9 | case 'l': | 
| 148 |  |  | if (strcmp(argv[an], "-l")) | 
| 149 |  |  | goto userr; | 
| 150 |  |  | thislabel = argv[++an]; | 
| 151 |  |  | break; | 
| 152 | greg | 1.1 | case '\0': | 
| 153 |  |  | if (argv[an][0] == '-') | 
| 154 |  |  | goto getfile; | 
| 155 | greg | 1.9 | goto userr; | 
| 156 | greg | 1.1 | default: | 
| 157 |  |  | goto userr; | 
| 158 |  |  | } | 
| 159 |  |  | getfile: | 
| 160 | greg | 1.7 | if (argc-an < (ncolumns ? 1 : 3)) | 
| 161 | greg | 1.1 | goto userr; | 
| 162 |  |  | if (!strcmp(argv[an], "-")) { | 
| 163 |  |  | input[nfile].name = "<stdin>"; | 
| 164 |  |  | input[nfile].fp = stdin; | 
| 165 |  |  | } else { | 
| 166 |  |  | input[nfile].name = argv[an]; | 
| 167 | greg | 1.7 | if ((input[nfile].fp = argv[an][0] == '!' ? | 
| 168 |  |  | popen(argv[an]+1, "r") : | 
| 169 |  |  | fopen(argv[an], "r")) == NULL) { | 
| 170 | greg | 1.5 | perror(argv[an]); | 
| 171 | greg | 1.1 | quit(1); | 
| 172 |  |  | } | 
| 173 |  |  | } | 
| 174 |  |  | an++; | 
| 175 |  |  | /* get header */ | 
| 176 |  |  | printf("%s:\n", input[nfile].name); | 
| 177 | greg | 1.6 | getheader(input[nfile].fp, tabputs, NULL); | 
| 178 |  |  | if (wrongformat) { | 
| 179 |  |  | fprintf(stderr, "%s: not a Radiance picture\n", | 
| 180 |  |  | input[nfile].name); | 
| 181 |  |  | quit(1); | 
| 182 |  |  | } | 
| 183 | greg | 1.1 | /* get picture size */ | 
| 184 | greg | 1.4 | if (fgetresolu(&input[nfile].xres, &input[nfile].yres, | 
| 185 | greg | 1.10 | input[nfile].fp) < 0) { | 
| 186 | greg | 1.1 | fprintf(stderr, "%s: bad picture size\n", | 
| 187 |  |  | input[nfile].name); | 
| 188 |  |  | quit(1); | 
| 189 |  |  | } | 
| 190 | greg | 1.7 | if (ncolumns > 0) { | 
| 191 |  |  | if (curcol >= ncolumns) { | 
| 192 |  |  | cury = ymax; | 
| 193 |  |  | curx = 0; | 
| 194 |  |  | curcol = 0; | 
| 195 |  |  | } | 
| 196 |  |  | input[nfile].xloc = curx; | 
| 197 |  |  | input[nfile].yloc = cury; | 
| 198 |  |  | curx += input[nfile].xres; | 
| 199 |  |  | curcol++; | 
| 200 |  |  | } else { | 
| 201 |  |  | input[nfile].xloc = atoi(argv[an++]); | 
| 202 |  |  | input[nfile].yloc = atoi(argv[an++]); | 
| 203 |  |  | } | 
| 204 | greg | 1.1 | if (input[nfile].xloc < xmin) | 
| 205 |  |  | xmin = input[nfile].xloc; | 
| 206 |  |  | if (input[nfile].yloc < ymin) | 
| 207 |  |  | ymin = input[nfile].yloc; | 
| 208 |  |  | if (input[nfile].xloc+input[nfile].xres > xmax) | 
| 209 |  |  | xmax = input[nfile].xloc+input[nfile].xres; | 
| 210 |  |  | if (input[nfile].yloc+input[nfile].yres > ymax) | 
| 211 |  |  | ymax = input[nfile].yloc+input[nfile].yres; | 
| 212 | greg | 1.9 | if (thislabel != NULL) { | 
| 213 |  |  | if (++nfile >= MAXFILE) | 
| 214 |  |  | goto toomany; | 
| 215 |  |  | input[nfile].name = "<Label>"; | 
| 216 |  |  | input[nfile].hasmin = input[nfile].hasmax = 0; | 
| 217 |  |  | if ((input[nfile].fp = lblopen(thislabel, | 
| 218 |  |  | &input[nfile].xres, | 
| 219 |  |  | &input[nfile].yres)) == NULL) | 
| 220 | greg | 1.7 | goto labelerr; | 
| 221 |  |  | input[nfile].xloc = input[nfile-1].xloc; | 
| 222 |  |  | input[nfile].yloc = input[nfile-1].yloc + | 
| 223 | greg | 1.9 | input[nfile-1].yres-input[nfile].yres; | 
| 224 | greg | 1.7 | } | 
| 225 | greg | 1.1 | } | 
| 226 |  |  | if (xsiz <= 0) | 
| 227 |  |  | xsiz = xmax; | 
| 228 |  |  | if (ysiz <= 0) | 
| 229 |  |  | ysiz = ymax; | 
| 230 |  |  | /* add new header info. */ | 
| 231 |  |  | printargs(argc, argv, stdout); | 
| 232 | greg | 1.6 | fputformat(COLRFMT, stdout); | 
| 233 | greg | 1.10 | putchar('\n'); | 
| 234 |  |  | fprtresolu(xsiz, ysiz, stdout); | 
| 235 | greg | 1.1 |  | 
| 236 |  |  | compos(); | 
| 237 |  |  |  | 
| 238 |  |  | quit(0); | 
| 239 |  |  | userr: | 
| 240 | greg | 1.9 | fprintf(stderr, "Usage: %s [-x xr][-y yr][-b r g b][-a n][-la][-lh h] ", | 
| 241 | greg | 1.8 | progname); | 
| 242 | greg | 1.9 | fprintf(stderr, "[-t min1][+t max1][-l lab] pic1 x1 y1 ..\n"); | 
| 243 | greg | 1.7 | quit(1); | 
| 244 | greg | 1.9 | toomany: | 
| 245 |  |  | fprintf(stderr, "%s: only %d files and labels allowed\n", | 
| 246 |  |  | progname, MAXFILE); | 
| 247 |  |  | quit(1); | 
| 248 | greg | 1.7 | labelerr: | 
| 249 |  |  | fprintf(stderr, "%s: error opening label\n", progname); | 
| 250 | greg | 1.1 | quit(1); | 
| 251 |  |  | } | 
| 252 |  |  |  | 
| 253 |  |  |  | 
| 254 |  |  | compos()                                /* composite pictures */ | 
| 255 |  |  | { | 
| 256 |  |  | COLR  *scanin, *scanout; | 
| 257 |  |  | int  y; | 
| 258 |  |  | register int  x, i; | 
| 259 |  |  |  | 
| 260 | greg | 1.3 | scanin = (COLR *)malloc((xmax-xmin)*sizeof(COLR)); | 
| 261 | greg | 1.1 | if (scanin == NULL) | 
| 262 |  |  | goto memerr; | 
| 263 |  |  | scanin -= xmin; | 
| 264 |  |  | if (checkthresh) { | 
| 265 |  |  | scanout = (COLR *)malloc(xsiz*sizeof(COLR)); | 
| 266 |  |  | if (scanout == NULL) | 
| 267 |  |  | goto memerr; | 
| 268 |  |  | } else | 
| 269 |  |  | scanout = scanin; | 
| 270 |  |  | for (y = ymax-1; y >= 0; y--) { | 
| 271 |  |  | for (x = 0; x < xsiz; x++) | 
| 272 |  |  | copycolr(scanout[x], bgcolr); | 
| 273 |  |  | for (i = 0; i < nfile; i++) { | 
| 274 |  |  | if (input[i].yloc > y || | 
| 275 |  |  | input[i].yloc+input[i].yres <= y) | 
| 276 |  |  | continue; | 
| 277 |  |  | if (freadcolrs(scanin+input[i].xloc, | 
| 278 |  |  | input[i].xres, input[i].fp) < 0) { | 
| 279 | greg | 1.2 | fprintf(stderr, "%s: read error (y==%d)\n", | 
| 280 |  |  | input[i].name, | 
| 281 |  |  | y-input[i].yloc); | 
| 282 | greg | 1.1 | quit(1); | 
| 283 |  |  | } | 
| 284 |  |  | if (y >= ysiz) | 
| 285 |  |  | continue; | 
| 286 |  |  | if (checkthresh) { | 
| 287 |  |  | x = input[i].xloc+input[i].xres; | 
| 288 |  |  | if (x > xsiz) | 
| 289 |  |  | x = xsiz; | 
| 290 |  |  | for (x--; x >= 0 && x >= input[i].xloc; x--) { | 
| 291 |  |  | if (input[i].hasmin && | 
| 292 |  |  | cmpcolr(scanin[x], input[i].thmin) <= 0) | 
| 293 |  |  | continue; | 
| 294 |  |  | if (input[i].hasmax && | 
| 295 |  |  | cmpcolr(scanin[x], input[i].thmax) >= 0) | 
| 296 |  |  | continue; | 
| 297 |  |  | copycolr(scanout[x], scanin[x]); | 
| 298 |  |  | } | 
| 299 |  |  | } | 
| 300 |  |  | } | 
| 301 |  |  | if (y >= ysiz) | 
| 302 |  |  | continue; | 
| 303 |  |  | if (fwritecolrs(scanout, xsiz, stdout) < 0) { | 
| 304 | greg | 1.5 | perror(progname); | 
| 305 | greg | 1.1 | quit(1); | 
| 306 |  |  | } | 
| 307 |  |  | } | 
| 308 |  |  | return; | 
| 309 |  |  | memerr: | 
| 310 | greg | 1.5 | perror(progname); | 
| 311 | greg | 1.1 | quit(1); | 
| 312 |  |  | } | 
| 313 |  |  |  | 
| 314 |  |  |  | 
| 315 |  |  | int | 
| 316 |  |  | cmpcolr(c1, c2)                 /* compare two colr's (improvisation) */ | 
| 317 |  |  | register COLR  c1, c2; | 
| 318 |  |  | { | 
| 319 |  |  | register int  i, j; | 
| 320 |  |  |  | 
| 321 |  |  | j = 4;                          /* check exponents first! */ | 
| 322 |  |  | while (j--) | 
| 323 |  |  | if (i = c1[j] - c2[j]) | 
| 324 |  |  | return(i); | 
| 325 |  |  | return(0); | 
| 326 | greg | 1.9 | } | 
| 327 |  |  |  | 
| 328 |  |  |  | 
| 329 |  |  | FILE * | 
| 330 |  |  | lblopen(s, xp, yp)              /* open pipe to label generator */ | 
| 331 |  |  | char  *s; | 
| 332 |  |  | int  *xp, *yp; | 
| 333 |  |  | { | 
| 334 |  |  | char  com[128]; | 
| 335 |  |  | FILE  *fp; | 
| 336 |  |  |  | 
| 337 |  |  | sprintf(com, "psign -h %d '%.30s' | pfilt -1 -x /2 -y /2", | 
| 338 |  |  | 2*labelht, s); | 
| 339 |  |  | if ((fp = popen(com, "r")) == NULL) | 
| 340 |  |  | return(NULL); | 
| 341 |  |  | if (checkheader(fp, COLRFMT, NULL) < 0) | 
| 342 |  |  | goto err; | 
| 343 | greg | 1.10 | if (fgetresolu(xp, yp, fp) < 0) | 
| 344 | greg | 1.9 | goto err; | 
| 345 |  |  | return(fp); | 
| 346 |  |  | err: | 
| 347 |  |  | pclose(fp); | 
| 348 |  |  | return(NULL); | 
| 349 | greg | 1.1 | } | 
| 350 |  |  |  | 
| 351 |  |  |  | 
| 352 |  |  | quit(code)              /* exit gracefully */ | 
| 353 |  |  | int  code; | 
| 354 |  |  | { | 
| 355 |  |  | exit(code); | 
| 356 |  |  | } |