| 1 | < | /* Copyright (c) 1993 Regents of the University of California */ | 
| 1 | > | /* Copyright (c) 1999 Silicon Graphics, Inc. */ | 
| 2 |  |  | 
| 3 |  | #ifndef lint | 
| 4 | < | static char SCCSid[] = "$SunId$ LBL"; | 
| 4 | > | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  | #endif | 
| 6 |  |  | 
| 7 |  | /* | 
| 20 |  |  | 
| 21 |  | #include "resolu.h" | 
| 22 |  |  | 
| 23 | + | #define LOG2            0.69314718055994530942 | 
| 24 | + |  | 
| 25 |  | #define pscan(y)        (ourpict+(y)*hresolu) | 
| 26 | + | #define sscan(y)        (ourspict+(y)*hresolu) | 
| 27 | + | #define wscan(y)        (ourweigh+(y)*hresolu) | 
| 28 |  | #define zscan(y)        (ourzbuf+(y)*hresolu) | 
| 29 | + | #define bscan(y)        (ourbpict+(y)*hresolu) | 
| 30 | + | #define averaging       (ourweigh != NULL) | 
| 31 | + | #define blurring        (ourbpict != NULL) | 
| 32 | + | #define usematrix       (hasmatrix & !averaging) | 
| 33 | + | #define zisnorm         (!usematrix | ourview.type != VT_PER) | 
| 34 |  |  | 
| 35 | + | #define MAXWT           1000.           /* maximum pixel weight (averaging) */ | 
| 36 | + |  | 
| 37 |  | #define F_FORE          1               /* fill foreground */ | 
| 38 |  | #define F_BACK          2               /* fill background */ | 
| 39 |  |  | 
| 40 |  | #define PACKSIZ         256             /* max. calculation packet size */ | 
| 41 |  |  | 
| 42 | < | #define RTCOM           "rtrace -h- -ovl -fff " | 
| 42 | > | #define RTCOM           "rtrace -h- -ovl -fff -ld- -i- -I- " | 
| 43 |  |  | 
| 44 |  | #define ABS(x)          ((x)>0?(x):-(x)) | 
| 45 |  |  | 
| 46 |  | struct position {int x,y; float z;}; | 
| 47 |  |  | 
| 48 | + | #define NSTEPS          64              /* number steps in overlap prescan */ | 
| 49 | + | #define MINSTEP         4               /* minimum worthwhile preview step */ | 
| 50 | + |  | 
| 51 | + | struct bound {int min,max;}; | 
| 52 | + |  | 
| 53 |  | VIEW    ourview = STDVIEW;              /* desired view */ | 
| 54 |  | int     hresolu = 512;                  /* horizontal resolution */ | 
| 55 |  | int     vresolu = 512;                  /* vertical resolution */ | 
| 57 |  |  | 
| 58 |  | double  zeps = .02;                     /* allowed z epsilon */ | 
| 59 |  |  | 
| 60 | < | COLR    *ourpict;                       /* output picture */ | 
| 60 | > | COLR    *ourpict;                       /* output picture (COLR's) */ | 
| 61 | > | COLOR   *ourspict;                      /* output pixel sums (averaging) */ | 
| 62 | > | float   *ourweigh = NULL;               /* output pixel weights (averaging) */ | 
| 63 |  | float   *ourzbuf;                       /* corresponding z-buffer */ | 
| 64 | + | COLOR   *ourbpict = NULL;               /* blurred picture (view averaging) */ | 
| 65 |  |  | 
| 66 | + | VIEW    avgview;                        /* average view for -B option */ | 
| 67 | + | int     nvavg;                          /* number of views averaged */ | 
| 68 | + |  | 
| 69 |  | char    *progname; | 
| 70 |  |  | 
| 71 |  | int     fillo = F_FORE|F_BACK;          /* selected fill options */ | 
| 73 |  | extern int      backfill(), rcalfill(); /* fill functions */ | 
| 74 |  | int     (*fillfunc)() = backfill;       /* selected fill function */ | 
| 75 |  | COLR    backcolr = BLKCOLR;             /* background color */ | 
| 76 | + | COLOR   backcolor = BLKCOLOR;           /* background color (float) */ | 
| 77 |  | double  backz = 0.0;                    /* background z value */ | 
| 78 | < | int     normdist = 1;                   /* normalized distance? */ | 
| 79 | < | double  ourexp = -1;                    /* output picture exposure */ | 
| 78 | > | int     normdist = 1;                   /* i/o normalized distance? */ | 
| 79 | > | char    ourfmt[LPICFMT+1] = PICFMT;     /* original picture format */ | 
| 80 | > | double  ourexp = -1;                    /* original picture exposure */ | 
| 81 | > | int     expadj = 0;                     /* exposure adjustment (f-stops) */ | 
| 82 | > | double  rexpadj = 1;                    /* real exposure adjustment */ | 
| 83 |  |  | 
| 84 | < | VIEW    theirview = STDVIEW;            /* input view */ | 
| 84 | > | VIEW    theirview;                      /* input view */ | 
| 85 |  | int     gotview;                        /* got input view? */ | 
| 86 |  | int     wrongformat = 0;                /* input in another format? */ | 
| 87 |  | RESOLU  tresolu;                        /* input resolution */ | 
| 88 |  | double  theirexp;                       /* input picture exposure */ | 
| 89 | < | double  theirs2ours[4][4];              /* transformation matrix */ | 
| 89 | > | MAT4    theirs2ours;                    /* transformation matrix */ | 
| 90 |  | int     hasmatrix = 0;                  /* has transformation matrix */ | 
| 91 |  |  | 
| 92 |  | int     PDesc[3] = {-1,-1,-1};          /* rtrace process descriptor */ | 
| 93 |  | #define childpid        (PDesc[2]) | 
| 94 |  | unsigned short  queue[PACKSIZ][2];      /* pending pixels */ | 
| 95 |  | int     packsiz;                        /* actual packet size */ | 
| 96 | < | int     queuesiz;                       /* number of pixels pending */ | 
| 96 | > | int     queuesiz = 0;                   /* number of pixels pending */ | 
| 97 |  |  | 
| 98 | + | extern double   movepixel(); | 
| 99 |  |  | 
| 100 | + |  | 
| 101 |  | main(argc, argv)                        /* interpolate pictures */ | 
| 102 |  | int     argc; | 
| 103 |  | char    *argv[]; | 
| 104 |  | { | 
| 105 | < | #define  check(ol,al)           if (argv[i][ol] || \ | 
| 106 | < | badarg(argc-i-1,argv+i+1,al)) \ | 
| 105 | > | #define  check(ol,al)           if (argv[an][ol] || \ | 
| 106 | > | badarg(argc-an-1,argv+an+1,al)) \ | 
| 107 |  | goto badopt | 
| 108 |  | int     gotvfile = 0; | 
| 109 | + | int     doavg = -1; | 
| 110 | + | int     doblur = 0; | 
| 111 |  | char    *zfile = NULL; | 
| 112 | < | char    *err; | 
| 113 | < | int     i, rval; | 
| 112 | > | char    *expcomp = NULL; | 
| 113 | > | int     i, an, rval; | 
| 114 |  |  | 
| 115 |  | progname = argv[0]; | 
| 116 |  |  | 
| 117 | < | for (i = 1; i < argc && argv[i][0] == '-'; i++) { | 
| 118 | < | rval = getviewopt(&ourview, argc-i, argv+i); | 
| 117 | > | for (an = 1; an < argc && argv[an][0] == '-'; an++) { | 
| 118 | > | rval = getviewopt(&ourview, argc-an, argv+an); | 
| 119 |  | if (rval >= 0) { | 
| 120 | < | i += rval; | 
| 120 | > | an += rval; | 
| 121 |  | continue; | 
| 122 |  | } | 
| 123 | < | switch (argv[i][1]) { | 
| 123 | > | switch (argv[an][1]) { | 
| 124 | > | case 'e':                               /* exposure */ | 
| 125 | > | check(2,"f"); | 
| 126 | > | expcomp = argv[++an]; | 
| 127 | > | break; | 
| 128 |  | case 't':                               /* threshold */ | 
| 129 |  | check(2,"f"); | 
| 130 | < | zeps = atof(argv[++i]); | 
| 130 | > | zeps = atof(argv[++an]); | 
| 131 |  | break; | 
| 132 | + | case 'a':                               /* average */ | 
| 133 | + | check(2,NULL); | 
| 134 | + | doavg = 1; | 
| 135 | + | break; | 
| 136 | + | case 'B':                               /* blur views */ | 
| 137 | + | check(2,NULL); | 
| 138 | + | doblur = 1; | 
| 139 | + | break; | 
| 140 | + | case 'q':                               /* quick (no avg.) */ | 
| 141 | + | check(2,NULL); | 
| 142 | + | doavg = 0; | 
| 143 | + | break; | 
| 144 |  | case 'n':                               /* dist. normalized? */ | 
| 145 |  | check(2,NULL); | 
| 146 |  | normdist = !normdist; | 
| 147 |  | break; | 
| 148 |  | case 'f':                               /* fill type */ | 
| 149 | < | switch (argv[i][2]) { | 
| 149 | > | switch (argv[an][2]) { | 
| 150 |  | case '0':                               /* none */ | 
| 151 |  | check(3,NULL); | 
| 152 |  | fillo = 0; | 
| 165 |  | break; | 
| 166 |  | case 's':                               /* sample */ | 
| 167 |  | check(3,"i"); | 
| 168 | < | fillsamp = atoi(argv[++i]); | 
| 168 | > | fillsamp = atoi(argv[++an]); | 
| 169 |  | break; | 
| 170 |  | case 'c':                               /* color */ | 
| 171 |  | check(3,"fff"); | 
| 172 |  | fillfunc = backfill; | 
| 173 | < | setcolr(backcolr, atof(argv[i+1]), | 
| 174 | < | atof(argv[i+2]), atof(argv[i+3])); | 
| 175 | < | i += 3; | 
| 173 | > | setcolor(backcolor, atof(argv[an+1]), | 
| 174 | > | atof(argv[an+2]), atof(argv[an+3])); | 
| 175 | > | setcolr(backcolr, colval(backcolor,RED), | 
| 176 | > | colval(backcolor,GRN), | 
| 177 | > | colval(backcolor,BLU)); | 
| 178 | > | an += 3; | 
| 179 |  | break; | 
| 180 |  | case 'z':                               /* z value */ | 
| 181 |  | check(3,"f"); | 
| 182 |  | fillfunc = backfill; | 
| 183 | < | backz = atof(argv[++i]); | 
| 183 | > | backz = atof(argv[++an]); | 
| 184 |  | break; | 
| 185 |  | case 'r':                               /* rtrace */ | 
| 186 |  | check(3,"s"); | 
| 187 |  | fillfunc = rcalfill; | 
| 188 | < | calstart(RTCOM, argv[++i]); | 
| 188 | > | calstart(RTCOM, argv[++an]); | 
| 189 |  | break; | 
| 190 |  | default: | 
| 191 |  | goto badopt; | 
| 193 |  | break; | 
| 194 |  | case 'z':                               /* z file */ | 
| 195 |  | check(2,"s"); | 
| 196 | < | zfile = argv[++i]; | 
| 196 | > | zfile = argv[++an]; | 
| 197 |  | break; | 
| 198 |  | case 'x':                               /* x resolution */ | 
| 199 |  | check(2,"i"); | 
| 200 | < | hresolu = atoi(argv[++i]); | 
| 200 | > | hresolu = atoi(argv[++an]); | 
| 201 |  | break; | 
| 202 |  | case 'y':                               /* y resolution */ | 
| 203 |  | check(2,"i"); | 
| 204 | < | vresolu = atoi(argv[++i]); | 
| 204 | > | vresolu = atoi(argv[++an]); | 
| 205 |  | break; | 
| 206 |  | case 'p':                               /* pixel aspect */ | 
| 207 | < | if (argv[i][2] != 'a') | 
| 207 | > | if (argv[an][2] != 'a') | 
| 208 |  | goto badopt; | 
| 209 |  | check(3,"f"); | 
| 210 | < | pixaspect = atof(argv[++i]); | 
| 210 | > | pixaspect = atof(argv[++an]); | 
| 211 |  | break; | 
| 212 |  | case 'v':                               /* view file */ | 
| 213 | < | if (argv[i][2] != 'f') | 
| 213 | > | if (argv[an][2] != 'f') | 
| 214 |  | goto badopt; | 
| 215 |  | check(3,"s"); | 
| 216 | < | gotvfile = viewfile(argv[++i], &ourview, 0, 0); | 
| 216 | > | gotvfile = viewfile(argv[++an], &ourview, 0, 0); | 
| 217 |  | if (gotvfile < 0) | 
| 218 | < | syserror(argv[i]); | 
| 218 | > | syserror(argv[an]); | 
| 219 |  | else if (gotvfile == 0) { | 
| 220 |  | fprintf(stderr, "%s: bad view file\n", | 
| 221 | < | argv[i]); | 
| 221 | > | argv[an]); | 
| 222 |  | exit(1); | 
| 223 |  | } | 
| 224 |  | break; | 
| 225 |  | default: | 
| 226 |  | badopt: | 
| 227 |  | fprintf(stderr, "%s: command line error at '%s'\n", | 
| 228 | < | progname, argv[i]); | 
| 228 | > | progname, argv[an]); | 
| 229 |  | goto userr; | 
| 230 |  | } | 
| 231 |  | } | 
| 232 |  | /* check arguments */ | 
| 233 | < | if ((argc-i)%2) | 
| 233 | > | if ((argc-an)%2) | 
| 234 |  | goto userr; | 
| 235 |  | if (fillsamp == 1) | 
| 236 |  | fillo &= ~F_BACK; | 
| 237 | + | if (doavg < 0) | 
| 238 | + | doavg = (argc-an) > 2; | 
| 239 | + | if (expcomp != NULL) | 
| 240 | + | if (expcomp[0] == '+' | expcomp[0] == '-') { | 
| 241 | + | expadj = atof(expcomp) + (expcomp[0]=='+' ? .5 : -.5); | 
| 242 | + | if (doavg | doblur) | 
| 243 | + | rexpadj = pow(2.0, atof(expcomp)); | 
| 244 | + | else | 
| 245 | + | rexpadj = pow(2.0, (double)expadj); | 
| 246 | + | } else { | 
| 247 | + | if (!isflt(expcomp)) | 
| 248 | + | goto userr; | 
| 249 | + | rexpadj = atof(expcomp); | 
| 250 | + | expadj = log(rexpadj)/LOG2 + (rexpadj>1 ? .5 : -.5); | 
| 251 | + | if (!(doavg | doblur)) | 
| 252 | + | rexpadj = pow(2.0, (double)expadj); | 
| 253 | + | } | 
| 254 |  | /* set view */ | 
| 255 | < | if (err = setview(&ourview)) { | 
| 256 | < | fprintf(stderr, "%s: %s\n", progname, err); | 
| 255 | > | if (nextview(doblur ? stdin : (FILE *)NULL) == EOF) { | 
| 256 | > | fprintf(stderr, "%s: no view on standard input!\n", | 
| 257 | > | progname); | 
| 258 |  | exit(1); | 
| 259 |  | } | 
| 260 |  | normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu); | 
| 261 |  | /* allocate frame */ | 
| 262 | < | ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR)); | 
| 262 | > | if (doavg) { | 
| 263 | > | ourspict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR)); | 
| 264 | > | ourweigh = (float *)bmalloc(hresolu*vresolu*sizeof(float)); | 
| 265 | > | if (ourspict == NULL | ourweigh == NULL) | 
| 266 | > | syserror(progname); | 
| 267 | > | } else { | 
| 268 | > | ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR)); | 
| 269 | > | if (ourpict == NULL) | 
| 270 | > | syserror(progname); | 
| 271 | > | } | 
| 272 | > | if (doblur) { | 
| 273 | > | ourbpict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR)); | 
| 274 | > | if (ourbpict == NULL) | 
| 275 | > | syserror(progname); | 
| 276 | > | } | 
| 277 |  | ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float)); | 
| 278 | < | if (ourpict == NULL || ourzbuf == NULL) | 
| 278 | > | if (ourzbuf == NULL) | 
| 279 |  | syserror(progname); | 
| 280 | < | bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); | 
| 281 | < | /* get input */ | 
| 282 | < | for ( ; i < argc; i += 2) | 
| 283 | < | addpicture(argv[i], argv[i+1]); | 
| 284 | < | /* fill in spaces */ | 
| 285 | < | if (fillo&F_BACK) | 
| 286 | < | backpicture(fillfunc, fillsamp); | 
| 287 | < | else | 
| 288 | < | fillpicture(fillfunc); | 
| 280 | > | /* new header */ | 
| 281 | > | newheader("RADIANCE", stdout); | 
| 282 | > | /* run pictures */ | 
| 283 | > | do { | 
| 284 | > | bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); | 
| 285 | > | for (i = an; i < argc; i += 2) | 
| 286 | > | addpicture(argv[i], argv[i+1]); | 
| 287 | > | if (fillo&F_BACK)                       /* fill in spaces */ | 
| 288 | > | backpicture(fillfunc, fillsamp); | 
| 289 | > | else | 
| 290 | > | fillpicture(fillfunc); | 
| 291 | > | /* aft clipping */ | 
| 292 | > | clipaft(); | 
| 293 | > | } while (addblur() && nextview(stdin) != EOF); | 
| 294 |  | /* close calculation */ | 
| 295 |  | caldone(); | 
| 296 |  | /* add to header */ | 
| 297 |  | printargs(argc, argv, stdout); | 
| 298 | < | if (gotvfile) { | 
| 298 | > | compavgview(); | 
| 299 | > | if (doblur | gotvfile) { | 
| 300 |  | fputs(VIEWSTR, stdout); | 
| 301 | < | fprintview(&ourview, stdout); | 
| 301 | > | fprintview(&avgview, stdout); | 
| 302 |  | putc('\n', stdout); | 
| 303 |  | } | 
| 304 | < | if (pixaspect < .99 || pixaspect > 1.01) | 
| 304 | > | if (pixaspect < .99 | pixaspect > 1.01) | 
| 305 |  | fputaspect(pixaspect, stdout); | 
| 306 | < | if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005)) | 
| 306 | > | if (ourexp > 0) | 
| 307 | > | ourexp *= rexpadj; | 
| 308 | > | else | 
| 309 | > | ourexp = rexpadj; | 
| 310 | > | if (ourexp < .995 | ourexp > 1.005) | 
| 311 |  | fputexpos(ourexp, stdout); | 
| 312 | < | fputformat(COLRFMT, stdout); | 
| 312 | > | if (strcmp(ourfmt, PICFMT))             /* print format if known */ | 
| 313 | > | fputformat(ourfmt, stdout); | 
| 314 |  | putc('\n', stdout); | 
| 315 |  | /* write picture */ | 
| 316 |  | writepicture(); | 
| 321 |  | exit(0); | 
| 322 |  | userr: | 
| 323 |  | fprintf(stderr, | 
| 324 | < | "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n", | 
| 324 | > | "Usage: %s [view opts][-t eps][-z zout][-e spec][-B][-a|-q][-fT][-n] pfile zspec ..\n", | 
| 325 |  | progname); | 
| 326 |  | exit(1); | 
| 327 |  | #undef check | 
| 328 |  | } | 
| 329 |  |  | 
| 330 |  |  | 
| 331 | + | int | 
| 332 |  | headline(s)                             /* process header string */ | 
| 333 |  | char    *s; | 
| 334 |  | { | 
| 335 |  | char    fmt[32]; | 
| 336 |  |  | 
| 337 | < | if (isformat(s)) { | 
| 338 | < | formatval(fmt, s); | 
| 339 | < | wrongformat = strcmp(fmt, COLRFMT); | 
| 340 | < | return; | 
| 337 | > | if (isheadid(s)) | 
| 338 | > | return(0); | 
| 339 | > | if (formatval(fmt, s)) { | 
| 340 | > | if (globmatch(ourfmt, fmt)) { | 
| 341 | > | wrongformat = 0; | 
| 342 | > | strcpy(ourfmt, fmt); | 
| 343 | > | } else | 
| 344 | > | wrongformat = 1; | 
| 345 | > | return(0); | 
| 346 |  | } | 
| 347 | < | putc('\t', stdout); | 
| 348 | < | fputs(s, stdout); | 
| 349 | < |  | 
| 347 | > | if (nvavg < 2) { | 
| 348 | > | putc('\t', stdout); | 
| 349 | > | fputs(s, stdout); | 
| 350 | > | } | 
| 351 |  | if (isexpos(s)) { | 
| 352 |  | theirexp *= exposval(s); | 
| 353 | < | return; | 
| 353 | > | return(0); | 
| 354 |  | } | 
| 355 |  | if (isview(s) && sscanview(&theirview, s) > 0) | 
| 356 |  | gotview++; | 
| 357 | + | return(0); | 
| 358 |  | } | 
| 359 |  |  | 
| 360 |  |  | 
| 361 | + | nextview(fp)                            /* get and set next view */ | 
| 362 | + | FILE    *fp; | 
| 363 | + | { | 
| 364 | + | char    linebuf[256]; | 
| 365 | + | char    *err; | 
| 366 | + | register int    i; | 
| 367 | + |  | 
| 368 | + | if (fp != NULL) { | 
| 369 | + | do                      /* get new view */ | 
| 370 | + | if (fgets(linebuf, sizeof(linebuf), fp) == NULL) | 
| 371 | + | return(EOF); | 
| 372 | + | while (!isview(linebuf) || !sscanview(&ourview, linebuf)); | 
| 373 | + | } | 
| 374 | + | /* set new view */ | 
| 375 | + | if ((err = setview(&ourview)) != NULL) { | 
| 376 | + | fprintf(stderr, "%s: %s\n", progname, err); | 
| 377 | + | exit(1); | 
| 378 | + | } | 
| 379 | + | if (!nvavg) {                   /* first view */ | 
| 380 | + | copystruct(&avgview, &ourview); | 
| 381 | + | return(nvavg++); | 
| 382 | + | } | 
| 383 | + | /* add to average view */ | 
| 384 | + | for (i = 0; i < 3; i++) { | 
| 385 | + | avgview.vp[i] += ourview.vp[i]; | 
| 386 | + | avgview.vdir[i] += ourview.vdir[i]; | 
| 387 | + | avgview.vup[i] += ourview.vup[i]; | 
| 388 | + | } | 
| 389 | + | avgview.horiz += ourview.horiz; | 
| 390 | + | avgview.vert += ourview.vert; | 
| 391 | + | avgview.hoff += ourview.hoff; | 
| 392 | + | avgview.voff += ourview.voff; | 
| 393 | + | avgview.vfore += ourview.vfore; | 
| 394 | + | avgview.vaft += ourview.vaft; | 
| 395 | + | return(nvavg++); | 
| 396 | + | } | 
| 397 | + |  | 
| 398 | + |  | 
| 399 | + | compavgview()                           /* compute average view */ | 
| 400 | + | { | 
| 401 | + | register int    i; | 
| 402 | + | double  f; | 
| 403 | + |  | 
| 404 | + | if (nvavg < 2) | 
| 405 | + | return; | 
| 406 | + | f = 1.0/nvavg; | 
| 407 | + | for (i = 0; i < 3; i++) { | 
| 408 | + | avgview.vp[i] *= f; | 
| 409 | + | avgview.vdir[i] *= f; | 
| 410 | + | avgview.vup[i] *= f; | 
| 411 | + | } | 
| 412 | + | avgview.horiz *= f; | 
| 413 | + | avgview.vert *= f; | 
| 414 | + | avgview.hoff *= f; | 
| 415 | + | avgview.voff *= f; | 
| 416 | + | avgview.vfore *= f; | 
| 417 | + | avgview.vaft *= f; | 
| 418 | + | if (setview(&avgview) != NULL)          /* in case of emergency... */ | 
| 419 | + | copystruct(&avgview, &ourview); | 
| 420 | + | pixaspect = viewaspect(&avgview) * hresolu / vresolu; | 
| 421 | + | } | 
| 422 | + |  | 
| 423 | + |  | 
| 424 |  | addpicture(pfile, zspec)                /* add picture to output */ | 
| 425 |  | char    *pfile, *zspec; | 
| 426 |  | { | 
| 430 |  | COLR    *scanin; | 
| 431 |  | float   *zin; | 
| 432 |  | struct position *plast; | 
| 433 | + | struct bound    *xlim, ylim; | 
| 434 |  | int     y; | 
| 435 |  | /* open picture file */ | 
| 436 |  | if ((pfp = fopen(pfile, "r")) == NULL) | 
| 437 |  | syserror(pfile); | 
| 438 |  | /* get header with exposure and view */ | 
| 439 |  | theirexp = 1.0; | 
| 440 | + | copystruct(&theirview, &stdview); | 
| 441 |  | gotview = 0; | 
| 442 | < | printf("%s:\n", pfile); | 
| 442 | > | if (nvavg < 2) | 
| 443 | > | printf("%s:\n", pfile); | 
| 444 |  | getheader(pfp, headline, NULL); | 
| 445 |  | if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) { | 
| 446 |  | fprintf(stderr, "%s: picture format error\n", pfile); | 
| 456 |  | } | 
| 457 |  | /* compute transformation */ | 
| 458 |  | hasmatrix = pixform(theirs2ours, &theirview, &ourview); | 
| 459 | < | /* allocate scanlines */ | 
| 294 | < | scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); | 
| 459 | > | /* get z specification or file */ | 
| 460 |  | zin = (float *)malloc(scanlen(&tresolu)*sizeof(float)); | 
| 461 | < | plast = (struct position *)calloc(scanlen(&tresolu), | 
| 297 | < | sizeof(struct position)); | 
| 298 | < | if (scanin == NULL || zin == NULL || plast == NULL) | 
| 461 | > | if (zin == NULL) | 
| 462 |  | syserror(progname); | 
| 300 | – | /* get z specification or file */ | 
| 463 |  | if ((zfd = open(zspec, O_RDONLY)) == -1) { | 
| 464 |  | double  zvalue; | 
| 465 |  | register int    x; | 
| 466 | < | if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) | 
| 466 | > | if (!isflt(zspec) || (zvalue = atof(zspec)) <= 0.0) | 
| 467 |  | syserror(zspec); | 
| 306 | – | exit(1); | 
| 468 |  | for (x = scanlen(&tresolu); x-- > 0; ) | 
| 469 |  | zin[x] = zvalue; | 
| 470 |  | } | 
| 471 | < | /* load image */ | 
| 472 | < | for (y = 0; y < numscans(&tresolu); y++) { | 
| 471 | > | /* compute transferrable perimeter */ | 
| 472 | > | xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound)); | 
| 473 | > | if (xlim == NULL) | 
| 474 | > | syserror(progname); | 
| 475 | > | if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */ | 
| 476 | > | free((char *)zin); | 
| 477 | > | free((char *)xlim); | 
| 478 | > | if (zfd != -1) | 
| 479 | > | close(zfd); | 
| 480 | > | fclose(pfp); | 
| 481 | > | return; | 
| 482 | > | } | 
| 483 | > | /* allocate scanlines */ | 
| 484 | > | scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); | 
| 485 | > | plast = (struct position *)calloc(scanlen(&tresolu), | 
| 486 | > | sizeof(struct position)); | 
| 487 | > | if (scanin == NULL | plast == NULL) | 
| 488 | > | syserror(progname); | 
| 489 | > | /* skip to starting point */ | 
| 490 | > | for (y = 0; y < ylim.min; y++) | 
| 491 |  | if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { | 
| 492 |  | fprintf(stderr, "%s: read error\n", pfile); | 
| 493 |  | exit(1); | 
| 494 |  | } | 
| 495 | < | if (zfd != -1 && read(zfd,(char *)zin, | 
| 496 | < | scanlen(&tresolu)*sizeof(float)) | 
| 497 | < | < scanlen(&tresolu)*sizeof(float)) { | 
| 498 | < | fprintf(stderr, "%s: read error\n", zspec); | 
| 495 | > | if (zfd != -1 && lseek(zfd, | 
| 496 | > | (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0) | 
| 497 | > | syserror(zspec); | 
| 498 | > | /* load image */ | 
| 499 | > | for (y = ylim.min; y <= ylim.max; y++) { | 
| 500 | > | if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { | 
| 501 | > | fprintf(stderr, "%s: read error\n", pfile); | 
| 502 |  | exit(1); | 
| 503 |  | } | 
| 504 | < | addscanline(y, scanin, zin, plast); | 
| 504 | > | if (zfd != -1 && read(zfd, (char *)zin, | 
| 505 | > | scanlen(&tresolu)*sizeof(float)) | 
| 506 | > | < scanlen(&tresolu)*sizeof(float)) | 
| 507 | > | syserror(zspec); | 
| 508 | > | addscanline(xlim+y, y, scanin, zin, plast); | 
| 509 |  | } | 
| 510 |  | /* clean up */ | 
| 511 | + | free((char *)xlim); | 
| 512 |  | free((char *)scanin); | 
| 513 |  | free((char *)zin); | 
| 514 |  | free((char *)plast); | 
| 519 |  |  | 
| 520 |  |  | 
| 521 |  | pixform(xfmat, vw1, vw2)                /* compute view1 to view2 matrix */ | 
| 522 | < | register double xfmat[4][4]; | 
| 522 | > | register MAT4   xfmat; | 
| 523 |  | register VIEW   *vw1, *vw2; | 
| 524 |  | { | 
| 525 |  | double  m4t[4][4]; | 
| 526 |  |  | 
| 527 | < | if (vw1->type != VT_PER && vw1->type != VT_PAR) | 
| 527 | > | if (vw1->type != VT_PER & vw1->type != VT_PAR) | 
| 528 |  | return(0); | 
| 529 | < | if (vw2->type != VT_PER && vw2->type != VT_PAR) | 
| 529 | > | if (vw2->type != VT_PER & vw2->type != VT_PAR) | 
| 530 |  | return(0); | 
| 531 |  | setident4(xfmat); | 
| 532 |  | xfmat[0][0] = vw1->hvec[0]; | 
| 559 |  | } | 
| 560 |  |  | 
| 561 |  |  | 
| 562 | < | addscanline(y, pline, zline, lasty)     /* add scanline to output */ | 
| 562 | > | addscanline(xl, y, pline, zline, lasty) /* add scanline to output */ | 
| 563 | > | struct bound    *xl; | 
| 564 |  | int     y; | 
| 565 |  | COLR    *pline; | 
| 566 |  | float   *zline; | 
| 568 |  | { | 
| 569 |  | FVECT   pos; | 
| 570 |  | struct position lastx, newpos; | 
| 571 | + | double  wt; | 
| 572 |  | register int    x; | 
| 573 |  |  | 
| 574 |  | lastx.z = 0; | 
| 575 | < | for (x = scanlen(&tresolu); x-- > 0; ) { | 
| 575 | > | for (x = xl->max; x >= xl->min; x--) { | 
| 576 |  | pix2loc(pos, &tresolu, x, y); | 
| 577 |  | pos[2] = zline[x]; | 
| 578 | < | if (movepixel(pos) < 0) { | 
| 578 | > | if ((wt = movepixel(pos)) <= FTINY) { | 
| 579 |  | lasty[x].z = lastx.z = 0;       /* mark invalid */ | 
| 580 |  | continue; | 
| 581 |  | } | 
| 582 | + | /* add pixel to our image */ | 
| 583 |  | newpos.x = pos[0] * hresolu; | 
| 584 |  | newpos.y = pos[1] * vresolu; | 
| 585 |  | newpos.z = zline[x]; | 
| 586 | < | /* add pixel to our image */ | 
| 587 | < | if (pos[0] >= 0 && newpos.x < hresolu | 
| 588 | < | && pos[1] >= 0 && newpos.y < vresolu) { | 
| 589 | < | addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); | 
| 400 | < | lasty[x].x = lastx.x = newpos.x; | 
| 401 | < | lasty[x].y = lastx.y = newpos.y; | 
| 402 | < | lasty[x].z = lastx.z = newpos.z; | 
| 403 | < | } else | 
| 404 | < | lasty[x].z = lastx.z = 0;       /* mark invalid */ | 
| 586 | > | addpixel(&newpos, &lastx, &lasty[x], pline[x], wt, pos[2]); | 
| 587 | > | lasty[x].x = lastx.x = newpos.x; | 
| 588 | > | lasty[x].y = lastx.y = newpos.y; | 
| 589 | > | lasty[x].z = lastx.z = newpos.z; | 
| 590 |  | } | 
| 591 |  | } | 
| 592 |  |  | 
| 593 |  |  | 
| 594 | < | addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */ | 
| 594 | > | addpixel(p0, p1, p2, pix, w, z)         /* fill in pixel parallelogram */ | 
| 595 |  | struct position *p0, *p1, *p2; | 
| 596 |  | COLR    pix; | 
| 597 | + | double  w; | 
| 598 |  | double  z; | 
| 599 |  | { | 
| 600 |  | double  zt = 2.*zeps*p0->z;             /* threshold */ | 
| 601 | + | COLOR   pval;                           /* converted+weighted pixel */ | 
| 602 |  | int     s1x, s1y, s2x, s2y;             /* step sizes */ | 
| 603 |  | int     l1, l2, c1, c2;                 /* side lengths and counters */ | 
| 604 |  | int     p1isy;                          /* p0p1 along y? */ | 
| 612 |  | l1 = ABS(s1x); | 
| 613 |  | if (p1isy = (ABS(s1y) > l1)) | 
| 614 |  | l1 = ABS(s1y); | 
| 615 | + | else if (l1 < 1) | 
| 616 | + | l1 = 1; | 
| 617 |  | } else { | 
| 618 |  | l1 = s1x = s1y = 1; | 
| 619 |  | p1isy = -1; | 
| 629 |  | if (p1isy != 0 && ABS(s2x) > l2) | 
| 630 |  | l2 = ABS(s2x); | 
| 631 |  | } | 
| 632 | + | if (l2 < 1) | 
| 633 | + | l2 = 1; | 
| 634 |  | } else | 
| 635 |  | l2 = s2x = s2y = 1; | 
| 636 |  | /* fill the parallelogram */ | 
| 637 | + | if (averaging) { | 
| 638 | + | colr_color(pval, pix); | 
| 639 | + | scalecolor(pval, w); | 
| 640 | + | } | 
| 641 |  | for (c1 = l1; c1-- > 0; ) { | 
| 642 |  | x1 = p0->x + c1*s1x/l1; | 
| 643 |  | y1 = p0->y + c1*s1y/l1; | 
| 644 |  | for (c2 = l2; c2-- > 0; ) { | 
| 645 |  | x = x1 + c2*s2x/l2; | 
| 646 | < | if (x < 0 || x >= hresolu) | 
| 646 | > | if (x < 0 | x >= hresolu) | 
| 647 |  | continue; | 
| 648 |  | y = y1 + c2*s2y/l2; | 
| 649 | < | if (y < 0 || y >= vresolu) | 
| 649 | > | if (y < 0 | y >= vresolu) | 
| 650 |  | continue; | 
| 651 | < | if (zscan(y)[x] <= 0 || zscan(y)[x]-z | 
| 651 | > | if (averaging) { | 
| 652 | > | if (zscan(y)[x] <= 0 || zscan(y)[x]-z | 
| 653 |  | > zeps*zscan(y)[x]) { | 
| 654 | < | zscan(y)[x] = z; | 
| 654 | > | copycolor(sscan(y)[x], pval); | 
| 655 | > | wscan(y)[x] = w; | 
| 656 | > | zscan(y)[x] = z; | 
| 657 | > | } else if (z-zscan(y)[x] <= zeps*zscan(y)[x]) { | 
| 658 | > | addcolor(sscan(y)[x], pval); | 
| 659 | > | wscan(y)[x] += w; | 
| 660 | > | } | 
| 661 | > | } else if (zscan(y)[x] <= 0 || zscan(y)[x]-z | 
| 662 | > | > zeps*zscan(y)[x]) { | 
| 663 |  | copycolr(pscan(y)[x], pix); | 
| 664 | + | zscan(y)[x] = z; | 
| 665 |  | } | 
| 666 |  | } | 
| 667 |  | } | 
| 668 |  | } | 
| 669 |  |  | 
| 670 |  |  | 
| 671 | + | double | 
| 672 |  | movepixel(pos)                          /* reposition image point */ | 
| 673 | < | FVECT   pos; | 
| 673 | > | register FVECT  pos; | 
| 674 |  | { | 
| 675 | < | FVECT   pt, direc; | 
| 675 | > | FVECT   pt, tdir, odir; | 
| 676 | > | double  d; | 
| 677 |  |  | 
| 678 |  | if (pos[2] <= 0)                /* empty pixel */ | 
| 679 | < | return(-1); | 
| 680 | < | if (hasmatrix) { | 
| 679 | > | return(0); | 
| 680 | > | if (usematrix) { | 
| 681 |  | pos[0] += theirview.hoff - .5; | 
| 682 |  | pos[1] += theirview.voff - .5; | 
| 683 | + | if (normdist & theirview.type == VT_PER) | 
| 684 | + | d = sqrt(1. + pos[0]*pos[0]*theirview.hn2 | 
| 685 | + | + pos[1]*pos[1]*theirview.vn2); | 
| 686 | + | else | 
| 687 | + | d = 1.; | 
| 688 | + | pos[2] += d*theirview.vfore; | 
| 689 |  | if (theirview.type == VT_PER) { | 
| 690 | < | if (normdist)   /* adjust for eye-ray distance */ | 
| 478 | < | pos[2] /= sqrt( 1. | 
| 479 | < | + pos[0]*pos[0]*theirview.hn2 | 
| 480 | < | + pos[1]*pos[1]*theirview.vn2 ); | 
| 690 | > | pos[2] /= d; | 
| 691 |  | pos[0] *= pos[2]; | 
| 692 |  | pos[1] *= pos[2]; | 
| 693 |  | } | 
| 694 |  | multp3(pos, pos, theirs2ours); | 
| 695 | < | if (pos[2] <= 0) | 
| 696 | < | return(-1); | 
| 695 | > | if (pos[2] <= ourview.vfore) | 
| 696 | > | return(0); | 
| 697 |  | if (ourview.type == VT_PER) { | 
| 698 |  | pos[0] /= pos[2]; | 
| 699 |  | pos[1] /= pos[2]; | 
| 700 |  | } | 
| 701 |  | pos[0] += .5 - ourview.hoff; | 
| 702 |  | pos[1] += .5 - ourview.voff; | 
| 703 | + | pos[2] -= ourview.vfore; | 
| 704 | + | } else { | 
| 705 | + | if (viewray(pt, tdir, &theirview, pos[0], pos[1]) < -FTINY) | 
| 706 | + | return(0); | 
| 707 | + | if (!normdist & theirview.type == VT_PER)       /* adjust */ | 
| 708 | + | pos[2] *= sqrt(1. + pos[0]*pos[0]*theirview.hn2 | 
| 709 | + | + pos[1]*pos[1]*theirview.vn2); | 
| 710 | + | pt[0] += tdir[0]*pos[2]; | 
| 711 | + | pt[1] += tdir[1]*pos[2]; | 
| 712 | + | pt[2] += tdir[2]*pos[2]; | 
| 713 | + | viewloc(pos, &ourview, pt); | 
| 714 | + | if (pos[2] <= 0) | 
| 715 | + | return(0); | 
| 716 | + | } | 
| 717 | + | if (pos[0] < 0 | pos[0] >= 1-FTINY | pos[1] < 0 | pos[1] >= 1-FTINY) | 
| 718 |  | return(0); | 
| 719 | + | if (!averaging) | 
| 720 | + | return(1); | 
| 721 | + | /* compute pixel weight */ | 
| 722 | + | if (ourview.type == VT_PAR) { | 
| 723 | + | d = DOT(ourview.vdir,tdir); | 
| 724 | + | d = 1. - d*d; | 
| 725 | + | } else { | 
| 726 | + | VSUB(odir, pt, ourview.vp); | 
| 727 | + | d = DOT(odir,tdir); | 
| 728 | + | d = 1. - d*d/DOT(odir,odir); | 
| 729 |  | } | 
| 730 | < | if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0) | 
| 731 | < | return(-1); | 
| 732 | < | pt[0] += direc[0]*pos[2]; | 
| 498 | < | pt[1] += direc[1]*pos[2]; | 
| 499 | < | pt[2] += direc[2]*pos[2]; | 
| 500 | < | viewloc(pos, &ourview, pt); | 
| 501 | < | if (pos[2] <= 0) | 
| 502 | < | return(-1); | 
| 503 | < | return(0); | 
| 730 | > | if (d <= 1./MAXWT/MAXWT) | 
| 731 | > | return(MAXWT);          /* clip to maximum weight */ | 
| 732 | > | return(1./sqrt(d)); | 
| 733 |  | } | 
| 734 |  |  | 
| 735 |  |  | 
| 736 | + | getperim(xl, yl, zline, zfd)            /* compute overlapping image area */ | 
| 737 | + | register struct bound   *xl; | 
| 738 | + | struct bound    *yl; | 
| 739 | + | float   *zline; | 
| 740 | + | int     zfd; | 
| 741 | + | { | 
| 742 | + | int     step; | 
| 743 | + | FVECT   pos; | 
| 744 | + | register int    x, y; | 
| 745 | + | /* set up step size */ | 
| 746 | + | if (scanlen(&tresolu) < numscans(&tresolu)) | 
| 747 | + | step = scanlen(&tresolu)/NSTEPS; | 
| 748 | + | else | 
| 749 | + | step = numscans(&tresolu)/NSTEPS; | 
| 750 | + | if (step < MINSTEP) {                   /* not worth cropping? */ | 
| 751 | + | yl->min = 0; | 
| 752 | + | yl->max = numscans(&tresolu) - 1; | 
| 753 | + | x = scanlen(&tresolu) - 1; | 
| 754 | + | for (y = numscans(&tresolu); y--; ) { | 
| 755 | + | xl[y].min = 0; | 
| 756 | + | xl[y].max = x; | 
| 757 | + | } | 
| 758 | + | return(1); | 
| 759 | + | } | 
| 760 | + | yl->min = 32000; yl->max = 0;           /* search for points on image */ | 
| 761 | + | for (y = step - 1; y < numscans(&tresolu); y += step) { | 
| 762 | + | if (zfd != -1) { | 
| 763 | + | if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float), | 
| 764 | + | 0) < 0) | 
| 765 | + | syserror("lseek"); | 
| 766 | + | if (read(zfd, (char *)zline, | 
| 767 | + | scanlen(&tresolu)*sizeof(float)) | 
| 768 | + | < scanlen(&tresolu)*sizeof(float)) | 
| 769 | + | syserror("read"); | 
| 770 | + | } | 
| 771 | + | xl[y].min = 32000; xl[y].max = 0;               /* x max */ | 
| 772 | + | for (x = scanlen(&tresolu); (x -= step) > 0; ) { | 
| 773 | + | pix2loc(pos, &tresolu, x, y); | 
| 774 | + | pos[2] = zline[x]; | 
| 775 | + | if (movepixel(pos) > FTINY) { | 
| 776 | + | xl[y].max = x + step - 1; | 
| 777 | + | xl[y].min = x - step + 1;       /* x min */ | 
| 778 | + | if (xl[y].min < 0) | 
| 779 | + | xl[y].min = 0; | 
| 780 | + | for (x = step - 1; x < xl[y].max; x += step) { | 
| 781 | + | pix2loc(pos, &tresolu, x, y); | 
| 782 | + | pos[2] = zline[x]; | 
| 783 | + | if (movepixel(pos) > FTINY) { | 
| 784 | + | xl[y].min = x - step + 1; | 
| 785 | + | break; | 
| 786 | + | } | 
| 787 | + | } | 
| 788 | + | if (y < yl->min)                /* y limits */ | 
| 789 | + | yl->min = y - step + 1; | 
| 790 | + | yl->max = y + step - 1; | 
| 791 | + | break; | 
| 792 | + | } | 
| 793 | + | } | 
| 794 | + | /* fill in between */ | 
| 795 | + | if (y < step) { | 
| 796 | + | xl[y-1].min = xl[y].min; | 
| 797 | + | xl[y-1].max = xl[y].max; | 
| 798 | + | } else { | 
| 799 | + | if (xl[y].min < xl[y-step].min) | 
| 800 | + | xl[y-1].min = xl[y].min; | 
| 801 | + | else | 
| 802 | + | xl[y-1].min = xl[y-step].min; | 
| 803 | + | if (xl[y].max > xl[y-step].max) | 
| 804 | + | xl[y-1].max = xl[y].max; | 
| 805 | + | else | 
| 806 | + | xl[y-1].max = xl[y-step].max; | 
| 807 | + | } | 
| 808 | + | for (x = 2; x < step; x++) | 
| 809 | + | copystruct(xl+y-x, xl+y-1); | 
| 810 | + | } | 
| 811 | + | if (yl->max >= numscans(&tresolu)) | 
| 812 | + | yl->max = numscans(&tresolu) - 1; | 
| 813 | + | y -= step; | 
| 814 | + | for (x = numscans(&tresolu) - 1; x > y; x--)    /* fill bottom rows */ | 
| 815 | + | copystruct(xl+x, xl+y); | 
| 816 | + | return(yl->max >= yl->min); | 
| 817 | + | } | 
| 818 | + |  | 
| 819 | + |  | 
| 820 |  | backpicture(fill, samp)                 /* background fill algorithm */ | 
| 821 |  | int     (*fill)(); | 
| 822 |  | int     samp; | 
| 887 |  | < zscan(y)[xback] ) ) { | 
| 888 |  | if (samp > 0 && ABS(x-xback) >= samp) | 
| 889 |  | goto fillit; | 
| 890 | < | copycolr(pscan(y)[x],pscan(y)[xback]); | 
| 890 | > | if (averaging) { | 
| 891 | > | copycolor(sscan(y)[x], | 
| 892 | > | sscan(y)[xback]); | 
| 893 | > | wscan(y)[x] = wscan(y)[xback]; | 
| 894 | > | } else | 
| 895 | > | copycolr(pscan(y)[x], | 
| 896 | > | pscan(y)[xback]); | 
| 897 |  | zscan(y)[x] = zscan(y)[xback]; | 
| 898 |  | } else { | 
| 899 |  | if (samp > 0 && ABS(y-yback[x]) > samp) | 
| 900 |  | goto fillit; | 
| 901 | < | copycolr(pscan(y)[x],pscan(yback[x])[x]); | 
| 901 | > | if (averaging) { | 
| 902 | > | copycolor(sscan(y)[x], | 
| 903 | > | sscan(yback[x])[x]); | 
| 904 | > | wscan(y)[x] = | 
| 905 | > | wscan(yback[x])[x]; | 
| 906 | > | } else | 
| 907 | > | copycolr(pscan(y)[x], | 
| 908 | > | pscan(yback[x])[x]); | 
| 909 |  | zscan(y)[x] = zscan(yback[x])[x]; | 
| 910 |  | } | 
| 911 |  | continue; | 
| 934 |  | for (x = 0; x < hresolu; x++) | 
| 935 |  | if (zscan(y)[x] <= 0) | 
| 936 |  | (*fill)(x,y); | 
| 937 | + | if (fill == rcalfill) | 
| 938 | + | clearqueue(); | 
| 939 |  | } | 
| 940 |  |  | 
| 941 |  |  | 
| 942 | < | writepicture()                          /* write out picture */ | 
| 942 | > | clipaft()                       /* perform aft clipping as indicated */ | 
| 943 |  | { | 
| 944 | + | register int    x, y; | 
| 945 | + | int     adjtest = ourview.type == VT_PER & zisnorm; | 
| 946 | + | double  tstdist; | 
| 947 | + | double  yzn2, vx; | 
| 948 | + |  | 
| 949 | + | if (ourview.vaft <= FTINY) | 
| 950 | + | return(0); | 
| 951 | + | tstdist = ourview.vaft - ourview.vfore; | 
| 952 | + | for (y = 0; y < vresolu; y++) { | 
| 953 | + | if (adjtest) {                          /* adjust test */ | 
| 954 | + | yzn2 = (y+.5)/vresolu + ourview.voff - .5; | 
| 955 | + | yzn2 = 1. + yzn2*yzn2*ourview.vn2; | 
| 956 | + | tstdist = (ourview.vaft - ourview.vfore)*sqrt(yzn2); | 
| 957 | + | } | 
| 958 | + | for (x = 0; x < hresolu; x++) | 
| 959 | + | if (zscan(y)[x] > tstdist) { | 
| 960 | + | if (adjtest) { | 
| 961 | + | vx = (x+.5)/hresolu + ourview.hoff - .5; | 
| 962 | + | if (zscan(y)[x] <= (ourview.vaft - | 
| 963 | + | ourview.vfore) * | 
| 964 | + | sqrt(vx*vx*ourview.hn2 + yzn2)) | 
| 965 | + | continue; | 
| 966 | + | } | 
| 967 | + | if (averaging) | 
| 968 | + | bzero(sscan(y)[x], sizeof(COLOR)); | 
| 969 | + | else | 
| 970 | + | bzero(pscan(y)[x], sizeof(COLR)); | 
| 971 | + | zscan(y)[x] = 0.0; | 
| 972 | + | } | 
| 973 | + | } | 
| 974 | + | return(1); | 
| 975 | + | } | 
| 976 | + |  | 
| 977 | + |  | 
| 978 | + | addblur()                               /* add to blurred picture */ | 
| 979 | + | { | 
| 980 | + | COLOR   cval; | 
| 981 | + | double  d; | 
| 982 | + | register int    i; | 
| 983 | + |  | 
| 984 | + | if (!blurring) | 
| 985 | + | return(0); | 
| 986 | + | i = hresolu*vresolu; | 
| 987 | + | if (nvavg < 2) | 
| 988 | + | if (averaging) | 
| 989 | + | while (i--) { | 
| 990 | + | copycolor(ourbpict[i], ourspict[i]); | 
| 991 | + | d = 1.0/ourweigh[i]; | 
| 992 | + | scalecolor(ourbpict[i], d); | 
| 993 | + | } | 
| 994 | + | else | 
| 995 | + | while (i--) | 
| 996 | + | colr_color(ourbpict[i], ourpict[i]); | 
| 997 | + | else | 
| 998 | + | if (averaging) | 
| 999 | + | while (i--) { | 
| 1000 | + | copycolor(cval, ourspict[i]); | 
| 1001 | + | d = 1.0/ourweigh[i]; | 
| 1002 | + | scalecolor(cval, d); | 
| 1003 | + | addcolor(ourbpict[i], cval); | 
| 1004 | + | } | 
| 1005 | + | else | 
| 1006 | + | while (i--) { | 
| 1007 | + | colr_color(cval, ourpict[i]); | 
| 1008 | + | addcolor(ourbpict[i], cval); | 
| 1009 | + | } | 
| 1010 | + | /* print view */ | 
| 1011 | + | printf("VIEW%d:", nvavg); | 
| 1012 | + | fprintview(&ourview, stdout); | 
| 1013 | + | putchar('\n'); | 
| 1014 | + | return(1); | 
| 1015 | + | } | 
| 1016 | + |  | 
| 1017 | + |  | 
| 1018 | + | writepicture()                          /* write out picture (alters buffer) */ | 
| 1019 | + | { | 
| 1020 |  | int     y; | 
| 1021 | + | register int    x; | 
| 1022 | + | double  d; | 
| 1023 |  |  | 
| 1024 |  | fprtresolu(hresolu, vresolu, stdout); | 
| 1025 |  | for (y = vresolu-1; y >= 0; y--) | 
| 1026 | < | if (fwritecolrs(pscan(y), hresolu, stdout) < 0) | 
| 1027 | < | syserror(progname); | 
| 1026 | > | if (blurring) { | 
| 1027 | > | for (x = 0; x < hresolu; x++) { /* compute avg. */ | 
| 1028 | > | d = rexpadj/nvavg; | 
| 1029 | > | scalecolor(bscan(y)[x], d); | 
| 1030 | > | } | 
| 1031 | > | if (fwritescan(bscan(y), hresolu, stdout) < 0) | 
| 1032 | > | syserror(progname); | 
| 1033 | > | } else if (averaging) { | 
| 1034 | > | for (x = 0; x < hresolu; x++) { /* average pixels */ | 
| 1035 | > | d = rexpadj/wscan(y)[x]; | 
| 1036 | > | scalecolor(sscan(y)[x], d); | 
| 1037 | > | } | 
| 1038 | > | if (fwritescan(sscan(y), hresolu, stdout) < 0) | 
| 1039 | > | syserror(progname); | 
| 1040 | > | } else { | 
| 1041 | > | if (expadj) | 
| 1042 | > | shiftcolrs(pscan(y), hresolu, expadj); | 
| 1043 | > | if (fwritecolrs(pscan(y), hresolu, stdout) < 0) | 
| 1044 | > | syserror(progname); | 
| 1045 | > | } | 
| 1046 |  | } | 
| 1047 |  |  | 
| 1048 |  |  | 
| 1049 | < | writedistance(fname)                    /* write out z file */ | 
| 1049 | > | writedistance(fname)                    /* write out z file (alters buffer) */ | 
| 1050 |  | char    *fname; | 
| 1051 |  | { | 
| 1052 | < | int     donorm = normdist && ourview.type == VT_PER; | 
| 1052 | > | int     donorm = normdist & !zisnorm ? 1 : | 
| 1053 | > | ourview.type == VT_PER & !normdist & zisnorm ? -1 : 0; | 
| 1054 |  | int     fd; | 
| 1055 |  | int     y; | 
| 631 | – | float   *zout; | 
| 1056 |  |  | 
| 1057 |  | if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) | 
| 1058 |  | syserror(fname); | 
| 635 | – | if (donorm | 
| 636 | – | && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) | 
| 637 | – | syserror(progname); | 
| 1059 |  | for (y = vresolu-1; y >= 0; y--) { | 
| 1060 |  | if (donorm) { | 
| 1061 | < | double  vx, yzn2; | 
| 1061 | > | double  vx, yzn2, d; | 
| 1062 |  | register int    x; | 
| 1063 |  | yzn2 = (y+.5)/vresolu + ourview.voff - .5; | 
| 1064 |  | yzn2 = 1. + yzn2*yzn2*ourview.vn2; | 
| 1065 |  | for (x = 0; x < hresolu; x++) { | 
| 1066 |  | vx = (x+.5)/hresolu + ourview.hoff - .5; | 
| 1067 | < | zout[x] = zscan(y)[x] | 
| 1068 | < | * sqrt(vx*vx*ourview.hn2 + yzn2); | 
| 1067 | > | d = sqrt(vx*vx*ourview.hn2 + yzn2); | 
| 1068 | > | if (donorm > 0) | 
| 1069 | > | zscan(y)[x] *= d; | 
| 1070 | > | else | 
| 1071 | > | zscan(y)[x] /= d; | 
| 1072 |  | } | 
| 1073 | < | } else | 
| 1074 | < | zout = zscan(y); | 
| 651 | < | if (write(fd, (char *)zout, hresolu*sizeof(float)) | 
| 1073 | > | } | 
| 1074 | > | if (write(fd, (char *)zscan(y), hresolu*sizeof(float)) | 
| 1075 |  | < hresolu*sizeof(float)) | 
| 1076 |  | syserror(fname); | 
| 1077 |  | } | 
| 655 | – | if (donorm) | 
| 656 | – | free((char *)zout); | 
| 1078 |  | close(fd); | 
| 1079 |  | } | 
| 1080 |  |  | 
| 1081 |  |  | 
| 661 | – | isfloat(s)                              /* see if string is floating number */ | 
| 662 | – | register char   *s; | 
| 663 | – | { | 
| 664 | – | for ( ; *s; s++) | 
| 665 | – | if ((*s < '0' || *s > '9') && *s != '.' && *s != '-' | 
| 666 | – | && *s != 'e' && *s != 'E' && *s != '+') | 
| 667 | – | return(0); | 
| 668 | – | return(1); | 
| 669 | – | } | 
| 670 | – |  | 
| 671 | – |  | 
| 1082 |  | backfill(x, y)                          /* fill pixel with background */ | 
| 1083 |  | int     x, y; | 
| 1084 |  | { | 
| 1085 | < | register BYTE   *dest = pscan(y)[x]; | 
| 1086 | < |  | 
| 1087 | < | copycolr(dest, backcolr); | 
| 1085 | > | if (averaging) { | 
| 1086 | > | copycolor(sscan(y)[x], backcolor); | 
| 1087 | > | wscan(y)[x] = 1; | 
| 1088 | > | } else | 
| 1089 | > | copycolr(pscan(y)[x], backcolr); | 
| 1090 |  | zscan(y)[x] = backz; | 
| 1091 |  | } | 
| 1092 |  |  | 
| 1108 |  | cp = combuf; | 
| 1109 |  | wp = argv; | 
| 1110 |  | for ( ; ; ) { | 
| 1111 | < | while (isspace(*cp)) cp++; | 
| 1112 | < | if (!*cp) break; | 
| 1113 | < | *wp++ = cp; | 
| 1114 | < | while (!isspace(*cp)) | 
| 1115 | < | if (!*cp++) goto done; | 
| 1116 | < | *cp++ = '\0'; | 
| 1111 | > | while (isspace(*cp))    /* nullify spaces */ | 
| 1112 | > | *cp++ = '\0'; | 
| 1113 | > | if (!*cp)               /* all done? */ | 
| 1114 | > | break; | 
| 1115 | > | *wp++ = cp;             /* add argument to list */ | 
| 1116 | > | while (*++cp && !isspace(*cp)) | 
| 1117 | > | ; | 
| 1118 |  | } | 
| 706 | – | done: | 
| 1119 |  | *wp = NULL; | 
| 1120 |  | /* start process */ | 
| 1121 |  | if ((rval = open_process(PDesc, argv)) < 0) | 
| 1159 |  | float   fbuf[6*(PACKSIZ+1)]; | 
| 1160 |  | register float  *fbp; | 
| 1161 |  | register int    i; | 
| 1162 | + | double  vx, vy; | 
| 1163 |  |  | 
| 1164 |  | if (queuesiz == 0) | 
| 1165 |  | return; | 
| 1173 |  | } | 
| 1174 |  | /* mark end and get results */ | 
| 1175 |  | bzero((char *)fbp, 6*sizeof(float)); | 
| 1176 | < | if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz, | 
| 1176 | > | if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*(queuesiz+1), | 
| 1177 |  | 6*sizeof(float)*(queuesiz+1)) != | 
| 1178 | < | 4*sizeof(float)*queuesiz) { | 
| 1178 | > | 4*sizeof(float)*(queuesiz+1)) { | 
| 1179 |  | fprintf(stderr, "%s: error reading from rtrace process\n", | 
| 1180 |  | progname); | 
| 1181 |  | exit(1); | 
| 1187 |  | fbp[1] *= ourexp; | 
| 1188 |  | fbp[2] *= ourexp; | 
| 1189 |  | } | 
| 1190 | < | setcolr(pscan(queue[i][1])[queue[i][0]], | 
| 1191 | < | fbp[0], fbp[1], fbp[2]); | 
| 1192 | < | zscan(queue[i][1])[queue[i][0]] = fbp[3]; | 
| 1190 | > | if (averaging) { | 
| 1191 | > | setcolor(sscan(queue[i][1])[queue[i][0]], | 
| 1192 | > | fbp[0], fbp[1], fbp[2]); | 
| 1193 | > | wscan(queue[i][1])[queue[i][0]] = 1; | 
| 1194 | > | } else | 
| 1195 | > | setcolr(pscan(queue[i][1])[queue[i][0]], | 
| 1196 | > | fbp[0], fbp[1], fbp[2]); | 
| 1197 | > | if (zisnorm) | 
| 1198 | > | zscan(queue[i][1])[queue[i][0]] = fbp[3]; | 
| 1199 | > | else { | 
| 1200 | > | vx = (queue[i][0]+.5)/hresolu + ourview.hoff - .5; | 
| 1201 | > | vy = (queue[i][1]+.5)/vresolu + ourview.voff - .5; | 
| 1202 | > | zscan(queue[i][1])[queue[i][0]] = fbp[3] / sqrt(1. + | 
| 1203 | > | vx*vx*ourview.hn2 + vy*vy*ourview.vn2); | 
| 1204 | > | } | 
| 1205 |  | fbp += 4; | 
| 1206 |  | } | 
| 1207 |  | queuesiz = 0; |