home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / RdBitF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-09  |  7.8 KB  |  276 lines

  1. /*
  2.  * $XConsortium: RdBitF.c,v 1.8 91/03/09 16:27:55 rws Exp $
  3.  *
  4.  * Copyright 1988 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  *
  24.  * This file contains miscellaneous utility routines and is not part of the
  25.  * Xlib standard.
  26.  *
  27.  * Public entry points:
  28.  *
  29.  *     XmuReadBitmapData        read data from FILE descriptor
  30.  *     XmuReadBitmapDataFromFile    read X10 or X11 format bitmap files
  31.  *                    and return data
  32.  *
  33.  * Note that this file and ../X/XRdBitF.c look very similar....  Keep them
  34.  * that way (but don't use common source code so that people can have one 
  35.  * without the other).
  36.  */
  37.  
  38.  
  39. /*
  40.  * Based on an optimized version provided by Jim Becker, Auguest 5, 1988.
  41.  */
  42.  
  43. #include <X11/Xos.h>
  44. #include <X11/Xlib.h>
  45. #include <X11/Xutil.h>
  46. #include <stdio.h>
  47. #include <ctype.h>
  48.  
  49.  
  50. #define MAX_SIZE 255
  51.  
  52. /* shared data for the image read/parse logic */
  53. static short hexTable[256];        /* conversion value */
  54. static Bool initialized = False;    /* easier to fill in at run time */
  55.  
  56.  
  57. /*
  58.  *    Table index for the hex values. Initialized once, first time.
  59.  *    Used for translation value or delimiter significance lookup.
  60.  */
  61. static void initHexTable()
  62. {
  63.     /*
  64.      * We build the table at run time for several reasons:
  65.      *
  66.      *     1.  portable to non-ASCII machines.
  67.      *     2.  still reentrant since we set the init flag after setting table.
  68.      *     3.  easier to extend.
  69.      *     4.  less prone to bugs.
  70.      */
  71.     hexTable['0'] = 0;    hexTable['1'] = 1;
  72.     hexTable['2'] = 2;    hexTable['3'] = 3;
  73.     hexTable['4'] = 4;    hexTable['5'] = 5;
  74.     hexTable['6'] = 6;    hexTable['7'] = 7;
  75.     hexTable['8'] = 8;    hexTable['9'] = 9;
  76.     hexTable['A'] = 10;    hexTable['B'] = 11;
  77.     hexTable['C'] = 12;    hexTable['D'] = 13;
  78.     hexTable['E'] = 14;    hexTable['F'] = 15;
  79.     hexTable['a'] = 10;    hexTable['b'] = 11;
  80.     hexTable['c'] = 12;    hexTable['d'] = 13;
  81.     hexTable['e'] = 14;    hexTable['f'] = 15;
  82.  
  83.     /* delimiters of significance are flagged w/ negative value */
  84.     hexTable[' '] = -1;    hexTable[','] = -1;
  85.     hexTable['}'] = -1;    hexTable['\n'] = -1;
  86.     hexTable['\t'] = -1;
  87.     
  88.     initialized = True;
  89. }
  90.  
  91. /*
  92.  *    read next hex value in the input stream, return -1 if EOF
  93.  */
  94. static NextInt (fstream)
  95.     FILE *fstream;
  96. {
  97.     int    ch;
  98.     int    value = 0;
  99.     int gotone = 0;
  100.     int done = 0;
  101.     
  102.     /* loop, accumulate hex value until find delimiter  */
  103.     /* skip any initial delimiters found in read stream */
  104.  
  105.     while (!done) {
  106.     ch = getc(fstream);
  107.     if (ch == EOF) {
  108.         value    = -1;
  109.         done++;
  110.     } else {
  111.         /* trim high bits, check type and accumulate */
  112.         ch &= 0xff;
  113.         if (isascii(ch) && isxdigit(ch)) {
  114.         value = (value << 4) + hexTable[ch];
  115.         gotone++;
  116.         } else if ((hexTable[ch]) < 0 && gotone)
  117.           done++;
  118.     }
  119.     }
  120.     return value;
  121. }
  122.  
  123.  
  124. /*
  125.  * The data returned by the following routine is always in left-most byte
  126.  * first and left-most bit first.  If it doesn't return BitmapSuccess then
  127.  * its arguments won't have been touched.  This routine should look as much
  128.  * like the Xlib routine XReadBitmapfile as possible.
  129.  */
  130. int XmuReadBitmapData (fstream, width, height, datap, x_hot, y_hot)
  131.     FILE *fstream;            /* handle on file  */
  132.     unsigned int *width, *height;    /* RETURNED */
  133.     unsigned char **datap;        /* RETURNED */
  134.     int *x_hot, *y_hot;            /* RETURNED */
  135. {
  136.     unsigned char *data = NULL;        /* working variable */
  137.     char line[MAX_SIZE];        /* input line from file */
  138.     int size;                /* number of bytes of data */
  139.     char name_and_type[MAX_SIZE];    /* an input line */
  140.     char *type;                /* for parsing */
  141.     int value;                /* from an input line */
  142.     int version10p;            /* boolean, old format */
  143.     int padding;            /* to handle alignment */
  144.     int bytes_per_line;            /* per scanline of data */
  145.     unsigned int ww = 0;        /* width */
  146.     unsigned int hh = 0;        /* height */
  147.     int hx = -1;            /* x hotspot */
  148.     int hy = -1;            /* y hotspot */
  149.  
  150. #define Xmalloc(size) malloc(size)
  151.  
  152.     /* first time initialization */
  153.     if (initialized == False) initHexTable();
  154.  
  155.     /* error cleanup and return macro    */
  156. #define    RETURN(code) { if (data) free (data); 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.     break;
  237.     }                    /* end while */
  238.  
  239.     if (data == NULL) {
  240.     RETURN (BitmapFileInvalid);
  241.     }
  242.  
  243.     *datap = data;
  244.     data = NULL;
  245.     *width = ww;
  246.     *height = hh;
  247.     if (x_hot) *x_hot = hx;
  248.     if (y_hot) *y_hot = hy;
  249.  
  250.     RETURN (BitmapSuccess);
  251. }
  252.  
  253.  
  254. #if NeedFunctionPrototypes
  255. int XmuReadBitmapDataFromFile (_Xconst char *filename, unsigned int *width, 
  256.                    unsigned int *height, unsigned char **datap,
  257.                    int *x_hot, int *y_hot)
  258. #else
  259. int XmuReadBitmapDataFromFile (filename, width, height, datap, x_hot, y_hot)
  260.     char *filename;
  261.     unsigned int *width, *height;    /* RETURNED */
  262.     unsigned char **datap;        /* RETURNED */
  263.     int *x_hot, *y_hot;            /* RETURNED */
  264. #endif
  265. {
  266.     FILE *fstream;
  267.     int status;
  268.  
  269.     if ((fstream = fopen (filename, "r")) == NULL) {
  270.     return BitmapOpenFailed;
  271.     }
  272.     status = XmuReadBitmapData (fstream, width, height, datap, x_hot, y_hot);
  273.     fclose (fstream);
  274.     return status;
  275. }
  276.