| 1 | – | /* Copyright (c) 1993 Regents of the University of California */ | 
| 2 | – |  | 
| 1 |  | #ifndef lint | 
| 2 | < | static char SCCSid[] = "$SunId$ LBL"; | 
| 2 | > | static const char       RCSid[] = "$Id$"; | 
| 3 |  | #endif | 
| 6 | – |  | 
| 4 |  | /* | 
| 5 |  | *  x11image.c - driver for X-windows | 
| 6 |  | * | 
| 25 |  | #include  <X11/Xatom.h> | 
| 26 |  |  | 
| 27 |  | #include  "color.h" | 
| 28 | + | #include  "tonemap.h" | 
| 29 |  | #include  "view.h" | 
| 30 |  | #include  "x11raster.h" | 
| 31 |  | #include  "random.h" | 
| 34 | – | #include  "resolu.h" | 
| 32 |  |  | 
| 36 | – | #ifdef  __alpha | 
| 37 | – | #define  int4           int | 
| 38 | – | #endif | 
| 39 | – | #ifndef  int4 | 
| 40 | – | #define  int4           long | 
| 41 | – | #endif | 
| 42 | – |  | 
| 33 |  | #define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 34 |  |  | 
| 35 |  | #define  CTRL(c)        ((c)-'@') | 
| 38 |  |  | 
| 39 |  | #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */ | 
| 40 |  |  | 
| 41 | + | #define  FIXWEIGHT      20              /* weight to add for fixation points */ | 
| 42 | + |  | 
| 43 |  | #define  ourscreen      DefaultScreen(thedisplay) | 
| 44 |  | #define  ourroot        RootWindow(thedisplay,ourscreen) | 
| 45 |  |  | 
| 48 |  | #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) | 
| 49 |  |  | 
| 50 |  | double  gamcor = 2.2;                   /* gamma correction */ | 
| 51 | + | char  *gamstr = NULL;                   /* gamma value override */ | 
| 52 |  |  | 
| 53 |  | int  dither = 1;                        /* dither colors? */ | 
| 54 |  | int  fast = 0;                          /* keep picture in Pixmap? */ | 
| 66 |  | int  yoff = 0;                          /* y image offset */ | 
| 67 |  |  | 
| 68 |  | int  parent = 0;                        /* number of children, -1 if child */ | 
| 69 | + | int  sequential = 0;                    /* display images in sequence */ | 
| 70 |  |  | 
| 71 | + | char  *tout = "od";                     /* output of 't' command */ | 
| 72 | + | int  tinterv = 0;                       /* interval between mouse reports */ | 
| 73 | + |  | 
| 74 | + | int  tmflags = TM_F_LINEAR;             /* tone mapping flags */ | 
| 75 | + |  | 
| 76 |  | VIEW  ourview = STDVIEW;                /* image view parameters */ | 
| 77 |  | int  gotview = 0;                       /* got parameters from file */ | 
| 78 |  |  | 
| 79 |  | COLR  *scanline;                        /* scan line buffer */ | 
| 80 | + | TMbright  *lscan;                       /* encoded luminance scanline */ | 
| 81 | + | BYTE  *cscan;                           /* encoded chroma scanline */ | 
| 82 | + | BYTE  *pscan;                           /* compute pixel scanline */ | 
| 83 |  |  | 
| 84 |  | RESOLU  inpres;                         /* input resolution and ordering */ | 
| 85 |  | int  xmax, ymax;                        /* picture dimensions */ | 
| 86 |  | int  width, height;                     /* window size */ | 
| 87 |  | char  *fname = NULL;                    /* input file name */ | 
| 88 | < | FILE  *fin = stdin;                     /* input file */ | 
| 88 | > | FILE  *fin = NULL;                      /* input file */ | 
| 89 |  | long  *scanpos = NULL;                  /* scan line positions in file */ | 
| 90 |  | int  cury = 0;                          /* current scan location */ | 
| 91 |  |  | 
| 103 |  |  | 
| 104 |  | struct { | 
| 105 |  | int  xmin, ymin, xsiz, ysiz; | 
| 106 | < | }  box = {0, 0, 0, 0};                  /* current box */ | 
| 106 | > | }  bbox = {0, 0, 0, 0};                 /* current bbox */ | 
| 107 |  |  | 
| 108 |  | char  *geometry = NULL;                 /* geometry specification */ | 
| 109 |  |  | 
| 114 |  |  | 
| 115 |  | char  errmsg[128]; | 
| 116 |  |  | 
| 117 | < | extern BYTE  clrtab[256][3];            /* global color map */ | 
| 117 | > | BYTE  clrtab[256][3];                   /* global color map */ | 
| 118 |  |  | 
| 119 |  | extern long  ftell(); | 
| 120 |  |  | 
| 121 | + | extern char  *getenv(); | 
| 122 | + |  | 
| 123 |  | Display  *thedisplay; | 
| 124 |  | Atom  closedownAtom, wmProtocolsAtom; | 
| 125 |  |  | 
| 126 | < | int  noop() {} | 
| 126 | > | int  sigrecv; | 
| 127 |  |  | 
| 128 | + | int  onsig() { sigrecv++; } | 
| 129 |  |  | 
| 130 | + |  | 
| 131 |  | main(argc, argv) | 
| 132 |  | int  argc; | 
| 133 |  | char  *argv[]; | 
| 134 |  | { | 
| 129 | – | extern char  *getenv(); | 
| 130 | – | char  *gv; | 
| 135 |  | int  headline(); | 
| 136 |  | int  i; | 
| 137 |  | int  pid; | 
| 138 |  |  | 
| 139 |  | progname = argv[0]; | 
| 140 | < | if ((gv = getenv("GAMMA")) != NULL) | 
| 137 | < | gamcor = atof(gv); | 
| 140 | > | fin = stdin; | 
| 141 |  |  | 
| 142 |  | for (i = 1; i < argc; i++) | 
| 143 |  | if (argv[i][0] == '-') | 
| 144 |  | switch (argv[i][1]) { | 
| 145 | < | case 'c': | 
| 145 | > | case 'c':                       /* number of colors */ | 
| 146 |  | maxcolors = atoi(argv[++i]); | 
| 147 |  | break; | 
| 148 | < | case 'b': | 
| 148 | > | case 'b':                       /* greyscale only */ | 
| 149 |  | greyscale = !greyscale; | 
| 150 |  | break; | 
| 151 | < | case 'm': | 
| 151 | > | case 'm':                       /* monochrome */ | 
| 152 | > | greyscale = 1; | 
| 153 |  | maxcolors = 2; | 
| 154 |  | break; | 
| 155 | < | case 'd': | 
| 155 | > | case 'd':                       /* display or dither */ | 
| 156 |  | if (argv[i][2] == 'i') | 
| 157 |  | dispname = argv[++i]; | 
| 158 |  | else | 
| 159 |  | dither = !dither; | 
| 160 |  | break; | 
| 161 | < | case 'f': | 
| 161 | > | case 'f':                       /* save pixmap */ | 
| 162 |  | fast = !fast; | 
| 163 |  | break; | 
| 164 | < | case 'e': | 
| 165 | < | if (argv[i+1][0] != '+' && argv[i+1][0] != '-') | 
| 164 | > | case 's':                       /* one at a time */ | 
| 165 | > | sequential = !sequential; | 
| 166 | > | break; | 
| 167 | > | case 'o':                       /* 't' output */ | 
| 168 | > | tout = argv[i]+2; | 
| 169 | > | break; | 
| 170 | > | case 't':                       /* msec interval */ | 
| 171 | > | tinterv = atoi(argv[++i]); | 
| 172 | > | break; | 
| 173 | > | case 'e':                       /* exposure comp. */ | 
| 174 | > | i++; | 
| 175 | > | if (argv[i][0] == 'a') { | 
| 176 | > | tmflags = TM_F_CAMERA; | 
| 177 | > | break; | 
| 178 | > | } | 
| 179 | > | if (argv[i][0] == 'h') { | 
| 180 | > | tmflags = TM_F_HUMAN; | 
| 181 | > | break; | 
| 182 | > | } | 
| 183 | > | if (argv[i][0] != '+' && argv[i][0] != '-') | 
| 184 |  | goto userr; | 
| 185 | < | scale = atoi(argv[++i]); | 
| 185 | > | scale = atoi(argv[i]); | 
| 186 |  | break; | 
| 187 | < | case 'g': | 
| 187 | > | case 'g':                       /* gamma comp. */ | 
| 188 |  | if (argv[i][2] == 'e') | 
| 189 |  | geometry = argv[++i]; | 
| 190 |  | else | 
| 191 | < | gamcor = atof(argv[++i]); | 
| 191 | > | gamstr = argv[++i]; | 
| 192 |  | break; | 
| 193 |  | default: | 
| 194 |  | goto userr; | 
| 201 |  | if (i > argc) | 
| 202 |  | goto userr; | 
| 203 |  | while (i < argc-1) { | 
| 204 | + | sigrecv = 0; | 
| 205 | + | signal(SIGCONT, onsig); | 
| 206 |  | if ((pid=fork()) == 0) {        /* a child for each picture */ | 
| 207 |  | parent = -1; | 
| 208 |  | break; | 
| 210 |  | if (pid < 0) | 
| 211 |  | quiterr("fork failed"); | 
| 212 |  | parent++; | 
| 213 | < | signal(SIGCONT, noop); | 
| 214 | < | pause();                /* wait for wake-up call */ | 
| 213 | > | while (!sigrecv) | 
| 214 | > | pause();        /* wait for wake-up call */ | 
| 215 |  | i++; | 
| 216 |  | } | 
| 217 |  | if (i < argc) {                 /* open picture file */ | 
| 218 |  | fname = argv[i]; | 
| 219 |  | fin = fopen(fname, "r"); | 
| 220 | < | if (fin == NULL) { | 
| 221 | < | sprintf(errmsg, "cannot open file \"%s\"", fname); | 
| 198 | < | quiterr(errmsg); | 
| 199 | < | } | 
| 220 | > | if (fin == NULL) | 
| 221 | > | quiterr("cannot open picture file"); | 
| 222 |  | } | 
| 223 |  | /* get header */ | 
| 224 |  | getheader(fin, headline, NULL); | 
| 235 |  |  | 
| 236 |  | init(argc, argv);                       /* get file and open window */ | 
| 237 |  |  | 
| 216 | – | if (parent < 0) | 
| 217 | – | kill(getppid(), SIGCONT);       /* signal parent if child */ | 
| 218 | – |  | 
| 238 |  | for ( ; ; ) | 
| 239 |  | getevent();             /* main loop */ | 
| 240 |  | userr: | 
| 241 |  | fprintf(stderr, | 
| 242 | < | "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n", | 
| 242 | > | "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", | 
| 243 |  | progname); | 
| 244 |  | exit(1); | 
| 245 |  | } | 
| 246 |  |  | 
| 247 |  |  | 
| 248 | + | int | 
| 249 |  | headline(s)             /* get relevant info from header */ | 
| 250 |  | char  *s; | 
| 251 |  | { | 
| 253 |  |  | 
| 254 |  | if (isexpos(s)) | 
| 255 |  | exposure *= exposval(s); | 
| 256 | < | else if (isformat(s)) { | 
| 237 | < | formatval(fmt, s); | 
| 256 | > | else if (formatval(fmt, s)) | 
| 257 |  | wrongformat = strcmp(fmt, COLRFMT); | 
| 258 | < | } else if (isview(s) && sscanview(&ourview, s) > 0) | 
| 258 | > | else if (isview(s) && sscanview(&ourview, s) > 0) | 
| 259 |  | gotview++; | 
| 260 | + | return(0); | 
| 261 |  | } | 
| 262 |  |  | 
| 263 |  |  | 
| 290 |  | name += i+1; | 
| 291 |  | if ((thedisplay = XOpenDisplay(dispname)) == NULL) | 
| 292 |  | quiterr("cannot open display"); | 
| 293 | + | /* set gamma value */ | 
| 294 | + | if (gamstr == NULL)             /* get it from the X server */ | 
| 295 | + | gamstr = XGetDefault(thedisplay, "radiance", "gamma"); | 
| 296 | + | if (gamstr == NULL)             /* get it from the environment */ | 
| 297 | + | gamstr = getenv("DISPLAY_GAMMA"); | 
| 298 | + | if (gamstr != NULL) | 
| 299 | + | gamcor = atof(gamstr); | 
| 300 |  | /* get best visual for default screen */ | 
| 301 |  | getbestvis(); | 
| 302 |  | /* store image */ | 
| 323 |  | } | 
| 324 |  | } | 
| 325 |  | /* open window */ | 
| 326 | + | i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; | 
| 327 |  | ourwinattr.border_pixel = ourwhite; | 
| 328 |  | ourwinattr.background_pixel = ourblack; | 
| 329 | < | ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, | 
| 330 | < | ourvis.visual, AllocNone); | 
| 329 | > | if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { | 
| 330 | > | ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); | 
| 331 | > | i |= CWColormap; | 
| 332 | > | } | 
| 333 |  | ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| | 
| 334 |  | ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; | 
| 335 |  | ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); | 
| 336 |  | wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, | 
| 337 |  | xszhints.width, xszhints.height, BORWIDTH, | 
| 338 | < | ourvis.depth, InputOutput, ourvis.visual, CWEventMask| | 
| 339 | < | CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); | 
| 338 | > | ourvis.depth, InputOutput, ourvis.visual, | 
| 339 | > | i, &ourwinattr); | 
| 340 |  | if (wind == 0) | 
| 341 |  | quiterr("cannot create window"); | 
| 342 |  | width = xmax; | 
| 343 |  | height = ymax; | 
| 344 | < | xgcv.foreground = ourblack; | 
| 315 | < | xgcv.background = ourwhite; | 
| 344 | > | /* prepare graphics drawing context */ | 
| 345 |  | if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) | 
| 346 |  | quiterr("cannot get font"); | 
| 347 | + | xgcv.foreground = ourblack; | 
| 348 | + | xgcv.background = ourwhite; | 
| 349 |  | ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| | 
| 350 |  | GCFont, &xgcv); | 
| 351 |  | xgcv.function = GXinvert; | 
| 374 |  | XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); | 
| 375 |  |  | 
| 376 |  | XMapWindow(thedisplay, wind); | 
| 346 | – | return; | 
| 377 |  | } /* end of init */ | 
| 378 |  |  | 
| 379 |  |  | 
| 380 |  | quiterr(err)            /* print message and exit */ | 
| 381 |  | char  *err; | 
| 382 |  | { | 
| 383 | < | if (err != NULL) | 
| 383 | > | register int  es; | 
| 384 | > | int  cs; | 
| 385 | > |  | 
| 386 | > | if (es = err != NULL) | 
| 387 |  | fprintf(stderr, "%s: %s: %s\n", progname, | 
| 388 |  | fname==NULL?"<stdin>":fname, err); | 
| 389 | < | if (wind) { | 
| 390 | < | XDestroyWindow(thedisplay, wind); | 
| 391 | < | XFlush(thedisplay); | 
| 392 | < | } | 
| 393 | < | while (parent > 0 && wait(0) != -1)     /* wait for any children */ | 
| 389 | > | if (thedisplay != NULL) | 
| 390 | > | XCloseDisplay(thedisplay); | 
| 391 | > | if (parent < 0 & sigrecv == 0) | 
| 392 | > | kill(getppid(), SIGCONT); | 
| 393 | > | while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ | 
| 394 | > | if (es == 0) | 
| 395 | > | es = cs>>8 & 0xff; | 
| 396 |  | parent--; | 
| 397 | < | exit(err != NULL); | 
| 397 | > | } | 
| 398 | > | exit(es); | 
| 399 |  | } | 
| 400 |  |  | 
| 401 |  |  | 
| 413 |  | return(-1); | 
| 414 |  | if (v1->depth == 32 && v2->depth == 24) | 
| 415 |  | return(1); | 
| 416 | < | return(0); | 
| 416 | > | /* go for maximum depth otherwise */ | 
| 417 | > | return(v2->depth - v1->depth); | 
| 418 |  | } | 
| 419 |  | /* don't be too greedy */ | 
| 420 |  | if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) | 
| 421 |  | return(v1->depth - v2->depth); | 
| 422 |  | return(v2->depth - v1->depth); | 
| 423 |  | } | 
| 424 | < | /* prefer Pseudo when < 24-bit */ | 
| 424 | > | /* prefer Pseudo when < 15-bit */ | 
| 425 |  | if ((v1->class == TrueColor || v1->class == DirectColor) && | 
| 426 | < | v1->depth < 24) | 
| 426 | > | v1->depth < 15) | 
| 427 |  | bad1 = 1; | 
| 428 |  | if ((v2->class == TrueColor || v2->class == DirectColor) && | 
| 429 | < | v2->depth < 24) | 
| 429 | > | v2->depth < 15) | 
| 430 |  | bad2 = -1; | 
| 431 |  | if (bad1 | bad2) | 
| 432 |  | return(bad1+bad2); | 
| 537 |  | goto fail; | 
| 538 |  | getmono(); | 
| 539 |  | } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { | 
| 540 | < | ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); | 
| 540 | > | int  datsiz = ourvis.depth>16 ? sizeof(int4) : sizeof(int2); | 
| 541 | > | ourdata = (unsigned char *)malloc(datsiz*xmax*ymax); | 
| 542 |  | if (ourdata == NULL) | 
| 543 |  | goto fail; | 
| 544 | < | ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, | 
| 545 | < | ourdata, xmax, ymax, 32); | 
| 544 | > | ourras = make_raster(thedisplay, &ourvis, datsiz*8, | 
| 545 | > | ourdata, xmax, ymax, datsiz*8); | 
| 546 |  | if (ourras == NULL) | 
| 547 |  | goto fail; | 
| 548 |  | getfull(); | 
| 554 |  | xmax, ymax, 8); | 
| 555 |  | if (ourras == NULL) | 
| 556 |  | goto fail; | 
| 557 | < | if (greyscale | ourvis.class == StaticGray) | 
| 557 | > | if (greyscale) | 
| 558 |  | getgrey(); | 
| 559 |  | else | 
| 560 |  | getmapped(); | 
| 584 |  | map_rcolors(ourras, wind); | 
| 585 |  | if (fast) | 
| 586 |  | make_rpixmap(ourras, wind); | 
| 587 | + | if (!sequential & parent < 0 & sigrecv == 0) { | 
| 588 | + | kill(getppid(), SIGCONT); | 
| 589 | + | sigrecv--; | 
| 590 | + | } | 
| 591 |  | break; | 
| 592 |  | case UnmapNotify: | 
| 593 |  | if (!fast) | 
| 600 |  | case ButtonPress: | 
| 601 |  | if (xev.xbutton.state & (ShiftMask|ControlMask)) | 
| 602 |  | moveimage(&xev.xbutton); | 
| 561 | – | else if (xev.xbutton.button == Button2) | 
| 562 | – | traceray(xev.xbutton.x, xev.xbutton.y); | 
| 603 |  | else | 
| 604 | < | getbox(&xev.xbutton); | 
| 604 | > | switch (xev.xbutton.button) { | 
| 605 | > | case Button1: | 
| 606 | > | getbox(&xev.xbutton); | 
| 607 | > | break; | 
| 608 | > | case Button2: | 
| 609 | > | traceray(xev.xbutton.x, xev.xbutton.y); | 
| 610 | > | break; | 
| 611 | > | case Button3: | 
| 612 | > | trackrays(&xev.xbutton); | 
| 613 | > | break; | 
| 614 | > | } | 
| 615 |  | break; | 
| 616 |  | case ClientMessage: | 
| 617 |  | if ((xev.xclient.message_type == wmProtocolsAtom) && | 
| 622 |  | } | 
| 623 |  |  | 
| 624 |  |  | 
| 625 | < | traceray(xpos, ypos)                    /* print ray corresponding to pixel */ | 
| 625 | > | traceray(xpos, ypos)                    /* print requested pixel data */ | 
| 626 |  | int  xpos, ypos; | 
| 627 |  | { | 
| 628 | + | extern char  *index(); | 
| 629 |  | FLOAT  hv[2]; | 
| 630 |  | FVECT  rorg, rdir; | 
| 631 | + | COLOR  cval; | 
| 632 | + | register char  *cp; | 
| 633 |  |  | 
| 634 | < | if (!gotview) {         /* no view, no can do */ | 
| 635 | < | XBell(thedisplay, 0); | 
| 636 | < | return(-1); | 
| 637 | < | } | 
| 634 | > | bbox.xmin = xpos; bbox.xsiz = 1; | 
| 635 | > | bbox.ymin = ypos; bbox.ysiz = 1; | 
| 636 | > | avgbox(cval); | 
| 637 | > | scalecolor(cval, 1./exposure); | 
| 638 |  | pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); | 
| 639 | < | if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 640 | < | return(-1); | 
| 641 | < | printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 642 | < | printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 639 | > | if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 640 | > | rorg[0] = rorg[1] = rorg[2] = | 
| 641 | > | rdir[0] = rdir[1] = rdir[2] = 0.; | 
| 642 | > |  | 
| 643 | > | for (cp = tout; *cp; cp++)      /* print what they asked for */ | 
| 644 | > | switch (*cp) { | 
| 645 | > | case 'o':                       /* origin */ | 
| 646 | > | printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 647 | > | break; | 
| 648 | > | case 'd':                       /* direction */ | 
| 649 | > | printf("%e %e %e ", rdir[0], rdir[1], rdir[2]); | 
| 650 | > | break; | 
| 651 | > | case 'v':                       /* radiance value */ | 
| 652 | > | printf("%e %e %e ", colval(cval,RED), | 
| 653 | > | colval(cval,GRN), colval(cval,BLU)); | 
| 654 | > | break; | 
| 655 | > | case 'l':                       /* luminance */ | 
| 656 | > | printf("%e ", luminance(cval)); | 
| 657 | > | break; | 
| 658 | > | case 'p':                       /* pixel position */ | 
| 659 | > | printf("%d %d ", (int)(hv[0]*inpres.xr), | 
| 660 | > | (int)(hv[1]*inpres.yr)); | 
| 661 | > | break; | 
| 662 | > | } | 
| 663 | > | putchar('\n'); | 
| 664 |  | fflush(stdout); | 
| 665 |  | return(0); | 
| 666 |  | } | 
| 689 |  | case '\r': | 
| 690 |  | case 'l': | 
| 691 |  | case 'c':                               /* value */ | 
| 692 | < | if (avgbox(cval) == -1) | 
| 692 | > | if (!avgbox(cval)) | 
| 693 |  | return(-1); | 
| 694 |  | switch (com) { | 
| 695 |  | case '\n': | 
| 697 |  | sprintf(buf, "%.3f", intens(cval)/exposure); | 
| 698 |  | break; | 
| 699 |  | case 'l':                               /* luminance */ | 
| 700 | < | sprintf(buf, "%.0fL", luminance(cval)/exposure); | 
| 700 | > | sprintf(buf, "%.1fL", luminance(cval)/exposure); | 
| 701 |  | break; | 
| 702 |  | case 'c':                               /* color */ | 
| 703 |  | comp = pow(2.0, (double)scale); | 
| 708 |  | break; | 
| 709 |  | } | 
| 710 |  | XDrawImageString(thedisplay, wind, ourgc, | 
| 711 | < | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 711 | > | bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); | 
| 712 |  | return(0); | 
| 713 |  | case 'i':                               /* identify (contour) */ | 
| 714 |  | if (ourras->pixels == NULL) | 
| 731 |  | return(0); | 
| 732 |  | case 't':                               /* trace */ | 
| 733 |  | return(traceray(ekey->x, ekey->y)); | 
| 734 | + | case 'a':                               /* auto exposure */ | 
| 735 | + | if (fname == NULL) | 
| 736 | + | return(-1); | 
| 737 | + | tmflags = TM_F_CAMERA; | 
| 738 | + | strcpy(buf, "auto exposure..."); | 
| 739 | + | goto remap; | 
| 740 | + | case 'h':                               /* human response */ | 
| 741 | + | if (fname == NULL) | 
| 742 | + | return(-1); | 
| 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)); | 
| 773 | > | bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); | 
| 774 |  | XFlush(thedisplay); | 
| 775 | < | free(ourdata); | 
| 775 | > | /* free(ourdata);       This is done in XDestroyImage()! */ | 
| 776 |  | free_raster(ourras); | 
| 777 |  | getras(); | 
| 778 |  | /* fall through */ | 
| 801 |  | redraw(0, 0, width, height); | 
| 802 |  | return(0); | 
| 803 |  | case ' ':                               /* clear */ | 
| 804 | < | redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); | 
| 804 | > | redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz); | 
| 805 |  | return(0); | 
| 806 |  | default: | 
| 807 |  | XBell(thedisplay, 0); | 
| 835 |  | } | 
| 836 |  |  | 
| 837 |  |  | 
| 838 | < | getbox(ebut)                            /* get new box */ | 
| 838 | > | getbox(ebut)                            /* get new bbox */ | 
| 839 |  | XButtonPressedEvent  *ebut; | 
| 840 |  | { | 
| 841 |  | XEvent  e; | 
| 842 |  |  | 
| 843 |  | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 844 |  | while (e.type == MotionNotify) { | 
| 845 | < | revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); | 
| 845 | > | revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y); | 
| 846 |  | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 847 | < | revbox(ebut->x, ebut->y, box.xmin, box.ymin); | 
| 847 | > | revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin); | 
| 848 |  | } | 
| 849 | < | box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); | 
| 850 | < | box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); | 
| 851 | < | if (box.xmin > ebut->x) { | 
| 852 | < | box.xsiz = box.xmin - ebut->x + 1; | 
| 853 | < | box.xmin = ebut->x; | 
| 849 | > | bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); | 
| 850 | > | bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); | 
| 851 | > | if (bbox.xmin > ebut->x) { | 
| 852 | > | bbox.xsiz = bbox.xmin - ebut->x + 1; | 
| 853 | > | bbox.xmin = ebut->x; | 
| 854 |  | } else { | 
| 855 | < | box.xsiz = ebut->x - box.xmin + 1; | 
| 855 | > | bbox.xsiz = ebut->x - bbox.xmin + 1; | 
| 856 |  | } | 
| 857 | < | if (box.ymin > ebut->y) { | 
| 858 | < | box.ysiz = box.ymin - ebut->y + 1; | 
| 859 | < | box.ymin = ebut->y; | 
| 857 | > | if (bbox.ymin > ebut->y) { | 
| 858 | > | bbox.ysiz = bbox.ymin - ebut->y + 1; | 
| 859 | > | bbox.ymin = ebut->y; | 
| 860 |  | } else { | 
| 861 | < | box.ysiz = ebut->y - box.ymin + 1; | 
| 861 | > | bbox.ysiz = ebut->y - bbox.ymin + 1; | 
| 862 |  | } | 
| 863 |  | } | 
| 864 |  |  | 
| 865 |  |  | 
| 866 | < | revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */ | 
| 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 bbox with reversed lines */ | 
| 886 |  | int  x0, y0, x1, y1; | 
| 887 |  | { | 
| 888 |  | revline(x0, y0, x1, y0); | 
| 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 bbox */ | 
| 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 bbox */ | 
| 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 |  |  | 
| 935 | < | setcolor(clr, 0.0, 0.0, 0.0); | 
| 936 | < | left = box.xmin - xoff; | 
| 791 | < | right = left + box.xsiz; | 
| 935 | > | left = bbox.xmin - xoff; | 
| 936 | > | right = left + bbox.xsiz; | 
| 937 |  | if (left < 0) | 
| 938 |  | left = 0; | 
| 939 |  | if (right > xmax) | 
| 940 |  | right = xmax; | 
| 941 |  | if (left >= right) | 
| 942 | < | return(-1); | 
| 943 | < | top = box.ymin - yoff; | 
| 944 | < | bottom = top + box.ysiz; | 
| 942 | > | return(0); | 
| 943 | > | top = bbox.ymin - yoff; | 
| 944 | > | bottom = top + bbox.ysiz; | 
| 945 |  | if (top < 0) | 
| 946 |  | 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; | 
| 1094 |  | cerr[x] = err + errp; | 
| 1095 |  | } | 
| 1096 |  | } | 
| 1097 | < | free((char *)cerr); | 
| 1097 | > | free((void *)cerr); | 
| 1098 |  | } | 
| 1099 |  |  | 
| 1100 |  |  | 
| 1137 |  | errp = err; | 
| 1138 |  | ti = x*xmax/iconwidth; | 
| 1139 |  | copycolr(clr, scan[ti]); | 
| 1140 | < | normcolrs(clr, 1, scale); | 
| 1140 | > | normcolrs(&clr, 1, scale); | 
| 1141 |  | err += normbright(clr) + cerr[x]; | 
| 1142 |  | if (err > 127) | 
| 1143 |  | err -= 255; | 
| 1154 |  | { | 
| 1155 |  | int     y; | 
| 1156 |  | register unsigned int4  *dp; | 
| 1157 | + | register unsigned int2  *dph; | 
| 1158 |  | register int    x; | 
| 1159 | < | /* set gamma correction */ | 
| 1160 | < | setcolrgam(gamcor); | 
| 1159 | > | /* initialize tone mapping */ | 
| 1160 | > | make_tonemap(); | 
| 1161 |  | /* read and convert file */ | 
| 1162 |  | dp = (unsigned int4 *)ourdata; | 
| 1163 | + | dph = (unsigned int2 *)ourdata; | 
| 1164 |  | for (y = 0; y < ymax; y++) { | 
| 1165 |  | getscan(y); | 
| 1166 |  | add2icon(y, scanline); | 
| 1167 | < | if (scale) | 
| 1168 | < | shiftcolrs(scanline, xmax, scale); | 
| 1169 | < | colrs_gambs(scanline, xmax); | 
| 925 | < | if (ourras->image->blue_mask & 1) | 
| 1167 | > | tmap_colrs(scanline, xmax); | 
| 1168 | > | switch (ourras->image->blue_mask) { | 
| 1169 | > | case 0xff:              /* 24-bit RGB */ | 
| 1170 |  | for (x = 0; x < xmax; x++) | 
| 1171 | < | *dp++ = scanline[x][RED] << 16 | | 
| 1172 | < | scanline[x][GRN] << 8 | | 
| 1173 | < | scanline[x][BLU] ; | 
| 1174 | < | else | 
| 1171 | > | *dp++ = (unsigned int4)scanline[x][RED] << 16 | | 
| 1172 | > | (unsigned int4)scanline[x][GRN] << 8 | | 
| 1173 | > | (unsigned int4)scanline[x][BLU] ; | 
| 1174 | > | break; | 
| 1175 | > | case 0xff0000:          /* 24-bit BGR */ | 
| 1176 |  | for (x = 0; x < xmax; x++) | 
| 1177 | < | *dp++ = scanline[x][RED] | | 
| 1178 | < | scanline[x][GRN] << 8 | | 
| 1179 | < | scanline[x][BLU] << 16 ; | 
| 1177 | > | *dp++ = (unsigned int4)scanline[x][RED] | | 
| 1178 | > | (unsigned int4)scanline[x][GRN] << 8 | | 
| 1179 | > | (unsigned int4)scanline[x][BLU] << 16 ; | 
| 1180 | > | break; | 
| 1181 | > | #if 0 | 
| 1182 | > | case 0x1f:              /* 15-bit RGB */ | 
| 1183 | > | for (x = 0; x < xmax; x++) | 
| 1184 | > | *dph++ =        (scanline[x][RED] << 7 & 0x7c00) | | 
| 1185 | > | (scanline[x][GRN] << 2 & 0x3e0) | | 
| 1186 | > | (unsigned)scanline[x][BLU] >> 3 ; | 
| 1187 | > | break; | 
| 1188 | > | case 0x7c00:            /* 15-bit BGR */ | 
| 1189 | > | for (x = 0; x < xmax; x++) | 
| 1190 | > | *dph++ =        (unsigned)scanline[x][RED] >> 3 | | 
| 1191 | > | (scanline[x][GRN] << 2 & 0x3e0) | | 
| 1192 | > | (scanline[x][BLU] << 7 & 0x7c00) ; | 
| 1193 | > | break; | 
| 1194 | > | #endif | 
| 1195 | > | default:                /* unknown */ | 
| 1196 | > | if (ourvis.depth > 16) | 
| 1197 | > | for (x = 0; x < xmax; x++) { | 
| 1198 | > | *dp = ourras->image->red_mask & | 
| 1199 | > | ourras->image->red_mask*scanline[x][RED]/255; | 
| 1200 | > | *dp |= ourras->image->green_mask & | 
| 1201 | > | ourras->image->green_mask*scanline[x][GRN]/255; | 
| 1202 | > | *dp++ |= ourras->image->blue_mask & | 
| 1203 | > | ourras->image->blue_mask*scanline[x][BLU]/255; | 
| 1204 | > | } | 
| 1205 | > | else | 
| 1206 | > | for (x = 0; x < xmax; x++) { | 
| 1207 | > | *dph = ourras->image->red_mask & | 
| 1208 | > | ourras->image->red_mask*scanline[x][RED]/255; | 
| 1209 | > | *dph |= ourras->image->green_mask & | 
| 1210 | > | ourras->image->green_mask*scanline[x][GRN]/255; | 
| 1211 | > | *dph++ |= ourras->image->blue_mask & | 
| 1212 | > | ourras->image->blue_mask*scanline[x][BLU]/255; | 
| 1213 | > | } | 
| 1214 | > | break; | 
| 1215 | > | } | 
| 1216 |  | } | 
| 1217 |  | } | 
| 1218 |  |  | 
| 1222 |  | int     y; | 
| 1223 |  | register unsigned char  *dp; | 
| 1224 |  | register int    x; | 
| 1225 | < | /* set gamma correction */ | 
| 1226 | < | setcolrgam(gamcor); | 
| 1225 | > | /* initialize tone mapping */ | 
| 1226 | > | make_tonemap(); | 
| 1227 |  | /* read and convert file */ | 
| 1228 |  | dp = ourdata; | 
| 1229 |  | for (y = 0; y < ymax; y++) { | 
| 1230 |  | getscan(y); | 
| 1231 |  | add2icon(y, scanline); | 
| 1232 | < | 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); | 
| 1232 | > | tmap_colrs(scanline, xmax); | 
| 1233 |  | if (maxcolors < 256) | 
| 1234 |  | for (x = 0; x < xmax; x++) | 
| 1235 | < | *dp++ = ((long)scanline[x][GRN] * | 
| 1235 | > | *dp++ = ((int4)scanline[x][GRN] * | 
| 1236 |  | maxcolors + maxcolors/2) >> 8; | 
| 1237 |  | else | 
| 1238 |  | for (x = 0; x < xmax; x++) | 
| 1240 |  | } | 
| 1241 |  | for (x = 0; x < maxcolors; x++) | 
| 1242 |  | clrtab[x][RED] = clrtab[x][GRN] = | 
| 1243 | < | clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; | 
| 1243 | > | clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; | 
| 1244 |  | } | 
| 1245 |  |  | 
| 1246 |  |  | 
| 1247 |  | getmapped()                     /* get color-mapped data */ | 
| 1248 |  | { | 
| 1249 |  | int     y; | 
| 1250 | < | /* set gamma correction */ | 
| 1251 | < | setcolrgam(gamcor); | 
| 1250 | > | /* make sure we can do it first */ | 
| 1251 | > | if (fname == NULL) | 
| 1252 | > | quiterr("cannot map colors from standard input"); | 
| 1253 | > | /* initialize tone mapping */ | 
| 1254 | > | make_tonemap(); | 
| 1255 |  | /* make histogram */ | 
| 1256 | < | new_histo(); | 
| 1256 | > | if (new_histo((int4)xmax*ymax) == -1) | 
| 1257 | > | quiterr("cannot initialize histogram"); | 
| 1258 |  | for (y = 0; y < ymax; y++) { | 
| 1259 |  | if (getscan(y) < 0) | 
| 1260 | < | quiterr("seek error in getmapped"); | 
| 1260 | > | break; | 
| 1261 |  | add2icon(y, scanline); | 
| 1262 | < | if (scale) | 
| 982 | < | shiftcolrs(scanline, xmax, scale); | 
| 983 | < | colrs_gambs(scanline, xmax); | 
| 1262 | > | tmap_colrs(scanline, xmax); | 
| 1263 |  | cnt_colrs(scanline, xmax); | 
| 1264 |  | } | 
| 1265 |  | /* map pixels */ | 
| 1266 |  | if (!new_clrtab(maxcolors)) | 
| 1267 |  | quiterr("cannot create color map"); | 
| 1268 |  | for (y = 0; y < ymax; y++) { | 
| 1269 | < | if (getscan(y) < 0) | 
| 1270 | < | quiterr("seek error in getmapped"); | 
| 992 | < | if (scale) | 
| 993 | < | shiftcolrs(scanline, xmax, scale); | 
| 994 | < | colrs_gambs(scanline, xmax); | 
| 1269 | > | getscan(y); | 
| 1270 | > | tmap_colrs(scanline, xmax); | 
| 1271 |  | if (dither) | 
| 1272 |  | dith_colrs(ourdata+y*xmax, scanline, xmax); | 
| 1273 |  | else | 
| 1307 |  | getscan(y) | 
| 1308 |  | int  y; | 
| 1309 |  | { | 
| 1310 | + | static int  trunced = -1;               /* truncated file? */ | 
| 1311 | + | skipit: | 
| 1312 | + | if (trunced >= 0 && y >= trunced) { | 
| 1313 | + | bzero(scanline, xmax*sizeof(COLR)); | 
| 1314 | + | return(-1); | 
| 1315 | + | } | 
| 1316 |  | if (y != cury) { | 
| 1317 |  | if (scanpos == NULL || scanpos[y] == -1) | 
| 1318 |  | return(-1); | 
| 1322 |  | } else if (scanpos != NULL && scanpos[y] == -1) | 
| 1323 |  | scanpos[y] = ftell(fin); | 
| 1324 |  |  | 
| 1325 | < | if (freadcolrs(scanline, xmax, fin) < 0) | 
| 1326 | < | quiterr("read error"); | 
| 1327 | < |  | 
| 1325 | > | if (freadcolrs(scanline, xmax, fin) < 0) { | 
| 1326 | > | fprintf(stderr, "%s: %s: unfinished picture\n", | 
| 1327 | > | progname, fname==NULL?"<stdin>":fname); | 
| 1328 | > | trunced = y; | 
| 1329 | > | goto skipit; | 
| 1330 | > | } | 
| 1331 |  | cury++; | 
| 1332 |  | return(0); | 
| 1333 |  | } |