home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
vis-ftp.cs.umass.edu
/
vis-ftp.cs.umass.edu.tar
/
vis-ftp.cs.umass.edu
/
pub
/
Software
/
universal_plane_file_format
/
XShowPlane.c
< prev
Wrap
C/C++ Source or Header
|
1992-01-29
|
11KB
|
400 lines
/*
* ------------------------------------------------------------------
* XShowPlane.c - C code for displaying an LLVS plane.
* Created by Robert Heller on Fri Sep 13 13:01:17 1991
* ------------------------------------------------------------------
* Modification History:
* ------------------------------------------------------------------
* Contents:
* OpenXDisplay Opens the X display
* CreateDisplayWindow Create an X window for displaying images
* and other stuff on. Creates both an X
* window and a background pixmap for it.
* ShowPlane Display an LLVS image on a X drawable
* (drawable == window or pixmap)
*
* ------------------------------------------------------------------
*
* Copyright (c) 1991 by The University of Massachusetts
* All Rights Reserved
*
*/
#include <stdio.h>
#include <malloc.h>
#include <X11/Xlib.h>
#include <llvs_per_plane.h>
#include <displaystructs.h>
/*
* OpenXDisplay() - open a connection to the X display server
* returns a pointer to a Display object.
*
* Arguments: none. (Uses DISPLAY environment variable)
*
* Returns: Display pointer (from XOpenDisplay()).
*/
Display *OpenXDisplay()
{
char *getenv();
return(XOpenDisplay(getenv("DISPLAY")));
}
/*
* Create an X window and a pixmap for its background (binding the
* pixmap as its background).
*
* Arguments: disp - Display* from OpenXDisplay(),
* width, height - ints - size of display
* title - char* - window banner string
*/
WindowAndPixmap CreateDisplayWindow(disp,width,height,title)
Display *disp;
int width, height;
char *title;
{
WindowAndPixmap result;
Window root;
XWindowAttributes windattrs;
XSetWindowAttributes setwindattrs;
int screen_id;
Visual visual;
result = (WindowAndPixmap) malloc(sizeof(windowAndPixmap));
if (result == NULL) return(NULL);
screen_id = DefaultScreen(disp);
visual.visualid = CopyFromParent;
setwindattrs.backing_store = Always;
setwindattrs.event_mask = ButtonPressMask;
setwindattrs.bit_gravity = NorthWestGravity;
setwindattrs.win_gravity = NorthWestGravity;
result->window = XCreateWindow(disp,RootWindow(disp,screen_id),
300,200,width,height, 0,
8, InputOutput,
&visual,
CWBitGravity|CWWinGravity|
CWBackingStore|CWEventMask, &setwindattrs);
XStoreName(disp,result->window,title);
result->background = XCreatePixmap(disp,result->window,width,height,
8);
XSetWindowBackgroundPixmap(disp,result->window,result->background);
XGetWindowAttributes(disp,result->window,&windattrs);
result->colormap = windattrs.colormap;
XMapRaised(disp,result->window);
XSync(disp,False);
return(result);
}
get_drawable_size(disp,drawable,width,height,depth)
Display *disp;
Drawable drawable;
unsigned int *width, *height, *depth;
{
Window root_return;
int x_return, y_return;
unsigned int bw_return;
return(XGetGeometry(disp,drawable,&root_return,&x_return,&y_return,
width,height,&bw_return,depth));
}
ShowPlane(disp,drawable,plane,plane_info,limits,
autoscale,absolute,negative,minval,maxval,number_grey,colormap)
Display *disp;
Drawable drawable;
PLANE *plane;
PLANE_INFO *plane_info;
LIMITS *limits;
int autoscale,negative,absolute,number_grey;
double minval,maxval;
Colormap colormap;
{
register XImage *ximage;
Screen *screen;
register unsigned char *imdata;
int i, ipixel;
int runlevel;
int deltapllevel;
int roff,coff;
int xim_y_coord, xim_x_coord;
int crow,ccol;
int byte, ipixrow, ipixcol;
double intensity_scale,value_range,pixel,scaled_pixel,fback;
unsigned int dw_width, dw_height, dw_depth;
unsigned int im_width, im_height;
int pixwidth, pixheight;
XColor color;
unsigned long int *pixmap;
double grey_level;
GC gc;
if (autoscale) plane_min_max(plane,plane_info,limits,&minval,&maxval);
runlevel = limits->level;
if (plane_info->datatype == FLOAT) fback = plane_info->background.flonum;
else fback = plane_info->background.fixnum;
deltapllevel = runlevel - plane_info->level;
value_range = maxval - minval;
intensity_scale = ((double) (number_grey - 1)) / value_range;
get_drawable_size(disp,drawable,&dw_width,&dw_height,&dw_depth);
if (dw_depth != 8) {
printf("Display is not an 8-bit display!\n");
return(0);
}
{int absecol, absscol, abserow, abssrow;
int absdrow, absdcol;
if (limits->deltacol < 0) absdcol = -limits->deltacol;
else absdcol = limits->deltacol;
if (limits->deltarow < 0) absdrow = -limits->deltarow;
else absdrow = limits->deltarow;
TRANSLEVEL(absecol,limits->endcol,deltapllevel,
plane_info->column_location);
TRANSLEVEL(absscol,limits->startcol,deltapllevel,
plane_info->column_location);
TRANSLEVEL(abserow,limits->endrow,deltapllevel,
plane_info->row_location);
TRANSLEVEL(abssrow,limits->startrow,deltapllevel,
plane_info->row_location);
im_width = (absecol - absscol) / absdcol;
im_height = (abserow - abssrow) / absdrow;
}
if (im_width > dw_width || im_height > dw_height) {
printf("Display window too small for image!\n");
return(0);
}
pixmap = (unsigned long int *) calloc(number_grey,
sizeof(unsigned long int));
if (pixmap == NULL) {
printf("Couldn't allocate pixmap\n");
return(0);
}
for (ipixel = 0; ipixel < number_grey; ipixel++) {
grey_level = (ipixel / ((double) number_grey)) * WHITE_VALUE;
color.blue = color.green = color.red = grey_level;
color.flags = DoRed | DoGreen | DoBlue;
XAllocColor(disp,colormap,&color);
pixmap[ipixel] = color.pixel;
}
pixwidth = dw_width / im_width;
pixheight = dw_height / im_height;
imdata = (unsigned char *) calloc(dw_width * dw_height,
sizeof(unsigned char));
if (imdata == NULL) {
printf("Acclocation failure!\n");
free(pixmap);
return(0);
}
screen = XDefaultScreenOfDisplay(disp);
ximage = XCreateImage(disp,XDefaultVisualOfScreen(screen),dw_depth,
ZPixmap,0,imdata,dw_width,dw_height,8,0);
for (crow = limits->startrow;
crow <= limits->endrow;
crow += limits->deltarow) {
TRANSLEVEL(roff,crow,deltapllevel,plane_info->row_location);
xim_y_coord = (crow - limits->startrow) * pixheight;
for (ccol = limits->startcol;
ccol <= limits->endcol;
ccol += limits->deltacol) {
TRANSLEVEL(coff,ccol,deltapllevel,plane_info->column_location);
xim_x_coord = (ccol - limits->startcol) * pixwidth;
GET_PIXEL(pixel,fback,plane,roff,coff,
(*plane_info));
if (absolute) pixel = fabs(pixel);
/* scale pixel. out of range values get black or white.
otherwise linear mapping */
if (intensity_scale > 0.0) {
/* positive slope case */
if (pixel < minval)
scaled_pixel = 0.0;
else
if (pixel > maxval)
scaled_pixel = number_grey-1;
else
scaled_pixel =(pixel - minval) *
intensity_scale;
} else { /* negative slope case */
if (pixel < maxval)
scaled_pixel = number_grey-1;
else
if (pixel > minval)
scaled_pixel = 0.0;
else
scaled_pixel =(pixel - minval) *
intensity_scale;
}
byte = scaled_pixel + 0.5;
if (negative) byte = (number_grey-1) - byte;
for (ipixrow = 0; ipixrow < pixheight; ipixrow++) {
for (ipixcol = 0; ipixcol < pixwidth; ipixcol++) {
XPutPixel(ximage,xim_x_coord+ipixcol,
xim_y_coord+ipixrow,pixmap[byte]);
}
}
}
}
gc = XCreateGC(disp,drawable,0L,NULL);
XSetState(disp, gc, 0L, 255L, GXcopy, AllPlanes);
XSetLineAttributes(disp,gc,2,
LineSolid,CapNotLast,JoinMiter);
XPutImage(disp,drawable,gc,ximage,0,0,0,0,dw_width,dw_height);
XSync(disp,False);
free(pixmap);
/* XDestroyImage(ximage); */
/* XFreeGC(gc); */
/* free(imdata); */
return(1);
}
static plane_min_max(plane,plane_info,limits,minval,maxval)
PLANE *plane;
PLANE_INFO *plane_info;
LIMITS *limits;
double *minval, *maxval;
{
int runlevel;
int deltapllevel;
int roff,coff;
int crow,ccol;
int flag;
double pixel,fback;
runlevel = limits->level;
if (plane_info->datatype == FLOAT) fback = plane_info->background.flonum;
else fback = plane_info->background.fixnum;
deltapllevel = runlevel - plane_info->level;
flag = TRUE;
for (crow = limits->startrow;
crow >= limits->endrow;
crow += limits->deltarow) {
TRANSLEVEL(roff,crow,deltapllevel,plane_info->row_location);
for (ccol = limits->startcol;
ccol <= limits->endcol;
ccol += limits->deltacol) {
TRANSLEVEL(coff,ccol,deltapllevel,plane_info->column_location);
GET_PIXEL(pixel,fback,plane,roff,coff,
(*plane_info));
if (flag) {
*minval = pixel;
*maxval = pixel;
flag = FALSE;
} else if (pixel > *maxval) *maxval = pixel;
else if (pixel < *minval) *minval = pixel;
}
}
}
#ifdef TESTING
main(argc,argv)
int argc;
char *argv[];
{
LIMITS pl_lims;
PLANE *inpl;
PLANE_INFO *inpl_info;
char *assoc;
char *infile;
register int iargc;
Display *disp;
WindowAndPixmap w_p, CreateDisplayWindow();
XEvent event;
infile = NULL;
for (iargc = 1; iargc < argc; iargc++) {
if (*argv[iargc] != '-') {
if (infile != NULL) usage();
infile = argv[iargc];
}
}
if (infile == NULL) usage();
if (read_plane(&inpl,&inpl_info,&assoc,infile) < 0) {
perror(argv[0]);
exit(12);
}
pl_lims.level = inpl_info->level;
pl_lims.deltarow = 1;
pl_lims.deltacol = 1;
pl_lims.startrow = inpl_info->row_location;
pl_lims.startcol = inpl_info->column_location;
pl_lims.endrow = inpl_info->row_location + inpl_info->row_dimension - 1;
pl_lims.endcol = inpl_info->column_location + inpl_info->column_dimension - 1;
for (iargc = 1; iargc < argc; iargc++) {
if (*argv[iargc] != '-') continue;
if (strncmp(argv[iargc],"-start-row=",11) == 0)
pl_lims.startrow = atoi(argv[iargc]+11);
else if (strncmp(argv[iargc],"-start-col=",11) == 0)
pl_lims.startcol = atoi(argv[iargc]+11);
else if (strncmp(argv[iargc],"-end-row=",9) == 0)
pl_lims.endrow = atoi(argv[iargc]+9);
else if (strncmp(argv[iargc],"-end-col=",9) == 0)
pl_lims.endcol = atoi(argv[iargc]+9);
else {
fprintf(stderr,"show_plane: unknown option: %s\n",argv[iargc]);
usage();
}
}
printf("Plane file: %s\nAssoc List: %s\nStart (%d,%d), End (%d,%d)\n\n",
infile,assoc,pl_lims.startrow,pl_lims.startcol,pl_lims.endrow,
pl_lims.endcol);
disp = OpenXDisplay();
if (disp == NULL) {
printf("Could not open display!\n");
exit(1);
}
w_p = CreateDisplayWindow(disp,512,512,"Show Plane");
ShowPlane(disp,w_p->background,inpl,inpl_info,&pl_lims,TRUE,FALSE,FALSE,0.0,0.0,
DEFAULT_NUMBER_GREY,w_p->colormap);
/* XSetWindowBackgroundPixmap(disp,w_p->window,w_p->background); */
XClearWindow(disp,w_p->window);
XSync(disp,False);
printf("Hit any key to stop:");
getchar();
}
static usage()
{
fprintf(stderr,"SYNOPIS: show_plane <opts> plane_file <opts>\n");
fprintf(stderr,"This program displays a LLVS plane on an X Display\n\n");
fprintf(stderr,"Options are:\n");
fprintf(stderr,"-start-row=n\tRow to start printing\n");
fprintf(stderr,"-start-col=n\tColumn to start printing\n");
fprintf(stderr,"-end-row=n\tRow to end printing\n");
fprintf(stderr,"-end-col=n\tColumn to end printing\n");
exit(12);
}
#endif