| 1 | greg | 1.8 | /* 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 |  |  | *  Combine picture files according to calcomp functions. | 
| 9 |  |  | * | 
| 10 |  |  | *      1/4/89 | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 |  |  | #include <stdio.h> | 
| 14 |  |  |  | 
| 15 |  |  | #include <errno.h> | 
| 16 |  |  |  | 
| 17 |  |  | #include "color.h" | 
| 18 |  |  |  | 
| 19 |  |  | #include "calcomp.h" | 
| 20 |  |  |  | 
| 21 | greg | 1.6 | #define MAXINP          32              /* maximum number of input files */ | 
| 22 | greg | 1.1 |  | 
| 23 |  |  | struct { | 
| 24 |  |  | char    *name;          /* file name */ | 
| 25 |  |  | FILE    *fp;            /* stream pointer */ | 
| 26 |  |  | COLOR   *scan;          /* input scanline */ | 
| 27 | greg | 1.3 | COLOR   coef;           /* coefficient */ | 
| 28 | greg | 1.1 | }       input[MAXINP];                  /* input pictures */ | 
| 29 |  |  |  | 
| 30 |  |  | int     nfiles;                         /* number of input files */ | 
| 31 |  |  |  | 
| 32 |  |  | char    *vcolin[3] = {"ri", "gi", "bi"}; | 
| 33 |  |  | char    *vcolout[3] = {"ro", "go", "bo"}; | 
| 34 | greg | 1.8 | #define vbrtin          "li" | 
| 35 |  |  | #define vbrtout         "lo" | 
| 36 | greg | 1.1 |  | 
| 37 | greg | 1.8 | #define vnfiles         "nfiles" | 
| 38 |  |  | #define vxres           "xres" | 
| 39 |  |  | #define vyres           "yres" | 
| 40 | greg | 1.1 | #define vxpos           "x" | 
| 41 |  |  | #define vypos           "y" | 
| 42 |  |  |  | 
| 43 |  |  | int     nowarn = 0;                     /* no warning messages? */ | 
| 44 |  |  |  | 
| 45 | greg | 1.9 | int     original = 0;                   /* origninal values? */ | 
| 46 |  |  |  | 
| 47 | greg | 1.1 | int     xres=0, yres=0;                 /* picture resolution */ | 
| 48 |  |  |  | 
| 49 |  |  | int     xpos, ypos;                     /* picture position */ | 
| 50 |  |  |  | 
| 51 | greg | 1.10 | int     wrongformat = 0; | 
| 52 | greg | 1.1 |  | 
| 53 | greg | 1.10 |  | 
| 54 | greg | 1.1 | tputs(s)                        /* put out string preceded by a tab */ | 
| 55 |  |  | char    *s; | 
| 56 |  |  | { | 
| 57 | greg | 1.10 | char    fmt[32]; | 
| 58 | greg | 1.9 | double  d; | 
| 59 |  |  | COLOR   ctmp; | 
| 60 | greg | 1.10 |  | 
| 61 |  |  | if (isformat(s)) {                      /* check format */ | 
| 62 |  |  | formatval(fmt, s); | 
| 63 |  |  | wrongformat = strcmp(fmt, COLRFMT); | 
| 64 |  |  | } else if (original && isexpos(s)) {    /* exposure */ | 
| 65 | greg | 1.9 | d = 1.0/exposval(s); | 
| 66 |  |  | scalecolor(input[nfiles].coef, d); | 
| 67 | greg | 1.10 | } else if (original && iscolcor(s)) {   /* color correction */ | 
| 68 | greg | 1.9 | colcorval(ctmp, s); | 
| 69 |  |  | colval(input[nfiles].coef,RED) /= colval(ctmp,RED); | 
| 70 |  |  | colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN); | 
| 71 |  |  | colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU); | 
| 72 | greg | 1.10 | } else {                                /* echo unaffected line */ | 
| 73 |  |  | putchar('\t'); | 
| 74 |  |  | fputs(s, stdout); | 
| 75 | greg | 1.9 | } | 
| 76 | greg | 1.10 |  | 
| 77 | greg | 1.1 | } | 
| 78 |  |  |  | 
| 79 |  |  |  | 
| 80 |  |  | main(argc, argv) | 
| 81 |  |  | int     argc; | 
| 82 |  |  | char    *argv[]; | 
| 83 |  |  | { | 
| 84 | greg | 1.8 | extern double   l_redin(), l_grnin(), l_bluin(), l_brtin(), atof(); | 
| 85 | greg | 1.3 | double  f; | 
| 86 | greg | 1.1 | int     a; | 
| 87 |  |  |  | 
| 88 |  |  | funset(vcolin[RED], 1, l_redin); | 
| 89 |  |  | funset(vcolin[GRN], 1, l_grnin); | 
| 90 |  |  | funset(vcolin[BLU], 1, l_bluin); | 
| 91 | greg | 1.8 | funset(vbrtin, 1, l_brtin); | 
| 92 | greg | 1.1 |  | 
| 93 |  |  | for (a = 1; a < argc; a++) | 
| 94 |  |  | if (argv[a][0] == '-') | 
| 95 |  |  | switch (argv[a][1]) { | 
| 96 |  |  | case '\0': | 
| 97 | greg | 1.3 | case 's': | 
| 98 |  |  | case 'c': | 
| 99 | greg | 1.1 | goto getfiles; | 
| 100 |  |  | case 'x': | 
| 101 |  |  | xres = atoi(argv[++a]); | 
| 102 |  |  | break; | 
| 103 |  |  | case 'y': | 
| 104 |  |  | yres = atoi(argv[++a]); | 
| 105 |  |  | break; | 
| 106 |  |  | case 'w': | 
| 107 |  |  | nowarn = !nowarn; | 
| 108 |  |  | break; | 
| 109 | greg | 1.9 | case 'o': | 
| 110 |  |  | original = !original; | 
| 111 |  |  | break; | 
| 112 | greg | 1.1 | case 'f': | 
| 113 |  |  | fcompile(argv[++a]); | 
| 114 |  |  | break; | 
| 115 |  |  | case 'e': | 
| 116 | greg | 1.7 | scompile(argv[++a], NULL, 0); | 
| 117 | greg | 1.1 | break; | 
| 118 |  |  | default: | 
| 119 | greg | 1.3 | goto usage; | 
| 120 | greg | 1.1 | } | 
| 121 |  |  | else | 
| 122 |  |  | break; | 
| 123 |  |  | getfiles: | 
| 124 | greg | 1.3 | for (nfiles = 0; nfiles < MAXINP; nfiles++) | 
| 125 |  |  | setcolor(input[nfiles].coef, 1.0, 1.0, 1.0); | 
| 126 | greg | 1.1 | nfiles = 0; | 
| 127 |  |  | for ( ; a < argc; a++) { | 
| 128 |  |  | if (nfiles >= MAXINP) { | 
| 129 |  |  | eputs(argv[0]); | 
| 130 |  |  | eputs(": too many picture files\n"); | 
| 131 |  |  | quit(1); | 
| 132 |  |  | } | 
| 133 | greg | 1.3 | if (argv[a][0] == '-') | 
| 134 |  |  | switch (argv[a][1]) { | 
| 135 |  |  | case '\0': | 
| 136 |  |  | input[nfiles].name = "<stdin>"; | 
| 137 |  |  | input[nfiles].fp = stdin; | 
| 138 |  |  | break; | 
| 139 |  |  | case 's': | 
| 140 |  |  | f = atof(argv[++a]); | 
| 141 |  |  | scalecolor(input[nfiles].coef, f); | 
| 142 |  |  | continue; | 
| 143 |  |  | case 'c': | 
| 144 |  |  | colval(input[nfiles].coef,RED)*=atof(argv[++a]); | 
| 145 |  |  | colval(input[nfiles].coef,GRN)*=atof(argv[++a]); | 
| 146 |  |  | colval(input[nfiles].coef,BLU)*=atof(argv[++a]); | 
| 147 |  |  | continue; | 
| 148 |  |  | default: | 
| 149 |  |  | goto usage; | 
| 150 |  |  | } | 
| 151 |  |  | else { | 
| 152 | greg | 1.1 | input[nfiles].name = argv[a]; | 
| 153 |  |  | input[nfiles].fp = fopen(argv[a], "r"); | 
| 154 |  |  | if (input[nfiles].fp == NULL) { | 
| 155 | greg | 1.6 | perror(argv[a]); | 
| 156 | greg | 1.1 | quit(1); | 
| 157 |  |  | } | 
| 158 |  |  | } | 
| 159 |  |  | fputs(input[nfiles].name, stdout); | 
| 160 |  |  | fputs(":\n", stdout); | 
| 161 | greg | 1.10 | getheader(input[nfiles].fp, tputs, NULL); | 
| 162 |  |  | if (wrongformat) { | 
| 163 |  |  | eputs(input[nfiles].name); | 
| 164 |  |  | eputs(": not in Radiance picture format\n"); | 
| 165 |  |  | quit(1); | 
| 166 |  |  | } | 
| 167 | greg | 1.5 | if (fgetresolu(&xpos, &ypos, input[nfiles].fp) != | 
| 168 |  |  | (YMAJOR|YDECR)) { | 
| 169 | greg | 1.1 | eputs(input[nfiles].name); | 
| 170 |  |  | eputs(": bad picture size\n"); | 
| 171 |  |  | quit(1); | 
| 172 |  |  | } | 
| 173 |  |  | if (xres == 0 && yres == 0) { | 
| 174 |  |  | xres = xpos; | 
| 175 |  |  | yres = ypos; | 
| 176 |  |  | } else if (xpos != xres || ypos != yres) { | 
| 177 |  |  | eputs(argv[0]); | 
| 178 |  |  | eputs(": resolution mismatch\n"); | 
| 179 |  |  | quit(1); | 
| 180 |  |  | } | 
| 181 |  |  | input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR)); | 
| 182 |  |  | nfiles++; | 
| 183 |  |  | } | 
| 184 |  |  | printargs(argc, argv, stdout); | 
| 185 | greg | 1.10 | fputformat(COLRFMT, stdout); | 
| 186 | greg | 1.1 | putchar('\n'); | 
| 187 | greg | 1.5 | fputresolu(YMAJOR|YDECR, xres, yres, stdout); | 
| 188 | greg | 1.1 | combine(); | 
| 189 |  |  | quit(0); | 
| 190 | greg | 1.3 | usage: | 
| 191 |  |  | eputs("Usage: "); | 
| 192 |  |  | eputs(argv[0]); | 
| 193 | greg | 1.9 | eputs( | 
| 194 |  |  | " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n"); | 
| 195 | greg | 1.3 | quit(1); | 
| 196 | greg | 1.1 | } | 
| 197 |  |  |  | 
| 198 |  |  |  | 
| 199 |  |  | combine()                       /* combine pictures */ | 
| 200 |  |  | { | 
| 201 | greg | 1.8 | EPNODE  *coldef[3], *brtdef; | 
| 202 | greg | 1.1 | COLOR   *scanout; | 
| 203 | greg | 1.8 | double  d; | 
| 204 | greg | 1.1 | register int    i, j; | 
| 205 |  |  | /* check defined variables */ | 
| 206 | greg | 1.4 | for (j = 0; j < 3; j++) { | 
| 207 |  |  | if (vardefined(vcolout[j])) | 
| 208 |  |  | coldef[j] = eparse(vcolout[j]); | 
| 209 |  |  | else | 
| 210 |  |  | coldef[j] = NULL; | 
| 211 |  |  | } | 
| 212 | greg | 1.8 | if (vardefined(vbrtout)) | 
| 213 |  |  | brtdef = eparse(vbrtout); | 
| 214 |  |  | else | 
| 215 |  |  | brtdef = NULL; | 
| 216 |  |  | /* predefine variables */ | 
| 217 | greg | 1.11 | varset(vnfiles, '=', (double)nfiles); | 
| 218 |  |  | varset(vxres, '=', (double)xres); | 
| 219 |  |  | varset(vyres, '=', (double)yres); | 
| 220 | greg | 1.1 | /* allocate scanline */ | 
| 221 |  |  | scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); | 
| 222 |  |  | /* combine files */ | 
| 223 |  |  | for (ypos = yres-1; ypos >= 0; ypos--) { | 
| 224 | greg | 1.8 | for (i = 0; i < nfiles; i++) | 
| 225 |  |  | if (freadscan(input[i].scan, xres, input[i].fp) < 0) { | 
| 226 |  |  | eputs(input[i].name); | 
| 227 |  |  | eputs(": read error\n"); | 
| 228 |  |  | quit(1); | 
| 229 |  |  | } | 
| 230 | greg | 1.11 | varset(vypos, '=', (double)ypos); | 
| 231 | greg | 1.8 | for (xpos = 0; xpos < xres; xpos++) { | 
| 232 | greg | 1.9 | for (i = 0; i < nfiles; i++) | 
| 233 |  |  | multcolor(input[i].scan[xpos],input[i].coef); | 
| 234 | greg | 1.11 | varset(vxpos, '=', (double)xpos); | 
| 235 | greg | 1.8 | eclock++; | 
| 236 |  |  | if (brtdef != NULL) { | 
| 237 |  |  | d = evalue(brtdef); | 
| 238 |  |  | if (d < 0.0) | 
| 239 |  |  | d = 0.0; | 
| 240 |  |  | setcolor(scanout[xpos], d, d, d); | 
| 241 |  |  | } else { | 
| 242 |  |  | for (j = 0; j < 3; j++) { | 
| 243 |  |  | if (coldef[j] != NULL) { | 
| 244 |  |  | colval(scanout[xpos],j) = evalue(coldef[j]); | 
| 245 |  |  | } else { | 
| 246 |  |  | colval(scanout[xpos],j) = 0.0; | 
| 247 |  |  | for (i = 0; i < nfiles; i++) | 
| 248 |  |  | colval(scanout[xpos],j) += | 
| 249 |  |  | colval(input[i].scan[xpos],j); | 
| 250 | greg | 1.1 | } | 
| 251 | greg | 1.8 | if (colval(scanout[xpos],j) < 0.0) | 
| 252 |  |  | colval(scanout[xpos],j) = 0.0; | 
| 253 |  |  | } | 
| 254 | greg | 1.1 | } | 
| 255 | greg | 1.8 | } | 
| 256 |  |  | if (fwritescan(scanout, xres, stdout) < 0) { | 
| 257 |  |  | perror("write error"); | 
| 258 |  |  | quit(1); | 
| 259 |  |  | } | 
| 260 | greg | 1.1 | } | 
| 261 |  |  | efree(scanout); | 
| 262 |  |  | } | 
| 263 |  |  |  | 
| 264 |  |  |  | 
| 265 |  |  | double | 
| 266 |  |  | colin(fn, ci)                   /* return color value for picture */ | 
| 267 |  |  | register int    fn, ci; | 
| 268 |  |  | { | 
| 269 |  |  | if (fn == 0) | 
| 270 |  |  | return((double)nfiles); | 
| 271 |  |  | if (fn < 1 || fn > nfiles) { | 
| 272 |  |  | errno = EDOM; | 
| 273 |  |  | return(0.0); | 
| 274 |  |  | } | 
| 275 |  |  | return(colval(input[fn-1].scan[xpos],ci)); | 
| 276 |  |  | } | 
| 277 |  |  |  | 
| 278 |  |  |  | 
| 279 |  |  | double | 
| 280 |  |  | l_redin()                       /* get red color */ | 
| 281 |  |  | { | 
| 282 |  |  | return(colin((int)(argument(1)+.5), RED)); | 
| 283 |  |  | } | 
| 284 |  |  |  | 
| 285 |  |  |  | 
| 286 |  |  | double | 
| 287 |  |  | l_grnin()                       /* get green color */ | 
| 288 |  |  | { | 
| 289 |  |  | return(colin((int)(argument(1)+.5), GRN)); | 
| 290 |  |  | } | 
| 291 |  |  |  | 
| 292 |  |  |  | 
| 293 |  |  | double | 
| 294 |  |  | l_bluin()                       /* get blue color */ | 
| 295 |  |  | { | 
| 296 |  |  | return(colin((int)(argument(1)+.5), BLU)); | 
| 297 | greg | 1.8 | } | 
| 298 |  |  |  | 
| 299 |  |  |  | 
| 300 |  |  | double | 
| 301 |  |  | l_brtin()                       /* get brightness value */ | 
| 302 |  |  | { | 
| 303 |  |  | register int    fn; | 
| 304 |  |  |  | 
| 305 |  |  | fn = argument(1)+.5; | 
| 306 |  |  | if (fn == 0) | 
| 307 |  |  | return((double)nfiles); | 
| 308 |  |  | if (fn < 1 || fn > nfiles) { | 
| 309 |  |  | errno = EDOM; | 
| 310 |  |  | return(0.0); | 
| 311 |  |  | } | 
| 312 |  |  | return(bright(input[fn-1].scan[xpos])); | 
| 313 | greg | 1.1 | } | 
| 314 |  |  |  | 
| 315 |  |  |  | 
| 316 |  |  | wputs(msg) | 
| 317 |  |  | char    *msg; | 
| 318 |  |  | { | 
| 319 |  |  | if (!nowarn) | 
| 320 |  |  | eputs(msg); | 
| 321 |  |  | } | 
| 322 |  |  |  | 
| 323 |  |  |  | 
| 324 |  |  | eputs(msg) | 
| 325 |  |  | char    *msg; | 
| 326 |  |  | { | 
| 327 |  |  | fputs(msg, stderr); | 
| 328 |  |  | } | 
| 329 |  |  |  | 
| 330 |  |  |  | 
| 331 |  |  | quit(code) | 
| 332 |  |  | int     code; | 
| 333 |  |  | { | 
| 334 |  |  | exit(code); | 
| 335 |  |  | } |