home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / puzzle / picture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-21  |  4.4 KB  |  168 lines

  1. /* $XConsortium: picture.c,v 1.7 91/08/21 11:48:53 rws Exp $ */
  2. /* Puzzle - (C) Copyright 1987, 1988 Don Bennett.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <X11/Xos.h>
  13. #include <X11/X.h>
  14. #include <X11/Xlib.h>
  15. #include <errno.h>
  16.  
  17. extern Display    *dpy;
  18. extern int    screen;
  19. extern int    errno;
  20. extern int    CreateNewColormap;
  21. extern Colormap PuzzleColormap;
  22.  
  23. /*
  24.  * This function reads a file that contains a color-mapped pixmap, in the
  25.  * following format:
  26.  * (a) width of the pixmap, first 4 bytes;
  27.  * (b) height of the pixmap next 4 bytes;
  28.  * (c) number of colormap entries, 1 byte;
  29.  * (d) colormap values, each is one byte each of (r,g,b),
  30.  *        3xnum_colormap_entries;
  31.  * (e) pixmap data, one byte per pixel;
  32.  * 
  33.  *******************************************************
  34.  * Thanks to Jack Palevich @apple, whose code I used
  35.  * for generating the color-mapped version of the mandrill
  36.  * from an original image of 8-bits per component.
  37.  *******************************************************
  38.  */
  39.  
  40. Pixmap PictureSetup(fname,width,height)
  41. char *fname;
  42. long *width, *height; /* RETURNS */
  43. {
  44. int readcount;
  45.  
  46.     int fd, i, cmapSize;
  47.     unsigned char cmapSizeByte;
  48.     unsigned char colormapData[256][3];
  49.     XColor colorMap[256];
  50.     Pixmap PicturePixmap;
  51.     unsigned char *data;
  52.     unsigned long swaptest = 1;
  53.     
  54.     fd = open(fname, O_RDONLY);
  55.     if (fd == -1) {
  56.     fprintf(stderr,"could not open picture file '%s', errno = %d\n",
  57.         fname, errno);
  58.     exit(1);
  59.     }
  60.  
  61.     read(fd, (char *)width, sizeof(*width));
  62.     read(fd, (char *)height, sizeof(*height));
  63.     if (*(char *) &swaptest) {
  64.     swap_long((char *) width, sizeof(*width));
  65.     swap_long((char *) height, sizeof(*height));
  66.     }
  67.  
  68.     read(fd, (char *)&cmapSizeByte, sizeof(cmapSizeByte));
  69.  
  70.     cmapSize = cmapSizeByte;
  71.     if (cmapSize == 0)
  72.     cmapSize = 256;
  73.  
  74.     read(fd, (char *)colormapData, 3*cmapSize);
  75.     data = (unsigned char *) malloc((*width)*(*height));
  76.     if (!data) {
  77.     fprintf(stderr,
  78.         "could not allocate memory for picture '%s', errno = %d\n",
  79.         fname, errno);
  80.     exit(1);
  81.     }
  82.     readcount = read(fd, (char *)data, (*width)*(*height));
  83.  
  84.     /***********************************/
  85.     /** allocate the colormap entries **/
  86.     /***********************************/
  87.  
  88.     for (i=0; i<cmapSize; i++) {
  89.     colorMap[i].red   = colormapData[i][0];
  90.     colorMap[i].green = colormapData[i][1];
  91.     colorMap[i].blue  = colormapData[i][2];
  92.     colorMap[i].red   |= colorMap[i].red   << 8;
  93.     colorMap[i].green |= colorMap[i].green << 8;
  94.     colorMap[i].blue  |= colorMap[i].blue  << 8;
  95.     }
  96.  
  97.     getColormapEntries(colorMap,cmapSize);
  98.  
  99.     /******************************************************/
  100.     /** convert the data to the appropriate pixel value, **/
  101.     /** cache the data as a pixmap on the server         **/
  102.     /******************************************************/
  103.       
  104.     for (i=0; i<(*width)*(*height); i++)
  105.     data[i] = colorMap[data[i]].pixel;
  106.  
  107.     {
  108.     GC gc;
  109.     XImage *image;
  110.  
  111.     gc = DefaultGC(dpy, screen);
  112.     image = XCreateImage(dpy, DefaultVisual(dpy,screen),
  113.                  8, ZPixmap, 0, (char *)data, *width, *height,
  114.                  8, *width);
  115.  
  116.     PicturePixmap = XCreatePixmap(dpy,RootWindow(dpy,screen),
  117.                       image->width,image->height,8);
  118.     XPutImage(dpy,PicturePixmap,gc,image,0,0,0,0,
  119.           image->width,image->height);
  120.     }
  121.     return(PicturePixmap);
  122. }
  123.  
  124. getColormapEntries(colorMap, numColors)
  125. XColor *colorMap;
  126. int numColors;
  127. {
  128.     int i;
  129.  
  130.     if (CreateNewColormap)
  131.     PuzzleColormap = XCreateColormap(dpy,RootWindow(dpy,screen),
  132.                      DefaultVisual(dpy,screen),AllocNone);
  133.     else
  134.     PuzzleColormap = DefaultColormap(dpy, screen);
  135.     
  136.     for (i = 0; i < numColors; i++) {
  137.     /* Need a read-only color for this value */
  138.     if (!XAllocColor(dpy,PuzzleColormap,&colorMap[i])) {
  139.         fprintf(stderr, "not enough colors (asked for %d, got %d).\n",
  140.             numColors, i);
  141.         fprintf(stderr, "try the -cm option.\n");
  142.         return (-1);
  143.     }
  144.     }
  145.     return(0);
  146. }
  147.  
  148. swap_long (bp, n)
  149.     register char *bp;
  150.     register unsigned n;
  151. {
  152.     register char c;
  153.     register char *ep = bp + n;
  154.     register char *sp;
  155.  
  156.     while (bp < ep) {
  157.     sp = bp + 3;
  158.     c = *sp;
  159.     *sp = *bp;
  160.     *bp++ = c;
  161.     sp = bp + 1;
  162.     c = *sp;
  163.     *sp = *bp;
  164.     *bp++ = c;
  165.     bp += 2;
  166.     }
  167. }
  168.