* 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.
*/
/*
- * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
+ * 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 "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
GdkVisual *visual;
GdkColormap *cmap;
- gulong *color_pixels;
- gulong *gray_pixels;
- gulong *reserved_pixels;
-
guint nred_shades;
guint ngreen_shades;
guint nblue_shades;
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)
}
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);
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;
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 ();
}
#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)
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];
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)
{
static const gint sizes[][3] = {
/* { 6, 7, 6 }, */
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;
}
}
gdk_rgb_score_visual (GdkVisual *visual)
{
guint32 quality, speed, sys, pseudo;
- static const gchar* visual_names[] =
- {
- "static gray",
- "grayscale",
- "static color",
- "pseudo color",
- "true color",
- "direct color",
- };
-
quality = 0;
speed = 1;
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,
return (quality << 12) | (speed << 8) | (sys << 4) | pseudo;
}
-static void
+static GdkVisual *
gdk_rgb_choose_visual (void)
{
GList *visuals, *tmp_list;
g_list_free (visuals);
- image_info->visual = best_visual;
+ 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;
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",
/* 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++)
{
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;
- static const 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 */
-#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
+ 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;
+}
- if (image_info == NULL)
+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);
+}
- image_info->visual = NULL;
- image_info->cmap = NULL;
+/* 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->color_pixels = NULL;
- image_info->gray_pixels = NULL;
- image_info->reserved_pixels = NULL;
+ image_info = g_new0 (GdkRgbInfo, 1);
- 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->visual = visual;
+ image_info->cmap = NULL;
- image_info->bpp = 0;
- image_info->cmap_alloced = FALSE;
- image_info->gamma = 1.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->stage_buf = NULL;
+ image_info->bpp = 0;
+ image_info->cmap_alloced = FALSE;
+ image_info->gamma = 1.0;
- image_info->own_gc = NULL;
+ image_info->stage_buf = NULL;
- gdk_rgb_choose_visual ();
+ image_info->own_gc = NULL;
- 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)
+ 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->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)
- /* Use malloc() instead of g_malloc since X will free() this mem */
- static_image[i] = gdk_image_new_bitmap (image_info->visual,
- (gpointer) 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 = 0;
+ GdkRgbInfo *image_info = gdk_rgb_get_info_from_colormap (colormap);
+
if (image_info->bitmap)
{
- return ((rgb & 0xff0000) >> 16) +
- ((rgb & 0xff00) >> 7) +
- (rgb & 0xff) > 510;
+ return (r + (g << 1) + b) > 131070;
}
else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
- pixel = colorcube[((rgb & 0xf00000) >> 12) |
- ((rgb & 0xf000) >> 8) |
- ((rgb & 0xf0) >> 4)];
+ 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 if (image_info->visual->type == GDK_VISUAL_TRUE_COLOR ||
image_info->visual->type == GDK_VISUAL_DIRECT_COLOR)
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 = ((rgb & 0xff0000) >> 16) +
- ((rgb & 0xff00) >> 7) +
- (rgb & 0xff);
-
- return gray >> (10 - image_info->visual->depth);
+ 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)
{
gdk_gc_set_background (gc, &color);
}
+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)
guchar *obuf, *obptr;
guchar *bptr, *bp2;
gint r, g, b;
+ guchar *colorcube = image_info->colorcube;
bptr = buf;
bpl = image->bpl;
}
#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)
guchar *obuf, *obptr;
guchar *bptr, *bp2;
gint r, g, b;
+ guchar *colorcube = image_info->colorcube;
bptr = buf;
bpl = image->bpl;
}
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)
gint r, g, b;
const guchar *dmp;
gint dith;
+ guchar *colorcube_d = image_info->colorcube_d;
bptr = buf;
bpl = image->bpl;
}
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,
const guchar *dmp;
gint dith;
gint rs, gs, bs;
+ guchar *colorcube_d = image_info->colorcube_d;
bptr = buf;
bpl = image->bpl;
}
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)
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;
}
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)
}
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)
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)
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)
#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)
}
#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)
#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)
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)
}
#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)
#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)
}
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)
}
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)
#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)
}
#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)
/* 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)
}
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)
}
static void
-gdk_rgb_convert_8880_br (GdkImage *image,
+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)
/* 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,
}
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,
}
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,
}
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,
/* 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,
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;
/* 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)
}
static void
-gdk_rgb_convert_gray4_pack (GdkImage *image,
+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)
/* 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;
}
static void
-gdk_rgb_convert_gray4_d_pack (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)
}
static void
-gdk_rgb_convert_1 (GdkImage *image,
+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,
/* 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;
/* 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);
}
/* 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;
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;
/* 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);
}
/* 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;
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;
/* 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);
}
/* 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;
gboolean mask_rgb, mask_bgr;
depth = image_info->visual->depth;
-#if defined (GDK_RGB_STANDALONE) || defined (GDK_WINDOWING_X11)
- bpp = ((GdkImagePrivate *)image)->ximage->bits_per_pixel;
-#elif defined (GDK_WINDOWING_WIN32)
- bpp = ((GdkVisualPrivate *) gdk_visual_get_system())->xvisual->bitspixel;
-#endif
+
+ /* 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");
#if G_BYTE_ORDER == G_BIG_ENDIAN
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 == 32 || depth == 24) && vtype == GDK_VISUAL_TRUE_COLOR &&
+ 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 &&
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
#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 ();
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++;
}
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,
}
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
}
}
+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,
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);
}
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);
}
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)
{
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 ||
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);
}
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 ());
}