📄 xwindow.c
字号:
/****************************************************************************
File Name : xwindow.c
Purpose : provides x-window interface routines
Release : Version 1.0
Date : Aug 31,1995
GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang Technological
University (NTU), Singapore.
These software programs are available to the user without any license or royalty fees. Permission is hereby granted to use, copy, modify, and distribute this software and its documentation for any purpose. ITI and NTU gives no warranty, express, implied, or statuary for the software and/or documentation provided, including, without limitation, waranty of merchantibility and warranty of fitness for a particular purpose. The software provided hereunder is on an "as is" basis, and ITI and NTU has no obligation to provide maintenance, support, updates, enhancements, or modifications.
GSNAKE API is available for any UNIX compatible system. User feedback, bugs, or software and manual suggestions should be sent via electronic mail to one of the following, who may or may not act on them as he/she desires :
asschan@ntu.ac.sg
kflai@iti.gov.sg
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "gsnake.h"
/* ---- internal constants and variables used by x window manager ---- */
#define COLORMAPSIZE 256
#define ALLOC_COLOR 1 /* allocate color for snaxel */
#define TITLE "gsnake"
#define VERBOSE 1
#define INCH 72 /* one inch = 72 points */
#define BORDERWIDTH 5 /* border width = 5 pixels */
static int FirstTime = 1 ; /* to raise X window */
static Display *gDisplay=NULL; /* display and others */
static int gScreen;
static Window gWindow;
static Visual *gVisual;
static GC imageGC; /* graphic contexts */
static GC pointGC ;
static GC rectGC;
static unsigned long pixels[COLORMAPSIZE]; /* pixel values */
static int c_map_size; /* colormap size */
static XEvent any_event;
static char *Title = NULL;
/* expand ratio for higher level snaxels */
static int ci_expand = 1;
static int POINT_DIM = 3; /* dimension of point in plotting */
static int POINT_OFF = 0; /* offset in plotting point */
/* ----- internal function prototypes ------ */
int xwin_init(short width, short height, char *title, char alloc_color,
char verbose);
void xwin_close(void);
Display *xwin_setupXWindow(char *DisplayName, char *title, short width,
short height, int *screen, Window *window, Visual **visual,
int verbose);
void xwin_setupGC(unsigned long *pixels, int *c_map_size, char alloc_color);
void xwin_setTitle(char *title)
{
Title = strdup(title);
}
void xwin_raiseWindow(short width, short height)
{
if( FirstTime ) {
xwin_init(width, height, ((Title) ? Title : TITLE), 1, VERBOSE);
FirstTime = 0 ;
}
else
XResizeWindow(gDisplay, gWindow, width, height);
}
/* create an x window of specified dimensions and initialize the graphics
context to display gray level images and drawing snake points and lines */
xwin_init(
short width,
short height, /* size of x window to be raised */
char *title, /* title of the window */
char alloc_color, /* 1 : alloc color for points */
char verbose /* 1 : turns verbose on */
) {
/* create and raise X window */
gDisplay = xwin_setupXWindow((char *) NULL, title, width, height,
&gScreen, &gWindow, &gVisual, verbose) ;
/* create gray level GC */
xwin_setupGC(pixels, &c_map_size, alloc_color);
return (gDisplay) ? 1 : 0 ;
}
/* close the x window */
void xwin_close(void)
{
if(gDisplay) XCloseDisplay(gDisplay) ;
}
/* create a raise a simple X window */
Display *xwin_setupXWindow(
char *DisplayName, /* name of display */
char *title, /* title of window */
short width,
short height, /* width and height of window */
int *screen, /* RETURN : default screen */
Window *window, /* RETURN : created window */
Visual **visual, /* RETURN : POINTER to vidual found */
int verbose /* 1 : set verbose on */
){
Display *display ; /* graphic display */
Window rootWindow ; /* the root window */
XSizeHints gHint ; /* hints to X Server on window raising */
unsigned long gForegrnd, gBackgrnd ;
int status, depth ;
/* connect to default X Server */
display = XOpenDisplay(DisplayName) ;
if( display == NULL ) {
fprintf(stderr, "\nERROR: Cannot connect to X server [%s]\n",
XDisplayName(DisplayName) );
return NULL ;
}
*screen = DefaultScreen( display);
rootWindow = RootWindow( display, *screen);
gBackgrnd = WhitePixel( display, *screen);
gForegrnd = BlackPixel( display, *screen);
*visual = DefaultVisual(display, *screen) ;
depth = DefaultDepth(display, *screen) ;
/* provide hints on where to raise the window */
gHint.x = 100 ;
gHint.y = 200 ;
gHint.width = width ;
gHint.height = height ;
gHint.flags = PPosition | PSize;
/* actually create the window */
*window = XCreateSimpleWindow( display, rootWindow,
gHint.x, gHint.y, gHint.width, gHint.height,
(unsigned int) BORDERWIDTH, gForegrnd, gBackgrnd);
XSetWMNormalHints(display, *window, &gHint) ;
XSelectInput( display, *window,
ButtonPressMask | KeyPressMask | ExposureMask |
ButtonReleaseMask | Button1MotionMask );
XMapRaised(display, *window);
XStoreName(display, *window, title);
XFlush(display) ;
/* print out information on x window if required */
if( verbose == 1 ) {
fprintf(stdout, "\n%s version %d of the %s\n",
ServerVendor(display),
VendorRelease(display),
"X Window System") ;
fprintf(stdout, "Color plane depth : %d %s\n\n", depth,
( (depth == 1) ? "(mono)": "") ) ;
}
return display ;
}
/* set-up graphics context */
void xwin_setupGC(
unsigned long *pixels,
int *c_map_size,
char alloc_color
) {
XGCValues gc_values;
unsigned long mask;
XColor xcolor, rgb_color, hw_color;
int i, j, status ;
Colormap color_map;
*c_map_size = 0 ;
color_map = DefaultColormap(gDisplay, gScreen) ;
while (XAllocColorCells(gDisplay, color_map, False, NULL, 0,
&(pixels[*c_map_size]), 1))
(*c_map_size)++;
xcolor.flags = DoRed | DoGreen | DoBlue;
for (i = 0; i < *c_map_size; ++i)
{
xcolor.pixel = pixels[i];
xcolor.red = (unsigned short) (i << 8);
xcolor.green = (unsigned short) (i << 8);
xcolor.blue = (unsigned short) (i << 8);
XStoreColor(gDisplay, color_map, &xcolor);
}
/* this is the GC for handling the image */
mask = GCForeground | GCBackground | GCLineWidth | GCFunction;
gc_values.function = GXcopy;
gc_values.foreground = pixels[*c_map_size - 1];
gc_values.background = pixels[0];
gc_values.line_width = 2 ;
imageGC = XCreateGC(gDisplay, gWindow, mask, &gc_values);
/* this is the GC for handling the points */
if( alloc_color ) {
hw_color.pixel = gc_values.foreground ;
status = XAllocNamedColor(gDisplay, color_map, "red",
&hw_color, &rgb_color) ;
gc_values.foreground = hw_color.pixel ;
}
pointGC = XCreateGC(gDisplay, gWindow, mask, &gc_values) ;
XSetForeground(gDisplay, pointGC, (unsigned long) gc_values.foreground);
/* this is the GC for handling the line */
if( alloc_color ) {
hw_color.pixel = gc_values.foreground ;
status = XAllocNamedColor(gDisplay, color_map, "green",
&hw_color, &rgb_color) ;
gc_values.foreground = hw_color.pixel ;
}
rectGC = XCreateGC(gDisplay, gWindow, mask, &gc_values) ;
XSetForeground(gDisplay, rectGC, (unsigned long) gc_values.foreground);
}
XImage *xwin_creatXimage(float *data, short height, short width, unsigned char magnify)
{
register short i, j, m, n ;
unsigned char *box ;
float *dptr ;
float maxmag, minmag;
double range ;
int index ;
/* Set up an image data structure for handling the image. */
box = (unsigned char *) _iMemAlloc(sizeof(unsigned char) *
height * width * magnify * magnify) ;
for(i=0, dptr=data, maxmag = minmag = *dptr ; i<height; i++)
for(j=0; j<width; j++, dptr++) {
maxmag = MAX(maxmag, *dptr);
minmag = MIN(minmag, *dptr);
}
range = (maxmag - minmag)/0.9 ; /* use 90% of the range */
/* put values in ximage data pointer */
for (i = 0, dptr=data; i < height; ++i)
for (j = 0; j < width; ++j, ++dptr) {
index = (int) ((double) c_map_size * (*dptr-minmag)/range);
for(m=0; m<magnify; m++)
for(n=0; n<magnify; n++)
box[(i*magnify+m)*magnify*width +
j*magnify + n] = pixels[index] ;
}
return XCreateImage(gDisplay, DefaultVisual(gDisplay, gScreen),
(unsigned int) 8, ZPixmap, 0, (char *) box,
width*magnify, magnify*height, 8,
width*magnify);
}
void xwin_drawImg(XImage *ximg, int xoffset, int yoffset)
{
XPutImage(gDisplay, gWindow, imageGC, ximg,
0, 0, xoffset, yoffset, ximg->width, ximg->height) ;
XFlush(gDisplay) ;
}
void xwin_DrawLine( short x1, short y1,
short x2, short y2,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -