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/.
67 #include "gdkprivate.h"
71 #define MAX_IMAGE_COLORS 256
75 hash_color (gconstpointer key)
77 const GdkColor *color = key;
79 return (color->red * 33023 + color->green * 30013 + color->blue * 27011);
83 compare_colors (gconstpointer a,
86 const GdkColor *aa = a;
87 const GdkColor *bb = b;
89 return ((aa->red == bb->red) && (aa->green == bb->green) && (aa->blue == bb->blue));
93 free_hash_entry (gpointer key,
97 g_free (key); /* key and value are the same GdkColor */
101 pixel_sort (const void *a, const void *b)
103 return ((GdkColor *) a)->pixel - ((GdkColor *) b)->pixel;
106 /* XXX: This function does an XQueryColors() the hard way, because there is
107 * no corresponding function in Gdk.
111 my_x_query_colors (GdkColormap *colormap,
118 xcolors = g_new (XColor, ncolors);
119 for (i = 0; i < ncolors; i++)
120 xcolors[i].pixel = colors[i].pixel;
122 XQueryColors (gdk_display, GDK_COLORMAP_XCOLORMAP (colormap), xcolors, ncolors);
124 for (i = 0; i < ncolors; i++)
126 colors[i].red = xcolors[i].red;
127 colors[i].green = xcolors[i].green;
128 colors[i].blue = xcolors[i].blue;
135 query_colors (GdkColorContext *cc)
138 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
139 cc->cmap = g_new (GdkColor, cc->num_colors);
141 for (i = 0; i < cc->num_colors; i++)
142 cc->cmap[i].pixel = cc->clut ? cc->clut[i] : ccp->std_cmap.base_pixel + i;
144 my_x_query_colors (cc->colormap, cc->cmap, cc->num_colors);
146 qsort (cc->cmap, cc->num_colors, sizeof (GdkColor), pixel_sort);
150 init_bw (GdkColorContext *cc)
154 g_warning ("init_bw: failed to allocate colors, falling back to black and white");
156 cc->mode = GDK_CC_MODE_BW;
158 color.red = color.green = color.blue = 0;
160 if (!gdk_color_alloc (cc->colormap, &color))
163 cc->black_pixel = color.pixel;
165 color.red = color.green = color.blue = 0xffff;
167 if (!gdk_color_alloc (cc->colormap, &color))
168 cc->white_pixel = cc->black_pixel ? 0 : 1;
170 cc->white_pixel = color.pixel;
176 init_gray (GdkColorContext *cc)
178 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
179 GdkColor *clrs, *cstart;
183 cc->num_colors = GDK_VISUAL_XVISUAL (cc->visual)->map_entries;
185 cc->clut = g_new (gulong, cc->num_colors);
186 cstart = g_new (GdkColor, cc->num_colors);
190 dinc = 65535.0 / (cc->num_colors - 1);
194 for (i = 0; i < cc->num_colors; i++)
196 clrs->red = clrs->green = clrs->blue = dinc * i;
198 if (!gdk_color_alloc (cc->colormap, clrs))
200 gdk_colors_free (cc->colormap, cc->clut, i, 0);
204 if (cc->num_colors > 1)
216 cc->clut[i] = clrs++->pixel;
221 /* XXX: is this the right thing to do? */
222 ccp->std_cmap.colormap = GDK_COLORMAP_XCOLORMAP (cc->colormap);
223 ccp->std_cmap.base_pixel = 0;
224 ccp->std_cmap.red_max = cc->num_colors - 1;
225 ccp->std_cmap.green_max = 0;
226 ccp->std_cmap.blue_max = 0;
227 ccp->std_cmap.red_mult = 1;
228 ccp->std_cmap.green_mult = 0;
229 ccp->std_cmap.blue_mult = 0;
231 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
232 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
236 cc->mode = GDK_CC_MODE_MY_GRAY;
240 init_color (GdkColorContext *cc)
242 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
246 while ((cubeval * cubeval * cubeval) < GDK_VISUAL_XVISUAL (cc->visual)->map_entries)
250 cc->num_colors = cubeval * cubeval * cubeval;
252 ccp->std_cmap.red_max = cubeval - 1;
253 ccp->std_cmap.green_max = cubeval - 1;
254 ccp->std_cmap.blue_max = cubeval - 1;
255 ccp->std_cmap.red_mult = cubeval * cubeval;
256 ccp->std_cmap.green_mult = cubeval;
257 ccp->std_cmap.blue_mult = 1;
258 ccp->std_cmap.base_pixel = 0;
260 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
261 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
262 cc->num_colors = DisplayCells (ccp->xdisplay, gdk_screen);
264 /* a CLUT for storing allocated pixel indices */
266 cc->max_colors = cc->num_colors;
267 cc->clut = g_new (gulong, cc->max_colors);
269 for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
270 cc->clut[cubeval] = cubeval;
274 cc->mode = GDK_CC_MODE_STD_CMAP;
279 init_true_color (GdkColorContext *cc)
281 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
282 gulong rmask, gmask, bmask;
284 cc->mode = GDK_CC_MODE_TRUE;
288 rmask = cc->masks.red = cc->visual->red_mask;
307 gmask = cc->masks.green = cc->visual->green_mask;
309 cc->shifts.green = 0;
326 bmask = cc->masks.blue = cc->visual->blue_mask;
343 cc->num_colors = (cc->visual->red_mask | cc->visual->green_mask | cc->visual->blue_mask) + 1;
344 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
345 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
349 init_direct_color (GdkColorContext *cc)
352 GdkColor *clrs, *cstart;
353 gulong rval, gval, bval;
359 init_true_color (cc); /* for shift stuff */
361 rval = cc->visual->red_mask >> cc->shifts.red;
362 gval = cc->visual->green_mask >> cc->shifts.green;
363 bval = cc->visual->blue_mask >> cc->shifts.blue;
365 rtable = g_new (gulong, rval + 1);
366 gtable = g_new (gulong, gval + 1);
367 btable = g_new (gulong, bval + 1);
369 cc->max_entry = MAX (rval, gval);
370 cc->max_entry = MAX (cc->max_entry, bval);
372 cstart = g_new (GdkColor, cc->max_entry + 1);
373 cc->clut = g_new (gulong, cc->max_entry + 1);
377 for (n = 0; n < rval; n++)
378 rtable[n] = rval ? (65535.0 / rval * n) : 0;
380 for (n = 0; n < gval; n++)
381 gtable[n] = gval ? (65535.0 / gval * n) : 0;
383 for (n = 0; n < bval; n++)
384 btable[n] = bval ? (65535.0 / bval * n) : 0;
386 cc->max_entry = MAX (rval, gval);
387 cc->max_entry = MAX (cc->max_entry, bval);
391 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
393 for (n = 0; n < cc->max_entry; n++)
395 dinc = (double) n / cc->max_entry;
397 clrs->red = rtable[(int) (dinc * rval)];
398 clrs->green = gtable[(int) (dinc * gval)];
399 clrs->blue = btable[(int) (dinc * bval)];
401 if (gdk_color_alloc (cc->colormap, clrs))
403 cc->clut[count++] = clrs->pixel;
408 gdk_colors_free (cc->colormap, cc->clut, count, 0);
414 cc->masks.red = (cc->masks.red >> 1) & cc->visual->red_mask;
415 cc->masks.green = (cc->masks.green >> 1) & cc->visual->green_mask;
416 cc->masks.blue = (cc->masks.blue >> 1) & cc->visual->blue_mask;
426 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
428 if (cc->num_colors >1)
440 /* Update allocated color count; original num_colors is max_entry, which
441 * is not necessarily the same as the really allocated number of colors.
444 cc->num_colors = count;
453 init_palette (GdkColorContext *cc)
455 /* restore correct mode for this cc */
457 switch (cc->visual->type)
459 case GDK_VISUAL_STATIC_GRAY:
460 case GDK_VISUAL_GRAYSCALE:
461 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
462 cc->mode = GDK_CC_MODE_BW;
464 cc->mode = GDK_CC_MODE_MY_GRAY;
467 case GDK_VISUAL_TRUE_COLOR:
468 case GDK_VISUAL_DIRECT_COLOR:
469 cc->mode = GDK_CC_MODE_TRUE;
472 case GDK_VISUAL_STATIC_COLOR:
473 case GDK_VISUAL_PSEUDO_COLOR:
474 cc->mode = GDK_CC_MODE_STD_CMAP;
478 cc->mode = GDK_CC_MODE_UNDEFINED;
482 /* previous palette */
485 g_free (cc->palette);
488 g_free (cc->fast_dither);
490 /* clear hash table if present */
494 g_hash_table_foreach (cc->color_hash,
497 g_hash_table_destroy (cc->color_hash);
498 cc->color_hash = NULL;
503 cc->fast_dither = NULL;
507 gdk_color_context_new (GdkVisual *visual,
508 GdkColormap *colormap)
510 GdkColorContextPrivate *ccp;
511 gint use_private_colormap = FALSE; /* XXX: maybe restore full functionality later? */
514 GdkColormap *default_colormap;
516 g_assert (visual != NULL);
517 g_assert (colormap != NULL);
519 ccp = g_new (GdkColorContextPrivate, 1);
520 cc = (GdkColorContext *) ccp;
521 ccp->xdisplay = gdk_display;
523 cc->colormap = colormap;
526 cc->mode = GDK_CC_MODE_UNDEFINED;
527 cc->need_to_free_colormap = FALSE;
529 cc->color_hash = NULL;
532 cc->fast_dither = NULL;
534 default_colormap = gdk_colormap_get_system ();
538 while (retry_count < 2)
540 /* Only create a private colormap if the visual found isn't equal
541 * to the default visual and we don't have a private colormap,
542 * -or- if we are instructed to create a private colormap (which
543 * never is the case for XmHTML).
546 if (use_private_colormap
547 || ((cc->visual != gdk_visual_get_system ()) /* default visual? */
548 && (GDK_COLORMAP_XCOLORMAP (colormap) == GDK_COLORMAP_XCOLORMAP (default_colormap))))
550 g_warning ("gdk_color_context_new: non-default visual detected, "
551 "using private colormap");
553 cc->colormap = gdk_colormap_new (cc->visual, FALSE);
555 cc->need_to_free_colormap = (GDK_COLORMAP_XCOLORMAP (colormap)
556 != GDK_COLORMAP_XCOLORMAP (default_colormap));
559 switch (visual->type)
561 case GDK_VISUAL_STATIC_GRAY:
562 case GDK_VISUAL_GRAYSCALE:
563 GDK_NOTE (COLOR_CONTEXT,
564 g_message ("gdk_color_context_new: visual class is %s\n",
565 (visual->type == GDK_VISUAL_STATIC_GRAY) ?
566 "GDK_VISUAL_STATIC_GRAY" :
567 "GDK_VISUAL_GRAYSCALE"));
569 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
576 case GDK_VISUAL_TRUE_COLOR: /* shifts */
577 GDK_NOTE (COLOR_CONTEXT,
578 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
580 init_true_color (cc);
583 case GDK_VISUAL_DIRECT_COLOR: /* shifts and fake CLUT */
584 GDK_NOTE (COLOR_CONTEXT,
585 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_DIRECT_COLOR\n"));
587 init_direct_color (cc);
590 case GDK_VISUAL_STATIC_COLOR:
591 case GDK_VISUAL_PSEUDO_COLOR:
592 GDK_NOTE (COLOR_CONTEXT,
593 g_message ("gdk_color_context_new: visual class is %s\n",
594 (visual->type == GDK_VISUAL_STATIC_COLOR) ?
595 "GDK_VISUAL_STATIC_COLOR" :
596 "GDK_VISUAL_PSEUDO_COLOR"));
602 g_assert_not_reached ();
605 if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
607 use_private_colormap = TRUE;
614 /* no. of colors allocated yet */
616 cc->num_allocated = 0;
618 GDK_NOTE (COLOR_CONTEXT,
619 g_message ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
620 cc->visual->depth, cc->num_colors));
622 return (GdkColorContext *) cc;
626 gdk_color_context_new_mono (GdkVisual *visual,
627 GdkColormap *colormap)
629 GdkColorContextPrivate *ccp;
632 g_assert (visual != NULL);
633 g_assert (colormap != NULL);
635 cc = g_new (GdkColorContext, 1);
636 ccp = (GdkColorContextPrivate *) cc;
637 ccp->xdisplay = gdk_display;
639 cc->colormap = colormap;
642 cc->mode = GDK_CC_MODE_UNDEFINED;
643 cc->need_to_free_colormap = FALSE;
647 return (GdkColorContext *) cc;
650 /* This doesn't currently free black/white, hmm... */
653 gdk_color_context_free (GdkColorContext *cc)
655 g_assert (cc != NULL);
657 if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
658 || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
660 gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
663 else if (cc->clut != NULL)
665 gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
669 if (cc->cmap != NULL)
672 if (cc->need_to_free_colormap)
673 gdk_colormap_unref (cc->colormap);
675 /* free any palette that has been associated with this GdkColorContext */
683 gdk_color_context_get_pixel (GdkColorContext *cc,
689 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
690 g_assert (cc != NULL);
691 g_assert (failed != NULL);
701 value = (red / 65535.0 * 0.30
702 + green / 65535.0 * 0.59
703 + blue / 65535.0 * 0.11);
706 return cc->white_pixel;
708 return cc->black_pixel;
711 case GDK_CC_MODE_MY_GRAY:
713 gulong ired, igreen, iblue;
715 red = red * 0.30 + green * 0.59 + blue * 0.11;
719 if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
720 ired = ccp->std_cmap.red_max;
722 ired *= ccp->std_cmap.red_mult;
724 if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
725 igreen = ccp->std_cmap.green_max;
727 igreen *= ccp->std_cmap.green_mult;
729 if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
730 iblue = ccp->std_cmap.blue_max;
732 iblue *= ccp->std_cmap.blue_mult;
734 if (cc->clut != NULL)
735 return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
737 return ccp->std_cmap.base_pixel + ired + igreen + iblue;
740 case GDK_CC_MODE_TRUE:
742 gulong ired, igreen, iblue;
744 if (cc->clut == NULL)
746 red >>= 16 - cc->bits.red;
747 green >>= 16 - cc->bits.green;
748 blue >>= 16 - cc->bits.blue;
750 ired = (red << cc->shifts.red) & cc->masks.red;
751 igreen = (green << cc->shifts.green) & cc->masks.green;
752 iblue = (blue << cc->shifts.blue) & cc->masks.blue;
754 return ired | igreen | iblue;
757 ired = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
758 igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
759 iblue = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
761 return ired | igreen | iblue;
764 case GDK_CC_MODE_PALETTE:
765 return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
767 case GDK_CC_MODE_STD_CMAP:
771 GdkColor *result = NULL;
778 result = g_hash_table_lookup (cc->color_hash, &color);
787 if (!gdk_color_alloc (cc->colormap, &color))
793 /* XXX: the following comment comes directly from
794 * XCC.c. I don't know if it is relevant for
795 * gdk_color_alloc() as it is for XAllocColor()
799 * I can't figure this out entirely, but it *is* possible
800 * that XAllocColor succeeds, even if the number of
801 * allocations we've made exceeds the number of available
802 * colors in the current colormap. And therefore it
803 * might be necessary for us to resize the CLUT.
806 if (cc->num_allocated == cc->max_colors)
810 GDK_NOTE (COLOR_CONTEXT,
811 g_message ("gdk_color_context_get_pixel: "
812 "resizing CLUT to %i entries\n",
815 cc->clut = g_realloc (cc->clut,
816 cc->max_colors * sizeof (gulong));
819 /* Key and value are the same color structure */
821 cnew = g_new (GdkColor, 1);
825 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
826 g_hash_table_insert (cc->color_hash, cnew, cnew);
828 cc->clut[cc->num_allocated] = color.pixel;
834 return result->pixel;
840 gdk_color_context_get_pixels (GdkColorContext *cc,
849 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
850 gint bad_alloc = FALSE;
851 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
852 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
853 #ifdef G_ENABLE_DEBUG
854 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
856 g_assert (cc != NULL);
857 g_assert (reds != NULL);
858 g_assert (greens != NULL);
859 g_assert (blues != NULL);
860 g_assert (colors != NULL);
861 g_assert (nallocated != NULL);
863 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
864 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
865 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
867 /* Will only have a value if used by the progressive image loader */
873 /* First allocate all pixels */
875 for (i = 0; i < ncolors; i++)
877 /* colors[i] is only zero if the pixel at that location hasn't
878 * been allocated yet. This is a sanity check required for proper
879 * color allocation by the progressive image loader
884 defs[i].red = reds[i];
885 defs[i].green = greens[i];
886 defs[i].blue = blues[i];
888 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i],
891 /* successfully allocated, store it */
895 defs[i].pixel = colors[i];
896 allocated[ncols++] = colors[i];
905 /* all colors available, all done */
907 if ((ncols == ncolors) || (nopen == 0))
909 GDK_NOTE (COLOR_CONTEXT,
910 g_message ("gdk_color_context_get_pixels: got all %i colors; "
911 "(%i colors allocated so far)\n", ncolors, cc->num_allocated));
916 /* The fun part. We now try to allocate the colors we couldn't allocate
917 * directly. The first step will map a color onto its nearest color
918 * that has been allocated (either by us or someone else). If any colors
919 * remain unallocated, we map these onto the colors that we have allocated
923 /* read up to MAX_IMAGE_COLORS colors of the current colormap */
925 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
927 /* see if the colormap has any colors to read */
931 g_warning ("gdk_color_context_get_pixels: oops! no colors available, "
932 "your images will look *really* ugly.");
937 #ifdef G_ENABLE_DEBUG
941 /* initialize pixels */
943 for (i = 0; i < cmapsize; i++)
946 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
949 /* read the colormap */
951 my_x_query_colors (cc->colormap, cmap, cmapsize);
953 /* get a close match for any unallocated colors */
961 gint d, j, mdist, close, ri, gi, bi;
969 /* Store these vals. Small performance increase as this skips three
970 * indexing operations in the loop code.
977 /* Walk all colors in the colormap and see which one is the
978 * closest. Uses plain least squares.
981 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
983 /* Don't replace these by shifts; the sign may get clobbered */
985 rd = (ri - cmap[j].red) / 256;
986 gd = (gi - cmap[j].green) / 256;
987 bd = (bi - cmap[j].blue) / 256;
989 d = rd * rd + gd * gd + bd * bd;
1000 rd = cmap[close].red;
1001 gd = cmap[close].green;
1002 bd = cmap[close].blue;
1006 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1012 defs[i] = cmap[close];
1013 defs[i].pixel = colors[i];
1014 allocated[ncols++] = colors[i];
1015 #ifdef G_ENABLE_DEBUG
1019 failed[nopen++] = i;
1021 failed[nopen++] = i;
1022 /* deal with in next stage if allocation failed */
1024 while (++idx < counter);
1026 *nallocated = ncols;
1028 /* This is the maximum no. of allocated colors. See also the nopen == 0
1032 if ((ncols == ncolors) || (nopen == 0))
1034 GDK_NOTE (COLOR_CONTEXT,
1035 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact and "
1036 "%i close (%i colors allocated so far)\n",
1037 ncolors, exact_col, close_col, cc->num_allocated));
1042 /* Now map any remaining unallocated pixels into the colors we did get */
1048 gint d, mdist, close, ri, gi, bi;
1062 /* search allocated colors */
1064 for (j = 0; (j < ncols) && (mdist != 0); j++)
1068 /* Don't replace these by shifts; the sign may get clobbered */
1070 rd = (ri - defs[k].red) / 256;
1071 gd = (gi - defs[k].green) / 256;
1072 bd = (bi - defs[k].blue) / 256;
1074 d = rd * rd + gd * gd + bd * bd;
1085 /* too bad, map to black */
1087 defs[i].pixel = cc->black_pixel;
1088 defs[i].red = defs[i].green = defs[i].blue = 0;
1089 #ifdef G_ENABLE_DEBUG
1095 defs[i] = defs[close];
1096 #ifdef G_ENABLE_DEBUG
1101 colors[i] = defs[i].pixel;
1103 while (++idx < nopen);
1105 GDK_NOTE (COLOR_CONTEXT,
1106 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact, %i close, "
1107 "%i substituted, %i to black (%i colors allocated so far)\n",
1108 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1112 gdk_color_context_get_pixels_incremental (GdkColorContext *cc,
1122 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
1123 gint bad_alloc = FALSE;
1124 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
1125 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
1126 #ifdef G_ENABLE_DEBUG
1127 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
1130 g_assert (cc != NULL);
1131 g_assert (reds != NULL);
1132 g_assert (greens != NULL);
1133 g_assert (blues != NULL);
1134 g_assert (used != NULL);
1135 g_assert (colors != NULL);
1136 g_assert (nallocated != NULL);
1138 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
1139 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
1140 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
1142 /* Will only have a value if used by the progressive image loader */
1144 ncols = *nallocated;
1148 /* First allocate all pixels */
1150 for (i = 0; i < ncolors; i++)
1152 /* used[i] is only -1 if the pixel at that location hasn't
1153 * been allocated yet. This is a sanity check required for proper
1154 * color allocation by the progressive image loader.
1155 * When colors[i] == 0 it indicates the slot is available for
1159 if (used[i] != FALSE)
1163 defs[i].red = reds[i];
1164 defs[i].green = greens[i];
1165 defs[i].blue = blues[i];
1167 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i], &bad_alloc);
1169 /* successfully allocated, store it */
1173 defs[i].pixel = colors[i];
1174 allocated[ncols++] = colors[i];
1177 failed[nopen++] = i;
1181 GDK_NOTE (COLOR_CONTEXT,
1182 g_message ("gdk_color_context_get_pixels_incremental: "
1183 "pixel at slot %i already allocated, skipping\n", i));
1188 *nallocated = ncols;
1190 if ((ncols == ncolors) || (nopen == 0))
1192 GDK_NOTE (COLOR_CONTEXT,
1193 g_message ("gdk_color_context_get_pixels_incremental: got all %i colors "
1194 "(%i colors allocated so far)\n",
1195 ncolors, cc->num_allocated));
1200 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
1204 g_warning ("gdk_color_context_get_pixels_incremental: oops! "
1205 "No colors available images will look *really* ugly.");
1209 #ifdef G_ENABLE_DEBUG
1213 /* initialize pixels */
1215 for (i = 0; i < cmapsize; i++)
1218 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
1223 my_x_query_colors (cc->colormap, cmap, cmapsize);
1225 /* now match any unallocated colors */
1233 gint d, j, mdist, close, ri, gi, bi;
1247 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
1249 /* Don't replace these by shifts; the sign may get clobbered */
1251 rd = (ri - cmap[j].red) / 256;
1252 gd = (gi - cmap[j].green) / 256;
1253 bd = (bi - cmap[j].blue) / 256;
1255 d = rd * rd + gd * gd + bd * bd;
1266 rd = cmap[close].red;
1267 gd = cmap[close].green;
1268 bd = cmap[close].blue;
1272 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1278 defs[i] = cmap[close];
1279 defs[i].pixel = colors[i];
1280 allocated[ncols++] = colors[i];
1281 #ifdef G_ENABLE_DEBUG
1286 failed[nopen++] = i;
1289 failed[nopen++] = i;
1290 /* deal with in next stage if allocation failed */
1292 while (++idx < counter);
1294 *nallocated = ncols;
1296 if ((ncols == ncolors) || (nopen == 0))
1298 GDK_NOTE (COLOR_CONTEXT,
1299 g_message ("gdk_color_context_get_pixels_incremental: "
1300 "got %i colors, %i exact and %i close "
1301 "(%i colors allocated so far)\n",
1302 ncolors, exact_col, close_col, cc->num_allocated));
1307 /* map remaining unallocated pixels into colors we did get */
1313 gint d, mdist, close, ri, gi, bi;
1325 /* search allocated colors */
1327 for (j = 0; (j < ncols) && (mdist != 0); j++)
1332 /* Don't replace these by shifts; the sign may get clobbered */
1334 rd = (ri - defs[k].red) / 256;
1335 gd = (gi - defs[k].green) / 256;
1336 bd = (bi - defs[k].blue) / 256;
1338 d = rd * rd + gd * gd + bd * bd;
1349 /* too bad, map to black */
1351 defs[i].pixel = cc->black_pixel;
1352 defs[i].red = defs[i].green = defs[i].blue = 0;
1353 #ifdef G_ENABLE_DEBUG
1359 defs[i] = defs[close];
1360 #ifdef G_ENABLE_DEBUG
1365 colors[i] = defs[i].pixel;
1367 while (++idx < nopen);
1369 GDK_NOTE (COLOR_CONTEXT,
1370 g_message ("gdk_color_context_get_pixels_incremental: "
1371 "got %i colors, %i exact, %i close, %i substituted, %i to black "
1372 "(%i colors allocated so far)\n",
1373 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1377 gdk_color_context_query_color (GdkColorContext *cc,
1380 return gdk_color_context_query_colors (cc, color, 1);
1384 gdk_color_context_query_colors (GdkColorContext *cc,
1391 g_assert (cc != NULL);
1392 g_assert (colors != NULL);
1396 case GDK_CC_MODE_BW:
1397 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1399 if (tc->pixel == cc->white_pixel)
1400 tc->red = tc->green = tc->blue = 65535;
1402 tc->red = tc->green = tc->blue = 0;
1406 case GDK_CC_MODE_TRUE:
1407 if (cc->clut == NULL)
1408 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1410 tc->red = ((tc->pixel & cc->masks.red) >> cc->shifts.red) << (16 - cc->bits.red);
1411 tc->green = ((tc->pixel & cc->masks.green) >> cc->shifts.green) << (16 - cc->bits.green);
1412 tc->blue = ((tc->pixel & cc->masks.blue) >> cc->shifts.blue) << (16 - cc->bits.blue);
1416 my_x_query_colors (cc->colormap, colors, num_colors);
1421 case GDK_CC_MODE_STD_CMAP:
1423 if (cc->cmap == NULL)
1425 my_x_query_colors (cc->colormap, colors, num_colors);
1430 gint first, last, half;
1433 for (i = 0, tc = colors; i < num_colors; i++)
1436 last = cc->num_colors - 1;
1438 while (first <= last)
1440 half = (first + last) / 2;
1441 half_pixel = cc->cmap[half].pixel;
1443 if (tc->pixel == half_pixel)
1445 tc->red = cc->cmap[half].red;
1446 tc->green = cc->cmap[half].green;
1447 tc->blue = cc->cmap[half].blue;
1448 first = last + 1; /* false break */
1452 if (tc->pixel > half_pixel)
1467 gdk_color_context_add_palette (GdkColorContext *cc,
1475 g_assert (cc != NULL);
1477 /* initialize this palette (will also erase previous palette as well) */
1481 /* restore previous mode if we aren't adding a new palette */
1483 if (num_palette == 0)
1486 /* copy incoming palette */
1488 cc->palette = g_new0(GdkColor, num_palette);
1492 for (i = 0; i < num_palette; i++)
1497 /* try to allocate this color */
1500 g = palette[i].green;
1501 b = palette[i].blue;
1503 gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1505 /* only store if we succeed */
1509 /* store in palette */
1511 cc->palette[j].red = r;
1512 cc->palette[j].green = g;
1513 cc->palette[j].blue = b;
1514 cc->palette[j].pixel = pixel[0];
1516 /* move to next slot */
1524 if (j != num_palette)
1525 cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1527 /* clear the hash table, we don't use it when dithering */
1531 g_hash_table_foreach (cc->color_hash,
1534 g_hash_table_destroy (cc->color_hash);
1535 cc->color_hash = NULL;
1538 /* store real palette size */
1540 cc->num_palette = j;
1542 /* switch to palette mode */
1544 cc->mode = GDK_CC_MODE_PALETTE;
1548 qsort (cc->palette, cc->num_palette, sizeof (GdkColor), pixel_sort);
1550 cc->fast_dither = NULL;
1556 gdk_color_context_init_dither (GdkColorContext *cc)
1558 gint rr, gg, bb, err, erg, erb;
1559 gint success = FALSE;
1561 g_assert (cc != NULL);
1563 /* now we can initialize the fast dither matrix */
1565 if (cc->fast_dither == NULL)
1566 cc->fast_dither = g_new (GdkColorContextDither, 1);
1568 /* Fill it. We ignore unsuccessful allocations, they are just mapped
1569 * to black instead */
1571 for (rr = 0; rr < 32; rr++)
1572 for (gg = 0; gg < 32; gg++)
1573 for (bb = 0; bb < 32; bb++)
1575 err = (rr << 3) | (rr >> 2);
1576 erg = (gg << 3) | (gg >> 2);
1577 erb = (bb << 3) | (bb >> 2);
1579 cc->fast_dither->fast_rgb[rr][gg][bb] =
1580 gdk_color_context_get_index_from_palette (cc, &err, &erg, &erb, &success);
1581 cc->fast_dither->fast_err[rr][gg][bb] = err;
1582 cc->fast_dither->fast_erg[rr][gg][bb] = erg;
1583 cc->fast_dither->fast_erb[rr][gg][bb] = erb;
1588 gdk_color_context_free_dither (GdkColorContext *cc)
1590 g_assert (cc != NULL);
1592 if (cc->fast_dither)
1593 g_free (cc->fast_dither);
1595 cc->fast_dither = NULL;
1599 gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
1606 gint dif, dr, dg, db, j = -1;
1607 gint mindif = 0x7fffffff;
1608 gint err = 0, erg = 0, erb = 0;
1611 g_assert (cc != NULL);
1612 g_assert (red != NULL);
1613 g_assert (green != NULL);
1614 g_assert (blue != NULL);
1615 g_assert (failed != NULL);
1619 for (i = 0; i < cc->num_palette; i++)
1621 dr = *red - cc->palette[i].red;
1622 dg = *green - cc->palette[i].green;
1623 db = *blue - cc->palette[i].blue;
1625 dif = dr * dr + dg * dg + db * db;
1631 pixel = cc->palette[i].pixel;
1641 /* we failed to map onto a color */
1656 gdk_color_context_get_index_from_palette (GdkColorContext *cc,
1662 gint dif, dr, dg, db, j = -1;
1663 gint mindif = 0x7fffffff;
1664 gint err = 0, erg = 0, erb = 0;
1667 g_assert (cc != NULL);
1668 g_assert (red != NULL);
1669 g_assert (green != NULL);
1670 g_assert (blue != NULL);
1671 g_assert (failed != NULL);
1675 for (i = 0; i < cc->num_palette; i++)
1677 dr = *red - cc->palette[i].red;
1678 dg = *green - cc->palette[i].green;
1679 db = *blue - cc->palette[i].blue;
1681 dif = dr * dr + dg * dg + db * db;
1696 /* we failed to map onto a color */
1705 /* return error fractions */