📄 viz2d-f77.c
字号:
/* * viz2d-f77.c Revision 1.2 (8-30-91) * by Jeff 'Lockheed' Sheffel * * viz2d-f77 is a package of functions written in C with a Fortran * interface. This package of functions allows a Fortrash programmer * to display 8-bit bitmap data in an X11 window. The programmer * interface is via three simple functions: * * call init_viz2d( width, height, colormap_type, colormap_size, * display_key ) * * The first two parameters are 'width' of the data and 'height' * of the data. 'width' and 'height' will be the width and * height of the window and the data. The 'colormap-type' is an * integer code with 1=gray-scale, 2=blue-to-red, and * 3=blue-to-green-to-red. The 'colormap_size' is a request for * the number of colors to display the data. If the * 'display_key' parameter is non-zero, then a gradient of the * colors representing the data values from 0 to 255 is * included at the bottom of the window. * * init_viz2d() returns 0 upon failure and 1 upon success. Failure * may result upon failure to connect to the server (inwhich case * the user is told) or failure to open a window or failure to * create a colormap (which must be of type PseudoColor - this * could be modified). * * call viz2d( buffer ) * * This subroutine displays the bitmap data in the window that * was created by init_viz2d(). The argument 'buffer' is a * pointer to an 8-bit (character) array of data. The width and * height of the buffer must match the width and height as * specified in the call to init_viz2d(). * * call quit_viz2d * * This subroutine is called when the program is finished * with the viz2d window. It removes the window and frees * the memory that was allocated by viz2d. quit_viz2d() may * be called even if init_viz2d() failed. * * viz2d USAGE: * * init_viz2d() must be called before the displaying routine viz2d() * can be called. Any 'colormap-size' between 2 and 256 may specified. * Since the 8-bit data can only take on values between 0 and 256, more * than 256 colors would not be useful. If 256 is selected and a * virtual colormap of that size is supported by the server, then all * possible values of the data will be represented by a unique color. * If fewer than 256 is selected or a virtual colormap of less than * 256 colors is supported, then in the viz2d() routine, all of * the currently passed data in the buffer is scaled (squeezed) such * that each color represents more than one data value. The init_viz2d() * routine will ensure that the most colors will be displayed if too * many colors were selected and can not be supported. In such cases of * filling an entire colormap, the server's default colormap will be * temporarily over-written and normal screen colors will temporarily * be lost (but controlled by mouse cursor movement). * * To avoid core dumps, do not call viz2d() unless viz2d_init() returns * a non-zero value (1). For example, * * if( init_viz2d( 200, 200, 3, 128, 1 ) ) then * do while( keep_displaying_data ) * call calculate_data( buffer ) * call viz2d( buffer ) * end do * endif * call quit_viz2d * * viz2d COMPILATION: * * After including viz2d routines in a Fortrash program (my_viz2d.f), * then compile with: * * f77 my_viz2d.f viz2d-f77.c -lX11 -lm * */#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Xatom.h>#include <stdio.h>#define BORDER_WIDTH 5#define MAX_COLORS_TO_COPY 32 /* # colors to copy from default colormap, */ /* # colors may be changed */#define MAX_INTENSITY 65535 /* X Color RGB max intensity (2**16) */#define KEY_HEIGHT 20 /* Height of key (color ramp) */#define TRUE 1#define FALSE 0Display *theDisplay; /* for eg. "unix:0.0" */int theScreen; /* Which screen on the display */int theDepth; /* Number of color planes */unsigned long theBlackPixel;unsigned long theWhitePixel;GC theGC; /* An X graphics context */Visual *aVisual;int colormapsize;int colors_created; /* # colors in colormap for viz2d */XImage *anImage; /* The viz2d image */XImage *keyImage = NULL; /* The optional bottom color key */Window aWindow; /* The viz2d window */int width, height; /* The viz2d window width & height */int mapstart; /* Viz2d colormap entries start */char *trans_buffer = NULL; /* Temp. buffer used in viz2d */char *key_buffer = NULL; /* Data buffer used for keyImage */Colormap aColormap; /* The created viz2d colormap */XColor *colorcell_defs = NULL; /* Viz2d colormap definitions */int colormap_created = FALSE;int viz2d_was_initialized = FALSE;#define icon_viz50x50_width 50#define icon_viz50x50_height 50static char icon_viz50x50_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x20, 0x03, 0x00, 0x00, 0x00, 0x00, 0x48, 0x25, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x08, 0x20, 0xc0, 0x03, 0x00, 0x00, 0x00, 0xa8, 0x2a, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x08, 0x20, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x48, 0x25, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x08, 0x20, 0x00, 0x00, 0xc0, 0x01, 0x00, 0xa8, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x20, 0xc0, 0xff, 0xff, 0x0f, 0x00, 0x48, 0x25, 0x40, 0x40, 0x00, 0x0c, 0x00, 0x08, 0x20, 0x40, 0xe0, 0x00, 0x0e, 0x00, 0xa8, 0x2a, 0x40, 0xf0, 0x01, 0x0f, 0x00, 0x08, 0x20, 0x40, 0xf8, 0x83, 0x0f, 0x00, 0x08, 0x20, 0x40, 0xfc, 0xc7, 0x0f, 0x00, 0xf8, 0x3f, 0xc0, 0xfe, 0xef, 0x0f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x0f, 0x00, 0x10, 0x00, 0xc0, 0xfe, 0xef, 0x0f, 0x00, 0x20, 0x00, 0x40, 0xfc, 0xc7, 0x0f, 0x00, 0x20, 0x00, 0x40, 0xf8, 0x83, 0x0f, 0x00, 0x40, 0x00, 0x40, 0xf0, 0x01, 0x0f, 0x00, 0x80, 0x00, 0x40, 0xe0, 0x00, 0x0e, 0x00, 0x00, 0x01, 0x40, 0x40, 0x00, 0x0c, 0x00, 0x00, 0x01, 0x40, 0xe0, 0x00, 0x0e, 0x00, 0x00, 0x02, 0x40, 0xf0, 0x01, 0x0f, 0x00, 0x00, 0x04, 0x40, 0xf8, 0x83, 0x0f, 0x00, 0x00, 0x08, 0x40, 0xfc, 0xc7, 0x0f, 0x00, 0x00, 0x08, 0xc0, 0xfe, 0xef, 0x0f, 0x00, 0x00, 0x10, 0xc0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x20, 0xc0, 0xfe, 0xef, 0x0f, 0x00, 0x00, 0x20, 0x40, 0xfc, 0xc7, 0x0f, 0x00, 0x00, 0x40, 0x40, 0xf8, 0x83, 0x0f, 0x00, 0x00, 0x80, 0x40, 0xf0, 0x01, 0x0f, 0x00, 0x00, 0x00, 0x41, 0xe0, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x41, 0x40, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x42, 0xe0, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x44, 0xf0, 0x01, 0x0e, 0x00, 0x00, 0x00, 0x48, 0xf8, 0x03, 0x0f, 0x00, 0x00, 0x00, 0x48, 0xfc, 0x87, 0x0f, 0x00, 0x00, 0x00, 0xd0, 0xfe, 0xcf, 0x0f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};/* * viz2d() * * This subroutine displays the bitmap data in the window that * was created by init_viz2d(). The argument 'buffer' is a * pointer to an 8-bit (character) array of data. The width and * height of the buffer must match the width and height as * specified in the call to init_viz2d(). * * If a 256 color colormap is used, then viz2d() simply blasts the * image to the window. But if less than 256 colors are used, then * the data is contracted down into 'trans_buffer'. This will * obviously create minute errors in the image representation. */viz2d_( buffer )char *buffer;{int iii, ival;float scaledown, fval; if( colors_created < colormapsize ){ /* Must contract the data since less than 256 colors */ scaledown = 256.0/(float)colors_created; for(iii=0; iii<width*height; iii++){ trans_buffer[iii] = (unsigned char)( (((unsigned char)buffer[iii])/scaledown) + mapstart ); } anImage = XCreateImage( theDisplay, aVisual, theDepth, ZPixmap, 0, trans_buffer, width, height, 8, 0 ); }else{ /* No need to use the translation buffer */ anImage = XCreateImage( theDisplay, aVisual, theDepth, ZPixmap, 0, buffer, width, height, 8, 0 ); } XPutImage( theDisplay, aWindow, theGC, anImage, 0,0,0,0, width,height ); XDestroyImage( anImage ); XFlush( theDisplay );}/* * init_viz2d() * * The first two parameters are 'width' of the data and 'height' * of the data. 'width' and 'height' will be the width and * height of the window and the data. The 'colormap-type' is an * integer code with 1=gray-scale, 2=blue-to-red, and * 3=blue-to-green-to-red. The 'colormap-size' is a request for * the number of colors to display the data. If 'display_key' is * non-zero, then a color key representing the data values 0 to 255 * will be displayed at the bottom of the viz2d window. * * init_viz2d() will first try to allocate (write) entries from * the (server's) default colormap. If this is successful, then the * server doesn't have to swap our colormap in and out and the viz2d * window will not go black when the cursor is not in our window. * * init_viz2d() returns 0 upon failure and 1 upon success. Failure * may result upon failure to connect to the server (inwhich case * the user is told) or failure to open a window or failure to * create a colormap (which must be of type PseudoColor - this could * be modified). * * POTENTIAL MODIFICATIONS: * 1) Don't fail if can't create a colormap of type PseudoColor, * 2) Add or modify different colormaps (this may be desired change * by joe-blow programmer if he is not satisfied with my * colormaps). * 3) Read in colormaps (pallets) created by the ximage program. */int init_viz2d_(request_width, request_height, colormap_type, request_colors, display_key)int *request_width, *request_height, *colormap_type, *request_colors, *display_key;{Window openWindow();Colormap theDefaultColormap;XSetWindowAttributes attrib;int nitems, iii, jjj;XVisualInfo *vlist, vinfo_template;Visual *theDefaultVisual;int colors_to_copy; /* # colors to copy from default colormap */int delta; /* Step size in our colormap definitions */int midmap; /* Midpoint of our colormap */int defstart; /* Start index into colormap definitions */int colors_to_store; /* # colors to write to colormap */long vinfo_mask; /* Visual information mask */char an8bitFlag;XColor *theDefColorDefs;unsigned long *plane_masks; /* Plane masks for XAllocColorCells, not used?*/unsigned int nplanes; /* # planes for XAllocColorCells, not used?*/unsigned long *pixels; /* Returned pixel values from XAllocColorCells*/unsigned int ncolors; /* # of requested colors in XAllocColorCells */ if( !initX() ) return 0; /* Create the transformation buffer (oversized) */ width = *request_width; height = *request_height; trans_buffer = (char *)calloc(sizeof(char),width*height+width+height); /* Create a window of the requested data size */ if( *display_key ){ /* but a slightly larger window if a colorkey is requested */ aWindow = openWindow( 200, 200, width, height+KEY_HEIGHT+3, 0, &theGC ); /* 0 = NOT popup */ key_buffer = (char *)calloc(sizeof(char),width*(KEY_HEIGHT+1)); }else{ aWindow = openWindow( 200, 200, width, height, 0, &theGC ); /* 0 = NOT popup */ } XSetIconName( theDisplay, aWindow, "2D fd-td" ); XStoreName( theDisplay, aWindow, "2D fd-td" ); sleep(1); /* Must wait or graphics won't work?? */ theDefaultVisual = DefaultVisual( theDisplay, theScreen ); theDefaultColormap = DefaultColormap(theDisplay, theScreen); /* 1st try to allocate colors from server's default colormap */ ncolors = *request_colors;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -