--- ray/src/px/x11image.c 1992/05/27 14:28:50 2.6 +++ ray/src/px/x11image.c 1992/10/12 16:24:02 2.14 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -27,7 +27,6 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" #include "view.h" -#include "pic.h" #include "x11raster.h" #include "random.h" #include "resolu.h" @@ -41,8 +40,6 @@ static char SCCSid[] = "$SunId$ LBL"; #define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ #define ourscreen DefaultScreen(thedisplay) -#define ourblack BlackPixel(thedisplay,ourscreen) -#define ourwhite WhitePixel(thedisplay,ourscreen) #define ourroot RootWindow(thedisplay,ourscreen) #define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) @@ -57,6 +54,7 @@ int fast = 0; /* keep picture in Pixmap? */ char *dispname = NULL; /* our display name */ Window wind = 0; /* our output window */ +unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */ Font fontid; /* our font */ int maxcolors = 0; /* maximum colors */ @@ -105,22 +103,27 @@ char *progname; char errmsg[128]; +extern BYTE clrtab[256][3]; /* global color map */ + extern long ftell(); extern char *malloc(), *calloc(); -extern double pow(), log(); - Display *thedisplay; + main(argc, argv) int argc; char *argv[]; { + extern char *getenv(); + char *gv; int headline(); int i; progname = argv[0]; + if ((gv = getenv("GAMMA")) != NULL) + gamcor = atof(gv); for (i = 1; i < argc; i++) if (argv[i][0] == '-') @@ -135,11 +138,10 @@ char *argv[]; maxcolors = 2; break; case 'd': - if (argv[i][2] == 'i') { + if (argv[i][2] == 'i') dispname = argv[++i]; - break; - } - dither = !dither; + else + dither = !dither; break; case 'f': fast = !fast; @@ -150,7 +152,7 @@ char *argv[]; scale = atoi(argv[++i]); break; case 'g': - if (!strcmp(argv[i], "-geometry")) + if (argv[i][2] == 'e') geometry = argv[++i]; else gamcor = atof(argv[++i]); @@ -191,7 +193,7 @@ char *argv[]; getevent(); /* main loop */ userr: fprintf(stderr, - "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", + "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", progname); quit(1); } @@ -242,11 +244,11 @@ init() /* get data and open window */ CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); if (wind == 0) quiterr("cannot create window"); + XFreeColormap(thedisplay, ourwinattr.colormap); width = xmax; height = ymax; ourgc = XCreateGC(thedisplay, wind, 0, 0); - XSetState(thedisplay, ourgc, BlackPixel(thedisplay,ourscreen), - WhitePixel(thedisplay,ourscreen), GXcopy, AllPlanes); + XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); revgc = XCreateGC(thedisplay, wind, 0, 0); XSetFunction(thedisplay, revgc, GXinvert); fontid = XLoadFont(thedisplay, FONTNAME); @@ -305,20 +307,6 @@ char *err; } -eputs(s) -char *s; -{ - fputs(s, stderr); -} - - -quit(code) -int code; -{ - exit(code); -} - - static int viscmp(v1,v2) /* compare visual to see which is better, descending */ register XVisualInfo *v1, *v2; @@ -362,6 +350,7 @@ register XVisualInfo *v1, *v2; getbestvis() /* get the best visual for this screen */ { +#ifdef DEBUG static char vistype[][12] = { "StaticGray", "GrayScale", @@ -370,10 +359,11 @@ static char vistype[][12] = { "TrueColor", "DirectColor" }; +#endif static int rankings[3][6] = { {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, - {PseudoColor,GrayScale,-1}, - {PseudoColor,GrayScale,-1} + {PseudoColor,GrayScale,StaticGray,-1}, + {PseudoColor,GrayScale,StaticGray,-1} }; XVisualInfo *xvi; int vismatched; @@ -393,8 +383,12 @@ static char vistype[][12] = { xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); if (xvi == NULL) quiterr("no visuals for this screen!"); -for (i = 0; i < vismatched; i++) -fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i].class], xvi[i].depth); +#ifdef DEBUG + fprintf(stderr, "Supported visuals:\n"); + for (i = 0; i < vismatched; i++) + fprintf(stderr, "\ttype %s, depth %d\n", + vistype[xvi[i].class], xvi[i].depth); +#endif for (i = 0, j = 1; j < vismatched; j++) if (viscmp(&xvi[i],&xvi[j]) > 0) i = j; @@ -407,21 +401,37 @@ fprintf(stderr, "Type %s, depth %d\n", vistype[xvi[i]. quiterr("inadequate visuals on this screen"); /* OK, we'll use it */ copystruct(&ourvis, &xvi[i]); -fprintf(stderr, "Selected visual type %s, depth %d\n", vistype[ourvis.class], -ourvis.depth); +#ifdef DEBUG + fprintf(stderr, "Selected visual type %s, depth %d\n", + vistype[ourvis.class], ourvis.depth); +#endif + /* make appropriate adjustments */ if (ourvis.class == GrayScale || ourvis.class == StaticGray) greyscale = 1; - if (1< 4) - maxcolors -= 2; + if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) + maxcolors = ourvis.colormap_size; + if (ourvis.class == StaticGray) { + ourblack = 0; + ourwhite = 255; + } else if (ourvis.class == PseudoColor) { + ourblack = BlackPixel(thedisplay,ourscreen); + ourwhite = WhitePixel(thedisplay,ourscreen); + if ((ourblack|ourwhite) & ~255L) { + ourblack = 0; + ourwhite = 1; + } + if (maxcolors > 4) + maxcolors -= 2; + } else { + ourblack = 0; + ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; + } XFree((char *)xvi); } getras() /* get raster file */ { - colormap ourmap; XVisualInfo vinfo; if (maxcolors <= 2) { /* monochrome */ @@ -433,7 +443,7 @@ getras() /* get raster file */ if (ourras == NULL) goto fail; getmono(); - } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { + } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { ourdata = (unsigned char *)malloc(4*xmax*ymax); if (ourdata == NULL) goto fail; @@ -450,11 +460,11 @@ getras() /* get raster file */ xmax, ymax, 8); if (ourras == NULL) goto fail; - if (greyscale) - biq(dither,maxcolors,1,ourmap); + if (greyscale | ourvis.class == StaticGray) + getgrey(); else - ciq(dither,maxcolors,1,ourmap); - if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) + getmapped(); + if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) goto fail; } return; @@ -488,7 +498,8 @@ getevent() /* process the next event */ make_rpixmap(ourras, wind); break; case UnmapNotify: - unmap_rcolors(ourras); + if (!fast) + unmap_rcolors(ourras); break; case Expose: redraw(e.e.x, e.e.y, e.e.width, e.e.height); @@ -600,7 +611,7 @@ XKeyPressedEvent *ekey; XClearWindow(thedisplay, wind); map_rcolors(ourras, wind); if (fast) - make_rpixmap(ourras); + make_rpixmap(ourras, wind); redraw(0, 0, width, height); return(0); case ' ': /* clear */ @@ -844,6 +855,69 @@ getfull() /* get full (24-bit) data */ } +getgrey() /* get greyscale data */ +{ + int y; + register unsigned char *dp; + register int x; + /* set gamma correction */ + setcolrgam(gamcor); + /* read and convert file */ + dp = ourdata; + for (y = 0; y < ymax; y++) { + if (getscan(y) < 0) + quiterr("seek error in getgrey"); + if (scale) + shiftcolrs(scanline, xmax, scale); + colrs_gambs(scanline, xmax); + add2icon(y, scanline); + if (maxcolors < 256) + for (x = 0; x < xmax; x++) + *dp++ = ((long)normbright(scanline[x]) * + maxcolors + 128) >> 8; + else + for (x = 0; x < xmax; x++) + *dp++ = normbright(scanline[x]); + } + for (x = 0; x < maxcolors; x++) + clrtab[x][RED] = clrtab[x][GRN] = + clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors; +} + + +getmapped() /* get color-mapped data */ +{ + int y; + /* set gamma correction */ + setcolrgam(gamcor); + /* make histogram */ + new_histo(); + for (y = 0; y < ymax; y++) { + if (getscan(y) < 0) + quiterr("seek error in getmapped"); + if (scale) + shiftcolrs(scanline, xmax, scale); + colrs_gambs(scanline, xmax); + add2icon(y, scanline); + cnt_colrs(scanline, xmax); + } + /* map pixels */ + if (!new_clrtab(maxcolors)) + quiterr("cannot create color map"); + for (y = 0; y < ymax; y++) { + if (getscan(y) < 0) + quiterr("seek error in getmapped"); + if (scale) + shiftcolrs(scanline, xmax, scale); + colrs_gambs(scanline, xmax); + if (dither) + dith_colrs(ourdata+y*xmax, scanline, xmax); + else + map_colrs(ourdata+y*xmax, scanline, xmax); + } +} + + scale_rcolors(xr, sf) /* scale color map */ register XRASTER *xr; double sf; @@ -881,7 +955,7 @@ int y; if (fseek(fin, scanpos[y], 0) == -1) quiterr("fseek error"); cury = y; - } else if (scanpos != NULL) + } else if (scanpos != NULL && scanpos[y] == -1) scanpos[y] = ftell(fin); if (freadcolrs(scanline, xmax, fin) < 0) @@ -889,56 +963,4 @@ int y; cury++; return(0); -} - - -picreadline3(y, l3) /* read in 3-byte scanline */ -int y; -register rgbpixel *l3; -{ - register int i; - /* read scanline */ - if (getscan(y) < 0) - quiterr("cannot seek for picreadline"); - /* convert scanline */ - normcolrs(scanline, xmax, scale); - add2icon(y, scanline); - for (i = 0; i < xmax; i++) { - l3[i].r = scanline[i][RED]; - l3[i].g = scanline[i][GRN]; - l3[i].b = scanline[i][BLU]; - } -} - - -picwriteline(y, l) /* add 8-bit scanline to image */ -int y; -pixel *l; -{ - bcopy((char *)l, (char *)ourdata+y*xmax, xmax); -} - - -picreadcm(map) /* do gamma correction */ -colormap map; -{ - extern double pow(); - register int i, val; - - for (i = 0; i < 256; i++) { - val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; - map[0][i] = map[1][i] = map[2][i] = val; - } -} - - -picwritecm(map) /* handled elsewhere */ -colormap map; -{ -#ifdef DEBUG - register int i; - - for (i = 0; i < 256; i++) - printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); -#endif }