| 1 | /* Copyright (c) 1991 Regents of the University of California */ | 
| 2 |  | 
| 3 | #ifndef lint | 
| 4 | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 | #endif | 
| 6 |  | 
| 7 | /* | 
| 8 | *  x11image.c - driver for X-windows | 
| 9 | * | 
| 10 | *     3/1/90 | 
| 11 | */ | 
| 12 |  | 
| 13 | /* | 
| 14 | *  Modified for X11 | 
| 15 | * | 
| 16 | *  January 1990 | 
| 17 | * | 
| 18 | *  Anat Grynberg  and Greg Ward | 
| 19 | */ | 
| 20 |  | 
| 21 |  | 
| 22 | #include  "standard.h" | 
| 23 |  | 
| 24 | #include  <X11/Xlib.h> | 
| 25 | #include  <X11/cursorfont.h> | 
| 26 | #include  <X11/Xutil.h> | 
| 27 |  | 
| 28 | #include  "color.h" | 
| 29 | #include  "view.h" | 
| 30 | #include  "pic.h" | 
| 31 | #include  "x11raster.h" | 
| 32 | #include  "random.h" | 
| 33 |  | 
| 34 | #define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 35 |  | 
| 36 | #define  CTRL(c)        ('c'-'@') | 
| 37 |  | 
| 38 | #define  BORWIDTH       5               /* border width */ | 
| 39 |  | 
| 40 | #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */ | 
| 41 |  | 
| 42 | #define  ourscreen      DefaultScreen(thedisplay) | 
| 43 | #define  ourblack       BlackPixel(thedisplay,ourscreen) | 
| 44 | #define  ourwhite       WhitePixel(thedisplay,ourscreen) | 
| 45 | #define  ourroot        RootWindow(thedisplay,ourscreen) | 
| 46 | #define  ourgc          DefaultGC(thedisplay,ourscreen) | 
| 47 |  | 
| 48 | #define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) | 
| 49 |  | 
| 50 | #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) | 
| 51 |  | 
| 52 | double  gamcor = 2.2;                   /* gamma correction */ | 
| 53 |  | 
| 54 | int  dither = 1;                        /* dither colors? */ | 
| 55 | int  fast = 0;                          /* keep picture in Pixmap? */ | 
| 56 |  | 
| 57 | Window  wind = 0;                       /* our output window */ | 
| 58 | Font  fontid;                           /* our font */ | 
| 59 |  | 
| 60 | int  maxcolors = 0;                     /* maximum colors */ | 
| 61 | int  greyscale = 0;                     /* in grey */ | 
| 62 |  | 
| 63 | int  scale = 0;                         /* scalefactor; power of two */ | 
| 64 |  | 
| 65 | int  xoff = 0;                          /* x image offset */ | 
| 66 | int  yoff = 0;                          /* y image offset */ | 
| 67 |  | 
| 68 | VIEW  ourview = STDVIEW;                /* image view parameters */ | 
| 69 | int  gotview = 0;                       /* got parameters from file */ | 
| 70 |  | 
| 71 | COLR  *scanline;                        /* scan line buffer */ | 
| 72 |  | 
| 73 | int  xmax, ymax;                        /* picture resolution */ | 
| 74 | int  width, height;                     /* window size */ | 
| 75 | char  *fname = NULL;                    /* input file name */ | 
| 76 | FILE  *fin = stdin;                     /* input file */ | 
| 77 | long  *scanpos = NULL;                  /* scan line positions in file */ | 
| 78 | int  cury = 0;                          /* current scan location */ | 
| 79 |  | 
| 80 | double  exposure = 1.0;                 /* exposure compensation used */ | 
| 81 |  | 
| 82 | int  wrongformat = 0;                   /* input in another format? */ | 
| 83 |  | 
| 84 | GC      revgc;                          /* graphics context with GXinvert */ | 
| 85 |  | 
| 86 | XRASTER *ourras;                        /* our stored image */ | 
| 87 | unsigned char   *ourdata;               /* our image data */ | 
| 88 |  | 
| 89 | struct { | 
| 90 | int  xmin, ymin, xsiz, ysiz; | 
| 91 | }  box = {0, 0, 0, 0};                  /* current box */ | 
| 92 |  | 
| 93 | char  *geometry = NULL;                 /* geometry specification */ | 
| 94 |  | 
| 95 | char  icondata[ICONSIZ*ICONSIZ/8];      /* icon bitmap data */ | 
| 96 | int  iconwidth = 0, iconheight = 0; | 
| 97 |  | 
| 98 | char  *progname; | 
| 99 |  | 
| 100 | char  errmsg[128]; | 
| 101 |  | 
| 102 | extern long  ftell(); | 
| 103 |  | 
| 104 | extern char  *malloc(), *calloc(); | 
| 105 |  | 
| 106 | extern double  atof(), pow(), log(); | 
| 107 |  | 
| 108 | Display  *thedisplay; | 
| 109 |  | 
| 110 | main(argc, argv) | 
| 111 | int  argc; | 
| 112 | char  *argv[]; | 
| 113 | { | 
| 114 | int  headline(); | 
| 115 | int  i; | 
| 116 |  | 
| 117 | progname = argv[0]; | 
| 118 |  | 
| 119 | for (i = 1; i < argc; i++) | 
| 120 | if (argv[i][0] == '-') | 
| 121 | switch (argv[i][1]) { | 
| 122 | case 'c': | 
| 123 | maxcolors = atoi(argv[++i]); | 
| 124 | break; | 
| 125 | case 'b': | 
| 126 | greyscale = !greyscale; | 
| 127 | break; | 
| 128 | case 'm': | 
| 129 | maxcolors = 2; | 
| 130 | break; | 
| 131 | case 'd': | 
| 132 | dither = !dither; | 
| 133 | break; | 
| 134 | case 'f': | 
| 135 | fast = !fast; | 
| 136 | break; | 
| 137 | case 'e': | 
| 138 | if (argv[i+1][0] != '+' && argv[i+1][0] != '-') | 
| 139 | goto userr; | 
| 140 | scale = atoi(argv[++i]); | 
| 141 | break; | 
| 142 | case 'g': | 
| 143 | if (!strcmp(argv[i], "-geometry")) | 
| 144 | geometry = argv[++i]; | 
| 145 | else | 
| 146 | gamcor = atof(argv[++i]); | 
| 147 | break; | 
| 148 | default: | 
| 149 | goto userr; | 
| 150 | } | 
| 151 | else if (argv[i][0] == '=') | 
| 152 | geometry = argv[i]; | 
| 153 | else | 
| 154 | break; | 
| 155 |  | 
| 156 | if (i == argc-1) { | 
| 157 | fname = argv[i]; | 
| 158 | fin = fopen(fname, "r"); | 
| 159 | if (fin == NULL) { | 
| 160 | sprintf(errmsg, "can't open file \"%s\"", fname); | 
| 161 | quiterr(errmsg); | 
| 162 | } | 
| 163 | } else if (i != argc) | 
| 164 | goto userr; | 
| 165 | /* get header */ | 
| 166 | getheader(fin, headline, NULL); | 
| 167 | /* get picture dimensions */ | 
| 168 | if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) | 
| 169 | quiterr("bad picture format"); | 
| 170 | /* set view parameters */ | 
| 171 | if (gotview && setview(&ourview) != NULL) | 
| 172 | gotview = 0; | 
| 173 | if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) | 
| 174 | quiterr("out of memory"); | 
| 175 |  | 
| 176 | init();                 /* get file and open window */ | 
| 177 |  | 
| 178 | for ( ; ; ) | 
| 179 | getevent();             /* main loop */ | 
| 180 | userr: | 
| 181 | fprintf(stderr, | 
| 182 | "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", | 
| 183 | progname); | 
| 184 | quit(1); | 
| 185 | } | 
| 186 |  | 
| 187 |  | 
| 188 | headline(s)             /* get relevant info from header */ | 
| 189 | char  *s; | 
| 190 | { | 
| 191 | static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; | 
| 192 | register char  **an; | 
| 193 | char  fmt[32]; | 
| 194 |  | 
| 195 | if (isexpos(s)) | 
| 196 | exposure *= exposval(s); | 
| 197 | else if (isformat(s)) { | 
| 198 | formatval(fmt, s); | 
| 199 | wrongformat = strcmp(fmt, COLRFMT); | 
| 200 | } else | 
| 201 | for (an = altname; *an != NULL; an++) | 
| 202 | if (!strncmp(*an, s, strlen(*an))) { | 
| 203 | if (sscanview(&ourview, s+strlen(*an)) > 0) | 
| 204 | gotview++; | 
| 205 | return; | 
| 206 | } | 
| 207 | } | 
| 208 |  | 
| 209 |  | 
| 210 | init()                  /* get data and open window */ | 
| 211 | { | 
| 212 | XWMHints        ourxwmhints; | 
| 213 | XSetWindowAttributes    ourwinattr; | 
| 214 | XSizeHints  oursizhints; | 
| 215 | register int  i; | 
| 216 |  | 
| 217 | if (fname != NULL) { | 
| 218 | scanpos = (long *)malloc(ymax*sizeof(long)); | 
| 219 | if (scanpos == NULL) | 
| 220 | goto memerr; | 
| 221 | for (i = 0; i < ymax; i++) | 
| 222 | scanpos[i] = -1; | 
| 223 | } | 
| 224 | if ((thedisplay = XOpenDisplay(NULL)) == NULL) | 
| 225 | quiterr("can't open display; DISPLAY variable set?"); | 
| 226 | if (maxcolors == 0) {           /* get number of available colors */ | 
| 227 | i = DisplayPlanes(thedisplay,ourscreen); | 
| 228 | maxcolors = i > 8 ? 256 : 1<<i; | 
| 229 | if (maxcolors > 4) maxcolors -= 2; | 
| 230 | } | 
| 231 | /* store image */ | 
| 232 | getras(); | 
| 233 | /* open window */ | 
| 234 | ourwinattr.border_pixel = ourblack; | 
| 235 | ourwinattr.background_pixel = ourwhite; | 
| 236 | wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, | 
| 237 | 0, InputOutput, ourras->visual, | 
| 238 | CWBackPixel|CWBorderPixel, &ourwinattr); | 
| 239 | if (wind == 0) | 
| 240 | quiterr("can't create window"); | 
| 241 | width = xmax; | 
| 242 | height = ymax; | 
| 243 | fontid = XLoadFont(thedisplay, FONTNAME); | 
| 244 | if (fontid == 0) | 
| 245 | quiterr("can't get font"); | 
| 246 | XSetFont(thedisplay, ourgc, fontid); | 
| 247 | revgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 248 | XSetFunction(thedisplay, revgc, GXinvert); | 
| 249 | XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, | 
| 250 | XC_diamond_cross)); | 
| 251 | XStoreName(thedisplay, wind, fname == NULL ? progname : fname); | 
| 252 | if (geometry != NULL) { | 
| 253 | bzero((char *)&oursizhints, sizeof(oursizhints)); | 
| 254 | i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, | 
| 255 | (unsigned *)&oursizhints.width, | 
| 256 | (unsigned *)&oursizhints.height); | 
| 257 | if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) | 
| 258 | oursizhints.flags |= USSize; | 
| 259 | else { | 
| 260 | oursizhints.width = xmax; | 
| 261 | oursizhints.height = ymax; | 
| 262 | oursizhints.flags |= PSize; | 
| 263 | } | 
| 264 | if ((i&(XValue|YValue)) == (XValue|YValue)) { | 
| 265 | oursizhints.flags |= USPosition; | 
| 266 | if (i & XNegative) | 
| 267 | oursizhints.x += DisplayWidth(thedisplay, | 
| 268 | ourscreen)-1-oursizhints.width-2*BORWIDTH; | 
| 269 | if (i & YNegative) | 
| 270 | oursizhints.y += DisplayHeight(thedisplay, | 
| 271 | ourscreen)-1-oursizhints.height-2*BORWIDTH; | 
| 272 | } | 
| 273 | XSetNormalHints(thedisplay, wind, &oursizhints); | 
| 274 | } | 
| 275 | ourxwmhints.flags = InputHint|IconPixmapHint; | 
| 276 | ourxwmhints.input = True; | 
| 277 | ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, | 
| 278 | wind, icondata, iconwidth, iconheight); | 
| 279 | XSetWMHints(thedisplay, wind, &ourxwmhints); | 
| 280 | XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask | 
| 281 | |ButtonMotionMask|StructureNotifyMask | 
| 282 | |KeyPressMask|ExposureMask); | 
| 283 | XMapWindow(thedisplay, wind); | 
| 284 | return; | 
| 285 | memerr: | 
| 286 | quiterr("out of memory"); | 
| 287 | } /* end of init */ | 
| 288 |  | 
| 289 |  | 
| 290 | quiterr(err)            /* print message and exit */ | 
| 291 | char  *err; | 
| 292 | { | 
| 293 | if (err != NULL) { | 
| 294 | fprintf(stderr, "%s: %s\n", progname, err); | 
| 295 | exit(1); | 
| 296 | } | 
| 297 | exit(0); | 
| 298 | } | 
| 299 |  | 
| 300 |  | 
| 301 | eputs(s) | 
| 302 | char    *s; | 
| 303 | { | 
| 304 | fputs(s, stderr); | 
| 305 | } | 
| 306 |  | 
| 307 |  | 
| 308 | quit(code) | 
| 309 | int  code; | 
| 310 | { | 
| 311 | exit(code); | 
| 312 | } | 
| 313 |  | 
| 314 |  | 
| 315 | getras()                                /* get raster file */ | 
| 316 | { | 
| 317 | colormap        ourmap; | 
| 318 | XVisualInfo     vinfo; | 
| 319 |  | 
| 320 | if (maxcolors <= 2) {           /* monochrome */ | 
| 321 | ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); | 
| 322 | if (ourdata == NULL) | 
| 323 | goto fail; | 
| 324 | ourras = make_raster(thedisplay, ourscreen, 1, ourdata, | 
| 325 | xmax, ymax, 8); | 
| 326 | if (ourras == NULL) | 
| 327 | goto fail; | 
| 328 | getmono(); | 
| 329 | } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) | 
| 330 | /* kludge for DirectColor */ | 
| 331 | || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { | 
| 332 | ourdata = (unsigned char *)malloc(xmax*ymax*3); | 
| 333 | if (ourdata == NULL) | 
| 334 | goto fail; | 
| 335 | ourras = make_raster(thedisplay, ourscreen, 24, ourdata, | 
| 336 | xmax, ymax, 8); | 
| 337 | if (ourras == NULL) | 
| 338 | goto fail; | 
| 339 | getfull(); | 
| 340 | } else { | 
| 341 | ourdata = (unsigned char *)malloc(xmax*ymax); | 
| 342 | if (ourdata == NULL) | 
| 343 | goto fail; | 
| 344 | ourras = make_raster(thedisplay, ourscreen, 8, ourdata, | 
| 345 | xmax, ymax, 8); | 
| 346 | if (ourras == NULL) | 
| 347 | goto fail; | 
| 348 | if (greyscale) | 
| 349 | biq(dither,maxcolors,1,ourmap); | 
| 350 | else | 
| 351 | ciq(dither,maxcolors,1,ourmap); | 
| 352 | if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) | 
| 353 | goto fail; | 
| 354 | } | 
| 355 | return; | 
| 356 | fail: | 
| 357 | quiterr("could not create raster image"); | 
| 358 | } | 
| 359 |  | 
| 360 |  | 
| 361 | getevent()                              /* process the next event */ | 
| 362 | { | 
| 363 | union { | 
| 364 | XEvent  u; | 
| 365 | XConfigureEvent  c; | 
| 366 | XExposeEvent  e; | 
| 367 | XButtonPressedEvent  b; | 
| 368 | XKeyPressedEvent  k; | 
| 369 | } e; | 
| 370 |  | 
| 371 | XNextEvent(thedisplay, &e.u); | 
| 372 | switch (e.u.type) { | 
| 373 | case KeyPress: | 
| 374 | docom(&e.k); | 
| 375 | break; | 
| 376 | case ConfigureNotify: | 
| 377 | width = e.c.width; | 
| 378 | height = e.c.height; | 
| 379 | break; | 
| 380 | case MapNotify: | 
| 381 | map_rcolors(ourras, wind); | 
| 382 | if (fast) | 
| 383 | make_rpixmap(ourras); | 
| 384 | break; | 
| 385 | case UnmapNotify: | 
| 386 | unmap_rcolors(ourras); | 
| 387 | break; | 
| 388 | case Expose: | 
| 389 | redraw(e.e.x, e.e.y, e.e.width, e.e.height); | 
| 390 | break; | 
| 391 | case ButtonPress: | 
| 392 | if (e.b.state & (ShiftMask|ControlMask)) | 
| 393 | moveimage(&e.b); | 
| 394 | else | 
| 395 | getbox(&e.b); | 
| 396 | break; | 
| 397 | } | 
| 398 | } | 
| 399 |  | 
| 400 |  | 
| 401 | docom(ekey)                                     /* execute command */ | 
| 402 | XKeyPressedEvent  *ekey; | 
| 403 | { | 
| 404 | char  buf[80]; | 
| 405 | COLOR  cval; | 
| 406 | XColor  cvx; | 
| 407 | int  com, n; | 
| 408 | double  comp; | 
| 409 | FVECT  rorg, rdir; | 
| 410 |  | 
| 411 | n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); | 
| 412 | if (n == 0) | 
| 413 | return(0); | 
| 414 | com = buf[0]; | 
| 415 | switch (com) {                  /* interpret command */ | 
| 416 | case 'q': | 
| 417 | case CTRL(D):                           /* quit */ | 
| 418 | quit(0); | 
| 419 | case '\n': | 
| 420 | case '\r': | 
| 421 | case 'l': | 
| 422 | case 'c':                               /* value */ | 
| 423 | if (avgbox(cval) == -1) | 
| 424 | return(-1); | 
| 425 | switch (com) { | 
| 426 | case '\n': | 
| 427 | case '\r':                              /* radiance */ | 
| 428 | sprintf(buf, "%.3f", intens(cval)/exposure); | 
| 429 | break; | 
| 430 | case 'l':                               /* luminance */ | 
| 431 | sprintf(buf, "%.0fL", luminance(cval)/exposure); | 
| 432 | break; | 
| 433 | case 'c':                               /* color */ | 
| 434 | comp = pow(2.0, (double)scale); | 
| 435 | sprintf(buf, "(%.2f,%.2f,%.2f)", | 
| 436 | colval(cval,RED)*comp, | 
| 437 | colval(cval,GRN)*comp, | 
| 438 | colval(cval,BLU)*comp); | 
| 439 | break; | 
| 440 | } | 
| 441 | XDrawImageString(thedisplay, wind, ourgc, | 
| 442 | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 443 | return(0); | 
| 444 | case 'i':                               /* identify (contour) */ | 
| 445 | if (ourras->pixels == NULL) | 
| 446 | return(-1); | 
| 447 | n = ourdata[ekey->x-xoff+xmax*(ekey->y-yoff)]; | 
| 448 | n = ourras->pmap[n]; | 
| 449 | cvx.pixel = ourras->cdefs[n].pixel; | 
| 450 | cvx.red = random() & 65535; | 
| 451 | cvx.green = random() & 65535; | 
| 452 | cvx.blue = random() & 65535; | 
| 453 | cvx.flags = DoRed|DoGreen|DoBlue; | 
| 454 | XStoreColor(thedisplay, ourras->cmap, &cvx); | 
| 455 | return(0); | 
| 456 | case 'p':                               /* position */ | 
| 457 | sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); | 
| 458 | XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y, | 
| 459 | buf, strlen(buf)); | 
| 460 | return(0); | 
| 461 | case 't':                               /* trace */ | 
| 462 | if (!gotview) { | 
| 463 | XBell(thedisplay, 0); | 
| 464 | return(-1); | 
| 465 | } | 
| 466 | if (viewray(rorg, rdir, &ourview, | 
| 467 | (ekey->x-xoff+.5)/xmax, | 
| 468 | (ymax-1-ekey->y+yoff+.5)/ymax) < 0) | 
| 469 | return(-1); | 
| 470 | printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 471 | printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 472 | fflush(stdout); | 
| 473 | return(0); | 
| 474 | case '=':                               /* adjust exposure */ | 
| 475 | if (avgbox(cval) == -1) | 
| 476 | return(-1); | 
| 477 | n = log(.5/bright(cval))/.69315 - scale;        /* truncate */ | 
| 478 | if (n == 0) | 
| 479 | return(0); | 
| 480 | scale_rcolors(ourras, pow(2.0, (double)n)); | 
| 481 | scale += n; | 
| 482 | sprintf(buf, "%+d", scale); | 
| 483 | XDrawImageString(thedisplay, wind, ourgc, | 
| 484 | box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); | 
| 485 | XFlush(thedisplay); | 
| 486 | free(ourdata); | 
| 487 | free_raster(ourras); | 
| 488 | getras(); | 
| 489 | /* fall through */ | 
| 490 | case CTRL(R):                           /* redraw */ | 
| 491 | case CTRL(L): | 
| 492 | unmap_rcolors(ourras); | 
| 493 | XClearWindow(thedisplay, wind); | 
| 494 | map_rcolors(ourras, wind); | 
| 495 | if (fast) | 
| 496 | make_rpixmap(ourras); | 
| 497 | redraw(0, 0, width, height); | 
| 498 | return(0); | 
| 499 | case ' ':                               /* clear */ | 
| 500 | redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); | 
| 501 | return(0); | 
| 502 | default: | 
| 503 | XBell(thedisplay, 0); | 
| 504 | return(-1); | 
| 505 | } | 
| 506 | } | 
| 507 |  | 
| 508 |  | 
| 509 | moveimage(ebut)                         /* shift the image */ | 
| 510 | XButtonPressedEvent  *ebut; | 
| 511 | { | 
| 512 | union { | 
| 513 | XEvent  u; | 
| 514 | XButtonReleasedEvent  b; | 
| 515 | XPointerMovedEvent  m; | 
| 516 | }  e; | 
| 517 | int     mxo, myo; | 
| 518 |  | 
| 519 | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); | 
| 520 | while (e.u.type == MotionNotify) { | 
| 521 | mxo = e.m.x; | 
| 522 | myo = e.m.y; | 
| 523 | revline(ebut->x, ebut->y, mxo, myo); | 
| 524 | revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, | 
| 525 | xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); | 
| 526 | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); | 
| 527 | revline(ebut->x, ebut->y, mxo, myo); | 
| 528 | revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, | 
| 529 | xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); | 
| 530 | } | 
| 531 | xoff += e.b.x - ebut->x; | 
| 532 | yoff += e.b.y - ebut->y; | 
| 533 | XClearWindow(thedisplay, wind); | 
| 534 | redraw(0, 0, width, height); | 
| 535 | } | 
| 536 |  | 
| 537 |  | 
| 538 | getbox(ebut)                            /* get new box */ | 
| 539 | XButtonPressedEvent  *ebut; | 
| 540 | { | 
| 541 | union { | 
| 542 | XEvent  u; | 
| 543 | XButtonReleasedEvent  b; | 
| 544 | XPointerMovedEvent  m; | 
| 545 | }  e; | 
| 546 |  | 
| 547 | XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); | 
| 548 | while (e.u.type == MotionNotify) { | 
| 549 | revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); | 
| 550 | XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); | 
| 551 | revbox(ebut->x, ebut->y, box.xmin, box.ymin); | 
| 552 | } | 
| 553 | box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); | 
| 554 | box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); | 
| 555 | if (box.xmin > ebut->x) { | 
| 556 | box.xsiz = box.xmin - ebut->x + 1; | 
| 557 | box.xmin = ebut->x; | 
| 558 | } else { | 
| 559 | box.xsiz = ebut->x - box.xmin + 1; | 
| 560 | } | 
| 561 | if (box.ymin > ebut->y) { | 
| 562 | box.ysiz = box.ymin - ebut->y + 1; | 
| 563 | box.ymin = ebut->y; | 
| 564 | } else { | 
| 565 | box.ysiz = ebut->y - box.ymin + 1; | 
| 566 | } | 
| 567 | } | 
| 568 |  | 
| 569 |  | 
| 570 | revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */ | 
| 571 | int  x0, y0, x1, y1; | 
| 572 | { | 
| 573 | revline(x0, y0, x1, y0); | 
| 574 | revline(x0, y1, x1, y1); | 
| 575 | revline(x0, y0, x0, y1); | 
| 576 | revline(x1, y0, x1, y1); | 
| 577 | } | 
| 578 |  | 
| 579 |  | 
| 580 | avgbox(clr)                             /* average color over current box */ | 
| 581 | COLOR  clr; | 
| 582 | { | 
| 583 | static COLOR  lc; | 
| 584 | static int  ll, lr, lt, lb; | 
| 585 | int  left, right, top, bottom; | 
| 586 | int  y; | 
| 587 | double  d; | 
| 588 | COLOR  ctmp; | 
| 589 | register int  x; | 
| 590 |  | 
| 591 | setcolor(clr, 0.0, 0.0, 0.0); | 
| 592 | left = box.xmin - xoff; | 
| 593 | right = left + box.xsiz; | 
| 594 | if (left < 0) | 
| 595 | left = 0; | 
| 596 | if (right > xmax) | 
| 597 | right = xmax; | 
| 598 | if (left >= right) | 
| 599 | return(-1); | 
| 600 | top = box.ymin - yoff; | 
| 601 | bottom = top + box.ysiz; | 
| 602 | if (top < 0) | 
| 603 | top = 0; | 
| 604 | if (bottom > ymax) | 
| 605 | bottom = ymax; | 
| 606 | if (top >= bottom) | 
| 607 | return(-1); | 
| 608 | if (left == ll && right == lr && top == lt && bottom == lb) { | 
| 609 | copycolor(clr, lc); | 
| 610 | return; | 
| 611 | } | 
| 612 | for (y = top; y < bottom; y++) { | 
| 613 | if (getscan(y) == -1) | 
| 614 | return(-1); | 
| 615 | for (x = left; x < right; x++) { | 
| 616 | colr_color(ctmp, scanline[x]); | 
| 617 | addcolor(clr, ctmp); | 
| 618 | } | 
| 619 | } | 
| 620 | d = 1.0/((right-left)*(bottom-top)); | 
| 621 | scalecolor(clr, d); | 
| 622 | ll = left; lr = right; lt = top; lb = bottom; | 
| 623 | copycolor(lc, clr); | 
| 624 | return(0); | 
| 625 | } | 
| 626 |  | 
| 627 |  | 
| 628 | getmono()                       /* get monochrome data */ | 
| 629 | { | 
| 630 | register unsigned char  *dp; | 
| 631 | register int    x, err; | 
| 632 | int     y, errp; | 
| 633 | short   *cerr; | 
| 634 |  | 
| 635 | if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) | 
| 636 | quiterr("out of memory in getmono"); | 
| 637 | dp = ourdata - 1; | 
| 638 | for (y = 0; y < ymax; y++) { | 
| 639 | if (getscan(y) < 0) | 
| 640 | quiterr("seek error in getmono"); | 
| 641 | normcolrs(scanline, xmax, scale); | 
| 642 | add2icon(y, scanline); | 
| 643 | err = 0; | 
| 644 | for (x = 0; x < xmax; x++) { | 
| 645 | if (!(x&7)) | 
| 646 | *++dp = 0; | 
| 647 | errp = err; | 
| 648 | err += normbright(scanline[x]) + cerr[x]; | 
| 649 | if (err > 127) | 
| 650 | err -= 255; | 
| 651 | else | 
| 652 | *dp |= 1<<(7-(x&07)); | 
| 653 | err /= 3; | 
| 654 | cerr[x] = err + errp; | 
| 655 | } | 
| 656 | } | 
| 657 | free((char *)cerr); | 
| 658 | } | 
| 659 |  | 
| 660 |  | 
| 661 | add2icon(y, scan)               /* add a scanline to our icon data */ | 
| 662 | int  y; | 
| 663 | COLR  *scan; | 
| 664 | { | 
| 665 | static short  cerr[ICONSIZ]; | 
| 666 | static int  ynext; | 
| 667 | static char  *dp; | 
| 668 | register int  err; | 
| 669 | register int    x, ti; | 
| 670 | int  errp; | 
| 671 |  | 
| 672 | if (iconheight == 0) {          /* initialize */ | 
| 673 | if (xmax <= ICONSIZ && ymax <= ICONSIZ) { | 
| 674 | iconwidth = xmax; | 
| 675 | iconheight = ymax; | 
| 676 | } else if (xmax > ymax) { | 
| 677 | iconwidth = ICONSIZ; | 
| 678 | iconheight = ICONSIZ*ymax/xmax; | 
| 679 | if (iconheight < 1) | 
| 680 | iconheight = 1; | 
| 681 | } else { | 
| 682 | iconwidth = ICONSIZ*xmax/ymax; | 
| 683 | if (iconwidth < 1) | 
| 684 | iconwidth = 1; | 
| 685 | iconheight = ICONSIZ; | 
| 686 | } | 
| 687 | ynext = 0; | 
| 688 | dp = icondata - 1; | 
| 689 | } | 
| 690 | if (y < ynext*ymax/iconheight)  /* skip this one */ | 
| 691 | return; | 
| 692 | err = 0; | 
| 693 | for (x = 0; x < iconwidth; x++) { | 
| 694 | if (!(x&7)) | 
| 695 | *++dp = 0; | 
| 696 | errp = err; | 
| 697 | ti = x*xmax/iconwidth; | 
| 698 | err += normbright(scan[ti]) + cerr[x]; | 
| 699 | if (err > 127) | 
| 700 | err -= 255; | 
| 701 | else | 
| 702 | *dp |= 1<<(x&07); | 
| 703 | err /= 3; | 
| 704 | cerr[x] = err + errp; | 
| 705 | } | 
| 706 | ynext++; | 
| 707 | } | 
| 708 |  | 
| 709 |  | 
| 710 | getfull()                       /* get full (24-bit) data */ | 
| 711 | { | 
| 712 | int     y; | 
| 713 | register unsigned char  *dp; | 
| 714 | register int    x; | 
| 715 | /* set gamma correction */ | 
| 716 | setcolrgam(gamcor); | 
| 717 | /* read and convert file */ | 
| 718 | dp = ourdata; | 
| 719 | for (y = 0; y < ymax; y++) { | 
| 720 | if (getscan(y) < 0) | 
| 721 | quiterr("seek error in getfull"); | 
| 722 | if (scale) | 
| 723 | shiftcolrs(scanline, xmax, scale); | 
| 724 | colrs_gambs(scanline, xmax); | 
| 725 | add2icon(y, scanline); | 
| 726 | for (x = 0; x < xmax; x++) { | 
| 727 | *dp++ = scanline[x][RED]; | 
| 728 | *dp++ = scanline[x][GRN]; | 
| 729 | *dp++ = scanline[x][BLU]; | 
| 730 | } | 
| 731 | } | 
| 732 | } | 
| 733 |  | 
| 734 |  | 
| 735 | scale_rcolors(xr, sf)                   /* scale color map */ | 
| 736 | register XRASTER        *xr; | 
| 737 | double  sf; | 
| 738 | { | 
| 739 | register int    i; | 
| 740 | long    maxv; | 
| 741 |  | 
| 742 | if (xr->pixels == NULL) | 
| 743 | return; | 
| 744 |  | 
| 745 | sf = pow(sf, 1.0/gamcor); | 
| 746 | maxv = 65535/sf; | 
| 747 |  | 
| 748 | for (i = xr->ncolors; i--; ) { | 
| 749 | xr->cdefs[i].red = xr->cdefs[i].red > maxv ? | 
| 750 | 65535 : | 
| 751 | xr->cdefs[i].red * sf; | 
| 752 | xr->cdefs[i].green = xr->cdefs[i].green > maxv ? | 
| 753 | 65535 : | 
| 754 | xr->cdefs[i].green * sf; | 
| 755 | xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? | 
| 756 | 65535 : | 
| 757 | xr->cdefs[i].blue * sf; | 
| 758 | } | 
| 759 | XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors); | 
| 760 | } | 
| 761 |  | 
| 762 |  | 
| 763 | getscan(y) | 
| 764 | int  y; | 
| 765 | { | 
| 766 | if (y != cury) { | 
| 767 | if (scanpos == NULL || scanpos[y] == -1) | 
| 768 | return(-1); | 
| 769 | if (fseek(fin, scanpos[y], 0) == -1) | 
| 770 | quiterr("fseek error"); | 
| 771 | cury = y; | 
| 772 | } else if (scanpos != NULL) | 
| 773 | scanpos[y] = ftell(fin); | 
| 774 |  | 
| 775 | if (freadcolrs(scanline, xmax, fin) < 0) | 
| 776 | quiterr("read error"); | 
| 777 |  | 
| 778 | cury++; | 
| 779 | return(0); | 
| 780 | } | 
| 781 |  | 
| 782 |  | 
| 783 | picreadline3(y, l3)                     /* read in 3-byte scanline */ | 
| 784 | int  y; | 
| 785 | register rgbpixel  *l3; | 
| 786 | { | 
| 787 | register int    i; | 
| 788 | /* read scanline */ | 
| 789 | if (getscan(y) < 0) | 
| 790 | quiterr("cannot seek for picreadline"); | 
| 791 | /* convert scanline */ | 
| 792 | normcolrs(scanline, xmax, scale); | 
| 793 | add2icon(y, scanline); | 
| 794 | for (i = 0; i < xmax; i++) { | 
| 795 | l3[i].r = scanline[i][RED]; | 
| 796 | l3[i].g = scanline[i][GRN]; | 
| 797 | l3[i].b = scanline[i][BLU]; | 
| 798 | } | 
| 799 | } | 
| 800 |  | 
| 801 |  | 
| 802 | picwriteline(y, l)              /* add 8-bit scanline to image */ | 
| 803 | int  y; | 
| 804 | pixel  *l; | 
| 805 | { | 
| 806 | bcopy((char *)l, (char *)ourdata+y*xmax, xmax); | 
| 807 | } | 
| 808 |  | 
| 809 |  | 
| 810 | picreadcm(map)                  /* do gamma correction */ | 
| 811 | colormap  map; | 
| 812 | { | 
| 813 | extern double  pow(); | 
| 814 | register int  i, val; | 
| 815 |  | 
| 816 | for (i = 0; i < 256; i++) { | 
| 817 | val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; | 
| 818 | map[0][i] = map[1][i] = map[2][i] = val; | 
| 819 | } | 
| 820 | } | 
| 821 |  | 
| 822 |  | 
| 823 | picwritecm(map)                 /* handled elsewhere */ | 
| 824 | colormap  map; | 
| 825 | { | 
| 826 | #ifdef DEBUG | 
| 827 | register int i; | 
| 828 |  | 
| 829 | for (i = 0; i < 256; i++) | 
| 830 | printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); | 
| 831 | #endif | 
| 832 | } |