--- ray/src/px/x11image.c 1991/05/03 09:49:02 1.18 +++ ray/src/px/x11image.c 1993/02/12 18:03:08 2.20 @@ -1,4 +1,4 @@ -/* Copyright (c) 1990 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -27,23 +27,20 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" #include "view.h" -#include "pic.h" #include "x11raster.h" #include "random.h" +#include "resolu.h" #define FONTNAME "8x13" /* text font we'll use */ -#define CTRL(c) ('c'-'@') +#define CTRL(c) ((c)-'@') #define BORWIDTH 5 /* border width */ #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 ourgc DefaultGC(thedisplay,ourscreen) #define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) @@ -54,7 +51,10 @@ double gamcor = 2.2; /* gamma correction */ int dither = 1; /* dither colors? */ 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 */ @@ -70,7 +70,8 @@ int gotview = 0; /* got parameters from file */ COLR *scanline; /* scan line buffer */ -int xmax, ymax; /* picture resolution */ +RESOLU inpres; /* input resolution and ordering */ +int xmax, ymax; /* picture dimensions */ int width, height; /* window size */ char *fname = NULL; /* input file name */ FILE *fin = stdin; /* input file */ @@ -81,9 +82,12 @@ double exposure = 1.0; /* exposure compensation use int wrongformat = 0; /* input in another format? */ +GC ourgc; /* standard graphics context */ GC revgc; /* graphics context with GXinvert */ -XRASTER *ourras; /* our stored image */ +int *ourrank; /* our visual class ranking */ +XVisualInfo ourvis; /* our visual */ +XRASTER *ourras; /* our stored image */ unsigned char *ourdata; /* our image data */ struct { @@ -99,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 atof(), 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] == '-') @@ -129,7 +138,10 @@ char *argv[]; maxcolors = 2; break; case 'd': - dither = !dither; + if (argv[i][2] == 'i') + dispname = argv[++i]; + else + dither = !dither; break; case 'f': fast = !fast; @@ -140,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]); @@ -157,7 +169,7 @@ char *argv[]; fname = argv[i]; fin = fopen(fname, "r"); if (fin == NULL) { - sprintf(errmsg, "can't open file \"%s\"", fname); + sprintf(errmsg, "cannot open file \"%s\"", fname); quiterr(errmsg); } } else if (i != argc) @@ -165,8 +177,10 @@ char *argv[]; /* get header */ getheader(fin, headline, NULL); /* get picture dimensions */ - if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) + if (wrongformat || !fgetsresolu(&inpres, fin)) quiterr("bad picture format"); + xmax = scanlen(&inpres); + ymax = numscans(&inpres); /* set view parameters */ if (gotview && setview(&ourview) != NULL) gotview = 0; @@ -179,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); } @@ -188,8 +202,6 @@ userr: headline(s) /* get relevant info from header */ char *s; { - static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; - register char **an; char fmt[32]; if (isexpos(s)) @@ -197,13 +209,8 @@ char *s; else if (isformat(s)) { formatval(fmt, s); wrongformat = strcmp(fmt, COLRFMT); - } else - for (an = altname; *an != NULL; an++) - if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&ourview, s+strlen(*an)) > 0) - gotview++; - return; - } + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; } @@ -211,8 +218,8 @@ init() /* get data and open window */ { XWMHints ourxwmhints; XSetWindowAttributes ourwinattr; - XSizeHints oursizhints; - register int i; + XSizeHints oursizhints; + register int i; if (fname != NULL) { scanpos = (long *)malloc(ymax*sizeof(long)); @@ -221,46 +228,23 @@ init() /* get data and open window */ for (i = 0; i < ymax; i++) scanpos[i] = -1; } - if ((thedisplay = XOpenDisplay(NULL)) == NULL) - quiterr("can't open display; DISPLAY variable set?"); - if (maxcolors == 0) { /* get number of available colors */ - i = DisplayPlanes(thedisplay,ourscreen); - maxcolors = i > 8 ? 256 : 1< 4) maxcolors -= 2; - } + if ((thedisplay = XOpenDisplay(dispname)) == NULL) + quiterr("cannot open display"); + /* get best visual for default screen */ + getbestvis(); /* store image */ getras(); - /* open window */ - ourwinattr.border_pixel = ourblack; - ourwinattr.background_pixel = ourwhite; - wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, - 0, InputOutput, ourras->visual, - CWBackPixel|CWBorderPixel, &ourwinattr); - if (wind == 0) - quiterr("can't create window"); - width = xmax; - height = ymax; - fontid = XLoadFont(thedisplay, FONTNAME); - if (fontid == 0) - quiterr("can't get font"); - XSetFont(thedisplay, ourgc, fontid); - revgc = XCreateGC(thedisplay, wind, 0, 0); - XSetFunction(thedisplay, revgc, GXinvert); - XStoreName(thedisplay, wind, fname == NULL ? progname : fname); - XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, - XC_diamond_cross)); + /* get size and position */ + bzero((char *)&oursizhints, sizeof(oursizhints)); + oursizhints.width = xmax; oursizhints.height = ymax; 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; + else oursizhints.flags |= PSize; - } if ((i&(XValue|YValue)) == (XValue|YValue)) { oursizhints.flags |= USPosition; if (i & XNegative) @@ -270,8 +254,34 @@ init() /* get data and open window */ oursizhints.y += DisplayHeight(thedisplay, ourscreen)-1-oursizhints.height-2*BORWIDTH; } - XSetNormalHints(thedisplay, wind, &oursizhints); } + /* open window */ + ourwinattr.border_pixel = ourblack; + ourwinattr.background_pixel = ourwhite; + ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, + ourvis.visual, AllocNone); + 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) + quiterr("cannot create window"); + XFreeColormap(thedisplay, ourwinattr.colormap); + width = xmax; + height = ymax; + ourgc = XCreateGC(thedisplay, wind, 0, 0); + XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); + revgc = XCreateGC(thedisplay, wind, 0, 0); + XSetFunction(thedisplay, revgc, GXinvert); + fontid = XLoadFont(thedisplay, FONTNAME); + if (fontid == 0) + quiterr("cannot get font"); + XSetFont(thedisplay, ourgc, fontid); + XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, + XC_diamond_cross)); + XStoreName(thedisplay, wind, fname == NULL ? progname : fname); + if (oursizhints.flags) + XSetNormalHints(thedisplay, wind, &oursizhints); ourxwmhints.flags = InputHint|IconPixmapHint; ourxwmhints.input = True; ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, @@ -298,42 +308,148 @@ char *err; } -eputs(s) -char *s; +static int +viscmp(v1,v2) /* compare visual to see which is better, descending */ +register XVisualInfo *v1, *v2; { - fputs(s, stderr); + int bad1 = 0, bad2 = 0; + register int *rp; + + if (v1->class == v2->class) { + if (v1->class == TrueColor || v1->class == DirectColor) { + /* prefer 24-bit to 32-bit */ + if (v1->depth == 24 && v2->depth == 32) + return(-1); + if (v1->depth == 32 && v2->depth == 24) + return(1); + return(0); + } + /* don't be too greedy */ + if (maxcolors <= 1<depth && maxcolors <= 1<depth) + return(v1->depth - v2->depth); + return(v2->depth - v1->depth); + } + /* prefer Pseudo when < 24-bit */ + if ((v1->class == TrueColor || v1->class == DirectColor) && + v1->depth < 24) + bad1 = 1; + if ((v2->class == TrueColor || v2->class == DirectColor) && + v2->depth < 24) + bad2 = -1; + if (bad1 | bad2) + return(bad1+bad2); + /* otherwise, use class ranking */ + for (rp = ourrank; *rp != -1; rp++) { + if (v1->class == *rp) + return(-1); + if (v2->class == *rp) + return(1); + } + return(0); } -quit(code) -int code; +getbestvis() /* get the best visual for this screen */ { - exit(code); +#ifdef DEBUG +static char vistype[][12] = { + "StaticGray", + "GrayScale", + "StaticColor", + "PseudoColor", + "TrueColor", + "DirectColor" +}; +#endif + static int rankings[3][6] = { + {TrueColor,DirectColor,PseudoColor,GrayScale,StaticGray,-1}, + {PseudoColor,GrayScale,StaticGray,-1}, + {PseudoColor,GrayScale,StaticGray,-1} + }; + XVisualInfo *xvi; + int vismatched; + register int i, j; + + if (greyscale) { + ourrank = rankings[2]; + if (maxcolors < 2) maxcolors = 256; + } else if (maxcolors >= 2 && maxcolors <= 256) + ourrank = rankings[1]; + else { + ourrank = rankings[0]; + maxcolors = 256; + } + /* find best visual */ + ourvis.screen = ourscreen; + xvi = XGetVisualInfo(thedisplay,VisualScreenMask,&ourvis,&vismatched); + if (xvi == NULL) + quiterr("no visuals for this screen!"); +#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; + /* compare to least acceptable */ + for (j = 0; ourrank[j++] != -1; ) + ; + ourvis.class = ourrank[--j]; + ourvis.depth = 1; + if (viscmp(&xvi[i],&ourvis) > 0) + quiterr("inadequate visuals on this screen"); + /* OK, we'll use it */ + copystruct(&ourvis, &xvi[i]); +#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 (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 */ ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); if (ourdata == NULL) goto fail; - ourras = make_raster(thedisplay, ourscreen, 1, ourdata, + ourras = make_raster(thedisplay, &ourvis, 1, ourdata, xmax, ymax, 8); if (ourras == NULL) goto fail; getmono(); - } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) - /* kludge for DirectColor */ - || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { - ourdata = (unsigned char *)malloc(xmax*ymax*3); + } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { + ourdata = (unsigned char *)malloc(4*xmax*ymax); if (ourdata == NULL) goto fail; - ourras = make_raster(thedisplay, ourscreen, 24, ourdata, - xmax, ymax, 8); + ourras = make_raster(thedisplay, &ourvis, 32, + ourdata, xmax, ymax, 32); if (ourras == NULL) goto fail; getfull(); @@ -341,15 +457,15 @@ getras() /* get raster file */ ourdata = (unsigned char *)malloc(xmax*ymax); if (ourdata == NULL) goto fail; - ourras = make_raster(thedisplay, ourscreen, 8, ourdata, + ourras = make_raster(thedisplay, &ourvis, 8, ourdata, 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; @@ -380,10 +496,11 @@ getevent() /* process the next event */ case MapNotify: map_rcolors(ourras, wind); if (fast) - make_rpixmap(ourras); + 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); @@ -406,6 +523,7 @@ XKeyPressedEvent *ekey; XColor cvx; int com, n; double comp; + FLOAT hv[2]; FVECT rorg, rdir; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); @@ -414,7 +532,7 @@ XKeyPressedEvent *ekey; com = buf[0]; switch (com) { /* interpret command */ case 'q': - case CTRL(D): /* quit */ + case CTRL('D'): /* quit */ quit(0); case '\n': case '\r': @@ -454,7 +572,9 @@ XKeyPressedEvent *ekey; XStoreColor(thedisplay, ourras->cmap, &cvx); return(0); case 'p': /* position */ - sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); + pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); + sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr), + (int)(hv[1]*inpres.yr)); XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y, buf, strlen(buf)); return(0); @@ -463,9 +583,8 @@ XKeyPressedEvent *ekey; XBell(thedisplay, 0); return(-1); } - if (viewray(rorg, rdir, &ourview, - (ekey->x-xoff+.5)/xmax, - (ymax-1-ekey->y+yoff+.5)/ymax) < 0) + pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); + if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) return(-1); printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); @@ -487,15 +606,22 @@ XKeyPressedEvent *ekey; free_raster(ourras); getras(); /* fall through */ - case CTRL(R): /* redraw */ - case CTRL(L): + case CTRL('R'): /* redraw */ + case CTRL('L'): unmap_rcolors(ourras); 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); @@ -580,6 +706,8 @@ int x0, y0, x1, y1; avgbox(clr) /* average color over current box */ COLOR clr; { + static COLOR lc; + static int ll, lr, lt, lb; int left, right, top, bottom; int y; double d; @@ -603,6 +731,10 @@ COLOR clr; bottom = ymax; if (top >= bottom) return(-1); + if (left == ll && right == lr && top == lt && bottom == lb) { + copycolor(clr, lc); + return(0); + } for (y = top; y < bottom; y++) { if (getscan(y) == -1) return(-1); @@ -613,6 +745,8 @@ COLOR clr; } d = 1.0/((right-left)*(bottom-top)); scalecolor(clr, d); + ll = left; lr = right; lt = top; lb = bottom; + copycolor(lc, clr); return(0); } @@ -621,27 +755,28 @@ getmono() /* get monochrome data */ { register unsigned char *dp; register int x, err; - int y; + int y, errp; short *cerr; if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) 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)) *++dp = 0; + errp = err; err += normbright(scanline[x]) + cerr[x]; if (err > 127) err -= 255; else *dp |= 1<<(7-(x&07)); - cerr[x] = err >>= 1; + err /= 3; + cerr[x] = err + errp; } } free((char *)cerr); @@ -652,10 +787,13 @@ add2icon(y, scan) /* add a scanline to our icon data int y; COLR *scan; { - static char *dp = NULL; static short cerr[ICONSIZ]; + static int ynext; + static char *dp; + COLR clr; register int err; - register int x, xi; + register int x, ti; + int errp; if (iconheight == 0) { /* initialize */ if (xmax <= ICONSIZ && ymax <= ICONSIZ) { @@ -664,36 +802,71 @@ COLR *scan; } else if (xmax > ymax) { iconwidth = ICONSIZ; iconheight = ICONSIZ*ymax/xmax; + if (iconheight < 1) + iconheight = 1; } else { iconwidth = ICONSIZ*xmax/ymax; + if (iconwidth < 1) + iconwidth = 1; iconheight = ICONSIZ; } + ynext = 0; dp = icondata - 1; } - if (dp == NULL) /* done already */ + if (y < ynext*ymax/iconheight) /* skip this one */ return; - if (y % (ymax/iconheight)) /* skip this one */ - return; err = 0; for (x = 0; x < iconwidth; x++) { if (!(x&7)) *++dp = 0; - xi = x*xmax/iconwidth; - err += normbright(scan[xi]) + cerr[x]; + errp = err; + ti = x*xmax/iconwidth; + copycolr(clr, scan[ti]); + normcolrs(clr, 1, scale); + err += normbright(clr) + cerr[x]; if (err > 127) err -= 255; else *dp |= 1<<(x&07); - cerr[x] = err >>= 1; + err /= 3; + cerr[x] = err + errp; } - if (y >= ymax - ymax/iconheight) /* all done */ - dp = NULL; + ynext++; } getfull() /* get full (24-bit) data */ { int y; + register unsigned long *dp; + register int x; + /* set gamma correction */ + setcolrgam(gamcor); + /* read and convert file */ + dp = (unsigned long *)ourdata; + for (y = 0; y < ymax; y++) { + getscan(y); + add2icon(y, scanline); + if (scale) + shiftcolrs(scanline, xmax, scale); + colrs_gambs(scanline, xmax); + if (ourras->image->blue_mask & 1) + for (x = 0; x < xmax; x++) + *dp++ = scanline[x][RED] << 16 | + scanline[x][GRN] << 8 | + scanline[x][BLU] ; + else + for (x = 0; x < xmax; x++) + *dp++ = scanline[x][RED] | + scanline[x][GRN] << 8 | + scanline[x][BLU] << 16 ; + } +} + + +getgrey() /* get greyscale data */ +{ + int y; register unsigned char *dp; register int x; /* set gamma correction */ @@ -701,18 +874,57 @@ getfull() /* get full (24-bit) 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); + if (maxcolors < 256) + for (x = 0; x < xmax; x++) + *dp++ = ((long)scanline[x][GRN] * + maxcolors + maxcolors/2) >> 8; + else + for (x = 0; x < xmax; 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); - for (x = 0; x < xmax; x++) { - *dp++ = scanline[x][RED]; - *dp++ = scanline[x][GRN]; - *dp++ = scanline[x][BLU]; - } + 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); + } } @@ -753,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) @@ -761,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; -{ -#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 }