home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / xbm / code / xrdbitf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-20  |  7.3 KB  |  254 lines

  1. /* $XConsortium: XRdBitF.c,v 1.15 91/02/01 16:34:46 gildea Exp $ */
  2. /* Copyright, 1987, Massachusetts Institute of Technology */
  3.  
  4. /*
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation, and that the name of M.I.T. not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior permission.  M.I.T. makes no representations about the
  12. suitability of this software for any purpose.  It is provided "as is"
  13. without express or implied warranty.
  14. */
  15.  
  16. /*
  17.  *    Code to read bitmaps from disk files. Interprets 
  18.  *    data from X10 and X11 bitmap files and creates
  19.  *    Pixmap representations of files. Returns Pixmap
  20.  *    ID and specifics about image.
  21.  *
  22.  *    Modified for speedup by Jim Becker, changed image
  23.  *    data parsing logic (removed some fscanf()s). 
  24.  *    Aug 5, 1988
  25.  *
  26.  * Note that this file and ../Xmu/RdBitF.c look very similar....  Keep them
  27.  * that way (but don't use common source code so that people can have one 
  28.  * without the other).
  29.  */
  30.  
  31. #include "Xlibint.h"
  32. #include <X11/Xos.h>
  33. #include "Xutil.h"
  34. #include <stdio.h>
  35. #include <ctype.h>
  36.  
  37.  
  38. #define MAX_SIZE 255
  39.  
  40. /* shared data for the image read/parse logic */
  41. static short hexTable[256];        /* conversion value */
  42. static Bool initialized = False;    /* easier to fill in at run time */
  43.  
  44.  
  45. /*
  46.  *    Table index for the hex values. Initialized once, first time.
  47.  *    Used for translation value or delimiter significance lookup.
  48.  */
  49. static void initHexTable()
  50. {
  51.     /*
  52.      * We build the table at run time for several reasons:
  53.      *
  54.      *     1.  portable to non-ASCII machines.
  55.      *     2.  still reentrant since we set the init flag after setting table.
  56.      *     3.  easier to extend.
  57.      *     4.  less prone to bugs.
  58.      */
  59.     hexTable['0'] = 0;    hexTable['1'] = 1;
  60.     hexTable['2'] = 2;    hexTable['3'] = 3;
  61.     hexTable['4'] = 4;    hexTable['5'] = 5;
  62.     hexTable['6'] = 6;    hexTable['7'] = 7;
  63.     hexTable['8'] = 8;    hexTable['9'] = 9;
  64.     hexTable['A'] = 10;    hexTable['B'] = 11;
  65.     hexTable['C'] = 12;    hexTable['D'] = 13;
  66.     hexTable['E'] = 14;    hexTable['F'] = 15;
  67.     hexTable['a'] = 10;    hexTable['b'] = 11;
  68.     hexTable['c'] = 12;    hexTable['d'] = 13;
  69.     hexTable['e'] = 14;    hexTable['f'] = 15;
  70.  
  71.     /* delimiters of significance are flagged w/ negative value */
  72.     hexTable[' '] = -1;    hexTable[','] = -1;
  73.     hexTable['}'] = -1;    hexTable['\n'] = -1;
  74.     hexTable['\t'] = -1;
  75.     
  76.     initialized = True;
  77. }
  78.  
  79. /*
  80.  *    read next hex value in the input stream, return -1 if EOF
  81.  */
  82. static NextInt (fstream)
  83.     FILE *fstream;
  84. {
  85.     int    ch;
  86.     int    value = 0;
  87.     int gotone = 0;
  88.     int done = 0;
  89.     
  90.     /* loop, accumulate hex value until find delimiter  */
  91.     /* skip any initial delimiters found in read stream */
  92.  
  93.     while (!done) {
  94.     ch = getc(fstream);
  95.     if (ch == EOF) {
  96.         value    = -1;
  97.         done++;
  98.     } else {
  99.         /* trim high bits, check type and accumulate */
  100.         ch &= 0xff;
  101.         if (isascii(ch) && isxdigit(ch)) {
  102.         value = (value << 4) + hexTable[ch];
  103.         gotone++;
  104.         } else if ((hexTable[ch]) < 0 && gotone)
  105.           done++;
  106.     }
  107.     }
  108.     return value;
  109. }
  110.  
  111.  
  112. #if NeedFunctionPrototypes
  113. int XReadBitmapFile (
  114.     Display *display,
  115.     Drawable d,
  116.     _Xconst char *filename,
  117.     unsigned int *width,                /* RETURNED */
  118.     unsigned int *height,               /* RETURNED */
  119.     Pixmap *pixmap,                     /* RETURNED */
  120.     int *x_hot,                         /* RETURNED */
  121.     int *y_hot)                         /* RETURNED */
  122. #else
  123. int XReadBitmapFile (display, d, filename, width, height, pixmap, x_hot, y_hot)
  124.     Display *display;
  125.     Drawable d;
  126.     char *filename;
  127.     unsigned int *width, *height;       /* RETURNED */
  128.     Pixmap *pixmap;                     /* RETURNED */
  129.     int *x_hot, *y_hot;                 /* RETURNED */
  130. #endif
  131. {
  132.     Pixmap pix;                /* value to return */
  133.     FILE *fstream;            /* handle on file  */
  134.     unsigned char *data = NULL;        /* working variable */
  135.     char line[MAX_SIZE];        /* input line from file */
  136.     int size;                /* number of bytes of data */
  137.     char name_and_type[MAX_SIZE];    /* an input line */
  138.     char *type;                /* for parsing */
  139.     int value;                /* from an input line */
  140.     int version10p;            /* boolean, old format */
  141.     int padding;            /* to handle alignment */
  142.     int bytes_per_line;            /* per scanline of data */
  143.     unsigned int ww = 0;        /* width */
  144.     unsigned int hh = 0;        /* height */
  145.     int hx = -1;            /* x hotspot */
  146.     int hy = -1;            /* y hotspot */
  147.  
  148.     /* first time initialization */
  149.     if (initialized == False) initHexTable();
  150.  
  151.     if ((fstream = fopen(filename, "r")) == NULL) {
  152.     return BitmapOpenFailed;
  153.     }
  154.  
  155.     /* error cleanup and return macro    */
  156. #define    RETURN(code) { if (data) free (data); fclose (fstream); return code; }
  157.  
  158.     while (fgets(line, MAX_SIZE, fstream)) {
  159.     if (strlen(line) == MAX_SIZE-1) {
  160.         RETURN (BitmapFileInvalid);
  161.     }
  162.     if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
  163.         if (!(type = rindex(name_and_type, '_')))
  164.           type = name_and_type;
  165.         else
  166.           type++;
  167.  
  168.         if (!strcmp("width", type))
  169.           ww = (unsigned int) value;
  170.         if (!strcmp("height", type))
  171.           hh = (unsigned int) value;
  172.         if (!strcmp("hot", type)) {
  173.         if (type-- == name_and_type || type-- == name_and_type)
  174.           continue;
  175.         if (!strcmp("x_hot", type))
  176.           hx = value;
  177.         if (!strcmp("y_hot", type))
  178.           hy = value;
  179.         }
  180.         continue;
  181.     }
  182.     
  183.     if (sscanf(line, "static short %s = {", name_and_type) == 1)
  184.       version10p = 1;
  185.     else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
  186.       version10p = 0;
  187.     else if (sscanf(line, "static char %s = {", name_and_type) == 1)
  188.       version10p = 0;
  189.     else
  190.       continue;
  191.  
  192.     if (!(type = rindex(name_and_type, '_')))
  193.       type = name_and_type;
  194.     else
  195.       type++;
  196.  
  197.     if (strcmp("bits[]", type))
  198.       continue;
  199.     
  200.     if (!ww || !hh)
  201.       RETURN (BitmapFileInvalid);
  202.  
  203.     if ((ww % 16) && ((ww % 16) < 9) && version10p)
  204.       padding = 1;
  205.     else
  206.       padding = 0;
  207.  
  208.     bytes_per_line = (ww+7)/8 + padding;
  209.  
  210.     size = bytes_per_line * hh;
  211.     data = (unsigned char *) Xmalloc ((unsigned int) size);
  212.     if (!data) 
  213.       RETURN (BitmapNoMemory);
  214.  
  215.     if (version10p) {
  216.         unsigned char *ptr;
  217.         int bytes;
  218.  
  219.         for (bytes=0, ptr=data; bytes<size; (bytes += 2)) {
  220.         if ((value = NextInt(fstream)) < 0)
  221.           RETURN (BitmapFileInvalid);
  222.         *(ptr++) = value;
  223.         if (!padding || ((bytes+2) % bytes_per_line))
  224.           *(ptr++) = value >> 8;
  225.         }
  226.     } else {
  227.         unsigned char *ptr;
  228.         int bytes;
  229.  
  230.         for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) {
  231.         if ((value = NextInt(fstream)) < 0) 
  232.           RETURN (BitmapFileInvalid);
  233.         *ptr=value;
  234.         }
  235.     }
  236.     }                    /* end while */
  237.  
  238.     if (data == NULL) {
  239.     RETURN (BitmapFileInvalid);
  240.     }
  241.  
  242.     pix = XCreateBitmapFromData (display, d, (char *) data, ww, hh);
  243.     if (pix == None) {
  244.     RETURN (BitmapNoMemory);
  245.     }
  246.     *pixmap = pix;
  247.     *width = ww;
  248.     *height = hh;
  249.     if (x_hot) *x_hot = hx;
  250.     if (y_hot) *y_hot = hy;
  251.  
  252.     RETURN (BitmapSuccess);
  253. }
  254.