|
|
1.1 root 1: /* Copyright, 1987, Massachusetts Institute of Technology */
2:
3: #include "copyright.h"
4:
5: #include "Xlib.h"
6: #include "Xutil.h"
7: #include "Xlibint.h"
8: #include <stdio.h>
9: #include <strings.h>
10:
11: #define MAX_LINE 1000
12:
13:
14: static cleanup(data, stream)
15: char *data;
16: FILE *stream;
17: {
18: if (data)
19: Xfree(data);
20: fclose(stream);
21: }
22:
23: int XReadBitmapFile(display, d, filename, width, height, bitmap, x_hot, y_hot)
24: Display *display;
25: Drawable d;
26: char *filename;
27: int *width, *height; /* RETURNED */
28: Pixmap *bitmap; /* RETURNED */
29: int *x_hot, *y_hot; /* RETURNED */
30: {
31: FILE *stream;
32: char *data = 0;
33: char *ptr;
34: char line[MAX_LINE];
35: int size, bytes;
36: char name_and_type[MAX_LINE];
37: char *type;
38: int value;
39: int version10p;
40: int padding;
41: int bytes_per_line;
42: int ww = 0;
43: int hh = 0;
44: int hx = -1;
45: int hy = -1;
46: Pixmap pix;
47:
48: if (!(stream = fopen(filename, "r")))
49: return(BitmapOpenFailed);
50:
51: for (;;) {
52: if (!fgets(line, MAX_LINE, stream))
53: break;
54: if (strlen(line) == MAX_LINE-1) {
55: cleanup(data, stream);
56: return(BitmapFileInvalid);
57: }
58:
59: if (sscanf(line, "#define %s %d", name_and_type, &value) == 2) {
60: if (!(type = rindex(name_and_type, '_')))
61: type = name_and_type;
62: else
63: type++;
64: if (!strcmp("width", type))
65: ww=value;
66: if (!strcmp("height", type))
67: hh=value;
68: if (!strcmp("hot", type)) {
69: if (type--==name_and_type || type--==name_and_type)
70: continue;
71: if (!strcmp("x_hot", type))
72: hx = value;
73: if (!strcmp("y_hot", type))
74: hy = value;
75: }
76: continue;
77: }
78:
79: if (sscanf(line, "static short %s = {", name_and_type) == 1)
80: version10p = 1;
81: else if (sscanf(line, "static char %s = {", name_and_type) == 1)
82: version10p = 0;
83: else continue;
84:
85: if (!(type = rindex(name_and_type, '_')))
86: type = name_and_type;
87: else
88: type++;
89: if (strcmp("bits[]", type))
90: continue;
91:
92: if (!ww || !hh) {
93: cleanup(data, stream);
94: return(BitmapFileInvalid);
95: }
96:
97: padding = 0;
98: if ((ww % 16) && ((ww % 16) < 9) && version10p)
99: padding = 1;
100:
101: bytes_per_line = (ww+7)/8 + padding;
102:
103: size = bytes_per_line * hh;
104: data = (char *) Xmalloc( size );
105: if (!data) {
106: cleanup(data, stream);
107: return(BitmapNoMemory);
108: }
109:
110: if (version10p)
111: for (bytes=0, ptr=data; bytes<size; (bytes += 2)) {
112: if (fscanf(stream, " 0x%x%*[,}]%*[ \n]", &value) != 1) {
113: cleanup(data, stream);
114: return(BitmapFileInvalid);
115: }
116: *(ptr++) = value & 0xff;
117: if (!padding || ((bytes+2) % bytes_per_line))
118: *(ptr++) = value >> 8;
119: }
120: else
121: for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) {
122: if (fscanf(stream, " 0x%x%*[,}]%*[ \n]", &value) != 1) {
123: cleanup(data, stream);
124: return(BitmapFileInvalid);
125: }
126: *ptr=value;
127: }
128:
129: }
130:
131: if (!data) {
132: cleanup(data, stream);
133: return(BitmapFileInvalid);
134: }
135:
136: pix = XCreateBitmapFromData(display, d, data, ww, hh);
137: if (!pix) {
138: cleanup(data, stream);
139: return(BitmapNoMemory);
140: }
141: *bitmap = pix;
142: *width = ww;
143: *height = hh;
144:
145: if (x_hot)
146: *x_hot = hx;
147: if (y_hot)
148: *y_hot = hy;
149:
150: cleanup(data, stream);
151: return(BitmapSuccess);
152: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.