| 1 | greg | 1.1 | #ifndef lint | 
| 2 | greg | 2.33 | static const char       RCSid[] = "$Id: pcomb.c,v 2.32 2004/03/28 20:33:14 schorsch Exp $"; | 
| 3 | greg | 1.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | *  Combine picture files according to calcomp functions. | 
| 6 |  |  | * | 
| 7 |  |  | *      1/4/89 | 
| 8 |  |  | */ | 
| 9 |  |  |  | 
| 10 | greg | 2.28 | #include "platform.h" | 
| 11 | greg | 2.33 | #include "standard.h" | 
| 12 | schorsch | 2.29 | #include "rtprocess.h" | 
| 13 | greg | 1.1 | #include "color.h" | 
| 14 |  |  | #include "calcomp.h" | 
| 15 | greg | 2.10 | #include "view.h" | 
| 16 |  |  |  | 
| 17 | greg | 1.6 | #define MAXINP          32              /* maximum number of input files */ | 
| 18 | greg | 2.22 | #define WINSIZ          64              /* scanline window size */ | 
| 19 | greg | 2.13 | #define MIDSCN          ((WINSIZ-1)/2+1) | 
| 20 | greg | 1.1 |  | 
| 21 |  |  | struct { | 
| 22 | greg | 1.19 | char    *name;          /* file or command name */ | 
| 23 | greg | 1.1 | FILE    *fp;            /* stream pointer */ | 
| 24 | greg | 2.10 | VIEW    vw;             /* view for picture */ | 
| 25 |  |  | RESOLU  rs;             /* image resolution and orientation */ | 
| 26 | greg | 2.17 | float   pa;             /* pixel aspect ratio */ | 
| 27 | greg | 1.14 | COLOR   *scan[WINSIZ];  /* input scanline window */ | 
| 28 | greg | 1.3 | COLOR   coef;           /* coefficient */ | 
| 29 | greg | 1.17 | COLOR   expos;          /* recorded exposure */ | 
| 30 | greg | 1.1 | }       input[MAXINP];                  /* input pictures */ | 
| 31 |  |  |  | 
| 32 |  |  | int     nfiles;                         /* number of input files */ | 
| 33 |  |  |  | 
| 34 | greg | 2.16 | char    ourfmt[LPICFMT+1] = PICFMT;     /* input picture format */ | 
| 35 |  |  |  | 
| 36 | greg | 2.31 | char    StandardInput[] = "<stdin>"; | 
| 37 | greg | 2.6 | char    Command[] = "<Command>"; | 
| 38 | greg | 1.21 | char    vcolin[3][4] = {"ri", "gi", "bi"}; | 
| 39 |  |  | char    vcolout[3][4] = {"ro", "go", "bo"}; | 
| 40 | greg | 1.15 | char    vbrtin[] = "li"; | 
| 41 |  |  | char    vbrtout[] = "lo"; | 
| 42 | greg | 1.21 | char    vcolexp[3][4] = {"re", "ge", "be"}; | 
| 43 | greg | 1.17 | char    vbrtexp[] = "le"; | 
| 44 | greg | 2.18 | char    vpixaspect[] = "pa"; | 
| 45 | greg | 1.1 |  | 
| 46 | greg | 2.18 | char    vray[7][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz", "T"}; | 
| 47 | greg | 2.10 |  | 
| 48 | greg | 2.17 | char    vpsize[] = "S"; | 
| 49 |  |  |  | 
| 50 | greg | 1.18 | char    vnfiles[] = "nfiles"; | 
| 51 | greg | 2.18 | char    vwhteff[] = "WE"; | 
| 52 | greg | 1.18 | char    vxmax[] = "xmax"; | 
| 53 |  |  | char    vymax[] = "ymax"; | 
| 54 |  |  | char    vxres[] = "xres"; | 
| 55 |  |  | char    vyres[] = "yres"; | 
| 56 |  |  | char    vxpos[] = "x"; | 
| 57 |  |  | char    vypos[] = "y"; | 
| 58 | greg | 1.1 |  | 
| 59 |  |  | int     nowarn = 0;                     /* no warning messages? */ | 
| 60 |  |  |  | 
| 61 | greg | 1.18 | int     xmax = 0, ymax = 0;             /* input resolution */ | 
| 62 | greg | 1.9 |  | 
| 63 | greg | 1.18 | int     xscan, yscan;                   /* input position */ | 
| 64 | greg | 1.1 |  | 
| 65 | greg | 1.18 | int     xres, yres;                     /* output resolution */ | 
| 66 | greg | 1.1 |  | 
| 67 | greg | 1.18 | int     xpos, ypos;                     /* output position */ | 
| 68 |  |  |  | 
| 69 | greg | 2.10 | char    *progname;                      /* global argv[0] */ | 
| 70 |  |  |  | 
| 71 | greg | 1.10 | int     wrongformat = 0; | 
| 72 | greg | 2.10 | int     gotview; | 
| 73 | greg | 1.1 |  | 
| 74 | greg | 1.10 |  | 
| 75 | schorsch | 2.30 | static gethfunc tabputs; | 
| 76 | schorsch | 2.32 | static void checkfile(void); | 
| 77 |  |  | static double rgb_bright(COLOR  clr); | 
| 78 |  |  | static double xyz_bright(COLOR  clr); | 
| 79 |  |  | static void init(void); | 
| 80 |  |  | static void combine(void); | 
| 81 |  |  | static void advance(void); | 
| 82 |  |  | static double l_expos(char      *nam); | 
| 83 |  |  | static double l_pixaspect(char *nm); | 
| 84 |  |  | static double l_colin(char      *nam); | 
| 85 |  |  | static double l_ray(char        *nam); | 
| 86 |  |  | static double l_psize(char *nm); | 
| 87 |  |  |  | 
| 88 |  |  |  | 
| 89 |  |  | int | 
| 90 |  |  | main( | 
| 91 |  |  | int     argc, | 
| 92 |  |  | char    *argv[] | 
| 93 |  |  | ) | 
| 94 | greg | 1.1 | { | 
| 95 | greg | 1.18 | int     original; | 
| 96 | greg | 1.3 | double  f; | 
| 97 | schorsch | 2.32 | int     a; | 
| 98 | schorsch | 2.23 | SET_DEFAULT_BINARY(); | 
| 99 |  |  | SET_FILE_BINARY(stdin); | 
| 100 |  |  | SET_FILE_BINARY(stdout); | 
| 101 | greg | 2.10 | progname = argv[0]; | 
| 102 | greg | 1.15 | /* scan options */ | 
| 103 |  |  | for (a = 1; a < argc; a++) { | 
| 104 | greg | 1.1 | if (argv[a][0] == '-') | 
| 105 |  |  | switch (argv[a][1]) { | 
| 106 |  |  | case 'x': | 
| 107 |  |  | case 'y': | 
| 108 | greg | 1.18 | a++; | 
| 109 | greg | 1.15 | continue; | 
| 110 | greg | 1.1 | case 'w': | 
| 111 |  |  | nowarn = !nowarn; | 
| 112 | greg | 1.15 | continue; | 
| 113 | greg | 1.1 | case 'f': | 
| 114 |  |  | case 'e': | 
| 115 | greg | 1.16 | a++; | 
| 116 | greg | 1.15 | continue; | 
| 117 | greg | 1.1 | } | 
| 118 | greg | 1.15 | break; | 
| 119 |  |  | } | 
| 120 | greg | 2.14 | newheader("RADIANCE", stdout);  /* start header */ | 
| 121 | greg | 2.22 | fputnow(stdout); | 
| 122 | greg | 1.15 | /* process files */ | 
| 123 | greg | 1.17 | for (nfiles = 0; nfiles < MAXINP; nfiles++) { | 
| 124 | greg | 1.3 | setcolor(input[nfiles].coef, 1.0, 1.0, 1.0); | 
| 125 | greg | 1.17 | setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); | 
| 126 | schorsch | 2.26 | input[nfiles].vw = stdview; | 
| 127 | greg | 2.17 | input[nfiles].pa = 1.0; | 
| 128 | greg | 1.17 | } | 
| 129 | greg | 1.1 | nfiles = 0; | 
| 130 | greg | 1.18 | original = 0; | 
| 131 | greg | 1.1 | for ( ; a < argc; a++) { | 
| 132 |  |  | if (nfiles >= MAXINP) { | 
| 133 |  |  | eputs(argv[0]); | 
| 134 |  |  | eputs(": too many picture files\n"); | 
| 135 |  |  | quit(1); | 
| 136 |  |  | } | 
| 137 | greg | 1.3 | if (argv[a][0] == '-') | 
| 138 |  |  | switch (argv[a][1]) { | 
| 139 |  |  | case '\0': | 
| 140 | greg | 2.31 | input[nfiles].name = StandardInput; | 
| 141 | greg | 1.3 | input[nfiles].fp = stdin; | 
| 142 |  |  | break; | 
| 143 | greg | 1.17 | case 'o': | 
| 144 |  |  | original++; | 
| 145 | greg | 1.22 | continue; | 
| 146 | greg | 1.3 | case 's': | 
| 147 |  |  | f = atof(argv[++a]); | 
| 148 |  |  | scalecolor(input[nfiles].coef, f); | 
| 149 |  |  | continue; | 
| 150 |  |  | case 'c': | 
| 151 |  |  | colval(input[nfiles].coef,RED)*=atof(argv[++a]); | 
| 152 |  |  | colval(input[nfiles].coef,GRN)*=atof(argv[++a]); | 
| 153 |  |  | colval(input[nfiles].coef,BLU)*=atof(argv[++a]); | 
| 154 |  |  | continue; | 
| 155 |  |  | default: | 
| 156 |  |  | goto usage; | 
| 157 |  |  | } | 
| 158 |  |  | else { | 
| 159 | greg | 2.2 | if (argv[a][0] == '!') { | 
| 160 | greg | 2.6 | input[nfiles].name = Command; | 
| 161 | greg | 2.2 | input[nfiles].fp = popen(argv[a]+1, "r"); | 
| 162 |  |  | } else { | 
| 163 |  |  | input[nfiles].name = argv[a]; | 
| 164 |  |  | input[nfiles].fp = fopen(argv[a], "r"); | 
| 165 |  |  | } | 
| 166 | greg | 1.1 | if (input[nfiles].fp == NULL) { | 
| 167 | greg | 1.6 | perror(argv[a]); | 
| 168 | greg | 1.1 | quit(1); | 
| 169 |  |  | } | 
| 170 |  |  | } | 
| 171 | greg | 1.16 | checkfile(); | 
| 172 | greg | 1.18 | if (original) { | 
| 173 |  |  | colval(input[nfiles].coef,RED) /= | 
| 174 |  |  | colval(input[nfiles].expos,RED); | 
| 175 |  |  | colval(input[nfiles].coef,GRN) /= | 
| 176 |  |  | colval(input[nfiles].expos,GRN); | 
| 177 |  |  | colval(input[nfiles].coef,BLU) /= | 
| 178 |  |  | colval(input[nfiles].expos,BLU); | 
| 179 | greg | 2.22 | setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); | 
| 180 | greg | 1.18 | } | 
| 181 |  |  | nfiles++; | 
| 182 | greg | 1.17 | original = 0; | 
| 183 | greg | 1.15 | } | 
| 184 | greg | 1.18 | init();                         /* set constants */ | 
| 185 | greg | 1.15 | /* go back and get expressions */ | 
| 186 |  |  | for (a = 1; a < argc; a++) { | 
| 187 |  |  | if (argv[a][0] == '-') | 
| 188 |  |  | switch (argv[a][1]) { | 
| 189 |  |  | case 'x': | 
| 190 | greg | 1.21 | varset(vxres, ':', eval(argv[++a])); | 
| 191 | greg | 1.18 | continue; | 
| 192 | greg | 1.15 | case 'y': | 
| 193 | greg | 1.21 | varset(vyres, ':', eval(argv[++a])); | 
| 194 | greg | 1.16 | continue; | 
| 195 | greg | 1.15 | case 'w': | 
| 196 |  |  | continue; | 
| 197 |  |  | case 'f': | 
| 198 |  |  | fcompile(argv[++a]); | 
| 199 |  |  | continue; | 
| 200 |  |  | case 'e': | 
| 201 |  |  | scompile(argv[++a], NULL, 0); | 
| 202 |  |  | continue; | 
| 203 |  |  | } | 
| 204 |  |  | break; | 
| 205 |  |  | } | 
| 206 | greg | 1.18 | /* set/get output resolution */ | 
| 207 |  |  | if (!vardefined(vxres)) | 
| 208 |  |  | varset(vxres, ':', (double)xmax); | 
| 209 |  |  | if (!vardefined(vyres)) | 
| 210 |  |  | varset(vyres, ':', (double)ymax); | 
| 211 |  |  | xres = varvalue(vxres) + .5; | 
| 212 |  |  | yres = varvalue(vyres) + .5; | 
| 213 |  |  | if (xres <= 0 || yres <= 0) { | 
| 214 |  |  | eputs(argv[0]); | 
| 215 |  |  | eputs(": illegal output resolution\n"); | 
| 216 |  |  | quit(1); | 
| 217 |  |  | } | 
| 218 | greg | 1.15 | /* complete header */ | 
| 219 |  |  | printargs(argc, argv, stdout); | 
| 220 | greg | 2.16 | if (strcmp(ourfmt, PICFMT)) | 
| 221 |  |  | fputformat(ourfmt, stdout);     /* print format if known */ | 
| 222 | greg | 1.15 | putchar('\n'); | 
| 223 | greg | 1.23 | fprtresolu(xres, yres, stdout); | 
| 224 | greg | 1.15 | /* combine pictures */ | 
| 225 |  |  | combine(); | 
| 226 |  |  | quit(0); | 
| 227 |  |  | usage: | 
| 228 |  |  | eputs("Usage: "); | 
| 229 |  |  | eputs(argv[0]); | 
| 230 |  |  | eputs( | 
| 231 | greg | 2.9 | " [-w][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] pic ..]\n"); | 
| 232 | greg | 1.15 | quit(1); | 
| 233 | schorsch | 2.32 | return 1; /* pro forma return */ | 
| 234 | greg | 1.15 | } | 
| 235 |  |  |  | 
| 236 |  |  |  | 
| 237 | schorsch | 2.30 | static int | 
| 238 |  |  | tabputs(                        /* put out string preceded by a tab */ | 
| 239 |  |  | char    *s, | 
| 240 |  |  | void    *p | 
| 241 |  |  | ) | 
| 242 | greg | 1.15 | { | 
| 243 |  |  | char    fmt[32]; | 
| 244 |  |  | double  d; | 
| 245 |  |  | COLOR   ctmp; | 
| 246 |  |  |  | 
| 247 | greg | 2.14 | if (isheadid(s))                        /* header id */ | 
| 248 | gwlarson | 2.21 | return(0);      /* don't echo */ | 
| 249 | greg | 2.14 | if (formatval(fmt, s)) {                /* check format */ | 
| 250 | greg | 2.16 | if (globmatch(ourfmt, fmt)) { | 
| 251 |  |  | wrongformat = 0; | 
| 252 |  |  | strcpy(ourfmt, fmt); | 
| 253 |  |  | } else | 
| 254 | greg | 2.22 | wrongformat = globmatch(PICFMT, fmt) ? 1 : -1; | 
| 255 | gwlarson | 2.21 | return(0);      /* don't echo */ | 
| 256 | greg | 1.17 | } | 
| 257 |  |  | if (isexpos(s)) {                       /* exposure */ | 
| 258 |  |  | d = exposval(s); | 
| 259 |  |  | scalecolor(input[nfiles].expos, d); | 
| 260 |  |  | } else if (iscolcor(s)) {               /* color correction */ | 
| 261 | greg | 1.15 | colcorval(ctmp, s); | 
| 262 | greg | 1.17 | multcolor(input[nfiles].expos, ctmp); | 
| 263 | greg | 2.18 | } else if (isaspect(s)) | 
| 264 | greg | 2.17 | input[nfiles].pa *= aspectval(s); | 
| 265 | greg | 2.18 | else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0) | 
| 266 | greg | 2.10 | gotview++; | 
| 267 | greg | 1.17 | /* echo line */ | 
| 268 |  |  | putchar('\t'); | 
| 269 | gwlarson | 2.21 | return(fputs(s, stdout)); | 
| 270 | greg | 1.16 | } | 
| 271 | greg | 1.15 |  | 
| 272 | greg | 1.16 |  | 
| 273 | schorsch | 2.32 | static void | 
| 274 |  |  | checkfile(void)                 /* ready a file */ | 
| 275 | greg | 1.16 | { | 
| 276 |  |  | register int    i; | 
| 277 |  |  | /* process header */ | 
| 278 | greg | 2.10 | gotview = 0; | 
| 279 | greg | 1.16 | fputs(input[nfiles].name, stdout); | 
| 280 |  |  | fputs(":\n", stdout); | 
| 281 | greg | 2.22 | getheader(input[nfiles].fp, tabputs, NULL); | 
| 282 |  |  | if (wrongformat < 0) { | 
| 283 | greg | 1.16 | eputs(input[nfiles].name); | 
| 284 | greg | 2.22 | eputs(": not a Radiance picture\n"); | 
| 285 | greg | 1.16 | quit(1); | 
| 286 |  |  | } | 
| 287 | greg | 2.22 | if (wrongformat > 0) { | 
| 288 |  |  | wputs(input[nfiles].name); | 
| 289 |  |  | wputs(": warning -- incompatible picture format\n"); | 
| 290 |  |  | } | 
| 291 | greg | 2.10 | if (!gotview || setview(&input[nfiles].vw) != NULL) | 
| 292 |  |  | input[nfiles].vw.type = 0; | 
| 293 |  |  | if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) { | 
| 294 | greg | 1.16 | eputs(input[nfiles].name); | 
| 295 |  |  | eputs(": bad picture size\n"); | 
| 296 |  |  | quit(1); | 
| 297 |  |  | } | 
| 298 | greg | 1.18 | if (xmax == 0 && ymax == 0) { | 
| 299 | greg | 2.10 | xmax = scanlen(&input[nfiles].rs); | 
| 300 |  |  | ymax = numscans(&input[nfiles].rs); | 
| 301 |  |  | } else if (scanlen(&input[nfiles].rs) != xmax || | 
| 302 |  |  | numscans(&input[nfiles].rs) != ymax) { | 
| 303 | greg | 1.16 | eputs(input[nfiles].name); | 
| 304 |  |  | eputs(": resolution mismatch\n"); | 
| 305 |  |  | quit(1); | 
| 306 |  |  | } | 
| 307 |  |  | /* allocate scanlines */ | 
| 308 |  |  | for (i = 0; i < WINSIZ; i++) | 
| 309 | greg | 1.18 | input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR)); | 
| 310 | greg | 1.15 | } | 
| 311 |  |  |  | 
| 312 |  |  |  | 
| 313 | schorsch | 2.32 | static double | 
| 314 |  |  | rgb_bright( | 
| 315 |  |  | COLOR  clr | 
| 316 |  |  | ) | 
| 317 | greg | 2.16 | { | 
| 318 |  |  | return(bright(clr)); | 
| 319 |  |  | } | 
| 320 |  |  |  | 
| 321 |  |  |  | 
| 322 | schorsch | 2.32 | static double | 
| 323 |  |  | xyz_bright( | 
| 324 |  |  | COLOR  clr | 
| 325 |  |  | ) | 
| 326 | greg | 2.16 | { | 
| 327 |  |  | return(clr[CIEY]); | 
| 328 |  |  | } | 
| 329 |  |  |  | 
| 330 |  |  |  | 
| 331 |  |  | double  (*ourbright)() = rgb_bright; | 
| 332 |  |  |  | 
| 333 |  |  |  | 
| 334 | schorsch | 2.32 | static void | 
| 335 |  |  | init(void)                                      /* perform final setup */ | 
| 336 | greg | 1.15 | { | 
| 337 | greg | 2.27 | double  l_colin(char *), l_expos(char *), l_pixaspect(char *), | 
| 338 |  |  | l_ray(char *), l_psize(char *); | 
| 339 | greg | 1.16 | register int    i; | 
| 340 | greg | 1.15 | /* define constants */ | 
| 341 | greg | 2.19 | varset("PI", ':', PI); | 
| 342 | greg | 1.15 | varset(vnfiles, ':', (double)nfiles); | 
| 343 | greg | 1.18 | varset(vxmax, ':', (double)xmax); | 
| 344 |  |  | varset(vymax, ':', (double)ymax); | 
| 345 | greg | 1.15 | /* set functions */ | 
| 346 |  |  | for (i = 0; i < 3; i++) { | 
| 347 | greg | 1.17 | funset(vcolexp[i], 1, ':', l_expos); | 
| 348 | greg | 1.15 | funset(vcolin[i], 1, '=', l_colin); | 
| 349 |  |  | } | 
| 350 | greg | 1.17 | funset(vbrtexp, 1, ':', l_expos); | 
| 351 | greg | 1.15 | funset(vbrtin, 1, '=', l_colin); | 
| 352 | greg | 2.18 | funset(vpixaspect, 1, ':', l_pixaspect); | 
| 353 |  |  | for (i = 0; i < 7; i++) | 
| 354 | greg | 2.10 | funset(vray[i], 1, '=', l_ray); | 
| 355 | greg | 2.17 | funset(vpsize, 1, '=', l_psize); | 
| 356 | greg | 2.16 | /* set brightness function */ | 
| 357 | greg | 2.18 | if (!strcmp(ourfmt, CIEFMT)) { | 
| 358 |  |  | varset(vwhteff, ':', 1.0); | 
| 359 | greg | 2.16 | ourbright = xyz_bright; | 
| 360 | greg | 2.18 | } else | 
| 361 |  |  | varset(vwhteff, ':', WHTEFFICACY); | 
| 362 | greg | 1.1 | } | 
| 363 |  |  |  | 
| 364 |  |  |  | 
| 365 | schorsch | 2.32 | static void | 
| 366 |  |  | combine(void)                   /* combine pictures */ | 
| 367 | greg | 1.1 | { | 
| 368 | greg | 1.8 | EPNODE  *coldef[3], *brtdef; | 
| 369 | greg | 1.1 | COLOR   *scanout; | 
| 370 | greg | 1.8 | double  d; | 
| 371 | greg | 1.1 | register int    i, j; | 
| 372 |  |  | /* check defined variables */ | 
| 373 | greg | 1.4 | for (j = 0; j < 3; j++) { | 
| 374 |  |  | if (vardefined(vcolout[j])) | 
| 375 |  |  | coldef[j] = eparse(vcolout[j]); | 
| 376 |  |  | else | 
| 377 |  |  | coldef[j] = NULL; | 
| 378 |  |  | } | 
| 379 | greg | 1.8 | if (vardefined(vbrtout)) | 
| 380 |  |  | brtdef = eparse(vbrtout); | 
| 381 |  |  | else | 
| 382 |  |  | brtdef = NULL; | 
| 383 | greg | 1.1 | /* allocate scanline */ | 
| 384 |  |  | scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); | 
| 385 | greg | 1.18 | /* set input position */ | 
| 386 |  |  | yscan = ymax+MIDSCN; | 
| 387 | greg | 1.1 | /* combine files */ | 
| 388 |  |  | for (ypos = yres-1; ypos >= 0; ypos--) { | 
| 389 | greg | 1.14 | advance(); | 
| 390 | greg | 1.11 | varset(vypos, '=', (double)ypos); | 
| 391 | greg | 1.8 | for (xpos = 0; xpos < xres; xpos++) { | 
| 392 | greg | 1.18 | xscan = (long)xpos*xmax/xres; | 
| 393 | greg | 1.11 | varset(vxpos, '=', (double)xpos); | 
| 394 | greg | 1.8 | eclock++; | 
| 395 |  |  | if (brtdef != NULL) { | 
| 396 |  |  | d = evalue(brtdef); | 
| 397 |  |  | if (d < 0.0) | 
| 398 |  |  | d = 0.0; | 
| 399 |  |  | setcolor(scanout[xpos], d, d, d); | 
| 400 |  |  | } else { | 
| 401 |  |  | for (j = 0; j < 3; j++) { | 
| 402 |  |  | if (coldef[j] != NULL) { | 
| 403 | greg | 1.13 | d = evalue(coldef[j]); | 
| 404 | greg | 1.8 | } else { | 
| 405 | greg | 1.13 | d = 0.0; | 
| 406 | greg | 1.8 | for (i = 0; i < nfiles; i++) | 
| 407 | greg | 1.18 | d += colval(input[i].scan[MIDSCN][xscan],j); | 
| 408 | greg | 1.1 | } | 
| 409 | greg | 1.13 | if (d < 0.0) | 
| 410 |  |  | d = 0.0; | 
| 411 |  |  | colval(scanout[xpos],j) = d; | 
| 412 | greg | 1.8 | } | 
| 413 | greg | 1.1 | } | 
| 414 | greg | 1.8 | } | 
| 415 |  |  | if (fwritescan(scanout, xres, stdout) < 0) { | 
| 416 |  |  | perror("write error"); | 
| 417 |  |  | quit(1); | 
| 418 |  |  | } | 
| 419 | greg | 1.1 | } | 
| 420 | greg | 2.22 | efree((char *)scanout); | 
| 421 | greg | 1.1 | } | 
| 422 |  |  |  | 
| 423 |  |  |  | 
| 424 | schorsch | 2.32 | static void | 
| 425 |  |  | advance(void)                   /* read in data for next scanline */ | 
| 426 | greg | 1.14 | { | 
| 427 | greg | 1.18 | int     ytarget; | 
| 428 | greg | 1.14 | register COLOR  *st; | 
| 429 |  |  | register int    i, j; | 
| 430 |  |  |  | 
| 431 | greg | 1.18 | for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--) | 
| 432 |  |  | for (i = 0; i < nfiles; i++) { | 
| 433 |  |  | st = input[i].scan[WINSIZ-1]; | 
| 434 |  |  | for (j = WINSIZ-1; j > 0; j--)  /* rotate window */ | 
| 435 |  |  | input[i].scan[j] = input[i].scan[j-1]; | 
| 436 |  |  | input[i].scan[0] = st; | 
| 437 |  |  | if (yscan <= MIDSCN)            /* hit bottom? */ | 
| 438 |  |  | continue; | 
| 439 | greg | 2.4 | if (freadscan(st, xmax, input[i].fp) < 0) {  /* read */ | 
| 440 | greg | 1.18 | eputs(input[i].name); | 
| 441 |  |  | eputs(": read error\n"); | 
| 442 |  |  | quit(1); | 
| 443 |  |  | } | 
| 444 | greg | 2.4 | if (fabs(colval(input[i].coef,RED)-1.0) > 1e-3 || | 
| 445 |  |  | fabs(colval(input[i].coef,GRN)-1.0) > 1e-3 || | 
| 446 |  |  | fabs(colval(input[i].coef,BLU)-1.0) > 1e-3) | 
| 447 |  |  | for (j = 0; j < xmax; j++)  /* adjust color */ | 
| 448 |  |  | multcolor(st[j], input[i].coef); | 
| 449 | greg | 1.14 | } | 
| 450 |  |  | } | 
| 451 |  |  |  | 
| 452 |  |  |  | 
| 453 | schorsch | 2.32 | static double | 
| 454 |  |  | l_expos(                        /* return picture exposure */ | 
| 455 |  |  | register char   *nam | 
| 456 |  |  | ) | 
| 457 | greg | 1.1 | { | 
| 458 | greg | 1.15 | register int    fn, n; | 
| 459 | greg | 1.14 |  | 
| 460 | greg | 1.18 | fn = argument(1) - .5; | 
| 461 |  |  | if (fn < 0 || fn >= nfiles) | 
| 462 |  |  | return(1.0); | 
| 463 | greg | 1.17 | if (nam == vbrtexp) | 
| 464 | greg | 2.16 | return((*ourbright)(input[fn].expos)); | 
| 465 | greg | 1.15 | n = 3; | 
| 466 |  |  | while (n--) | 
| 467 | greg | 1.17 | if (nam == vcolexp[n]) | 
| 468 |  |  | return(colval(input[fn].expos,n)); | 
| 469 |  |  | eputs("Bad call to l_expos()!\n"); | 
| 470 | greg | 1.15 | quit(1); | 
| 471 | schorsch | 2.32 | return 1; /* pro forma return */ | 
| 472 | greg | 1.15 | } | 
| 473 |  |  |  | 
| 474 |  |  |  | 
| 475 | schorsch | 2.32 | static double | 
| 476 | greg | 2.27 | l_pixaspect(char *nm)           /* return pixel aspect ratio */ | 
| 477 | greg | 2.18 | { | 
| 478 |  |  | register int    fn; | 
| 479 |  |  |  | 
| 480 |  |  | fn = argument(1) - .5; | 
| 481 |  |  | if (fn < 0 || fn >= nfiles) | 
| 482 |  |  | return(1.0); | 
| 483 |  |  | return(input[fn].pa); | 
| 484 |  |  | } | 
| 485 |  |  |  | 
| 486 |  |  |  | 
| 487 | schorsch | 2.32 | static double | 
| 488 |  |  | l_colin(                        /* return color value for picture */ | 
| 489 |  |  | register char   *nam | 
| 490 |  |  | ) | 
| 491 | greg | 1.15 | { | 
| 492 |  |  | int     fn; | 
| 493 |  |  | register int    n, xoff, yoff; | 
| 494 |  |  | double  d; | 
| 495 |  |  |  | 
| 496 | greg | 2.18 | fn = argument(1) - .5; | 
| 497 | greg | 1.16 | if (fn < 0 || fn >= nfiles) { | 
| 498 | greg | 1.15 | errno = EDOM; | 
| 499 |  |  | return(0.0); | 
| 500 |  |  | } | 
| 501 | greg | 1.14 | xoff = yoff = 0; | 
| 502 |  |  | n = nargum(); | 
| 503 |  |  | if (n >= 2) { | 
| 504 |  |  | d = argument(2); | 
| 505 |  |  | if (d < 0.0) { | 
| 506 |  |  | xoff = d-.5; | 
| 507 | greg | 1.18 | if (xscan+xoff < 0) | 
| 508 |  |  | xoff = -xscan; | 
| 509 | greg | 1.14 | } else { | 
| 510 |  |  | xoff = d+.5; | 
| 511 | greg | 1.18 | if (xscan+xoff >= xmax) | 
| 512 |  |  | xoff = xmax-1-xscan; | 
| 513 | greg | 1.14 | } | 
| 514 |  |  | } | 
| 515 |  |  | if (n >= 3) { | 
| 516 |  |  | d = argument(3); | 
| 517 |  |  | if (d < 0.0) { | 
| 518 |  |  | yoff = d-.5; | 
| 519 |  |  | if (yoff+MIDSCN < 0) | 
| 520 |  |  | yoff = -MIDSCN; | 
| 521 | greg | 1.18 | if (yscan+yoff < 0) | 
| 522 |  |  | yoff = -yscan; | 
| 523 | greg | 1.14 | } else { | 
| 524 |  |  | yoff = d+.5; | 
| 525 |  |  | if (yoff+MIDSCN >= WINSIZ) | 
| 526 |  |  | yoff = WINSIZ-1-MIDSCN; | 
| 527 | greg | 1.18 | if (yscan+yoff >= ymax) | 
| 528 |  |  | yoff = ymax-1-yscan; | 
| 529 | greg | 1.14 | } | 
| 530 |  |  | } | 
| 531 | greg | 1.15 | if (nam == vbrtin) | 
| 532 | greg | 2.16 | return((*ourbright)(input[fn].scan[MIDSCN+yoff][xscan+xoff])); | 
| 533 | greg | 1.15 | n = 3; | 
| 534 |  |  | while (n--) | 
| 535 |  |  | if (nam == vcolin[n]) | 
| 536 | greg | 1.18 | return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n)); | 
| 537 | greg | 1.15 | eputs("Bad call to l_colin()!\n"); | 
| 538 | greg | 2.10 | quit(1); | 
| 539 | schorsch | 2.32 | return 1; /* pro forma return */ | 
| 540 | greg | 2.10 | } | 
| 541 |  |  |  | 
| 542 |  |  |  | 
| 543 | schorsch | 2.32 | static double | 
| 544 |  |  | l_ray(          /* return ray origin or direction */ | 
| 545 |  |  | register char   *nam | 
| 546 |  |  | ) | 
| 547 | greg | 2.10 | { | 
| 548 | greg | 2.15 | static unsigned long    ltick[MAXINP]; | 
| 549 | greg | 2.10 | static FVECT    lorg[MAXINP], ldir[MAXINP]; | 
| 550 | greg | 2.18 | static double   ldist[MAXINP]; | 
| 551 | schorsch | 2.25 | RREAL   loc[2]; | 
| 552 | greg | 2.10 | int     fn; | 
| 553 |  |  | register int    i; | 
| 554 |  |  |  | 
| 555 | greg | 2.18 | fn = argument(1) - .5; | 
| 556 | greg | 2.10 | if (fn < 0 || fn >= nfiles) { | 
| 557 |  |  | errno = EDOM; | 
| 558 |  |  | return(0.0); | 
| 559 |  |  | } | 
| 560 | greg | 2.22 | if (ltick[fn] != eclock) {              /* need to compute? */ | 
| 561 | greg | 2.10 | lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0; | 
| 562 |  |  | ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0; | 
| 563 | greg | 2.20 | ldist[fn] = -1.0; | 
| 564 | greg | 2.10 | if (input[fn].vw.type == 0) | 
| 565 | greg | 2.12 | errno = EDOM; | 
| 566 | greg | 2.10 | else { | 
| 567 | greg | 2.11 | pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan); | 
| 568 | greg | 2.18 | ldist[fn] = viewray(lorg[fn], ldir[fn], | 
| 569 |  |  | &input[fn].vw, loc[0], loc[1]); | 
| 570 | greg | 2.10 | } | 
| 571 | greg | 2.18 | ltick[fn] = eclock; | 
| 572 | greg | 2.10 | } | 
| 573 | greg | 2.18 | if (nam == vray[i=6]) | 
| 574 |  |  | return(ldist[fn]); | 
| 575 | greg | 2.10 | while (i--) | 
| 576 |  |  | if (nam == vray[i]) | 
| 577 |  |  | return(i < 3 ? lorg[fn][i] : ldir[fn][i-3]); | 
| 578 |  |  | eputs("Bad call to l_ray()!\n"); | 
| 579 | greg | 1.15 | quit(1); | 
| 580 | schorsch | 2.32 | return 1; /* pro forma return */ | 
| 581 | greg | 2.17 | } | 
| 582 |  |  |  | 
| 583 |  |  |  | 
| 584 | schorsch | 2.32 | static double | 
| 585 | greg | 2.27 | l_psize(char *nm)               /* compute pixel size in steradians */ | 
| 586 | greg | 2.17 | { | 
| 587 |  |  | static unsigned long    ltick[MAXINP]; | 
| 588 |  |  | static double   psize[MAXINP]; | 
| 589 | greg | 2.18 | FVECT   dir0, org, dirx, diry; | 
| 590 | schorsch | 2.25 | RREAL   locx[2], locy[2]; | 
| 591 | greg | 2.17 | double  d; | 
| 592 |  |  | int     fn; | 
| 593 | greg | 2.18 | register int    i; | 
| 594 | greg | 2.17 |  | 
| 595 |  |  | d = argument(1); | 
| 596 | greg | 2.18 | if (d < .5 || d >= nfiles+.5) { | 
| 597 | greg | 2.17 | errno = EDOM; | 
| 598 |  |  | return(0.0); | 
| 599 |  |  | } | 
| 600 | greg | 2.18 | fn = d - .5; | 
| 601 | greg | 2.22 | if (ltick[fn] != eclock) {              /* need to compute? */ | 
| 602 | greg | 2.18 | psize[fn] = 0.0; | 
| 603 |  |  | if (input[fn].vw.type == 0) | 
| 604 | greg | 2.17 | errno = EDOM; | 
| 605 | greg | 2.18 | else if (input[fn].vw.type != VT_PAR && | 
| 606 |  |  | funvalue(vray[6], 1, &d) >= 0) { | 
| 607 |  |  | for (i = 0; i < 3; i++) | 
| 608 |  |  | dir0[i] = funvalue(vray[3+i], 1, &d); | 
| 609 |  |  | pix2loc(locx, &input[fn].rs, xscan+1, ymax-1-yscan); | 
| 610 |  |  | pix2loc(locy, &input[fn].rs, xscan, ymax-yscan); | 
| 611 |  |  | if (viewray(org, dirx, &input[fn].vw, | 
| 612 |  |  | locx[0], locx[1]) >= 0 && | 
| 613 |  |  | viewray(org, diry, &input[fn].vw, | 
| 614 |  |  | locy[0], locy[1]) >= 0) { | 
| 615 | greg | 2.17 | /* approximate solid angle */ | 
| 616 | greg | 2.18 | for (i = 0; i < 3; i++) { | 
| 617 |  |  | dirx[i] -= dir0[i]; | 
| 618 |  |  | diry[i] -= dir0[i]; | 
| 619 |  |  | } | 
| 620 |  |  | fcross(dir0, dirx, diry); | 
| 621 | greg | 2.22 | psize[fn] = sqrt(DOT(dir0,dir0)); | 
| 622 | greg | 2.17 | } | 
| 623 |  |  | } | 
| 624 | greg | 2.18 | ltick[fn] = eclock; | 
| 625 |  |  | } | 
| 626 | greg | 2.17 | return(psize[fn]); | 
| 627 | greg | 1.1 | } | 
| 628 |  |  |  | 
| 629 |  |  |  | 
| 630 | schorsch | 2.29 | extern void | 
| 631 |  |  | wputs(char      *msg) | 
| 632 | greg | 1.1 | { | 
| 633 |  |  | if (!nowarn) | 
| 634 |  |  | eputs(msg); | 
| 635 |  |  | } | 
| 636 |  |  |  | 
| 637 |  |  |  | 
| 638 | schorsch | 2.29 | extern void | 
| 639 |  |  | eputs(char *msg) | 
| 640 | greg | 1.1 | { | 
| 641 |  |  | fputs(msg, stderr); | 
| 642 |  |  | } | 
| 643 |  |  |  | 
| 644 |  |  |  | 
| 645 | schorsch | 2.29 | extern void | 
| 646 |  |  | quit(int code)          /* exit gracefully */ | 
| 647 | greg | 2.4 | { | 
| 648 | greg | 2.5 | register int  i; | 
| 649 |  |  | /* close input files */ | 
| 650 |  |  | for (i = 0; i < nfiles; i++) | 
| 651 | greg | 2.6 | if (input[i].name == Command) | 
| 652 |  |  | pclose(input[i].fp); | 
| 653 |  |  | else | 
| 654 |  |  | fclose(input[i].fp); | 
| 655 | greg | 1.1 | exit(code); | 
| 656 |  |  | } |