]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkimage.c
gdk: Get rid of GdkRGB usage
[~andy/gtk] / gdk / gdkimage.c
index c7f426581a9b4cf30d7a9d762dbba5eb599f1bb9..3cc2cb48e996cf550e4e8a67beaa51f2257e0578 100644 (file)
  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
-#include "../config.h"
 
-#include <sys/types.h>
+/*
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ */
 
-#if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
-#define USE_SHM
-#endif
+#include "config.h"
+#include <stdlib.h>
+#include <sys/types.h>
 
-#ifdef USE_SHM
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#endif /* USE_SHM */
+#include "gdk.h"               /* For gdk_flush() */
+#include "gdkimage.h"
+#include "gdkprivate.h"
+#include "gdkinternals.h"      /* For scratch_image code */
+
+/**
+ * gdk_image_set_colormap:
+ * @image: a #GdkImage
+ * @colormap: a #GdkColormap
+ * 
+ * Sets the colormap for the image to the given colormap.  Normally
+ * there's no need to use this function, images are created with the
+ * correct colormap if you get the image from a drawable. If you
+ * create the image from scratch, use the colormap of the drawable you
+ * intend to render the image to.
+ **/
+void
+gdk_image_set_colormap (GdkImage       *image,
+                        GdkColormap    *colormap)
+{
+  g_return_if_fail (GDK_IS_IMAGE (image));
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
 
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
+  if (image->colormap != colormap)
+    {
+      if (image->colormap)
+       g_object_unref (image->colormap);
 
-#ifdef USE_SHM
-#include <X11/extensions/XShm.h>
-#endif /* USE_SHM */
+      image->colormap = colormap;
+      g_object_ref (image->colormap);
+    }
+}
 
-#include "gdk.h"
-#include "gdkprivate.h"
+/**
+ * gdk_image_get_colormap:
+ * @image: a #GdkImage
+ * 
+ * Retrieves the colormap for a given image, if it exists.  An image
+ * will have a colormap if the drawable from which it was created has
+ * a colormap, or if a colormap was set explicitely with
+ * gdk_image_set_colormap().
+ * 
+ * Return value: colormap for the image
+ **/
+GdkColormap *
+gdk_image_get_colormap (GdkImage *image)
+{
+  g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
 
+  return image->colormap;
+}
 
-static void gdk_image_put_normal (GdkDrawable *drawable,
-                                 GdkGC       *gc,
-                                 GdkImage    *image,
-                                 gint         xsrc,
-                                 gint         ysrc,
-                                 gint         xdest,
-                                 gint         ydest,
-                                 gint         width,
-                                 gint         height);
-static void gdk_image_put_shared (GdkDrawable *drawable,
-                                 GdkGC       *gc,
-                                 GdkImage    *image,
-                                 gint         xsrc,
-                                 gint         ysrc,
-                                 gint         xdest,
-                                 gint         ydest,
-                                 gint         width,
-                                 gint         height);
+/**
+ * gdk_image_get_image_type:
+ * @image: a #GdkImage
+ *
+ * Determines the type of a given image.
+ *
+ * Return value: the #GdkImageType of the image
+ *
+ * Since: 2.22
+ **/
+GdkImageType
+gdk_image_get_image_type (GdkImage *image)
+{
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
 
+  return image->type;
+}
 
-static GList *image_list = NULL;
+/**
+ * gdk_image_get_visual:
+ * @image: a #GdkImage
+ *
+ * Determines the visual that was used to create the image.
+ *
+ * Return value: a #GdkVisual
+ *
+ * Since: 2.22
+ **/
+GdkVisual *
+gdk_image_get_visual (GdkImage *image)
+{
+  g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
 
+  return image->visual;
+}
 
-void
-gdk_image_exit (void)
+/**
+ * gdk_image_get_byte_order:
+ * @image: a #GdkImage
+ *
+ * Determines the byte order of the image.
+ *
+ * Return value: a #GdkVisual
+ *
+ * Since: 2.22
+ **/
+GdkByteOrder
+gdk_image_get_byte_order (GdkImage *image)
 {
-  GdkImage *image;
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
 
-  while (image_list)
-    {
-      image = image_list->data;
-      gdk_image_destroy (image);
-    }
+  return image->byte_order;
 }
 
-GdkImage *
-gdk_image_new_bitmap(GdkVisual *visual, gpointer data, gint w, gint h)
-/*
- * Desc: create a new bitmap image
- */
-{
-        Visual *xvisual;
-        GdkImage *image;
-        GdkImagePrivate *private;
-        private = malloc (sizeof (GdkImagePrivate));
-        image = (GdkImage *) private;
-        private->xdisplay = gdk_display;
-        private->image_put = gdk_image_put_normal;
-        image->type = GDK_IMAGE_NORMAL;
-        image->visual = visual;
-        image->width = w;
-        image->height = h;
-        image->depth = 1;
-        xvisual = ((GdkVisualPrivate*) visual)->xvisual;
-        private->ximage = XCreateImage(private->xdisplay, xvisual, 1, XYBitmap,
-                                      0, 0, w ,h, 8, 0);
-        private->ximage->data = data;
-        private->ximage->bitmap_bit_order = MSBFirst;
-        private->ximage->byte_order = MSBFirst;
-        image->byte_order = MSBFirst;
-        image->mem =  private->ximage->data;
-        image->bpl = private->ximage->bytes_per_line;
-        image->bpp = 1;
-       return(image);
-} /* gdk_image_new_bitmap() */
-
-static int
-gdk_image_check_xshm(Display *display)
-/* 
- * Desc: query the server for support for the MIT_SHM extension
- * Return:  0 = not available
- *          1 = shared XImage support available
- *          2 = shared Pixmap support available also
- */
+/**
+ * gdk_image_get_width:
+ * @image: a #GdkImage
+ *
+ * Determines the width of the image.
+ *
+ * Return value: the width
+ *
+ * Since: 2.22
+ **/
+gint
+gdk_image_get_width (GdkImage *image)
 {
-#ifdef USE_SHM
-  int major, minor, ignore;
-  Bool pixmaps;
-  
-  if (XQueryExtension(display, "MIT-SHM", &ignore, &ignore, &ignore)) 
-    {
-      if (XShmQueryVersion(display, &major, &minor, &pixmaps )==True) 
-       {
-         return (pixmaps==True) ? 2 : 1;
-       }
-    }
-#endif /* USE_SHM */
-  return 0;
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
+
+  return image->width;
 }
 
-void
-gdk_image_init (void)
+/**
+ * gdk_image_get_height:
+ * @image: a #GdkImage
+ *
+ * Determines the height of the image.
+ *
+ * Return value: the height
+ *
+ * Since: 2.22
+ **/
+gint
+gdk_image_get_height (GdkImage *image)
 {
-  if (gdk_use_xshm)
-    {
-      if (!gdk_image_check_xshm (gdk_display))
-       {
-         gdk_use_xshm = False;
-       }
-    }
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
+
+  return image->height;
 }
 
-GdkImage*
-gdk_image_new (GdkImageType  type,
-              GdkVisual    *visual,
-              gint          width,
-              gint          height)
+/**
+ * gdk_image_get_depth:
+ * @image: a #GdkImage
+ *
+ * Determines the depth of the image.
+ *
+ * Return value: the depth
+ *
+ * Since: 2.22
+ **/
+guint16
+gdk_image_get_depth (GdkImage *image)
 {
-  GdkImage *image;
-  GdkImagePrivate *private;
-#ifdef USE_SHM
-  XShmSegmentInfo *x_shm_info;
-#endif /* USE_SHM */
-  Visual *xvisual;
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
 
-  switch (type)
-    {
-    case GDK_IMAGE_FASTEST:
-      image = gdk_image_new (GDK_IMAGE_SHARED, visual, width, height);
+  return image->depth;
+}
 
-      if (!image)
-       image = gdk_image_new (GDK_IMAGE_NORMAL, visual, width, height);
-      break;
+/**
+ * gdk_image_get_bytes_per_pixel:
+ * @image: a #GdkImage
+ *
+ * Determines the number of bytes per pixel of the image.
+ *
+ * Return value: the bytes per pixel
+ *
+ * Since: 2.22
+ **/
+guint16
+gdk_image_get_bytes_per_pixel (GdkImage *image)
+{
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
 
-    default:
-      private = malloc (sizeof (GdkImagePrivate));
-      image = (GdkImage*) private;
+  return image->bpp;
+}
 
-      private->xdisplay = gdk_display;
-      private->image_put = NULL;
+/**
+ * gdk_image_get_bytes_per_line:
+ * @image: a #GdkImage
+ *
+ * Determines the number of bytes per line of the image.
+ *
+ * Return value: the bytes per line
+ *
+ * Since: 2.22
+ **/
+guint16
+gdk_image_get_bytes_per_line (GdkImage *image)
+{
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
 
-      image->type = type;
-      image->visual = visual;
-      image->width = width;
-      image->height = height;
-      image->depth = visual->depth;
+  return image->bpl;
+}
 
-      xvisual = ((GdkVisualPrivate*) visual)->xvisual;
+/**
+ * gdk_image_get_bits_per_pixel:
+ * @image: a #GdkImage
+ *
+ * Determines the number of bits per pixel of the image.
+ *
+ * Return value: the bits per pixel
+ *
+ * Since: 2.22
+ **/
+guint16
+gdk_image_get_bits_per_pixel (GdkImage *image)
+{
+  g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
 
-      switch (type)
-       {
-       case GDK_IMAGE_SHARED:
-#ifdef USE_SHM
-         if (gdk_use_xshm)
-           {
-             private->image_put = gdk_image_put_shared;
-
-             private->x_shm_info = malloc (sizeof (XShmSegmentInfo));
-             x_shm_info = private->x_shm_info;
-
-             private->ximage = XShmCreateImage (private->xdisplay, xvisual, visual->depth,
-                                                ZPixmap, NULL, x_shm_info, width, height);
-             if (private->ximage == NULL)
-               {
-                 g_warning ("XShmCreateImage failed");
-                 
-                 free (image);
-                 gdk_use_xshm = False;
-                 return NULL;
-               }
-
-             x_shm_info->shmid = shmget (IPC_PRIVATE,
-                                         private->ximage->bytes_per_line * private->ximage->height,
-                                         IPC_CREAT | 0777);
-
-             if (x_shm_info->shmid == -1)
-               {
-                 g_warning ("shmget failed!");
-
-                 XDestroyImage (private->ximage);
-                 free (private->x_shm_info);
-                 free (image);
-
-                 gdk_use_xshm = False;
-                 return NULL;
-               }
-
-             x_shm_info->readOnly = False;
-             x_shm_info->shmaddr = shmat (x_shm_info->shmid, 0, 0);
-             private->ximage->data = x_shm_info->shmaddr;
-
-             if (x_shm_info->shmaddr == (char*) -1)
-               {
-                 g_warning ("shmat failed!");
-
-                 XDestroyImage (private->ximage);
-                 shmctl (x_shm_info->shmid, IPC_RMID, 0);
-                 
-                 free (private->x_shm_info);
-                 free (image);
-
-                 return NULL;
-               }
-
-#ifdef IPC_RMID_DEFERRED_RELEASE
-             if (x_shm_info->shmaddr != (char*) -1)
-               shmctl (x_shm_info->shmid, IPC_RMID, 0);                      
-#endif
+  return image->bits_per_pixel;
+}
 
-             gdk_error_code = 0;
-             gdk_error_warnings = 0;
-
-             XShmAttach (private->xdisplay, x_shm_info);
-             XSync (private->xdisplay, False);
-
-             gdk_error_warnings = 1;
-             if (gdk_error_code == -1)
-               {
-                 /* this is the common failure case so omit warning */
-                 XDestroyImage (private->ximage);
-                 shmdt (x_shm_info->shmaddr);
-                 shmctl (x_shm_info->shmid, IPC_RMID, 0);
-                  
-                 free (private->x_shm_info);
-                 free (image);
-
-                 gdk_use_xshm = False;
-                 return NULL;
-               }
-
-             if (image)
-               image_list = g_list_prepend (image_list, image);
-           }
-         else
-           {
-             free (image);
-             return NULL;
-           }
-         break;
-#else /* USE_SHM */
-         free (image);
-         return NULL;
-#endif /* USE_SHM */
-       case GDK_IMAGE_NORMAL:
-         private->image_put = gdk_image_put_normal;
+/**
+ * gdk_image_get_pixels:
+ * @image: a #GdkImage
+ *
+ * Returns a pointer to the pixel data of the image.
+ *
+ * Returns: the pixel data of the image
+ *
+ * Since: 2.22
+ */
+gpointer
+gdk_image_get_pixels (GdkImage *image)
+{
+  g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
 
-         private->ximage = XCreateImage (private->xdisplay, xvisual, visual->depth,
-                                         ZPixmap, 0, 0, width, height, 32, 0);
+  return image->mem;
+}
 
-         private->ximage->data = malloc (sizeof (char) * private->ximage->bytes_per_line * private->ximage->height);
-         break;
+/* We have N_REGION GDK_SCRATCH_IMAGE_WIDTH x GDK_SCRATCH_IMAGE_HEIGHT regions divided
+ * up between n_images different images. possible_n_images gives
+ * various divisors of N_REGIONS. The reason for allowing this
+ * flexibility is that we want to create as few images as possible,
+ * but we want to deal with the abberant systems that have a SHMMAX
+ * limit less than
+ *
+ * GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT * N_REGIONS * 4 (384k)
+ *
+ * (Are there any such?)
+ */
+#define N_REGIONS 6
+static const int possible_n_images[] = { 1, 2, 3, 6 };
 
-       case GDK_IMAGE_FASTEST:
-         g_assert_not_reached ();
-       }
+/* We allocate one GdkScratchImageInfo structure for each
+ * depth where we are allocating scratch images. (Future: one
+ * per depth, per display)
+ */
+typedef struct _GdkScratchImageInfo GdkScratchImageInfo;
 
-      if (image)
+struct _GdkScratchImageInfo {
+  gint depth;
+  
+  gint n_images;
+  GdkImage *static_image[N_REGIONS];
+  gint static_image_idx;
+
+  /* In order to optimize filling fractions, we simultaneously fill in up
+   * to three regions of size GDK_SCRATCH_IMAGE_WIDTH * GDK_SCRATCH_IMAGE_HEIGHT: one
+   * for images that are taller than GDK_SCRATCH_IMAGE_HEIGHT / 2, and must
+   * be tiled horizontally. One for images that are wider than
+   * GDK_SCRATCH_IMAGE_WIDTH / 2 and must be tiled vertically, and a third
+   * for images smaller than GDK_SCRATCH_IMAGE_HEIGHT / 2 x GDK_SCRATCH_IMAGE_WIDTH x 2
+   * that we tile in horizontal rows.
+   */
+  gint horiz_idx;
+  gint horiz_y;
+  gint vert_idx;
+  gint vert_x;
+  
+  /* tile_y1 and tile_y2 define the horizontal band into
+   * which we are tiling images. tile_x is the x extent to
+   * which that is filled
+   */
+  gint tile_idx;
+  gint tile_x;
+  gint tile_y1;
+  gint tile_y2;
+
+  GdkScreen *screen;
+};
+
+static GSList *scratch_image_infos = NULL;
+
+static gboolean
+allocate_scratch_images (GdkScratchImageInfo *info,
+                        gint                 n_images,
+                        gboolean             shared)
+{
+  gint i;
+  
+  for (i = 0; i < n_images; i++)
+    {
+      info->static_image[i] = _gdk_image_new_for_depth (info->screen,
+                                                       shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
+                                                       NULL,
+                                                       GDK_SCRATCH_IMAGE_WIDTH * (N_REGIONS / n_images), 
+                                                       GDK_SCRATCH_IMAGE_HEIGHT,
+                                                       info->depth);
+      
+      if (!info->static_image[i])
        {
-         image->byte_order = private->ximage->byte_order;
-         image->mem = private->ximage->data;
-         image->bpl = private->ximage->bytes_per_line;
-
-         switch (private->ximage->bits_per_pixel)
-           {
-           case 8:
-             image->bpp = 1;
-             break;
-           case 16:
-             image->bpp = 2;
-             break;
-           case 24:
-             image->bpp = 3;
-             break;
-           case 32:
-             image->bpp = 4;
-             break;
-           }
+         gint j;
+         
+         for (j = 0; j < i; j++)
+           g_object_unref (info->static_image[j]);
+         
+         return FALSE;
        }
     }
-
-  return image;
+  
+  return TRUE;
 }
 
-GdkImage*
-gdk_image_get (GdkWindow *window,
-              gint       x,
-              gint       y,
-              gint       width,
-              gint       height)
+static void
+scratch_image_info_display_closed (GdkDisplay          *display,
+                                   gboolean             is_error,
+                                   GdkScratchImageInfo *image_info)
 {
-  GdkImage *image;
-  GdkImagePrivate *private;
-  GdkWindowPrivate *win_private;
-
-  g_return_val_if_fail (window != NULL, NULL);
+  gint i;
 
-  win_private = (GdkWindowPrivate *) window;
-  if (win_private->destroyed)
-    return NULL;
+  g_signal_handlers_disconnect_by_func (display,
+                                        scratch_image_info_display_closed,
+                                        image_info);
 
-  private = malloc (sizeof (GdkImagePrivate));
-  image = (GdkImage*) private;
+  scratch_image_infos = g_slist_remove (scratch_image_infos, image_info);
 
-  private->xdisplay = gdk_display;
-  private->image_put = gdk_image_put_normal;
-  private->ximage = XGetImage (private->xdisplay,
-                              win_private->xwindow,
-                              x, y, width, height,
-                              AllPlanes, ZPixmap);
+  for (i = 0; i < image_info->n_images; i++)
+    g_object_unref (image_info->static_image[i]);
 
-  image->type = GDK_IMAGE_NORMAL;
-  image->visual = gdk_window_get_visual (window);
-  image->width = width;
-  image->height = height;
-  image->depth = private->ximage->depth;
-
-  image->mem = private->ximage->data;
-  image->bpl = private->ximage->bytes_per_line;
-  image->bpp = 1;
-
-  return image;
+  g_free (image_info);
 }
 
-guint32
-gdk_image_get_pixel (GdkImage *image,
-                    gint x,
-                    gint y)
+static GdkScratchImageInfo *
+scratch_image_info_for_depth (GdkScreen *screen,
+                             gint       depth)
 {
-  guint32 pixel;
-  GdkImagePrivate *private;
+  GSList *tmp_list;
+  GdkScratchImageInfo *image_info;
+  gint i;
 
-  g_return_val_if_fail (image != NULL, 0);
+  tmp_list = scratch_image_infos;
+  while (tmp_list)
+    {
+      image_info = tmp_list->data;
+      if (image_info->depth == depth && image_info->screen == screen)
+       return image_info;
+      
+      tmp_list = tmp_list->next;
+    }
 
-  private = (GdkImagePrivate *) image;
+  image_info = g_new (GdkScratchImageInfo, 1);
 
-  pixel = XGetPixel (private->ximage, x, y);
+  image_info->depth = depth;
+  image_info->screen = screen;
 
-  return pixel;
-}
+  g_signal_connect (gdk_screen_get_display (screen), "closed",
+                    G_CALLBACK (scratch_image_info_display_closed),
+                    image_info);
 
-void
-gdk_image_put_pixel (GdkImage *image,
-                    gint x,
-                    gint y,
-                    guint32 pixel)
-{
-  GdkImagePrivate *private;
+  /* Try to allocate as few possible shared images */
+  for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
+    {
+      if (allocate_scratch_images (image_info, possible_n_images[i], TRUE))
+       {
+         image_info->n_images = possible_n_images[i];
+         break;
+       }
+    }
 
-  g_return_if_fail (image != NULL);
+  /* If that fails, just allocate N_REGIONS normal images */
+  if (i == G_N_ELEMENTS (possible_n_images))
+    {
+      allocate_scratch_images (image_info, N_REGIONS, FALSE);
+      image_info->n_images = N_REGIONS;
+    }
 
-  private = (GdkImagePrivate *) image;
+  image_info->static_image_idx = 0;
 
-  pixel = XPutPixel (private->ximage, x, y, pixel);
-}
+  image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
+  image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
+  image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
+  image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
 
-void
-gdk_image_destroy (GdkImage *image)
-{
-  GdkImagePrivate *private;
-#ifdef USE_SHM
-  XShmSegmentInfo *x_shm_info;
-#endif /* USE_SHM */
+  scratch_image_infos = g_slist_prepend (scratch_image_infos, image_info);
 
-  g_return_if_fail (image != NULL);
+  return image_info;
+}
 
-  private = (GdkImagePrivate*) image;
-  switch (image->type)
-    {
-    case GDK_IMAGE_NORMAL:
-      XDestroyImage (private->ximage);
-      break;
-
-    case GDK_IMAGE_SHARED:
-#ifdef USE_SHM
-      XShmDetach (private->xdisplay, private->x_shm_info);
-      XDestroyImage (private->ximage);
-
-      x_shm_info = private->x_shm_info;
-      shmdt (x_shm_info->shmaddr);
-      shmctl (x_shm_info->shmid, IPC_RMID, 0);
-      
-      free (private->x_shm_info);
+/* Defining NO_FLUSH can cause inconsistent screen updates, but is useful
+   for performance evaluation. */
 
-      image_list = g_list_remove (image_list, image);
-#else /* USE_SHM */
-      g_error ("trying to destroy shared memory image when gdk was compiled without shared memory support");
-#endif /* USE_SHM */
-      break;
+#undef NO_FLUSH
 
-    case GDK_IMAGE_FASTEST:
-      g_assert_not_reached ();
-    }
+#ifdef VERBOSE
+static gint sincelast;
+#endif
 
-  free (image);
+static gint
+alloc_scratch_image (GdkScratchImageInfo *image_info)
+{
+  if (image_info->static_image_idx == N_REGIONS)
+    {
+#ifndef NO_FLUSH
+      gdk_flush ();
+#endif
+#ifdef VERBOSE
+      g_print ("flush, %d puts since last flush\n", sincelast);
+      sincelast = 0;
+#endif
+      image_info->static_image_idx = 0;
+
+      /* Mark all regions that we might be filling in as completely
+       * full, to force new tiles to be allocated for subsequent
+       * images
+       */
+      image_info->horiz_y = GDK_SCRATCH_IMAGE_HEIGHT;
+      image_info->vert_x = GDK_SCRATCH_IMAGE_WIDTH;
+      image_info->tile_x = GDK_SCRATCH_IMAGE_WIDTH;
+      image_info->tile_y1 = image_info->tile_y2 = GDK_SCRATCH_IMAGE_HEIGHT;
+    }
+  return image_info->static_image_idx++;
 }
 
-static void
-gdk_image_put_normal (GdkDrawable *drawable,
-                     GdkGC       *gc,
-                     GdkImage    *image,
-                     gint         xsrc,
-                     gint         ysrc,
-                     gint         xdest,
-                     gint         ydest,
-                     gint         width,
-                     gint         height)
+/**
+ * _gdk_image_get_scratch:
+ * @screen: a #GdkScreen
+ * @width: desired width
+ * @height: desired height
+ * @depth: depth of image 
+ * @x: X location within returned image of scratch image
+ * @y: Y location within returned image of scratch image
+ * 
+ * Allocates an image of size width/height, up to a maximum
+ * of GDK_SCRATCH_IMAGE_WIDTHxGDK_SCRATCH_IMAGE_HEIGHT that is
+ * suitable to use on @screen.
+ * 
+ * Return value: a scratch image. This must be used by a
+ *  call to gdk_image_put() before any other calls to
+ *  _gdk_image_get_scratch()
+ **/
+GdkImage *
+_gdk_image_get_scratch (GdkScreen   *screen,
+                       gint         width,                     
+                       gint         height,
+                       gint         depth,
+                       gint        *x,
+                       gint        *y)
 {
-  GdkWindowPrivate *drawable_private;
-  GdkImagePrivate *image_private;
-  GdkGCPrivate *gc_private;
-
-  g_return_if_fail (drawable != NULL);
-  g_return_if_fail (image != NULL);
-  g_return_if_fail (gc != NULL);
-
-  drawable_private = (GdkWindowPrivate*) drawable;
-  if (drawable_private->destroyed)
-    return;
-  image_private = (GdkImagePrivate*) image;
-  gc_private = (GdkGCPrivate*) gc;
+  GdkScratchImageInfo *image_info;
+  GdkImage *image;
+  gint idx;
+  
+  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
 
-  g_return_if_fail (image->type == GDK_IMAGE_NORMAL);
+  image_info = scratch_image_info_for_depth (screen, depth);
 
-  XPutImage (drawable_private->xdisplay, drawable_private->xwindow,
-            gc_private->xgc, image_private->ximage,
-            xsrc, ysrc, xdest, ydest, width, height);
+  if (width >= (GDK_SCRATCH_IMAGE_WIDTH >> 1))
+    {
+      if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
+       {
+         idx = alloc_scratch_image (image_info);
+         *x = 0;
+         *y = 0;
+       }
+      else
+       {
+         if (height + image_info->horiz_y > GDK_SCRATCH_IMAGE_HEIGHT)
+           {
+             image_info->horiz_idx = alloc_scratch_image (image_info);
+             image_info->horiz_y = 0;
+           }
+         idx = image_info->horiz_idx;
+         *x = 0;
+         *y = image_info->horiz_y;
+         image_info->horiz_y += height;
+       }
+    }
+  else
+    {
+      if (height >= (GDK_SCRATCH_IMAGE_HEIGHT >> 1))
+       {
+         if (width + image_info->vert_x > GDK_SCRATCH_IMAGE_WIDTH)
+           {
+             image_info->vert_idx = alloc_scratch_image (image_info);
+             image_info->vert_x = 0;
+           }
+         idx = image_info->vert_idx;
+         *x = image_info->vert_x;
+         *y = 0;
+         /* using 3 and -4 would be slightly more efficient on 32-bit machines
+            with > 1bpp displays */
+         image_info->vert_x += (width + 7) & -8;
+       }
+      else
+       {
+         if (width + image_info->tile_x > GDK_SCRATCH_IMAGE_WIDTH)
+           {
+             image_info->tile_y1 = image_info->tile_y2;
+             image_info->tile_x = 0;
+           }
+         if (height + image_info->tile_y1 > GDK_SCRATCH_IMAGE_HEIGHT)
+           {
+             image_info->tile_idx = alloc_scratch_image (image_info);
+             image_info->tile_x = 0;
+             image_info->tile_y1 = 0;
+             image_info->tile_y2 = 0;
+           }
+         if (height + image_info->tile_y1 > image_info->tile_y2)
+           image_info->tile_y2 = height + image_info->tile_y1;
+         idx = image_info->tile_idx;
+         *x = image_info->tile_x;
+         *y = image_info->tile_y1;
+         image_info->tile_x += (width + 7) & -8;
+       }
+    }
+  image = image_info->static_image[idx * image_info->n_images / N_REGIONS];
+  *x += GDK_SCRATCH_IMAGE_WIDTH * (idx % (N_REGIONS / image_info->n_images));
+#ifdef VERBOSE
+  g_print ("index %d, x %d, y %d (%d x %d)\n", idx, *x, *y, width, height);
+  sincelast++;
+#endif
+  return image;
 }
 
-static void
-gdk_image_put_shared (GdkDrawable *drawable,
-                     GdkGC       *gc,
-                     GdkImage    *image,
-                     gint         xsrc,
-                     gint         ysrc,
-                     gint         xdest,
-                     gint         ydest,
-                     gint         width,
-                     gint         height)
+GdkImage*
+gdk_image_new (GdkImageType  type,
+              GdkVisual    *visual,
+              gint          width,
+              gint          height)
 {
-#ifdef USE_SHM
-  GdkWindowPrivate *drawable_private;
-  GdkImagePrivate *image_private;
-  GdkGCPrivate *gc_private;
-
-  g_return_if_fail (drawable != NULL);
-  g_return_if_fail (image != NULL);
-  g_return_if_fail (gc != NULL);
-
-  drawable_private = (GdkWindowPrivate*) drawable;
-  if (drawable_private->destroyed)
-    return;
-  image_private = (GdkImagePrivate*) image;
-  gc_private = (GdkGCPrivate*) gc;
-
-  g_return_if_fail (image->type == GDK_IMAGE_SHARED);
-
-  XShmPutImage (drawable_private->xdisplay, drawable_private->xwindow,
-               gc_private->xgc, image_private->ximage,
-               xsrc, ysrc, xdest, ydest, width, height, False);
-#else /* USE_SHM */
-  g_error ("trying to draw shared memory image when gdk was compiled without shared memory support");
-#endif /* USE_SHM */
+  return _gdk_image_new_for_depth (gdk_visual_get_screen (visual), type,
+                                  visual, width, height, -1);
 }