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