1 /* xscreensaver, Copyright (c) 2001-2006 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* I implemented this subset of libXPM here because I don't want the
13 xscreensaver daemon to depend on libXPM for two reasons: first,
14 because I want the logo to show up even if libXPM is not installed
15 on the system; and second, I don't want to have to security-audit
16 libXPM. The fewer libraries that are linked into the xscreensaver
17 daemon, the more likely to be secure it is.
19 Also, the Cocoa port uses this code since libXPM isn't available
33 #else /* !HAVE_COCOA - real Xlib */
34 # include <X11/Xlib.h>
35 # include <X11/Xutil.h>
36 #endif /* !HAVE_COCOA */
40 extern const char *progname;
45 union { int i; char c[sizeof(int)]; } u;
50 static const char hex[128] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
54 0, 10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 10,11,12,13,14,15,0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
60 minixpm_to_ximage (Display *dpy, Visual *visual, Colormap colormap, int depth,
61 unsigned long transparent_color,
62 const char * const * data,
63 int *width_ret, int *height_ret,
64 unsigned long **pixels_ret, int *npixels_ret,
65 unsigned char **mask_ret)
67 int w, w8, h, ncolors, nbytes;
69 int x, y, i, pixel_count;
72 int cr; int cg; int cb;
73 int mr; int mg; int mb;
75 unsigned char rmap[256];
77 unsigned long *pixels;
80 memset (cmap, 0, sizeof(cmap)); /* avoid warnings */
82 if (4 != sscanf ((const char *) *data,
83 "%d %d %d %d %c", &w, &h, &ncolors, &nbytes, &c)) {
84 fprintf (stderr, "%s: unparsable XPM header\n", progname);
88 if (ncolors < 1 || ncolors > 255) {
89 fprintf (stderr, "%s: XPM: ncolors is %d\n", progname, ncolors);
93 fprintf (stderr, "%s: %d-byte XPM files not supported\n",
99 for (i = 0; i < ncolors; i++)
101 const char *line = *data;
102 cmap[i].byte = *line++;
107 while (*line == ' ' || *line == '\t')
110 if (!which) continue; /* whitespace at end of line */
112 if (which != 'c' && which != 'm') {
113 fprintf (stderr, "%s: unknown XPM pixel type '%c' in \"%s\"\n",
114 progname, which, *data);
117 while (*line == ' ' || *line == '\t')
119 if (!strncasecmp(line, "None", 4))
127 fprintf (stderr, "%s: unparsable XPM color spec: \"%s\"\n",
133 r = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
134 g = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
135 b = (hex[(int) line[0]] << 4) | hex[(int) line[1]]; line += 2;
155 if (depth == 1) transparent_color = 1;
157 pixels = (unsigned long *) calloc (ncolors+1, sizeof(*pixels));
159 for (i = 0; i < ncolors; i++)
161 if (cmap[i].cr == -1) /* transparent */
163 rmap[(int) cmap[i].byte] = 255;
168 color.flags = DoRed|DoGreen|DoBlue;
169 color.red = (cmap[i].cr << 8) | cmap[i].cr;
170 color.green = (cmap[i].cg << 8) | cmap[i].cg;
171 color.blue = (cmap[i].cb << 8) | cmap[i].cb;
173 !XAllocColor (dpy, colormap, &color))
175 color.red = (cmap[i].mr << 8) | cmap[i].mr;
176 color.green = (cmap[i].mg << 8) | cmap[i].mg;
177 color.blue = (cmap[i].mb << 8) | cmap[i].mb;
178 if (!XAllocColor (dpy, colormap, &color)) {
179 fprintf (stderr, "%s: unable to allocate XPM color\n",
184 pixels[pixel_count] = color.pixel;
185 rmap[(int) cmap[i].byte] = pixel_count;
190 ximage = XCreateImage (dpy, visual, depth,
191 (depth == 1 ? XYBitmap : ZPixmap),
193 if (! ximage) return 0;
195 ximage->bitmap_bit_order =
197 (bigendian() ? MSBFirst : LSBFirst);
199 ximage->data = (char *) calloc (ximage->height, ximage->bytes_per_line);
201 XDestroyImage (ximage);
208 int s = (w8 * h) + 1;
209 *mask_ret = (unsigned char *) malloc (s);
213 memset (*mask_ret, 255, s);
216 for (y = 0; y < h; y++)
218 const char *line = *data++;
219 for (x = 0; x < w; x++)
221 int p = rmap[(int) *line];
223 XPutPixel (ximage, x, y, (p == 255 ? transparent_color : pixels[p]));
225 if (p == 255 && mask_ret)
226 (*mask_ret)[(y * w8) + (x >> 3)] &= (~(1 << (x & 7)));
232 *pixels_ret = pixels;
233 *npixels_ret = pixel_count;