/* VERY Simple X interface J. Watlington, 9/13/95 - 10/25/95 Significant parts from xline.c, Neil Gershenfeld 9/11/93 Also included some of xng.c, Neil Gershenfeld v 1.6 1993/09/01 Restructured to contain the following interface : window_tag display_init_window( name, x_size, y_size, num_levels ) display_update( window_tag ) display_set_pixel( window_tag, x, y, raw_pixel_value ) display_set_scale( window_tag, min, max ) display_set_spixel( window_tag, x, y, scaled_value ) display_set_fpixel( window_tag, x, y, scaled_value_float ) */ #include #include #include "display.h" #define MAX_CLUT_VALUE 49152 #define MIN_CLUT_VALUE 16384 #define MIN_WINDOW_SIZE 64 window_desc window_desc_table[DISPLAY_MAX_WINDOWS]; int num_windows_open = 0; /* The following are declared globally, since they are needed for several of the functions. */ static Display *BigDisplay; static int BigScreen; window_tag display_init_window( char *name, int xsize, int ysize, int num_levels ) { static int first_time = 1; Visual *V; Colormap Cmap; XColor Colors[DISPLAY_MAX_COLORS]; XSetWindowAttributes Attr; XVisualInfo Info; window_desc *wd; window_tag wt; int i; if( num_levels > DISPLAY_MAX_COLORS ) { printf("display_init_window: Max num levels is %d\n", DISPLAY_MAX_COLORS ); return( -1 ); } if( (xsize < MIN_WINDOW_SIZE) || (ysize < MIN_WINDOW_SIZE) ) { printf("display_init_window: Size too small for X !\n" ); return( -1 ); } /* assign a window_tag, and find the window descriptor */ wt = num_windows_open++; if( wt > DISPLAY_MAX_WINDOWS ) { printf( "display_window_init: Too many windows open ! (%d max)\n", DISPLAY_MAX_WINDOWS ); return( -1 ); } wd = window_desc_table + wt; /* Some X stuff is only done for the first window that an application opens. */ if( first_time ) { BigDisplay = XOpenDisplay(""); BigScreen = DefaultScreen( BigDisplay ); first_time = 0; } /* Now do the window specific X initialization. */ if( XMatchVisualInfo( BigDisplay, BigScreen, DISPLAY_DEPTH, PseudoColor, &Info) == 0) { printf("Display can not handle %d bit pseudo color for the gray scale\n", DISPLAY_DEPTH); return( -1 ); } V = Info.visual; /* Now build the color table */ Colors[0].pixel = 0; Colors[0].red = 0; Colors[0].blue = 0; Colors[0].green = 0; Colors[0].flags = DoRed | DoGreen | DoBlue; for( i = 1; i < num_levels; ++i) { Colors[i].pixel = i; Colors[i].red = 0; Colors[i].green = (int)(((MAX_CLUT_VALUE*i)/num_levels) + MIN_CLUT_VALUE ); Colors[i].blue = (int)(((MAX_CLUT_VALUE*i)/num_levels) + MIN_CLUT_VALUE ); Colors[i].flags = DoRed | DoGreen | DoBlue; } Cmap = XCreateColormap( BigDisplay, RootWindow( BigDisplay, BigScreen), V, AllocAll ); XStoreColors( BigDisplay, Cmap, Colors, num_levels ); Attr.colormap = Cmap; Attr.background_pixel = WhitePixel( BigDisplay, BigScreen ); Attr.border_pixel = BlackPixel( BigDisplay, BigScreen ); wd->W = XCreateWindow( BigDisplay, DefaultRootWindow( BigDisplay ), 0, 0, xsize, ysize, 1, DISPLAY_DEPTH, InputOutput, V, CWColormap | CWBackPixel | CWBorderPixel, &Attr ); XStoreName( BigDisplay, wd->W, name ); XMapRaised( BigDisplay, wd->W ); wd->Gc = XCreateGC( BigDisplay, wd->W, 0L, (XGCValues *)0 ); /* Allocate an image buffer and assign it to the window */ if( (wd->image = (unsigned char *)malloc( xsize * ysize )) == NULL ) { printf("display_init_window: Unable to alloc %d bytes of mem for image\n", xsize * ysize ); return( -1 ); } wd->I = XCreateImage( BigDisplay, V, DISPLAY_DEPTH, ZPixmap, 0, wd->image, xsize, ysize, DISPLAY_DEPTH, 0 ); wd->x_size = xsize; wd->y_size = ysize; wd->num_levels = num_levels; wd->min = 0; wd->delta = 1; return( wt ); } void display_update( window_tag id ) { window_desc *wd = window_desc_table + id; XPutImage( BigDisplay, wd->W, wd->Gc, wd->I, 0, 0, 0, 0, wd->x_size, wd->x_size ); }