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_WIN32COLORMAP (colormap)->palette, colors[i].pixel, 1, &palentry);
126 colors[i].red = (palentry.peRed * 65535) / 255;
127 colors[i].green = (palentry.peGreen * 65535) / 255;
128 colors[i].blue = (palentry.peBlue * 65535) / 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_WIN32COLORMAP (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_WIN32COLORMAP (colormap) ==
441 GDK_COLORMAP_WIN32COLORMAP (default_colormap))))
443 g_warning ("gdk_color_context_new: non-default visual detected, "
444 "using private colormap");
446 cc->colormap = gdk_colormap_new (cc->visual, FALSE);
448 cc->need_to_free_colormap =
449 (GDK_COLORMAP_WIN32COLORMAP (colormap)
450 != GDK_COLORMAP_WIN32COLORMAP (default_colormap));
453 switch (visual->type)
455 case GDK_VISUAL_STATIC_GRAY:
456 case GDK_VISUAL_GRAYSCALE:
457 GDK_NOTE (COLOR_CONTEXT,
458 g_message ("gdk_color_context_new: visual class is %s\n",
459 (visual->type == GDK_VISUAL_STATIC_GRAY) ?
460 "GDK_VISUAL_STATIC_GRAY" :
461 "GDK_VISUAL_GRAYSCALE"));
463 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
470 case GDK_VISUAL_TRUE_COLOR: /* shifts */
471 GDK_NOTE (COLOR_CONTEXT,
472 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
474 init_true_color (cc);
477 case GDK_VISUAL_STATIC_COLOR:
478 case GDK_VISUAL_PSEUDO_COLOR:
479 GDK_NOTE (COLOR_CONTEXT,
480 g_message ("gdk_color_context_new: visual class is %s\n",
481 (visual->type == GDK_VISUAL_STATIC_COLOR) ?
482 "GDK_VISUAL_STATIC_COLOR" :
483 "GDK_VISUAL_PSEUDO_COLOR"));
489 g_assert_not_reached ();
492 if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
494 use_private_colormap = TRUE;
501 /* no. of colors allocated yet */
503 cc->num_allocated = 0;
505 GDK_NOTE (COLOR_CONTEXT,
506 g_message ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
507 cc->visual->depth, cc->num_colors));
509 return (GdkColorContext *) cc;
513 gdk_color_context_new_mono (GdkVisual *visual,
514 GdkColormap *colormap)
516 GdkColorContextPrivate *ccp;
519 g_assert (visual != NULL);
520 g_assert (colormap != NULL);
522 cc = g_new (GdkColorContext, 1);
523 ccp = (GdkColorContextPrivate *) cc;
525 cc->colormap = colormap;
528 cc->mode = GDK_CC_MODE_UNDEFINED;
529 cc->need_to_free_colormap = FALSE;
533 return (GdkColorContext *) cc;
536 /* This doesn't currently free black/white, hmm... */
539 gdk_color_context_free (GdkColorContext *cc)
541 g_assert (cc != NULL);
543 if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
544 || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
546 gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
549 else if (cc->clut != NULL)
551 gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
555 if (cc->cmap != NULL)
558 if (cc->need_to_free_colormap)
559 gdk_colormap_unref (cc->colormap);
561 /* free any palette that has been associated with this GdkColorContext */
569 gdk_color_context_get_pixel (GdkColorContext *cc,
575 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
576 g_assert (cc != NULL);
577 g_assert (failed != NULL);
587 value = (red / 65535.0 * 0.30
588 + green / 65535.0 * 0.59
589 + blue / 65535.0 * 0.11);
592 return cc->white_pixel;
594 return cc->black_pixel;
597 case GDK_CC_MODE_MY_GRAY:
599 gulong ired, igreen, iblue;
601 red = red * 0.30 + green * 0.59 + blue * 0.11;
605 if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
606 ired = ccp->std_cmap.red_max;
608 ired *= ccp->std_cmap.red_mult;
610 if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
611 igreen = ccp->std_cmap.green_max;
613 igreen *= ccp->std_cmap.green_mult;
615 if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
616 iblue = ccp->std_cmap.blue_max;
618 iblue *= ccp->std_cmap.blue_mult;
620 if (cc->clut != NULL)
621 return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
623 return ccp->std_cmap.base_pixel + ired + igreen + iblue;
626 case GDK_CC_MODE_TRUE:
628 gulong ired, igreen, iblue;
630 if (cc->clut == NULL)
632 red >>= 16 - cc->bits.red;
633 green >>= 16 - cc->bits.green;
634 blue >>= 16 - cc->bits.blue;
636 ired = (red << cc->shifts.red) & cc->masks.red;
637 igreen = (green << cc->shifts.green) & cc->masks.green;
638 iblue = (blue << cc->shifts.blue) & cc->masks.blue;
640 return ired | igreen | iblue;
643 ired = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
644 igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
645 iblue = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
647 return ired | igreen | iblue;
650 case GDK_CC_MODE_PALETTE:
651 return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
653 case GDK_CC_MODE_STD_CMAP:
657 GdkColor *result = NULL;
664 result = g_hash_table_lookup (cc->color_hash, &color);
673 if (!gdk_color_alloc (cc->colormap, &color))
679 /* XXX: the following comment comes directly from
680 * XCC.c. I don't know if it is relevant for
681 * gdk_color_alloc() as it is for XAllocColor()
685 * I can't figure this out entirely, but it *is* possible
686 * that XAllocColor succeeds, even if the number of
687 * allocations we've made exceeds the number of available
688 * colors in the current colormap. And therefore it
689 * might be necessary for us to resize the CLUT.
692 if (cc->num_allocated == cc->max_colors)
696 GDK_NOTE (COLOR_CONTEXT,
697 g_message ("gdk_color_context_get_pixel: "
698 "resizing CLUT to %i entries\n",
701 cc->clut = g_realloc (cc->clut,
702 cc->max_colors * sizeof (gulong));
705 /* Key and value are the same color structure */
707 cnew = g_new (GdkColor, 1);
711 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
712 g_hash_table_insert (cc->color_hash, cnew, cnew);
714 cc->clut[cc->num_allocated] = color.pixel;
720 return result->pixel;
726 gdk_color_context_get_pixels (GdkColorContext *cc,
735 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
736 gint bad_alloc = FALSE;
737 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
738 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
739 #ifdef G_ENABLE_DEBUG
740 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
742 g_assert (cc != NULL);
743 g_assert (reds != NULL);
744 g_assert (greens != NULL);
745 g_assert (blues != NULL);
746 g_assert (colors != NULL);
747 g_assert (nallocated != NULL);
749 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
750 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
751 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
753 /* Will only have a value if used by the progressive image loader */
759 /* First allocate all pixels */
761 for (i = 0; i < ncolors; i++)
763 /* colors[i] is only zero if the pixel at that location hasn't
764 * been allocated yet. This is a sanity check required for proper
765 * color allocation by the progressive image loader
770 defs[i].red = reds[i];
771 defs[i].green = greens[i];
772 defs[i].blue = blues[i];
774 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i],
777 /* successfully allocated, store it */
781 defs[i].pixel = colors[i];
782 allocated[ncols++] = colors[i];
791 /* all colors available, all done */
793 if ((ncols == ncolors) || (nopen == 0))
795 GDK_NOTE (COLOR_CONTEXT,
796 g_message ("gdk_color_context_get_pixels: got all %i colors; "
797 "(%i colors allocated so far)\n", ncolors, cc->num_allocated));
802 /* The fun part. We now try to allocate the colors we couldn't allocate
803 * directly. The first step will map a color onto its nearest color
804 * that has been allocated (either by us or someone else). If any colors
805 * remain unallocated, we map these onto the colors that we have allocated
809 /* read up to MAX_IMAGE_COLORS colors of the current colormap */
811 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
813 /* see if the colormap has any colors to read */
817 g_warning ("gdk_color_context_get_pixels: oops! no colors available, "
818 "your images will look *really* ugly.");
823 #ifdef G_ENABLE_DEBUG
827 /* initialize pixels */
829 for (i = 0; i < cmapsize; i++)
832 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
835 /* read the colormap */
837 my_x_query_colors (cc->colormap, cmap, cmapsize);
839 /* get a close match for any unallocated colors */
847 gint d, j, mdist, close, ri, gi, bi;
855 /* Store these vals. Small performance increase as this skips three
856 * indexing operations in the loop code.
863 /* Walk all colors in the colormap and see which one is the
864 * closest. Uses plain least squares.
867 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
869 /* Don't replace these by shifts; the sign may get clobbered */
871 rd = (ri - cmap[j].red) / 256;
872 gd = (gi - cmap[j].green) / 256;
873 bd = (bi - cmap[j].blue) / 256;
875 d = rd * rd + gd * gd + bd * bd;
886 rd = cmap[close].red;
887 gd = cmap[close].green;
888 bd = cmap[close].blue;
892 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
898 defs[i] = cmap[close];
899 defs[i].pixel = colors[i];
900 allocated[ncols++] = colors[i];
901 #ifdef G_ENABLE_DEBUG
908 /* deal with in next stage if allocation failed */
910 while (++idx < counter);
914 /* This is the maximum no. of allocated colors. See also the nopen == 0
918 if ((ncols == ncolors) || (nopen == 0))
920 GDK_NOTE (COLOR_CONTEXT,
921 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact and "
922 "%i close (%i colors allocated so far)\n",
923 ncolors, exact_col, close_col, cc->num_allocated));
928 /* Now map any remaining unallocated pixels into the colors we did get */
934 gint d, mdist, close, ri, gi, bi;
948 /* search allocated colors */
950 for (j = 0; (j < ncols) && (mdist != 0); j++)
954 /* Don't replace these by shifts; the sign may get clobbered */
956 rd = (ri - defs[k].red) / 256;
957 gd = (gi - defs[k].green) / 256;
958 bd = (bi - defs[k].blue) / 256;
960 d = rd * rd + gd * gd + bd * bd;
971 /* too bad, map to black */
973 defs[i].pixel = cc->black_pixel;
974 defs[i].red = defs[i].green = defs[i].blue = 0;
975 #ifdef G_ENABLE_DEBUG
981 defs[i] = defs[close];
982 #ifdef G_ENABLE_DEBUG
987 colors[i] = defs[i].pixel;
989 while (++idx < nopen);
991 GDK_NOTE (COLOR_CONTEXT,
992 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact, %i close, "
993 "%i substituted, %i to black (%i colors allocated so far)\n",
994 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
998 gdk_color_context_get_pixels_incremental (GdkColorContext *cc,
1008 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
1009 gint bad_alloc = FALSE;
1010 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
1011 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
1012 #ifdef G_ENABLE_DEBUG
1013 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
1016 g_assert (cc != NULL);
1017 g_assert (reds != NULL);
1018 g_assert (greens != NULL);
1019 g_assert (blues != NULL);
1020 g_assert (used != NULL);
1021 g_assert (colors != NULL);
1022 g_assert (nallocated != NULL);
1024 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
1025 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
1026 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
1028 /* Will only have a value if used by the progressive image loader */
1030 ncols = *nallocated;
1034 /* First allocate all pixels */
1036 for (i = 0; i < ncolors; i++)
1038 /* used[i] is only -1 if the pixel at that location hasn't
1039 * been allocated yet. This is a sanity check required for proper
1040 * color allocation by the progressive image loader.
1041 * When colors[i] == 0 it indicates the slot is available for
1045 if (used[i] != FALSE)
1049 defs[i].red = reds[i];
1050 defs[i].green = greens[i];
1051 defs[i].blue = blues[i];
1053 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i], &bad_alloc);
1055 /* successfully allocated, store it */
1059 defs[i].pixel = colors[i];
1060 allocated[ncols++] = colors[i];
1063 failed[nopen++] = i;
1067 GDK_NOTE (COLOR_CONTEXT,
1068 g_message ("gdk_color_context_get_pixels_incremental: "
1069 "pixel at slot %i already allocated, skipping\n", i));
1074 *nallocated = ncols;
1076 if ((ncols == ncolors) || (nopen == 0))
1078 GDK_NOTE (COLOR_CONTEXT,
1079 g_message ("gdk_color_context_get_pixels_incremental: got all %i colors "
1080 "(%i colors allocated so far)\n",
1081 ncolors, cc->num_allocated));
1086 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
1090 g_warning ("gdk_color_context_get_pixels_incremental: oops! "
1091 "No colors available images will look *really* ugly.");
1095 #ifdef G_ENABLE_DEBUG
1099 /* initialize pixels */
1101 for (i = 0; i < cmapsize; i++)
1104 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
1109 my_x_query_colors (cc->colormap, cmap, cmapsize);
1111 /* now match any unallocated colors */
1119 gint d, j, mdist, close, ri, gi, bi;
1133 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
1135 /* Don't replace these by shifts; the sign may get clobbered */
1137 rd = (ri - cmap[j].red) / 256;
1138 gd = (gi - cmap[j].green) / 256;
1139 bd = (bi - cmap[j].blue) / 256;
1141 d = rd * rd + gd * gd + bd * bd;
1152 rd = cmap[close].red;
1153 gd = cmap[close].green;
1154 bd = cmap[close].blue;
1158 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1164 defs[i] = cmap[close];
1165 defs[i].pixel = colors[i];
1166 allocated[ncols++] = colors[i];
1167 #ifdef G_ENABLE_DEBUG
1172 failed[nopen++] = i;
1175 failed[nopen++] = i;
1176 /* deal with in next stage if allocation failed */
1178 while (++idx < counter);
1180 *nallocated = ncols;
1182 if ((ncols == ncolors) || (nopen == 0))
1184 GDK_NOTE (COLOR_CONTEXT,
1185 g_message ("gdk_color_context_get_pixels_incremental: "
1186 "got %i colors, %i exact and %i close "
1187 "(%i colors allocated so far)\n",
1188 ncolors, exact_col, close_col, cc->num_allocated));
1193 /* map remaining unallocated pixels into colors we did get */
1199 gint d, mdist, close, ri, gi, bi;
1211 /* search allocated colors */
1213 for (j = 0; (j < ncols) && (mdist != 0); j++)
1218 /* Don't replace these by shifts; the sign may get clobbered */
1220 rd = (ri - defs[k].red) / 256;
1221 gd = (gi - defs[k].green) / 256;
1222 bd = (bi - defs[k].blue) / 256;
1224 d = rd * rd + gd * gd + bd * bd;
1235 /* too bad, map to black */
1237 defs[i].pixel = cc->black_pixel;
1238 defs[i].red = defs[i].green = defs[i].blue = 0;
1239 #ifdef G_ENABLE_DEBUG
1245 defs[i] = defs[close];
1246 #ifdef G_ENABLE_DEBUG
1251 colors[i] = defs[i].pixel;
1253 while (++idx < nopen);
1255 GDK_NOTE (COLOR_CONTEXT,
1256 g_message ("gdk_color_context_get_pixels_incremental: "
1257 "got %i colors, %i exact, %i close, %i substituted, %i to black "
1258 "(%i colors allocated so far)\n",
1259 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1263 gdk_color_context_query_color (GdkColorContext *cc,
1266 return gdk_color_context_query_colors (cc, color, 1);
1270 gdk_color_context_query_colors (GdkColorContext *cc,
1277 g_assert (cc != NULL);
1278 g_assert (colors != NULL);
1282 case GDK_CC_MODE_BW:
1283 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1285 if (tc->pixel == cc->white_pixel)
1286 tc->red = tc->green = tc->blue = 65535;
1288 tc->red = tc->green = tc->blue = 0;
1292 case GDK_CC_MODE_TRUE:
1293 if (cc->clut == NULL)
1294 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1296 tc->red = ((tc->pixel & cc->masks.red) >> cc->shifts.red) << (16 - cc->bits.red);
1297 tc->green = ((tc->pixel & cc->masks.green) >> cc->shifts.green) << (16 - cc->bits.green);
1298 tc->blue = ((tc->pixel & cc->masks.blue) >> cc->shifts.blue) << (16 - cc->bits.blue);
1302 my_x_query_colors (cc->colormap, colors, num_colors);
1307 case GDK_CC_MODE_STD_CMAP:
1309 if (cc->cmap == NULL)
1311 my_x_query_colors (cc->colormap, colors, num_colors);
1316 gint first, last, half;
1319 for (i = 0, tc = colors; i < num_colors; i++)
1322 last = cc->num_colors - 1;
1324 while (first <= last)
1326 half = (first + last) / 2;
1327 half_pixel = cc->cmap[half].pixel;
1329 if (tc->pixel == half_pixel)
1331 tc->red = cc->cmap[half].red;
1332 tc->green = cc->cmap[half].green;
1333 tc->blue = cc->cmap[half].blue;
1334 first = last + 1; /* false break */
1338 if (tc->pixel > half_pixel)
1353 gdk_color_context_add_palette (GdkColorContext *cc,
1361 g_assert (cc != NULL);
1363 /* initialize this palette (will also erase previous palette as well) */
1367 /* restore previous mode if we aren't adding a new palette */
1369 if (num_palette == 0)
1372 /* copy incoming palette */
1374 cc->palette = g_new0(GdkColor, num_palette);
1378 for (i = 0; i < num_palette; i++)
1383 /* try to allocate this color */
1386 g = palette[i].green;
1387 b = palette[i].blue;
1389 gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1391 /* only store if we succeed */
1395 /* store in palette */
1397 cc->palette[j].red = r;
1398 cc->palette[j].green = g;
1399 cc->palette[j].blue = b;
1400 cc->palette[j].pixel = pixel[0];
1402 /* move to next slot */
1410 if (j != num_palette)
1411 cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1413 /* clear the hash table, we don't use it when dithering */
1417 g_hash_table_foreach (cc->color_hash,
1420 g_hash_table_destroy (cc->color_hash);
1421 cc->color_hash = NULL;
1424 /* store real palette size */
1426 cc->num_palette = j;
1428 /* switch to palette mode */
1430 cc->mode = GDK_CC_MODE_PALETTE;
1434 qsort (cc->palette, cc->num_palette, sizeof (GdkColor), pixel_sort);
1436 cc->fast_dither = NULL;
1442 gdk_color_context_init_dither (GdkColorContext *cc)
1444 gint rr, gg, bb, err, erg, erb;
1445 gint success = FALSE;
1447 g_assert (cc != NULL);
1449 /* now we can initialize the fast dither matrix */
1451 if (cc->fast_dither == NULL)
1452 cc->fast_dither = g_new (GdkColorContextDither, 1);
1454 /* Fill it. We ignore unsuccessful allocations, they are just mapped
1455 * to black instead */
1457 for (rr = 0; rr < 32; rr++)
1458 for (gg = 0; gg < 32; gg++)
1459 for (bb = 0; bb < 32; bb++)
1461 err = (rr << 3) | (rr >> 2);
1462 erg = (gg << 3) | (gg >> 2);
1463 erb = (bb << 3) | (bb >> 2);
1465 cc->fast_dither->fast_rgb[rr][gg][bb] =
1466 gdk_color_context_get_index_from_palette (cc, &err, &erg, &erb, &success);
1467 cc->fast_dither->fast_err[rr][gg][bb] = err;
1468 cc->fast_dither->fast_erg[rr][gg][bb] = erg;
1469 cc->fast_dither->fast_erb[rr][gg][bb] = erb;
1474 gdk_color_context_free_dither (GdkColorContext *cc)
1476 g_assert (cc != NULL);
1478 if (cc->fast_dither)
1479 g_free (cc->fast_dither);
1481 cc->fast_dither = NULL;
1485 gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
1492 gint dif, dr, dg, db, j = -1;
1493 gint mindif = 0x7fffffff;
1494 gint err = 0, erg = 0, erb = 0;
1497 g_assert (cc != NULL);
1498 g_assert (red != NULL);
1499 g_assert (green != NULL);
1500 g_assert (blue != NULL);
1501 g_assert (failed != NULL);
1505 for (i = 0; i < cc->num_palette; i++)
1507 dr = *red - cc->palette[i].red;
1508 dg = *green - cc->palette[i].green;
1509 db = *blue - cc->palette[i].blue;
1511 dif = dr * dr + dg * dg + db * db;
1517 pixel = cc->palette[i].pixel;
1527 /* we failed to map onto a color */
1542 gdk_color_context_get_index_from_palette (GdkColorContext *cc,
1548 gint dif, dr, dg, db, j = -1;
1549 gint mindif = 0x7fffffff;
1550 gint err = 0, erg = 0, erb = 0;
1553 g_assert (cc != NULL);
1554 g_assert (red != NULL);
1555 g_assert (green != NULL);
1556 g_assert (blue != NULL);
1557 g_assert (failed != NULL);
1561 for (i = 0; i < cc->num_palette; i++)
1563 dr = *red - cc->palette[i].red;
1564 dg = *green - cc->palette[i].green;
1565 db = *blue - cc->palette[i].blue;
1567 dif = dr * dr + dg * dg + db * db;
1582 /* we failed to map onto a color */
1591 /* return error fractions */