home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_RdBitF.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  9KB  |  298 lines

  1. /* $XConsortium: RdBitF.c,v 1.19 94/04/17 20:20:42 rws Exp $ */
  2. /* $XFree86: xc/lib/X11/RdBitF.c,v 3.0 1994/10/20 06:03:09 dawes Exp $ */
  3. /*
  4.  
  5. Copyright (c) 1987  X Consortium
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files (the
  9. "Software"), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and/or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14.  
  15. The above copyright notice and this permission notice shall be included
  16. in all copies or substantial portions of the Software.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. OTHER DEALINGS IN THE SOFTWARE.
  25.  
  26. Except as contained in this notice, the name of the X Consortium shall
  27. not be used in advertising or otherwise to promote the sale, use or
  28. other dealings in this Software without prior written authorization
  29. from the X Consortium.
  30.  
  31. */
  32.  
  33. /*
  34.  *    Code to read bitmaps from disk files. Interprets 
  35.  *    data from X10 and X11 bitmap files and creates
  36.  *    Pixmap representations of files. Returns Pixmap
  37.  *    ID and specifics about image.
  38.  *
  39.  *    Modified for speedup by Jim Becker, changed image
  40.  *    data parsing logic (removed some fscanf()s). 
  41.  *    Aug 5, 1988
  42.  *
  43.  * Note that this file and ../Xmu/RdBitF.c look very similar....  Keep them
  44.  * that way (but don't use common source code so that people can have one 
  45.  * without the other).
  46.  */
  47.  
  48. #undef XLIB_ILLEGAL_ACCESS
  49. #include <X11/Xlibint.h>
  50. #include <X11/Xos.h>
  51. #include "Xutil.h"
  52. #include <stdio.h>
  53. #include <ctype.h>
  54.  
  55.  
  56. #define MAX_SIZE 255
  57.  
  58. /* shared data for the image read/parse logic */
  59. static short hexTable[256];        /* conversion value */
  60. static Bool initialized = False;    /* easier to fill in at run time */
  61.  
  62.  
  63. /*
  64.  *    Table index for the hex values. Initialized once, first time.
  65.  *    Used for translation value or delimiter significance lookup.
  66.  */
  67. static void initHexTable()
  68. {
  69.     /*
  70.      * We build the table at run time for several reasons:
  71.      *
  72.      *     1.  portable to non-ASCII machines.
  73.      *     2.  still reentrant since we set the init flag after setting table.
  74.      *     3.  easier to extend.
  75.      *     4.  less prone to bugs.
  76.      */
  77.     hexTable['0'] = 0;    hexTable['1'] = 1;
  78.     hexTable['2'] = 2;    hexTable['3'] = 3;
  79.     hexTable['4'] = 4;    hexTable['5'] = 5;
  80.     hexTable['6'] = 6;    hexTable['7'] = 7;
  81.     hexTable['8'] = 8;    hexTable['9'] = 9;
  82.     hexTable['A'] = 10;    hexTable['B'] = 11;
  83.     hexTable['C'] = 12;    hexTable['D'] = 13;
  84.     hexTable['E'] = 14;    hexTable['F'] = 15;
  85.     hexTable['a'] = 10;    hexTable['b'] = 11;
  86.     hexTable['c'] = 12;    hexTable['d'] = 13;
  87.     hexTable['e'] = 14;    hexTable['f'] = 15;
  88.  
  89.     /* delimiters of significance are flagged w/ negative value */
  90.     hexTable[' '] = -1;    hexTable[','] = -1;
  91.     hexTable['}'] = -1;    hexTable['\n'] = -1;
  92.     hexTable['\t'] = -1;
  93.     
  94.     initialized = True;
  95. }
  96.  
  97. /*
  98.  *    read next hex value in the input stream, return -1 if EOF
  99.  */
  100. static NextInt (fstream)
  101.     FILE *fstream;
  102. {
  103.     int    ch;
  104.     int    value = 0;
  105.     int gotone = 0;
  106.     int done = 0;
  107.     
  108.     /* loop, accumulate hex value until find delimiter  */
  109.     /* skip any initial delimiters found in read stream */
  110.  
  111.     while (!done) {
  112.     ch = getc(fstream);
  113.     if (ch == EOF) {
  114.         value    = -1;
  115.         done++;
  116.     } else {
  117.         /* trim high bits, check type and accumulate */
  118.         ch &= 0xff;
  119.         if (isascii(ch) && isxdigit(ch)) {
  120.         value = (value << 4) + hexTable[ch];
  121.         gotone++;
  122.         } else if ((hexTable[ch]) < 0 && gotone)
  123.           done++;
  124.     }
  125.     }
  126.     return value;
  127. }
  128.  
  129. #if NeedFunctionPrototypes
  130. int XReadBitmapFileData (
  131.     _Xconst char *filename,
  132.     unsigned int *width,                /* RETURNED */
  133.     unsigned int *height,               /* RETURNED */
  134.     unsigned char **data,               /* RETURNED */
  135.     int *x_hot,                         /* RETURNED */
  136.     int *y_hot)                         /* RETURNED */
  137. #else
  138. int XReadBitmapFileData (filename, width, height, data, x_hot, y_hot)
  139.     char *filename;
  140.     unsigned int *width, *height;       /* RETURNED */
  141.     unsigned char **data;               /* RETURNED */
  142.     int *x_hot, *y_hot;                 /* RETURNED */
  143. #endif
  144. {
  145.     FILE *fstream;            /* handle on file  */
  146.     unsigned char *bits = NULL;        /* working variable */
  147.     char line[MAX_SIZE];        /* input line from file */
  148.     int size;                /* number of bytes of data */
  149.     char name_and_type[MAX_SIZE];    /* an input line */
  150.     char *type;                /* for parsing */
  151.     int value;                /* from an input line */
  152.     int version10p;            /* boolean, old format */
  153.     int padding;            /* to handle alignment */
  154.     int bytes_per_line;            /* per scanline of data */
  155.     unsigned int ww = 0;        /* width */
  156.     unsigned int hh = 0;        /* height */
  157.     int hx = -1;            /* x hotspot */
  158.     int hy = -1;            /* y hotspot */
  159.  
  160.     /* first time initialization */
  161.     if (initialized == False) initHexTable();
  162.  
  163. #ifdef __EMX__
  164.     filename = __XOS2RedirRoot(filename);
  165. #endif
  166.     if (!(fstream = fopen(filename, "r")))
  167.     return BitmapOpenFailed;
  168.  
  169.     /* error cleanup and return macro    */
  170. #define    RETURN(code) \
  171. { if (bits) Xfree ((char *)bits); fclose (fstream); return code; }
  172.  
  173.     while (fgets(line, MAX_SIZE, fstream)) {
  174.     if (strlen(line) == MAX_SIZE-1)
  175.         RETURN (BitmapFileInvalid);
  176.     if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
  177.         if (!(type = strrchr(name_and_type, '_')))
  178.           type = name_and_type;
  179.         else
  180.           type++;
  181.  
  182.         if (!strcmp("width", type))
  183.           ww = (unsigned int) value;
  184.         if (!strcmp("height", type))
  185.           hh = (unsigned int) value;
  186.         if (!strcmp("hot", type)) {
  187.         if (type-- == name_and_type || type-- == name_and_type)
  188.           continue;
  189.         if (!strcmp("x_hot", type))
  190.           hx = value;
  191.         if (!strcmp("y_hot", type))
  192.           hy = value;
  193.         }
  194.         continue;
  195.     }
  196.     
  197.     if (sscanf(line, "static short %s = {", name_and_type) == 1)
  198.       version10p = 1;
  199.     else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
  200.       version10p = 0;
  201.     else if (sscanf(line, "static char %s = {", name_and_type) == 1)
  202.       version10p = 0;
  203.     else
  204.       continue;
  205.  
  206.     if (!(type = strrchr(name_and_type, '_')))
  207.       type = name_and_type;
  208.     else
  209.       type++;
  210.  
  211.     if (strcmp("bits[]", type))
  212.       continue;
  213.     
  214.     if (!ww || !hh)
  215.       RETURN (BitmapFileInvalid);
  216.  
  217.     if ((ww % 16) && ((ww % 16) < 9) && version10p)
  218.       padding = 1;
  219.     else
  220.       padding = 0;
  221.  
  222.     bytes_per_line = (ww+7)/8 + padding;
  223.  
  224.     size = bytes_per_line * hh;
  225.     bits = (unsigned char *) Xmalloc ((unsigned int) size);
  226.     if (!bits) 
  227.       RETURN (BitmapNoMemory);
  228.  
  229.     if (version10p) {
  230.         unsigned char *ptr;
  231.         int bytes;
  232.  
  233.         for (bytes=0, ptr=bits; bytes<size; (bytes += 2)) {
  234.         if ((value = NextInt(fstream)) < 0)
  235.           RETURN (BitmapFileInvalid);
  236.         *(ptr++) = value;
  237.         if (!padding || ((bytes+2) % bytes_per_line))
  238.           *(ptr++) = value >> 8;
  239.         }
  240.     } else {
  241.         unsigned char *ptr;
  242.         int bytes;
  243.  
  244.         for (bytes=0, ptr=bits; bytes<size; bytes++, ptr++) {
  245.         if ((value = NextInt(fstream)) < 0) 
  246.           RETURN (BitmapFileInvalid);
  247.         *ptr=value;
  248.         }
  249.     }
  250.     }                    /* end while */
  251.  
  252.     fclose(fstream);
  253.     if (!bits)
  254.     return (BitmapFileInvalid);
  255.  
  256.     *data = bits;
  257.     *width = ww;
  258.     *height = hh;
  259.     if (x_hot) *x_hot = hx;
  260.     if (y_hot) *y_hot = hy;
  261.  
  262.     return (BitmapSuccess);
  263. }
  264.  
  265. #if NeedFunctionPrototypes
  266. int XReadBitmapFile (
  267.     Display *display,
  268.     Drawable d,
  269.     _Xconst char *filename,
  270.     unsigned int *width,                /* RETURNED */
  271.     unsigned int *height,               /* RETURNED */
  272.     Pixmap *pixmap,                     /* RETURNED */
  273.     int *x_hot,                         /* RETURNED */
  274.     int *y_hot)                         /* RETURNED */
  275. #else
  276. int XReadBitmapFile (display, d, filename, width, height, pixmap, x_hot, y_hot)
  277.     Display *display;
  278.     Drawable d;
  279.     char *filename;
  280.     unsigned int *width, *height;       /* RETURNED */
  281.     Pixmap *pixmap;                     /* RETURNED */
  282.     int *x_hot, *y_hot;                 /* RETURNED */
  283. #endif
  284. {
  285.     Pixmap pix;                /* value to return */
  286.     unsigned char *data;
  287.     int res;
  288.  
  289.     res = XReadBitmapFileData(filename, width, height, &data, x_hot, y_hot);
  290.     if (res != BitmapSuccess)
  291.     return res;
  292.     *pixmap = XCreateBitmapFromData(display, d, (char *)data, *width, *height);
  293.     Xfree((char *)data);
  294.     if (*pixmap == None)
  295.     return (BitmapNoMemory);
  296.     return (BitmapSuccess);
  297. }
  298.