RdBitF.c revision 258a0ebe
11ab64890Smrg/* 21ab64890Smrg 31ab64890SmrgCopyright 1987, 1998 The Open Group 41ab64890Smrg 51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its 61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that 71ab64890Smrgthe above copyright notice appear in all copies and that both that 81ab64890Smrgcopyright notice and this permission notice appear in supporting 91ab64890Smrgdocumentation. 101ab64890Smrg 111ab64890SmrgThe above copyright notice and this permission notice shall be included 121ab64890Smrgin all copies or substantial portions of the Software. 131ab64890Smrg 141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151ab64890SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161ab64890SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 171ab64890SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 181ab64890SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 191ab64890SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 201ab64890SmrgOTHER DEALINGS IN THE SOFTWARE. 211ab64890Smrg 221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall 231ab64890Smrgnot be used in advertising or otherwise to promote the sale, use or 241ab64890Smrgother dealings in this Software without prior written authorization 251ab64890Smrgfrom The Open Group. 261ab64890Smrg 271ab64890Smrg*/ 281ab64890Smrg 291ab64890Smrg/* 3061b2299dSmrg * Code to read bitmaps from disk files. Interprets 311ab64890Smrg * data from X10 and X11 bitmap files and creates 321ab64890Smrg * Pixmap representations of files. Returns Pixmap 331ab64890Smrg * ID and specifics about image. 341ab64890Smrg * 351ab64890Smrg * Modified for speedup by Jim Becker, changed image 3661b2299dSmrg * data parsing logic (removed some fscanf()s). 371ab64890Smrg * Aug 5, 1988 381ab64890Smrg * 391ab64890Smrg * Note that this file and ../Xmu/RdBitF.c look very similar.... Keep them 4061b2299dSmrg * that way (but don't use common source code so that people can have one 411ab64890Smrg * without the other). 421ab64890Smrg */ 431ab64890Smrg 441ab64890Smrg#ifdef HAVE_CONFIG_H 451ab64890Smrg#include <config.h> 461ab64890Smrg#endif 471ab64890Smrg#include "Xlibint.h" 481ab64890Smrg#include <X11/Xos.h> 491ab64890Smrg#include "Xutil.h" 501ab64890Smrg#include <stdio.h> 511ab64890Smrg#include <ctype.h> 52258a0ebeSmrg#include "reallocarray.h" 531ab64890Smrg 541ab64890Smrg#define MAX_SIZE 255 551ab64890Smrg 561ab64890Smrg/* shared data for the image read/parse logic */ 571ab64890Smrgstatic const short hexTable[256] = { 581ab64890Smrg ['0'] = 0, ['1'] = 1, 591ab64890Smrg ['2'] = 2, ['3'] = 3, 601ab64890Smrg ['4'] = 4, ['5'] = 5, 611ab64890Smrg ['6'] = 6, ['7'] = 7, 621ab64890Smrg ['8'] = 8, ['9'] = 9, 631ab64890Smrg ['A'] = 10, ['B'] = 11, 641ab64890Smrg ['C'] = 12, ['D'] = 13, 651ab64890Smrg ['E'] = 14, ['F'] = 15, 661ab64890Smrg ['a'] = 10, ['b'] = 11, 671ab64890Smrg ['c'] = 12, ['d'] = 13, 681ab64890Smrg ['e'] = 14, ['f'] = 15, 691ab64890Smrg 701ab64890Smrg [' '] = -1, [','] = -1, 711ab64890Smrg ['}'] = -1, ['\n'] = -1, 721ab64890Smrg ['\t'] = -1 731ab64890Smrg}; 741ab64890Smrg 751ab64890Smrg/* 761ab64890Smrg * read next hex value in the input stream, return -1 if EOF 771ab64890Smrg */ 781ab64890Smrgstatic int 791ab64890SmrgNextInt ( 801ab64890Smrg FILE *fstream) 811ab64890Smrg{ 821ab64890Smrg int ch; 831ab64890Smrg int value = 0; 841ab64890Smrg int gotone = 0; 851ab64890Smrg int done = 0; 8661b2299dSmrg 871ab64890Smrg /* loop, accumulate hex value until find delimiter */ 881ab64890Smrg /* skip any initial delimiters found in read stream */ 891ab64890Smrg 901ab64890Smrg while (!done) { 911ab64890Smrg ch = getc(fstream); 921ab64890Smrg if (ch == EOF) { 931ab64890Smrg value = -1; 941ab64890Smrg done++; 951ab64890Smrg } else { 961ab64890Smrg /* trim high bits, check type and accumulate */ 971ab64890Smrg ch &= 0xff; 981ab64890Smrg if (isascii(ch) && isxdigit(ch)) { 991ab64890Smrg value = (value << 4) + hexTable[ch]; 1001ab64890Smrg gotone++; 1011ab64890Smrg } else if ((hexTable[ch]) < 0 && gotone) 1021ab64890Smrg done++; 1031ab64890Smrg } 1041ab64890Smrg } 1051ab64890Smrg return value; 1061ab64890Smrg} 1071ab64890Smrg 1081ab64890Smrgint 1091ab64890SmrgXReadBitmapFileData ( 1101ab64890Smrg _Xconst char *filename, 1111ab64890Smrg unsigned int *width, /* RETURNED */ 1121ab64890Smrg unsigned int *height, /* RETURNED */ 1131ab64890Smrg unsigned char **data, /* RETURNED */ 1141ab64890Smrg int *x_hot, /* RETURNED */ 1151ab64890Smrg int *y_hot) /* RETURNED */ 1161ab64890Smrg{ 1171ab64890Smrg FILE *fstream; /* handle on file */ 1181ab64890Smrg unsigned char *bits = NULL; /* working variable */ 1191ab64890Smrg char line[MAX_SIZE]; /* input line from file */ 1201ab64890Smrg int size; /* number of bytes of data */ 1211ab64890Smrg char name_and_type[MAX_SIZE]; /* an input line */ 1221ab64890Smrg char *type; /* for parsing */ 1231ab64890Smrg int value; /* from an input line */ 1241ab64890Smrg int version10p; /* boolean, old format */ 1251ab64890Smrg int padding; /* to handle alignment */ 1261ab64890Smrg int bytes_per_line; /* per scanline of data */ 1271ab64890Smrg unsigned int ww = 0; /* width */ 1281ab64890Smrg unsigned int hh = 0; /* height */ 1291ab64890Smrg int hx = -1; /* x hotspot */ 1301ab64890Smrg int hy = -1; /* y hotspot */ 1311ab64890Smrg 1321ab64890Smrg#ifdef __UNIXOS2__ 1331ab64890Smrg filename = __XOS2RedirRoot(filename); 1341ab64890Smrg#endif 1351ab64890Smrg if (!(fstream = fopen(filename, "r"))) 1361ab64890Smrg return BitmapOpenFailed; 1371ab64890Smrg 1381ab64890Smrg /* error cleanup and return macro */ 1391ab64890Smrg#define RETURN(code) \ 1400f8248bfSmrg{ Xfree (bits); fclose (fstream); return code; } 1411ab64890Smrg 1421ab64890Smrg while (fgets(line, MAX_SIZE, fstream)) { 1431ab64890Smrg if (strlen(line) == MAX_SIZE-1) 1441ab64890Smrg RETURN (BitmapFileInvalid); 1451ab64890Smrg if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) { 1461ab64890Smrg if (!(type = strrchr(name_and_type, '_'))) 1471ab64890Smrg type = name_and_type; 1481ab64890Smrg else 1491ab64890Smrg type++; 1501ab64890Smrg 1511ab64890Smrg if (!strcmp("width", type)) 1521ab64890Smrg ww = (unsigned int) value; 1531ab64890Smrg if (!strcmp("height", type)) 1541ab64890Smrg hh = (unsigned int) value; 1551ab64890Smrg if (!strcmp("hot", type)) { 1561ab64890Smrg if (type-- == name_and_type || type-- == name_and_type) 1571ab64890Smrg continue; 1581ab64890Smrg if (!strcmp("x_hot", type)) 1591ab64890Smrg hx = value; 1601ab64890Smrg if (!strcmp("y_hot", type)) 1611ab64890Smrg hy = value; 1621ab64890Smrg } 1631ab64890Smrg continue; 1641ab64890Smrg } 16561b2299dSmrg 1661ab64890Smrg if (sscanf(line, "static short %s = {", name_and_type) == 1) 1671ab64890Smrg version10p = 1; 1681ab64890Smrg else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1) 1691ab64890Smrg version10p = 0; 1701ab64890Smrg else if (sscanf(line, "static char %s = {", name_and_type) == 1) 1711ab64890Smrg version10p = 0; 1721ab64890Smrg else 1731ab64890Smrg continue; 1741ab64890Smrg 1751ab64890Smrg if (!(type = strrchr(name_and_type, '_'))) 1761ab64890Smrg type = name_and_type; 1771ab64890Smrg else 1781ab64890Smrg type++; 1791ab64890Smrg 1801ab64890Smrg if (strcmp("bits[]", type)) 1811ab64890Smrg continue; 18261b2299dSmrg 1831ab64890Smrg if (!ww || !hh) 1841ab64890Smrg RETURN (BitmapFileInvalid); 1851ab64890Smrg 1861ab64890Smrg if ((ww % 16) && ((ww % 16) < 9) && version10p) 1871ab64890Smrg padding = 1; 1881ab64890Smrg else 1891ab64890Smrg padding = 0; 1901ab64890Smrg 1911ab64890Smrg bytes_per_line = (ww+7)/8 + padding; 1921ab64890Smrg 193258a0ebeSmrg bits = Xmallocarray (hh, bytes_per_line); 19461b2299dSmrg if (!bits) 1951ab64890Smrg RETURN (BitmapNoMemory); 196258a0ebeSmrg size = bytes_per_line * hh; 1971ab64890Smrg 1981ab64890Smrg if (version10p) { 1991ab64890Smrg unsigned char *ptr; 2001ab64890Smrg int bytes; 2011ab64890Smrg 2021ab64890Smrg for (bytes=0, ptr=bits; bytes<size; (bytes += 2)) { 2031ab64890Smrg if ((value = NextInt(fstream)) < 0) 2041ab64890Smrg RETURN (BitmapFileInvalid); 2051ab64890Smrg *(ptr++) = value; 2061ab64890Smrg if (!padding || ((bytes+2) % bytes_per_line)) 2071ab64890Smrg *(ptr++) = value >> 8; 2081ab64890Smrg } 2091ab64890Smrg } else { 2101ab64890Smrg unsigned char *ptr; 2111ab64890Smrg int bytes; 2121ab64890Smrg 2131ab64890Smrg for (bytes=0, ptr=bits; bytes<size; bytes++, ptr++) { 21461b2299dSmrg if ((value = NextInt(fstream)) < 0) 2151ab64890Smrg RETURN (BitmapFileInvalid); 2161ab64890Smrg *ptr=value; 2171ab64890Smrg } 2181ab64890Smrg } 2196cc2b21fSmrg 2206cc2b21fSmrg /* If we got to this point, we read a full bitmap file. Break so we don't 2216cc2b21fSmrg * start reading another one from the same file and leak the memory 2226cc2b21fSmrg * allocated for the previous one. */ 2236cc2b21fSmrg break; 2241ab64890Smrg } /* end while */ 2251ab64890Smrg 2261ab64890Smrg fclose(fstream); 2271ab64890Smrg if (!bits) 2281ab64890Smrg return (BitmapFileInvalid); 2291ab64890Smrg 2301ab64890Smrg *data = bits; 2311ab64890Smrg *width = ww; 2321ab64890Smrg *height = hh; 2331ab64890Smrg if (x_hot) *x_hot = hx; 2341ab64890Smrg if (y_hot) *y_hot = hy; 2351ab64890Smrg 2361ab64890Smrg return (BitmapSuccess); 2371ab64890Smrg} 2381ab64890Smrg 2391ab64890Smrgint 2401ab64890SmrgXReadBitmapFile ( 2411ab64890Smrg Display *display, 2421ab64890Smrg Drawable d, 2431ab64890Smrg _Xconst char *filename, 2441ab64890Smrg unsigned int *width, /* RETURNED */ 2451ab64890Smrg unsigned int *height, /* RETURNED */ 2461ab64890Smrg Pixmap *pixmap, /* RETURNED */ 2471ab64890Smrg int *x_hot, /* RETURNED */ 2481ab64890Smrg int *y_hot) /* RETURNED */ 2491ab64890Smrg{ 2501ab64890Smrg unsigned char *data; 2511ab64890Smrg int res; 2521ab64890Smrg 2531ab64890Smrg res = XReadBitmapFileData(filename, width, height, &data, x_hot, y_hot); 2541ab64890Smrg if (res != BitmapSuccess) 2551ab64890Smrg return res; 2561ab64890Smrg *pixmap = XCreateBitmapFromData(display, d, (char *)data, *width, *height); 257818534a1Smrg Xfree(data); 2581ab64890Smrg if (*pixmap == None) 2591ab64890Smrg return (BitmapNoMemory); 2601ab64890Smrg return (BitmapSuccess); 2611ab64890Smrg} 262