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