--- ray/src/px/x11image.c 1999/07/20 14:07:38 2.55 +++ ray/src/px/x11image.c 2005/01/07 20:33:02 2.69 @@ -1,9 +1,6 @@ -/* Copyright (c) 1999 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: x11image.c,v 2.69 2005/01/07 20:33:02 greg Exp $"; #endif - /* * x11image.c - driver for X-windows * @@ -21,20 +18,22 @@ static char SCCSid[] = "$SunId$ SGI"; #include "standard.h" +#include #include +#include +#include +#include #include #include #include #include -#undef NOPROTO -#define NOPROTO 1 #include "color.h" #include "tonemap.h" +#include "clrtab.h" #include "view.h" #include "x11raster.h" #include "random.h" -#include "resolu.h" #define FONTNAME "8x13" /* text font we'll use */ @@ -99,6 +98,9 @@ double exposure = 1.0; /* exposure compensation use int wrongformat = 0; /* input in another format? */ +TMstruct *tmGlobal; /* base tone-mapping */ +TMstruct *tmCurrent; /* curren tone-mapping */ + GC ourgc; /* standard graphics context */ GC revgc; /* graphics context with GXinvert */ @@ -109,7 +111,7 @@ unsigned char *ourdata; /* our image data */ struct { int xmin, ymin, xsiz, ysiz; -} box = {0, 0, 0, 0}; /* current box */ +} bbox = {0, 0, 0, 0}; /* current bbox */ char *geometry = NULL; /* geometry specification */ @@ -122,23 +124,47 @@ char errmsg[128]; BYTE clrtab[256][3]; /* global color map */ -extern long ftell(); -extern char *getenv(); - Display *thedisplay; Atom closedownAtom, wmProtocolsAtom; int sigrecv; -int onsig() { sigrecv++; } +void onsig(int i) { sigrecv++; } +typedef void doboxf_t(COLR *scn, int n, void *); -main(argc, argv) -int argc; -char *argv[]; +static gethfunc headline; +static void init(int argc, char **argv); +static void quiterr(char *err); +static int viscmp(XVisualInfo *v1, XVisualInfo *v2); +static void getbestvis(void); +static void getras(void); +static void getevent(void); +static int traceray(int xpos, int ypos); +static int docom(XKeyPressedEvent *ekey); +static void moveimage(XButtonPressedEvent *ebut); +static void getbox(XButtonPressedEvent *ebut); +static void trackrays(XButtonPressedEvent *ebut); +static void revbox(int x0, int y0, int x1, int y1); +static doboxf_t colavg; +static doboxf_t addfix; +static int avgbox(COLOR cavg); +static int dobox(doboxf_t *f, void *p); +static void make_tonemap(void); +static void tmap_colrs(COLR *scn, int len); +static void getmono(void); +static void add2icon(int y, COLR *scan); +static void getfull(void); +static void getgrey(void); +static void getmapped(void); +static void scale_rcolors(XRASTER *xr, double sf); +static int getscan(int y); + + +int +main(int argc, char *argv[]) { - int headline(); int i; int pid; @@ -251,9 +277,11 @@ userr: } -int -headline(s) /* get relevant info from header */ -char *s; +static int +headline( /* get relevant info from header */ + char *s, + void *p +) { char fmt[32]; @@ -267,9 +295,11 @@ char *s; } -init(argc, argv) /* get data and open window */ -int argc; -char **argv; +static void +init( /* get data and open window */ + int argc, + char **argv +) { XSetWindowAttributes ourwinattr; XClassHint xclshints; @@ -383,18 +413,20 @@ char **argv; } /* end of init */ -quiterr(err) /* print message and exit */ -char *err; +static void +quiterr( /* print message and exit */ + char *err +) { register int es; int cs; - if (es = err != NULL) + if ( (es = err != NULL) ) fprintf(stderr, "%s: %s: %s\n", progname, fname==NULL?"":fname, err); if (thedisplay != NULL) XCloseDisplay(thedisplay); - if (parent < 0 & sigrecv == 0) + if ((parent < 0) & (sigrecv == 0)) kill(getppid(), SIGCONT); while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ if (es == 0) @@ -406,8 +438,10 @@ char *err; static int -viscmp(v1,v2) /* compare visual to see which is better, descending */ -register XVisualInfo *v1, *v2; +viscmp( /* compare visual to see which is better, descending */ + register XVisualInfo *v1, + register XVisualInfo *v2 +) { int bad1 = 0, bad2 = 0; register int *rp; @@ -447,7 +481,8 @@ register XVisualInfo *v1, *v2; } -getbestvis() /* get the best visual for this screen */ +static void +getbestvis(void) /* get the best visual for this screen */ { #ifdef DEBUG static char vistype[][12] = { @@ -499,7 +534,7 @@ static char vistype[][12] = { if (viscmp(&xvi[i],&ourvis) > 0) quiterr("inadequate visuals on this screen"); /* OK, we'll use it */ - copystruct(&ourvis, &xvi[i]); + ourvis = xvi[i]; #ifdef DEBUG fprintf(stderr, "Selected visual type %s, depth %d\n", vistype[ourvis.class], ourvis.depth); @@ -529,10 +564,9 @@ static char vistype[][12] = { } -getras() /* get raster file */ +static void +getras(void) /* get raster file */ { - XVisualInfo vinfo; - if (maxcolors <= 2) { /* monochrome */ ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); if (ourdata == NULL) @@ -542,8 +576,8 @@ getras() /* get raster file */ if (ourras == NULL) goto fail; getmono(); - } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { - int datsiz = ourvis.depth>16 ? sizeof(int4) : sizeof(int2); + } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) { + int datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16); ourdata = (unsigned char *)malloc(datsiz*xmax*ymax); if (ourdata == NULL) goto fail; @@ -573,7 +607,8 @@ fail: } -getevent() /* process the next event */ +static void +getevent(void) /* process the next event */ { XEvent xev; @@ -590,7 +625,7 @@ getevent() /* process the next event */ map_rcolors(ourras, wind); if (fast) make_rpixmap(ourras, wind); - if (!sequential & parent < 0 & sigrecv == 0) { + if ((!sequential) & (parent < 0) & (sigrecv == 0)) { kill(getppid(), SIGCONT); sigrecv--; } @@ -628,17 +663,19 @@ getevent() /* process the next event */ } -traceray(xpos, ypos) /* print requested pixel data */ -int xpos, ypos; +static int +traceray( /* print requested pixel data */ + int xpos, + int ypos +) { - extern char *index(); - FLOAT hv[2]; + RREAL hv[2]; FVECT rorg, rdir; COLOR cval; register char *cp; - box.xmin = xpos; box.xsiz = 1; - box.ymin = ypos; box.ysiz = 1; + bbox.xmin = xpos; bbox.xsiz = 1; + bbox.ymin = ypos; bbox.ysiz = 1; avgbox(cval); scalecolor(cval, 1./exposure); pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); @@ -672,15 +709,17 @@ int xpos, ypos; } -docom(ekey) /* execute command */ -XKeyPressedEvent *ekey; +static int +docom( /* execute command */ + XKeyPressedEvent *ekey +) { char buf[80]; COLOR cval; XColor cvx; int com, n; double comp; - FLOAT hv[2]; + RREAL hv[2]; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); if (n == 0) @@ -703,7 +742,7 @@ XKeyPressedEvent *ekey; sprintf(buf, "%.3f", intens(cval)/exposure); break; case 'l': /* luminance */ - sprintf(buf, "%.0fL", luminance(cval)/exposure); + sprintf(buf, "%.1fL", luminance(cval)/exposure); break; case 'c': /* color */ comp = pow(2.0, (double)scale); @@ -714,7 +753,7 @@ XKeyPressedEvent *ekey; break; } XDrawImageString(thedisplay, wind, ourgc, - box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); + bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); return(0); case 'i': /* identify (contour) */ if (ourras->pixels == NULL) @@ -776,9 +815,9 @@ XKeyPressedEvent *ekey; sprintf(buf, "%+d", scale); remap: XDrawImageString(thedisplay, wind, ourgc, - box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); + bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); XFlush(thedisplay); - free(ourdata); + /* free(ourdata); This is done in XDestroyImage()! */ free_raster(ourras); getras(); /* fall through */ @@ -800,14 +839,14 @@ XKeyPressedEvent *ekey; free_rpixmap(ourras); return(0); case '0': /* recenter origin */ - if (xoff == 0 & yoff == 0) + 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); + redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz); return(0); default: XBell(thedisplay, 0); @@ -816,8 +855,10 @@ XKeyPressedEvent *ekey; } -moveimage(ebut) /* shift the image */ -XButtonPressedEvent *ebut; +static void +moveimage( /* shift the image */ + XButtonPressedEvent *ebut +) { XEvent e; int mxo, myo; @@ -841,36 +882,40 @@ XButtonPressedEvent *ebut; } -getbox(ebut) /* get new box */ -XButtonPressedEvent *ebut; +static void +getbox( /* get new bbox */ + XButtonPressedEvent *ebut +) { XEvent e; XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); while (e.type == MotionNotify) { - revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); + revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y); XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); - revbox(ebut->x, ebut->y, box.xmin, box.ymin); + revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin); } - box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); - box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); - if (box.xmin > ebut->x) { - box.xsiz = box.xmin - ebut->x + 1; - box.xmin = ebut->x; + bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); + bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); + if (bbox.xmin > ebut->x) { + bbox.xsiz = bbox.xmin - ebut->x + 1; + bbox.xmin = ebut->x; } else { - box.xsiz = ebut->x - box.xmin + 1; + bbox.xsiz = ebut->x - bbox.xmin + 1; } - if (box.ymin > ebut->y) { - box.ysiz = box.ymin - ebut->y + 1; - box.ymin = ebut->y; + if (bbox.ymin > ebut->y) { + bbox.ysiz = bbox.ymin - ebut->y + 1; + bbox.ymin = ebut->y; } else { - box.ysiz = ebut->y - box.ymin + 1; + bbox.ysiz = ebut->y - bbox.ymin + 1; } } -trackrays(ebut) /* trace rays as mouse moves */ -XButtonPressedEvent *ebut; +static void +trackrays( /* trace rays as mouse moves */ + XButtonPressedEvent *ebut +) { XEvent e; unsigned long lastrept; @@ -888,8 +933,13 @@ XButtonPressedEvent *ebut; } -revbox(x0, y0, x1, y1) /* draw box with reversed lines */ -int x0, y0, x1, y1; +static void +revbox( /* draw bbox with reversed lines */ + int x0, + int y0, + int x1, + int y1 +) { revline(x0, y0, x1, y0); revline(x0, y1, x1, y1); @@ -898,30 +948,32 @@ int x0, y0, x1, y1; } -int -colavg(scn, n, cavg) -register COLR *scn; -register int n; -COLOR cavg; +static void +colavg( + register COLR *scn, + register int n, + void *cavg +) { COLOR col; while (n--) { - colr_color(col, scn++); - addcolor(cavg, col); + colr_color(col, *scn++); + addcolor((COLORV*)cavg, col); } } -int -avgbox(cavg) /* average color over current box */ -COLOR cavg; +static int +avgbox( /* average color over current bbox */ + COLOR cavg +) { double d; register int rval; setcolor(cavg, 0., 0., 0.); - rval = dobox(colavg, (char *)cavg); + rval = dobox(colavg, (void *)cavg); if (rval > 0) { d = 1./rval; scalecolor(cavg, d); @@ -930,24 +982,26 @@ COLOR cavg; } -int -dobox(f, p) /* run function over box */ -int (*f)(); /* function to call for each subscan */ -char *p; /* pointer to private data */ +static int +dobox( /* run function over bbox */ + //void (*f)(), /* function to call for each subscan */ + doboxf_t *f, /* function to call for each subscan */ + void *p /* pointer to private data */ +) { int left, right, top, bottom; int y; - left = box.xmin - xoff; - right = left + box.xsiz; + left = bbox.xmin - xoff; + right = left + bbox.xsiz; if (left < 0) left = 0; if (right > xmax) right = xmax; if (left >= right) return(0); - top = box.ymin - yoff; - bottom = top + box.ysiz; + top = bbox.ymin - yoff; + bottom = top + bbox.ysiz; if (top < 0) top = 0; if (bottom > ymax) @@ -963,14 +1017,18 @@ char *p; /* pointer to private data */ } -int -addfix(scn, n) /* add fixation points to histogram */ -COLR *scn; -int n; +static void +addfix( /* add fixation points to histogram */ + COLR *scn, + int n, + void *p +) { - if (tmCvColrs(lscan, TM_NOCHROM, scn, n)) + TMstruct * tms = (TMstruct *)p; + + if (tmCvColrs(tms, lscan, TM_NOCHROM, scn, n)) goto tmerr; - if (tmAddHisto(lscan, n, FIXWEIGHT)) + if (tmAddHisto(tms, lscan, n, FIXWEIGHT)) goto tmerr; return; tmerr: @@ -978,7 +1036,8 @@ tmerr: } -make_tonemap() /* initialize tone mapping */ +static void +make_tonemap(void) /* initialize tone mapping */ { int flags, y; @@ -993,9 +1052,9 @@ make_tonemap() /* initialize tone mapping */ } flags = tmflags; /* histogram adjustment */ if (greyscale) flags |= TM_F_BW; - if (tmTop != NULL) { /* reuse old histogram if one */ - tmDone(tmTop); - tmTop->flags = flags; + if (tmGlobal != NULL) { /* reuse old histogram if one */ + tmDone(tmCurrent); tmCurrent = NULL; + tmGlobal->flags = flags; } else { /* else initialize */ if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) goto memerr; @@ -1007,23 +1066,25 @@ make_tonemap() /* initialize tone mapping */ == NULL) goto memerr; /* initialize tm library */ - if (tmInit(flags, stdprims, gamcor) == NULL) + tmGlobal = tmInit(flags, stdprims, gamcor); + if (tmGlobal == NULL) goto memerr; - if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) + if (tmSetSpace(tmGlobal, stdprims, WHTEFFICACY/exposure)) goto tmerr; /* compute picture histogram */ for (y = 0; y < ymax; y++) { getscan(y); - if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) + if (tmCvColrs(tmGlobal, lscan, TM_NOCHROM, + scanline, xmax)) goto tmerr; - if (tmAddHisto(lscan, xmax, 1)) + if (tmAddHisto(tmGlobal, lscan, xmax, 1)) goto tmerr; } } - tmDup(); /* add fixations to duplicate map */ - dobox(addfix, NULL); + tmCurrent = tmDup(tmGlobal); /* add fixations to duplicate map */ + dobox(addfix, (void *)tmCurrent); /* (re)compute tone mapping */ - if (tmComputeMapping(gamcor, 0., 0.)) + if (tmComputeMapping(tmCurrent, gamcor, 0., 0.)) goto tmerr; return; memerr: @@ -1033,9 +1094,11 @@ tmerr: } -tmap_colrs(scn, len) /* apply tone mapping to scanline */ -register COLR *scn; -int len; +static void +tmap_colrs( /* apply tone mapping to scanline */ + register COLR *scn, + int len +) { register BYTE *ps; @@ -1047,9 +1110,9 @@ int len; } if (len > xmax) quiterr("code error 1 in tmap_colrs"); - if (tmCvColrs(lscan, cscan, scn, len)) + if (tmCvColrs(tmCurrent, lscan, cscan, scn, len)) goto tmerr; - if (tmMapPixels(pscan, lscan, cscan, len)) + if (tmMapPixels(tmCurrent, pscan, lscan, cscan, len)) goto tmerr; ps = pscan; if (greyscale) @@ -1072,7 +1135,8 @@ tmerr: } -getmono() /* get monochrome data */ +static void +getmono(void) /* get monochrome data */ { register unsigned char *dp; register int x, err; @@ -1100,13 +1164,15 @@ getmono() /* get monochrome data */ cerr[x] = err + errp; } } - free((char *)cerr); + free((void *)cerr); } -add2icon(y, scan) /* add a scanline to our icon data */ -int y; -COLR *scan; +static void +add2icon( /* add a scanline to our icon data */ + int y, + COLR *scan +) { static short cerr[ICONSIZ]; static int ynext; @@ -1143,7 +1209,7 @@ COLR *scan; errp = err; ti = x*xmax/iconwidth; copycolr(clr, scan[ti]); - normcolrs(clr, 1, scale); + normcolrs(&clr, 1, scale); err += normbright(clr) + cerr[x]; if (err > 127) err -= 255; @@ -1156,17 +1222,18 @@ COLR *scan; } -getfull() /* get full (24-bit) data */ +static void +getfull(void) /* get full (24-bit) data */ { int y; - register unsigned int4 *dp; - register unsigned int2 *dph; + register uint32 *dp; + register uint16 *dph; register int x; /* initialize tone mapping */ make_tonemap(); /* read and convert file */ - dp = (unsigned int4 *)ourdata; - dph = (unsigned int2 *)ourdata; + dp = (uint32 *)ourdata; + dph = (uint16 *)ourdata; for (y = 0; y < ymax; y++) { getscan(y); add2icon(y, scanline); @@ -1174,15 +1241,15 @@ getfull() /* get full (24-bit) data */ switch (ourras->image->blue_mask) { case 0xff: /* 24-bit RGB */ for (x = 0; x < xmax; x++) - *dp++ = (unsigned int4)scanline[x][RED] << 16 | - (unsigned int4)scanline[x][GRN] << 8 | - (unsigned int4)scanline[x][BLU] ; + *dp++ = (uint32)scanline[x][RED] << 16 | + (uint32)scanline[x][GRN] << 8 | + (uint32)scanline[x][BLU] ; break; case 0xff0000: /* 24-bit BGR */ for (x = 0; x < xmax; x++) - *dp++ = (unsigned int4)scanline[x][RED] | - (unsigned int4)scanline[x][GRN] << 8 | - (unsigned int4)scanline[x][BLU] << 16 ; + *dp++ = (uint32)scanline[x][RED] | + (uint32)scanline[x][GRN] << 8 | + (uint32)scanline[x][BLU] << 16 ; break; #if 0 case 0x1f: /* 15-bit RGB */ @@ -1223,7 +1290,8 @@ getfull() /* get full (24-bit) data */ } -getgrey() /* get greyscale data */ +static void +getgrey(void) /* get greyscale data */ { int y; register unsigned char *dp; @@ -1238,7 +1306,7 @@ getgrey() /* get greyscale data */ tmap_colrs(scanline, xmax); if (maxcolors < 256) for (x = 0; x < xmax; x++) - *dp++ = ((int4)scanline[x][GRN] * + *dp++ = ((int32)scanline[x][GRN] * maxcolors + maxcolors/2) >> 8; else for (x = 0; x < xmax; x++) @@ -1246,11 +1314,12 @@ getgrey() /* get greyscale data */ } for (x = 0; x < maxcolors; x++) clrtab[x][RED] = clrtab[x][GRN] = - clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; + clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors; } -getmapped() /* get color-mapped data */ +static void +getmapped(void) /* get color-mapped data */ { int y; /* make sure we can do it first */ @@ -1259,7 +1328,7 @@ getmapped() /* get color-mapped data */ /* initialize tone mapping */ make_tonemap(); /* make histogram */ - if (new_histo((int4)xmax*ymax) == -1) + if (new_histo((int32)xmax*ymax) == -1) quiterr("cannot initialize histogram"); for (y = 0; y < ymax; y++) { if (getscan(y) < 0) @@ -1282,9 +1351,11 @@ getmapped() /* get color-mapped data */ } -scale_rcolors(xr, sf) /* scale color map */ -register XRASTER *xr; -double sf; +static void +scale_rcolors( /* scale color map */ + register XRASTER *xr, + double sf +) { register int i; long maxv; @@ -1310,13 +1381,15 @@ double sf; } -getscan(y) -int y; +static int +getscan( + int y +) { static int trunced = -1; /* truncated file? */ skipit: if (trunced >= 0 && y >= trunced) { - bzero(scanline, xmax*sizeof(COLR)); + memset(scanline, '\0', xmax*sizeof(COLR)); return(-1); } if (y != cury) {