--- ray/src/px/x11image.c 1992/06/24 09:17:39 2.10 +++ ray/src/px/x11image.c 1992/10/22 20:50:40 2.17 @@ -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,14 +103,15 @@ 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[]; @@ -307,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; @@ -424,8 +410,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; @@ -436,6 +420,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; @@ -446,7 +432,6 @@ static char vistype[][12] = { getras() /* get raster file */ { - colormap ourmap; XVisualInfo vinfo; if (maxcolors <= 2) { /* monochrome */ @@ -458,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; @@ -475,19 +460,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"); } @@ -631,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 */ @@ -745,7 +725,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) @@ -774,10 +754,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)) @@ -803,6 +782,7 @@ COLR *scan; static short cerr[ICONSIZ]; static int ynext; static char *dp; + COLR clr; register int err; register int x, ti; int errp; @@ -833,7 +813,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 @@ -855,12 +837,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 | @@ -885,23 +866,58 @@ 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); 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; + 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"); + 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; @@ -939,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) @@ -947,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; -{ -#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 }