+ GdkPixbuf *pb;
+ Display *dpy = DisplayOfScreen (screen);
+ unsigned int win_width, win_height;
+# ifdef HAVE_GTK2
+ GError *gerr = 0;
+# endif /* HAVE_GTK2 */
+
+ {
+ Window root;
+ int x, y;
+ unsigned int bw, d;
+ XWindowAttributes xgwa;
+ XGetWindowAttributes (dpy, window, &xgwa);
+ screen = xgwa.screen;
+ XGetGeometry (dpy, drawable,
+ &root, &x, &y, &win_width, &win_height, &bw, &d);
+ }
+
+ gdk_pixbuf_xlib_init (dpy, screen_number (screen));
+# ifdef HAVE_GTK2
+ g_type_init();
+# else /* !HAVE_GTK2 */
+ xlib_rgb_init (dpy, screen);
+# endif /* !HAVE_GTK2 */
+
+ pb = gdk_pixbuf_new_from_file (filename
+# ifdef HAVE_GTK2
+ , &gerr
+# endif /* HAVE_GTK2 */
+ );
+
+ if (!pb)
+ {
+ fprintf (stderr, "%s: unable to load \"%s\"\n", progname, filename);
+# ifdef HAVE_GTK2
+ if (gerr && gerr->message && *gerr->message)
+ fprintf (stderr, "%s: reason: %s\n", progname, gerr->message);
+# endif /* HAVE_GTK2 */
+ return False;
+ }
+ else
+ {
+ int w = gdk_pixbuf_get_width (pb);
+ int h = gdk_pixbuf_get_height (pb);
+ int srcx, srcy, destx, desty, w2, h2;
+
+ compute_image_scaling (w, h, win_width, win_height, verbose_p,
+ &srcx, &srcy, &destx, &desty, &w2, &h2);
+ if (w != w2 || h != h2)
+ {
+ GdkPixbuf *pb2 = gdk_pixbuf_scale_simple (pb, w2, h2,
+ GDK_INTERP_BILINEAR);
+ if (pb2)
+ {
+ gdk_pixbuf_unref (pb);
+ pb = pb2;
+ w = w2;
+ h = h2;
+ }
+ else
+ fprintf (stderr, "%s: out of memory when scaling?\n", progname);
+ }
+
+ clear_drawable (screen, drawable);
+
+ /* #### Note that this always uses the default colormap! Morons!
+ Owen says that in Gnome 2.0, I should try using
+ gdk_pixbuf_render_pixmap_and_mask_for_colormap() instead.
+ But I haven't tried.
+ */
+ gdk_pixbuf_xlib_render_to_drawable_alpha (pb, drawable,
+ srcx, srcy, destx, desty,
+ w, h,
+ GDK_PIXBUF_ALPHA_FULL, 127,
+ XLIB_RGB_DITHER_NORMAL,
+ 0, 0);
+ XSync (dpy, False);
+ }
+
+ return True;
+}
+
+#endif /* HAVE_GDK_PIXBUF */
+
+
+
+#ifdef HAVE_JPEGLIB
+
+/* Allocates a colormap that makes a PseudoColor or DirectColor
+ visual behave like a TrueColor visual of the same depth.
+ */
+static void
+allocate_cubic_colormap (Screen *screen, Visual *visual, Colormap cmap,
+ Bool verbose_p)
+{
+ Display *dpy = DisplayOfScreen (screen);
+ int nr, ng, nb, cells;
+ int r, g, b;
+ int depth;
+ XColor colors[4097];
+ int i;
+
+ depth = visual_depth (screen, visual);
+
+ switch (depth)
+ {
+ case 8: nr = 3; ng = 3; nb = 2; cells = 256; break;
+ case 12: nr = 4; ng = 4; nb = 4; cells = 4096; break;
+ default: abort(); break;
+ }
+
+ memset(colors, 0, sizeof(colors));
+ for (r = 0; r < (1 << nr); r++)
+ for (g = 0; g < (1 << ng); g++)
+ for (b = 0; b < (1 << nb); b++)
+ {
+ i = (r | (g << nr) | (b << (nr + ng)));
+ colors[i].pixel = i;
+ colors[i].flags = DoRed|DoGreen|DoBlue;
+ if (depth == 8)
+ {
+ colors[i].red = ((r << 13) | (r << 10) | (r << 7) |
+ (r << 4) | (r << 1));
+ colors[i].green = ((g << 13) | (g << 10) | (g << 7) |
+ (g << 4) | (g << 1));
+ colors[i].blue = ((b << 14) | (b << 12) | (b << 10) |
+ (b << 8) | (b << 6) | (b << 4) |
+ (b << 2) | b);
+ }
+ else
+ {
+ colors[i].red = (r << 12) | (r << 8) | (r << 4) | r;
+ colors[i].green = (g << 12) | (g << 8) | (g << 4) | g;
+ colors[i].blue = (b << 12) | (b << 8) | (b << 4) | b;
+ }
+ }
+
+ {
+ int j;
+ int allocated = 0;
+ int interleave = cells / 8; /* skip around, rather than allocating in
+ order, so that we get better coverage if
+ we can't allocated all of them. */
+ for (j = 0; j < interleave; j++)
+ for (i = 0; i < cells; i += interleave)
+ if (XAllocColor (dpy, cmap, &colors[i + j]))
+ allocated++;
+
+ if (verbose_p)
+ fprintf (stderr, "%s: allocated %d of %d colors for cubic map\n",
+ progname, allocated, cells);
+ }
+}
+
+/* Find the pixel index that is closest to the given color
+ (using linear distance in RGB space -- which is far from the best way.)
+ */
+static unsigned long
+find_closest_pixel (XColor *colors, int ncolors,
+ unsigned long r, unsigned long g, unsigned long b)
+{
+ unsigned long distance = ~0;
+ int i, found = 0;
+
+ if (ncolors == 0)
+ abort();
+ for (i = 0; i < ncolors; i++)
+ {
+ unsigned long d;
+ int rd, gd, bd;
+
+ rd = r - colors[i].red;
+ gd = g - colors[i].green;
+ bd = b - colors[i].blue;
+ if (rd < 0) rd = -rd;
+ if (gd < 0) gd = -gd;
+ if (bd < 0) bd = -bd;
+ d = (rd << 1) + (gd << 2) + bd;
+
+ if (d < distance)
+ {
+ distance = d;
+ found = i;
+ if (distance == 0)
+ break;
+ }
+ }
+
+ return found;
+}
+
+
+/* Given an XImage with 8-bit or 12-bit RGB data, convert it to be
+ displayable with the given X colormap. The farther from a perfect
+ color cube the contents of the colormap are, the lossier the
+ transformation will be. No dithering is done.
+ */
+static void
+remap_image (Screen *screen, Colormap cmap, XImage *image, Bool verbose_p)
+{
+ Display *dpy = DisplayOfScreen (screen);
+ unsigned long map[4097];
+ int x, y, i;
+ int cells;
+ XColor colors[4097];
+
+ if (image->depth == 8)
+ cells = 256;
+ else if (image->depth == 12)
+ cells = 4096;
+ else
+ abort();
+
+ memset(map, -1, sizeof(*map));
+ memset(colors, -1, sizeof(*colors));
+
+ for (i = 0; i < cells; i++)
+ colors[i].pixel = i;
+ XQueryColors (dpy, cmap, colors, cells);
+
+ if (verbose_p)
+ fprintf(stderr, "%s: building color cube for %d bit image\n",
+ progname, image->depth);
+
+ for (i = 0; i < cells; i++)
+ {
+ unsigned short r, g, b;
+
+ if (cells == 256)
+ {
+ /* "RRR GGG BB" In an 8 bit map. Convert that to
+ "RRR RRR RR" "GGG GGG GG" "BB BB BB BB" to give
+ an even spread. */
+ r = (i & 0x07);
+ g = (i & 0x38) >> 3;
+ b = (i & 0xC0) >> 6;
+
+ r = ((r << 13) | (r << 10) | (r << 7) | (r << 4) | (r << 1));
+ g = ((g << 13) | (g << 10) | (g << 7) | (g << 4) | (g << 1));
+ b = ((b << 14) | (b << 12) | (b << 10) | (b << 8) |
+ (b << 6) | (b << 4) | (b << 2) | b);
+ }
+ else
+ {
+ /* "RRRR GGGG BBBB" In a 12 bit map. Convert that to
+ "RRRR RRRR" "GGGG GGGG" "BBBB BBBB" to give an even
+ spread. */
+ r = (i & 0x00F);
+ g = (i & 0x0F0) >> 4;
+ b = (i & 0xF00) >> 8;
+
+ r = (r << 12) | (r << 8) | (r << 4) | r;
+ g = (g << 12) | (g << 8) | (g << 4) | g;
+ b = (b << 12) | (b << 8) | (b << 4) | b;
+ }
+
+ map[i] = find_closest_pixel (colors, cells, r, g, b);
+ }
+
+ if (verbose_p)
+ fprintf(stderr, "%s: remapping colors in %d bit image\n",
+ progname, image->depth);
+
+ for (y = 0; y < image->height; y++)
+ for (x = 0; x < image->width; x++)
+ {
+ unsigned long pixel = XGetPixel(image, x, y);
+ if (pixel >= cells) abort();
+ XPutPixel(image, x, y, map[pixel]);
+ }
+}
+
+
+/* If the file has a PPM (P6) on it, read it and return an XImage.
+ Otherwise, rewind the fd back to the beginning, and return 0.
+ */
+static XImage *
+maybe_read_ppm (Screen *screen, Visual *visual,
+ const char *filename, FILE *in, Bool verbose_p)
+{
+ Display *dpy = DisplayOfScreen (screen);
+ int depth = visual_depth (screen, visual);
+ struct stat st;
+ char *buf = 0;
+ int bufsiz = 0;
+ char *s, dummy;
+ int i, j;
+ int x, y, w, h, maxval;
+ XImage *ximage = 0;
+
+ if (fstat (fileno (in), &st))
+ goto FAIL;
+
+ bufsiz = st.st_size;
+ buf = (char *) malloc (bufsiz + 1);
+ if (!buf)
+ {
+ fprintf (stderr, "%s: out of memory loading %d byte PPM file %s\n",
+ progname, bufsiz, filename);
+ goto FAIL;
+ }
+
+ if (! (s = fgets (buf, bufsiz, in))) /* line 1 */
+ goto FAIL;
+
+ if (!strncmp (buf, "\107\111", 2))
+ {
+ fprintf (stderr, "%s: %s: sorry, GIF files not supported"
+ " when compiled with JPEGlib instead of GDK_Pixbuf.\n",
+ progname, filename);
+ goto FAIL;
+ }
+ else if (!strncmp (buf, "\211\120", 2))
+ {
+ fprintf (stderr, "%s: %s: sorry, PNG files not supported"
+ " when compiled with JPEGlib instead of GDK_Pixbuf.\n",
+ progname, filename);
+ goto FAIL;
+ }
+
+ if (strncmp (s, "P6", 2))
+ goto FAIL;
+
+ if (! (s = fgets (buf, bufsiz, in))) /* line 2 */
+ goto FAIL;
+ if (2 != sscanf (s, " %d %d %c", &w, &h, &dummy))
+ {
+ fprintf (stderr, "%s: %s: invalid PPM (line 2)\n", progname, filename);
+ goto FAIL;
+ }
+
+ if (! (s = fgets (buf, bufsiz, in))) /* line 3 */
+ goto FAIL;
+ if (1 != sscanf (s, " %d %c", &maxval, &dummy))
+ {
+ fprintf (stderr, "%s: %s: invalid PPM (line 3)\n", progname, filename);
+ goto FAIL;
+ }
+ if (maxval != 255)
+ {
+ fprintf (stderr, "%s: %s: unparsable PPM: maxval is %d\n",
+ progname, filename, maxval);
+ goto FAIL;
+ }
+
+ ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
+ w, h, 8, 0);
+ if (ximage)
+ ximage->data = (unsigned char *)
+ calloc (ximage->height, ximage->bytes_per_line);
+ if (!ximage || !ximage->data)
+ {
+ fprintf (stderr, "%s: out of memory loading %dx%d PPM file %s\n",
+ progname, ximage->width, ximage->height, filename);
+ goto FAIL;
+ }
+
+ s = buf;
+ j = bufsiz;
+ while ((i = fread (s, 1, j, in)) > 0)
+ s += i, j -= i;
+
+ i = 0;
+ for (y = 0; y < ximage->height; y++)
+ for (x = 0; x < ximage->width; x++)
+ {
+ unsigned char r = buf[i++];
+ unsigned char g = buf[i++];
+ unsigned char b = buf[i++];
+ unsigned long pixel;
+
+ if (depth > 16)
+ pixel = (r << 16) | (g << 8) | b;
+ else if (depth == 8)
+ pixel = ((r >> 5) | ((g >> 5) << 3) | ((b >> 6) << 6));
+ else if (depth == 12)
+ pixel = ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8));
+ else if (depth == 16 || depth == 15)
+ pixel = (((r >> 3) << 10) | ((g >> 3) << 5) | ((b >> 3)));
+ else
+ abort();
+
+ XPutPixel (ximage, x, y, pixel);
+ }
+
+ free (buf);
+ return ximage;
+
+ FAIL:
+ if (buf) free (buf);
+ if (ximage && ximage->data)
+ {
+ free (ximage->data);
+ ximage->data = 0;
+ }
+ if (ximage) XDestroyImage (ximage);
+ fseek (in, 0, SEEK_SET);