RdBitF.c File Reference

#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>
#include <stdio.h>
#include <ctype.h>
#include <X11/Xmu/Drawing.h>

Include dependency graph for RdBitF.c:

Go to the source code of this file.

Defines

#define MAX_SIZE   255
#define Xmalloc(size)   malloc(size)
#define RETURN(code)   { if (data) free (data); return code; }
#define fopen_file   fopen

Functions

static void initHexTable (void)
static int NextInt (FILE *)
int XmuReadBitmapData (FILE *fstream, unsigned int *width, unsigned int *height, unsigned char **datap, int *x_hot, int *y_hot)
int XmuReadBitmapDataFromFile (_Xconst char *filename, unsigned int *width, unsigned int *height, unsigned char **datap, int *x_hot, int *y_hot)

Variables

static short hexTable [256]
static Bool initialized = False


Define Documentation

#define fopen_file   fopen

Definition at line 377 of file RdBitF.c.

Referenced by XmuReadBitmapDataFromFile().

#define MAX_SIZE   255

Definition at line 58 of file RdBitF.c.

Referenced by XmuReadBitmapData().

#define RETURN ( code   )     { if (data) free (data); return code; }

Referenced by XmuReadBitmapData().

#define Xmalloc ( size   )     malloc(size)

Referenced by XmuReadBitmapData().


Function Documentation

static void initHexTable ( void   )  [static]

Definition at line 76 of file RdBitF.c.

References hexTable, and initialized.

Referenced by XmuReadBitmapData().

00077 {
00078     /*
00079      * We build the table at run time for several reasons:
00080      *
00081      *     1.  portable to non-ASCII machines.
00082      *     2.  still reentrant since we set the init flag after setting table.
00083      *     3.  easier to extend.
00084      *     4.  less prone to bugs.
00085      */
00086     hexTable['0'] = 0;  hexTable['1'] = 1;
00087     hexTable['2'] = 2;  hexTable['3'] = 3;
00088     hexTable['4'] = 4;  hexTable['5'] = 5;
00089     hexTable['6'] = 6;  hexTable['7'] = 7;
00090     hexTable['8'] = 8;  hexTable['9'] = 9;
00091     hexTable['A'] = 10; hexTable['B'] = 11;
00092     hexTable['C'] = 12; hexTable['D'] = 13;
00093     hexTable['E'] = 14; hexTable['F'] = 15;
00094     hexTable['a'] = 10; hexTable['b'] = 11;
00095     hexTable['c'] = 12; hexTable['d'] = 13;
00096     hexTable['e'] = 14; hexTable['f'] = 15;
00097 
00098     /* delimiters of significance are flagged w/ negative value */
00099     hexTable[' '] = -1; hexTable[','] = -1;
00100     hexTable['}'] = -1; hexTable['\n'] = -1;
00101     hexTable['\t'] = -1;
00102         
00103     initialized = True;
00104 }

Here is the caller graph for this function:

static int NextInt ( FILE *   )  [static]

Definition at line 110 of file RdBitF.c.

References done, hexTable, and value.

Referenced by XmuReadBitmapData().

00111 {
00112     int ch;
00113     int value = 0;
00114     int gotone = 0;
00115     int done = 0;
00116     
00117     /* loop, accumulate hex value until find delimiter  */
00118     /* skip any initial delimiters found in read stream */
00119 
00120     while (!done) {
00121         ch = getc(fstream);
00122         if (ch == EOF) {
00123             value       = -1;
00124             done++;
00125         } else {
00126             /* trim high bits, check type and accumulate */
00127             ch &= 0xff;
00128             if (isascii(ch) && isxdigit(ch)) {
00129                 value = (value << 4) + hexTable[ch];
00130                 gotone++;
00131             } else if ((hexTable[ch]) < 0 && gotone)
00132               done++;
00133         }
00134     }
00135     return value;
00136 }

Here is the caller graph for this function:

int XmuReadBitmapData ( FILE *  fstream,
unsigned int width,
unsigned int height,
unsigned char **  datap,
int x_hot,
int y_hot 
)

Definition at line 146 of file RdBitF.c.

References data, initHexTable(), initialized, int, line, MAX_SIZE, NextInt(), NULL, RETURN, size, type, value, and Xmalloc.

Referenced by XmuReadBitmapDataFromFile().

00148 {
00149     unsigned char *data = NULL;         /* working variable */
00150     char line[MAX_SIZE];                /* input line from file */
00151     int size;                           /* number of bytes of data */
00152     char name_and_type[MAX_SIZE];       /* an input line */
00153     char *type;                         /* for parsing */
00154     int value;                          /* from an input line */
00155     int version10p;                     /* boolean, old format */
00156     int padding;                        /* to handle alignment */
00157     int bytes_per_line;                 /* per scanline of data */
00158     unsigned int ww = 0;                /* width */
00159     unsigned int hh = 0;                /* height */
00160     int hx = -1;                        /* x hotspot */
00161     int hy = -1;                        /* y hotspot */
00162 
00163 #undef  Xmalloc /* see MALLOC_0_RETURNS_NULL in Xlibint.h */
00164 #define Xmalloc(size) malloc(size)
00165 
00166     /* first time initialization */
00167     if (initialized == False) initHexTable();
00168 
00169     /* error cleanup and return macro   */
00170 #define RETURN(code) { if (data) free (data); return code; }
00171 
00172     while (fgets(line, MAX_SIZE, fstream)) {
00173         if (strlen(line) == MAX_SIZE-1) {
00174             RETURN (BitmapFileInvalid);
00175         }
00176         if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
00177             if (!(type = strrchr(name_and_type, '_')))
00178               type = name_and_type;
00179             else
00180               type++;
00181 
00182             if (!strcmp("width", type))
00183               ww = (unsigned int) value;
00184             if (!strcmp("height", type))
00185               hh = (unsigned int) value;
00186             if (!strcmp("hot", type)) {
00187                 if (type-- == name_and_type || type-- == name_and_type)
00188                   continue;
00189                 if (!strcmp("x_hot", type))
00190                   hx = value;
00191                 if (!strcmp("y_hot", type))
00192                   hy = value;
00193             }
00194             continue;
00195         }
00196     
00197         if (sscanf(line, "static short %s = {", name_and_type) == 1)
00198           version10p = 1;
00199         else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
00200           version10p = 0;
00201         else if (sscanf(line, "static char %s = {", name_and_type) == 1)
00202           version10p = 0;
00203         else
00204           continue;
00205 
00206         if (!(type = strrchr(name_and_type, '_')))
00207           type = name_and_type;
00208         else
00209           type++;
00210 
00211         if (strcmp("bits[]", type))
00212           continue;
00213     
00214         if (!ww || !hh)
00215           RETURN (BitmapFileInvalid);
00216 
00217         if ((ww % 16) && ((ww % 16) < 9) && version10p)
00218           padding = 1;
00219         else
00220           padding = 0;
00221 
00222         bytes_per_line = (ww+7)/8 + padding;
00223 
00224         size = bytes_per_line * hh;
00225         data = (unsigned char *) Xmalloc ((unsigned int) size);
00226         if (!data) 
00227           RETURN (BitmapNoMemory);
00228 
00229         if (version10p) {
00230             unsigned char *ptr;
00231             int bytes;
00232 
00233             for (bytes=0, ptr=data; bytes<size; (bytes += 2)) {
00234                 if ((value = NextInt(fstream)) < 0)
00235                   RETURN (BitmapFileInvalid);
00236                 *(ptr++) = value;
00237                 if (!padding || ((bytes+2) % bytes_per_line))
00238                   *(ptr++) = value >> 8;
00239             }
00240         } else {
00241             unsigned char *ptr;
00242             int bytes;
00243 
00244             for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) {
00245                 if ((value = NextInt(fstream)) < 0) 
00246                   RETURN (BitmapFileInvalid);
00247                 *ptr=value;
00248             }
00249         }
00250         break;
00251     }                                   /* end while */
00252 
00253     if (data == NULL) {
00254         RETURN (BitmapFileInvalid);
00255     }
00256 
00257     *datap = data;
00258     data = NULL;
00259     *width = ww;
00260     *height = hh;
00261     if (x_hot) *x_hot = hx;
00262     if (y_hot) *y_hot = hy;
00263 
00264     RETURN (BitmapSuccess);
00265 }

Here is the call graph for this function:

Here is the caller graph for this function:

int XmuReadBitmapDataFromFile ( _Xconst char *  filename,
unsigned int width,
unsigned int height,
unsigned char **  datap,
int x_hot,
int y_hot 
)

Definition at line 382 of file RdBitF.c.

References fopen_file, NULL, and XmuReadBitmapData().

Referenced by XmuCvtStringToBitmap(), and XmuLocatePixmapFile().

00385 {
00386     FILE *fstream;
00387     int status;
00388 
00389 #ifdef __UNIXOS2__
00390     filename = __XOS2RedirRoot(filename);
00391 #endif
00392     if ((fstream = fopen_file (filename, "r")) == NULL) {
00393         return BitmapOpenFailed;
00394     }
00395     status = XmuReadBitmapData(fstream, width, height, datap, x_hot, y_hot);
00396     fclose (fstream);
00397     return status;
00398 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

short hexTable[256] [static]

Definition at line 67 of file RdBitF.c.

Referenced by initHexTable(), and NextInt().

Bool initialized = False [static]

Definition at line 68 of file RdBitF.c.

Referenced by initHexTable(), and XmuReadBitmapData().


Generated on Sun Mar 4 16:04:16 2007 for Scilab [trunk] by  doxygen 1.5.1