]> 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 4923d2c5fcedac610e97c3b4d49bab1a1c39b378..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
 
 #else
 
 /* Compiling as a part of Gtk 1.1 or later */
-#include "../config.h"
-#include "gdk.h"
+#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
@@ -65,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;
@@ -102,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;
 
-#define IMAGE_WIDTH 256
-#define STAGE_ROWSTRIDE (IMAGE_WIDTH * 3)
-#define IMAGE_HEIGHT 64
-#define N_IMAGES 6
+  guchar lut[256];             /* For 8-bit modes */
+};
 
-static GdkRgbInfo *image_info = NULL;
-static GdkImage *static_image[N_IMAGES];
-static gint static_image_idx;
+static GdkRgbCmapInfo *gdk_rgb_cmap_get_info (GdkRgbCmap *cmap, GdkRgbInfo *image_info);
 
-static guchar *colorcube;
-static guchar *colorcube_d;
+static const char *gdk_rgb_key = "gdk-rgb-info";
+static GQuark gdk_rgb_quark = 0;
+
+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)
@@ -140,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);
@@ -155,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;
@@ -197,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 ();
@@ -213,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)
@@ -274,8 +357,7 @@ gdk_rgb_try_colormap (gint nr, gint ng, gint nb)
              color.blue = 65535 * pow (color.blue / 65535.0, 0.5);
 #endif
 
-             /* This should be a raw XAllocColor call */
-             if (!gdk_color_alloc (cmap, &color))
+             if (!gdk_colormap_alloc_color (cmap, &color, FALSE, force))
                {
                  char tmp_str[80];
 
@@ -294,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 }, 
@@ -315,37 +397,38 @@ 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 (void)
+gdk_rgb_colorcube_222 (GdkRgbInfo *image_info)
 {
   int i;
   GdkColor color;
-  GdkColormap *cmap;
 
-  if (image_info->cmap_alloced)
-    cmap = image_info->cmap;
-  else
-    cmap = gdk_colormap_get_system ();
-
-  colorcube_d = g_new (guchar, 512);
+  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_color_alloc (cmap, &color);
-      colorcube_d[((i & 4) << 4) | ((i & 2) << 2) | (i & 1)] = color.pixel;
+      gdk_colormap_alloc_color (image_info->cmap, &color, FALSE, TRUE);
+      image_info->colorcube_d[((i & 4) << 4) | ((i & 2) << 2) | (i & 1)] = color.pixel;
     }
 }
 
@@ -386,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;
@@ -447,8 +520,7 @@ gdk_rgb_score_visual (GdkVisual *visual)
   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,
@@ -460,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;
@@ -504,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",
@@ -515,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++)
     {
@@ -526,141 +602,266 @@ 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 big 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]);
 
-  if (image_info == NULL)
+         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->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->visual = NULL;
-      image_info->cmap = NULL;
+  image_info = g_new0 (GdkRgbInfo, 1);
 
-      image_info->color_pixels = NULL;
-      image_info->gray_pixels = NULL;
-      image_info->reserved_pixels = NULL;
+  image_info->visual = visual;
+  image_info->cmap = 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;
+  image_info->own_gc = NULL;
 
-      gdk_rgb_choose_visual ();
+  image_info->cmap = colormap;
 
-      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->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 ()))
        {
-         image_info->cmap = gdk_colormap_get_system ();
-         gdk_rgb_colorcube_222 ();
+         image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
+         image_info->cmap_alloced = TRUE;
        }
-      else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
+      if (!gdk_rgb_do_colormaps (image_info, image_info->cmap != NULL))
        {
-         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;
+         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;
            }
        }
+    }
 
-      image_info->bitmap = (image_info->visual->depth == 1);
+  image_info->bitmap = (image_info->visual->depth == 1);
 
-      for (i = 0; i < N_IMAGES; i++)
-       if (image_info->bitmap)
-         static_image[i] = gdk_image_new_bitmap (image_info->visual,
-                                                 g_malloc (IMAGE_WIDTH * IMAGE_HEIGHT >> 3),
-                                                 IMAGE_WIDTH, IMAGE_HEIGHT);
-       else
-         static_image[i] = gdk_image_new (GDK_IMAGE_FASTEST,
-                                          image_info->visual,
-                                          IMAGE_WIDTH, IMAGE_HEIGHT);
+  /* 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;
+       }
+    }
 
-      image_info->bpp = static_image[0]->bpp;
+  /* 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;
+    }
 
-      gdk_rgb_select_conv (static_image[0]);
+  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)
+{
+  static const gint byte_order[1] = { 1 };
+
+  /* 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;
+  gulong pixel = 0;
 
-  if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
-    pixel = colorcube[((rgb & 0xf00000) >> 12) |
-                    ((rgb & 0xf000) >> 8) |
-                    ((rgb & 0xf0) >> 4)];
+  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 = colorcube_d[((rgb & 0x800000) >> 17) |
-                        ((rgb & 0x8000) >> 12) |
-                        ((rgb & 0x80) >> 7)];
+      pixel = image_info->colorcube_d[((r & 0x8000) >> 9) |
+                                     ((g & 0x8000) >> 12) |
+                                     ((b & 0x8000) >> 15)];
     }
-  else
+  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",
@@ -672,20 +873,32 @@ gdk_rgb_xpixel_from_rgb (guint32 rgb)
               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)
 {
@@ -704,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)
@@ -720,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;
@@ -785,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)
@@ -795,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;
@@ -828,7 +1050,7 @@ gdk_rgb_convert_8 (GdkImage *image,
 #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 },
@@ -964,7 +1186,7 @@ static guchar DM[128][128] =
 #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 },
@@ -1000,7 +1222,7 @@ gdk_rgb_preprocess_dm_565 (void)
 }
 
 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)
@@ -1010,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;
@@ -1039,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,
@@ -1050,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;
@@ -1083,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)
@@ -1094,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;
@@ -1115,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)
@@ -1147,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)
@@ -1168,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
 
@@ -1182,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)
@@ -1279,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)
@@ -1313,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)
@@ -1381,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)
@@ -1412,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)
@@ -1451,7 +1676,7 @@ gdk_rgb_convert_565_br (GdkImage *image,
    in this mode. */
 #ifdef HAIRY_CONVERT_565
 static void
-gdk_rgb_convert_565_d (GdkImage *image,
+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)
@@ -1576,7 +1801,7 @@ gdk_rgb_convert_565_d (GdkImage *image,
 }
 #else
 static void
-gdk_rgb_convert_565_d (GdkImage *image,
+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)
@@ -1621,7 +1846,7 @@ gdk_rgb_convert_565_d (GdkImage *image,
 #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)
@@ -1653,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)
@@ -1689,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)
@@ -1711,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)
@@ -1791,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)
@@ -1827,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)
@@ -1849,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;
@@ -1858,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)
@@ -1880,7 +2105,38 @@ 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;
+      obuf += bpl;
+    }
+}
+
+static void
+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;
@@ -1891,7 +2147,7 @@ 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,
+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,
@@ -1944,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,
@@ -1963,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;
@@ -2008,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,
@@ -2061,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,
@@ -2080,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;
@@ -2126,7 +2382,7 @@ gdk_rgb_convert_truecolor_msb_d (GdkImage *image,
 
 /* This actually works for depths from 3 to 7 */
 static void
-gdk_rgb_convert_4 (GdkImage *image,
+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,
@@ -2137,9 +2393,10 @@ gdk_rgb_convert_4 (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;
   obuf = ((guchar *)image->mem) + y0 * bpl + x0;
@@ -2166,7 +2423,7 @@ gdk_rgb_convert_4 (GdkImage *image,
 
 /* This actually works for depths from 3 to 7 */
 static void
-gdk_rgb_convert_gray4 (GdkImage *image,
+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)
@@ -2199,19 +2456,68 @@ gdk_rgb_convert_gray4 (GdkImage *image,
     }
 }
 
+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 (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_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;
-  guchar *dmp;
+  const guchar *dmp;
   gint prec, right;
   gint gray;
 
@@ -2224,9 +2530,9 @@ gdk_rgb_convert_gray4_d (GdkImage *image,
     {
       bp2 = bptr;
       obptr = obuf;
+      dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
       for (x = 0; x < width; x++)
        {
-         dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
          r = *bp2++;
          g = *bp2++;
          b = *bp2++;
@@ -2241,7 +2547,66 @@ gdk_rgb_convert_gray4_d (GdkImage *image,
 }
 
 static void
-gdk_rgb_convert_1 (GdkImage *image,
+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,
@@ -2252,7 +2617,7 @@ gdk_rgb_convert_1 (GdkImage *image,
   guchar *obuf, *obptr;
   guchar *bptr, *bp2;
   gint r, g, b;
-  guchar *dmp;
+  const guchar *dmp;
   gint dith;
   guchar byte;
 
@@ -2287,23 +2652,24 @@ gdk_rgb_convert_1 (GdkImage *image,
 
 /* 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;
@@ -2323,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);
 }
@@ -2338,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;
@@ -2360,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;
@@ -2380,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);
 }
@@ -2432,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;
@@ -2441,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;
@@ -2461,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);
 }
@@ -2490,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;
@@ -2500,18 +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 lsb_24, msb_24;
+  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 0x%x, image bpp=%d, %s first\n",
-            (gint)(((GdkVisualPrivate *)image_info->visual)->xvisual->visualid),
+    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);
@@ -2525,16 +2900,8 @@ gdk_rgb_select_conv (GdkImage *image)
   green_mask = image_info->visual->green_mask;
   blue_mask = image_info->visual->blue_mask;
 
-  lsb_24 =
-    (byte_order == GDK_LSB_FIRST &&
-     red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff) ||
-    (byte_order == GDK_MSB_FIRST &&
-     red_mask == 0xff && green_mask == 0xff00 && blue_mask == 0xff0000);
-  msb_24 =
-    (byte_order == GDK_MSB_FIRST &&
-     red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff) ||
-    (byte_order == GDK_LSB_FIRST &&
-     red_mask == 0xff && green_mask == 0xff00 && blue_mask == 0xff0000);
+  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;
@@ -2552,7 +2919,7 @@ gdk_rgb_select_conv (GdkImage *image)
 
   if (image_info->bitmap)
     conv = gdk_rgb_convert_1;
-  else if (bpp == 2 && depth == 16 && !byterev &&
+  else if (bpp == 16 && depth == 16 && !byterev &&
       red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f)
     {
       conv = gdk_rgb_convert_565;
@@ -2560,36 +2927,50 @@ gdk_rgb_select_conv (GdkImage *image)
       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 - but testing will show*/
-  else if (bpp == 3 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && lsb_24)
+  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 && msb_24)
+  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;
-#ifdef WORDS_BIGENDIAN
-  else if (bpp == 4 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && lsb_24)
+#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 == 4 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && msb_24)
+  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 == 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 == 4 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && lsb_24)
-    conv = gdk_rgb_convert_0888;
-  else if (bpp == 4 && depth == 24 && vtype == GDK_VISUAL_TRUE_COLOR && msb_24)
+  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)
@@ -2602,7 +2983,7 @@ gdk_rgb_select_conv (GdkImage *image)
       conv = gdk_rgb_convert_truecolor_msb;
       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
@@ -2622,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
@@ -2631,16 +3012,24 @@ gdk_rgb_select_conv (GdkImage *image)
       conv = gdk_rgb_convert_gray8;
       conv_gray = gdk_rgb_convert_gray8_gray;
     }
-  else if (depth < 8 && depth >= 3 && (vtype == GDK_VISUAL_STATIC_GRAY
-                                      || vtype == GDK_VISUAL_GRAYSCALE))
+  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 (depth < 8 && depth >= 3)
+  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;
@@ -2658,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
@@ -2677,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 ();
@@ -2688,81 +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;
          /* using 3 and -4 would be slightly more efficient on 32-bit machines
             with > 1bpp displays */
-         vert_x += (width + 7) & -8;
+         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 + 7) & -8;
+         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++;
@@ -2771,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,
@@ -2805,17 +3193,17 @@ gdk_draw_rgb_image_core (GdkDrawable *drawable,
        }
       gc = image_info->own_gc;
     }
-  for (y0 = 0; y0 < height; y0 += IMAGE_HEIGHT)
+  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,
+         conv (image_info, image, xs0, ys0, width1, height1, buf_ptr, rowstride,
                x + x0 + xdith, y + y0 + ydith, cmap);
 
 #ifndef DONT_ACTUALLY_DRAW
@@ -2826,6 +3214,34 @@ gdk_draw_rgb_image_core (GdkDrawable *drawable,
     }
 }
 
+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,
@@ -2838,13 +3254,17 @@ 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 (drawable, gc, x, y, width, height,
+    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 (drawable, gc, x, y, width, height,
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
                             rgb_buf, 3, rowstride, image_info->conv_d, NULL,
                             0, 0);
 }
@@ -2862,13 +3282,17 @@ gdk_draw_rgb_image_dithalign (GdkDrawable *drawable,
                              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,
                             rgb_buf, 3, rowstride, image_info->conv, 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,
                             rgb_buf, 3, rowstride, image_info->conv_d, NULL,
                             xdith, ydith);
 }
@@ -2884,17 +3308,49 @@ 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 (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, 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, 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 (image_info, drawable, gc, x, y, width, height,
+                            buf, 4, rowstride,
+                            image_info->conv_32, NULL, xdith, ydith);
+  else
+    gdk_draw_rgb_image_core (image_info, drawable, gc, x, y, width, height,
+                            buf, 4, rowstride,
+                            image_info->conv_32_d, NULL, xdith, ydith);
+}
+
 static void
 gdk_rgb_make_gray_cmap (GdkRgbInfo *info)
 {
@@ -2917,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 ||
@@ -2925,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, 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, 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);
 }
 
@@ -2980,33 +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, 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, 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)
 {
-  gdk_rgb_init ();
-  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)
 {
-  gdk_rgb_init ();
-  return image_info->visual;
+  return gdk_colormap_get_visual (gdk_rgb_get_colormap ());
 }