1 /* xpm-pixmap.c --- converts XPM data to a Pixmap.
2 * xscreensaver, Copyright (c) 1998, 2001, 2002 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
21 #include "visual.h" /* for screen_number() */
23 extern char *progname;
26 #if defined(HAVE_GDK_PIXBUF)
28 # include <gdk-pixbuf/gdk-pixbuf.h>
29 # include <gdk-pixbuf/gdk-pixbuf-xlib.h>
32 /* Returns a Pixmap structure containing the bits of the given XPM image.
33 This is the gdk_pixbuf version of this function.
36 xpm_to_pixmap_1 (Display *dpy, Window window,
37 int *width_ret, int *height_ret,
43 static int initted = 0;
44 XWindowAttributes xgwa;
45 XGetWindowAttributes (dpy, window, &xgwa);
49 gdk_pixbuf_xlib_init (dpy, screen_number (xgwa.screen));
50 xlib_rgb_init (dpy, xgwa.screen);
55 ? gdk_pixbuf_new_from_file (filename)
56 : gdk_pixbuf_new_from_xpm_data ((const char **) xpm_data));
59 int w = gdk_pixbuf_get_width (pb);
60 int h = gdk_pixbuf_get_height (pb);
63 /* #### Note that this always uses the default colormap! Morons!
64 Owen says that in Gnome 2.0, I should try using
65 gdk_pixbuf_render_pixmap_and_mask_for_colormap() instead.
66 But I don't have Gnome 2.0 yet.
68 gdk_pixbuf_xlib_render_pixmap_and_mask (pb, &pixmap, mask_ret, 128);
72 fprintf (stderr, "%s: out of memory (%d x %d)\n", progname, w, h);
75 /* gdk_pixbuf_unref (pb); -- #### does doing this free colors? */
77 if (width_ret) *width_ret = w;
78 if (height_ret) *height_ret = h;
84 fprintf (stderr, "%s: unable to load %s\n", progname, filename);
89 fprintf (stderr, "%s: unable to initialize built-in images\n", progname);
95 #elif defined(HAVE_XPM)
97 #include <X11/Intrinsic.h>
98 #include <X11/Xutil.h>
103 # include <X11/Xmu/Drawing.h>
105 # include <Xmu/Drawing.h>
110 #define countof(x) (sizeof((x))/sizeof((*x)))
114 handle_xpm_error (const char *filename, int status)
116 if (!filename) filename = "builtin";
123 fprintf (stderr, "%s: %s: warning: color substitution performed\n",
131 fprintf (stderr, "%s: %s: no such file\n", progname, filename);
134 fprintf (stderr, "%s: %s: out of memory\n", progname, filename);
137 fprintf (stderr, "%s: %s: color allocation failed\n",
141 fprintf (stderr, "%s: %s: unknown XPM error %d\n", progname,
149 /* The libxpm version of this function...
152 xpm_to_pixmap_1 (Display *dpy, Window window,
153 int *width_ret, int *height_ret,
155 const char *filename,
159 XpmAttributes xpmattrs;
163 XWindowAttributes xgwa;
164 XGetWindowAttributes (dpy, window, &xgwa);
166 memset (&xpm_image, 0, sizeof(xpm_image));
167 memset (&xpm_info, 0, sizeof(xpm_info));
172 status = XpmReadFileToData ((char *) filename, &xpm_data);
173 if (handle_xpm_error (filename, status))
177 xpmattrs.valuemask = 0;
180 xpmattrs.valuemask |= XpmCloseness;
181 xpmattrs.closeness = 40000;
184 xpmattrs.valuemask |= XpmVisual;
185 xpmattrs.visual = xgwa.visual;
188 xpmattrs.valuemask |= XpmDepth;
189 xpmattrs.depth = xgwa.depth;
192 xpmattrs.valuemask |= XpmColormap;
193 xpmattrs.colormap = xgwa.colormap;
196 status = XpmCreatePixmapFromData (dpy, window, xpm_data,
197 &pixmap, mask_ret, &xpmattrs);
198 if (handle_xpm_error (filename, status))
202 if (width_ret) *width_ret = xpmattrs.width;
203 if (height_ret) *height_ret = xpmattrs.height;
211 unsigned long fg = BlackPixelOfScreen (xgwa.screen);
212 unsigned long bg = WhitePixelOfScreen (xgwa.screen);
215 pixmap = XmuLocateBitmapFile (xgwa.screen, filename,
216 0, 0, width_ret, height_ret, &xh, &yh);
219 fprintf (stderr, "%s: couldn't find XBM %s\n", progname,
223 b2 = XmuCreatePixmapFromBitmap (dpy, window, pixmap,
224 *width_ret, *height_ret,
226 XFreePixmap (dpy, pixmap);
231 fprintf (stderr, "%s: couldn't load XBM %s\n", progname, filename);
238 "%s: your vendor doesn't ship the standard Xmu library.\n",
240 fprintf (stderr, "\tWe can't load XBM files without it.\n");
248 #else /* !HAVE_XPM && !HAVE_GDK_PIXBUF */
251 xpm_to_pixmap_1 (Display *dpy, Window window,
252 int *width_ret, int *height_ret,
254 const char *filename,
257 fprintf(stderr, "%s: not compiled with XPM or Pixbuf support.\n", progname);
261 #endif /* !HAVE_XPM */
265 xpm_data_to_pixmap (Display *dpy, Window window, char **xpm_data,
266 int *width_ret, int *height_ret,
269 return xpm_to_pixmap_1 (dpy, window, width_ret, height_ret, mask_ret,
275 xpm_file_to_pixmap (Display *dpy, Window window, const char *filename,
276 int *width_ret, int *height_ret,
279 return xpm_to_pixmap_1 (dpy, window, width_ret, height_ret, mask_ret,