1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | #ifdef HAVE_CONFIG_H1 |
30 | #include <config.h> |
31 | #endif |
32 | #include "Xlibint.h" |
33 | #include <X11/Xos.h> |
34 | #include "Xutil.h" |
35 | #include <stdio.h> |
36 | |
37 | #define ERR_RETURN((void*)0) NULL((void*)0) |
38 | |
39 | static char *Format_Image( |
40 | XImage *image, |
41 | int *resultsize) |
42 | { |
43 | register int x, c, b; |
44 | register char *ptr; |
45 | int y; |
46 | char *data; |
47 | int width, height; |
48 | int bytes_per_line; |
49 | |
50 | width = image->width; |
51 | height = image->height; |
52 | |
53 | bytes_per_line = (width+7)/8; |
54 | *resultsize = bytes_per_line * height; |
55 | |
56 | data = Xmalloc( *resultsize )malloc(((*resultsize) == 0 ? 1 : (*resultsize))); |
57 | if (!data) |
58 | return(ERR_RETURN((void*)0)); |
59 | |
60 | |
61 | |
62 | |
63 | ptr = data; |
64 | c = 0; b=1; |
65 | for (y=0; y<height; y++) { |
66 | for (x=0; x<width;) { |
67 | if (XGetPixel(image, x, y)((*((image)->f.get_pixel))((image), (x), (y)))) |
68 | c |= b; |
69 | b <<= 1; |
70 | if (!(++x & 7)) { |
71 | *(ptr++)=c; |
72 | c=0; b=1; |
73 | } |
74 | } |
75 | if (x & 7) { |
76 | *(ptr++)=c; |
77 | c=0; b=1; |
78 | } |
79 | } |
80 | |
81 | return(data); |
82 | } |
83 | |
84 | #define BYTES_PER_OUTPUT_LINE12 12 |
85 | |
86 | int |
87 | XWriteBitmapFile( |
88 | Display *display, |
89 | _Xconstconst char *filename, |
90 | Pixmap bitmap, |
91 | unsigned int width, |
92 | unsigned int height, |
93 | int x_hot, |
94 | int y_hot) |
95 | { |
96 | char *data, *ptr; |
97 | int size, byte; |
98 | int c; |
99 | XImage *image; |
100 | FILE *stream; |
101 | char *name; |
102 | |
103 | if (!(name = strrchr(filename, '/'))) |
| 1 | Assuming 'name' is not null | |
|
| |
104 | name = (char *)filename; |
105 | else |
106 | name++; |
107 | |
108 | #ifdef __UNIXOS2__ |
109 | filename = (char*)__XOS2RedirRoot(filename); |
110 | #endif |
111 | if (!(stream = fopen(filename, "w"))) |
| 3 | | Assuming 'stream' is not null | |
|
| |
112 | return(BitmapOpenFailed1); |
113 | |
114 | |
115 | image = XGetImage(display, bitmap, 0,0,width, height, 1L, XYPixmap1); |
116 | if (!image) { |
| 5 | | Assuming 'image' is non-null | |
|
| |
117 | fclose(stream); |
118 | return(4); |
119 | } |
120 | |
121 | |
122 | data = Format_Image(image, &size); |
123 | XDestroyImage(image)((*((image)->f.destroy_image))((image))); |
124 | if (!data) { |
| |
125 | fclose(stream); |
126 | return(BitmapNoMemory3); |
127 | } |
128 | |
129 | |
130 | fprintf(stream, "#define %s_width %d\n", name, width); |
131 | fprintf(stream, "#define %s_height %d\n", name, height); |
132 | if (x_hot != -1) { |
| |
133 | fprintf(stream, "#define %s_x_hot %d\n", name, x_hot); |
134 | fprintf(stream, "#define %s_y_hot %d\n", name, y_hot); |
135 | } |
136 | |
137 | |
138 | fprintf(stream, "static unsigned char %s_bits[] = {", name); |
139 | for (byte=0, ptr=data; byte<size; byte++, ptr++) { |
| 9 | | Assuming 'byte' is < 'size' | |
|
| 10 | | Loop condition is true. Entering loop body | |
|
140 | if (!byte) |
| |
141 | fprintf(stream, "\n "); |
142 | else if (!(byte % BYTES_PER_OUTPUT_LINE12)) |
143 | fprintf(stream, ",\n "); |
144 | else |
145 | fprintf(stream, ", "); |
146 | c = *ptr; |
| 12 | | Assigned value is garbage or undefined |
|
147 | if (c<0) |
148 | c += 256; |
149 | fprintf(stream, "0x%02x", c); |
150 | } |
151 | fprintf(stream, "};\n"); |
152 | |
153 | Xfree(data)free((data)); |
154 | fclose(stream); |
155 | return(BitmapSuccess0); |
156 | } |