--- ray/src/px/x11image.c 1998/10/27 09:08:28 2.53 +++ ray/src/px/x11image.c 2003/07/07 17:21:51 2.62 @@ -1,9 +1,6 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id"; #endif - /* * x11image.c - driver for X-windows * @@ -21,20 +18,18 @@ static char SCCSid[] = "$SunId$ SGI"; #include "standard.h" +#include #include #include #include #include #include -#undef NOPROTO -#define NOPROTO 1 #include "color.h" #include "tonemap.h" #include "view.h" #include "x11raster.h" #include "random.h" -#include "resolu.h" #define FONTNAME "8x13" /* text font we'll use */ @@ -91,7 +86,7 @@ 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 */ +FILE *fin = NULL; /* input file */ long *scanpos = NULL; /* scan line positions in file */ int cury = 0; /* current scan location */ @@ -109,7 +104,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 */ @@ -131,7 +126,7 @@ Atom closedownAtom, wmProtocolsAtom; int sigrecv; -int onsig() { sigrecv++; } +void onsig(int i) { sigrecv++; } main(argc, argv) @@ -143,6 +138,7 @@ char *argv[]; int pid; progname = argv[0]; + fin = stdin; for (i = 1; i < argc; i++) if (argv[i][0] == '-') @@ -418,19 +414,20 @@ register XVisualInfo *v1, *v2; return(-1); if (v1->depth == 32 && v2->depth == 24) return(1); - return(0); + /* go for maximum depth otherwise */ + return(v2->depth - v1->depth); } /* 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 */ + /* prefer Pseudo when < 15-bit */ if ((v1->class == TrueColor || v1->class == DirectColor) && - v1->depth < 24) + v1->depth < 15) bad1 = 1; if ((v2->class == TrueColor || v2->class == DirectColor) && - v2->depth < 24) + v2->depth < 15) bad2 = -1; if (bad1 | bad2) return(bad1+bad2); @@ -541,11 +538,12 @@ getras() /* get raster file */ goto fail; getmono(); } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { - ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); + int datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16); + ourdata = (unsigned char *)malloc(datsiz*xmax*ymax); if (ourdata == NULL) goto fail; - ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, - ourdata, xmax, ymax, 32); + ourras = make_raster(thedisplay, &ourvis, datsiz*8, + ourdata, xmax, ymax, datsiz*8); if (ourras == NULL) goto fail; getfull(); @@ -628,14 +626,13 @@ getevent() /* process the next event */ traceray(xpos, ypos) /* print requested pixel data */ int xpos, 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); @@ -677,7 +674,7 @@ XKeyPressedEvent *ekey; XColor cvx; int com, n; double comp; - FLOAT hv[2]; + RREAL hv[2]; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); if (n == 0) @@ -700,7 +697,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); @@ -711,7 +708,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) @@ -773,9 +770,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 */ @@ -804,7 +801,7 @@ XKeyPressedEvent *ekey; 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); @@ -838,30 +835,30 @@ XButtonPressedEvent *ebut; } -getbox(ebut) /* get new box */ +getbox(ebut) /* 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; } } @@ -885,7 +882,7 @@ XButtonPressedEvent *ebut; } -revbox(x0, y0, x1, y1) /* draw box with reversed lines */ +revbox(x0, y0, x1, y1) /* draw bbox with reversed lines */ int x0, y0, x1, y1; { revline(x0, y0, x1, y0); @@ -895,7 +892,7 @@ int x0, y0, x1, y1; } -int +void colavg(scn, n, cavg) register COLR *scn; register int n; @@ -904,14 +901,14 @@ COLOR cavg; COLOR col; while (n--) { - colr_color(col, scn++); + colr_color(col, *scn++); addcolor(cavg, col); } } int -avgbox(cavg) /* average color over current box */ +avgbox(cavg) /* average color over current bbox */ COLOR cavg; { double d; @@ -928,23 +925,23 @@ COLOR cavg; int -dobox(f, p) /* run function over box */ -int (*f)(); /* function to call for each subscan */ +dobox(f, p) /* run function over bbox */ +void (*f)(); /* function to call for each subscan */ char *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) @@ -960,7 +957,7 @@ char *p; /* pointer to private data */ } -int +void addfix(scn, n) /* add fixation points to histogram */ COLR *scn; int n; @@ -1097,7 +1094,7 @@ getmono() /* get monochrome data */ cerr[x] = err + errp; } } - free((char *)cerr); + free((void *)cerr); } @@ -1140,7 +1137,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,26 +1153,66 @@ COLR *scan; getfull() /* get full (24-bit) data */ { int y; - register unsigned int4 *dp; + register uint32 *dp; + register uint16 *dph; register int x; /* initialize tone mapping */ make_tonemap(); /* read and convert file */ - dp = (unsigned int4 *)ourdata; + dp = (uint32 *)ourdata; + dph = (uint16 *)ourdata; for (y = 0; y < ymax; y++) { getscan(y); add2icon(y, scanline); tmap_colrs(scanline, xmax); - if (ourras->image->blue_mask & 1) + 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] ; - else + *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 */ + for (x = 0; x < xmax; x++) + *dph++ = (scanline[x][RED] << 7 & 0x7c00) | + (scanline[x][GRN] << 2 & 0x3e0) | + (unsigned)scanline[x][BLU] >> 3 ; + break; + case 0x7c00: /* 15-bit BGR */ + for (x = 0; x < xmax; x++) + *dph++ = (unsigned)scanline[x][RED] >> 3 | + (scanline[x][GRN] << 2 & 0x3e0) | + (scanline[x][BLU] << 7 & 0x7c00) ; + break; +#endif + default: /* unknown */ + if (ourvis.depth > 16) + for (x = 0; x < xmax; x++) { + *dp = ourras->image->red_mask & + ourras->image->red_mask*scanline[x][RED]/255; + *dp |= ourras->image->green_mask & + ourras->image->green_mask*scanline[x][GRN]/255; + *dp++ |= ourras->image->blue_mask & + ourras->image->blue_mask*scanline[x][BLU]/255; + } + else + for (x = 0; x < xmax; x++) { + *dph = ourras->image->red_mask & + ourras->image->red_mask*scanline[x][RED]/255; + *dph |= ourras->image->green_mask & + ourras->image->green_mask*scanline[x][GRN]/255; + *dph++ |= ourras->image->blue_mask & + ourras->image->blue_mask*scanline[x][BLU]/255; + } + break; + } } } @@ -1195,7 +1232,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++) @@ -1203,7 +1240,7 @@ 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; } @@ -1216,7 +1253,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) @@ -1273,7 +1310,7 @@ 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) {