+ int iw, ih, w8, x, y;
+ XImage *ximage;
+ char *data;
+ unsigned char *mask = 0;
+ int depth = 32;
+ unsigned long background_color =
+ BlackPixelOfScreen (DefaultScreenOfDisplay (dpy));
+ unsigned long *pixels = 0;
+ int npixels = 0;
+ int bpl;
+
+ unsigned long rpos=0, gpos=0, bpos=0, apos=0;
+ unsigned long rmsk=0, gmsk=0, bmsk=0, amsk=0;
+ unsigned long rsiz=0, gsiz=0, bsiz=0, asiz=0;
+
+ if (filename)
+ {
+ fprintf(stderr,
+ "%s: no files: not compiled with XPM or Pixbuf support.\n",
+ progname);
+ exit (1);
+ }
+
+ if (! xpm_data) abort();
+ ximage = minixpm_to_ximage (dpy, visual, cmap, depth, background_color,
+ (const char * const *) xpm_data,
+ &iw, &ih, &pixels, &npixels, &mask);
+ if (pixels) free (pixels);
+
+ bpl = ximage->bytes_per_line;
+ data = ximage->data;
+ ximage->data = malloc (ximage->height * bpl);
+
+ /* Flip image upside down, for texture maps;
+ process the mask; and re-arrange the color components for GL.
+ */
+ w8 = (ximage->width + 7) / 8;
+
+ rmsk = ximage->red_mask;
+ gmsk = ximage->green_mask;
+ bmsk = ximage->blue_mask;
+ amsk = ~(rmsk|gmsk|bmsk);
+
+ decode_mask (rmsk, &rpos, &rsiz);
+ decode_mask (gmsk, &gpos, &gsiz);
+ decode_mask (bmsk, &bpos, &bsiz);
+ decode_mask (amsk, &apos, &asiz);
+
+ for (y = 0; y < ximage->height; y++)
+ {
+ int y2 = (ximage->height-1-y);
+
+ unsigned int *oline = (unsigned int *) (ximage->data + (y * bpl));
+ unsigned int *iline = (unsigned int *) (data + (y2 * bpl));
+
+ for (x = 0; x < ximage->width; x++)
+ {
+ unsigned int pixel = iline[x];
+ unsigned char r = (pixel & rmsk) >> rpos;
+ unsigned char g = (pixel & gmsk) >> gpos;
+ unsigned char b = (pixel & bmsk) >> bpos;
+ unsigned char a = (mask
+ ? ((mask [(y2 * w8) + (x >> 3)] & (1 << (x % 8)))
+ ? 0xFF : 0)
+ : 0xFF);
+# if 0
+ pixel = ((r << rpos) | (g << gpos) | (b << bpos) | (a << apos));
+# else
+ pixel = ((a << 24) | (b << 16) | (g << 8) | r);
+# endif
+ oline[x] = pixel;
+ }
+ }
+ free (data);
+
+ return ximage;