00001
00010 #include "openX.h"
00011
00012
00013 #ifndef DISABLE_X11
00014
00015
00034 openX disp_image(WSGraph gd, int lc, int hc, int cflg)
00035 {
00036 int i, j, sl, sh;
00037 char *image;
00038 int nc = COLOR_NUM;
00039 int width, height, dpth, dpb;
00040 int tmp, col;
00041 unsigned short int* pnt;
00042
00043 sWord *imagewk;
00044 XImage *ximg;
00045 Visual *visl;
00046 Pixmap pmap;
00047 openX xid;
00048
00049 width = gd.xs;
00050 height = gd.ys;
00051 imagewk = gd.gp;
00052
00053 xid = displayOpen(gd.xs, gd.ys, cflg);
00054
00055 sl = sh = 1;
00056 if (hc<=0) sh = -1;
00057 if (lc<0) {
00058 sl = -1;
00059 lc = SINTMAX;
00060 }
00061 if (sl<0 || sh<0) {
00062 for(i=0; i<width*height; i++){
00063 tmp = *(imagewk + i);
00064 if (sl<0) lc = Min(lc, tmp);
00065 if (sh<0) hc = Max(hc, tmp);
00066 }
00067 }
00068 hc = Max(hc, lc+2);
00069
00070 visl = DefaultVisual(xid.display, xid.screen);
00071 dpth = DefaultDepth (xid.display, xid.screen);
00072 dpb = dpth/8;
00073 if (dpb==3 && COLOR_DEPTH>=32) dpb = 4;
00074
00075 image = (char*)malloc(width*height*dpb);
00076 for(i=0; i<width*height; i++){
00077 tmp = *(imagewk + i);
00078 if (tmp>hc) tmp = hc;
00079 if (tmp<lc) tmp = lc;
00080 if (cflg==MONOC) tmp = (int)((double)(tmp-lc)*(nc-1)/(double)(hc-lc));
00081 else tmp = (int)(nc-(double)(tmp-lc)*(nc-1)/(double)(hc-lc)-1);
00082
00083 col = xid.color_index[tmp];
00084 if (dpb>=3) {
00085 for (j=0; j<dpb; j++) image[dpb*i+j] = col;
00086 }
00087 else {
00088 pnt = (unsigned short int*)&(image[dpb*i]);
00089 *pnt = col;
00090 }
00091 }
00092
00093 pmap = XCreatePixmap(xid.display, xid.window, width, height, dpth);
00094 ximg = XCreateImage(xid.display,visl,dpth,ZPixmap,0,(char*)image,width,height,8,0);
00095 XPutImage(xid.display, pmap, xid.gc, ximg, 0, 0, 0, 0, width, height);
00096 XSetWindowBackgroundPixmap(xid.display, xid.window, pmap);
00097 XMapWindow (xid.display, xid.window);
00098 XClearWindow(xid.display, xid.window);
00099 XFlush(xid.display);
00100
00101 xid.id = 1;
00102 return xid;
00103 }
00104
00105
00106
00114 void displayClose(openX xid)
00115 {
00116 if (xid.id >= 0) {
00117 free(xid.color_index);
00118 XFreeGC(xid.display, xid.gc);
00119 XDestroyWindow(xid.display, xid.window);
00120 XCloseDisplay(xid.display);
00121 xid.id = -1;
00122 }
00123 }
00124
00125
00126
00127 openX displayOpen(int xs, int ys, int cflg)
00128 {
00129 Window root_window;
00130 XSizeHints hint;
00131 XColor color;
00132 Colormap cmap;
00133 XSetWindowAttributes at;
00134 openX xid;
00135
00136 int ir, ig, ib;
00137 unsigned long foreground, background;
00138 unsigned short values[] = {0x0, 0x4444, 0x8888, 0xcccc, 0xffff};
00139
00140 hint.x = 0;
00141 hint.y = 0;
00142 hint.width = xs;
00143 hint.height = ys;
00144 hint.flags = PPosition|PSize;
00145
00146 xid.display = XOpenDisplay(NULL);
00147 root_window = DefaultRootWindow(xid.display);
00148
00149 xid.screen = DefaultScreen(xid.display);
00150 foreground = WhitePixel(xid.display, xid.screen);
00151 background = BlackPixel(xid.display, xid.screen);
00152
00153 xid.window = XCreateSimpleWindow(xid.display, root_window, hint.x, hint.y,
00154 hint.width, hint.height, 5, foreground, background);
00155 XSetStandardProperties(xid.display,xid.window,"openX","openX",None,NULL,0,&hint);
00156
00157 xid.gc = XCreateGC(xid.display, xid.window, 0, 0);
00158 XSetBackground(xid.display, xid.gc, background);
00159 XSetForeground(xid.display, xid.gc, foreground);
00160
00161 at.backing_store = WhenMapped;
00162 at.bit_gravity = CenterGravity;
00163 XChangeWindowAttributes(xid.display, xid.window, CWBackingStore, &at);
00164 XChangeWindowAttributes(xid.display, xid.window, CWBitGravity, &at);
00165
00166 XSelectInput(xid.display, xid.window, ButtonPressMask|KeyPressMask|ExposureMask);
00167 XMapRaised(xid.display, xid.window);
00168
00169 cmap = DefaultColormap(xid.display, xid.screen);
00170 xid.color_index = (long unsigned int*)malloc(COLOR_NUM*sizeof(long));
00171 for (ir = 0; ir < 5; ir++) {
00172 for (ig = 0; ig < 5; ig++) {
00173 for (ib = 0; ib < 5; ib++) {
00174 if (cflg==MONOC) {
00175 color.red = (int)((ir+5.*ig+ib*25.)/(double)COLOR_NUM*65532.);
00176 color.green = color.blue = color.red;
00177 }
00178 else {
00179 color.red = values[ir];
00180 color.green = values[ig];
00181 color.blue = values[ib];
00182 }
00183 XAllocColor(xid.display, cmap, &color);
00184 xid.color_index[ir+5*ig+25*ib] = color.pixel;
00185 }
00186 }
00187 }
00188
00189 xid.id = 0;
00190 set_color(xid, 0.0, 0.0, 0.0);
00191 return xid;
00192 }
00193
00194
00195
00196 void set_color(openX xid, double r, double g, double b)
00197 {
00198 int ir, ig, ib;
00199
00200 r = Max(0.0, Min(r, 1.0));
00201 g = Max(0.0, Min(g, 1.0));
00202 b = Max(0.0, Min(b, 1.0));
00203
00204 ir = (int)(4*r + 0.5);
00205 ig = (int)(4*g + 0.5);
00206 ib = (int)(4*b + 0.5);
00207
00208 XSetForeground(xid.display, xid.gc, xid.color_index[ir+5*ig+25*ib]);
00209 }
00210
00211
00212 #endif // DISABLE_X11