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>
31 # include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
32 # else /* !HAVE_GTK2 */
33 # include <gdk-pixbuf/gdk-pixbuf-xlib.h>
34 # endif /* !HAVE_GTK2 */
37 /* Returns a Pixmap structure containing the bits of the given XPM image.
38 This is the gdk_pixbuf version of this function.
41 xpm_to_pixmap_1 (Display *dpy, Window window,
42 int *width_ret, int *height_ret,
48 static int initted = 0;
49 XWindowAttributes xgwa;
52 #endif /* HAVE_GTK2 */
53 XGetWindowAttributes (dpy, window, &xgwa);
59 #endif /* HAVE_GTK2 */
60 gdk_pixbuf_xlib_init (dpy, screen_number (xgwa.screen));
61 xlib_rgb_init (dpy, xgwa.screen);
67 ? gdk_pixbuf_new_from_file (filename, &gerr)
68 #else /* !HAVE_GTK2 */
69 ? gdk_pixbuf_new_from_file (filename)
70 #endif /* !HAVE_GTK2 */
71 : gdk_pixbuf_new_from_xpm_data ((const char **) xpm_data));
74 int w = gdk_pixbuf_get_width (pb);
75 int h = gdk_pixbuf_get_height (pb);
78 /* #### Note that this always uses the default colormap! Morons!
79 Owen says that in Gnome 2.0, I should try using
80 gdk_pixbuf_render_pixmap_and_mask_for_colormap() instead.
81 But I don't have Gnome 2.0 yet.
83 gdk_pixbuf_xlib_render_pixmap_and_mask (pb, &pixmap, mask_ret, 128);
87 fprintf (stderr, "%s: out of memory (%d x %d)\n", progname, w, h);
90 /* gdk_pixbuf_unref (pb); -- #### does doing this free colors? */
92 if (width_ret) *width_ret = w;
93 if (height_ret) *height_ret = h;
100 fprintf (stderr, "%s: %s\n", progname, gerr->message);
102 #else /* !HAVE_GTK2 */
103 fprintf (stderr, "%s: unable to load %s\n", progname, filename);
104 #endif /* !HAVE_GTK2 */
109 fprintf (stderr, "%s: unable to initialize built-in images\n", progname);
115 #elif defined(HAVE_XPM)
117 #include <X11/Intrinsic.h>
118 #include <X11/Xutil.h>
123 # include <X11/Xmu/Drawing.h>
125 # include <Xmu/Drawing.h>
130 #define countof(x) (sizeof((x))/sizeof((*x)))
134 handle_xpm_error (const char *filename, int status)
136 if (!filename) filename = "builtin";
143 fprintf (stderr, "%s: %s: warning: color substitution performed\n",
151 fprintf (stderr, "%s: %s: no such file\n", progname, filename);
154 fprintf (stderr, "%s: %s: out of memory\n", progname, filename);
157 fprintf (stderr, "%s: %s: color allocation failed\n",
161 fprintf (stderr, "%s: %s: unknown XPM error %d\n", progname,
169 /* The libxpm version of this function...
172 xpm_to_pixmap_1 (Display *dpy, Window window,
173 int *width_ret, int *height_ret,
175 const char *filename,
179 XpmAttributes xpmattrs;
183 XWindowAttributes xgwa;
184 XGetWindowAttributes (dpy, window, &xgwa);
186 memset (&xpm_image, 0, sizeof(xpm_image));
187 memset (&xpm_info, 0, sizeof(xpm_info));
192 status = XpmReadFileToData ((char *) filename, &xpm_data);
193 if (handle_xpm_error (filename, status))
197 xpmattrs.valuemask = 0;
200 xpmattrs.valuemask |= XpmCloseness;
201 xpmattrs.closeness = 40000;
204 xpmattrs.valuemask |= XpmVisual;
205 xpmattrs.visual = xgwa.visual;
208 xpmattrs.valuemask |= XpmDepth;
209 xpmattrs.depth = xgwa.depth;
212 xpmattrs.valuemask |= XpmColormap;
213 xpmattrs.colormap = xgwa.colormap;
216 status = XpmCreatePixmapFromData (dpy, window, xpm_data,
217 &pixmap, mask_ret, &xpmattrs);
218 if (handle_xpm_error (filename, status))
222 if (width_ret) *width_ret = xpmattrs.width;
223 if (height_ret) *height_ret = xpmattrs.height;
231 unsigned long fg = BlackPixelOfScreen (xgwa.screen);
232 unsigned long bg = WhitePixelOfScreen (xgwa.screen);
235 pixmap = XmuLocateBitmapFile (xgwa.screen, filename,
236 0, 0, width_ret, height_ret, &xh, &yh);
239 fprintf (stderr, "%s: couldn't find XBM %s\n", progname,
243 b2 = XmuCreatePixmapFromBitmap (dpy, window, pixmap,
244 *width_ret, *height_ret,
246 XFreePixmap (dpy, pixmap);
251 fprintf (stderr, "%s: couldn't load XBM %s\n", progname, filename);
258 "%s: your vendor doesn't ship the standard Xmu library.\n",
260 fprintf (stderr, "\tWe can't load XBM files without it.\n");
268 #else /* !HAVE_XPM && !HAVE_GDK_PIXBUF */
271 xpm_to_pixmap_1 (Display *dpy, Window window,
272 int *width_ret, int *height_ret,
274 const char *filename,
277 fprintf(stderr, "%s: not compiled with XPM or Pixbuf support.\n", progname);
281 #endif /* !HAVE_XPM */
285 xpm_data_to_pixmap (Display *dpy, Window window, char **xpm_data,
286 int *width_ret, int *height_ret,
289 return xpm_to_pixmap_1 (dpy, window, width_ret, height_ret, mask_ret,
295 xpm_file_to_pixmap (Display *dpy, Window window, const char *filename,
296 int *width_ret, int *height_ret,
299 return xpm_to_pixmap_1 (dpy, window, width_ret, height_ret, mask_ret,