Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

ximage.c

Go to the documentation of this file.
00001 
00002 /* ximage.c: Simple program to plot simple image data...
00003  *
00004  *      arguments:      nx, ny          - specify image dimensions
00005  *                      -s nx [ny]      - same (ny = nx if unspecified)
00006  *                      -c colormap     - specify colormap file
00007  */
00008 
00009 #define USAGE "[nx] [ny] [-c colormapfile] [-o xo [yo]] [-s nx [ny]]"
00010 
00011 #include "win.h"
00012 extern lux_wins *get_currentwin();
00013 
00014 /* For convenience... */
00015 
00016 #include "Xnames.h"
00017 
00018 #define DEBUG           0
00019 
00020 #define NX_DEFAULT      128
00021 #define NY_DEFAULT      128
00022 #define COLORMAP_DIR    "/src/gfx/palettes/"
00023 #define COLORMAP_FILE   "Standard"
00024 
00025 #define NDIR            3
00026 static char *colormap_dir[] = {
00027   "./",
00028   "../",
00029   COLORMAP_DIR
00030 };
00031 
00032 void get_cmapfilename(char *name)
00033 {
00034   /* Try various directories prepended to the input file name if
00035      it doesn't exist in the current directory.  Change name to
00036      the correct name, if found. */
00037 
00038   int i;
00039   char tempname[256];
00040   FILE *cmap;
00041 
00042   for (i = 0; i < NDIR; i++) {
00043 
00044     if (i == 2) {
00045       char *local_directory;
00046       if ((local_directory = getenv("STARLAB_PATH")) == NULL) {
00047         name[0] = '\0';
00048         return;
00049       }
00050       strcpy(tempname, local_directory);
00051       strcat(tempname, colormap_dir[i]);
00052     } else
00053       strcpy(tempname, colormap_dir[i]);
00054 
00055     strcat(tempname, name);
00056     if ((cmap = fopen(tempname, "r"))) {
00057       fclose(cmap);
00058       strcpy(name, tempname);
00059       return;
00060     }
00061   }
00062 
00063   name[0] = '\0';
00064 
00065 }
00066 
00067 static unsigned char red[256], green[256], blue[256];
00068 
00069 void read_colormap(char *filename)
00070 {
00071   FILE *cmap;
00072 
00073   if ((cmap = fopen(filename, "r")) == NULL) {
00074     fprintf(stderr, "Unable to open colormap file.\n");
00075     return;
00076   }
00077 
00078   fread(red,   1, 256, cmap);
00079   fread(green, 1, 256, cmap);
00080   fread(blue,  1, 256, cmap);
00081 
00082   fclose(cmap);
00083 }
00084 
00085 int get_data(unsigned char *data, int n, int nr)
00086 {
00087   /* Read the next chunk of data (the next image). 
00088      Only nr = 1 and nr = 4 are actually supported... */
00089 
00090   int i = 0, c;
00091 
00092   for (i = 0; i < n; i++) {
00093 
00094     if ((c = getchar()) == EOF) break;
00095 
00096     if (nr == 1)
00097 
00098       *(data++) = c;
00099 
00100     else {
00101 
00102       /* Turn the input value into a 3-byte (B, G, R) sequence,
00103          followed by a null byte. */
00104 
00105       *(data++) = blue[c];
00106       *(data++) = green[c];
00107       *(data++) = red[c];
00108       *(data++) = 0;
00109 
00110     }
00111 
00112   }
00113   return i*nr;
00114 }
00115 
00116 main(int argc, char* argv[])
00117 {
00118   int i, j;
00119   char colormapfile[256];
00120   char *dir;
00121   int xo, yo, nx, ny;
00122   Window window;
00123   XEvent report;
00124 
00125   /* Establish defaults. */
00126 
00127   xo = 50;
00128   yo = 50;
00129   nx = NX_DEFAULT;
00130   ny = NY_DEFAULT;
00131   strcpy(colormapfile, COLORMAP_FILE);
00132   j = 0;
00133 
00134   /* Parse the argument list. */
00135 
00136   for (i = 1; i < argc; i++) {
00137 
00138     if (argv[i][0] == '-') {
00139 
00140       switch (argv[i][1]) {
00141 
00142       case 'c': strcpy(colormapfile, argv[++i]);
00143                 break;
00144 
00145       case 'o': xo = atoi(argv[++i]);
00146                 if (i < argc-1 && argv[i+1][0] != '-')
00147                   yo = atoi(argv[++i]);
00148                 else
00149                   yo = xo;
00150                 break;
00151 
00152       case 's': nx = atoi(argv[++i]);
00153                 if (i < argc-1 && argv[i+1][0] != '-')
00154                   ny = atoi(argv[++i]);
00155                 else
00156                   ny = nx;
00157 
00158                 break;
00159 
00160       case 'h':
00161       case '-':
00162       default:  dir = rindex(argv[0], '/');
00163                 if (dir == NULL) dir = argv[0] - 1;
00164                 fprintf(stderr, "%s: %s\n", dir+1, USAGE);
00165                 exit(0);
00166 
00167       }
00168 
00169     } else {
00170 
00171       /* Interpret argument as nx or ny. */
00172 
00173       if (j == 0)
00174         nx = atoi(argv[i]);
00175       else
00176         ny = atoi(argv[i]);
00177       j++;
00178 
00179     }
00180   }
00181 
00182   /* Open an X-window. */
00183 
00184   if ( (window = lux_openwin(xo, yo, nx, ny)) <= 0) {
00185 
00186       fprintf(stderr, "Error opening X window.\n");
00187       exit(1);
00188 
00189   } else {
00190 
00191     int ns = 0, nbytes, nrep;
00192     lux_wins *current, *repwin;
00193     Pixmap pixmap;
00194     XImage *image;
00195     unsigned char *data;
00196 
00197     current = get_currentwin(window);
00198 
00199     pixmap = XCreatePixmap(current->win.display, current->win.window, 
00200                            nx, ny, current->win.window_depth);
00201     image = XGetImage(current->win.display, pixmap, 
00202                       0, 0, nx, ny, AllPlanes, ZPixmap);
00203 
00204     fprintf(stderr, "Image width = %d\n", image->width);
00205     fprintf(stderr, "Image height = %d\n", image->height);
00206     fprintf(stderr, "Image depth = %d\n", image->depth);
00207     fprintf(stderr, "Bits per pixel = %d\n", image->bits_per_pixel);
00208 
00209     /* Getting the colors right is a kludge.  The lux package apparently
00210        only knows how to handle 8-bit color, so use it only in that case.
00211        For 24-bit color, for now at least, use the default colormap
00212        and modify the data appropriately as it is read in... */
00213 
00214     get_cmapfilename(colormapfile);     /* Note: changes colormapfile */
00215 
00216     if (colormapfile[0] == '\0')
00217 
00218       fprintf(stderr, "Can't load color map file.\n");
00219 
00220     else {
00221 
00222       fprintf(stderr, "Color map file is %s\n", colormapfile);
00223 
00224       if (image->depth == 8) {
00225 
00226         /* Load in a standard 8-bit colormap. */
00227 
00228         lux_set_window_colormap(window, colormapfile);
00229 
00230       } else {
00231 
00232         /* Read the colormap file for use in manipulating the data. */
00233 
00234         read_colormap(colormapfile);
00235       }
00236 
00237     }
00238 
00239     /* Create the data array.  Note that 24-bit applications use
00240        4 bytes per pixel... */
00241 
00242     nbytes = nx*ny;
00243     nrep = 1;
00244     if (image->bits_per_pixel > 8) {
00245       nrep = image->bits_per_pixel / 8;
00246       nbytes *= nrep;
00247     }
00248 
00249     if ((data = (unsigned char*)malloc(nbytes)) == NULL) {
00250       fprintf(stderr, "Error allocating memory for image.\n");
00251       exit(1);
00252     }
00253 
00254     while (get_data(data, nx*ny, nrep) == nbytes) {
00255 
00256       image->data = (char*)data;
00257 
00258       XPutImage(current->win.display, window, current->win.gc,
00259                 image, 0, 0, 0, 0, nx, ny);
00260 
00261       /* Sleep for ns microseconds between frames. */
00262 
00263       lux_pause(ns);
00264 
00265     }
00266 
00267     /* Enter idle mode before quitting. */
00268     
00269     fprintf(stderr,
00270           "Press any key or button in display window to quit current plot\n");
00271 
00272     while (1) {
00273 
00274       /* Get the next event from the queue. */
00275 
00276       XNextEvent(current->win.display, &report);
00277 
00278       repwin = get_currentwin(report.xany.window);
00279 
00280       if (repwin->win.window == window) {
00281 
00282         if (DEBUG)
00283           fprintf(stderr, "event %d (%s)\n",
00284                   report.type, event_name[report.type]);
00285 
00286         if (report.type == Expose || report.type == ConfigureNotify)
00287 
00288           XPutImage(current->win.display, window, current->win.gc,
00289                     image, 0, 0, 0, 0, nx, ny);
00290 
00291         else if (report.type == KeyPress || report.type == ButtonPress)
00292 
00293           break;
00294 
00295       }
00296     }
00297   }
00298 }

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