]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkrgb.c
Fix off-by-one error when destroying allocated segments on failure.
[~andy/gtk] / gdk / gdkrgb.c
index bec679cdbab89b3051cd9375d3c602639bef7f90..aa24b3cfa8d526eaab4199067020df04709f6f0b 100644 (file)
@@ -2,16 +2,16 @@
  * 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.
    Raph Levien <raph@acm.org>
    */
 
+/*
+ * 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/. 
+ */
+
 #include <math.h>
-#include <stdio.h>
+
+#if HAVE_CONFIG_H
+#  include <config.h>
+#  if STDC_HEADERS
+#    include <stdio.h>
+#    include <stdlib.h>
+#    include <string.h>
+#  endif
+#else
+#  include <stdio.h>
+#  include <stdlib.h>
+#endif
+
 
 #define ENABLE_GRAYSCALE
 
-#include "../config.h"
-#include "gdk/gdk.h"
-#include "gdk/gdkprivate.h"
+#ifdef GDK_RGB_STANDALONE
+
+/* Compiling as a standalone module (i.e. with Gtk 1.0) */
+/* gtk/gtk.h is already included in gdkrgbstub.c */
+#include "config.h"
+#include <gdk/gdkprivate.h>
+
+#else
+
+/* Compiling as a part of Gtk 1.1 or later */
+#include "config.h"
+#include "gdkprivate.h"
+
+#endif
+
+#include "gdk.h"               /* For gdk_flush() */
 #include "gdkrgb.h"
 
-typedef struct _GdkRgbInfo   GdkRgbInfo;
+typedef struct _GdkRgbInfo     GdkRgbInfo;
+typedef struct _GdkRgbCmapInfo GdkRgbCmapInfo;
 
-typedef void (*GdkRgbConvFunc) (GdkImage *image,
+typedef void (*GdkRgbConvFunc) (GdkRgbInfo *image_info, GdkImage *image,
                                gint x0, gint y0,
                                gint width, gint height,
                                guchar *buf, int rowstride,
                                gint x_align, gint y_align,
                                GdkRgbCmap *cmap);
 
+static const gchar* visual_names[] =
+{
+  "static gray",
+  "grayscale",
+  "static color",
+  "pseudo color",
+  "true color",
+  "direct color",
+};
+
+#define REGION_WIDTH 256
+#define STAGE_ROWSTRIDE (REGION_WIDTH * 3)
+#define REGION_HEIGHT 64
+
+/* We have N_REGION REGION_WIDTH x REGION_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
+ *
+ * REGION_WIDTH * REGION_HEIGHT * N_REGIONS * 4 (384k)
+ *
+ * (Are there any such?)
+ */
+#define N_REGIONS 6
+static const int possible_n_images[] = { 1, 2, 3, 6 };
+
 /* Some of these fields should go, as they're not being used at all.
    Globals should generally migrate into here - it's very likely that
    we'll want to run more than one GdkRgbInfo context at the same time
@@ -52,10 +113,6 @@ struct _GdkRgbInfo
   GdkVisual *visual;
   GdkColormap *cmap;
 
-  gulong *color_pixels;
-  gulong *gray_pixels;
-  gulong *reserved_pixels;
-
   guint nred_shades;
   guint ngreen_shades;
   guint nblue_shades;
@@ -74,6 +131,9 @@ struct _GdkRgbInfo
 
   gboolean dith_default;
 
+  gboolean bitmap; /* set true if in 1 bit per pixel mode */
+  GdkGC *own_gc;
+
   /* Convert functions */
   GdkRgbConvFunc conv;
   GdkRgbConvFunc conv_d;
@@ -86,23 +146,59 @@ struct _GdkRgbInfo
 
   GdkRgbConvFunc conv_indexed;
   GdkRgbConvFunc conv_indexed_d;
+
+  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 REGION_WIDTH * REGION_HEIGHT: one
+   * for images that are taller than REGION_HEIGHT / 2, and must
+   * be tiled horizontally. One for images that are wider than
+   * REGION_WIDTH / 2 and must be tiled vertically, and a third
+   * for images smaller than REGION_HEIGHT / 2 x REGION_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;
+
+  guchar *colorcube;
+  guchar *colorcube_d;
+
+  /* We need to track LUT's for pairs of GdkRgbInfo / GdkRgbCmap, so we
+   * keep a list of pointers to GdkRgbCmapInfo on both structures so we
+   * can remove as necessary when freeing a GdkRgbInfo or GdkRgbCmap
+   */
+  GSList *cmap_info_list;
 };
 
-static gboolean gdk_rgb_install_cmap = FALSE;
-static gint gdk_rgb_min_colors = 5 * 5 * 5;
-static gboolean gdk_rgb_verbose = FALSE;
+struct _GdkRgbCmapInfo
+{
+  GdkRgbInfo *image_info;
+  GdkRgbCmap *cmap;
+
+  guchar lut[256];             /* For 8-bit modes */
+};
 
-#define IMAGE_WIDTH 256
-#define STAGE_ROWSTRIDE (IMAGE_WIDTH * 3)
-#define IMAGE_HEIGHT 64
-#define N_IMAGES 6
+static GdkRgbCmapInfo *gdk_rgb_cmap_get_info (GdkRgbCmap *cmap, GdkRgbInfo *image_info);
 
-static GdkRgbInfo *image_info = NULL;
-static GdkImage *static_image[N_IMAGES];
-static gint static_image_idx;
+static const char *gdk_rgb_key = "gdk-rgb-info";
+static GQuark gdk_rgb_quark = 0;
 
-static guchar *colorcube;
-static guchar *colorcube_d;
+static gboolean gdk_rgb_install_cmap = FALSE;
+static gint gdk_rgb_min_colors = 5 * 5 * 5;
+static gboolean gdk_rgb_verbose = FALSE;
 
 static gint
 gdk_rgb_cmap_fail (const char *msg, GdkColormap *cmap, gulong *pixels)
@@ -124,12 +220,13 @@ gdk_rgb_cmap_fail (const char *msg, GdkColormap *cmap, gulong *pixels)
 }
 
 static void
-gdk_rgb_make_colorcube (gulong *pixels, gint nr, gint ng, gint nb)
+gdk_rgb_make_colorcube (GdkRgbInfo *image_info, gulong *pixels,
+                       gint nr, gint ng, gint nb)
 {
   guchar rt[16], gt[16], bt[16];
   gint i;
 
-  colorcube = g_new (guchar, 4096);
+  image_info->colorcube = g_new (guchar, 4096);
   for (i = 0; i < 16; i++)
     {
       rt[i] = ng * nb * ((i * 17 * (nr - 1) + 128) >> 8);
@@ -139,34 +236,36 @@ gdk_rgb_make_colorcube (gulong *pixels, gint nr, gint ng, gint nb)
 
   for (i = 0; i < 4096; i++)
     {
-      colorcube[i] = pixels[rt[i >> 8] + gt[(i >> 4) & 0x0f] + bt[i & 0x0f]];
+      image_info->colorcube[i] = pixels[rt[i >> 8] + gt[(i >> 4) & 0x0f] + bt[i & 0x0f]];
 #ifdef VERBOSE
-      g_print ("%03x %02x %x %x %x\n", i, colorcube[i], rt[i >> 8], gt[(i >> 4) & 0x0f], bt[i & 0x0f]);
+      g_print ("%03x %02x %x %x %x\n", i, image-info->colorcube[i], rt[i >> 8], gt[(i >> 4) & 0x0f], bt[i & 0x0f]);
 #endif
     }
 }
 
 /* this is the colorcube suitable for dithering */
 static void
-gdk_rgb_make_colorcube_d (gulong *pixels, gint nr, gint ng, gint nb)
+gdk_rgb_make_colorcube_d (GdkRgbInfo *image_info, gulong *pixels,
+                         gint nr, gint ng, gint nb)
 {
   gint r, g, b;
   gint i;
 
-  colorcube_d = g_new (guchar, 512);
+  image_info->colorcube_d = g_new (guchar, 512);
   for (i = 0; i < 512; i++)
     {
       r = MIN (nr - 1, i >> 6);
       g = MIN (ng - 1, (i >> 3) & 7);
       b = MIN (nb - 1, i & 7);
-      colorcube_d[i] = pixels[(r * ng + g) * nb + b];
+      image_info->colorcube_d[i] = pixels[(r * ng + g) * nb + b];
     }
 }
 
 /* Try installing a color cube of the specified size.
    Make the colorcube and return TRUE on success */
 static gint
-gdk_rgb_try_colormap (gint nr, gint ng, gint nb)
+gdk_rgb_try_colormap (GdkRgbInfo *image_info, gboolean force,
+                     gint nr, gint ng, gint nb)
 {
   gint r, g, b;
   gint ri, gi, bi;
@@ -181,10 +280,10 @@ gdk_rgb_try_colormap (gint nr, gint ng, gint nb)
   gint idx;
   gint best[256];
 
-  if (nr * ng * nb < gdk_rgb_min_colors)
+  if (!force && nr * ng * nb < gdk_rgb_min_colors)
     return FALSE;
 
-  if (image_info->cmap_alloced)
+  if (image_info->cmap)
     cmap = image_info->cmap;
   else
     cmap = gdk_colormap_get_system ();
@@ -197,34 +296,34 @@ gdk_rgb_try_colormap (gint nr, gint ng, gint nb)
     }
 
 #ifndef GAMMA
-  if (!gdk_rgb_install_cmap)
-  /* find color cube colors that are already present */
-  for (i = 0; i < MIN (256, cmap->size); i++)
-    {
-      r = cmap->colors[i].red >> 8;
-      g = cmap->colors[i].green >> 8;
-      b = cmap->colors[i].blue >> 8;
-      ri = (r * (nr - 1) + 128) >> 8;
-      gi = (g * (ng - 1) + 128) >> 8;
-      bi = (b * (nb - 1) + 128) >> 8;
-      r0 = ri * 255 / (nr - 1);
-      g0 = gi * 255 / (ng - 1);
-      b0 = bi * 255 / (nb - 1);
-      idx = ((ri * nr) + gi) * nb + bi;
-      d2 = (r - r0) * (r - r0) + (g - g0) * (g - g0) + (b - b0) * (b - b0);
-      if (d2 < best[idx]) {
-       if (pixels[idx] < 256)
-         gdk_colors_free (cmap, pixels + idx, 1, 0);
-       else
-         colors_needed--;
-       color = cmap->colors[i];
-       if (!gdk_color_alloc (cmap, &color))
-         return gdk_rgb_cmap_fail ("error allocating system color\n",
-                                   cmap, pixels);
-       pixels[idx] = color.pixel; /* which is almost certainly i */
-       best[idx] = d2;
+  if (cmap == gdk_colormap_get_system())
+    /* find color cube colors that are already present */
+    for (i = 0; i < MIN (256, cmap->size); i++)
+      {
+       r = cmap->colors[i].red >> 8;
+       g = cmap->colors[i].green >> 8;
+       b = cmap->colors[i].blue >> 8;
+       ri = (r * (nr - 1) + 128) >> 8;
+       gi = (g * (ng - 1) + 128) >> 8;
+       bi = (b * (nb - 1) + 128) >> 8;
+       r0 = ri * 255 / (nr - 1);
+       g0 = gi * 255 / (ng - 1);
+       b0 = bi * 255 / (nb - 1);
+       idx = ((ri * nr) + gi) * nb + bi;
+       d2 = (r - r0) * (r - r0) + (g - g0) * (g - g0) + (b - b0) * (b - b0);
+       if (d2 < best[idx]) {
+         if (pixels[idx] < 256)
+           gdk_colors_free (cmap, pixels + idx, 1, 0);
+         else
+           colors_needed--;
+         color = cmap->colors[i];
+         if (!gdk_colormap_alloc_color (cmap, &color, FALSE, FALSE))
+           return gdk_rgb_cmap_fail ("error allocating system color\n",
+                                     cmap, pixels);
+         pixels[idx] = color.pixel; /* which is almost certainly i */
+         best[idx] = d2;
+       }
       }
-    }
 #endif
 
   if (colors_needed)
@@ -258,7 +357,7 @@ gdk_rgb_try_colormap (gint nr, gint ng, gint nb)
              color.blue = 65535 * pow (color.blue / 65535.0, 0.5);
 #endif
 
-             if (!gdk_color_alloc (cmap, &color))
+             if (!gdk_colormap_alloc_color (cmap, &color, FALSE, force))
                {
                  char tmp_str[80];
 
@@ -277,16 +376,16 @@ gdk_rgb_try_colormap (gint nr, gint ng, gint nb)
   image_info->nred_shades = nr;
   image_info->ngreen_shades = ng;
   image_info->nblue_shades = nb;
-  gdk_rgb_make_colorcube (pixels, nr, ng, nb);
-  gdk_rgb_make_colorcube_d (pixels, nr, ng, nb);
+  gdk_rgb_make_colorcube (image_info, pixels, nr, ng, nb);
+  gdk_rgb_make_colorcube_d (image_info, pixels, nr, ng, nb);
   return TRUE;
 }
 
 /* Return TRUE on success. */
 static gboolean
-gdk_rgb_do_colormaps (void)
+gdk_rgb_do_colormaps (GdkRgbInfo *image_info, gboolean force)
 {
-  const gint sizes[][3] = {
+  static const gint sizes[][3] = {
     /*    { 6, 7, 6 }, */
     { 6, 6, 6 }, 
     { 6, 6, 5 }, 
@@ -298,15 +397,41 @@ gdk_rgb_do_colormaps (void)
     { 3, 3, 3 }, 
     { 2, 2, 2 }
   };
-  const gint n_sizes = sizeof(sizes) / (3 * sizeof(gint));
+  static const gint n_sizes = sizeof(sizes) / (3 * sizeof(gint));
   gint i;
 
+  /* Try the possible sizes. If the force parameter is set to TRUE
+   * and all larger sizes fail, force the larger size to succeed -
+   * this will involve allowing closest matches when allocating the
+   * colors
+   */
   for (i = 0; i < n_sizes; i++)
-    if (gdk_rgb_try_colormap (sizes[i][0], sizes[i][1], sizes[i][2]))
+    if (gdk_rgb_try_colormap (image_info,
+                             (i == n_sizes - 1 ) && force,
+                             sizes[i][0], sizes[i][1], sizes[i][2]))
       return TRUE;
   return FALSE;
 }
 
+/* Make a 2 x 2 x 2 colorcube */
+static void
+gdk_rgb_colorcube_222 (GdkRgbInfo *image_info)
+{
+  int i;
+  GdkColor color;
+
+  image_info->colorcube_d = g_new (guchar, 512);
+
+  for (i = 0; i < 8; i++)
+    {
+      color.red = ((i & 4) >> 2) * 65535;
+      color.green = ((i & 2) >> 1) * 65535;
+      color.blue = (i & 1) * 65535;
+      gdk_colormap_alloc_color (image_info->cmap, &color, FALSE, TRUE);
+      image_info->colorcube_d[((i & 4) << 4) | ((i & 2) << 2) | (i & 1)] = color.pixel;
+    }
+}
+
 void
 gdk_rgb_set_verbose (gboolean verbose)
 {
@@ -344,16 +469,6 @@ static guint32
 gdk_rgb_score_visual (GdkVisual *visual)
 {
   guint32 quality, speed, sys, pseudo;
-  gchar* visual_names[] =
-  {
-    "static gray",
-    "grayscale",
-    "static color",
-    "pseudo color",
-    "true color",
-    "direct color",
-  };
-
 
   quality = 0;
   speed = 1;
@@ -373,10 +488,15 @@ gdk_rgb_score_visual (GdkVisual *visual)
       else if (visual->depth == 8)
        quality = 4;
     }
-  else if (visual->type == GDK_VISUAL_PSEUDO_COLOR)
+  else if (visual->type == GDK_VISUAL_PSEUDO_COLOR ||
+          visual->type == GDK_VISUAL_STATIC_COLOR)
     {
       if (visual->depth == 8)
        quality = 4;
+      else if (visual->depth == 4)
+       quality = 2;
+      else if (visual->depth == 1)
+       quality = 1;
     }
   else if (visual->type == GDK_VISUAL_STATIC_GRAY
 #ifdef ENABLE_GRAYSCALE
@@ -397,11 +517,10 @@ gdk_rgb_score_visual (GdkVisual *visual)
 
   sys = (visual == gdk_visual_get_system ());
 
-  pseudo = (visual->type == GDK_VISUAL_PSEUDO_COLOR);
+  pseudo = (visual->type == GDK_VISUAL_PSEUDO_COLOR || visual->type == GDK_VISUAL_TRUE_COLOR);
 
   if (gdk_rgb_verbose)
-    g_print ("Visual 0x%x, type = %s, depth = %d, %x:%x:%x%s; score=%x\n",
-            (gint)(((GdkVisualPrivate *)visual)->xvisual->visualid),
+    g_print ("Visual type = %s, depth = %d, %x:%x:%x%s; score=%x\n",
             visual_names[visual->type],
             visual->depth,
             visual->red_mask,
@@ -413,37 +532,41 @@ gdk_rgb_score_visual (GdkVisual *visual)
   return (quality << 12) | (speed << 8) | (sys << 4) | pseudo;
 }
 
-static void
+static GdkVisual *
 gdk_rgb_choose_visual (void)
 {
-  GList *visuals;
+  GList *visuals, *tmp_list;
   guint32 score, best_score;
   GdkVisual *visual, *best_visual;
 
   visuals = gdk_list_visuals ();
+  tmp_list = visuals;
 
-  best_visual = visuals->data;
+  best_visual = tmp_list->data;
   best_score = gdk_rgb_score_visual (best_visual);
-  visuals = visuals->next;
-  while (visuals)
+  tmp_list = tmp_list->next;
+  while (tmp_list)
     {
-      visual = visuals->data;
+      visual = tmp_list->data;
       score = gdk_rgb_score_visual (visual);
       if (score > best_score)
        {
          best_score = score;
          best_visual = visual;
        }
-      visuals = visuals->next;
+      tmp_list = tmp_list->next;
     }
 
-  image_info->visual = best_visual;
+  g_list_free (visuals);
+
+  return best_visual;
 }
 
-static void gdk_rgb_select_conv (GdkImage *image);
+static void gdk_rgb_select_conv (GdkRgbInfo *image_info, GdkImage *image);
 
 static void
-gdk_rgb_set_gray_cmap (GdkColormap *cmap)
+gdk_rgb_set_gray_cmap (GdkRgbInfo  *image_info,
+                      GdkColormap *cmap)
 {
   gint i;
   GdkColor color;
@@ -457,7 +580,7 @@ gdk_rgb_set_gray_cmap (GdkColormap *cmap)
       color.red = i * 257;
       color.green = i * 257;
       color.blue = i * 257;
-      status = gdk_color_alloc (cmap, &color);
+      status = gdk_colormap_alloc_color (cmap, &color, FALSE, TRUE);
       pixels[i] = color.pixel;
 #ifdef VERBOSE
       g_print ("allocating pixel %d, %x %x %x, result %d\n",
@@ -468,7 +591,7 @@ gdk_rgb_set_gray_cmap (GdkColormap *cmap)
   /* Now, we make fake colorcubes - we ultimately just use the pseudocolor
      methods. */
 
-  colorcube = g_new (guchar, 4096);
+  image_info->colorcube = g_new (guchar, 4096);
 
   for (i = 0; i < 4096; i++)
     {
@@ -479,144 +602,303 @@ gdk_rgb_set_gray_cmap (GdkColormap *cmap)
       b = (i << 4 & 0xf0);
       b = b | b >> 4;
       gray = (g + ((r + b) >> 1)) >> 1;
-      colorcube[i] = pixels[gray];
+      image_info->colorcube[i] = pixels[gray];
     }
 }
 
-void
-gdk_rgb_init (void)
+static gboolean
+gdk_rgb_allocate_images (GdkRgbInfo *image_info,
+                        gint        n_images,
+                        gboolean    shared)
 {
   gint i;
-  gint byte_order[1] = { 1 };
+  
+  for (i = 0; i < n_images; i++)
+    {
+      if (image_info->bitmap)
+       /* Use malloc() instead of g_malloc since X will free() this mem */
+       image_info->static_image[i] = gdk_image_new_bitmap (image_info->visual,
+                                                           (gpointer) malloc (REGION_WIDTH * REGION_HEIGHT >> 3),
+                                                           REGION_WIDTH * (N_REGIONS / n_images), REGION_HEIGHT);
+      else
+       image_info->static_image[i] = gdk_image_new (shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
+                                                    image_info->visual,
+                                                    REGION_WIDTH * (N_REGIONS / n_images), REGION_HEIGHT);
 
-  /* check endian sanity */
-#ifdef WORDS_BIGENDIAN
-  if (((char *)byte_order)[0] == 1)
-    g_error ("gdk_rgb_init: WORDS_BIGENDIAN is defined, but this is a little endian machine.\n\n");
-#else
-  if (((char *)byte_order)[0] != 1)
-    g_error ("gdk_rgb_init: WORDS_BIGENDIAN is not defined, but this is a little endian machine.\n\n");
-#endif
+      if (!image_info->static_image[i])
+       {
+         gint j;
+
+         for (j = 0; j < i; j++)
+           gdk_image_unref (image_info->static_image[i]);
+
+         return FALSE;
+       }
+    }
+  
+  return TRUE;
+}
+
+static void
+gdk_rgb_free_info (GdkRgbInfo *image_info)
+{
+  gint i;
+  GSList *tmp_list;
+
+  
+  if (image_info->stage_buf)
+    g_free (image_info->stage_buf);
+  
+  if (image_info->gray_cmap)
+    gdk_rgb_cmap_free (image_info->gray_cmap);
 
-  if (image_info == NULL)
+  if (image_info->own_gc)
+    gdk_gc_unref (image_info->own_gc);
+
+  for (i = 0; i < image_info->n_images; i++)
+    gdk_image_unref (image_info->static_image[i]);
+
+  if (image_info->colorcube)
+    g_free (image_info->colorcube);
+  
+  if (image_info->colorcube_d)
+    g_free (image_info->colorcube_d);
+
+  tmp_list = image_info->cmap_info_list;
+  while (tmp_list)
     {
-      image_info = g_new0 (GdkRgbInfo, 1);
+      GdkRgbCmapInfo *cmap_info = tmp_list->data;
+      cmap_info->cmap->info_list = g_slist_remove (cmap_info->cmap->info_list, cmap_info);
+      g_free (cmap_info);
+    }
+  g_slist_free (image_info->cmap_info_list);
+  
+  g_free (image_info);
+}
+
+/* Create a GdkRgbInfo for the given visual/colormap pair. If colormap
+ * is NULL, it will be determined and stored in image_info->cmap. 
+ * In this case, image_info->cmap will have an extra refcount which
+ * is owned by the caller. 
+ */
+static GdkRgbInfo *
+gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap)
+{
+  GdkRgbInfo *image_info;
+  gint i;
+
+  image_info = g_new0 (GdkRgbInfo, 1);
 
-      image_info->visual = NULL;
-      image_info->cmap = NULL;
+  image_info->visual = visual;
+  image_info->cmap = NULL;
 
-      image_info->color_pixels = NULL;
-      image_info->gray_pixels = NULL;
-      image_info->reserved_pixels = NULL;
+  image_info->nred_shades = 6;
+  image_info->ngreen_shades = 6;
+  image_info->nblue_shades = 4;
+  image_info->ngray_shades = 24;
+  image_info->nreserved = 0;
 
-      image_info->nred_shades = 6;
-      image_info->ngreen_shades = 6;
-      image_info->nblue_shades = 4;
-      image_info->ngray_shades = 24;
-      image_info->nreserved = 0;
+  image_info->bpp = 0;
+  image_info->cmap_alloced = FALSE;
+  image_info->gamma = 1.0;
 
-      image_info->bpp = 0;
-      image_info->cmap_alloced = FALSE;
-      image_info->gamma = 1.0;
+  image_info->stage_buf = NULL;
 
-      image_info->stage_buf = NULL;
+  image_info->own_gc = NULL;
 
-      gdk_rgb_choose_visual ();
+  image_info->cmap = colormap;
 
-      if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
+  if ((image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
+       image_info->visual->type == GDK_VISUAL_STATIC_COLOR) &&
+      image_info->visual->depth < 8 &&
+      image_info->visual->depth >= 3)
+    {
+      if (!image_info->cmap)
+       image_info->cmap = gdk_colormap_ref (gdk_colormap_get_system ());
+      
+      gdk_rgb_colorcube_222 (image_info);
+    }
+  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
+    {
+      if (!image_info->cmap &&
+         (gdk_rgb_install_cmap || image_info->visual != gdk_visual_get_system ()))
        {
-         if (gdk_rgb_install_cmap ||
-             image_info->visual != gdk_visual_get_system ())
-           {
-             image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
-             image_info->cmap_alloced = TRUE;
-           }
-         if (!gdk_rgb_do_colormaps ())
-           {
-             image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
-             image_info->cmap_alloced = TRUE;
-             gdk_rgb_do_colormaps ();
-           }
-         if (gdk_rgb_verbose)
-           g_print ("color cube: %d x %d x %d\n",
-                    image_info->nred_shades,
-                    image_info->ngreen_shades,
-                    image_info->nblue_shades);
-
-         if (!image_info->cmap_alloced)
-             image_info->cmap = gdk_colormap_get_system ();
+         image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
+         image_info->cmap_alloced = TRUE;
+       }
+      if (!gdk_rgb_do_colormaps (image_info, image_info->cmap != NULL))
+       {
+         image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
+         image_info->cmap_alloced = TRUE;
+         gdk_rgb_do_colormaps (image_info, TRUE);
        }
+      if (gdk_rgb_verbose)
+       g_print ("color cube: %d x %d x %d\n",
+                image_info->nred_shades,
+                image_info->ngreen_shades,
+                image_info->nblue_shades);
+
+      if (!image_info->cmap)
+       image_info->cmap = gdk_colormap_ref (gdk_colormap_get_system ());
+    }
 #ifdef ENABLE_GRAYSCALE
-      else if (image_info->visual->type == GDK_VISUAL_GRAYSCALE)
+  else if (image_info->visual->type == GDK_VISUAL_GRAYSCALE)
+    {
+      if (!image_info->cmap)
        {
          image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
-         gdk_rgb_set_gray_cmap (image_info->cmap);
          image_info->cmap_alloced = TRUE;
-       }
+       }
+      
+      gdk_rgb_set_gray_cmap (image_info, image_info->cmap);
+    }
 #endif
-      else
+  else
+    {
+      if (!image_info->cmap)
        {
          /* Always install colormap in direct color. */
          if (image_info->visual->type != GDK_VISUAL_DIRECT_COLOR &&
              image_info->visual == gdk_visual_get_system ())
-           image_info->cmap = gdk_colormap_get_system ();
+           image_info->cmap = gdk_colormap_ref (gdk_colormap_get_system ());
          else
            {
              image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
              image_info->cmap_alloced = TRUE;
            }
        }
+    }
 
-      for (i = 0; i < N_IMAGES; i++)
-       static_image[i] = gdk_image_new (GDK_IMAGE_FASTEST,
-                                        image_info->visual,
-                                        IMAGE_WIDTH, IMAGE_HEIGHT);
-
-      image_info->bpp = static_image[0]->bpp;
+  image_info->bitmap = (image_info->visual->depth == 1);
 
-      gdk_rgb_select_conv (static_image[0]);
+  /* Try to allocate as few possible shared images */
+  for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
+    {
+      if (gdk_rgb_allocate_images (image_info, possible_n_images[i], TRUE))
+       {
+         image_info->n_images = possible_n_images[i];
+         break;
+       }
+    }
 
+  /* If that fails, just allocate N_REGIONS normal images */
+  if (i == G_N_ELEMENTS (possible_n_images))
+    {
+      gdk_rgb_allocate_images (image_info, N_REGIONS, FALSE);
+      image_info->n_images = N_REGIONS;
     }
+
+  image_info->bpp = image_info->static_image[0]->bpp;
+
+  image_info->static_image_idx = 0;
+
+  image_info->horiz_y = REGION_HEIGHT;
+  image_info->vert_x = REGION_WIDTH;
+  image_info->tile_x = REGION_WIDTH;
+  image_info->tile_y1 = image_info->tile_y2 = REGION_HEIGHT;
+
+  gdk_rgb_select_conv (image_info, image_info->static_image[0]);
+
+  if (!gdk_rgb_quark)
+    gdk_rgb_quark = g_quark_from_static_string (gdk_rgb_key);
+
+  g_object_set_qdata_full (G_OBJECT (image_info->cmap), gdk_rgb_quark,
+                          image_info, (GDestroyNotify)gdk_rgb_free_info);
+  return image_info;
 }
 
-/* convert an rgb value into an X pixel code */
-gulong
-gdk_rgb_xpixel_from_rgb (guint32 rgb)
+void
+gdk_rgb_init (void)
 {
-  gulong pixel;
+  static const gint byte_order[1] = { 1 };
 
-  if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
-    pixel = colorcube[((rgb & 0xf00000) >> 12) |
-                    ((rgb & 0xf000) >> 8) |
-                    ((rgb & 0xf0) >> 4)];
-    else
-      {
+  /* check endian sanity */
+#if G_BYTE_ORDER == G_BIG_ENDIAN
+  if (((char *)byte_order)[0] == 1)
+    g_error ("gdk_rgb_init: compiled for big endian, but this is a little endian machine.\n\n");
+#else
+  if (((char *)byte_order)[0] != 1)
+    g_error ("gdk_rgb_init: compiled for little endian, but this is a big endian machine.\n\n");
+#endif
+}
+
+static GdkRgbInfo *
+gdk_rgb_get_info_from_colormap (GdkColormap *cmap)
+{
+  GdkRgbInfo *image_info;
+
+  if (!gdk_rgb_quark)
+    gdk_rgb_quark = g_quark_from_static_string (gdk_rgb_key);
+
+  image_info = g_object_get_qdata (G_OBJECT (cmap), gdk_rgb_quark);
+  if (!image_info)
+    image_info = gdk_rgb_create_info (gdk_colormap_get_visual (cmap), cmap);
+
+  return image_info;
+}
+
+static gulong
+gdk_rgb_xpixel_from_rgb_internal (GdkColormap *colormap,
+                                 guint16 r, guint16 g, guint16 b)
+{
+  gulong pixel = 0;
+
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_colormap (colormap);
+
+  if (image_info->bitmap)
+    {
+      return (r + (g << 1) + b) > 131070;
+    }
+  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
+    pixel = image_info->colorcube[((r & 0xf000) >> 4) |
+                                 ((g & 0xf000) >> 8) |
+                                 ((b & 0xf000) >> 12)];
+  else if (image_info->visual->depth < 8 &&
+          image_info->visual->type == GDK_VISUAL_STATIC_COLOR)
+    {
+      pixel = image_info->colorcube_d[((r & 0x8000) >> 9) |
+                                     ((g & 0x8000) >> 12) |
+                                     ((b & 0x8000) >> 15)];
+    }
+  else if (image_info->visual->type == GDK_VISUAL_TRUE_COLOR ||
+          image_info->visual->type == GDK_VISUAL_DIRECT_COLOR)
+    {
 #ifdef VERBOSE
-       g_print ("shift, prec: r %d %d g %d %d b %d %d\n",
-                image_info->visual->red_shift,
-                image_info->visual->red_prec,
-                image_info->visual->green_shift,
-                image_info->visual->green_prec,
-                image_info->visual->blue_shift,
-                image_info->visual->blue_prec);
+      g_print ("shift, prec: r %d %d g %d %d b %d %d\n",
+              image_info->visual->red_shift,
+              image_info->visual->red_prec,
+              image_info->visual->green_shift,
+              image_info->visual->green_prec,
+              image_info->visual->blue_shift,
+              image_info->visual->blue_prec);
 #endif
 
-       pixel = (((((rgb & 0xff0000) >> 16) >>
-                  (8 - image_info->visual->red_prec)) <<
-                 image_info->visual->red_shift) +
-                ((((rgb & 0xff00) >> 8)  >>
-                  (8 - image_info->visual->green_prec)) <<
-                 image_info->visual->green_shift) +
-                (((rgb & 0xff) >>
-                  (8 - image_info->visual->blue_prec)) <<
-                 image_info->visual->blue_shift));
-
-       ;
-      }
+      pixel = (((r >> (16 - image_info->visual->red_prec)) << image_info->visual->red_shift) +
+              ((g >> (16 - image_info->visual->green_prec)) << image_info->visual->green_shift) +
+              ((b >> (16 - image_info->visual->blue_prec)) << image_info->visual->blue_shift));
+    }
+  else if (image_info->visual->type == GDK_VISUAL_STATIC_GRAY ||
+          image_info->visual->type == GDK_VISUAL_GRAYSCALE)
+    {
+      int gray = r + g * 2 + b;
+      return gray >> (18 - image_info->visual->depth);
+    }
 
   return pixel;
 }
 
+/* convert an rgb value into an X pixel code */
+gulong
+gdk_rgb_xpixel_from_rgb (guint32 rgb)
+{
+  guint32 r = rgb & 0xff0000;
+  guint32 g = rgb & 0xff00;
+  guint32 b = rgb & 0xff;
+
+  return gdk_rgb_xpixel_from_rgb_internal (gdk_rgb_get_colormap(),
+                                          (r >> 8) + (r >> 16), g + (g >> 8), b + (b << 8));
+}
+
 void
 gdk_rgb_gc_set_foreground (GdkGC *gc, guint32 rgb)
 {
@@ -635,13 +917,20 @@ gdk_rgb_gc_set_background (GdkGC *gc, guint32 rgb)
   gdk_gc_set_background (gc, &color);
 }
 
-#ifndef WORDS_BIGENDIAN
+void
+gdk_rgb_find_color (GdkColormap *colormap, GdkColor *color)
+{
+  color->pixel = gdk_rgb_xpixel_from_rgb_internal (colormap,
+                                                  color->red, color->green, color->blue);
+}
+
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
 #define HAIRY_CONVERT_8
 #endif
 
 #ifdef HAIRY_CONVERT_8
 static void
-gdk_rgb_convert_8 (GdkImage *image,
+gdk_rgb_convert_8 (GdkRgbInfo *image_info, GdkImage *image,
                   gint x0, gint y0, gint width, gint height,
                   guchar *buf, int rowstride,
                   gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -651,6 +940,7 @@ gdk_rgb_convert_8 (GdkImage *image,
   guchar *obuf, *obptr;
   guchar *bptr, *bp2;
   gint r, g, b;
+  guchar *colorcube = image_info->colorcube;
 
   bptr = buf;
   bpl = image->bpl;
@@ -659,7 +949,7 @@ gdk_rgb_convert_8 (GdkImage *image,
     {
       bp2 = bptr;
       obptr = obuf;
-      if (width < 8 || (((unsigned long)obuf | (unsigned long) bp2) & 3))
+      if (((unsigned long)obuf | (unsigned long) bp2) & 3)
        {
          for (x = 0; x < width; x++)
            {
@@ -716,7 +1006,7 @@ gdk_rgb_convert_8 (GdkImage *image,
 }
 #else
 static void
-gdk_rgb_convert_8 (GdkImage *image,
+gdk_rgb_convert_8 (GdkRgbInfo *image_info, GdkImage *image,
                   gint x0, gint y0, gint width, gint height,
                   guchar *buf, int rowstride,
                   gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -726,6 +1016,7 @@ gdk_rgb_convert_8 (GdkImage *image,
   guchar *obuf, *obptr;
   guchar *bptr, *bp2;
   gint r, g, b;
+  guchar *colorcube = image_info->colorcube;
 
   bptr = buf;
   bpl = image->bpl;
@@ -757,8 +1048,9 @@ gdk_rgb_convert_8 (GdkImage *image,
    public domain. */
 
 #define DM_WIDTH 128
+#define DM_WIDTH_SHIFT 7
 #define DM_HEIGHT 128
-static guchar DM[128][128] =
+static const guchar DM[128][128] =
 {
   { 0, 41, 23, 5, 17, 39, 7, 15, 62, 23, 40, 51, 31, 47, 9, 32, 52, 27, 57, 25, 6, 61, 27, 52, 37, 7, 40, 63, 18, 36, 10, 42, 25, 62, 45, 34, 20, 42, 37, 14, 35, 29, 50, 10, 61, 2, 40, 8, 37, 12, 58, 22, 5, 41, 10, 39, 0, 60, 11, 46, 2, 55, 38, 17, 36, 59, 13, 54, 37, 56, 8, 29, 16, 13, 63, 22, 41, 55, 7, 20, 49, 14, 23, 55, 37, 23, 19, 36, 15, 49, 23, 63, 30, 14, 38, 27, 53, 13, 22, 41, 19, 31, 7, 19, 50, 30, 49, 16, 3, 32, 56, 40, 29, 34, 8, 48, 19, 45, 4, 51, 12, 46, 35, 49, 16, 42, 12, 62 },
   { 30, 57, 36, 54, 47, 34, 52, 27, 43, 4, 28, 7, 17, 36, 62, 13, 44, 7, 18, 48, 33, 21, 44, 14, 30, 47, 12, 33, 5, 55, 31, 58, 13, 30, 4, 17, 52, 10, 60, 26, 46, 0, 39, 27, 42, 22, 47, 25, 60, 32, 9, 38, 48, 17, 59, 30, 49, 18, 34, 25, 51, 19, 5, 48, 21, 8, 28, 46, 1, 32, 41, 19, 54, 47, 37, 18, 28, 11, 44, 30, 39, 56, 2, 33, 8, 42, 61, 28, 58, 8, 46, 9, 41, 4, 58, 7, 21, 48, 59, 10, 52, 14, 42, 57, 12, 25, 7, 53, 42, 24, 11, 50, 17, 59, 42, 2, 36, 60, 32, 17, 63, 29, 21, 7, 59, 32, 24, 39 },
@@ -863,7 +1155,7 @@ static guchar DM[128][128] =
   { 46, 2, 56, 11, 41, 1, 49, 6, 27, 47, 2, 48, 5, 32, 37, 3, 13, 19, 32, 1, 55, 28, 60, 17, 43, 59, 32, 20, 49, 16, 55, 23, 14, 46, 2, 36, 6, 30, 20, 49, 12, 47, 35, 14, 21, 60, 29, 14, 35, 24, 46, 1, 56, 29, 53, 8, 33, 23, 56, 1, 35, 46, 20, 39, 26, 4, 53, 28, 17, 38, 60, 34, 48, 9, 55, 15, 46, 7, 41, 31, 60, 24, 16, 36, 1, 59, 19, 52, 35, 6, 55, 11, 59, 33, 7, 57, 4, 29, 48, 1, 19, 26, 37, 30, 18, 63, 37, 6, 59, 1, 40, 24, 56, 33, 46, 22, 35, 7, 24, 53, 39, 5, 26, 45, 55, 18, 62, 7 },
   { 20, 60, 29, 34, 20, 62, 33, 52, 10, 36, 13, 60, 41, 21, 50, 27, 56, 49, 8, 51, 21, 45, 11, 48, 8, 23, 53, 3, 29, 44, 5, 52, 9, 32, 50, 17, 43, 56, 3, 38, 24, 10, 62, 25, 51, 9, 33, 49, 61, 7, 30, 62, 22, 19, 2, 42, 63, 5, 49, 18, 60, 15, 52, 7, 43, 56, 23, 50, 5, 50, 2, 20, 41, 30, 1, 52, 22, 61, 14, 26, 3, 43, 53, 7, 47, 28, 11, 14, 23, 58, 33, 25, 47, 13, 50, 17, 40, 54, 34, 60, 41, 6, 59, 14, 50, 7, 25, 55, 20, 42, 51, 8, 27, 4, 16, 60, 28, 50, 44, 3, 22, 49, 63, 12, 33, 1, 43, 31 },
   { 36, 5, 46, 8, 44, 24, 13, 39, 25, 57, 31, 18, 8, 52, 10, 45, 6, 30, 36, 24, 63, 4, 33, 26, 57, 40, 15, 56, 37, 12, 40, 25, 37, 58, 11, 63, 21, 45, 16, 60, 31, 53, 18, 33, 3, 45, 23, 0, 20, 54, 40, 15, 50, 38, 60, 16, 25, 42, 29, 38, 7, 41, 25, 62, 18, 33, 8, 35, 42, 16, 32, 56, 12, 39, 59, 19, 34, 9, 49, 38, 57, 12, 21, 50, 14, 40, 61, 44, 50, 9, 49, 19, 3, 29, 35, 62, 12, 24, 7, 18, 52, 32, 10, 46, 21, 41, 32, 11, 36, 29, 14, 34, 60, 38, 54, 11, 41, 14, 19, 57, 32, 16, 7, 41, 51, 25, 14, 57 },
-  { 53, 18, 26, 50, 15, 58, 4, 63, 17, 43, 7, 40, 61, 35, 15, 41, 23, 60, 16, 38, 14, 42, 19, 50, 0, 31, 10, 46, 27, 63, 18, 60, 0, 20, 29, 39, 8, 26, 37, 5, 42, 0, 44, 39, 57, 17, 58, 41, 28, 37, 4, 32, 9, 44, 12, 31, 54, 10, 59, 14, 27, 53, 12, 36, 0, 47, 13, 64, 21, 58, 10, 24, 50, 27, 4, 26, 44, 53, 31, 0, 18, 42, 29, 33, 57, 4, 32, 26, 0, 38, 16, 61, 41, 53, 20, 0, 42, 44, 49, 27, 10, 56, 39, 0, 57, 15, 53, 49, 3, 61, 22, 47, 17, 5, 49, 26, 2, 63, 39, 10, 47, 27, 37, 23, 4, 59, 38, 10 },
+  { 53, 18, 26, 50, 15, 58, 4, 63, 17, 43, 7, 40, 61, 35, 15, 41, 23, 60, 16, 38, 14, 42, 19, 50, 0, 31, 10, 46, 27, 63, 18, 60, 0, 20, 29, 39, 8, 26, 37, 5, 42, 0, 44, 39, 57, 17, 58, 41, 28, 37, 4, 32, 9, 44, 12, 31, 54, 10, 59, 14, 27, 53, 12, 36, 0, 47, 13, 63, 21, 58, 10, 24, 50, 27, 4, 26, 44, 53, 31, 0, 18, 42, 29, 33, 57, 4, 32, 26, 0, 38, 16, 61, 41, 53, 20, 0, 42, 44, 49, 27, 10, 56, 39, 0, 57, 15, 53, 49, 3, 61, 22, 47, 17, 5, 49, 26, 2, 63, 39, 10, 47, 27, 37, 23, 4, 59, 38, 10 },
   { 23, 39, 61, 3, 37, 28, 48, 31, 0, 34, 51, 23, 2, 26, 58, 0, 53, 11, 46, 1, 57, 29, 52, 14, 37, 61, 21, 35, 2, 49, 7, 34, 47, 55, 4, 33, 54, 13, 58, 52, 19, 50, 22, 7, 13, 29, 36, 11, 51, 17, 60, 25, 55, 4, 34, 51, 0, 35, 20, 48, 32, 3, 51, 30, 59, 28, 40, 3, 46, 29, 54, 43, 7, 62, 47, 11, 39, 4, 23, 46, 55, 8, 63, 5, 25, 37, 18, 46, 21, 56, 31, 5, 36, 8, 45, 58, 26, 15, 2, 36, 47, 21, 29, 44, 25, 34, 3, 27, 43, 10, 52, 0, 45, 30, 24, 36, 43, 18, 34, 59, 0, 52, 61, 15, 44, 19, 30, 49 },
   { 0, 27, 12, 43, 54, 9, 22, 53, 21, 46, 15, 55, 29, 47, 20, 33, 39, 28, 59, 35, 9, 44, 5, 24, 47, 7, 52, 17, 56, 22, 30, 42, 14, 26, 45, 18, 49, 1, 24, 34, 11, 27, 55, 32, 61, 47, 2, 56, 6, 44, 13, 47, 36, 27, 58, 22, 16, 47, 40, 4, 57, 38, 21, 45, 16, 9, 56, 26, 11, 38, 0, 22, 36, 17, 33, 57, 16, 30, 62, 15, 35, 40, 20, 45, 59, 10, 54, 8, 63, 13, 52, 27, 22, 57, 28, 12, 32, 51, 55, 22, 63, 4, 16, 54, 12, 62, 45, 19, 58, 13, 32, 40, 20, 56, 7, 57, 9, 54, 6, 29, 42, 21, 8, 55, 35, 47, 6, 41 },
   { 56, 33, 58, 32, 19, 35, 42, 6, 59, 11, 38, 5, 49, 12, 62, 7, 52, 17, 5, 25, 54, 20, 61, 31, 54, 27, 41, 11, 44, 5, 59, 12, 36, 51, 10, 61, 28, 41, 48, 9, 43, 63, 5, 40, 20, 8, 49, 26, 34, 21, 58, 1, 18, 45, 7, 39, 61, 26, 8, 50, 23, 10, 63, 5, 55, 37, 19, 49, 52, 15, 59, 47, 13, 54, 1, 25, 42, 58, 10, 48, 3, 27, 50, 1, 17, 48, 34, 41, 16, 40, 2, 45, 10, 39, 17, 61, 5, 38, 19, 9, 41, 31, 60, 38, 5, 23, 36, 8, 30, 55, 24, 63, 12, 48, 14, 51, 31, 20, 45, 25, 12, 50, 32, 2, 28, 11, 62, 14 },
@@ -892,8 +1184,9 @@ static guchar DM[128][128] =
 
 #else
 #define DM_WIDTH 8
+#define DM_WIDTH_SHIFT 3
 #define DM_HEIGHT 8
-static guchar DM[8][8] =
+static const guchar DM[8][8] =
 {
   { 0,  32, 8,  40, 2,  34, 10, 42 },
   { 48, 16, 56, 24, 50, 18, 58, 26 },
@@ -906,8 +1199,30 @@ static guchar DM[8][8] =
 };
 #endif
 
+static guint32 *DM_565 = NULL;
+
+static void
+gdk_rgb_preprocess_dm_565 (void)
+{
+  int i;
+  guint32 dith;
+
+  if (DM_565 == NULL)
+    {
+      DM_565 = g_new (guint32, DM_WIDTH * DM_HEIGHT);
+      for (i = 0; i < DM_WIDTH * DM_HEIGHT; i++)
+       {
+         dith = DM[0][i] >> 3;
+         DM_565[i] = (dith << 20) | dith | (((7 - dith) >> 1) << 10);
+#ifdef VERBOSE
+         g_print ("%i %x %x\n", i, dith, DM_565[i]);
+#endif
+       }
+    }
+}
+
 static void
-gdk_rgb_convert_8_d666 (GdkImage *image,
+gdk_rgb_convert_8_d666 (GdkRgbInfo *image_info, GdkImage *image,
                        gint x0, gint y0, gint width, gint height,
                        guchar *buf, int rowstride,
                        gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -917,8 +1232,9 @@ gdk_rgb_convert_8_d666 (GdkImage *image,
   guchar *obuf, *obptr;
   guchar *bptr, *bp2;
   gint r, g, b;
-  guchar *dmp;
+  const guchar *dmp;
   gint dith;
+  guchar *colorcube_d = image_info->colorcube_d;
 
   bptr = buf;
   bpl = image->bpl;
@@ -946,7 +1262,7 @@ gdk_rgb_convert_8_d666 (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_8_d (GdkImage *image,
+gdk_rgb_convert_8_d (GdkRgbInfo *image_info, GdkImage *image,
                     gint x0, gint y0, gint width, gint height,
                     guchar *buf, int rowstride,
                     gint x_align, gint y_align,
@@ -957,9 +1273,10 @@ gdk_rgb_convert_8_d (GdkImage *image,
   guchar *obuf, *obptr;
   guchar *bptr, *bp2;
   gint r, g, b;
-  guchar *dmp;
+  const guchar *dmp;
   gint dith;
   gint rs, gs, bs;
+  guchar *colorcube_d = image_info->colorcube_d;
 
   bptr = buf;
   bpl = image->bpl;
@@ -990,7 +1307,7 @@ gdk_rgb_convert_8_d (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_8_indexed (GdkImage *image,
+gdk_rgb_convert_8_indexed (GdkRgbInfo *image_info, GdkImage *image,
                           gint x0, gint y0, gint width, gint height,
                           guchar *buf, int rowstride,
                           gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1001,8 +1318,9 @@ gdk_rgb_convert_8_indexed (GdkImage *image,
   guchar *bptr, *bp2;
   guchar c;
   guchar *lut;
+  GdkRgbCmapInfo *cmap_info = gdk_rgb_cmap_get_info (cmap, image_info);
 
-  lut = cmap->lut;
+  lut = cmap_info->lut;
   bptr = buf;
   bpl = image->bpl;
   obuf = ((guchar *)image->mem) + y0 * bpl + x0;
@@ -1022,7 +1340,7 @@ gdk_rgb_convert_8_indexed (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_gray8 (GdkImage *image,
+gdk_rgb_convert_gray8 (GdkRgbInfo *image_info, GdkImage *image,
                       gint x0, gint y0, gint width, gint height,
                       guchar *buf, int rowstride,
                       gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1054,7 +1372,7 @@ gdk_rgb_convert_gray8 (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_gray8_gray (GdkImage *image,
+gdk_rgb_convert_gray8_gray (GdkRgbInfo *image_info, GdkImage *image,
                            gint x0, gint y0, gint width, gint height,
                            guchar *buf, int rowstride,
                            gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1075,7 +1393,7 @@ gdk_rgb_convert_gray8_gray (GdkImage *image,
     }
 }
 
-#ifndef WORDS_BIGENDIAN
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
 #define HAIRY_CONVERT_565
 #endif
 
@@ -1089,7 +1407,7 @@ gdk_rgb_convert_gray8_gray (GdkImage *image,
    words (i.e. 4 24-bit pixels), does a lot of shifting and masking,
    then writes 2 words. */
 static void
-gdk_rgb_convert_565 (GdkImage *image,
+gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image,
                     gint x0, gint y0, gint width, gint height,
                     guchar *buf, int rowstride,
                     gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1107,7 +1425,7 @@ gdk_rgb_convert_565 (GdkImage *image,
     {
       bp2 = bptr;
       obptr = obuf;
-      if (width < 8 || (((unsigned long)obuf | (unsigned long) bp2) & 3))
+      if (((unsigned long)obuf | (unsigned long) bp2) & 3)
        {
          for (x = 0; x < width; x++)
            {
@@ -1186,7 +1504,7 @@ gdk_rgb_convert_565 (GdkImage *image,
    shifting and masking, then writes 2 words.
 */
 static void
-gdk_rgb_convert_565 (GdkImage *image,
+gdk_rgb_convert_565 (GdkRgbInfo *image_info, GdkImage *image,
                     gint x0, gint y0, gint width, gint height,
                     guchar *buf, int rowstride,
                     gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1220,7 +1538,7 @@ gdk_rgb_convert_565 (GdkImage *image,
 
 #ifdef HAIRY_CONVERT_565
 static void
-gdk_rgb_convert_565_gray (GdkImage *image,
+gdk_rgb_convert_565_gray (GdkRgbInfo *image_info, GdkImage *image,
                          gint x0, gint y0, gint width, gint height,
                          guchar *buf, int rowstride,
                          gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1238,7 +1556,7 @@ gdk_rgb_convert_565_gray (GdkImage *image,
     {
       bp2 = bptr;
       obptr = obuf;
-      if (width < 8 || (((unsigned long)obuf | (unsigned long) bp2) & 3))
+      if (((unsigned long)obuf | (unsigned long) bp2) & 3)
        {
          for (x = 0; x < width; x++)
            {
@@ -1288,7 +1606,7 @@ gdk_rgb_convert_565_gray (GdkImage *image,
 }
 #else
 static void
-gdk_rgb_convert_565_gray (GdkImage *image,
+gdk_rgb_convert_565_gray (GdkRgbInfo *image_info, GdkImage *image,
                          gint x0, gint y0, gint width, gint height,
                          guchar *buf, int rowstride,
                          gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1319,7 +1637,7 @@ gdk_rgb_convert_565_gray (GdkImage *image,
 #endif
 
 static void
-gdk_rgb_convert_565_br (GdkImage *image,
+gdk_rgb_convert_565_br (GdkRgbInfo *image_info, GdkImage *image,
                        gint x0, gint y0, gint width, gint height,
                        guchar *buf, int rowstride,
                        gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1354,48 +1672,181 @@ gdk_rgb_convert_565_br (GdkImage *image,
     }
 }
 
+/* Thanks to Ray Lehtiniemi for a patch that resulted in a ~25% speedup
+   in this mode. */
+#ifdef HAIRY_CONVERT_565
 static void
-gdk_rgb_convert_565_d (GdkImage *image,
-                      gint x0, gint y0, gint width, gint height,
-                      guchar *buf, int rowstride,
-                      gint x_align, gint y_align, GdkRgbCmap *cmap)
+gdk_rgb_convert_565_d (GdkRgbInfo *image_info, GdkImage *image,
+                    gint x0, gint y0, gint width, gint height,
+                    guchar *buf, int rowstride,
+                    gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
+  /* Now this is what I'd call some highly tuned code! */
   int x, y;
-  guchar *obuf;
+  guchar *obuf, *obptr;
   gint bpl;
   guchar *bptr, *bp2;
-  guchar r, g, b;
-  gint dith;
-  gint r1, g1, b1;
-  guchar *dmp;
 
+  width += x_align;
+  height += y_align;
+  
   bptr = buf;
   bpl = image->bpl;
   obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 2;
-  for (y = 0; y < height; y++)
+  for (y = y_align; y < height; y++)
     {
-      dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
+      guint32 *dmp = DM_565 + ((y & (DM_HEIGHT - 1)) << DM_WIDTH_SHIFT);
       bp2 = bptr;
-      for (x = 0; x < width; x++)
+      obptr = obuf;
+      if (((unsigned long)obuf | (unsigned long) bp2) & 3)
        {
-         r = *bp2++;
-         g = *bp2++;
-         b = *bp2++;
-         dith = dmp[(x_align + x) & (DM_WIDTH - 1)] >> 3;
-         r1 = r + dith;
-         g1 = g + ((7 - dith) >> 1);
-         b1 = b + dith;
-         ((unsigned short *)obuf)[x] = (((r1 - (r1 >> 5)) & 0xf8) << 8) |
-           (((g1 - (g1 >> 6)) & 0xfc) << 3) |
-           (((b1 - (b1 >> 5)) & 0xf8) >> 3);
+         for (x = x_align; x < width; x++)
+           {
+             gint32 rgb = *bp2++ << 20;
+             rgb += *bp2++ << 10;
+             rgb += *bp2++;
+             rgb += dmp[x & (DM_WIDTH - 1)];
+             rgb += 0x10040100
+               - ((rgb & 0x1e0001e0) >> 5)
+               - ((rgb & 0x00070000) >> 6);
+
+             ((unsigned short *)obptr)[0] =
+               ((rgb & 0x0f800000) >> 12) |
+               ((rgb & 0x0003f000) >> 7) |
+               ((rgb & 0x000000f8) >> 3);
+             obptr += 2;
+           }
+       }
+      else
+       {
+         for (x = x_align; x < width - 3; x += 4)
+           {
+             guint32 r1b0g0r0;
+             guint32 g2r2b1g1;
+             guint32 b3g3r3b2;
+             guint32 rgb02, rgb13;
+
+             r1b0g0r0 = ((guint32 *)bp2)[0];
+             g2r2b1g1 = ((guint32 *)bp2)[1];
+             b3g3r3b2 = ((guint32 *)bp2)[2];
+             rgb02 =
+               ((r1b0g0r0 & 0xff) << 20) +
+               ((r1b0g0r0 & 0xff00) << 2) +
+               ((r1b0g0r0 & 0xff0000) >> 16) +
+               dmp[x & (DM_WIDTH - 1)];
+             rgb02 += 0x10040100
+               - ((rgb02 & 0x1e0001e0) >> 5)
+               - ((rgb02 & 0x00070000) >> 6);
+             rgb13 =
+               ((r1b0g0r0 & 0xff000000) >> 4) +
+               ((g2r2b1g1 & 0xff) << 10) +
+               ((g2r2b1g1 & 0xff00) >> 8) +
+               dmp[(x + 1) & (DM_WIDTH - 1)];
+             rgb13 += 0x10040100
+               - ((rgb13 & 0x1e0001e0) >> 5)
+               - ((rgb13 & 0x00070000) >> 6);
+             ((guint32 *)obptr)[0] =
+               ((rgb02 & 0x0f800000) >> 12) |
+               ((rgb02 & 0x0003f000) >> 7) |
+               ((rgb02 & 0x000000f8) >> 3) |
+               ((rgb13 & 0x0f800000) << 4) |
+               ((rgb13 & 0x0003f000) << 9) |
+               ((rgb13 & 0x000000f8) << 13);
+             rgb02 =
+               ((g2r2b1g1 & 0xff0000) << 4) +
+               ((g2r2b1g1 & 0xff000000) >> 14) +
+               (b3g3r3b2 & 0xff) +
+               dmp[(x + 2) & (DM_WIDTH - 1)];
+             rgb02 += 0x10040100
+               - ((rgb02 & 0x1e0001e0) >> 5)
+               - ((rgb02 & 0x00070000) >> 6);
+             rgb13 =
+               ((b3g3r3b2 & 0xff00) << 12) +
+               ((b3g3r3b2 & 0xff0000) >> 6) +
+               ((b3g3r3b2 & 0xff000000) >> 24) +
+               dmp[(x + 3) & (DM_WIDTH - 1)];
+             rgb13 += 0x10040100
+               - ((rgb13 & 0x1e0001e0) >> 5)
+               - ((rgb13 & 0x00070000) >> 6);
+             ((guint32 *)obptr)[1] =
+               ((rgb02 & 0x0f800000) >> 12) |
+               ((rgb02 & 0x0003f000) >> 7) |
+               ((rgb02 & 0x000000f8) >> 3) |
+               ((rgb13 & 0x0f800000) << 4) |
+               ((rgb13 & 0x0003f000) << 9) |
+               ((rgb13 & 0x000000f8) << 13);
+             bp2 += 12;
+             obptr += 8;
+           }
+         for (; x < width; x++)
+           {
+             gint32 rgb = *bp2++ << 20;
+             rgb += *bp2++ << 10;
+             rgb += *bp2++;
+             rgb += dmp[x & (DM_WIDTH - 1)];
+             rgb += 0x10040100
+               - ((rgb & 0x1e0001e0) >> 5)
+               - ((rgb & 0x00070000) >> 6);
+
+             ((unsigned short *)obptr)[0] =
+               ((rgb & 0x0f800000) >> 12) |
+               ((rgb & 0x0003f000) >> 7) |
+               ((rgb & 0x000000f8) >> 3);
+             obptr += 2;
+           }
        }
       bptr += rowstride;
       obuf += bpl;
     }
 }
+#else
+static void
+gdk_rgb_convert_565_d (GdkRgbInfo *image_info, GdkImage *image,
+                       gint x0, gint y0, gint width, gint height,
+                       guchar *buf, int rowstride,
+                       gint x_align, gint y_align, GdkRgbCmap *cmap)
+{
+  int x, y;
+  guchar *obuf;
+  gint bpl;
+  guchar *bptr;
+
+  width += x_align;
+  height += y_align;
+  
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + (x0 - x_align) * 2;
+
+  for (y = y_align; y < height; y++)
+    {
+      guint32 *dmp = DM_565 + ((y & (DM_HEIGHT - 1)) << DM_WIDTH_SHIFT);
+      guchar *bp2 = bptr;
+
+      for (x = x_align; x < width; x++)
+        {
+          gint32 rgb = *bp2++ << 20;
+          rgb += *bp2++ << 10;
+          rgb += *bp2++;
+         rgb += dmp[x & (DM_WIDTH - 1)];
+          rgb += 0x10040100
+            - ((rgb & 0x1e0001e0) >> 5)
+            - ((rgb & 0x00070000) >> 6);
+
+          ((unsigned short *)obuf)[x] =
+            ((rgb & 0x0f800000) >> 12) |
+            ((rgb & 0x0003f000) >> 7) |
+            ((rgb & 0x000000f8) >> 3);
+        }
+
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+#endif
 
 static void
-gdk_rgb_convert_555 (GdkImage *image,
+gdk_rgb_convert_555 (GdkRgbInfo *image_info, GdkImage *image,
                     gint x0, gint y0, gint width, gint height,
                     guchar *buf, int rowstride,
                     gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1427,7 +1878,7 @@ gdk_rgb_convert_555 (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_555_br (GdkImage *image,
+gdk_rgb_convert_555_br (GdkRgbInfo *image_info, GdkImage *image,
                        gint x0, gint y0, gint width, gint height,
                        guchar *buf, int rowstride,
                        gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1463,7 +1914,7 @@ gdk_rgb_convert_555_br (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_888_msb (GdkImage *image,
+gdk_rgb_convert_888_msb (GdkRgbInfo *image_info, GdkImage *image,
                         gint x0, gint y0, gint width, gint height,
                         guchar *buf, int rowstride,
                         gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1485,13 +1936,13 @@ gdk_rgb_convert_888_msb (GdkImage *image,
 }
 
 /* todo: optimize this */
-#ifndef WORDS_BIGENDIAN
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
 #define HAIRY_CONVERT_888
 #endif
 
 #ifdef HAIRY_CONVERT_888
 static void
-gdk_rgb_convert_888_lsb (GdkImage *image,
+gdk_rgb_convert_888_lsb (GdkRgbInfo *image_info, GdkImage *image,
                         gint x0, gint y0, gint width, gint height,
                         guchar *buf, int rowstride,
                         gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1509,7 +1960,7 @@ gdk_rgb_convert_888_lsb (GdkImage *image,
     {
       bp2 = bptr;
       obptr = obuf;
-      if (width < 8 || (((unsigned long)obuf | (unsigned long) bp2) & 3))
+      if (((unsigned long)obuf | (unsigned long) bp2) & 3)
        {
          for (x = 0; x < width; x++)
            {
@@ -1565,7 +2016,7 @@ gdk_rgb_convert_888_lsb (GdkImage *image,
 }
 #else
 static void
-gdk_rgb_convert_888_lsb (GdkImage *image,
+gdk_rgb_convert_888_lsb (GdkRgbInfo *image_info, GdkImage *image,
                         gint x0, gint y0, gint width, gint height,
                         guchar *buf, int rowstride,
                         gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1601,7 +2052,7 @@ gdk_rgb_convert_888_lsb (GdkImage *image,
 /* convert 24-bit packed to 32-bit unpacked */
 /* todo: optimize this */
 static void
-gdk_rgb_convert_0888 (GdkImage *image,
+gdk_rgb_convert_0888 (GdkRgbInfo *image_info, GdkImage *image,
                      gint x0, gint y0, gint width, gint height,
                      guchar *buf, int rowstride,
                      gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1623,7 +2074,7 @@ gdk_rgb_convert_0888 (GdkImage *image,
          r = bp2[0];
          g = bp2[1];
          b = bp2[2];
-         ((unsigned long *)obuf)[x] = (r << 16) | (g << 8) | b;
+         ((guint32 *)obuf)[x] = (r << 16) | (g << 8) | b;
          bp2 += 3;
        }
       bptr += rowstride;
@@ -1632,7 +2083,7 @@ gdk_rgb_convert_0888 (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_0888_br (GdkImage *image,
+gdk_rgb_convert_0888_br (GdkRgbInfo *image_info, GdkImage *image,
                         gint x0, gint y0, gint width, gint height,
                         guchar *buf, int rowstride,
                         gint x_align, gint y_align, GdkRgbCmap *cmap)
@@ -1654,7 +2105,7 @@ gdk_rgb_convert_0888_br (GdkImage *image,
          r = bp2[0];
          g = bp2[1];
          b = bp2[2];
-         ((unsigned long *)obuf)[x] = (b << 24) | (g << 16) | (r << 8);
+         ((guint32 *)obuf)[x] = (b << 24) | (g << 16) | (r << 8);
          bp2 += 3;
        }
       bptr += rowstride;
@@ -1662,11 +2113,42 @@ gdk_rgb_convert_0888_br (GdkImage *image,
     }
 }
 
-/* Generic truecolor/directcolor conversion function. Slow, but these
-   are oddball modes. */
 static void
-gdk_rgb_convert_truecolor_lsb (GdkImage *image,
-                              gint x0, gint y0, gint width, gint height,
+gdk_rgb_convert_8880_br (GdkRgbInfo *image_info, GdkImage *image,
+                        gint x0, gint y0, gint width, gint height,
+                        guchar *buf, int rowstride,
+                        gint x_align, gint y_align, GdkRgbCmap *cmap)
+{
+  int x, y;
+  guchar *obuf;
+  gint bpl;
+  guchar *bptr, *bp2;
+  int r, g, b;
+
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + x0 * 4;
+  for (y = 0; y < height; y++)
+    {
+      bp2 = bptr;
+      for (x = 0; x < width; x++)
+       {
+         r = bp2[0];
+         g = bp2[1];
+         b = bp2[2];
+         ((guint32 *)obuf)[x] = (b << 16) | (g << 8) | r;
+         bp2 += 3;
+       }
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+
+/* Generic truecolor/directcolor conversion function. Slow, but these
+   are oddball modes. */
+static void
+gdk_rgb_convert_truecolor_lsb (GdkRgbInfo *image_info, GdkImage *image,
+                              gint x0, gint y0, gint width, gint height,
                               guchar *buf, int rowstride,
                               gint x_align, gint y_align,
                               GdkRgbCmap *cmap)
@@ -1718,7 +2200,7 @@ gdk_rgb_convert_truecolor_lsb (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_truecolor_lsb_d (GdkImage *image,
+gdk_rgb_convert_truecolor_lsb_d (GdkRgbInfo *image_info, GdkImage *image,
                                 gint x0, gint y0, gint width, gint height,
                                 guchar *buf, int rowstride,
                                 gint x_align, gint y_align,
@@ -1737,7 +2219,7 @@ gdk_rgb_convert_truecolor_lsb_d (GdkImage *image,
   gint i;
   gint dith;
   gint r1, g1, b1;
-  guchar *dmp;
+  const guchar *dmp;
 
   r_right = 8 - image_info->visual->red_prec;
   r_left = image_info->visual->red_shift;
@@ -1782,7 +2264,7 @@ gdk_rgb_convert_truecolor_lsb_d (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_truecolor_msb (GdkImage *image,
+gdk_rgb_convert_truecolor_msb (GdkRgbInfo *image_info, GdkImage *image,
                               gint x0, gint y0, gint width, gint height,
                               guchar *buf, int rowstride,
                               gint x_align, gint y_align,
@@ -1835,7 +2317,7 @@ gdk_rgb_convert_truecolor_msb (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_truecolor_msb_d (GdkImage *image,
+gdk_rgb_convert_truecolor_msb_d (GdkRgbInfo *image_info, GdkImage *image,
                                 gint x0, gint y0, gint width, gint height,
                                 guchar *buf, int rowstride,
                                 gint x_align, gint y_align,
@@ -1854,7 +2336,7 @@ gdk_rgb_convert_truecolor_msb_d (GdkImage *image,
   gint shift, shift_init;
   gint dith;
   gint r1, g1, b1;
-  guchar *dmp;
+  const guchar *dmp;
 
   r_right = 8 - image_info->visual->red_prec;
   r_left = image_info->visual->red_shift;
@@ -1898,25 +2380,296 @@ gdk_rgb_convert_truecolor_msb_d (GdkImage *image,
     }
 }
 
+/* This actually works for depths from 3 to 7 */
+static void
+gdk_rgb_convert_4 (GdkRgbInfo *image_info, GdkImage *image,
+                  gint x0, gint y0, gint width, gint height,
+                  guchar *buf, int rowstride,
+                  gint x_align, gint y_align,
+                  GdkRgbCmap *cmap)
+{
+  int x, y;
+  gint bpl;
+  guchar *obuf, *obptr;
+  guchar *bptr, *bp2;
+  gint r, g, b;
+  const guchar *dmp;
+  gint dith;
+  guchar *colorcube_d = image_info->colorcube_d;
+  
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + x0;
+  for (y = 0; y < height; y++)
+    {
+      dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
+      bp2 = bptr;
+      obptr = obuf;
+      for (x = 0; x < width; x += 1)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 3;
+         obptr[0] = colorcube_d[(((r + dith) & 0x100) >> 2) |
+                               (((g + 258 - dith) & 0x100) >> 5) |
+                               (((b + dith) & 0x100) >> 8)];
+         obptr++;
+       }
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+
+/* This actually works for depths from 3 to 7 */
+static void
+gdk_rgb_convert_gray4 (GdkRgbInfo *image_info, GdkImage *image,
+                      gint x0, gint y0, gint width, gint height,
+                      guchar *buf, int rowstride,
+                      gint x_align, gint y_align, GdkRgbCmap *cmap)
+{
+  int x, y;
+  gint bpl;
+  guchar *obuf, *obptr;
+  guchar *bptr, *bp2;
+  gint r, g, b;
+  gint shift;
+
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + x0;
+  shift = 9 - image_info->visual->depth;
+  for (y = 0; y < height; y++)
+    {
+      bp2 = bptr;
+      obptr = obuf;
+      for (x = 0; x < width; x++)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         obptr[0] = (g + ((b + r) >> 1)) >> shift;
+         obptr++;
+       }
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+
+static void
+gdk_rgb_convert_gray4_pack (GdkRgbInfo *image_info, GdkImage *image,
+                           gint x0, gint y0, gint width, gint height,
+                           guchar *buf, int rowstride,
+                           gint x_align, gint y_align, GdkRgbCmap *cmap)
+{
+  int x, y;
+  gint bpl;
+  guchar *obuf, *obptr;
+  guchar *bptr, *bp2;
+  gint r, g, b;
+  gint shift;
+  guchar pix0, pix1;
+  /* todo: this is hardcoded to big-endian. Make endian-agile. */
+
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + (x0 >> 1);
+  shift = 9 - image_info->visual->depth;
+  for (y = 0; y < height; y++)
+    {
+      bp2 = bptr;
+      obptr = obuf;
+      for (x = 0; x < width; x += 2)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         pix0 = (g + ((b + r) >> 1)) >> shift;
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         pix1 = (g + ((b + r) >> 1)) >> shift;
+         obptr[0] = (pix0 << 4) | pix1;
+         obptr++;
+       }
+      if (width & 1)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         pix0 = (g + ((b + r) >> 1)) >> shift;
+         obptr[0] = (pix0 << 4);
+       }
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+
+/* This actually works for depths from 3 to 7 */
+static void
+gdk_rgb_convert_gray4_d (GdkRgbInfo *image_info, GdkImage *image,
+                        gint x0, gint y0, gint width, gint height,
+                        guchar *buf, int rowstride,
+                        gint x_align, gint y_align, GdkRgbCmap *cmap)
+{
+  int x, y;
+  gint bpl;
+  guchar *obuf, *obptr;
+  guchar *bptr, *bp2;
+  gint r, g, b;
+  const guchar *dmp;
+  gint prec, right;
+  gint gray;
+
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + x0;
+  prec = image_info->visual->depth;
+  right = 8 - prec;
+  for (y = 0; y < height; y++)
+    {
+      bp2 = bptr;
+      obptr = obuf;
+      dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
+      for (x = 0; x < width; x++)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         gray = (g + ((b + r) >> 1)) >> 1;
+         gray += (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) >> prec;
+         obptr[0] = (gray - (gray >> prec)) >> right;
+         obptr++;
+       }
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+
+static void
+gdk_rgb_convert_gray4_d_pack (GdkRgbInfo *image_info, GdkImage *image,
+                             gint x0, gint y0, gint width, gint height,
+                             guchar *buf, int rowstride,
+                             gint x_align, gint y_align, GdkRgbCmap *cmap)
+{
+  int x, y;
+  gint bpl;
+  guchar *obuf, *obptr;
+  guchar *bptr, *bp2;
+  gint r, g, b;
+  const guchar *dmp;
+  gint prec, right;
+  gint gray;
+  guchar pix0, pix1;
+  /* todo: this is hardcoded to big-endian. Make endian-agile. */
+
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + (x0 >> 1);
+  prec = image_info->visual->depth;
+  right = 8 - prec;
+  for (y = 0; y < height; y++)
+    {
+      bp2 = bptr;
+      obptr = obuf;
+      dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
+      for (x = 0; x < width; x += 2)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         gray = (g + ((b + r) >> 1)) >> 1;
+         gray += (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) >> prec;
+         pix0 = (gray - (gray >> prec)) >> right;
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         gray = (g + ((b + r) >> 1)) >> 1;
+         gray += (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) >> prec;
+         pix1 = (gray - (gray >> prec)) >> right;
+         obptr[0] = (pix0 << 4) | pix1;
+         obptr++;
+       }
+      if (width & 1)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         gray = (g + ((b + r) >> 1)) >> 1;
+         gray += (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) >> prec;
+         pix0 = (gray - (gray >> prec)) >> right;
+         obptr[0] = (pix0 << 4);
+       }
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+
+static void
+gdk_rgb_convert_1 (GdkRgbInfo *image_info, GdkImage *image,
+                  gint x0, gint y0, gint width, gint height,
+                  guchar *buf, int rowstride,
+                  gint x_align, gint y_align,
+                  GdkRgbCmap *cmap)
+{
+  int x, y;
+  gint bpl;
+  guchar *obuf, *obptr;
+  guchar *bptr, *bp2;
+  gint r, g, b;
+  const guchar *dmp;
+  gint dith;
+  guchar byte;
+
+  bptr = buf;
+  bpl = image->bpl;
+  obuf = ((guchar *)image->mem) + y0 * bpl + (x0 >> 3);
+  byte = 0; /* unnecessary, but it keeps gcc from complaining */
+  for (y = 0; y < height; y++)
+    {
+      dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
+      bp2 = bptr;
+      obptr = obuf;
+      for (x = 0; x < width; x++)
+       {
+         r = *bp2++;
+         g = *bp2++;
+         b = *bp2++;
+         dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 4) | 4;
+         byte += byte + (r + g + g + b + dith > 1020);
+         if ((x & 7) == 7)
+           {
+             obptr[0] = byte;
+             obptr++;
+           }
+       }
+      if (x & 7)
+       obptr[0] = byte << (8 - (x & 7));
+      bptr += rowstride;
+      obuf += bpl;
+    }
+}
+
 /* Returns a pointer to the stage buffer. */
 static guchar *
-gdk_rgb_ensure_stage (void)
+gdk_rgb_ensure_stage (GdkRgbInfo *image_info)
 {
   if (image_info->stage_buf == NULL)
-    image_info->stage_buf = g_malloc (IMAGE_HEIGHT * STAGE_ROWSTRIDE);
+    image_info->stage_buf = g_malloc (REGION_HEIGHT * STAGE_ROWSTRIDE);
   return image_info->stage_buf;
 }
 
 /* This is slow. Speed me up, please. */
 static void
-gdk_rgb_32_to_stage (guchar *buf, gint rowstride, gint width, gint height)
+gdk_rgb_32_to_stage (GdkRgbInfo *image_info,
+                    guchar *buf, gint rowstride, gint width, gint height)
 {
   gint x, y;
   guchar *pi_start, *po_start;
   guchar *pi, *po;
 
   pi_start = buf;
-  po_start = gdk_rgb_ensure_stage ();
+  po_start = gdk_rgb_ensure_stage (image_info);
   for (y = 0; y < height; y++)
     {
       pi = pi_start;
@@ -1936,14 +2689,14 @@ gdk_rgb_32_to_stage (guchar *buf, gint rowstride, gint width, gint height)
 /* Generic 32bit RGB conversion function - convert to 24bit packed, then
    go from there. */
 static void
-gdk_rgb_convert_32_generic (GdkImage *image,
+gdk_rgb_convert_32_generic (GdkRgbInfo *image_info, GdkImage *image,
                            gint x0, gint y0, gint width, gint height,
                            guchar *buf, gint rowstride,
                            gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
-  gdk_rgb_32_to_stage (buf, rowstride, width, height);
+  gdk_rgb_32_to_stage (image_info, buf, rowstride, width, height);
 
-  (*image_info->conv) (image, x0, y0, width, height,
+  (*image_info->conv) (image_info, image, x0, y0, width, height,
                       image_info->stage_buf, STAGE_ROWSTRIDE,
                       x_align, y_align, cmap);
 }
@@ -1951,21 +2704,22 @@ gdk_rgb_convert_32_generic (GdkImage *image,
 /* Generic 32bit RGB conversion function - convert to 24bit packed, then
    go from there. */
 static void
-gdk_rgb_convert_32_generic_d (GdkImage *image,
+gdk_rgb_convert_32_generic_d (GdkRgbInfo *image_info, GdkImage *image,
                              gint x0, gint y0, gint width, gint height,
                              guchar *buf, gint rowstride,
                              gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
-  gdk_rgb_32_to_stage (buf, rowstride, width, height);
+  gdk_rgb_32_to_stage (image_info, buf, rowstride, width, height);
 
-  (*image_info->conv_d) (image, x0, y0, width, height,
+  (*image_info->conv_d) (image_info, image, x0, y0, width, height,
                         image_info->stage_buf, STAGE_ROWSTRIDE,
                         x_align, y_align, cmap);
 }
 
 /* This is slow. Speed me up, please. */
 static void
-gdk_rgb_gray_to_stage (guchar *buf, gint rowstride, gint width, gint height)
+gdk_rgb_gray_to_stage (GdkRgbInfo *image_info,
+                      guchar *buf, gint rowstride, gint width, gint height)
 {
   gint x, y;
   guchar *pi_start, *po_start;
@@ -1973,7 +2727,7 @@ gdk_rgb_gray_to_stage (guchar *buf, gint rowstride, gint width, gint height)
   guchar gray;
 
   pi_start = buf;
-  po_start = gdk_rgb_ensure_stage ();
+  po_start = gdk_rgb_ensure_stage (image_info);
   for (y = 0; y < height; y++)
     {
       pi = pi_start;
@@ -1993,51 +2747,51 @@ gdk_rgb_gray_to_stage (guchar *buf, gint rowstride, gint width, gint height)
 /* Generic gray conversion function - convert to 24bit packed, then go
    from there. */
 static void
-gdk_rgb_convert_gray_generic (GdkImage *image,
+gdk_rgb_convert_gray_generic (GdkRgbInfo *image_info, GdkImage *image,
                              gint x0, gint y0, gint width, gint height,
                              guchar *buf, gint rowstride,
                              gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
-  gdk_rgb_gray_to_stage (buf, rowstride, width, height);
+  gdk_rgb_gray_to_stage (image_info, buf, rowstride, width, height);
 
-  (*image_info->conv) (image, x0, y0, width, height,
+  (*image_info->conv) (image_info, image, x0, y0, width, height,
                       image_info->stage_buf, STAGE_ROWSTRIDE,
                       x_align, y_align, cmap);
 }
 
 static void
-gdk_rgb_convert_gray_generic_d (GdkImage *image,
+gdk_rgb_convert_gray_generic_d (GdkRgbInfo *image_info, GdkImage *image,
                                gint x0, gint y0, gint width, gint height,
                                guchar *buf, gint rowstride,
                                gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
-  gdk_rgb_gray_to_stage (buf, rowstride, width, height);
+  gdk_rgb_gray_to_stage (image_info, buf, rowstride, width, height);
 
-  (*image_info->conv_d) (image, x0, y0, width, height,
+  (*image_info->conv_d) (image_info, image, x0, y0, width, height,
                         image_info->stage_buf, STAGE_ROWSTRIDE,
                         x_align, y_align, cmap);
 }
 
 /* Render grayscale using indexed method. */
 static void
-gdk_rgb_convert_gray_cmap (GdkImage *image,
+gdk_rgb_convert_gray_cmap (GdkRgbInfo *image_info, GdkImage *image,
                           gint x0, gint y0, gint width, gint height,
                           guchar *buf, gint rowstride,
                           gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
-  (*image_info->conv_indexed) (image, x0, y0, width, height,
+  (*image_info->conv_indexed) (image_info, image, x0, y0, width, height,
                               buf, rowstride,
                               x_align, y_align, image_info->gray_cmap);
 }
 
 #if 0
 static void
-gdk_rgb_convert_gray_cmap_d (GdkImage *image,
-                               gint x0, gint y0, gint width, gint height,
-                               guchar *buf, gint rowstride,
-                               gint x_align, gint y_align, GdkRgbCmap *cmap)
+gdk_rgb_convert_gray_cmap_d (GdkRgbInfo *image_info, GdkImage *image,
+                            gint x0, gint y0, gint width, gint height,
+                            guchar *buf, gint rowstride,
+                            gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
-  (*image_info->conv_indexed_d) (image, x0, y0, width, height,
+  (*image_info->conv_indexed_d) (image_info, image, x0, y0, width, height,
                                 buf, rowstride,
                                 x_align, y_align, image_info->gray_cmap);
 }
@@ -2045,7 +2799,8 @@ gdk_rgb_convert_gray_cmap_d (GdkImage *image,
 
 /* This is slow. Speed me up, please. */
 static void
-gdk_rgb_indexed_to_stage (guchar *buf, gint rowstride, gint width, gint height,
+gdk_rgb_indexed_to_stage (GdkRgbInfo *image_info,
+                         guchar *buf, gint rowstride, gint width, gint height,
                          GdkRgbCmap *cmap)
 {
   gint x, y;
@@ -2054,7 +2809,7 @@ gdk_rgb_indexed_to_stage (guchar *buf, gint rowstride, gint width, gint height,
   gint rgb;
 
   pi_start = buf;
-  po_start = gdk_rgb_ensure_stage ();
+  po_start = gdk_rgb_ensure_stage (image_info);
   for (y = 0; y < height; y++)
     {
       pi = pi_start;
@@ -2074,28 +2829,28 @@ gdk_rgb_indexed_to_stage (guchar *buf, gint rowstride, gint width, gint height,
 /* Generic gray conversion function - convert to 24bit packed, then go
    from there. */
 static void
-gdk_rgb_convert_indexed_generic (GdkImage *image,
+gdk_rgb_convert_indexed_generic (GdkRgbInfo *image_info, GdkImage *image,
                                 gint x0, gint y0, gint width, gint height,
                                 guchar *buf, gint rowstride,
                                 gint x_align, gint y_align, GdkRgbCmap *cmap)
 {
-  gdk_rgb_indexed_to_stage (buf, rowstride, width, height, cmap);
+  gdk_rgb_indexed_to_stage (image_info, buf, rowstride, width, height, cmap);
 
-  (*image_info->conv) (image, x0, y0, width, height,
+  (*image_info->conv) (image_info, image, x0, y0, width, height,
                       image_info->stage_buf, STAGE_ROWSTRIDE,
                       x_align, y_align, cmap);
 }
 
 static void
-gdk_rgb_convert_indexed_generic_d (GdkImage *image,
+gdk_rgb_convert_indexed_generic_d (GdkRgbInfo *image_info, GdkImage *image,
                                   gint x0, gint y0, gint width, gint height,
                                   guchar *buf, gint rowstride,
                                   gint x_align, gint y_align,
                                   GdkRgbCmap *cmap)
 {
-  gdk_rgb_indexed_to_stage (buf, rowstride, width, height, cmap);
+  gdk_rgb_indexed_to_stage (image_info, buf, rowstride, width, height, cmap);
 
-  (*image_info->conv_d) (image, x0, y0, width, height,
+  (*image_info->conv_d) (image_info, image, x0, y0, width, height,
                         image_info->stage_buf, STAGE_ROWSTRIDE,
                         x_align, y_align, cmap);
 }
@@ -2103,7 +2858,7 @@ gdk_rgb_convert_indexed_generic_d (GdkImage *image,
 /* Select a conversion function based on the visual and a
    representative image. */
 static void
-gdk_rgb_select_conv (GdkImage *image)
+gdk_rgb_select_conv (GdkRgbInfo *image_info, GdkImage *image)
 {
   GdkByteOrder byte_order;
   gint depth, bpp, byterev;
@@ -2113,13 +2868,25 @@ gdk_rgb_select_conv (GdkImage *image)
   GdkRgbConvFunc conv_32, conv_32_d;
   GdkRgbConvFunc conv_gray, conv_gray_d;
   GdkRgbConvFunc conv_indexed, conv_indexed_d;
+  gboolean mask_rgb, mask_bgr;
 
   depth = image_info->visual->depth;
-  bpp = image->bpp;
+
+  /* FIXME: save the bpp in the image; this is hack that works for
+   *        common visuals, not otherwise.
+   */
+  if (depth <= 8)
+    bpp = depth;
+  else
+    bpp = 8 * image->bpp;
 
   byte_order = image->byte_order;
+  if (gdk_rgb_verbose)
+    g_print ("Chose visual type=%s depth=%d, image bpp=%d, %s first\n",
+            visual_names[image_info->visual->type], image_info->visual->depth,
+            bpp, byte_order == GDK_LSB_FIRST ? "lsb" : "msb");
 
-#ifdef WORDS_BIGENDIAN
+#if G_BYTE_ORDER == G_BIG_ENDIAN
   byterev = (byte_order == GDK_LSB_FIRST);
 #else
   byterev = (byte_order == GDK_MSB_FIRST);
@@ -2133,6 +2900,9 @@ gdk_rgb_select_conv (GdkImage *image)
   green_mask = image_info->visual->green_mask;
   blue_mask = image_info->visual->blue_mask;
 
+  mask_rgb = red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff;
+  mask_bgr = red_mask == 0xff && green_mask == 0xff00 && blue_mask == 0xff0000;
+
   conv = NULL;
   conv_d = NULL;
 
@@ -2147,45 +2917,61 @@ gdk_rgb_select_conv (GdkImage *image)
 
   image_info->dith_default = FALSE;
 
-  if (bpp == 2 && depth == 16 && !byterev &&
+  if (image_info->bitmap)
+    conv = gdk_rgb_convert_1;
+  else if (bpp == 16 && depth == 16 && !byterev &&
       red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f)
     {
       conv = gdk_rgb_convert_565;
       conv_d = gdk_rgb_convert_565_d;
       conv_gray = gdk_rgb_convert_565_gray;
+      gdk_rgb_preprocess_dm_565 ();
     }
-  else if (bpp == 2 && depth == 16 &&
+  else if (bpp == 16 && depth == 16 &&
           vtype == GDK_VISUAL_TRUE_COLOR && byterev &&
       red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f)
     conv = gdk_rgb_convert_565_br;
 
-  else if (bpp == 2 && depth == 15 &&
+  else if (bpp == 16 && depth == 15 &&
           vtype == GDK_VISUAL_TRUE_COLOR && !byterev &&
       red_mask == 0x7c00 && green_mask == 0x3e0 && blue_mask == 0x1f)
     conv = gdk_rgb_convert_555;
 
-  else if (bpp == 2 && depth == 15 &&
+  else if (bpp == 16 && depth == 15 &&
           vtype == GDK_VISUAL_TRUE_COLOR && byterev &&
       red_mask == 0x7c00 && green_mask == 0x3e0 && blue_mask == 0x1f)
     conv = gdk_rgb_convert_555_br;
 
-  /* I'm not 100% sure about the 24bpp tests */
-  else if (bpp == 3 && depth == 24 &&
-          vtype == GDK_VISUAL_TRUE_COLOR && byte_order == GDK_LSB_FIRST &&
-          red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff)
+  /* I'm not 100% sure about the 24bpp tests - but testing will show*/
+  else if (bpp == 24 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          ((mask_rgb && byte_order == GDK_LSB_FIRST) ||
+           (mask_bgr && byte_order == GDK_MSB_FIRST)))
     conv = gdk_rgb_convert_888_lsb;
-  else if (bpp == 3 && depth == 24 &&
-          vtype == GDK_VISUAL_TRUE_COLOR && byte_order == GDK_MSB_FIRST &&
-          red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff)
+  else if (bpp == 24 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          ((mask_rgb && byte_order == GDK_MSB_FIRST) ||
+           (mask_bgr && byte_order == GDK_LSB_FIRST)))
     conv = gdk_rgb_convert_888_msb;
-  else if (bpp == 4 && depth == 24 &&
-          vtype == GDK_VISUAL_TRUE_COLOR && !byterev &&
-          red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff)
+#if G_BYTE_ORDER == G_BIG_ENDIAN
+  else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          (mask_rgb && byte_order == GDK_LSB_FIRST))
+    conv = gdk_rgb_convert_0888_br;
+  else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          (mask_rgb && byte_order == GDK_MSB_FIRST))
     conv = gdk_rgb_convert_0888;
-  else if (bpp == 4 && depth == 24 &&
-          vtype == GDK_VISUAL_TRUE_COLOR && byterev &&
-          red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff)
+  else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          (mask_bgr && byte_order == GDK_MSB_FIRST))
+    conv = gdk_rgb_convert_8880_br;
+#else
+  else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          (mask_rgb && byte_order == GDK_MSB_FIRST))
     conv = gdk_rgb_convert_0888_br;
+  else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          (mask_rgb && byte_order == GDK_LSB_FIRST))
+    conv = gdk_rgb_convert_0888;
+  else if (bpp == 32 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR &&
+          (mask_bgr && byte_order == GDK_LSB_FIRST))
+    conv = gdk_rgb_convert_8880_br;
+#endif
 
   else if (vtype == GDK_VISUAL_TRUE_COLOR && byte_order == GDK_LSB_FIRST)
     {
@@ -2195,9 +2981,9 @@ gdk_rgb_select_conv (GdkImage *image)
   else if (vtype == GDK_VISUAL_TRUE_COLOR && byte_order == GDK_MSB_FIRST)
     {
       conv = gdk_rgb_convert_truecolor_msb;
-      conv = gdk_rgb_convert_truecolor_msb_d;
+      conv_d = gdk_rgb_convert_truecolor_msb_d;
     }
-  else if (bpp == 1 && depth == 8 && (vtype == GDK_VISUAL_PSEUDO_COLOR
+  else if (bpp == 8 && depth == 8 && (vtype == GDK_VISUAL_PSEUDO_COLOR
 #ifdef ENABLE_GRAYSCALE
                                      || vtype == GDK_VISUAL_GRAYSCALE
 #endif
@@ -2217,7 +3003,7 @@ gdk_rgb_select_conv (GdkImage *image)
       conv_indexed = gdk_rgb_convert_8_indexed;
       conv_gray = gdk_rgb_convert_gray_cmap;
     }
-  else if (bpp == 1 && depth == 8 && (vtype == GDK_VISUAL_STATIC_GRAY
+  else if (bpp == 8 && depth == 8 && (vtype == GDK_VISUAL_STATIC_GRAY
 #ifdef not_ENABLE_GRAYSCALE
                                      || vtype == GDK_VISUAL_GRAYSCALE
 #endif
@@ -2226,6 +3012,24 @@ gdk_rgb_select_conv (GdkImage *image)
       conv = gdk_rgb_convert_gray8;
       conv_gray = gdk_rgb_convert_gray8_gray;
     }
+  else if (bpp == 8 && depth < 8 && depth >= 2 &&
+          (vtype == GDK_VISUAL_STATIC_GRAY
+           || vtype == GDK_VISUAL_GRAYSCALE))
+    {
+      conv = gdk_rgb_convert_gray4;
+      conv_d = gdk_rgb_convert_gray4_d;
+    }
+  else if (bpp == 8 && depth < 8 && depth >= 3)
+    {
+      conv = gdk_rgb_convert_4;
+    }
+  else if (bpp == 4 && depth <= 4 && depth >= 2 &&
+          (vtype == GDK_VISUAL_STATIC_GRAY
+           || vtype == GDK_VISUAL_GRAYSCALE))
+    {
+      conv = gdk_rgb_convert_gray4_pack;
+      conv_d = gdk_rgb_convert_gray4_d_pack;
+    }
 
   if (conv_d == NULL)
     conv_d = conv;
@@ -2243,15 +3047,6 @@ gdk_rgb_select_conv (GdkImage *image)
   image_info->conv_indexed_d = conv_indexed_d;
 }
 
-static gint horiz_idx;
-static gint horiz_y = IMAGE_HEIGHT;
-static gint vert_idx;
-static gint vert_x = IMAGE_WIDTH;
-static gint tile_idx;
-static gint tile_x = IMAGE_WIDTH;
-static gint tile_y1 = IMAGE_HEIGHT;
-static gint tile_y2 = IMAGE_HEIGHT;
-
 #ifdef VERBOSE
 static gint sincelast;
 #endif
@@ -2262,9 +3057,9 @@ static gint sincelast;
 #undef NO_FLUSH
 
 static gint
-gdk_rgb_alloc_scratch_image (void)
+gdk_rgb_alloc_scratch_image (GdkRgbInfo *image_info)
 {
-  if (static_image_idx == N_IMAGES)
+  if (image_info->static_image_idx == N_REGIONS)
     {
 #ifndef NO_FLUSH
       gdk_flush ();
@@ -2273,80 +3068,88 @@ gdk_rgb_alloc_scratch_image (void)
       g_print ("flush, %d puts since last flush\n", sincelast);
       sincelast = 0;
 #endif
-      static_image_idx = 0;
-      horiz_y = IMAGE_HEIGHT;
-      vert_x = IMAGE_WIDTH;
-      tile_x = IMAGE_WIDTH;
-      tile_y1 = tile_y2 = IMAGE_HEIGHT;
+      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 = REGION_HEIGHT;
+      image_info->vert_x = REGION_WIDTH;
+      image_info->tile_x = REGION_WIDTH;
+      image_info->tile_y1 = image_info->tile_y2 = REGION_HEIGHT;
     }
-  return static_image_idx++;
+  return image_info->static_image_idx++;
 }
 
 static GdkImage *
-gdk_rgb_alloc_scratch (gint width, gint height, gint *x0, gint *y0)
+gdk_rgb_alloc_scratch (GdkRgbInfo *image_info,
+                      gint width, gint height, gint *x0, gint *y0)
 {
   GdkImage *image;
   gint idx;
 
-
-  if (width >= (IMAGE_WIDTH >> 1))
+  if (width >= (REGION_WIDTH >> 1))
     {
-      if (height >= (IMAGE_HEIGHT >> 1))
+      if (height >= (REGION_HEIGHT >> 1))
        {
-         idx = gdk_rgb_alloc_scratch_image ();
+         idx = gdk_rgb_alloc_scratch_image (image_info);
          *x0 = 0;
          *y0 = 0;
        }
       else
        {
-         if (height + horiz_y > IMAGE_HEIGHT)
+         if (height + image_info->horiz_y > REGION_HEIGHT)
            {
-             horiz_idx = gdk_rgb_alloc_scratch_image ();
-             horiz_y = 0;
+             image_info->horiz_idx = gdk_rgb_alloc_scratch_image (image_info);
+             image_info->horiz_y = 0;
            }
-         idx = horiz_idx;
+         idx = image_info->horiz_idx;
          *x0 = 0;
-         *y0 = horiz_y;
-         horiz_y += height;
+         *y0 = image_info->horiz_y;
+         image_info->horiz_y += height;
        }
     }
   else
     {
-      if (height >= (IMAGE_HEIGHT >> 1))
+      if (height >= (REGION_HEIGHT >> 1))
        {
-         if (width + vert_x > IMAGE_WIDTH)
+         if (width + image_info->vert_x > REGION_WIDTH)
            {
-             vert_idx = gdk_rgb_alloc_scratch_image ();
-             vert_x = 0;
+             image_info->vert_idx = gdk_rgb_alloc_scratch_image (image_info);
+             image_info->vert_x = 0;
            }
-         idx = vert_idx;
-         *x0 = vert_x;
+         idx = image_info->vert_idx;
+         *x0 = image_info->vert_x;
          *y0 = 0;
-         vert_x += (width + 3) & -4;
+         /* 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 + tile_x > IMAGE_WIDTH)
+         if (width + image_info->tile_x > REGION_WIDTH)
            {
-             tile_y1 = tile_y2;
-             tile_x = 0;
+             image_info->tile_y1 = image_info->tile_y2;
+             image_info->tile_x = 0;
            }
-         if (height + tile_y1 > IMAGE_HEIGHT)
+         if (height + image_info->tile_y1 > REGION_HEIGHT)
            {
-             tile_idx = gdk_rgb_alloc_scratch_image ();
-             tile_x = 0;
-             tile_y1 = 0;
-             tile_y2 = 0;
+             image_info->tile_idx = gdk_rgb_alloc_scratch_image (image_info);
+             image_info->tile_x = 0;
+             image_info->tile_y1 = 0;
+             image_info->tile_y2 = 0;
            }
-         if (height + tile_y1 > tile_y2)
-           tile_y2 = height + tile_y1;
-         idx = tile_idx;
-         *x0 = tile_x;
-         *y0 = tile_y1;
-         tile_x += (width + 3) & -4;
+         if (height + image_info->tile_y1 > image_info->tile_y2)
+           image_info->tile_y2 = height + image_info->tile_y1;
+         idx = image_info->tile_idx;
+         *x0 = image_info->tile_x;
+         *y0 = image_info->tile_y1;
+         image_info->tile_x += (width + 7) & -8;
        }
     }
-  image = static_image[idx];
+  image = image_info->static_image[idx * image_info->n_images / N_REGIONS];
+  *x0 += REGION_WIDTH * (idx % (N_REGIONS / image_info->n_images));
 #ifdef VERBOSE
   g_print ("index %d, x %d, y %d (%d x %d)\n", idx, *x0, *y0, width, height);
   sincelast++;
@@ -2355,7 +3158,8 @@ gdk_rgb_alloc_scratch (gint width, gint height, gint *x0, gint *y0)
 }
 
 static void
-gdk_draw_rgb_image_core (GdkDrawable *drawable,
+gdk_draw_rgb_image_core (GdkRgbInfo *image_info,
+                        GdkDrawable *drawable,
                         GdkGC *gc,
                         gint x,
                         gint y,
@@ -2365,7 +3169,9 @@ gdk_draw_rgb_image_core (GdkDrawable *drawable,
                         gint pixstride,
                         gint rowstride,
                         GdkRgbConvFunc conv,
-                        GdkRgbCmap *cmap)
+                        GdkRgbCmap *cmap,
+                        gint xdith,
+                        gint ydith)
 {
   gint y0, x0;
   gint xs0, ys0;
@@ -2373,25 +3179,69 @@ gdk_draw_rgb_image_core (GdkDrawable *drawable,
   gint width1, height1;
   guchar *buf_ptr;
 
-  for (y0 = 0; y0 < height; y0 += IMAGE_HEIGHT)
+  if (image_info->bitmap)
+    {
+      if (image_info->own_gc == NULL)
+       {
+         GdkColor color;
+
+         image_info->own_gc = gdk_gc_new (drawable);
+         gdk_color_white (image_info->cmap, &color);
+         gdk_gc_set_foreground (image_info->own_gc, &color);
+         gdk_color_black (image_info->cmap, &color);
+         gdk_gc_set_background (image_info->own_gc, &color);
+       }
+      gc = image_info->own_gc;
+    }
+  for (y0 = 0; y0 < height; y0 += REGION_HEIGHT)
     {
-      height1 = MIN (height - y0, IMAGE_HEIGHT);
-      for (x0 = 0; x0 < width; x0 += IMAGE_WIDTH)
+      height1 = MIN (height - y0, REGION_HEIGHT);
+      for (x0 = 0; x0 < width; x0 += REGION_WIDTH)
        {
-         width1 = MIN (width - x0, IMAGE_WIDTH);
+         width1 = MIN (width - x0, REGION_WIDTH);
          buf_ptr = buf + y0 * rowstride + x0 * pixstride;
 
-         image = gdk_rgb_alloc_scratch (width1, height1, &xs0, &ys0);
+         image = gdk_rgb_alloc_scratch (image_info, width1, height1, &xs0, &ys0);
 
-         conv (image, xs0, ys0, width1, height1, buf_ptr, rowstride,
-               x + x0, y + y0, cmap);
+         conv (image_info, image, xs0, ys0, width1, height1, buf_ptr, rowstride,
+               x + x0 + xdith, y + y0 + ydith, cmap);
 
+#ifndef DONT_ACTUALLY_DRAW
          gdk_draw_image (drawable, gc,
                          image, xs0, ys0, x + x0, y + y0, width1, height1);
+#endif
        }
     }
 }
 
+static GdkRgbInfo *
+gdk_rgb_get_info_from_drawable (GdkDrawable *drawable)
+{
+  GdkColormap *cmap = gdk_drawable_get_colormap (drawable);
+
+  if (!cmap)
+    {
+      /* This guessing is required to maintain backward compatibility,
+       * but would otherwise be a bad thing
+       */
+
+      gint depth = gdk_drawable_get_depth (drawable);
+      GdkColormap *rgb_cmap = gdk_rgb_get_colormap();
+      if (depth == gdk_colormap_get_visual (rgb_cmap)->depth)
+       cmap = rgb_cmap;
+      else
+       {
+         g_warning ("The gdk_draw_*_image require the drawable argument to\n"
+                    "have a specified colormap. All windows have a colormap,\n"
+                    "however, pixmaps only have colormap by default if they\n"
+                    "were created with a non-NULL window argument. Otherwise\n"
+                    "a colormap must be set on them with gdk_drawable_set_colormap");
+         return NULL;
+       }
+    }
+
+  return gdk_rgb_get_info_from_colormap (cmap);
+}
 
 void
 gdk_draw_rgb_image (GdkDrawable *drawable,
@@ -2404,13 +3254,47 @@ gdk_draw_rgb_image (GdkDrawable *drawable,
                    guchar *rgb_buf,
                    gint rowstride)
 {
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_drawable (drawable);
+  if (!image_info)
+    return;
+  
+  if (dith == GDK_RGB_DITHER_NONE || (dith == GDK_RGB_DITHER_NORMAL &&
+                                     !image_info->dith_default))
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
+                            rgb_buf, 3, rowstride, image_info->conv, NULL,
+                            0, 0);
+  else
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
+                            rgb_buf, 3, rowstride, image_info->conv_d, NULL,
+                            0, 0);
+}
+
+void
+gdk_draw_rgb_image_dithalign (GdkDrawable *drawable,
+                             GdkGC *gc,
+                             gint x,
+                             gint y,
+                             gint width,
+                             gint height,
+                             GdkRgbDither dith,
+                             guchar *rgb_buf,
+                             gint rowstride,
+                             gint xdith,
+                             gint ydith)
+{
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_drawable (drawable);
+  if (!image_info)
+    return;
+  
   if (dith == GDK_RGB_DITHER_NONE || (dith == GDK_RGB_DITHER_NORMAL &&
                                      !image_info->dith_default))
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
-                            rgb_buf, 3, rowstride, image_info->conv, NULL);
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
+                            rgb_buf, 3, rowstride, image_info->conv, NULL,
+                            xdith, ydith);
   else
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
-                            rgb_buf, 3, rowstride, image_info->conv_d, NULL);
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
+                            rgb_buf, 3, rowstride, image_info->conv_d, NULL,
+                            xdith, ydith);
 }
 
 void
@@ -2424,15 +3308,47 @@ gdk_draw_rgb_32_image (GdkDrawable *drawable,
                       guchar *buf,
                       gint rowstride)
 {
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_drawable (drawable);
+  if (!image_info)
+    return;
+  
+  if (dith == GDK_RGB_DITHER_NONE || (dith == GDK_RGB_DITHER_NORMAL &&
+                                     !image_info->dith_default))
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
+                            buf, 4, rowstride,
+                            image_info->conv_32, NULL, 0, 0);
+  else
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
+                            buf, 4, rowstride,
+                            image_info->conv_32_d, NULL, 0, 0);
+}
+
+void
+gdk_draw_rgb_32_image_dithalign (GdkDrawable *drawable,
+                                GdkGC *gc,
+                                gint x,
+                                gint y,
+                                gint width,
+                                gint height,
+                                GdkRgbDither dith,
+                                guchar *buf,
+                                gint rowstride,
+                                gint xdith,
+                                gint ydith)
+{
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_drawable (drawable);
+  if (!image_info)
+    return;
+  
   if (dith == GDK_RGB_DITHER_NONE || (dith == GDK_RGB_DITHER_NORMAL &&
                                      !image_info->dith_default))
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
                             buf, 4, rowstride,
-                            image_info->conv_32, NULL);
+                            image_info->conv_32, NULL, xdith, ydith);
   else
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
                             buf, 4, rowstride,
-                            image_info->conv_32_d, NULL);
+                            image_info->conv_32_d, NULL, xdith, ydith);
 }
 
 static void
@@ -2457,6 +3373,10 @@ gdk_draw_gray_image (GdkDrawable *drawable,
                     guchar *buf,
                     gint rowstride)
 {
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_drawable (drawable);
+  if (!image_info)
+    return;
+  
   if (image_info->bpp == 1 &&
       image_info->gray_cmap == NULL &&
       (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
@@ -2465,46 +3385,95 @@ gdk_draw_gray_image (GdkDrawable *drawable,
   
   if (dith == GDK_RGB_DITHER_NONE || (dith == GDK_RGB_DITHER_NORMAL &&
                                      !image_info->dith_default))
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
                             buf, 1, rowstride,
-                            image_info->conv_gray, NULL);
+                            image_info->conv_gray, NULL, 0, 0);
   else
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
                             buf, 1, rowstride,
-                            image_info->conv_gray_d, NULL);
+                            image_info->conv_gray_d, NULL, 0, 0);
 }
 
-GdkRgbCmap *
-gdk_rgb_cmap_new (guint32 *colors, gint n_colors)
+guchar lut[256]; /* for 8-bit modes */
+
+static GdkRgbCmapInfo *
+gdk_rgb_cmap_get_info (GdkRgbCmap *cmap,
+                      GdkRgbInfo *image_info)
 {
-  GdkRgbCmap *cmap;
+  GSList *tmp_list;
+  GdkRgbCmapInfo *cmap_info;
   int i, j;
   guint32 rgb;
 
+  /* We only need a LUT for pseudo-color and grayscale cmaps */
+  if (image_info->bpp != 1 ||
+      !(image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
+       image_info->visual->type == GDK_VISUAL_GRAYSCALE))
+    return NULL;
+  
+  tmp_list = cmap->info_list;
+
+  while (tmp_list)
+    {
+     cmap_info = tmp_list->data;
+      if (cmap_info->image_info == image_info)
+       return cmap_info;
+    }
+
+  cmap_info = g_new (GdkRgbCmapInfo, 1);
+  cmap_info->image_info = image_info;
+  cmap_info->cmap = cmap;
+  
+  for (i = 0; i < cmap->n_colors; i++)
+    {
+      rgb = cmap->colors[i];
+      j = ((rgb & 0xf00000) >> 12) |
+       ((rgb & 0xf000) >> 8) |
+       ((rgb & 0xf0) >> 4);
+#ifdef VERBOSE
+      g_print ("%d %x %x %d\n", i, j, image_info->colorcube[j]);
+#endif
+      cmap_info->lut[i] = image_info->colorcube[j];
+    }
+
+  cmap->info_list = g_slist_prepend (cmap->info_list, cmap_info);
+  image_info->cmap_info_list = g_slist_prepend (image_info->cmap_info_list, cmap_info);
+  
+  return cmap_info;
+}
+
+GdkRgbCmap *
+gdk_rgb_cmap_new (guint32 *colors, gint n_colors)
+{
+  GdkRgbCmap *cmap;
+    
   g_return_val_if_fail (n_colors >= 0, NULL);
   g_return_val_if_fail (n_colors <= 256, NULL);
+
   cmap = g_new (GdkRgbCmap, 1);
+
+  cmap->n_colors = n_colors;
   memcpy (cmap->colors, colors, n_colors * sizeof(guint32));
-  if (image_info->bpp == 1 &&
-      (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
-       image_info->visual->type == GDK_VISUAL_GRAYSCALE))
-    for (i = 0; i < n_colors; i++)
-      {
-       rgb = colors[i];
-       j = ((rgb & 0xf00000) >> 12) |
-                  ((rgb & 0xf000) >> 8) |
-                  ((rgb & 0xf0) >> 4);
-#ifdef VERBOSE
-       g_print ("%d %x %x %d\n", i, j, colorcube[j]);
-#endif
-       cmap->lut[i] = colorcube[j];
-      }
+
+  cmap->info_list = NULL;
+  
   return cmap;
 }
 
 void
 gdk_rgb_cmap_free (GdkRgbCmap *cmap)
 {
+  GSList *tmp_list;
+
+  tmp_list = cmap->info_list;
+  while (tmp_list)
+    {
+      GdkRgbCmapInfo *cmap_info = tmp_list->data;
+      cmap_info->image_info->cmap_info_list = g_slist_remove (cmap_info->image_info->cmap_info_list, cmap_info);
+      g_free (cmap_info);
+    }
+  g_slist_free (cmap->info_list);
+  
   g_free (cmap);
 }
 
@@ -2520,31 +3489,51 @@ gdk_draw_indexed_image (GdkDrawable *drawable,
                        gint rowstride,
                        GdkRgbCmap *cmap)
 {
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_drawable (drawable);
+  if (!image_info)
+    return;
+  
   if (dith == GDK_RGB_DITHER_NONE || (dith == GDK_RGB_DITHER_NORMAL &&
                                      !image_info->dith_default))
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
                             buf, 1, rowstride,
-                            image_info->conv_indexed, cmap);
+                            image_info->conv_indexed, cmap, 0, 0);
   else
-    gdk_draw_rgb_image_core (drawable, gc, x, y, width, height,
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
                             buf, 1, rowstride,
-                            image_info->conv_indexed_d, cmap);
+                            image_info->conv_indexed_d, cmap, 0, 0);
 }
 
 gboolean
-gdk_rgb_ditherable (void)
+gdk_rgb_colormap_ditherable (GdkColormap *cmap)
 {
+  GdkRgbInfo *image_info = gdk_rgb_get_info_from_colormap (cmap);
+
   return (image_info->conv != image_info->conv_d);
 }
 
+gboolean
+gdk_rgb_ditherable (void)
+{
+  return gdk_rgb_colormap_ditherable (gdk_rgb_get_colormap ());
+}
+
 GdkColormap *
-gdk_rgb_get_cmap (void)
+gdk_rgb_get_colormap (void)
 {
-  return image_info->cmap;
+  static GdkColormap *cmap = NULL;
+  
+  if (!cmap)
+    {
+      GdkRgbInfo *image_info = gdk_rgb_create_info (gdk_rgb_choose_visual (), NULL);
+      cmap = image_info->cmap;
+    }
+
+  return cmap;
 }
 
 GdkVisual *
 gdk_rgb_get_visual (void)
 {
-  return image_info->visual;
+  return gdk_colormap_get_visual (gdk_rgb_get_colormap ());
 }