| 1 | 
< | 
/* Copyright (c) 1992 Regents of the University of California */ | 
| 1 | 
> | 
/* Copyright (c) 1993 Regents of the University of California */ | 
| 2 | 
  | 
 | 
| 3 | 
  | 
#ifndef lint | 
| 4 | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 21 | 
  | 
 | 
| 22 | 
  | 
#include  "standard.h" | 
| 23 | 
  | 
 | 
| 24 | 
+ | 
#include  <signal.h> | 
| 25 | 
  | 
#include  <X11/Xlib.h> | 
| 26 | 
  | 
#include  <X11/cursorfont.h> | 
| 27 | 
  | 
#include  <X11/Xutil.h> | 
| 28 | 
+ | 
#include  <X11/Xatom.h> | 
| 29 | 
  | 
 | 
| 30 | 
  | 
#include  "color.h" | 
| 31 | 
  | 
#include  "view.h" | 
| 33 | 
  | 
#include  "random.h" | 
| 34 | 
  | 
#include  "resolu.h" | 
| 35 | 
  | 
 | 
| 36 | 
+ | 
#ifdef  __alpha | 
| 37 | 
+ | 
#define  int4           int | 
| 38 | 
+ | 
#endif | 
| 39 | 
+ | 
#ifndef  int4 | 
| 40 | 
+ | 
#define  int4           long | 
| 41 | 
+ | 
#endif | 
| 42 | 
+ | 
 | 
| 43 | 
  | 
#define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 44 | 
  | 
 | 
| 45 | 
  | 
#define  CTRL(c)        ((c)-'@') | 
| 56 | 
  | 
#define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) | 
| 57 | 
  | 
 | 
| 58 | 
  | 
double  gamcor = 2.2;                   /* gamma correction */ | 
| 59 | 
+ | 
char  *gamstr = NULL;                   /* gamma value override */ | 
| 60 | 
  | 
 | 
| 61 | 
  | 
int  dither = 1;                        /* dither colors? */ | 
| 62 | 
  | 
int  fast = 0;                          /* keep picture in Pixmap? */ | 
| 65 | 
  | 
 | 
| 66 | 
  | 
Window  wind = 0;                       /* our output window */ | 
| 67 | 
  | 
unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */ | 
| 58 | 
– | 
Font  fontid;                           /* our font */ | 
| 59 | 
– | 
 | 
| 68 | 
  | 
int  maxcolors = 0;                     /* maximum colors */ | 
| 69 | 
  | 
int  greyscale = 0;                     /* in grey */ | 
| 70 | 
  | 
 | 
| 73 | 
  | 
int  xoff = 0;                          /* x image offset */ | 
| 74 | 
  | 
int  yoff = 0;                          /* y image offset */ | 
| 75 | 
  | 
 | 
| 76 | 
+ | 
int  parent = 0;                        /* number of children, -1 if child */ | 
| 77 | 
+ | 
 | 
| 78 | 
  | 
VIEW  ourview = STDVIEW;                /* image view parameters */ | 
| 79 | 
  | 
int  gotview = 0;                       /* got parameters from file */ | 
| 80 | 
  | 
 | 
| 113 | 
  | 
 | 
| 114 | 
  | 
char  errmsg[128]; | 
| 115 | 
  | 
 | 
| 116 | 
< | 
extern BYTE  clrtab[256][3];            /* global color map */ | 
| 116 | 
> | 
BYTE  clrtab[256][3];                   /* global color map */ | 
| 117 | 
  | 
 | 
| 118 | 
  | 
extern long  ftell(); | 
| 119 | 
  | 
 | 
| 120 | 
< | 
extern char  *malloc(), *calloc(); | 
| 120 | 
> | 
extern char  *getenv(); | 
| 121 | 
  | 
 | 
| 122 | 
  | 
Display  *thedisplay; | 
| 123 | 
+ | 
Atom  closedownAtom, wmProtocolsAtom; | 
| 124 | 
  | 
 | 
| 125 | 
+ | 
int  sigrecv; | 
| 126 | 
  | 
 | 
| 127 | 
+ | 
int  onsig() { sigrecv++; } | 
| 128 | 
+ | 
 | 
| 129 | 
+ | 
 | 
| 130 | 
  | 
main(argc, argv) | 
| 131 | 
  | 
int  argc; | 
| 132 | 
  | 
char  *argv[]; | 
| 133 | 
  | 
{ | 
| 119 | 
– | 
        extern char  *getenv(); | 
| 120 | 
– | 
        char  *gv; | 
| 134 | 
  | 
        int  headline(); | 
| 135 | 
  | 
        int  i; | 
| 136 | 
+ | 
        int  pid; | 
| 137 | 
  | 
         | 
| 138 | 
  | 
        progname = argv[0]; | 
| 125 | 
– | 
        if ((gv = getenv("GAMMA")) != NULL) | 
| 126 | 
– | 
                gamcor = atof(gv); | 
| 139 | 
  | 
 | 
| 140 | 
  | 
        for (i = 1; i < argc; i++) | 
| 141 | 
  | 
                if (argv[i][0] == '-') | 
| 167 | 
  | 
                                if (argv[i][2] == 'e') | 
| 168 | 
  | 
                                        geometry = argv[++i]; | 
| 169 | 
  | 
                                else | 
| 170 | 
< | 
                                        gamcor = atof(argv[++i]); | 
| 170 | 
> | 
                                        gamstr = argv[++i]; | 
| 171 | 
  | 
                                break; | 
| 172 | 
  | 
                        default: | 
| 173 | 
  | 
                                goto userr; | 
| 177 | 
  | 
                else | 
| 178 | 
  | 
                        break; | 
| 179 | 
  | 
 | 
| 180 | 
< | 
        if (i == argc-1) { | 
| 180 | 
> | 
        if (i > argc) | 
| 181 | 
> | 
                goto userr; | 
| 182 | 
> | 
        while (i < argc-1) { | 
| 183 | 
> | 
                sigrecv = 0; | 
| 184 | 
> | 
                signal(SIGCONT, onsig); | 
| 185 | 
> | 
                if ((pid=fork()) == 0) {        /* a child for each picture */ | 
| 186 | 
> | 
                        parent = -1; | 
| 187 | 
> | 
                        break; | 
| 188 | 
> | 
                } | 
| 189 | 
> | 
                if (pid < 0) | 
| 190 | 
> | 
                        quiterr("fork failed"); | 
| 191 | 
> | 
                parent++; | 
| 192 | 
> | 
                while (!sigrecv) | 
| 193 | 
> | 
                        pause();        /* wait for wake-up call */ | 
| 194 | 
> | 
                i++; | 
| 195 | 
> | 
        } | 
| 196 | 
> | 
        if (i < argc) {                 /* open picture file */ | 
| 197 | 
  | 
                fname = argv[i]; | 
| 198 | 
  | 
                fin = fopen(fname, "r"); | 
| 199 | 
< | 
                if (fin == NULL) { | 
| 200 | 
< | 
                        sprintf(errmsg, "cannot open file \"%s\"", fname); | 
| 201 | 
< | 
                        quiterr(errmsg); | 
| 174 | 
< | 
                } | 
| 175 | 
< | 
        } else if (i != argc) | 
| 176 | 
< | 
                goto userr; | 
| 199 | 
> | 
                if (fin == NULL) | 
| 200 | 
> | 
                        quiterr("cannot open picture file"); | 
| 201 | 
> | 
        } | 
| 202 | 
  | 
                                /* get header */ | 
| 203 | 
  | 
        getheader(fin, headline, NULL); | 
| 204 | 
  | 
                                /* get picture dimensions */ | 
| 212 | 
  | 
        if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) | 
| 213 | 
  | 
                quiterr("out of memory"); | 
| 214 | 
  | 
 | 
| 215 | 
< | 
        init();                 /* get file and open window */ | 
| 215 | 
> | 
        init(argc, argv);                       /* get file and open window */ | 
| 216 | 
  | 
 | 
| 217 | 
+ | 
        if (parent < 0) { | 
| 218 | 
+ | 
                kill(getppid(), SIGCONT);       /* signal parent if child */ | 
| 219 | 
+ | 
                sigrecv--; | 
| 220 | 
+ | 
        } | 
| 221 | 
  | 
        for ( ; ; ) | 
| 222 | 
  | 
                getevent();             /* main loop */ | 
| 223 | 
  | 
userr: | 
| 224 | 
  | 
        fprintf(stderr, | 
| 225 | 
< | 
        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", | 
| 225 | 
> | 
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n", | 
| 226 | 
  | 
                        progname); | 
| 227 | 
< | 
        quit(1); | 
| 227 | 
> | 
        exit(1); | 
| 228 | 
  | 
} | 
| 229 | 
  | 
 | 
| 230 | 
  | 
 | 
| 243 | 
  | 
} | 
| 244 | 
  | 
 | 
| 245 | 
  | 
 | 
| 246 | 
< | 
init()                  /* get data and open window */ | 
| 246 | 
> | 
init(argc, argv)                        /* get data and open window */ | 
| 247 | 
> | 
int argc; | 
| 248 | 
> | 
char **argv; | 
| 249 | 
  | 
{ | 
| 219 | 
– | 
        XWMHints        ourxwmhints; | 
| 250 | 
  | 
        XSetWindowAttributes    ourwinattr; | 
| 251 | 
< | 
        XSizeHints      oursizhints; | 
| 251 | 
> | 
        XClassHint      xclshints; | 
| 252 | 
> | 
        XWMHints        xwmhints; | 
| 253 | 
> | 
        XSizeHints      xszhints; | 
| 254 | 
> | 
        XTextProperty   windowName, iconName; | 
| 255 | 
> | 
        XGCValues       xgcv; | 
| 256 | 
> | 
        char    *name; | 
| 257 | 
  | 
        register int    i; | 
| 258 | 
  | 
         | 
| 259 | 
  | 
        if (fname != NULL) { | 
| 260 | 
  | 
                scanpos = (long *)malloc(ymax*sizeof(long)); | 
| 261 | 
  | 
                if (scanpos == NULL) | 
| 262 | 
< | 
                        goto memerr; | 
| 262 | 
> | 
                        quiterr("out of memory"); | 
| 263 | 
  | 
                for (i = 0; i < ymax; i++) | 
| 264 | 
  | 
                        scanpos[i] = -1; | 
| 265 | 
< | 
        } | 
| 265 | 
> | 
                name = fname; | 
| 266 | 
> | 
        } else | 
| 267 | 
> | 
                name = progname; | 
| 268 | 
> | 
                                /* remove directory prefix from name */ | 
| 269 | 
> | 
        for (i = strlen(name); i-- > 0; ) | 
| 270 | 
> | 
                if (name[i] == '/') | 
| 271 | 
> | 
                        break; | 
| 272 | 
> | 
        name += i+1; | 
| 273 | 
  | 
        if ((thedisplay = XOpenDisplay(dispname)) == NULL) | 
| 274 | 
  | 
                quiterr("cannot open display"); | 
| 275 | 
+ | 
                                /* set gamma value */ | 
| 276 | 
+ | 
        if (gamstr == NULL)             /* get it from the X server */ | 
| 277 | 
+ | 
                gamstr = XGetDefault(thedisplay, "radiance", "gamma"); | 
| 278 | 
+ | 
        if (gamstr == NULL)             /* get it from the environment */ | 
| 279 | 
+ | 
                gamstr = getenv("GAMMA"); | 
| 280 | 
+ | 
        if (gamstr != NULL) | 
| 281 | 
+ | 
                gamcor = atof(gamstr); | 
| 282 | 
  | 
                                /* get best visual for default screen */ | 
| 283 | 
  | 
        getbestvis(); | 
| 284 | 
  | 
                                /* store image */ | 
| 285 | 
  | 
        getras(); | 
| 286 | 
< | 
                                /* open window */ | 
| 287 | 
< | 
        ourwinattr.border_pixel = ourblack; | 
| 288 | 
< | 
        ourwinattr.background_pixel = ourwhite; | 
| 240 | 
< | 
        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, | 
| 241 | 
< | 
                        ourvis.visual, AllocNone); | 
| 242 | 
< | 
        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, | 
| 243 | 
< | 
                        ourvis.depth, InputOutput, ourvis.visual,  | 
| 244 | 
< | 
                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); | 
| 245 | 
< | 
        if (wind == 0) | 
| 246 | 
< | 
                quiterr("cannot create window"); | 
| 247 | 
< | 
        XFreeColormap(thedisplay, ourwinattr.colormap); | 
| 248 | 
< | 
        width = xmax; | 
| 249 | 
< | 
        height = ymax; | 
| 250 | 
< | 
        ourgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 251 | 
< | 
        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); | 
| 252 | 
< | 
        revgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 253 | 
< | 
        XSetFunction(thedisplay, revgc, GXinvert); | 
| 254 | 
< | 
        fontid = XLoadFont(thedisplay, FONTNAME); | 
| 255 | 
< | 
        if (fontid == 0) | 
| 256 | 
< | 
                quiterr("cannot get font"); | 
| 257 | 
< | 
        XSetFont(thedisplay, ourgc, fontid); | 
| 258 | 
< | 
        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,  | 
| 259 | 
< | 
                        XC_diamond_cross)); | 
| 260 | 
< | 
        XStoreName(thedisplay, wind, fname == NULL ? progname : fname); | 
| 286 | 
> | 
                                /* get size and position */ | 
| 287 | 
> | 
        xszhints.flags = 0; | 
| 288 | 
> | 
        xszhints.width = xmax; xszhints.height = ymax; | 
| 289 | 
  | 
        if (geometry != NULL) { | 
| 290 | 
< | 
                bzero((char *)&oursizhints, sizeof(oursizhints)); | 
| 291 | 
< | 
                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, | 
| 292 | 
< | 
                                (unsigned *)&oursizhints.width, | 
| 265 | 
< | 
                                (unsigned *)&oursizhints.height); | 
| 290 | 
> | 
                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y, | 
| 291 | 
> | 
                                (unsigned *)&xszhints.width, | 
| 292 | 
> | 
                                (unsigned *)&xszhints.height); | 
| 293 | 
  | 
                if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) | 
| 294 | 
< | 
                        oursizhints.flags |= USSize; | 
| 295 | 
< | 
                else { | 
| 296 | 
< | 
                        oursizhints.width = xmax; | 
| 270 | 
< | 
                        oursizhints.height = ymax; | 
| 271 | 
< | 
                        oursizhints.flags |= PSize; | 
| 272 | 
< | 
                } | 
| 294 | 
> | 
                        xszhints.flags |= USSize; | 
| 295 | 
> | 
                else | 
| 296 | 
> | 
                        xszhints.flags |= PSize; | 
| 297 | 
  | 
                if ((i&(XValue|YValue)) == (XValue|YValue)) { | 
| 298 | 
< | 
                        oursizhints.flags |= USPosition; | 
| 298 | 
> | 
                        xszhints.flags |= USPosition; | 
| 299 | 
  | 
                        if (i & XNegative) | 
| 300 | 
< | 
                                oursizhints.x += DisplayWidth(thedisplay, | 
| 301 | 
< | 
                                ourscreen)-1-oursizhints.width-2*BORWIDTH; | 
| 300 | 
> | 
                                xszhints.x += DisplayWidth(thedisplay, | 
| 301 | 
> | 
                                ourscreen)-1-xszhints.width-2*BORWIDTH; | 
| 302 | 
  | 
                        if (i & YNegative) | 
| 303 | 
< | 
                                oursizhints.y += DisplayHeight(thedisplay, | 
| 304 | 
< | 
                                ourscreen)-1-oursizhints.height-2*BORWIDTH; | 
| 303 | 
> | 
                                xszhints.y += DisplayHeight(thedisplay, | 
| 304 | 
> | 
                                ourscreen)-1-xszhints.height-2*BORWIDTH; | 
| 305 | 
  | 
                } | 
| 282 | 
– | 
                XSetNormalHints(thedisplay, wind, &oursizhints); | 
| 306 | 
  | 
        } | 
| 307 | 
< | 
        ourxwmhints.flags = InputHint|IconPixmapHint; | 
| 308 | 
< | 
        ourxwmhints.input = True; | 
| 309 | 
< | 
        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, | 
| 307 | 
> | 
        /* open window */ | 
| 308 | 
> | 
        ourwinattr.border_pixel = ourwhite; | 
| 309 | 
> | 
        ourwinattr.background_pixel = ourblack; | 
| 310 | 
> | 
        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, | 
| 311 | 
> | 
                        ourvis.visual, AllocNone); | 
| 312 | 
> | 
        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| | 
| 313 | 
> | 
                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; | 
| 314 | 
> | 
        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); | 
| 315 | 
> | 
        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, | 
| 316 | 
> | 
                        xszhints.width, xszhints.height, BORWIDTH, | 
| 317 | 
> | 
                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask| | 
| 318 | 
> | 
                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); | 
| 319 | 
> | 
        if (wind == 0) | 
| 320 | 
> | 
                quiterr("cannot create window"); | 
| 321 | 
> | 
        width = xmax; | 
| 322 | 
> | 
        height = ymax; | 
| 323 | 
> | 
        /* prepare graphics drawing context */ | 
| 324 | 
> | 
        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) | 
| 325 | 
> | 
                quiterr("cannot get font"); | 
| 326 | 
> | 
        xgcv.foreground = ourblack; | 
| 327 | 
> | 
        xgcv.background = ourwhite; | 
| 328 | 
> | 
        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| | 
| 329 | 
> | 
                        GCFont, &xgcv); | 
| 330 | 
> | 
        xgcv.function = GXinvert; | 
| 331 | 
> | 
        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| | 
| 332 | 
> | 
                        GCFunction, &xgcv); | 
| 333 | 
> | 
 | 
| 334 | 
> | 
        /* set up the window manager */ | 
| 335 | 
> | 
        xwmhints.flags = InputHint|IconPixmapHint; | 
| 336 | 
> | 
        xwmhints.input = True; | 
| 337 | 
> | 
        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, | 
| 338 | 
  | 
                        wind, icondata, iconwidth, iconheight); | 
| 339 | 
< | 
        XSetWMHints(thedisplay, wind, &ourxwmhints); | 
| 340 | 
< | 
        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask | 
| 341 | 
< | 
                        |ButtonMotionMask|StructureNotifyMask | 
| 342 | 
< | 
                        |KeyPressMask|ExposureMask); | 
| 339 | 
> | 
 | 
| 340 | 
> | 
        windowName.encoding = iconName.encoding = XA_STRING; | 
| 341 | 
> | 
        windowName.format = iconName.format = 8; | 
| 342 | 
> | 
        windowName.value = (u_char *)name; | 
| 343 | 
> | 
        windowName.nitems = strlen(windowName.value); | 
| 344 | 
> | 
        iconName.value = (u_char *)name; | 
| 345 | 
> | 
        iconName.nitems = strlen(windowName.value); | 
| 346 | 
> | 
 | 
| 347 | 
> | 
        xclshints.res_name = NULL; | 
| 348 | 
> | 
        xclshints.res_class = "Ximage"; | 
| 349 | 
> | 
        XSetWMProperties(thedisplay, wind, &windowName, &iconName, | 
| 350 | 
> | 
                        argv, argc, &xszhints, &xwmhints, &xclshints); | 
| 351 | 
> | 
        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False); | 
| 352 | 
> | 
        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False); | 
| 353 | 
> | 
        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); | 
| 354 | 
> | 
 | 
| 355 | 
  | 
        XMapWindow(thedisplay, wind); | 
| 356 | 
  | 
        return; | 
| 294 | 
– | 
memerr: | 
| 295 | 
– | 
        quiterr("out of memory"); | 
| 357 | 
  | 
} /* end of init */ | 
| 358 | 
  | 
 | 
| 359 | 
  | 
 | 
| 360 | 
  | 
quiterr(err)            /* print message and exit */ | 
| 361 | 
  | 
char  *err; | 
| 362 | 
  | 
{ | 
| 363 | 
< | 
        if (err != NULL) { | 
| 364 | 
< | 
                fprintf(stderr, "%s: %s\n", progname, err); | 
| 365 | 
< | 
                exit(1); | 
| 363 | 
> | 
        register int  es; | 
| 364 | 
> | 
        int  cs; | 
| 365 | 
> | 
 | 
| 366 | 
> | 
        if (es = err != NULL) | 
| 367 | 
> | 
                fprintf(stderr, "%s: %s: %s\n", progname,  | 
| 368 | 
> | 
                                fname==NULL?"<stdin>":fname, err); | 
| 369 | 
> | 
        if (thedisplay != NULL) | 
| 370 | 
> | 
                XCloseDisplay(thedisplay); | 
| 371 | 
> | 
        if (parent < 0 & sigrecv == 0) | 
| 372 | 
> | 
                kill(getppid(), SIGCONT); | 
| 373 | 
> | 
        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ | 
| 374 | 
> | 
                if (es == 0) | 
| 375 | 
> | 
                        es = cs>>8 & 0xff; | 
| 376 | 
> | 
                parent--; | 
| 377 | 
  | 
        } | 
| 378 | 
< | 
        exit(0); | 
| 378 | 
> | 
        exit(es); | 
| 379 | 
  | 
} | 
| 380 | 
  | 
 | 
| 381 | 
  | 
 | 
| 516 | 
  | 
                        goto fail; | 
| 517 | 
  | 
                getmono(); | 
| 518 | 
  | 
        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { | 
| 519 | 
< | 
                ourdata = (unsigned char *)malloc(4*xmax*ymax); | 
| 519 | 
> | 
                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); | 
| 520 | 
  | 
                if (ourdata == NULL) | 
| 521 | 
  | 
                        goto fail; | 
| 522 | 
< | 
                ourras = make_raster(thedisplay, &ourvis, 32, | 
| 522 | 
> | 
                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, | 
| 523 | 
  | 
                                ourdata, xmax, ymax, 32); | 
| 524 | 
  | 
                if (ourras == NULL) | 
| 525 | 
  | 
                        goto fail; | 
| 547 | 
  | 
 | 
| 548 | 
  | 
getevent()                              /* process the next event */ | 
| 549 | 
  | 
{ | 
| 550 | 
< | 
        union { | 
| 479 | 
< | 
                XEvent  u; | 
| 480 | 
< | 
                XConfigureEvent  c; | 
| 481 | 
< | 
                XExposeEvent  e; | 
| 482 | 
< | 
                XButtonPressedEvent  b; | 
| 483 | 
< | 
                XKeyPressedEvent  k; | 
| 484 | 
< | 
        } e; | 
| 550 | 
> | 
        XEvent xev; | 
| 551 | 
  | 
 | 
| 552 | 
< | 
        XNextEvent(thedisplay, &e.u); | 
| 553 | 
< | 
        switch (e.u.type) { | 
| 552 | 
> | 
        XNextEvent(thedisplay, &xev); | 
| 553 | 
> | 
        switch ((int)xev.type) { | 
| 554 | 
  | 
        case KeyPress: | 
| 555 | 
< | 
                docom(&e.k); | 
| 555 | 
> | 
                docom(&xev.xkey); | 
| 556 | 
  | 
                break; | 
| 557 | 
  | 
        case ConfigureNotify: | 
| 558 | 
< | 
                width = e.c.width; | 
| 559 | 
< | 
                height = e.c.height; | 
| 558 | 
> | 
                width = xev.xconfigure.width; | 
| 559 | 
> | 
                height = xev.xconfigure.height; | 
| 560 | 
  | 
                break; | 
| 561 | 
  | 
        case MapNotify: | 
| 562 | 
  | 
                map_rcolors(ourras, wind); | 
| 568 | 
  | 
                        unmap_rcolors(ourras); | 
| 569 | 
  | 
                break; | 
| 570 | 
  | 
        case Expose: | 
| 571 | 
< | 
                redraw(e.e.x, e.e.y, e.e.width, e.e.height); | 
| 571 | 
> | 
                redraw(xev.xexpose.x, xev.xexpose.y, | 
| 572 | 
> | 
                                xev.xexpose.width, xev.xexpose.height); | 
| 573 | 
  | 
                break; | 
| 574 | 
  | 
        case ButtonPress: | 
| 575 | 
< | 
                if (e.b.state & (ShiftMask|ControlMask)) | 
| 576 | 
< | 
                        moveimage(&e.b); | 
| 575 | 
> | 
                if (xev.xbutton.state & (ShiftMask|ControlMask)) | 
| 576 | 
> | 
                        moveimage(&xev.xbutton); | 
| 577 | 
> | 
                else if (xev.xbutton.button == Button2) | 
| 578 | 
> | 
                        traceray(xev.xbutton.x, xev.xbutton.y); | 
| 579 | 
  | 
                else | 
| 580 | 
< | 
                        getbox(&e.b); | 
| 580 | 
> | 
                        getbox(&xev.xbutton); | 
| 581 | 
  | 
                break; | 
| 582 | 
+ | 
        case ClientMessage: | 
| 583 | 
+ | 
                if ((xev.xclient.message_type == wmProtocolsAtom) && | 
| 584 | 
+ | 
                                (xev.xclient.data.l[0] == closedownAtom)) | 
| 585 | 
+ | 
                        quiterr(NULL); | 
| 586 | 
+ | 
                break; | 
| 587 | 
  | 
        } | 
| 588 | 
  | 
} | 
| 589 | 
  | 
 | 
| 590 | 
  | 
 | 
| 591 | 
< | 
docom(ekey)                                     /* execute command */ | 
| 591 | 
> | 
traceray(xpos, ypos)                    /* print ray corresponding to pixel */ | 
| 592 | 
> | 
int  xpos, ypos; | 
| 593 | 
> | 
{ | 
| 594 | 
> | 
        FLOAT  hv[2]; | 
| 595 | 
> | 
        FVECT  rorg, rdir; | 
| 596 | 
> | 
 | 
| 597 | 
> | 
        if (!gotview) {         /* no view, no can do */ | 
| 598 | 
> | 
                XBell(thedisplay, 0); | 
| 599 | 
> | 
                return(-1); | 
| 600 | 
> | 
        } | 
| 601 | 
> | 
        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); | 
| 602 | 
> | 
        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 603 | 
> | 
                return(-1); | 
| 604 | 
> | 
        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 605 | 
> | 
        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 606 | 
> | 
        fflush(stdout); | 
| 607 | 
> | 
        return(0); | 
| 608 | 
> | 
} | 
| 609 | 
> | 
 | 
| 610 | 
> | 
 | 
| 611 | 
> | 
docom(ekey)                             /* execute command */ | 
| 612 | 
  | 
XKeyPressedEvent  *ekey; | 
| 613 | 
  | 
{ | 
| 614 | 
  | 
        char  buf[80]; | 
| 617 | 
  | 
        int  com, n; | 
| 618 | 
  | 
        double  comp; | 
| 619 | 
  | 
        FLOAT  hv[2]; | 
| 526 | 
– | 
        FVECT  rorg, rdir; | 
| 620 | 
  | 
 | 
| 621 | 
  | 
        n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);  | 
| 622 | 
  | 
        if (n == 0) | 
| 624 | 
  | 
        com = buf[0]; | 
| 625 | 
  | 
        switch (com) {                  /* interpret command */ | 
| 626 | 
  | 
        case 'q': | 
| 627 | 
+ | 
        case 'Q': | 
| 628 | 
  | 
        case CTRL('D'):                         /* quit */ | 
| 629 | 
< | 
                quit(0); | 
| 629 | 
> | 
                quiterr(NULL); | 
| 630 | 
  | 
        case '\n': | 
| 631 | 
  | 
        case '\r': | 
| 632 | 
  | 
        case 'l': | 
| 672 | 
  | 
                                        buf, strlen(buf)); | 
| 673 | 
  | 
                return(0); | 
| 674 | 
  | 
        case 't':                               /* trace */ | 
| 675 | 
< | 
                if (!gotview) { | 
| 582 | 
< | 
                        XBell(thedisplay, 0); | 
| 583 | 
< | 
                        return(-1); | 
| 584 | 
< | 
                } | 
| 585 | 
< | 
                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); | 
| 586 | 
< | 
                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) | 
| 587 | 
< | 
                        return(-1); | 
| 588 | 
< | 
                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); | 
| 589 | 
< | 
                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); | 
| 590 | 
< | 
                fflush(stdout); | 
| 591 | 
< | 
                return(0); | 
| 675 | 
> | 
                return(traceray(ekey->x, ekey->y)); | 
| 676 | 
  | 
        case '=':                               /* adjust exposure */ | 
| 677 | 
+ | 
        case '@':                               /* adaptation level */ | 
| 678 | 
  | 
                if (avgbox(cval) == -1) | 
| 679 | 
  | 
                        return(-1); | 
| 680 | 
< | 
                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */ | 
| 680 | 
> | 
                comp = bright(cval); | 
| 681 | 
> | 
                if (comp < 1e-20) { | 
| 682 | 
> | 
                        XBell(thedisplay, 0); | 
| 683 | 
> | 
                        return(-1); | 
| 684 | 
> | 
                } | 
| 685 | 
> | 
                if (com == '@') | 
| 686 | 
> | 
                        comp = 106./exposure/ | 
| 687 | 
> | 
                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); | 
| 688 | 
> | 
                else | 
| 689 | 
> | 
                        comp = .5/comp; | 
| 690 | 
> | 
                comp = log(comp)/.69315 - scale; | 
| 691 | 
> | 
                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */ | 
| 692 | 
  | 
                if (n == 0) | 
| 693 | 
  | 
                        return(0); | 
| 694 | 
  | 
                scale_rcolors(ourras, pow(2.0, (double)n)); | 
| 710 | 
  | 
                        make_rpixmap(ourras, wind); | 
| 711 | 
  | 
                redraw(0, 0, width, height); | 
| 712 | 
  | 
                return(0); | 
| 713 | 
+ | 
        case 'f':                               /* turn on fast redraw */ | 
| 714 | 
+ | 
                fast = 1; | 
| 715 | 
+ | 
                make_rpixmap(ourras, wind); | 
| 716 | 
+ | 
                return(0); | 
| 717 | 
+ | 
        case 'F':                               /* turn off fast redraw */ | 
| 718 | 
+ | 
                fast = 0; | 
| 719 | 
+ | 
                free_rpixmap(ourras); | 
| 720 | 
+ | 
                return(0); | 
| 721 | 
+ | 
        case '0':                               /* recenter origin */ | 
| 722 | 
+ | 
                if (xoff == 0 & yoff == 0) | 
| 723 | 
+ | 
                        return(0); | 
| 724 | 
+ | 
                xoff = yoff = 0; | 
| 725 | 
+ | 
                XClearWindow(thedisplay, wind); | 
| 726 | 
+ | 
                redraw(0, 0, width, height); | 
| 727 | 
+ | 
                return(0); | 
| 728 | 
  | 
        case ' ':                               /* clear */ | 
| 729 | 
  | 
                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); | 
| 730 | 
  | 
                return(0); | 
| 738 | 
  | 
moveimage(ebut)                         /* shift the image */ | 
| 739 | 
  | 
XButtonPressedEvent  *ebut; | 
| 740 | 
  | 
{ | 
| 741 | 
< | 
        union { | 
| 631 | 
< | 
                XEvent  u; | 
| 632 | 
< | 
                XButtonReleasedEvent  b; | 
| 633 | 
< | 
                XPointerMovedEvent  m; | 
| 634 | 
< | 
        }  e; | 
| 741 | 
> | 
        XEvent  e; | 
| 742 | 
  | 
        int     mxo, myo; | 
| 743 | 
  | 
 | 
| 744 | 
< | 
        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); | 
| 745 | 
< | 
        while (e.u.type == MotionNotify) { | 
| 746 | 
< | 
                mxo = e.m.x; | 
| 747 | 
< | 
                myo = e.m.y; | 
| 744 | 
> | 
        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 745 | 
> | 
        while (e.type == MotionNotify) { | 
| 746 | 
> | 
                mxo = e.xmotion.x; | 
| 747 | 
> | 
                myo = e.xmotion.y; | 
| 748 | 
  | 
                revline(ebut->x, ebut->y, mxo, myo); | 
| 749 | 
  | 
                revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, | 
| 750 | 
  | 
                                xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); | 
| 751 | 
< | 
                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); | 
| 751 | 
> | 
                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 752 | 
  | 
                revline(ebut->x, ebut->y, mxo, myo); | 
| 753 | 
  | 
                revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, | 
| 754 | 
  | 
                                xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); | 
| 755 | 
  | 
        } | 
| 756 | 
< | 
        xoff += e.b.x - ebut->x; | 
| 757 | 
< | 
        yoff += e.b.y - ebut->y; | 
| 756 | 
> | 
        xoff += e.xbutton.x - ebut->x; | 
| 757 | 
> | 
        yoff += e.xbutton.y - ebut->y; | 
| 758 | 
  | 
        XClearWindow(thedisplay, wind); | 
| 759 | 
  | 
        redraw(0, 0, width, height); | 
| 760 | 
  | 
} | 
| 763 | 
  | 
getbox(ebut)                            /* get new box */ | 
| 764 | 
  | 
XButtonPressedEvent  *ebut; | 
| 765 | 
  | 
{ | 
| 766 | 
< | 
        union { | 
| 660 | 
< | 
                XEvent  u; | 
| 661 | 
< | 
                XButtonReleasedEvent  b; | 
| 662 | 
< | 
                XPointerMovedEvent  m; | 
| 663 | 
< | 
        }  e; | 
| 766 | 
> | 
        XEvent  e; | 
| 767 | 
  | 
 | 
| 768 | 
< | 
        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); | 
| 769 | 
< | 
        while (e.u.type == MotionNotify) { | 
| 770 | 
< | 
                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); | 
| 771 | 
< | 
                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); | 
| 768 | 
> | 
        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); | 
| 769 | 
> | 
        while (e.type == MotionNotify) { | 
| 770 | 
> | 
                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); | 
| 771 | 
> | 
                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); | 
| 772 | 
  | 
                revbox(ebut->x, ebut->y, box.xmin, box.ymin); | 
| 773 | 
  | 
        } | 
| 774 | 
< | 
        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); | 
| 775 | 
< | 
        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); | 
| 774 | 
> | 
        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); | 
| 775 | 
> | 
        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); | 
| 776 | 
  | 
        if (box.xmin > ebut->x) { | 
| 777 | 
  | 
                box.xsiz = box.xmin - ebut->x + 1; | 
| 778 | 
  | 
                box.xmin = ebut->x; | 
| 885 | 
  | 
        static short  cerr[ICONSIZ]; | 
| 886 | 
  | 
        static int  ynext; | 
| 887 | 
  | 
        static char  *dp; | 
| 888 | 
< | 
        double  sf; | 
| 786 | 
< | 
        COLOR  col; | 
| 888 | 
> | 
        COLR  clr; | 
| 889 | 
  | 
        register int  err; | 
| 890 | 
  | 
        register int    x, ti; | 
| 891 | 
  | 
        int  errp; | 
| 910 | 
  | 
        } | 
| 911 | 
  | 
        if (y < ynext*ymax/iconheight)  /* skip this one */ | 
| 912 | 
  | 
                return; | 
| 811 | 
– | 
        sf = pow(2.0, (double)(scale+8)); | 
| 913 | 
  | 
        err = 0; | 
| 914 | 
  | 
        for (x = 0; x < iconwidth; x++) { | 
| 915 | 
  | 
                if (!(x&7)) | 
| 916 | 
  | 
                        *++dp = 0; | 
| 917 | 
  | 
                errp = err; | 
| 918 | 
  | 
                ti = x*xmax/iconwidth; | 
| 919 | 
< | 
                colr_color(col, scan[ti]); | 
| 920 | 
< | 
                ti = sf*bright(col); | 
| 921 | 
< | 
                if (ti > 255) ti = 255; | 
| 821 | 
< | 
                err += ti + cerr[x]; | 
| 919 | 
> | 
                copycolr(clr, scan[ti]); | 
| 920 | 
> | 
                normcolrs(clr, 1, scale); | 
| 921 | 
> | 
                err += normbright(clr) + cerr[x]; | 
| 922 | 
  | 
                if (err > 127) | 
| 923 | 
  | 
                        err -= 255; | 
| 924 | 
  | 
                else | 
| 933 | 
  | 
getfull()                       /* get full (24-bit) data */ | 
| 934 | 
  | 
{ | 
| 935 | 
  | 
        int     y; | 
| 936 | 
< | 
        register unsigned long  *dp; | 
| 936 | 
> | 
        register unsigned int4  *dp; | 
| 937 | 
  | 
        register int    x; | 
| 938 | 
  | 
                                        /* set gamma correction */ | 
| 939 | 
  | 
        setcolrgam(gamcor); | 
| 940 | 
  | 
                                        /* read and convert file */ | 
| 941 | 
< | 
        dp = (unsigned long *)ourdata; | 
| 941 | 
> | 
        dp = (unsigned int4 *)ourdata; | 
| 942 | 
  | 
        for (y = 0; y < ymax; y++) { | 
| 943 | 
  | 
                getscan(y); | 
| 944 | 
  | 
                add2icon(y, scanline); | 
| 947 | 
  | 
                colrs_gambs(scanline, xmax); | 
| 948 | 
  | 
                if (ourras->image->blue_mask & 1) | 
| 949 | 
  | 
                        for (x = 0; x < xmax; x++) | 
| 950 | 
< | 
                                *dp++ = scanline[x][RED] << 16 | | 
| 951 | 
< | 
                                        scanline[x][GRN] << 8 | | 
| 952 | 
< | 
                                        scanline[x][BLU] ; | 
| 950 | 
> | 
                                *dp++ = (unsigned int4)scanline[x][RED] << 16 | | 
| 951 | 
> | 
                                        (unsigned int4)scanline[x][GRN] << 8 | | 
| 952 | 
> | 
                                        (unsigned int4)scanline[x][BLU] ; | 
| 953 | 
  | 
                else | 
| 954 | 
  | 
                        for (x = 0; x < xmax; x++) | 
| 955 | 
< | 
                                *dp++ = scanline[x][RED] | | 
| 956 | 
< | 
                                        scanline[x][GRN] << 8 | | 
| 957 | 
< | 
                                        scanline[x][BLU] << 16 ; | 
| 955 | 
> | 
                                *dp++ = (unsigned int4)scanline[x][RED] | | 
| 956 | 
> | 
                                        (unsigned int4)scanline[x][GRN] << 8 | | 
| 957 | 
> | 
                                        (unsigned int4)scanline[x][BLU] << 16 ; | 
| 958 | 
  | 
        } | 
| 959 | 
  | 
} | 
| 960 | 
  | 
 | 
| 973 | 
  | 
                add2icon(y, scanline); | 
| 974 | 
  | 
                if (scale) | 
| 975 | 
  | 
                        shiftcolrs(scanline, xmax, scale); | 
| 976 | 
+ | 
                for (x = 0; x < xmax; x++) | 
| 977 | 
+ | 
                        scanline[x][GRN] = normbright(scanline[x]); | 
| 978 | 
  | 
                colrs_gambs(scanline, xmax); | 
| 979 | 
  | 
                if (maxcolors < 256) | 
| 980 | 
  | 
                        for (x = 0; x < xmax; x++) | 
| 981 | 
< | 
                                *dp++ = ((long)normbright(scanline[x]) * | 
| 982 | 
< | 
                                        maxcolors + 128) >> 8; | 
| 981 | 
> | 
                                *dp++ = ((long)scanline[x][GRN] * | 
| 982 | 
> | 
                                        maxcolors + maxcolors/2) >> 8; | 
| 983 | 
  | 
                else | 
| 984 | 
  | 
                        for (x = 0; x < xmax; x++) | 
| 985 | 
< | 
                                *dp++ = normbright(scanline[x]); | 
| 985 | 
> | 
                                *dp++ = scanline[x][GRN]; | 
| 986 | 
  | 
        } | 
| 987 | 
  | 
        for (x = 0; x < maxcolors; x++) | 
| 988 | 
  | 
                clrtab[x][RED] = clrtab[x][GRN] = | 
| 989 | 
< | 
                        clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors; | 
| 989 | 
> | 
                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; | 
| 990 | 
  | 
} | 
| 991 | 
  | 
 | 
| 992 | 
  | 
 | 
| 993 | 
  | 
getmapped()                     /* get color-mapped data */ | 
| 994 | 
  | 
{ | 
| 995 | 
  | 
        int     y; | 
| 996 | 
+ | 
                                        /* make sure we can do it first */ | 
| 997 | 
+ | 
        if (fname == NULL) | 
| 998 | 
+ | 
                quiterr("cannot map colors from standard input"); | 
| 999 | 
  | 
                                        /* set gamma correction */ | 
| 1000 | 
  | 
        setcolrgam(gamcor); | 
| 1001 | 
  | 
                                        /* make histogram */ | 
| 1002 | 
< | 
        new_histo(); | 
| 1002 | 
> | 
        if (new_histo((long)xmax*ymax) == -1) | 
| 1003 | 
> | 
                quiterr("cannot initialize histogram"); | 
| 1004 | 
  | 
        for (y = 0; y < ymax; y++) { | 
| 1005 | 
  | 
                if (getscan(y) < 0) | 
| 1006 | 
< | 
                        quiterr("seek error in getmapped"); | 
| 1006 | 
> | 
                        break; | 
| 1007 | 
  | 
                add2icon(y, scanline); | 
| 1008 | 
  | 
                if (scale) | 
| 1009 | 
  | 
                        shiftcolrs(scanline, xmax, scale); | 
| 1014 | 
  | 
        if (!new_clrtab(maxcolors)) | 
| 1015 | 
  | 
                quiterr("cannot create color map"); | 
| 1016 | 
  | 
        for (y = 0; y < ymax; y++) { | 
| 1017 | 
< | 
                if (getscan(y) < 0) | 
| 912 | 
< | 
                        quiterr("seek error in getmapped"); | 
| 1017 | 
> | 
                getscan(y); | 
| 1018 | 
  | 
                if (scale) | 
| 1019 | 
  | 
                        shiftcolrs(scanline, xmax, scale); | 
| 1020 | 
  | 
                colrs_gambs(scanline, xmax); | 
| 1057 | 
  | 
getscan(y) | 
| 1058 | 
  | 
int  y; | 
| 1059 | 
  | 
{ | 
| 1060 | 
+ | 
        static int  trunced = -1;               /* truncated file? */ | 
| 1061 | 
+ | 
skipit: | 
| 1062 | 
+ | 
        if (trunced >= 0 && y >= trunced) { | 
| 1063 | 
+ | 
                bzero(scanline, xmax*sizeof(COLR)); | 
| 1064 | 
+ | 
                return(-1); | 
| 1065 | 
+ | 
        } | 
| 1066 | 
  | 
        if (y != cury) { | 
| 1067 | 
  | 
                if (scanpos == NULL || scanpos[y] == -1) | 
| 1068 | 
  | 
                        return(-1); | 
| 1072 | 
  | 
        } else if (scanpos != NULL && scanpos[y] == -1) | 
| 1073 | 
  | 
                scanpos[y] = ftell(fin); | 
| 1074 | 
  | 
 | 
| 1075 | 
< | 
        if (freadcolrs(scanline, xmax, fin) < 0) | 
| 1076 | 
< | 
                quiterr("read error"); | 
| 1077 | 
< | 
 | 
| 1075 | 
> | 
        if (freadcolrs(scanline, xmax, fin) < 0) { | 
| 1076 | 
> | 
                fprintf(stderr, "%s: %s: unfinished picture\n", | 
| 1077 | 
> | 
                                progname, fname==NULL?"<stdin>":fname); | 
| 1078 | 
> | 
                trunced = y; | 
| 1079 | 
> | 
                goto skipit; | 
| 1080 | 
> | 
        } | 
| 1081 | 
  | 
        cury++; | 
| 1082 | 
  | 
        return(0); | 
| 1083 | 
  | 
} |