| 1 | < | /* Copyright (c) 1993 Regents of the University of California */ | 
| 1 | > | /* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 2 |  |  | 
| 3 |  | #ifndef lint | 
| 4 | < | static char SCCSid[] = "$SunId$ LBL"; | 
| 4 | > | static char SCCSid[] = "$SunId$ SGI"; | 
| 5 |  | #endif | 
| 6 |  |  | 
| 7 |  | /* | 
| 27 |  | #include  <X11/Xutil.h> | 
| 28 |  | #include  <X11/Xatom.h> | 
| 29 |  |  | 
| 30 | < | #include  "color.h" | 
| 30 | > | #undef  NOPROTO | 
| 31 | > | #define NOPROTO 1 | 
| 32 | > | #include  "tonemap.h" | 
| 33 |  | #include  "view.h" | 
| 34 |  | #include  "x11raster.h" | 
| 35 |  | #include  "random.h" | 
| 36 |  | #include  "resolu.h" | 
| 37 |  |  | 
| 36 | – | #ifdef  __alpha | 
| 37 | – | #define  int4           int | 
| 38 | – | #endif | 
| 38 |  | #ifndef  int4 | 
| 39 | < | #define  int4           long | 
| 39 | > | #define  int4           int             /* most int's are 32-bit */ | 
| 40 |  | #endif | 
| 41 |  |  | 
| 42 |  | #define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 47 |  |  | 
| 48 |  | #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */ | 
| 49 |  |  | 
| 50 | + | #define  FIXWEIGHT      20              /* weight to add for fixation points */ | 
| 51 | + |  | 
| 52 |  | #define  ourscreen      DefaultScreen(thedisplay) | 
| 53 |  | #define  ourroot        RootWindow(thedisplay,ourscreen) | 
| 54 |  |  | 
| 57 |  | #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) | 
| 58 |  |  | 
| 59 |  | double  gamcor = 2.2;                   /* gamma correction */ | 
| 60 | + | char  *gamstr = NULL;                   /* gamma value override */ | 
| 61 |  |  | 
| 62 |  | int  dither = 1;                        /* dither colors? */ | 
| 63 |  | int  fast = 0;                          /* keep picture in Pixmap? */ | 
| 75 |  | int  yoff = 0;                          /* y image offset */ | 
| 76 |  |  | 
| 77 |  | int  parent = 0;                        /* number of children, -1 if child */ | 
| 78 | + | int  sequential = 0;                    /* display images in sequence */ | 
| 79 |  |  | 
| 80 | + | char  *tout = "od";                     /* output of 't' command */ | 
| 81 | + | int  tinterv = 0;                       /* interval between mouse reports */ | 
| 82 | + |  | 
| 83 | + | int  tmflags = TM_F_LINEAR;             /* tone mapping flags */ | 
| 84 | + |  | 
| 85 |  | VIEW  ourview = STDVIEW;                /* image view parameters */ | 
| 86 |  | int  gotview = 0;                       /* got parameters from file */ | 
| 87 |  |  | 
| 88 |  | COLR  *scanline;                        /* scan line buffer */ | 
| 89 | + | TMbright  *lscan;                       /* encoded luminance scanline */ | 
| 90 | + | BYTE  *cscan;                           /* encoded chroma scanline */ | 
| 91 | + | BYTE  *pscan;                           /* compute pixel scanline */ | 
| 92 |  |  | 
| 93 |  | RESOLU  inpres;                         /* input resolution and ordering */ | 
| 94 |  | int  xmax, ymax;                        /* picture dimensions */ | 
| 123 |  |  | 
| 124 |  | char  errmsg[128]; | 
| 125 |  |  | 
| 126 | < | extern BYTE  clrtab[256][3];            /* global color map */ | 
| 126 | > | BYTE  clrtab[256][3];                   /* global color map */ | 
| 127 |  |  | 
| 128 |  | extern long  ftell(); | 
| 129 |  |  | 
| 130 | + | extern char  *getenv(); | 
| 131 | + |  | 
| 132 |  | Display  *thedisplay; | 
| 133 |  | Atom  closedownAtom, wmProtocolsAtom; | 
| 134 |  |  | 
| 135 | < | int  noop() {} | 
| 135 | > | int  sigrecv; | 
| 136 |  |  | 
| 137 | + | int  onsig() { sigrecv++; } | 
| 138 |  |  | 
| 139 | + |  | 
| 140 |  | main(argc, argv) | 
| 141 |  | int  argc; | 
| 142 |  | char  *argv[]; | 
| 143 |  | { | 
| 129 | – | extern char  *getenv(); | 
| 130 | – | char  *gv; | 
| 144 |  | int  headline(); | 
| 145 |  | int  i; | 
| 146 |  | int  pid; | 
| 147 |  |  | 
| 148 |  | progname = argv[0]; | 
| 136 | – | if ((gv = getenv("GAMMA")) != NULL) | 
| 137 | – | gamcor = atof(gv); | 
| 149 |  |  | 
| 150 |  | for (i = 1; i < argc; i++) | 
| 151 |  | if (argv[i][0] == '-') | 
| 152 |  | switch (argv[i][1]) { | 
| 153 | < | case 'c': | 
| 153 | > | case 'c':                       /* number of colors */ | 
| 154 |  | maxcolors = atoi(argv[++i]); | 
| 155 |  | break; | 
| 156 | < | case 'b': | 
| 156 | > | case 'b':                       /* greyscale only */ | 
| 157 |  | greyscale = !greyscale; | 
| 158 |  | break; | 
| 159 | < | case 'm': | 
| 159 | > | case 'm':                       /* monochrome */ | 
| 160 | > | greyscale = 1; | 
| 161 |  | maxcolors = 2; | 
| 162 |  | break; | 
| 163 | < | case 'd': | 
| 163 | > | case 'd':                       /* display or dither */ | 
| 164 |  | if (argv[i][2] == 'i') | 
| 165 |  | dispname = argv[++i]; | 
| 166 |  | else | 
| 167 |  | dither = !dither; | 
| 168 |  | break; | 
| 169 | < | case 'f': | 
| 169 | > | case 'f':                       /* save pixmap */ | 
| 170 |  | fast = !fast; | 
| 171 |  | break; | 
| 172 | < | case 'e': | 
| 173 | < | if (argv[i+1][0] != '+' && argv[i+1][0] != '-') | 
| 172 | > | case 's':                       /* one at a time */ | 
| 173 | > | sequential = !sequential; | 
| 174 | > | break; | 
| 175 | > | case 'o':                       /* 't' output */ | 
| 176 | > | tout = argv[i]+2; | 
| 177 | > | break; | 
| 178 | > | case 't':                       /* msec interval */ | 
| 179 | > | tinterv = atoi(argv[++i]); | 
| 180 | > | break; | 
| 181 | > | case 'e':                       /* exposure comp. */ | 
| 182 | > | i++; | 
| 183 | > | if (argv[i][0] == 'a') { | 
| 184 | > | tmflags = TM_F_CAMERA; | 
| 185 | > | break; | 
| 186 | > | } | 
| 187 | > | if (argv[i][0] == 'h') { | 
| 188 | > | tmflags = TM_F_HUMAN; | 
| 189 | > | break; | 
| 190 | > | } | 
| 191 | > | if (argv[i][0] != '+' && argv[i][0] != '-') | 
| 192 |  | goto userr; | 
| 193 | < | scale = atoi(argv[++i]); | 
| 193 | > | scale = atoi(argv[i]); | 
| 194 |  | break; | 
| 195 | < | case 'g': | 
| 195 | > | case 'g':                       /* gamma comp. */ | 
| 196 |  | if (argv[i][2] == 'e') | 
| 197 |  | geometry = argv[++i]; | 
| 198 |  | else | 
| 199 | < | gamcor = atof(argv[++i]); | 
| 199 | > | gamstr = argv[++i]; | 
| 200 |  | break; | 
| 201 |  | default: | 
| 202 |  | goto userr; | 
| 209 |  | if (i > argc) | 
| 210 |  | goto userr; | 
| 211 |  | while (i < argc-1) { | 
| 212 | + | sigrecv = 0; | 
| 213 | + | signal(SIGCONT, onsig); | 
| 214 |  | if ((pid=fork()) == 0) {        /* a child for each picture */ | 
| 215 |  | parent = -1; | 
| 216 |  | break; | 
| 218 |  | if (pid < 0) | 
| 219 |  | quiterr("fork failed"); | 
| 220 |  | parent++; | 
| 221 | < | signal(SIGCONT, noop); | 
| 222 | < | pause();                /* wait for wake-up call */ | 
| 221 | > | while (!sigrecv) | 
| 222 | > | pause();        /* wait for wake-up call */ | 
| 223 |  | i++; | 
| 224 |  | } | 
| 225 |  | if (i < argc) {                 /* open picture file */ | 
| 226 |  | fname = argv[i]; | 
| 227 |  | fin = fopen(fname, "r"); | 
| 228 | < | if (fin == NULL) { | 
| 229 | < | sprintf(errmsg, "cannot open file \"%s\"", fname); | 
| 198 | < | quiterr(errmsg); | 
| 199 | < | } | 
| 228 | > | if (fin == NULL) | 
| 229 | > | quiterr("cannot open picture file"); | 
| 230 |  | } | 
| 231 |  | /* get header */ | 
| 232 |  | getheader(fin, headline, NULL); | 
| 243 |  |  | 
| 244 |  | init(argc, argv);                       /* get file and open window */ | 
| 245 |  |  | 
| 216 | – | if (parent < 0) | 
| 217 | – | kill(getppid(), SIGCONT);       /* signal parent if child */ | 
| 218 | – |  | 
| 246 |  | for ( ; ; ) | 
| 247 |  | getevent();             /* main loop */ | 
| 248 |  | userr: | 
| 249 |  | fprintf(stderr, | 
| 250 | < | "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n", | 
| 250 | > | "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", | 
| 251 |  | progname); | 
| 252 |  | exit(1); | 
| 253 |  | } | 
| 260 |  |  | 
| 261 |  | if (isexpos(s)) | 
| 262 |  | exposure *= exposval(s); | 
| 263 | < | else if (isformat(s)) { | 
| 237 | < | formatval(fmt, s); | 
| 263 | > | else if (formatval(fmt, s)) | 
| 264 |  | wrongformat = strcmp(fmt, COLRFMT); | 
| 265 | < | } else if (isview(s) && sscanview(&ourview, s) > 0) | 
| 265 | > | else if (isview(s) && sscanview(&ourview, s) > 0) | 
| 266 |  | gotview++; | 
| 267 |  | } | 
| 268 |  |  | 
| 296 |  | name += i+1; | 
| 297 |  | if ((thedisplay = XOpenDisplay(dispname)) == NULL) | 
| 298 |  | quiterr("cannot open display"); | 
| 299 | + | /* set gamma value */ | 
| 300 | + | if (gamstr == NULL)             /* get it from the X server */ | 
| 301 | + | gamstr = XGetDefault(thedisplay, "radiance", "gamma"); | 
| 302 | + | if (gamstr == NULL)             /* get it from the environment */ | 
| 303 | + | gamstr = getenv("DISPLAY_GAMMA"); | 
| 304 | + | if (gamstr != NULL) | 
| 305 | + | gamcor = atof(gamstr); | 
| 306 |  | /* get best visual for default screen */ | 
| 307 |  | getbestvis(); | 
| 308 |  | /* store image */ | 
| 329 |  | } | 
| 330 |  | } | 
| 331 |  | /* open window */ | 
| 332 | + | i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; | 
| 333 |  | ourwinattr.border_pixel = ourwhite; | 
| 334 |  | ourwinattr.background_pixel = ourblack; | 
| 335 | < | ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, | 
| 336 | < | ourvis.visual, AllocNone); | 
| 335 | > | if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { | 
| 336 | > | ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); | 
| 337 | > | i |= CWColormap; | 
| 338 | > | } | 
| 339 |  | ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| | 
| 340 |  | ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; | 
| 341 |  | ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); | 
| 342 |  | wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, | 
| 343 |  | xszhints.width, xszhints.height, BORWIDTH, | 
| 344 | < | ourvis.depth, InputOutput, ourvis.visual, CWEventMask| | 
| 345 | < | CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); | 
| 344 | > | ourvis.depth, InputOutput, ourvis.visual, | 
| 345 | > | i, &ourwinattr); | 
| 346 |  | if (wind == 0) | 
| 347 |  | quiterr("cannot create window"); | 
| 348 |  | width = xmax; | 
| 349 |  | height = ymax; | 
| 350 | < | xgcv.foreground = ourblack; | 
| 315 | < | xgcv.background = ourwhite; | 
| 350 | > | /* prepare graphics drawing context */ | 
| 351 |  | if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) | 
| 352 |  | quiterr("cannot get font"); | 
| 353 | + | xgcv.foreground = ourblack; | 
| 354 | + | xgcv.background = ourwhite; | 
| 355 |  | ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| | 
| 356 |  | GCFont, &xgcv); | 
| 357 |  | xgcv.function = GXinvert; | 
| 380 |  | XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); | 
| 381 |  |  | 
| 382 |  | XMapWindow(thedisplay, wind); | 
| 346 | – | return; | 
| 383 |  | } /* end of init */ | 
| 384 |  |  | 
| 385 |  |  | 
| 386 |  | quiterr(err)            /* print message and exit */ | 
| 387 |  | char  *err; | 
| 388 |  | { | 
| 389 | < | if (err != NULL) | 
| 389 | > | register int  es; | 
| 390 | > | int  cs; | 
| 391 | > |  | 
| 392 | > | if (es = err != NULL) | 
| 393 |  | fprintf(stderr, "%s: %s: %s\n", progname, | 
| 394 |  | fname==NULL?"<stdin>":fname, err); | 
| 395 | < | if (wind) { | 
| 396 | < | XDestroyWindow(thedisplay, wind); | 
| 397 | < | XFlush(thedisplay); | 
| 398 | < | } | 
| 399 | < | while (parent > 0 && wait(0) != -1)     /* wait for any children */ | 
| 395 | > | if (thedisplay != NULL) | 
| 396 | > | XCloseDisplay(thedisplay); | 
| 397 | > | if (parent < 0 & sigrecv == 0) | 
| 398 | > | kill(getppid(), SIGCONT); | 
| 399 | > | while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ | 
| 400 | > | if (es == 0) | 
| 401 | > | es = cs>>8 & 0xff; | 
| 402 |  | parent--; | 
| 403 | < | exit(err != NULL); | 
| 403 | > | } | 
| 404 | > | exit(es); | 
| 405 |  | } | 
| 406 |  |  | 
| 407 |  |  | 
| 558 |  | xmax, ymax, 8); | 
| 559 |  | if (ourras == NULL) | 
| 560 |  | goto fail; | 
| 561 | < | if (greyscale | ourvis.class == StaticGray) | 
| 561 | > | if (greyscale) | 
| 562 |  | getgrey(); | 
| 563 |  | else | 
| 564 |  | getmapped(); | 
| 588 |  | map_rcolors(ourras, wind); | 
| 589 |  | if (fast) | 
| 590 |  | make_rpixmap(ourras, wind); | 
| 591 | + | if (!sequential & parent < 0 & sigrecv == 0) { | 
| 592 | + | kill(getppid(), SIGCONT); | 
| 593 | + | sigrecv--; | 
| 594 | + | } | 
| 595 |  | break; | 
| 596 |  | case UnmapNotify: | 
| 597 |  | if (!fast) | 
| 604 |  | case ButtonPress: | 
| 605 |  | if (xev.xbutton.state & (ShiftMask|ControlMask)) | 
| 606 |  | moveimage(&xev.xbutton); | 
| 561 | – | else if (xev.xbutton.button == Button2) | 
| 562 | – | traceray(xev.xbutton.x, xev.xbutton.y); | 
| 607 |  | else | 
| 608 | < | getbox(&xev.xbutton); | 
| 608 | > | switch (xev.xbutton.button) { | 
| 609 | > | case Button1: | 
| 610 | > | getbox(&xev.xbutton); | 
| 611 | > | break; | 
| 612 | > | case Button2: | 
| 613 | > | traceray(xev.xbutton.x, xev.xbutton.y); | 
| 614 | > | break; | 
| 615 | > | case Button3: | 
| 616 | > | trackrays(&xev.xbutton); | 
| 617 | > | break; | 
| 618 | > | } | 
| 619 |  | break; | 
| 620 |  | case ClientMessage: | 
| 621 |  | if ((xev.xclient.message_type == wmProtocolsAtom) && | 
| 626 |  | } | 
| 627 |  |  | 
| 628 |  |  | 
| 629 | < | traceray(xpos, ypos)                    /* print ray corresponding to pixel */ | 
| 629 | > | traceray(xpos, ypos)                    /* print requested pixel data */ | 
| 630 |  | int  xpos, ypos; | 
| 631 |  | { | 
| 632 | + | extern char  *index(); | 
| 633 |  | FLOAT  hv[2]; | 
| 634 |  | FVECT  rorg, rdir; | 
| 635 | + | COLOR  cval; | 
| 636 | + | register char  *cp; | 
| 637 |  |  | 
| 638 | < | if (!gotview) {         /* no view, no can do */ | 
| 639 | < | XBell(thedisplay, 0); | 
| 640 | < | return(-1); | 
| 641 | < | } | 
| 638 | > | box.xmin = xpos; box.xsiz = 1; | 
| 639 | > | box.ymin = ypos; box.ysiz = 1; | 
| 640 | > | avgbox(cval); | 
| 641 | > | scalecolor(cval, 1./exposure); | 
| 642 |  | pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); | 
| 643 | < | if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 644 | < | return(-1); | 
| 645 | < | printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 646 | < | printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 643 | > | if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 644 | > | rorg[0] = rorg[1] = rorg[2] = | 
| 645 | > | rdir[0] = rdir[1] = rdir[2] = 0.; | 
| 646 | > |  | 
| 647 | > | for (cp = tout; *cp; cp++)      /* print what they asked for */ | 
| 648 | > | switch (*cp) { | 
| 649 | > | case 'o':                       /* origin */ | 
| 650 | > | printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 651 | > | break; | 
| 652 | > | case 'd':                       /* direction */ | 
| 653 | > | printf("%e %e %e ", rdir[0], rdir[1], rdir[2]); | 
| 654 | > | break; | 
| 655 | > | case 'v':                       /* radiance value */ | 
| 656 | > | printf("%e %e %e ", colval(cval,RED), | 
| 657 | > | colval(cval,GRN), colval(cval,BLU)); | 
| 658 | > | break; | 
| 659 | > | case 'l':                       /* luminance */ | 
| 660 | > | printf("%e ", luminance(cval)); | 
| 661 | > | break; | 
| 662 | > | case 'p':                       /* pixel position */ | 
| 663 | > | printf("%d %d ", (int)(hv[0]*inpres.xr), | 
| 664 | > | (int)(hv[1]*inpres.yr)); | 
| 665 | > | break; | 
| 666 | > | } | 
| 667 | > | putchar('\n'); | 
| 668 |  | fflush(stdout); | 
| 669 |  | return(0); | 
| 670 |  | } | 
| 693 |  | case '\r': | 
| 694 |  | case 'l': | 
| 695 |  | case 'c':                               /* value */ | 
| 696 | < | if (avgbox(cval) == -1) | 
| 696 | > | if (!avgbox(cval)) | 
| 697 |  | return(-1); | 
| 698 |  | switch (com) { | 
| 699 |  | case '\n': | 
| 735 |  | return(0); | 
| 736 |  | case 't':                               /* trace */ | 
| 737 |  | return(traceray(ekey->x, ekey->y)); | 
| 738 | + | case 'a':                               /* auto exposure */ | 
| 739 | + | tmflags = TM_F_CAMERA; | 
| 740 | + | strcpy(buf, "auto exposure..."); | 
| 741 | + | goto remap; | 
| 742 | + | case 'h':                               /* human response */ | 
| 743 | + | tmflags = TM_F_HUMAN; | 
| 744 | + | strcpy(buf, "human exposure..."); | 
| 745 | + | goto remap; | 
| 746 |  | case '=':                               /* adjust exposure */ | 
| 747 |  | case '@':                               /* adaptation level */ | 
| 748 | < | if (avgbox(cval) == -1) | 
| 748 | > | if (!avgbox(cval)) | 
| 749 |  | return(-1); | 
| 750 | < | comp = com=='@' | 
| 751 | < | ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure | 
| 752 | < | : .5/bright(cval) ; | 
| 750 | > | comp = bright(cval); | 
| 751 | > | if (comp < 1e-20) { | 
| 752 | > | XBell(thedisplay, 0); | 
| 753 | > | return(-1); | 
| 754 | > | } | 
| 755 | > | if (com == '@') | 
| 756 | > | comp = 106./exposure/ | 
| 757 | > | pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); | 
| 758 | > | else | 
| 759 | > | comp = .5/comp; | 
| 760 |  | comp = log(comp)/.69315 - scale; | 
| 761 |  | n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */ | 
| 762 | < | if (n == 0) | 
| 763 | < | return(0); | 
| 764 | < | scale_rcolors(ourras, pow(2.0, (double)n)); | 
| 762 | > | if (tmflags != TM_F_LINEAR) | 
| 763 | > | tmflags = TM_F_LINEAR;  /* turn off tone mapping */ | 
| 764 | > | else { | 
| 765 | > | if (n == 0)             /* else check if any change */ | 
| 766 | > | return(0); | 
| 767 | > | scale_rcolors(ourras, pow(2.0, (double)n)); | 
| 768 | > | } | 
| 769 |  | scale += n; | 
| 770 |  | sprintf(buf, "%+d", scale); | 
| 771 | + | remap: | 
| 772 |  | XDrawImageString(thedisplay, wind, ourgc, | 
| 773 |  | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 774 |  | XFlush(thedisplay); | 
| 863 |  | } | 
| 864 |  |  | 
| 865 |  |  | 
| 866 | + | trackrays(ebut)                         /* trace rays as mouse moves */ | 
| 867 | + | XButtonPressedEvent  *ebut; | 
| 868 | + | { | 
| 869 | + | XEvent  e; | 
| 870 | + | unsigned long   lastrept; | 
| 871 | + |  | 
| 872 | + | traceray(ebut->x, ebut->y); | 
| 873 | + | lastrept = ebut->time; | 
| 874 | + | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 875 | + | while (e.type == MotionNotify) { | 
| 876 | + | if (e.xmotion.time >= lastrept + tinterv) { | 
| 877 | + | traceray(e.xmotion.x, e.xmotion.y); | 
| 878 | + | lastrept = e.xmotion.time; | 
| 879 | + | } | 
| 880 | + | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 881 | + | } | 
| 882 | + | } | 
| 883 | + |  | 
| 884 | + |  | 
| 885 |  | revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */ | 
| 886 |  | int  x0, y0, x1, y1; | 
| 887 |  | { | 
| 892 |  | } | 
| 893 |  |  | 
| 894 |  |  | 
| 895 | < | avgbox(clr)                             /* average color over current box */ | 
| 896 | < | COLOR  clr; | 
| 895 | > | int | 
| 896 | > | colavg(scn, n, cavg) | 
| 897 | > | register COLR   *scn; | 
| 898 | > | register int    n; | 
| 899 | > | COLOR   cavg; | 
| 900 |  | { | 
| 901 | < | static COLOR  lc; | 
| 902 | < | static int  ll, lr, lt, lb; | 
| 901 | > | COLOR   col; | 
| 902 | > |  | 
| 903 | > | while (n--) { | 
| 904 | > | colr_color(col, scn++); | 
| 905 | > | addcolor(cavg, col); | 
| 906 | > | } | 
| 907 | > | } | 
| 908 | > |  | 
| 909 | > |  | 
| 910 | > | int | 
| 911 | > | avgbox(cavg)                            /* average color over current box */ | 
| 912 | > | COLOR   cavg; | 
| 913 | > | { | 
| 914 | > | double  d; | 
| 915 | > | register int    rval; | 
| 916 | > |  | 
| 917 | > | setcolor(cavg, 0., 0., 0.); | 
| 918 | > | rval = dobox(colavg, (char *)cavg); | 
| 919 | > | if (rval > 0) { | 
| 920 | > | d = 1./rval; | 
| 921 | > | scalecolor(cavg, d); | 
| 922 | > | } | 
| 923 | > | return(rval); | 
| 924 | > | } | 
| 925 | > |  | 
| 926 | > |  | 
| 927 | > | int | 
| 928 | > | dobox(f, p)                             /* run function over box */ | 
| 929 | > | int     (*f)();                 /* function to call for each subscan */ | 
| 930 | > | char    *p;                     /* pointer to private data */ | 
| 931 | > | { | 
| 932 |  | int  left, right, top, bottom; | 
| 933 |  | int  y; | 
| 785 | – | double  d; | 
| 786 | – | COLOR  ctmp; | 
| 787 | – | register int  x; | 
| 934 |  |  | 
| 789 | – | setcolor(clr, 0.0, 0.0, 0.0); | 
| 935 |  | left = box.xmin - xoff; | 
| 936 |  | right = left + box.xsiz; | 
| 937 |  | if (left < 0) | 
| 939 |  | if (right > xmax) | 
| 940 |  | right = xmax; | 
| 941 |  | if (left >= right) | 
| 942 | < | return(-1); | 
| 942 | > | return(0); | 
| 943 |  | top = box.ymin - yoff; | 
| 944 |  | bottom = top + box.ysiz; | 
| 945 |  | if (top < 0) | 
| 947 |  | if (bottom > ymax) | 
| 948 |  | bottom = ymax; | 
| 949 |  | if (top >= bottom) | 
| 805 | – | return(-1); | 
| 806 | – | if (left == ll && right == lr && top == lt && bottom == lb) { | 
| 807 | – | copycolor(clr, lc); | 
| 950 |  | return(0); | 
| 809 | – | } | 
| 951 |  | for (y = top; y < bottom; y++) { | 
| 952 |  | if (getscan(y) == -1) | 
| 953 |  | return(-1); | 
| 954 | < | for (x = left; x < right; x++) { | 
| 955 | < | colr_color(ctmp, scanline[x]); | 
| 956 | < | addcolor(clr, ctmp); | 
| 954 | > | (*f)(scanline+left, right-left, p); | 
| 955 | > | } | 
| 956 | > | return((right-left)*(bottom-top)); | 
| 957 | > | } | 
| 958 | > |  | 
| 959 | > |  | 
| 960 | > | int | 
| 961 | > | addfix(scn, n)                  /* add fixation points to histogram */ | 
| 962 | > | COLR    *scn; | 
| 963 | > | int     n; | 
| 964 | > | { | 
| 965 | > | if (tmCvColrs(lscan, TM_NOCHROM, scn, n)) | 
| 966 | > | goto tmerr; | 
| 967 | > | if (tmAddHisto(lscan, n, FIXWEIGHT)) | 
| 968 | > | goto tmerr; | 
| 969 | > | return; | 
| 970 | > | tmerr: | 
| 971 | > | quiterr("tone mapping error"); | 
| 972 | > | } | 
| 973 | > |  | 
| 974 | > |  | 
| 975 | > | make_tonemap()                  /* initialize tone mapping */ | 
| 976 | > | { | 
| 977 | > | int  flags, y; | 
| 978 | > |  | 
| 979 | > | if (tmflags != TM_F_LINEAR && fname == NULL) { | 
| 980 | > | fprintf(stderr, "%s: cannot adjust tone of standard input\n", | 
| 981 | > | progname); | 
| 982 | > | tmflags = TM_F_LINEAR; | 
| 983 | > | } | 
| 984 | > | if (tmflags == TM_F_LINEAR) {   /* linear with clamping */ | 
| 985 | > | setcolrcor(pow, 1.0/gamcor); | 
| 986 | > | return; | 
| 987 | > | } | 
| 988 | > | flags = tmflags;                /* histogram adjustment */ | 
| 989 | > | if (greyscale) flags |= TM_F_BW; | 
| 990 | > | if (tmTop != NULL) {            /* reuse old histogram if one */ | 
| 991 | > | tmDone(tmTop); | 
| 992 | > | tmTop->flags = flags; | 
| 993 | > | } else {                        /* else initialize */ | 
| 994 | > | if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) | 
| 995 | > | goto memerr; | 
| 996 | > | if (greyscale) { | 
| 997 | > | cscan = TM_NOCHROM; | 
| 998 | > | if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) | 
| 999 | > | goto memerr; | 
| 1000 | > | } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) | 
| 1001 | > | == NULL) | 
| 1002 | > | goto memerr; | 
| 1003 | > | /* initialize tm library */ | 
| 1004 | > | if (tmInit(flags, stdprims, gamcor) == NULL) | 
| 1005 | > | goto memerr; | 
| 1006 | > | if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) | 
| 1007 | > | goto tmerr; | 
| 1008 | > | /* compute picture histogram */ | 
| 1009 | > | for (y = 0; y < ymax; y++) { | 
| 1010 | > | getscan(y); | 
| 1011 | > | if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) | 
| 1012 | > | goto tmerr; | 
| 1013 | > | if (tmAddHisto(lscan, xmax, 1)) | 
| 1014 | > | goto tmerr; | 
| 1015 |  | } | 
| 1016 |  | } | 
| 1017 | < | d = 1.0/((right-left)*(bottom-top)); | 
| 1018 | < | scalecolor(clr, d); | 
| 1019 | < | ll = left; lr = right; lt = top; lb = bottom; | 
| 1020 | < | copycolor(lc, clr); | 
| 1021 | < | return(0); | 
| 1017 | > | tmDup();                        /* add fixations to duplicate map */ | 
| 1018 | > | dobox(addfix, NULL); | 
| 1019 | > | /* (re)compute tone mapping */ | 
| 1020 | > | if (tmComputeMapping(gamcor, 0., 0.)) | 
| 1021 | > | goto tmerr; | 
| 1022 | > | return; | 
| 1023 | > | memerr: | 
| 1024 | > | quiterr("out of memory in make_tonemap"); | 
| 1025 | > | tmerr: | 
| 1026 | > | quiterr("tone mapping error"); | 
| 1027 |  | } | 
| 1028 |  |  | 
| 1029 |  |  | 
| 1030 | + | tmap_colrs(scn, len)            /* apply tone mapping to scanline */ | 
| 1031 | + | register COLR  *scn; | 
| 1032 | + | int  len; | 
| 1033 | + | { | 
| 1034 | + | register BYTE  *ps; | 
| 1035 | + |  | 
| 1036 | + | if (tmflags == TM_F_LINEAR) { | 
| 1037 | + | if (scale) | 
| 1038 | + | shiftcolrs(scn, len, scale); | 
| 1039 | + | colrs_gambs(scn, len); | 
| 1040 | + | return; | 
| 1041 | + | } | 
| 1042 | + | if (len > xmax) | 
| 1043 | + | quiterr("code error 1 in tmap_colrs"); | 
| 1044 | + | if (tmCvColrs(lscan, cscan, scn, len)) | 
| 1045 | + | goto tmerr; | 
| 1046 | + | if (tmMapPixels(pscan, lscan, cscan, len)) | 
| 1047 | + | goto tmerr; | 
| 1048 | + | ps = pscan; | 
| 1049 | + | if (greyscale) | 
| 1050 | + | while (len--) { | 
| 1051 | + | scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; | 
| 1052 | + | scn[0][EXP] = COLXS; | 
| 1053 | + | scn++; | 
| 1054 | + | } | 
| 1055 | + | else | 
| 1056 | + | while (len--) { | 
| 1057 | + | scn[0][RED] = *ps++; | 
| 1058 | + | scn[0][GRN] = *ps++; | 
| 1059 | + | scn[0][BLU] = *ps++; | 
| 1060 | + | scn[0][EXP] = COLXS; | 
| 1061 | + | scn++; | 
| 1062 | + | } | 
| 1063 | + | return; | 
| 1064 | + | tmerr: | 
| 1065 | + | quiterr("tone mapping error"); | 
| 1066 | + | } | 
| 1067 | + |  | 
| 1068 | + |  | 
| 1069 |  | getmono()                       /* get monochrome data */ | 
| 1070 |  | { | 
| 1071 |  | register unsigned char  *dp; | 
| 1155 |  | int     y; | 
| 1156 |  | register unsigned int4  *dp; | 
| 1157 |  | register int    x; | 
| 1158 | < | /* set gamma correction */ | 
| 1159 | < | setcolrgam(gamcor); | 
| 1158 | > | /* initialize tone mapping */ | 
| 1159 | > | make_tonemap(); | 
| 1160 |  | /* read and convert file */ | 
| 1161 |  | dp = (unsigned int4 *)ourdata; | 
| 1162 |  | for (y = 0; y < ymax; y++) { | 
| 1163 |  | getscan(y); | 
| 1164 |  | add2icon(y, scanline); | 
| 1165 | < | if (scale) | 
| 923 | < | shiftcolrs(scanline, xmax, scale); | 
| 924 | < | colrs_gambs(scanline, xmax); | 
| 1165 | > | tmap_colrs(scanline, xmax); | 
| 1166 |  | if (ourras->image->blue_mask & 1) | 
| 1167 |  | for (x = 0; x < xmax; x++) | 
| 1168 | < | *dp++ = scanline[x][RED] << 16 | | 
| 1169 | < | scanline[x][GRN] << 8 | | 
| 1170 | < | scanline[x][BLU] ; | 
| 1168 | > | *dp++ = (unsigned int4)scanline[x][RED] << 16 | | 
| 1169 | > | (unsigned int4)scanline[x][GRN] << 8 | | 
| 1170 | > | (unsigned int4)scanline[x][BLU] ; | 
| 1171 |  | else | 
| 1172 |  | for (x = 0; x < xmax; x++) | 
| 1173 | < | *dp++ = scanline[x][RED] | | 
| 1174 | < | scanline[x][GRN] << 8 | | 
| 1175 | < | scanline[x][BLU] << 16 ; | 
| 1173 | > | *dp++ = (unsigned int4)scanline[x][RED] | | 
| 1174 | > | (unsigned int4)scanline[x][GRN] << 8 | | 
| 1175 | > | (unsigned int4)scanline[x][BLU] << 16 ; | 
| 1176 |  | } | 
| 1177 |  | } | 
| 1178 |  |  | 
| 1182 |  | int     y; | 
| 1183 |  | register unsigned char  *dp; | 
| 1184 |  | register int    x; | 
| 1185 | < | /* set gamma correction */ | 
| 1186 | < | setcolrgam(gamcor); | 
| 1185 | > | /* initialize tone mapping */ | 
| 1186 | > | make_tonemap(); | 
| 1187 |  | /* read and convert file */ | 
| 1188 |  | dp = ourdata; | 
| 1189 |  | for (y = 0; y < ymax; y++) { | 
| 1190 |  | getscan(y); | 
| 1191 |  | add2icon(y, scanline); | 
| 1192 | < | if (scale) | 
| 952 | < | shiftcolrs(scanline, xmax, scale); | 
| 953 | < | for (x = 0; x < xmax; x++) | 
| 954 | < | scanline[x][GRN] = normbright(scanline[x]); | 
| 955 | < | colrs_gambs(scanline, xmax); | 
| 1192 | > | tmap_colrs(scanline, xmax); | 
| 1193 |  | if (maxcolors < 256) | 
| 1194 |  | for (x = 0; x < xmax; x++) | 
| 1195 | < | *dp++ = ((long)scanline[x][GRN] * | 
| 1195 | > | *dp++ = ((int4)scanline[x][GRN] * | 
| 1196 |  | maxcolors + maxcolors/2) >> 8; | 
| 1197 |  | else | 
| 1198 |  | for (x = 0; x < xmax; x++) | 
| 1200 |  | } | 
| 1201 |  | for (x = 0; x < maxcolors; x++) | 
| 1202 |  | clrtab[x][RED] = clrtab[x][GRN] = | 
| 1203 | < | clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; | 
| 1203 | > | clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; | 
| 1204 |  | } | 
| 1205 |  |  | 
| 1206 |  |  | 
| 1207 |  | getmapped()                     /* get color-mapped data */ | 
| 1208 |  | { | 
| 1209 |  | int     y; | 
| 1210 | < | /* set gamma correction */ | 
| 1211 | < | setcolrgam(gamcor); | 
| 1210 | > | /* make sure we can do it first */ | 
| 1211 | > | if (fname == NULL) | 
| 1212 | > | quiterr("cannot map colors from standard input"); | 
| 1213 | > | /* initialize tone mapping */ | 
| 1214 | > | make_tonemap(); | 
| 1215 |  | /* make histogram */ | 
| 1216 | < | new_histo(); | 
| 1216 | > | if (new_histo((int4)xmax*ymax) == -1) | 
| 1217 | > | quiterr("cannot initialize histogram"); | 
| 1218 |  | for (y = 0; y < ymax; y++) { | 
| 1219 |  | if (getscan(y) < 0) | 
| 1220 | < | quiterr("seek error in getmapped"); | 
| 1220 | > | break; | 
| 1221 |  | add2icon(y, scanline); | 
| 1222 | < | if (scale) | 
| 982 | < | shiftcolrs(scanline, xmax, scale); | 
| 983 | < | colrs_gambs(scanline, xmax); | 
| 1222 | > | tmap_colrs(scanline, xmax); | 
| 1223 |  | cnt_colrs(scanline, xmax); | 
| 1224 |  | } | 
| 1225 |  | /* map pixels */ | 
| 1226 |  | if (!new_clrtab(maxcolors)) | 
| 1227 |  | quiterr("cannot create color map"); | 
| 1228 |  | for (y = 0; y < ymax; y++) { | 
| 1229 | < | if (getscan(y) < 0) | 
| 1230 | < | quiterr("seek error in getmapped"); | 
| 992 | < | if (scale) | 
| 993 | < | shiftcolrs(scanline, xmax, scale); | 
| 994 | < | colrs_gambs(scanline, xmax); | 
| 1229 | > | getscan(y); | 
| 1230 | > | tmap_colrs(scanline, xmax); | 
| 1231 |  | if (dither) | 
| 1232 |  | dith_colrs(ourdata+y*xmax, scanline, xmax); | 
| 1233 |  | else | 
| 1267 |  | getscan(y) | 
| 1268 |  | int  y; | 
| 1269 |  | { | 
| 1270 | + | static int  trunced = -1;               /* truncated file? */ | 
| 1271 | + | skipit: | 
| 1272 | + | if (trunced >= 0 && y >= trunced) { | 
| 1273 | + | bzero(scanline, xmax*sizeof(COLR)); | 
| 1274 | + | return(-1); | 
| 1275 | + | } | 
| 1276 |  | if (y != cury) { | 
| 1277 |  | if (scanpos == NULL || scanpos[y] == -1) | 
| 1278 |  | return(-1); | 
| 1282 |  | } else if (scanpos != NULL && scanpos[y] == -1) | 
| 1283 |  | scanpos[y] = ftell(fin); | 
| 1284 |  |  | 
| 1285 | < | if (freadcolrs(scanline, xmax, fin) < 0) | 
| 1286 | < | quiterr("read error"); | 
| 1287 | < |  | 
| 1285 | > | if (freadcolrs(scanline, xmax, fin) < 0) { | 
| 1286 | > | fprintf(stderr, "%s: %s: unfinished picture\n", | 
| 1287 | > | progname, fname==NULL?"<stdin>":fname); | 
| 1288 | > | trunced = y; | 
| 1289 | > | goto skipit; | 
| 1290 | > | } | 
| 1291 |  | cury++; | 
| 1292 |  | return(0); | 
| 1293 |  | } |