| 1 | 
< | 
/* Copyright (c) 1991 Regents of the University of California */ | 
| 1 | 
> | 
/* Copyright (c) 1992 Regents of the University of California */ | 
| 2 | 
  | 
 | 
| 3 | 
  | 
#ifndef lint | 
| 4 | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 27 | 
  | 
 | 
| 28 | 
  | 
#include  "color.h" | 
| 29 | 
  | 
#include  "view.h" | 
| 30 | 
– | 
#include  "pic.h" | 
| 30 | 
  | 
#include  "x11raster.h" | 
| 31 | 
  | 
#include  "random.h" | 
| 32 | 
  | 
#include  "resolu.h" | 
| 33 | 
  | 
 | 
| 34 | 
  | 
#define  FONTNAME       "8x13"          /* text font we'll use */ | 
| 35 | 
  | 
 | 
| 36 | 
< | 
#define  CTRL(c)        ('c'-'@') | 
| 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) | 
| 44 | 
– | 
#define  ourblack       BlackPixel(thedisplay,ourscreen) | 
| 45 | 
– | 
#define  ourwhite       WhitePixel(thedisplay,ourscreen) | 
| 43 | 
  | 
#define  ourroot        RootWindow(thedisplay,ourscreen) | 
| 47 | 
– | 
#define  ourgc          DefaultGC(thedisplay,ourscreen) | 
| 44 | 
  | 
 | 
| 45 | 
  | 
#define  revline(x0,y0,x1,y1)   XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) | 
| 46 | 
  | 
 | 
| 51 | 
  | 
int  dither = 1;                        /* dither colors? */ | 
| 52 | 
  | 
int  fast = 0;                          /* keep picture in Pixmap? */ | 
| 53 | 
  | 
 | 
| 54 | 
+ | 
char    *dispname = NULL;               /* our display name */ | 
| 55 | 
+ | 
 | 
| 56 | 
  | 
Window  wind = 0;                       /* our output window */ | 
| 57 | 
+ | 
unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */ | 
| 58 | 
  | 
Font  fontid;                           /* our font */ | 
| 59 | 
  | 
 | 
| 60 | 
  | 
int  maxcolors = 0;                     /* maximum colors */ | 
| 82 | 
  | 
 | 
| 83 | 
  | 
int  wrongformat = 0;                   /* input in another format? */ | 
| 84 | 
  | 
 | 
| 85 | 
+ | 
GC      ourgc;                          /* standard graphics context */ | 
| 86 | 
  | 
GC      revgc;                          /* graphics context with GXinvert */ | 
| 87 | 
  | 
 | 
| 88 | 
< | 
XRASTER *ourras;                        /* our stored image */ | 
| 88 | 
> | 
int             *ourrank;               /* our visual class ranking */ | 
| 89 | 
> | 
XVisualInfo     ourvis;                 /* our visual */ | 
| 90 | 
> | 
XRASTER         *ourras;                /* our stored image */ | 
| 91 | 
  | 
unsigned char   *ourdata;               /* our image data */ | 
| 92 | 
  | 
 | 
| 93 | 
  | 
struct { | 
| 103 | 
  | 
 | 
| 104 | 
  | 
char  errmsg[128]; | 
| 105 | 
  | 
 | 
| 106 | 
+ | 
extern BYTE  clrtab[256][3];            /* global color map */ | 
| 107 | 
+ | 
 | 
| 108 | 
  | 
extern long  ftell(); | 
| 109 | 
  | 
 | 
| 110 | 
  | 
extern char  *malloc(), *calloc(); | 
| 111 | 
  | 
 | 
| 108 | 
– | 
extern double  atof(), pow(), log(); | 
| 109 | 
– | 
 | 
| 112 | 
  | 
Display  *thedisplay; | 
| 113 | 
  | 
 | 
| 114 | 
+ | 
 | 
| 115 | 
  | 
main(argc, argv) | 
| 116 | 
  | 
int  argc; | 
| 117 | 
  | 
char  *argv[]; | 
| 118 | 
  | 
{ | 
| 119 | 
+ | 
        extern char  *getenv(); | 
| 120 | 
+ | 
        char  *gv; | 
| 121 | 
  | 
        int  headline(); | 
| 122 | 
  | 
        int  i; | 
| 123 | 
  | 
         | 
| 124 | 
  | 
        progname = argv[0]; | 
| 125 | 
+ | 
        if ((gv = getenv("GAMMA")) != NULL) | 
| 126 | 
+ | 
                gamcor = atof(gv); | 
| 127 | 
  | 
 | 
| 128 | 
  | 
        for (i = 1; i < argc; i++) | 
| 129 | 
  | 
                if (argv[i][0] == '-') | 
| 138 | 
  | 
                                maxcolors = 2; | 
| 139 | 
  | 
                                break; | 
| 140 | 
  | 
                        case 'd': | 
| 141 | 
< | 
                                dither = !dither; | 
| 141 | 
> | 
                                if (argv[i][2] == 'i') | 
| 142 | 
> | 
                                        dispname = argv[++i]; | 
| 143 | 
> | 
                                else | 
| 144 | 
> | 
                                        dither = !dither; | 
| 145 | 
  | 
                                break; | 
| 146 | 
  | 
                        case 'f': | 
| 147 | 
  | 
                                fast = !fast; | 
| 152 | 
  | 
                                scale = atoi(argv[++i]); | 
| 153 | 
  | 
                                break; | 
| 154 | 
  | 
                        case 'g': | 
| 155 | 
< | 
                                if (!strcmp(argv[i], "-geometry")) | 
| 155 | 
> | 
                                if (argv[i][2] == 'e') | 
| 156 | 
  | 
                                        geometry = argv[++i]; | 
| 157 | 
  | 
                                else | 
| 158 | 
  | 
                                        gamcor = atof(argv[++i]); | 
| 169 | 
  | 
                fname = argv[i]; | 
| 170 | 
  | 
                fin = fopen(fname, "r"); | 
| 171 | 
  | 
                if (fin == NULL) { | 
| 172 | 
< | 
                        sprintf(errmsg, "can't open file \"%s\"", fname); | 
| 172 | 
> | 
                        sprintf(errmsg, "cannot open file \"%s\"", fname); | 
| 173 | 
  | 
                        quiterr(errmsg); | 
| 174 | 
  | 
                } | 
| 175 | 
  | 
        } else if (i != argc) | 
| 193 | 
  | 
                getevent();             /* main loop */ | 
| 194 | 
  | 
userr: | 
| 195 | 
  | 
        fprintf(stderr, | 
| 196 | 
< | 
        "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", | 
| 196 | 
> | 
        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", | 
| 197 | 
  | 
                        progname); | 
| 198 | 
  | 
        quit(1); | 
| 199 | 
  | 
} | 
| 202 | 
  | 
headline(s)             /* get relevant info from header */ | 
| 203 | 
  | 
char  *s; | 
| 204 | 
  | 
{ | 
| 195 | 
– | 
        static char  *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; | 
| 196 | 
– | 
        register char  **an; | 
| 205 | 
  | 
        char  fmt[32]; | 
| 206 | 
  | 
 | 
| 207 | 
  | 
        if (isexpos(s)) | 
| 209 | 
  | 
        else if (isformat(s)) { | 
| 210 | 
  | 
                formatval(fmt, s); | 
| 211 | 
  | 
                wrongformat = strcmp(fmt, COLRFMT); | 
| 212 | 
< | 
        } else | 
| 213 | 
< | 
                for (an = altname; *an != NULL; an++) | 
| 206 | 
< | 
                        if (!strncmp(*an, s, strlen(*an))) { | 
| 207 | 
< | 
                                if (sscanview(&ourview, s+strlen(*an)) > 0) | 
| 208 | 
< | 
                                        gotview++; | 
| 209 | 
< | 
                                return; | 
| 210 | 
< | 
                        } | 
| 212 | 
> | 
        } else if (isview(s) && sscanview(&ourview, s) > 0) | 
| 213 | 
> | 
                gotview++; | 
| 214 | 
  | 
} | 
| 215 | 
  | 
 | 
| 216 | 
  | 
 | 
| 218 | 
  | 
{ | 
| 219 | 
  | 
        XWMHints        ourxwmhints; | 
| 220 | 
  | 
        XSetWindowAttributes    ourwinattr; | 
| 221 | 
< | 
        XSizeHints  oursizhints; | 
| 222 | 
< | 
        register int  i; | 
| 221 | 
> | 
        XSizeHints      oursizhints; | 
| 222 | 
> | 
        register int    i; | 
| 223 | 
  | 
         | 
| 224 | 
  | 
        if (fname != NULL) { | 
| 225 | 
  | 
                scanpos = (long *)malloc(ymax*sizeof(long)); | 
| 228 | 
  | 
                for (i = 0; i < ymax; i++) | 
| 229 | 
  | 
                        scanpos[i] = -1; | 
| 230 | 
  | 
        } | 
| 231 | 
< | 
        if ((thedisplay = XOpenDisplay(NULL)) == NULL) | 
| 232 | 
< | 
                quiterr("can't open display; DISPLAY variable set?"); | 
| 233 | 
< | 
        if (maxcolors == 0) {           /* get number of available colors */ | 
| 234 | 
< | 
                i = DisplayPlanes(thedisplay,ourscreen); | 
| 232 | 
< | 
                maxcolors = i > 8 ? 256 : 1<<i; | 
| 233 | 
< | 
                if (maxcolors > 4) maxcolors -= 2; | 
| 234 | 
< | 
        } | 
| 231 | 
> | 
        if ((thedisplay = XOpenDisplay(dispname)) == NULL) | 
| 232 | 
> | 
                quiterr("cannot open display"); | 
| 233 | 
> | 
                                /* get best visual for default screen */ | 
| 234 | 
> | 
        getbestvis(); | 
| 235 | 
  | 
                                /* store image */ | 
| 236 | 
  | 
        getras(); | 
| 237 | 
  | 
                                /* open window */ | 
| 238 | 
  | 
        ourwinattr.border_pixel = ourblack; | 
| 239 | 
  | 
        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 | 
< | 
                        0, InputOutput, ourras->visual,  | 
| 244 | 
< | 
                        CWBackPixel|CWBorderPixel, &ourwinattr); | 
| 243 | 
> | 
                        ourvis.depth, InputOutput, ourvis.visual,  | 
| 244 | 
> | 
                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); | 
| 245 | 
  | 
        if (wind == 0) | 
| 246 | 
< | 
                quiterr("can't create window"); | 
| 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("can't get font"); | 
| 256 | 
> | 
                quiterr("cannot get font"); | 
| 257 | 
  | 
        XSetFont(thedisplay, ourgc, fontid); | 
| 251 | 
– | 
        revgc = XCreateGC(thedisplay, wind, 0, 0); | 
| 252 | 
– | 
        XSetFunction(thedisplay, revgc, GXinvert); | 
| 258 | 
  | 
        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,  | 
| 259 | 
  | 
                        XC_diamond_cross)); | 
| 260 | 
  | 
        XStoreName(thedisplay, wind, fname == NULL ? progname : fname); | 
| 307 | 
  | 
} | 
| 308 | 
  | 
 | 
| 309 | 
  | 
 | 
| 310 | 
< | 
eputs(s) | 
| 311 | 
< | 
char    *s; | 
| 310 | 
> | 
static int | 
| 311 | 
> | 
viscmp(v1,v2)           /* compare visual to see which is better, descending */ | 
| 312 | 
> | 
register XVisualInfo    *v1, *v2; | 
| 313 | 
  | 
{ | 
| 314 | 
< | 
        fputs(s, stderr); | 
| 314 | 
> | 
        int     bad1 = 0, bad2 = 0; | 
| 315 | 
> | 
        register int  *rp; | 
| 316 | 
> | 
 | 
| 317 | 
> | 
        if (v1->class == v2->class) { | 
| 318 | 
> | 
                if (v1->class == TrueColor || v1->class == DirectColor) { | 
| 319 | 
> | 
                                        /* prefer 24-bit to 32-bit */ | 
| 320 | 
> | 
                        if (v1->depth == 24 && v2->depth == 32) | 
| 321 | 
> | 
                                return(-1); | 
| 322 | 
> | 
                        if (v1->depth == 32 && v2->depth == 24) | 
| 323 | 
> | 
                                return(1); | 
| 324 | 
> | 
                        return(0); | 
| 325 | 
> | 
                } | 
| 326 | 
> | 
                                        /* don't be too greedy */ | 
| 327 | 
> | 
                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth) | 
| 328 | 
> | 
                        return(v1->depth - v2->depth); | 
| 329 | 
> | 
                return(v2->depth - v1->depth); | 
| 330 | 
> | 
        } | 
| 331 | 
> | 
                                        /* prefer Pseudo when < 24-bit */ | 
| 332 | 
> | 
        if ((v1->class == TrueColor || v1->class == DirectColor) && | 
| 333 | 
> | 
                        v1->depth < 24) | 
| 334 | 
> | 
                bad1 = 1; | 
| 335 | 
> | 
        if ((v2->class == TrueColor || v2->class == DirectColor) && | 
| 336 | 
> | 
                        v2->depth < 24) | 
| 337 | 
> | 
                bad2 = -1; | 
| 338 | 
> | 
        if (bad1 | bad2) | 
| 339 | 
> | 
                return(bad1+bad2); | 
| 340 | 
> | 
                                        /* otherwise, use class ranking */ | 
| 341 | 
> | 
        for (rp = ourrank; *rp != -1; rp++) { | 
| 342 | 
> | 
                if (v1->class == *rp) | 
| 343 | 
> | 
                        return(-1); | 
| 344 | 
> | 
                if (v2->class == *rp) | 
| 345 | 
> | 
                        return(1); | 
| 346 | 
> | 
        } | 
| 347 | 
> | 
        return(0); | 
| 348 | 
  | 
} | 
| 349 | 
  | 
 | 
| 350 | 
  | 
 | 
| 351 | 
< | 
quit(code) | 
| 313 | 
< | 
int  code; | 
| 351 | 
> | 
getbestvis()                    /* get the best visual for this screen */ | 
| 352 | 
  | 
{ | 
| 353 | 
< | 
        exit(code); | 
| 353 | 
> | 
#ifdef DEBUG | 
| 354 | 
> | 
static char  vistype[][12] = { | 
| 355 | 
> | 
                "StaticGray", | 
| 356 | 
> | 
                "GrayScale", | 
| 357 | 
> | 
                "StaticColor", | 
| 358 | 
> | 
                "PseudoColor", | 
| 359 | 
> | 
                "TrueColor", | 
| 360 | 
> | 
                "DirectColor" | 
| 361 | 
> | 
}; | 
| 362 | 
> | 
#endif | 
| 363 | 
> | 
        static int      rankings[3][6] = { | 
| 364 | 
> | 
                {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, | 
| 365 | 
> | 
                {PseudoColor,GrayScale,StaticGray,-1}, | 
| 366 | 
> | 
                {PseudoColor,GrayScale,StaticGray,-1} | 
| 367 | 
> | 
        }; | 
| 368 | 
> | 
        XVisualInfo     *xvi; | 
| 369 | 
> | 
        int     vismatched; | 
| 370 | 
> | 
        register int    i, j; | 
| 371 | 
> | 
 | 
| 372 | 
> | 
        if (greyscale) { | 
| 373 | 
> | 
                ourrank = rankings[2]; | 
| 374 | 
> | 
                if (maxcolors < 2) maxcolors = 256; | 
| 375 | 
> | 
        } else if (maxcolors >= 2 && maxcolors <= 256) | 
| 376 | 
> | 
                ourrank = rankings[1]; | 
| 377 | 
> | 
        else { | 
| 378 | 
> | 
                ourrank = rankings[0]; | 
| 379 | 
> | 
                maxcolors = 256; | 
| 380 | 
> | 
        } | 
| 381 | 
> | 
                                        /* find best visual */ | 
| 382 | 
> | 
        ourvis.screen = ourscreen; | 
| 383 | 
> | 
        xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); | 
| 384 | 
> | 
        if (xvi == NULL) | 
| 385 | 
> | 
                quiterr("no visuals for this screen!"); | 
| 386 | 
> | 
#ifdef DEBUG | 
| 387 | 
> | 
        fprintf(stderr, "Supported visuals:\n"); | 
| 388 | 
> | 
        for (i = 0; i < vismatched; i++) | 
| 389 | 
> | 
                fprintf(stderr, "\ttype %s, depth %d\n", | 
| 390 | 
> | 
                                vistype[xvi[i].class], xvi[i].depth); | 
| 391 | 
> | 
#endif | 
| 392 | 
> | 
        for (i = 0, j = 1; j < vismatched; j++) | 
| 393 | 
> | 
                if (viscmp(&xvi[i],&xvi[j]) > 0) | 
| 394 | 
> | 
                        i = j; | 
| 395 | 
> | 
                                        /* compare to least acceptable */ | 
| 396 | 
> | 
        for (j = 0; ourrank[j++] != -1; ) | 
| 397 | 
> | 
                ; | 
| 398 | 
> | 
        ourvis.class = ourrank[--j]; | 
| 399 | 
> | 
        ourvis.depth = 1; | 
| 400 | 
> | 
        if (viscmp(&xvi[i],&ourvis) > 0) | 
| 401 | 
> | 
                quiterr("inadequate visuals on this screen"); | 
| 402 | 
> | 
                                        /* OK, we'll use it */ | 
| 403 | 
> | 
        copystruct(&ourvis, &xvi[i]); | 
| 404 | 
> | 
#ifdef DEBUG | 
| 405 | 
> | 
        fprintf(stderr, "Selected visual type %s, depth %d\n", | 
| 406 | 
> | 
                        vistype[ourvis.class], ourvis.depth); | 
| 407 | 
> | 
#endif | 
| 408 | 
> | 
                                        /* make appropriate adjustments */ | 
| 409 | 
> | 
        if (ourvis.class == GrayScale || ourvis.class == StaticGray) | 
| 410 | 
> | 
                greyscale = 1; | 
| 411 | 
> | 
        if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) | 
| 412 | 
> | 
                maxcolors = ourvis.colormap_size; | 
| 413 | 
> | 
        if (ourvis.class == StaticGray) { | 
| 414 | 
> | 
                ourblack = 0; | 
| 415 | 
> | 
                ourwhite = 255; | 
| 416 | 
> | 
        } else if (ourvis.class == PseudoColor) { | 
| 417 | 
> | 
                ourblack = BlackPixel(thedisplay,ourscreen); | 
| 418 | 
> | 
                ourwhite = WhitePixel(thedisplay,ourscreen); | 
| 419 | 
> | 
                if ((ourblack|ourwhite) & ~255L) { | 
| 420 | 
> | 
                        ourblack = 0; | 
| 421 | 
> | 
                        ourwhite = 1; | 
| 422 | 
> | 
                } | 
| 423 | 
> | 
                if (maxcolors > 4) | 
| 424 | 
> | 
                        maxcolors -= 2; | 
| 425 | 
> | 
        } else { | 
| 426 | 
> | 
                ourblack = 0; | 
| 427 | 
> | 
                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; | 
| 428 | 
> | 
        } | 
| 429 | 
> | 
        XFree((char *)xvi); | 
| 430 | 
  | 
} | 
| 431 | 
  | 
 | 
| 432 | 
  | 
 | 
| 433 | 
  | 
getras()                                /* get raster file */ | 
| 434 | 
  | 
{ | 
| 321 | 
– | 
        colormap        ourmap; | 
| 435 | 
  | 
        XVisualInfo     vinfo; | 
| 436 | 
  | 
 | 
| 437 | 
  | 
        if (maxcolors <= 2) {           /* monochrome */ | 
| 438 | 
  | 
                ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); | 
| 439 | 
  | 
                if (ourdata == NULL) | 
| 440 | 
  | 
                        goto fail; | 
| 441 | 
< | 
                ourras = make_raster(thedisplay, ourscreen, 1, ourdata, | 
| 441 | 
> | 
                ourras = make_raster(thedisplay, &ourvis, 1, ourdata, | 
| 442 | 
  | 
                                xmax, ymax, 8); | 
| 443 | 
  | 
                if (ourras == NULL) | 
| 444 | 
  | 
                        goto fail; | 
| 445 | 
  | 
                getmono(); | 
| 446 | 
< | 
        } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) | 
| 447 | 
< | 
                                                /* kludge for DirectColor */ | 
| 335 | 
< | 
        || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { | 
| 336 | 
< | 
                ourdata = (unsigned char *)malloc(xmax*ymax*3); | 
| 446 | 
> | 
        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { | 
| 447 | 
> | 
                ourdata = (unsigned char *)malloc(4*xmax*ymax); | 
| 448 | 
  | 
                if (ourdata == NULL) | 
| 449 | 
  | 
                        goto fail; | 
| 450 | 
< | 
                ourras = make_raster(thedisplay, ourscreen, 24, ourdata, | 
| 451 | 
< | 
                                xmax, ymax, 8); | 
| 450 | 
> | 
                ourras = make_raster(thedisplay, &ourvis, 32, | 
| 451 | 
> | 
                                ourdata, xmax, ymax, 32); | 
| 452 | 
  | 
                if (ourras == NULL) | 
| 453 | 
  | 
                        goto fail; | 
| 454 | 
  | 
                getfull(); | 
| 456 | 
  | 
                ourdata = (unsigned char *)malloc(xmax*ymax); | 
| 457 | 
  | 
                if (ourdata == NULL) | 
| 458 | 
  | 
                        goto fail; | 
| 459 | 
< | 
                ourras = make_raster(thedisplay, ourscreen, 8, ourdata, | 
| 459 | 
> | 
                ourras = make_raster(thedisplay, &ourvis, 8, ourdata, | 
| 460 | 
  | 
                                xmax, ymax, 8); | 
| 461 | 
  | 
                if (ourras == NULL) | 
| 462 | 
  | 
                        goto fail; | 
| 463 | 
< | 
                if (greyscale) | 
| 464 | 
< | 
                        biq(dither,maxcolors,1,ourmap); | 
| 463 | 
> | 
                if (greyscale | ourvis.class == StaticGray) | 
| 464 | 
> | 
                        getgrey(); | 
| 465 | 
  | 
                else | 
| 466 | 
< | 
                        ciq(dither,maxcolors,1,ourmap); | 
| 467 | 
< | 
                if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) | 
| 466 | 
> | 
                        getmapped(); | 
| 467 | 
> | 
                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) | 
| 468 | 
  | 
                        goto fail; | 
| 469 | 
  | 
        } | 
| 470 | 
  | 
        return; | 
| 495 | 
  | 
        case MapNotify: | 
| 496 | 
  | 
                map_rcolors(ourras, wind); | 
| 497 | 
  | 
                if (fast) | 
| 498 | 
< | 
                        make_rpixmap(ourras); | 
| 498 | 
> | 
                        make_rpixmap(ourras, wind); | 
| 499 | 
  | 
                break; | 
| 500 | 
  | 
        case UnmapNotify: | 
| 501 | 
< | 
                unmap_rcolors(ourras); | 
| 501 | 
> | 
                if (!fast) | 
| 502 | 
> | 
                        unmap_rcolors(ourras); | 
| 503 | 
  | 
                break; | 
| 504 | 
  | 
        case Expose: | 
| 505 | 
  | 
                redraw(e.e.x, e.e.y, e.e.width, e.e.height); | 
| 531 | 
  | 
        com = buf[0]; | 
| 532 | 
  | 
        switch (com) {                  /* interpret command */ | 
| 533 | 
  | 
        case 'q': | 
| 534 | 
< | 
        case CTRL(D):                           /* quit */ | 
| 534 | 
> | 
        case CTRL('D'):                         /* quit */ | 
| 535 | 
  | 
                quit(0); | 
| 536 | 
  | 
        case '\n': | 
| 537 | 
  | 
        case '\r': | 
| 605 | 
  | 
                free_raster(ourras); | 
| 606 | 
  | 
                getras(); | 
| 607 | 
  | 
        /* fall through */ | 
| 608 | 
< | 
        case CTRL(R):                           /* redraw */ | 
| 609 | 
< | 
        case CTRL(L): | 
| 608 | 
> | 
        case CTRL('R'):                         /* redraw */ | 
| 609 | 
> | 
        case CTRL('L'): | 
| 610 | 
  | 
                unmap_rcolors(ourras); | 
| 611 | 
  | 
                XClearWindow(thedisplay, wind); | 
| 612 | 
  | 
                map_rcolors(ourras, wind); | 
| 613 | 
  | 
                if (fast) | 
| 614 | 
< | 
                        make_rpixmap(ourras); | 
| 614 | 
> | 
                        make_rpixmap(ourras, wind); | 
| 615 | 
  | 
                redraw(0, 0, width, height); | 
| 616 | 
  | 
                return(0); | 
| 617 | 
  | 
        case ' ':                               /* clear */ | 
| 725 | 
  | 
                return(-1); | 
| 726 | 
  | 
        if (left == ll && right == lr && top == lt && bottom == lb) { | 
| 727 | 
  | 
                copycolor(clr, lc); | 
| 728 | 
< | 
                return; | 
| 728 | 
> | 
                return(0); | 
| 729 | 
  | 
        } | 
| 730 | 
  | 
        for (y = top; y < bottom; y++) { | 
| 731 | 
  | 
                if (getscan(y) == -1) | 
| 754 | 
  | 
                quiterr("out of memory in getmono"); | 
| 755 | 
  | 
        dp = ourdata - 1; | 
| 756 | 
  | 
        for (y = 0; y < ymax; y++) { | 
| 757 | 
< | 
                if (getscan(y) < 0) | 
| 646 | 
< | 
                        quiterr("seek error in getmono"); | 
| 647 | 
< | 
                normcolrs(scanline, xmax, scale); | 
| 757 | 
> | 
                getscan(y); | 
| 758 | 
  | 
                add2icon(y, scanline); | 
| 759 | 
+ | 
                normcolrs(scanline, xmax, scale); | 
| 760 | 
  | 
                err = 0; | 
| 761 | 
  | 
                for (x = 0; x < xmax; x++) { | 
| 762 | 
  | 
                        if (!(x&7)) | 
| 782 | 
  | 
        static short  cerr[ICONSIZ]; | 
| 783 | 
  | 
        static int  ynext; | 
| 784 | 
  | 
        static char  *dp; | 
| 785 | 
+ | 
        COLR  clr; | 
| 786 | 
  | 
        register int  err; | 
| 787 | 
  | 
        register int    x, ti; | 
| 788 | 
  | 
        int  errp; | 
| 813 | 
  | 
                        *++dp = 0; | 
| 814 | 
  | 
                errp = err; | 
| 815 | 
  | 
                ti = x*xmax/iconwidth; | 
| 816 | 
< | 
                err += normbright(scan[ti]) + cerr[x]; | 
| 816 | 
> | 
                copycolr(clr, scan[ti]); | 
| 817 | 
> | 
                normcolrs(clr, 1, scale); | 
| 818 | 
> | 
                err += normbright(clr) + cerr[x]; | 
| 819 | 
  | 
                if (err > 127) | 
| 820 | 
  | 
                        err -= 255; | 
| 821 | 
  | 
                else | 
| 830 | 
  | 
getfull()                       /* get full (24-bit) data */ | 
| 831 | 
  | 
{ | 
| 832 | 
  | 
        int     y; | 
| 833 | 
+ | 
        register unsigned long  *dp; | 
| 834 | 
+ | 
        register int    x; | 
| 835 | 
+ | 
                                        /* set gamma correction */ | 
| 836 | 
+ | 
        setcolrgam(gamcor); | 
| 837 | 
+ | 
                                        /* read and convert file */ | 
| 838 | 
+ | 
        dp = (unsigned long *)ourdata; | 
| 839 | 
+ | 
        for (y = 0; y < ymax; y++) { | 
| 840 | 
+ | 
                getscan(y); | 
| 841 | 
+ | 
                add2icon(y, scanline); | 
| 842 | 
+ | 
                if (scale) | 
| 843 | 
+ | 
                        shiftcolrs(scanline, xmax, scale); | 
| 844 | 
+ | 
                colrs_gambs(scanline, xmax); | 
| 845 | 
+ | 
                if (ourras->image->blue_mask & 1) | 
| 846 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 847 | 
+ | 
                                *dp++ = scanline[x][RED] << 16 | | 
| 848 | 
+ | 
                                        scanline[x][GRN] << 8 | | 
| 849 | 
+ | 
                                        scanline[x][BLU] ; | 
| 850 | 
+ | 
                else | 
| 851 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 852 | 
+ | 
                                *dp++ = scanline[x][RED] | | 
| 853 | 
+ | 
                                        scanline[x][GRN] << 8 | | 
| 854 | 
+ | 
                                        scanline[x][BLU] << 16 ; | 
| 855 | 
+ | 
        } | 
| 856 | 
+ | 
} | 
| 857 | 
+ | 
 | 
| 858 | 
+ | 
 | 
| 859 | 
+ | 
getgrey()                       /* get greyscale data */ | 
| 860 | 
+ | 
{ | 
| 861 | 
+ | 
        int     y; | 
| 862 | 
  | 
        register unsigned char  *dp; | 
| 863 | 
  | 
        register int    x; | 
| 864 | 
  | 
                                        /* set gamma correction */ | 
| 866 | 
  | 
                                        /* read and convert file */ | 
| 867 | 
  | 
        dp = ourdata; | 
| 868 | 
  | 
        for (y = 0; y < ymax; y++) { | 
| 869 | 
< | 
                if (getscan(y) < 0) | 
| 870 | 
< | 
                        quiterr("seek error in getfull"); | 
| 869 | 
> | 
                getscan(y); | 
| 870 | 
> | 
                add2icon(y, scanline); | 
| 871 | 
  | 
                if (scale) | 
| 872 | 
  | 
                        shiftcolrs(scanline, xmax, scale); | 
| 873 | 
  | 
                colrs_gambs(scanline, xmax); | 
| 874 | 
+ | 
                if (maxcolors < 256) | 
| 875 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 876 | 
+ | 
                                *dp++ = ((long)normbright(scanline[x]) * | 
| 877 | 
+ | 
                                        maxcolors + 128) >> 8; | 
| 878 | 
+ | 
                else | 
| 879 | 
+ | 
                        for (x = 0; x < xmax; x++) | 
| 880 | 
+ | 
                                *dp++ = normbright(scanline[x]); | 
| 881 | 
+ | 
        } | 
| 882 | 
+ | 
        for (x = 0; x < maxcolors; x++) | 
| 883 | 
+ | 
                clrtab[x][RED] = clrtab[x][GRN] = | 
| 884 | 
+ | 
                        clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors; | 
| 885 | 
+ | 
} | 
| 886 | 
+ | 
 | 
| 887 | 
+ | 
 | 
| 888 | 
+ | 
getmapped()                     /* get color-mapped data */ | 
| 889 | 
+ | 
{ | 
| 890 | 
+ | 
        int     y; | 
| 891 | 
+ | 
                                        /* set gamma correction */ | 
| 892 | 
+ | 
        setcolrgam(gamcor); | 
| 893 | 
+ | 
                                        /* make histogram */ | 
| 894 | 
+ | 
        new_histo(); | 
| 895 | 
+ | 
        for (y = 0; y < ymax; y++) { | 
| 896 | 
+ | 
                if (getscan(y) < 0) | 
| 897 | 
+ | 
                        quiterr("seek error in getmapped"); | 
| 898 | 
  | 
                add2icon(y, scanline); | 
| 899 | 
< | 
                for (x = 0; x < xmax; x++) { | 
| 900 | 
< | 
                        *dp++ = scanline[x][RED]; | 
| 901 | 
< | 
                        *dp++ = scanline[x][GRN]; | 
| 902 | 
< | 
                        *dp++ = scanline[x][BLU]; | 
| 736 | 
< | 
                } | 
| 899 | 
> | 
                if (scale) | 
| 900 | 
> | 
                        shiftcolrs(scanline, xmax, scale); | 
| 901 | 
> | 
                colrs_gambs(scanline, xmax); | 
| 902 | 
> | 
                cnt_colrs(scanline, xmax); | 
| 903 | 
  | 
        } | 
| 904 | 
+ | 
                                        /* map pixels */ | 
| 905 | 
+ | 
        if (!new_clrtab(maxcolors)) | 
| 906 | 
+ | 
                quiterr("cannot create color map"); | 
| 907 | 
+ | 
        for (y = 0; y < ymax; y++) { | 
| 908 | 
+ | 
                if (getscan(y) < 0) | 
| 909 | 
+ | 
                        quiterr("seek error in getmapped"); | 
| 910 | 
+ | 
                if (scale) | 
| 911 | 
+ | 
                        shiftcolrs(scanline, xmax, scale); | 
| 912 | 
+ | 
                colrs_gambs(scanline, xmax); | 
| 913 | 
+ | 
                if (dither) | 
| 914 | 
+ | 
                        dith_colrs(ourdata+y*xmax, scanline, xmax); | 
| 915 | 
+ | 
                else | 
| 916 | 
+ | 
                        map_colrs(ourdata+y*xmax, scanline, xmax); | 
| 917 | 
+ | 
        } | 
| 918 | 
  | 
} | 
| 919 | 
  | 
 | 
| 920 | 
  | 
 | 
| 955 | 
  | 
                if (fseek(fin, scanpos[y], 0) == -1) | 
| 956 | 
  | 
                        quiterr("fseek error"); | 
| 957 | 
  | 
                cury = y; | 
| 958 | 
< | 
        } else if (scanpos != NULL) | 
| 958 | 
> | 
        } else if (scanpos != NULL && scanpos[y] == -1) | 
| 959 | 
  | 
                scanpos[y] = ftell(fin); | 
| 960 | 
  | 
 | 
| 961 | 
  | 
        if (freadcolrs(scanline, xmax, fin) < 0) | 
| 963 | 
  | 
 | 
| 964 | 
  | 
        cury++; | 
| 965 | 
  | 
        return(0); | 
| 786 | 
– | 
} | 
| 787 | 
– | 
 | 
| 788 | 
– | 
 | 
| 789 | 
– | 
picreadline3(y, l3)                     /* read in 3-byte scanline */ | 
| 790 | 
– | 
int  y; | 
| 791 | 
– | 
register rgbpixel  *l3; | 
| 792 | 
– | 
{ | 
| 793 | 
– | 
        register int    i; | 
| 794 | 
– | 
                                                        /* read scanline */ | 
| 795 | 
– | 
        if (getscan(y) < 0) | 
| 796 | 
– | 
                quiterr("cannot seek for picreadline"); | 
| 797 | 
– | 
                                                        /* convert scanline */ | 
| 798 | 
– | 
        normcolrs(scanline, xmax, scale); | 
| 799 | 
– | 
        add2icon(y, scanline); | 
| 800 | 
– | 
        for (i = 0; i < xmax; i++) { | 
| 801 | 
– | 
                l3[i].r = scanline[i][RED]; | 
| 802 | 
– | 
                l3[i].g = scanline[i][GRN]; | 
| 803 | 
– | 
                l3[i].b = scanline[i][BLU]; | 
| 804 | 
– | 
        } | 
| 805 | 
– | 
} | 
| 806 | 
– | 
 | 
| 807 | 
– | 
 | 
| 808 | 
– | 
picwriteline(y, l)              /* add 8-bit scanline to image */ | 
| 809 | 
– | 
int  y; | 
| 810 | 
– | 
pixel  *l; | 
| 811 | 
– | 
{ | 
| 812 | 
– | 
        bcopy((char *)l, (char *)ourdata+y*xmax, xmax); | 
| 813 | 
– | 
} | 
| 814 | 
– | 
 | 
| 815 | 
– | 
 | 
| 816 | 
– | 
picreadcm(map)                  /* do gamma correction */ | 
| 817 | 
– | 
colormap  map; | 
| 818 | 
– | 
{ | 
| 819 | 
– | 
        extern double  pow(); | 
| 820 | 
– | 
        register int  i, val; | 
| 821 | 
– | 
 | 
| 822 | 
– | 
        for (i = 0; i < 256; i++) { | 
| 823 | 
– | 
                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; | 
| 824 | 
– | 
                map[0][i] = map[1][i] = map[2][i] = val; | 
| 825 | 
– | 
        } | 
| 826 | 
– | 
} | 
| 827 | 
– | 
 | 
| 828 | 
– | 
 | 
| 829 | 
– | 
picwritecm(map)                 /* handled elsewhere */ | 
| 830 | 
– | 
colormap  map; | 
| 831 | 
– | 
{ | 
| 832 | 
– | 
#ifdef DEBUG | 
| 833 | 
– | 
        register int i; | 
| 834 | 
– | 
 | 
| 835 | 
– | 
        for (i = 0; i < 256; i++) | 
| 836 | 
– | 
                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); | 
| 837 | 
– | 
#endif | 
| 966 | 
  | 
} |