Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

ximage_compiled.c

Go to the documentation of this file.
00001 
00002 #include "win.h"
00003 extern lux_wins *get_currentwin();
00004 
00005 /* For convenience... */
00006 
00007 #include "Xnames.h"
00008 
00009 #define DEBUG           0
00010 
00011 #define COLORMAP_DIR    "/home/beowulf2/starlab/2.1/src/gfx/palettes/"
00012 #define COLORMAP_FILE   "Standard"
00013 
00014 #define NDIR            3
00015 static char *colormap_dir[] = {
00016   "./",
00017   "../",
00018   COLORMAP_DIR
00019 };
00020 
00021 int get_cmapfilename(char *name)
00022 {
00023   /* Try various directories prepended to the input file name if
00024      it doesn't exist in the current directory. */
00025 
00026   int i;
00027   char tempname[256];
00028   FILE *cmap;
00029 
00030   for (i = 0; i < NDIR; i++) {
00031     strcpy(tempname, colormap_dir[i]);
00032     strcat(tempname, name);
00033     if ((cmap = fopen(tempname, "r"))) {
00034       fclose(cmap);
00035       strcpy(name, tempname);
00036       return;
00037     }
00038   }
00039 
00040   name[0] = '\0';
00041 
00042 }
00043 
00044 static unsigned char red[256], green[256], blue[256];
00045 
00046 int read_colormap(char *filename)
00047 {
00048   FILE *cmap;
00049 
00050   if ((cmap = fopen(filename, "r")) == NULL) {
00051     fprintf(stderr, "Unable to open colormap file.\n");
00052     return;
00053   }
00054 
00055   fread(red,   1, 256, cmap);
00056   fread(green, 1, 256, cmap);
00057   fread(blue,  1, 256, cmap);
00058 
00059   fclose(cmap);
00060 }
00061 
00062 static char colormapfile[256];
00063 static int xo, yo, nx, ny;
00064 static Window window;
00065 static XEvent report;
00066 
00067 static lux_wins *current;
00068 static Pixmap pixmap;
00069 static XImage *image;
00070 
00071 static int nrep = 1;
00072 
00073 int ximage_init(int sizex, int sizey)
00074 {
00075   int i, j;
00076 
00077   /* Establish defaults. */
00078 
00079   xo = 50;
00080   yo = 50;
00081   nx = sizex;
00082   ny = sizey;
00083   strcpy(colormapfile, COLORMAP_FILE);
00084 
00085   /* Open an X-window. */
00086 
00087   if ( (window = lux_openwin(xo, yo, nx, ny)) <= 0) {
00088 
00089       fprintf(stderr, "Error opening X window.\n");
00090       exit(1);
00091 
00092   }
00093 
00094   current = get_currentwin(window);
00095 
00096   pixmap = XCreatePixmap(current->win.display, current->win.window, 
00097                          nx, ny, current->win.window_depth);
00098   image = XGetImage(current->win.display, pixmap, 
00099                     0, 0, nx, ny, AllPlanes, ZPixmap);
00100 
00101   /* Getting the colors right is a kludge.  The lux package apparently
00102      only knows how to handle 8-bit color, so use it only in that case.
00103      For 24-bit color, for now at least, use the default colormap
00104      and modify the data appropriately as it is read in... */
00105 
00106   get_cmapfilename(colormapfile);
00107 
00108   if (colormapfile[0] == '\0')
00109 
00110     fprintf(stderr, "Can't load color map file.\n");
00111 
00112   else {
00113 
00114     fprintf(stderr, "Color map file is %s\n", colormapfile);
00115 
00116     if (image->depth == 8) {
00117 
00118       /* Load in a standard 8-bit colormap. */
00119 
00120       lux_set_window_colormap(window, colormapfile);
00121 
00122     } else {
00123 
00124       /* Read the colormap file for use in manipulating the data. */
00125 
00126       read_colormap(colormapfile);
00127 
00128     }
00129 
00130   }
00131 
00132   if (image->bits_per_pixel > 8) nrep = image->bits_per_pixel / 8;
00133 
00134 }
00135 
00136 int ximage(char *data)
00137 {
00138   image->data = (char*)data;
00139 
00140   XPutImage(current->win.display, window, current->win.gc,
00141             image, 0, 0, 0, 0, nx, ny);
00142 }
00143 
00144 int ximage_quit()
00145 {
00146     lux_wins *repwin;
00147 
00148     /* Enter idle mode before quitting. */
00149     
00150     fprintf(stderr,
00151           "Press any key or button in display window to quit current plot\n");
00152 
00153     while (1) {
00154 
00155       /* Get the next event from the queue. */
00156 
00157       XNextEvent(current->win.display, &report);
00158 
00159       repwin = get_currentwin(report.xany.window);
00160 
00161       if (repwin->win.window == window) {
00162 
00163         if (DEBUG)
00164           fprintf(stderr, "event %d (%s)\n",
00165                   report.type, event_name[report.type]);
00166 
00167         if (report.type == Expose || report.type == ConfigureNotify)
00168 
00169           XPutImage(current->win.display, window, current->win.gc,
00170                     image, 0, 0, 0, 0, nx, ny);
00171 
00172         else if (report.type == KeyPress || report.type == ButtonPress)
00173 
00174           break;
00175 
00176       }
00177 
00178     }
00179 
00180 }
00181 
00182 /*------------------------------------------------------------------------*/
00183 
00184 /* Accessor functions. */
00185 
00186 int get_nrep()
00187 {
00188   return nrep;
00189 }
00190 
00191 int get_cmap(unsigned char *r, unsigned char *g, unsigned char *b)
00192 {
00193   int i;
00194   for (i = 0; i < 255; i++) {
00195     r[i] = red[i];
00196     g[i] = green[i];
00197     b[i] = blue[i];
00198   }
00199   return 0;
00200 }

Generated at Sun Feb 24 09:57:21 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001