1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* Color Context module
21 * Copyright 1994,1995 John L. Cwikla
22 * Copyright (C) 1997 by Ripley Software Development
23 * Copyright (C) 1997 by Federico Mena (port to Gtk/Gdk)
26 /* Copyright 1994,1995 John L. Cwikla
28 * Permission to use, copy, modify, distribute, and sell this software
29 * and its documentation for any purpose is hereby granted without fee,
30 * provided that the above copyright notice appears in all copies and that
31 * both that copyright notice and this permission notice appear in
32 * supporting documentation, and that the name of John L. Cwikla or
33 * Wolfram Research, Inc not be used in advertising or publicity
34 * pertaining to distribution of the software without specific, written
35 * prior permission. John L. Cwikla and Wolfram Research, Inc make no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
39 * John L. Cwikla and Wolfram Research, Inc disclaim all warranties with
40 * regard to this software, including all implied warranties of
41 * merchantability and fitness, in no event shall John L. Cwikla or
42 * Wolfram Research, Inc be liable for any special, indirect or
43 * consequential damages or any damages whatsoever resulting from loss of
44 * use, data or profits, whether in an action of contract, negligence or
45 * other tortious action, arising out of or in connection with the use or
46 * performance of this software.
51 * Wolfram Research Inc.
57 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
58 * file for a list of people on the GTK+ Team. See the ChangeLog
59 * files for a list of changes. These files are distributed with
60 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
72 #define MAX_IMAGE_COLORS 256
74 typedef struct _GdkColorContextPrivate GdkColorContextPrivate;
76 struct _GdkColorContextPrivate
78 GdkColorContext color_context;
79 XStandardColormap std_cmap;
83 hash_color (gconstpointer key)
85 const GdkColor *color = key;
87 return (color->red * 33023 + color->green * 30013 + color->blue * 27011);
91 compare_colors (gconstpointer a,
94 const GdkColor *aa = a;
95 const GdkColor *bb = b;
97 return ((aa->red == bb->red) && (aa->green == bb->green) && (aa->blue == bb->blue));
101 free_hash_entry (gpointer key,
105 g_free (key); /* key and value are the same GdkColor */
109 pixel_sort (const void *a, const void *b)
111 return ((GdkColor *) a)->pixel - ((GdkColor *) b)->pixel;
115 my_x_query_colors (GdkColormap *colormap,
121 for (i = 0; i < ncolors; i++)
123 PALETTEENTRY palentry;
125 GetPaletteEntries (GDK_COLORMAP_XCOLORMAP (colormap)->palette, colors[i].pixel, 1, &palentry);
126 colors[i].red = (palentry.peRed * 65536) / 255;
127 colors[i].green = (palentry.peGreen * 65536) / 255;
128 colors[i].blue = (palentry.peBlue * 65536) / 255;
133 query_colors (GdkColorContext *cc)
136 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
137 cc->cmap = g_new (GdkColor, cc->num_colors);
139 for (i = 0; i < cc->num_colors; i++)
140 cc->cmap[i].pixel = cc->clut ? cc->clut[i] : ccp->std_cmap.base_pixel + i;
142 my_x_query_colors (cc->colormap, cc->cmap, cc->num_colors);
144 qsort (cc->cmap, cc->num_colors, sizeof (GdkColor), pixel_sort);
148 init_bw (GdkColorContext *cc)
152 g_warning ("init_bw: failed to allocate colors, falling back to black and white");
154 cc->mode = GDK_CC_MODE_BW;
156 color.red = color.green = color.blue = 0;
158 if (!gdk_color_alloc (cc->colormap, &color))
161 cc->black_pixel = color.pixel;
163 color.red = color.green = color.blue = 0xffff;
165 if (!gdk_color_alloc (cc->colormap, &color))
166 cc->white_pixel = cc->black_pixel ? 0 : 1;
168 cc->white_pixel = color.pixel;
174 init_gray (GdkColorContext *cc)
176 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
177 GdkColor *clrs, *cstart;
181 cc->num_colors = 256; /* Bogus, but will never get here anyway? */
183 cc->clut = g_new (gulong, cc->num_colors);
184 cstart = g_new (GdkColor, cc->num_colors);
188 dinc = 65535.0 / (cc->num_colors - 1);
192 for (i = 0; i < cc->num_colors; i++)
194 clrs->red = clrs->green = clrs->blue = dinc * i;
196 if (!gdk_color_alloc (cc->colormap, clrs))
198 gdk_colors_free (cc->colormap, cc->clut, i, 0);
202 if (cc->num_colors > 1)
214 cc->clut[i] = clrs++->pixel;
219 /* XXX: is this the right thing to do? */
220 ccp->std_cmap.colormap = GDK_COLORMAP_XCOLORMAP (cc->colormap);
221 ccp->std_cmap.base_pixel = 0;
222 ccp->std_cmap.red_max = cc->num_colors - 1;
223 ccp->std_cmap.green_max = 0;
224 ccp->std_cmap.blue_max = 0;
225 ccp->std_cmap.red_mult = 1;
226 ccp->std_cmap.green_mult = 0;
227 ccp->std_cmap.blue_mult = 0;
229 cc->white_pixel = 255;
234 cc->mode = GDK_CC_MODE_MY_GRAY;
238 init_color (GdkColorContext *cc)
240 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
244 while ((cubeval * cubeval * cubeval) < GDK_VISUAL_XVISUAL (cc->visual)->map_entries)
248 cc->num_colors = cubeval * cubeval * cubeval;
250 ccp->std_cmap.red_max = cubeval - 1;
251 ccp->std_cmap.green_max = cubeval - 1;
252 ccp->std_cmap.blue_max = cubeval - 1;
253 ccp->std_cmap.red_mult = cubeval * cubeval;
254 ccp->std_cmap.green_mult = cubeval;
255 ccp->std_cmap.blue_mult = 1;
256 ccp->std_cmap.base_pixel = 0;
258 cc->white_pixel = 255; /* ??? */
259 cc->black_pixel = 0; /* ??? */
261 /* a CLUT for storing allocated pixel indices */
263 cc->max_colors = cc->num_colors;
264 cc->clut = g_new (gulong, cc->max_colors);
266 for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
267 cc->clut[cubeval] = cubeval;
271 cc->mode = GDK_CC_MODE_STD_CMAP;
276 init_true_color (GdkColorContext *cc)
278 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
279 gulong rmask, gmask, bmask;
281 cc->mode = GDK_CC_MODE_TRUE;
285 rmask = cc->masks.red = cc->visual->red_mask;
304 gmask = cc->masks.green = cc->visual->green_mask;
306 cc->shifts.green = 0;
323 bmask = cc->masks.blue = cc->visual->blue_mask;
340 cc->num_colors = (cc->visual->red_mask | cc->visual->green_mask | cc->visual->blue_mask) + 1;
342 cc->white_pixel = 0xffffff;
347 init_palette (GdkColorContext *cc)
349 /* restore correct mode for this cc */
351 switch (cc->visual->type)
353 case GDK_VISUAL_STATIC_GRAY:
354 case GDK_VISUAL_GRAYSCALE:
355 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
356 cc->mode = GDK_CC_MODE_BW;
358 cc->mode = GDK_CC_MODE_MY_GRAY;
361 case GDK_VISUAL_TRUE_COLOR:
362 cc->mode = GDK_CC_MODE_TRUE;
365 case GDK_VISUAL_STATIC_COLOR:
366 case GDK_VISUAL_PSEUDO_COLOR:
367 cc->mode = GDK_CC_MODE_STD_CMAP;
371 cc->mode = GDK_CC_MODE_UNDEFINED;
375 /* previous palette */
378 g_free (cc->palette);
381 g_free (cc->fast_dither);
383 /* clear hash table if present */
387 g_hash_table_foreach (cc->color_hash,
390 g_hash_table_destroy (cc->color_hash);
391 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
396 cc->fast_dither = NULL;
400 gdk_color_context_new (GdkVisual *visual,
401 GdkColormap *colormap)
403 GdkColorContextPrivate *ccp;
404 gint use_private_colormap = FALSE; /* XXX: maybe restore full functionality later? */
407 GdkColormap *default_colormap;
409 g_assert (visual != NULL);
410 g_assert (colormap != NULL);
412 ccp = g_new (GdkColorContextPrivate, 1);
413 cc = (GdkColorContext *) ccp;
415 cc->colormap = colormap;
418 cc->mode = GDK_CC_MODE_UNDEFINED;
419 cc->need_to_free_colormap = FALSE;
421 cc->color_hash = NULL;
424 cc->fast_dither = NULL;
426 default_colormap = gdk_colormap_get_system ();
430 while (retry_count < 2)
432 /* Only create a private colormap if the visual found isn't equal
433 * to the default visual and we don't have a private colormap,
434 * -or- if we are instructed to create a private colormap (which
435 * never is the case for XmHTML).
438 if (use_private_colormap
439 || ((cc->visual != gdk_visual_get_system ()) /* default visual? */
440 && (GDK_COLORMAP_XCOLORMAP (colormap) == GDK_COLORMAP_XCOLORMAP (default_colormap))))
442 g_warning ("gdk_color_context_new: non-default visual detected, "
443 "using private colormap");
445 cc->colormap = gdk_colormap_new (cc->visual, FALSE);
447 cc->need_to_free_colormap = (GDK_COLORMAP_XCOLORMAP (colormap)
448 != GDK_COLORMAP_XCOLORMAP (default_colormap));
451 switch (visual->type)
453 case GDK_VISUAL_STATIC_GRAY:
454 case GDK_VISUAL_GRAYSCALE:
455 GDK_NOTE (COLOR_CONTEXT,
456 g_message ("gdk_color_context_new: visual class is %s\n",
457 (visual->type == GDK_VISUAL_STATIC_GRAY) ?
458 "GDK_VISUAL_STATIC_GRAY" :
459 "GDK_VISUAL_GRAYSCALE"));
461 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
468 case GDK_VISUAL_TRUE_COLOR: /* shifts */
469 GDK_NOTE (COLOR_CONTEXT,
470 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
472 init_true_color (cc);
475 case GDK_VISUAL_STATIC_COLOR:
476 case GDK_VISUAL_PSEUDO_COLOR:
477 GDK_NOTE (COLOR_CONTEXT,
478 g_message ("gdk_color_context_new: visual class is %s\n",
479 (visual->type == GDK_VISUAL_STATIC_COLOR) ?
480 "GDK_VISUAL_STATIC_COLOR" :
481 "GDK_VISUAL_PSEUDO_COLOR"));
487 g_assert_not_reached ();
490 if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
492 use_private_colormap = TRUE;
499 /* no. of colors allocated yet */
501 cc->num_allocated = 0;
503 GDK_NOTE (COLOR_CONTEXT,
504 g_message ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
505 cc->visual->depth, cc->num_colors));
507 return (GdkColorContext *) cc;
511 gdk_color_context_new_mono (GdkVisual *visual,
512 GdkColormap *colormap)
514 GdkColorContextPrivate *ccp;
517 g_assert (visual != NULL);
518 g_assert (colormap != NULL);
520 cc = g_new (GdkColorContext, 1);
521 ccp = (GdkColorContextPrivate *) cc;
523 cc->colormap = colormap;
526 cc->mode = GDK_CC_MODE_UNDEFINED;
527 cc->need_to_free_colormap = FALSE;
531 return (GdkColorContext *) cc;
534 /* This doesn't currently free black/white, hmm... */
537 gdk_color_context_free (GdkColorContext *cc)
539 g_assert (cc != NULL);
541 if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
542 || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
544 gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
547 else if (cc->clut != NULL)
549 gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
553 if (cc->cmap != NULL)
556 if (cc->need_to_free_colormap)
557 gdk_colormap_unref (cc->colormap);
559 /* free any palette that has been associated with this GdkColorContext */
567 gdk_color_context_get_pixel (GdkColorContext *cc,
573 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
574 g_assert (cc != NULL);
575 g_assert (failed != NULL);
585 value = (red / 65535.0 * 0.30
586 + green / 65535.0 * 0.59
587 + blue / 65535.0 * 0.11);
590 return cc->white_pixel;
592 return cc->black_pixel;
595 case GDK_CC_MODE_MY_GRAY:
597 gulong ired, igreen, iblue;
599 red = red * 0.30 + green * 0.59 + blue * 0.11;
603 if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
604 ired = ccp->std_cmap.red_max;
606 ired *= ccp->std_cmap.red_mult;
608 if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
609 igreen = ccp->std_cmap.green_max;
611 igreen *= ccp->std_cmap.green_mult;
613 if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
614 iblue = ccp->std_cmap.blue_max;
616 iblue *= ccp->std_cmap.blue_mult;
618 if (cc->clut != NULL)
619 return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
621 return ccp->std_cmap.base_pixel + ired + igreen + iblue;
624 case GDK_CC_MODE_TRUE:
626 gulong ired, igreen, iblue;
628 if (cc->clut == NULL)
630 red >>= 16 - cc->bits.red;
631 green >>= 16 - cc->bits.green;
632 blue >>= 16 - cc->bits.blue;
634 ired = (red << cc->shifts.red) & cc->masks.red;
635 igreen = (green << cc->shifts.green) & cc->masks.green;
636 iblue = (blue << cc->shifts.blue) & cc->masks.blue;
638 return ired | igreen | iblue;
641 ired = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
642 igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
643 iblue = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
645 return ired | igreen | iblue;
648 case GDK_CC_MODE_PALETTE:
649 return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
651 case GDK_CC_MODE_STD_CMAP:
655 GdkColor *result = NULL;
662 result = g_hash_table_lookup (cc->color_hash, &color);
671 if (!gdk_color_alloc (cc->colormap, &color))
677 /* XXX: the following comment comes directly from
678 * XCC.c. I don't know if it is relevant for
679 * gdk_color_alloc() as it is for XAllocColor()
683 * I can't figure this out entirely, but it *is* possible
684 * that XAllocColor succeeds, even if the number of
685 * allocations we've made exceeds the number of available
686 * colors in the current colormap. And therefore it
687 * might be necessary for us to resize the CLUT.
690 if (cc->num_allocated == cc->max_colors)
694 GDK_NOTE (COLOR_CONTEXT,
695 g_message ("gdk_color_context_get_pixel: "
696 "resizing CLUT to %i entries\n",
699 cc->clut = g_realloc (cc->clut,
700 cc->max_colors * sizeof (gulong));
703 /* Key and value are the same color structure */
705 cnew = g_new (GdkColor, 1);
709 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
710 g_hash_table_insert (cc->color_hash, cnew, cnew);
712 cc->clut[cc->num_allocated] = color.pixel;
718 return result->pixel;
724 gdk_color_context_get_pixels (GdkColorContext *cc,
733 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
734 gint bad_alloc = FALSE;
735 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
736 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
737 #ifdef G_ENABLE_DEBUG
738 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
740 g_assert (cc != NULL);
741 g_assert (reds != NULL);
742 g_assert (greens != NULL);
743 g_assert (blues != NULL);
744 g_assert (colors != NULL);
745 g_assert (nallocated != NULL);
747 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
748 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
749 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
751 /* Will only have a value if used by the progressive image loader */
757 /* First allocate all pixels */
759 for (i = 0; i < ncolors; i++)
761 /* colors[i] is only zero if the pixel at that location hasn't
762 * been allocated yet. This is a sanity check required for proper
763 * color allocation by the progressive image loader
768 defs[i].red = reds[i];
769 defs[i].green = greens[i];
770 defs[i].blue = blues[i];
772 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i],
775 /* successfully allocated, store it */
779 defs[i].pixel = colors[i];
780 allocated[ncols++] = colors[i];
789 /* all colors available, all done */
791 if ((ncols == ncolors) || (nopen == 0))
793 GDK_NOTE (COLOR_CONTEXT,
794 g_message ("gdk_color_context_get_pixels: got all %i colors; "
795 "(%i colors allocated so far)\n", ncolors, cc->num_allocated));
800 /* The fun part. We now try to allocate the colors we couldn't allocate
801 * directly. The first step will map a color onto its nearest color
802 * that has been allocated (either by us or someone else). If any colors
803 * remain unallocated, we map these onto the colors that we have allocated
807 /* read up to MAX_IMAGE_COLORS colors of the current colormap */
809 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
811 /* see if the colormap has any colors to read */
815 g_warning ("gdk_color_context_get_pixels: oops! no colors available, "
816 "your images will look *really* ugly.");
821 #ifdef G_ENABLE_DEBUG
825 /* initialize pixels */
827 for (i = 0; i < cmapsize; i++)
830 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
833 /* read the colormap */
835 my_x_query_colors (cc->colormap, cmap, cmapsize);
837 /* get a close match for any unallocated colors */
845 gint d, j, mdist, close, ri, gi, bi;
853 /* Store these vals. Small performance increase as this skips three
854 * indexing operations in the loop code.
861 /* Walk all colors in the colormap and see which one is the
862 * closest. Uses plain least squares.
865 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
867 /* Don't replace these by shifts; the sign may get clobbered */
869 rd = (ri - cmap[j].red) / 256;
870 gd = (gi - cmap[j].green) / 256;
871 bd = (bi - cmap[j].blue) / 256;
873 d = rd * rd + gd * gd + bd * bd;
884 rd = cmap[close].red;
885 gd = cmap[close].green;
886 bd = cmap[close].blue;
890 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
896 defs[i] = cmap[close];
897 defs[i].pixel = colors[i];
898 allocated[ncols++] = colors[i];
899 #ifdef G_ENABLE_DEBUG
906 /* deal with in next stage if allocation failed */
908 while (++idx < counter);
912 /* This is the maximum no. of allocated colors. See also the nopen == 0
916 if ((ncols == ncolors) || (nopen == 0))
918 GDK_NOTE (COLOR_CONTEXT,
919 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact and "
920 "%i close (%i colors allocated so far)\n",
921 ncolors, exact_col, close_col, cc->num_allocated));
926 /* Now map any remaining unallocated pixels into the colors we did get */
932 gint d, mdist, close, ri, gi, bi;
946 /* search allocated colors */
948 for (j = 0; (j < ncols) && (mdist != 0); j++)
952 /* Don't replace these by shifts; the sign may get clobbered */
954 rd = (ri - defs[k].red) / 256;
955 gd = (gi - defs[k].green) / 256;
956 bd = (bi - defs[k].blue) / 256;
958 d = rd * rd + gd * gd + bd * bd;
969 /* too bad, map to black */
971 defs[i].pixel = cc->black_pixel;
972 defs[i].red = defs[i].green = defs[i].blue = 0;
973 #ifdef G_ENABLE_DEBUG
979 defs[i] = defs[close];
980 #ifdef G_ENABLE_DEBUG
985 colors[i] = defs[i].pixel;
987 while (++idx < nopen);
989 GDK_NOTE (COLOR_CONTEXT,
990 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact, %i close, "
991 "%i substituted, %i to black (%i colors allocated so far)\n",
992 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
996 gdk_color_context_get_pixels_incremental (GdkColorContext *cc,
1006 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
1007 gint bad_alloc = FALSE;
1008 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
1009 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
1010 #ifdef G_ENABLE_DEBUG
1011 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
1014 g_assert (cc != NULL);
1015 g_assert (reds != NULL);
1016 g_assert (greens != NULL);
1017 g_assert (blues != NULL);
1018 g_assert (used != NULL);
1019 g_assert (colors != NULL);
1020 g_assert (nallocated != NULL);
1022 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
1023 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
1024 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
1026 /* Will only have a value if used by the progressive image loader */
1028 ncols = *nallocated;
1032 /* First allocate all pixels */
1034 for (i = 0; i < ncolors; i++)
1036 /* used[i] is only -1 if the pixel at that location hasn't
1037 * been allocated yet. This is a sanity check required for proper
1038 * color allocation by the progressive image loader.
1039 * When colors[i] == 0 it indicates the slot is available for
1043 if (used[i] != FALSE)
1047 defs[i].red = reds[i];
1048 defs[i].green = greens[i];
1049 defs[i].blue = blues[i];
1051 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i], &bad_alloc);
1053 /* successfully allocated, store it */
1057 defs[i].pixel = colors[i];
1058 allocated[ncols++] = colors[i];
1061 failed[nopen++] = i;
1065 GDK_NOTE (COLOR_CONTEXT,
1066 g_message ("gdk_color_context_get_pixels_incremental: "
1067 "pixel at slot %i already allocated, skipping\n", i));
1072 *nallocated = ncols;
1074 if ((ncols == ncolors) || (nopen == 0))
1076 GDK_NOTE (COLOR_CONTEXT,
1077 g_message ("gdk_color_context_get_pixels_incremental: got all %i colors "
1078 "(%i colors allocated so far)\n",
1079 ncolors, cc->num_allocated));
1084 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
1088 g_warning ("gdk_color_context_get_pixels_incremental: oops! "
1089 "No colors available images will look *really* ugly.");
1093 #ifdef G_ENABLE_DEBUG
1097 /* initialize pixels */
1099 for (i = 0; i < cmapsize; i++)
1102 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
1107 my_x_query_colors (cc->colormap, cmap, cmapsize);
1109 /* now match any unallocated colors */
1117 gint d, j, mdist, close, ri, gi, bi;
1131 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
1133 /* Don't replace these by shifts; the sign may get clobbered */
1135 rd = (ri - cmap[j].red) / 256;
1136 gd = (gi - cmap[j].green) / 256;
1137 bd = (bi - cmap[j].blue) / 256;
1139 d = rd * rd + gd * gd + bd * bd;
1150 rd = cmap[close].red;
1151 gd = cmap[close].green;
1152 bd = cmap[close].blue;
1156 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1162 defs[i] = cmap[close];
1163 defs[i].pixel = colors[i];
1164 allocated[ncols++] = colors[i];
1165 #ifdef G_ENABLE_DEBUG
1170 failed[nopen++] = i;
1173 failed[nopen++] = i;
1174 /* deal with in next stage if allocation failed */
1176 while (++idx < counter);
1178 *nallocated = ncols;
1180 if ((ncols == ncolors) || (nopen == 0))
1182 GDK_NOTE (COLOR_CONTEXT,
1183 g_message ("gdk_color_context_get_pixels_incremental: "
1184 "got %i colors, %i exact and %i close "
1185 "(%i colors allocated so far)\n",
1186 ncolors, exact_col, close_col, cc->num_allocated));
1191 /* map remaining unallocated pixels into colors we did get */
1197 gint d, mdist, close, ri, gi, bi;
1209 /* search allocated colors */
1211 for (j = 0; (j < ncols) && (mdist != 0); j++)
1216 /* Don't replace these by shifts; the sign may get clobbered */
1218 rd = (ri - defs[k].red) / 256;
1219 gd = (gi - defs[k].green) / 256;
1220 bd = (bi - defs[k].blue) / 256;
1222 d = rd * rd + gd * gd + bd * bd;
1233 /* too bad, map to black */
1235 defs[i].pixel = cc->black_pixel;
1236 defs[i].red = defs[i].green = defs[i].blue = 0;
1237 #ifdef G_ENABLE_DEBUG
1243 defs[i] = defs[close];
1244 #ifdef G_ENABLE_DEBUG
1249 colors[i] = defs[i].pixel;
1251 while (++idx < nopen);
1253 GDK_NOTE (COLOR_CONTEXT,
1254 g_message ("gdk_color_context_get_pixels_incremental: "
1255 "got %i colors, %i exact, %i close, %i substituted, %i to black "
1256 "(%i colors allocated so far)\n",
1257 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1261 gdk_color_context_query_color (GdkColorContext *cc,
1264 return gdk_color_context_query_colors (cc, color, 1);
1268 gdk_color_context_query_colors (GdkColorContext *cc,
1275 g_assert (cc != NULL);
1276 g_assert (colors != NULL);
1280 case GDK_CC_MODE_BW:
1281 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1283 if (tc->pixel == cc->white_pixel)
1284 tc->red = tc->green = tc->blue = 65535;
1286 tc->red = tc->green = tc->blue = 0;
1290 case GDK_CC_MODE_TRUE:
1291 if (cc->clut == NULL)
1292 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1294 tc->red = ((tc->pixel & cc->masks.red) >> cc->shifts.red) << (16 - cc->bits.red);
1295 tc->green = ((tc->pixel & cc->masks.green) >> cc->shifts.green) << (16 - cc->bits.green);
1296 tc->blue = ((tc->pixel & cc->masks.blue) >> cc->shifts.blue) << (16 - cc->bits.blue);
1300 my_x_query_colors (cc->colormap, colors, num_colors);
1305 case GDK_CC_MODE_STD_CMAP:
1307 if (cc->cmap == NULL)
1309 my_x_query_colors (cc->colormap, colors, num_colors);
1314 gint first, last, half;
1317 for (i = 0, tc = colors; i < num_colors; i++)
1320 last = cc->num_colors - 1;
1322 while (first <= last)
1324 half = (first + last) / 2;
1325 half_pixel = cc->cmap[half].pixel;
1327 if (tc->pixel == half_pixel)
1329 tc->red = cc->cmap[half].red;
1330 tc->green = cc->cmap[half].green;
1331 tc->blue = cc->cmap[half].blue;
1332 first = last + 1; /* false break */
1336 if (tc->pixel > half_pixel)
1351 gdk_color_context_add_palette (GdkColorContext *cc,
1359 g_assert (cc != NULL);
1361 /* initialize this palette (will also erase previous palette as well) */
1365 /* restore previous mode if we aren't adding a new palette */
1367 if (num_palette == 0)
1370 /* copy incoming palette */
1372 cc->palette = g_new0(GdkColor, num_palette);
1376 for (i = 0; i < num_palette; i++)
1381 /* try to allocate this color */
1384 g = palette[i].green;
1385 b = palette[i].blue;
1387 gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1389 /* only store if we succeed */
1393 /* store in palette */
1395 cc->palette[j].red = r;
1396 cc->palette[j].green = g;
1397 cc->palette[j].blue = b;
1398 cc->palette[j].pixel = pixel[0];
1400 /* move to next slot */
1408 if (j != num_palette)
1409 cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1411 /* clear the hash table, we don't use it when dithering */
1415 g_hash_table_foreach (cc->color_hash,
1418 g_hash_table_destroy (cc->color_hash);
1419 cc->color_hash = NULL;
1422 /* store real palette size */
1424 cc->num_palette = j;
1426 /* switch to palette mode */
1428 cc->mode = GDK_CC_MODE_PALETTE;
1432 qsort (cc->palette, cc->num_palette, sizeof (GdkColor), pixel_sort);
1434 cc->fast_dither = NULL;
1440 gdk_color_context_init_dither (GdkColorContext *cc)
1442 gint rr, gg, bb, err, erg, erb;
1443 gint success = FALSE;
1445 g_assert (cc != NULL);
1447 /* now we can initialize the fast dither matrix */
1449 if (cc->fast_dither == NULL)
1450 cc->fast_dither = g_new (GdkColorContextDither, 1);
1452 /* Fill it. We ignore unsuccessful allocations, they are just mapped
1453 * to black instead */
1455 for (rr = 0; rr < 32; rr++)
1456 for (gg = 0; gg < 32; gg++)
1457 for (bb = 0; bb < 32; bb++)
1459 err = (rr << 3) | (rr >> 2);
1460 erg = (gg << 3) | (gg >> 2);
1461 erb = (bb << 3) | (bb >> 2);
1463 cc->fast_dither->fast_rgb[rr][gg][bb] =
1464 gdk_color_context_get_index_from_palette (cc, &err, &erg, &erb, &success);
1465 cc->fast_dither->fast_err[rr][gg][bb] = err;
1466 cc->fast_dither->fast_erg[rr][gg][bb] = erg;
1467 cc->fast_dither->fast_erb[rr][gg][bb] = erb;
1472 gdk_color_context_free_dither (GdkColorContext *cc)
1474 g_assert (cc != NULL);
1476 if (cc->fast_dither)
1477 g_free (cc->fast_dither);
1479 cc->fast_dither = NULL;
1483 gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
1490 gint dif, dr, dg, db, j = -1;
1491 gint mindif = 0x7fffffff;
1492 gint err = 0, erg = 0, erb = 0;
1495 g_assert (cc != NULL);
1496 g_assert (red != NULL);
1497 g_assert (green != NULL);
1498 g_assert (blue != NULL);
1499 g_assert (failed != NULL);
1503 for (i = 0; i < cc->num_palette; i++)
1505 dr = *red - cc->palette[i].red;
1506 dg = *green - cc->palette[i].green;
1507 db = *blue - cc->palette[i].blue;
1509 dif = dr * dr + dg * dg + db * db;
1515 pixel = cc->palette[i].pixel;
1525 /* we failed to map onto a color */
1540 gdk_color_context_get_index_from_palette (GdkColorContext *cc,
1546 gint dif, dr, dg, db, j = -1;
1547 gint mindif = 0x7fffffff;
1548 gint err = 0, erg = 0, erb = 0;
1551 g_assert (cc != NULL);
1552 g_assert (red != NULL);
1553 g_assert (green != NULL);
1554 g_assert (blue != NULL);
1555 g_assert (failed != NULL);
1559 for (i = 0; i < cc->num_palette; i++)
1561 dr = *red - cc->palette[i].red;
1562 dg = *green - cc->palette[i].green;
1563 db = *blue - cc->palette[i].blue;
1565 dif = dr * dr + dg * dg + db * db;
1580 /* we failed to map onto a color */
1589 /* return error fractions */