--- ray/src/px/x11image.c 1992/05/30 09:33:01 2.9 +++ ray/src/px/x11image.c 1993/02/19 10:46:16 2.21 @@ -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" @@ -104,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] == '-') @@ -189,9 +193,9 @@ char *argv[]; getevent(); /* main loop */ userr: fprintf(stderr, - "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", +"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", progname); - quit(1); + exit(1); } @@ -230,12 +234,34 @@ init() /* get data and open window */ getbestvis(); /* store image */ getras(); + /* get size and position */ + bzero((char *)&oursizhints, sizeof(oursizhints)); + oursizhints.width = xmax; oursizhints.height = ymax; + if (geometry != NULL) { + i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, + (unsigned *)&oursizhints.width, + (unsigned *)&oursizhints.height); + if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) + oursizhints.flags |= USSize; + else + oursizhints.flags |= PSize; + if ((i&(XValue|YValue)) == (XValue|YValue)) { + oursizhints.flags |= USPosition; + if (i & XNegative) + oursizhints.x += DisplayWidth(thedisplay, + ourscreen)-1-oursizhints.width-2*BORWIDTH; + if (i & YNegative) + oursizhints.y += DisplayHeight(thedisplay, + ourscreen)-1-oursizhints.height-2*BORWIDTH; + } + } /* open window */ ourwinattr.border_pixel = ourblack; ourwinattr.background_pixel = ourwhite; ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, ourvis.visual, AllocNone); - wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, + wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y, + oursizhints.width, oursizhints.height, BORWIDTH, ourvis.depth, InputOutput, ourvis.visual, CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); if (wind == 0) @@ -254,29 +280,8 @@ init() /* get data and open window */ XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, XC_diamond_cross)); XStoreName(thedisplay, wind, fname == NULL ? progname : fname); - if (geometry != NULL) { - bzero((char *)&oursizhints, sizeof(oursizhints)); - i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, - (unsigned *)&oursizhints.width, - (unsigned *)&oursizhints.height); - if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) - oursizhints.flags |= USSize; - else { - oursizhints.width = xmax; - oursizhints.height = ymax; - oursizhints.flags |= PSize; - } - if ((i&(XValue|YValue)) == (XValue|YValue)) { - oursizhints.flags |= USPosition; - if (i & XNegative) - oursizhints.x += DisplayWidth(thedisplay, - ourscreen)-1-oursizhints.width-2*BORWIDTH; - if (i & YNegative) - oursizhints.y += DisplayHeight(thedisplay, - ourscreen)-1-oursizhints.height-2*BORWIDTH; - } + if (oursizhints.flags) XSetNormalHints(thedisplay, wind, &oursizhints); - } ourxwmhints.flags = InputHint|IconPixmapHint; ourxwmhints.input = True; ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, @@ -303,20 +308,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; @@ -420,8 +411,6 @@ static char vistype[][12] = { greyscale = 1; if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) maxcolors = ourvis.colormap_size; - if (maxcolors > 4) - maxcolors -= 2; if (ourvis.class == StaticGray) { ourblack = 0; ourwhite = 255; @@ -432,6 +421,8 @@ static char vistype[][12] = { ourblack = 0; ourwhite = 1; } + if (maxcolors > 4) + maxcolors -= 2; } else { ourblack = 0; ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; @@ -442,7 +433,6 @@ static char vistype[][12] = { getras() /* get raster file */ { - colormap ourmap; XVisualInfo vinfo; if (maxcolors <= 2) { /* monochrome */ @@ -454,7 +444,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; @@ -471,19 +461,14 @@ getras() /* get raster file */ xmax, ymax, 8); if (ourras == NULL) goto fail; - if (ourvis.class == StaticGray) + if (greyscale | ourvis.class == StaticGray) getgrey(); - else { - if (greyscale) - biq(dither,maxcolors,1,ourmap); - else - ciq(dither,maxcolors,1,ourmap); - if (init_rcolors(ourras, ourmap[0], - ourmap[1], ourmap[2]) == 0) - goto fail; - } + else + getmapped(); + if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) + goto fail; } - return; + return; fail: quiterr("could not create raster image"); } @@ -548,7 +533,7 @@ XKeyPressedEvent *ekey; switch (com) { /* interpret command */ case 'q': case CTRL('D'): /* quit */ - quit(0); + quiterr(NULL); case '\n': case '\r': case 'l': @@ -627,9 +612,16 @@ 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 '0': /* recenter origin */ + if (xoff == 0 & yoff == 0) + return(0); + xoff = yoff = 0; + XClearWindow(thedisplay, wind); + redraw(0, 0, width, height); + return(0); case ' ': /* clear */ redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); return(0); @@ -741,7 +733,7 @@ COLOR clr; return(-1); if (left == ll && right == lr && top == lt && bottom == lb) { copycolor(clr, lc); - return; + return(0); } for (y = top; y < bottom; y++) { if (getscan(y) == -1) @@ -770,10 +762,9 @@ getmono() /* get monochrome data */ quiterr("out of memory in getmono"); dp = ourdata - 1; for (y = 0; y < ymax; y++) { - if (getscan(y) < 0) - quiterr("seek error in getmono"); - normcolrs(scanline, xmax, scale); + getscan(y); add2icon(y, scanline); + normcolrs(scanline, xmax, scale); err = 0; for (x = 0; x < xmax; x++) { if (!(x&7)) @@ -799,6 +790,7 @@ COLR *scan; static short cerr[ICONSIZ]; static int ynext; static char *dp; + COLR clr; register int err; register int x, ti; int errp; @@ -829,7 +821,9 @@ COLR *scan; *++dp = 0; errp = err; ti = x*xmax/iconwidth; - err += normbright(scan[ti]) + cerr[x]; + copycolr(clr, scan[ti]); + normcolrs(clr, 1, scale); + err += normbright(clr) + cerr[x]; if (err > 127) err -= 255; else @@ -851,12 +845,11 @@ getfull() /* get full (24-bit) data */ /* read and convert file */ dp = (unsigned long *)ourdata; for (y = 0; y < ymax; y++) { - if (getscan(y) < 0) - quiterr("seek error in getfull"); + getscan(y); + add2icon(y, scanline); if (scale) shiftcolrs(scanline, xmax, scale); colrs_gambs(scanline, xmax); - add2icon(y, scanline); if (ourras->image->blue_mask & 1) for (x = 0; x < xmax; x++) *dp++ = scanline[x][RED] << 16 | @@ -881,23 +874,60 @@ getgrey() /* get greyscale data */ /* read and convert file */ dp = ourdata; for (y = 0; y < ymax; y++) { - if (getscan(y) < 0) - quiterr("seek error in getfull"); + getscan(y); + add2icon(y, scanline); if (scale) shiftcolrs(scanline, xmax, scale); + for (x = 0; x < xmax; x++) + scanline[x][GRN] = normbright(scanline[x]); colrs_gambs(scanline, xmax); - add2icon(y, scanline); - if (ourvis.colormap_size < 256) + if (maxcolors < 256) for (x = 0; x < xmax; x++) - *dp++ = ((long)normbright(scanline[x]) * - ourvis.colormap_size + 128) >> 8; + *dp++ = ((long)scanline[x][GRN] * + maxcolors + maxcolors/2) >> 8; else for (x = 0; x < xmax; x++) - *dp++ = normbright(scanline[x]); + *dp++ = scanline[x][GRN]; } + for (x = 0; x < maxcolors; x++) + clrtab[x][RED] = clrtab[x][GRN] = + clrtab[x][BLU] = ((long)x*256 + 128)/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"); + add2icon(y, scanline); + if (scale) + shiftcolrs(scanline, xmax, scale); + colrs_gambs(scanline, xmax); + 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; @@ -935,7 +965,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) @@ -943,56 +973,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; -{ -#if 0 - register int i; - - for (i = 0; i < 256; i++) - printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); -#endif }