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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* Color Context module
20 * Copyright 1994,1995 John L. Cwikla
21 * Copyright (C) 1997 by Ripley Software Development
22 * Copyright (C) 1997 by Federico Mena (port to Gtk/Gdk)
25 /* Copyright 1994,1995 John L. Cwikla
27 * Permission to use, copy, modify, distribute, and sell this software
28 * and its documentation for any purpose is hereby granted without fee,
29 * provided that the above copyright notice appears in all copies and that
30 * both that copyright notice and this permission notice appear in
31 * supporting documentation, and that the name of John L. Cwikla or
32 * Wolfram Research, Inc not be used in advertising or publicity
33 * pertaining to distribution of the software without specific, written
34 * prior permission. John L. Cwikla and Wolfram Research, Inc make no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
38 * John L. Cwikla and Wolfram Research, Inc disclaim all warranties with
39 * regard to this software, including all implied warranties of
40 * merchantability and fitness, in no event shall John L. Cwikla or
41 * Wolfram Research, Inc be liable for any special, indirect or
42 * consequential damages or any damages whatsoever resulting from loss of
43 * use, data or profits, whether in an action of contract, negligence or
44 * other tortious action, arising out of or in connection with the use or
45 * performance of this software.
50 * Wolfram Research Inc.
60 #include "gdkprivate.h"
64 #define MAX_IMAGE_COLORS 256
68 hash_color (gpointer key)
70 GdkColor *color = key;
72 return (color->red * 33023 + color->green * 30013 + color->blue * 27011);
76 compare_colors (gpointer a,
82 return ((aa->red == bb->red) && (aa->green == bb->green) && (aa->blue == bb->blue));
86 free_hash_entry (gpointer key,
90 g_free (key); /* key and value are the same GdkColor */
94 pixel_sort (const void *a, const void *b)
96 return ((GdkColor *) a)->pixel - ((GdkColor *) b)->pixel;
99 /* XXX: This function does an XQueryColors() the hard way, because there is
100 * no corresponding function in Gdk.
104 my_x_query_colors (GdkColormap *colormap,
111 xcolors = g_new (XColor, ncolors);
112 for (i = 0; i < ncolors; i++)
113 xcolors[i].pixel = colors[i].pixel;
115 XQueryColors (gdk_display, GDK_COLORMAP_XCOLORMAP (colormap), xcolors, ncolors);
117 for (i = 0; i < ncolors; i++)
119 colors[i].red = xcolors[i].red;
120 colors[i].green = xcolors[i].green;
121 colors[i].blue = xcolors[i].blue;
128 query_colors (GdkColorContext *cc)
131 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
132 cc->cmap = g_new (GdkColor, cc->num_colors);
134 for (i = 0; i < cc->num_colors; i++)
135 cc->cmap[i].pixel = cc->clut ? cc->clut[i] : ccp->std_cmap.base_pixel + i;
137 my_x_query_colors (cc->colormap, cc->cmap, cc->num_colors);
139 qsort (cc->cmap, cc->num_colors, sizeof (GdkColor), pixel_sort);
143 init_bw (GdkColorContext *cc)
147 g_warning ("init_bw: failed to allocate colors, falling back to black and white");
149 cc->mode = GDK_CC_MODE_BW;
151 color.red = color.green = color.blue = 0;
153 if (!gdk_color_alloc (cc->colormap, &color))
156 cc->black_pixel = color.pixel;
158 color.red = color.green = color.blue = 0xffff;
160 if (!gdk_color_alloc (cc->colormap, &color))
161 cc->white_pixel = cc->black_pixel ? 0 : 1;
163 cc->white_pixel = color.pixel;
169 init_gray (GdkColorContext *cc)
171 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
172 GdkColor *clrs, *cstart;
176 cc->num_colors = GDK_VISUAL_XVISUAL (cc->visual)->map_entries;
178 cc->clut = g_new (gulong, cc->num_colors);
179 cstart = g_new (GdkColor, cc->num_colors);
183 dinc = 65535.0 / (cc->num_colors - 1);
187 for (i = 0; i < cc->num_colors; i++)
189 clrs->red = clrs->green = clrs->blue = dinc * i;
191 if (!gdk_color_alloc (cc->colormap, clrs))
193 gdk_colors_free (cc->colormap, cc->clut, i, 0);
197 if (cc->num_colors > 1)
209 cc->clut[i] = clrs++->pixel;
214 /* XXX: is this the right thing to do? */
215 ccp->std_cmap.colormap = GDK_COLORMAP_XCOLORMAP (cc->colormap);
216 ccp->std_cmap.base_pixel = 0;
217 ccp->std_cmap.red_max = cc->num_colors - 1;
218 ccp->std_cmap.green_max = 0;
219 ccp->std_cmap.blue_max = 0;
220 ccp->std_cmap.red_mult = 1;
221 ccp->std_cmap.green_mult = 0;
222 ccp->std_cmap.blue_mult = 0;
224 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
225 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
229 cc->mode = GDK_CC_MODE_MY_GRAY;
233 init_color (GdkColorContext *cc)
235 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
239 while ((cubeval * cubeval * cubeval) < GDK_VISUAL_XVISUAL (cc->visual)->map_entries)
243 cc->num_colors = cubeval * cubeval * cubeval;
245 ccp->std_cmap.red_max = cubeval - 1;
246 ccp->std_cmap.green_max = cubeval - 1;
247 ccp->std_cmap.blue_max = cubeval - 1;
248 ccp->std_cmap.red_mult = cubeval * cubeval;
249 ccp->std_cmap.green_mult = cubeval;
250 ccp->std_cmap.blue_mult = 1;
251 ccp->std_cmap.base_pixel = 0;
253 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
254 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
255 cc->num_colors = DisplayCells (ccp->xdisplay, gdk_screen);
257 /* a CLUT for storing allocated pixel indices */
259 cc->max_colors = cc->num_colors;
260 cc->clut = g_new (gulong, cc->max_colors);
262 for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
263 cc->clut[cubeval] = cubeval;
267 cc->mode = GDK_CC_MODE_STD_CMAP;
272 init_true_color (GdkColorContext *cc)
274 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
275 gulong rmask, gmask, bmask;
277 cc->mode = GDK_CC_MODE_TRUE;
281 rmask = cc->masks.red = cc->visual->red_mask;
300 gmask = cc->masks.green = cc->visual->green_mask;
302 cc->shifts.green = 0;
319 bmask = cc->masks.blue = cc->visual->blue_mask;
336 cc->num_colors = (cc->visual->red_mask | cc->visual->green_mask | cc->visual->blue_mask) + 1;
337 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
338 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
342 init_direct_color (GdkColorContext *cc)
345 GdkColor *clrs, *cstart;
346 gulong rval, gval, bval;
352 init_true_color (cc); /* for shift stuff */
354 rval = cc->visual->red_mask >> cc->shifts.red;
355 gval = cc->visual->green_mask >> cc->shifts.green;
356 bval = cc->visual->blue_mask >> cc->shifts.blue;
358 rtable = g_new (gulong, rval + 1);
359 gtable = g_new (gulong, gval + 1);
360 btable = g_new (gulong, bval + 1);
362 cc->max_entry = MAX (rval, gval);
363 cc->max_entry = MAX (cc->max_entry, bval);
365 cstart = g_new (GdkColor, cc->max_entry + 1);
366 cc->clut = g_new (gulong, cc->max_entry + 1);
370 for (n = 0; n < rval; n++)
371 rtable[n] = rval ? (65535.0 / rval * n) : 0;
373 for (n = 0; n < gval; n++)
374 gtable[n] = gval ? (65535.0 / gval * n) : 0;
376 for (n = 0; n < bval; n++)
377 btable[n] = bval ? (65535.0 / bval * n) : 0;
379 cc->max_entry = MAX (rval, gval);
380 cc->max_entry = MAX (cc->max_entry, bval);
384 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
386 for (n = 0; n < cc->max_entry; n++)
388 dinc = (double) n / cc->max_entry;
390 clrs->red = rtable[(int) (dinc * rval)];
391 clrs->green = gtable[(int) (dinc * gval)];
392 clrs->blue = btable[(int) (dinc * bval)];
394 if (gdk_color_alloc (cc->colormap, clrs))
396 cc->clut[count++] = clrs->pixel;
401 gdk_colors_free (cc->colormap, cc->clut, count, 0);
407 cc->masks.red = (cc->masks.red >> 1) & cc->visual->red_mask;
408 cc->masks.green = (cc->masks.green >> 1) & cc->visual->green_mask;
409 cc->masks.blue = (cc->masks.blue >> 1) & cc->visual->blue_mask;
419 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
421 if (cc->num_colors >1)
433 /* Update allocated color count; original num_colors is max_entry, which
434 * is not necessarily the same as the really allocated number of colors.
437 cc->num_colors = count;
446 init_palette (GdkColorContext *cc)
448 /* restore correct mode for this cc */
450 switch (cc->visual->type)
452 case GDK_VISUAL_STATIC_GRAY:
453 case GDK_VISUAL_GRAYSCALE:
454 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
455 cc->mode = GDK_CC_MODE_BW;
457 cc->mode = GDK_CC_MODE_MY_GRAY;
460 case GDK_VISUAL_TRUE_COLOR:
461 case GDK_VISUAL_DIRECT_COLOR:
462 cc->mode = GDK_CC_MODE_TRUE;
465 case GDK_VISUAL_STATIC_COLOR:
466 case GDK_VISUAL_PSEUDO_COLOR:
467 cc->mode = GDK_CC_MODE_STD_CMAP;
471 cc->mode = GDK_CC_MODE_UNDEFINED;
475 /* previous palette */
478 g_free (cc->palette);
481 g_free (cc->fast_dither);
483 /* clear hash table if present */
487 /* XXX: quick-and-dirty way to remove everything */
489 g_hash_table_destroy (cc->color_hash);
490 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
495 cc->fast_dither = NULL;
499 gdk_color_context_new (GdkVisual *visual,
500 GdkColormap *colormap)
502 GdkColorContextPrivate *ccp;
503 gint use_private_colormap = FALSE; /* XXX: maybe restore full functionality later? */
506 GdkColormap *default_colormap;
508 g_assert (visual != NULL);
509 g_assert (colormap != NULL);
511 ccp = g_new (GdkColorContextPrivate, 1);
512 cc = (GdkColorContext *) ccp;
513 ccp->xdisplay = gdk_display;
515 cc->colormap = colormap;
518 cc->mode = GDK_CC_MODE_UNDEFINED;
519 cc->need_to_free_colormap = FALSE;
521 cc->color_hash = NULL;
524 cc->fast_dither = NULL;
526 default_colormap = gdk_colormap_get_system ();
530 while (retry_count < 2)
532 /* Only create a private colormap if the visual found isn't equal
533 * to the default visual and we don't have a private colormap,
534 * -or- if we are instructed to create a private colormap (which
535 * never is the case for XmHTML).
538 if (use_private_colormap
539 || ((cc->visual != gdk_visual_get_system ()) /* default visual? */
540 && (GDK_COLORMAP_XCOLORMAP (colormap) == GDK_COLORMAP_XCOLORMAP (default_colormap))))
542 g_warning ("gdk_color_context_new: non-default visual detected, "
543 "using private colormap");
545 cc->colormap = gdk_colormap_new (cc->visual, FALSE);
547 cc->need_to_free_colormap = (GDK_COLORMAP_XCOLORMAP (colormap)
548 != GDK_COLORMAP_XCOLORMAP (default_colormap));
551 switch (visual->type)
553 case GDK_VISUAL_STATIC_GRAY:
554 case GDK_VISUAL_GRAYSCALE:
555 if (gdk_debug_level >= 1)
556 g_print ("gdk_color_context_new: visual class is %s",
557 (visual->type == GDK_VISUAL_STATIC_GRAY) ?
558 "GDK_VISUAL_STATIC_GRAY" :
559 "GDK_VISUAL_GRAYSCALE");
561 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
568 case GDK_VISUAL_TRUE_COLOR: /* shifts */
569 if (gdk_debug_level >= 1)
570 g_print ("gdk_color_context_new: visual class is "
571 "GDK_VISUAL_TRUE_COLOR");
573 init_true_color (cc);
576 case GDK_VISUAL_DIRECT_COLOR: /* shifts and fake CLUT */
577 if (gdk_debug_level >= 1)
578 g_print ("gdk_color_context_new: visual class is "
579 "GDK_VISUAL_DIRECT_COLOR");
581 init_direct_color (cc);
584 case GDK_VISUAL_STATIC_COLOR:
585 case GDK_VISUAL_PSEUDO_COLOR:
586 if (gdk_debug_level >= 1)
587 g_print ("gdk_color_context_new: visual class is %s",
588 (visual->type == GDK_VISUAL_STATIC_COLOR) ?
589 "GDK_VISUAL_STATIC_COLOR" :
590 "GDK_VISUAL_PSEUDO_COLOR");
596 g_assert_not_reached ();
599 if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
601 use_private_colormap = TRUE;
608 /* no. of colors allocated yet */
610 cc->num_allocated = 0;
612 if (gdk_debug_level >= 1)
613 g_print ("gdk_color_context_new: screen depth is %i, no. of colors is %i",
614 cc->visual->depth, cc->num_colors);
616 /* check if we need to initialize a hash table */
618 if ((cc->mode == GDK_CC_MODE_STD_CMAP) || (cc->mode == GDK_CC_MODE_UNDEFINED))
619 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
621 return (GdkColorContext *) cc;
625 gdk_color_context_new_mono (GdkVisual *visual,
626 GdkColormap *colormap)
628 GdkColorContextPrivate *ccp;
631 g_assert (visual != NULL);
632 g_assert (colormap != NULL);
634 cc = g_new (GdkColorContext, 1);
635 ccp = (GdkColorContextPrivate *) cc;
636 ccp->xdisplay = gdk_display;
638 cc->colormap = colormap;
641 cc->mode = GDK_CC_MODE_UNDEFINED;
642 cc->need_to_free_colormap = FALSE;
646 return (GdkColorContext *) cc;
649 /* This doesn't currently free black/white, hmm... */
652 gdk_color_context_free (GdkColorContext *cc)
654 g_assert (cc != NULL);
656 if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
657 || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
659 gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
662 else if (cc->clut != NULL)
664 gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
668 if (cc->cmap != NULL)
671 if (cc->need_to_free_colormap)
672 gdk_colormap_unref (cc->colormap);
674 /* free any palette that has been associated with this GdkColorContext */
680 g_hash_table_foreach (cc->color_hash,
683 g_hash_table_destroy (cc->color_hash);
690 gdk_color_context_get_pixel (GdkColorContext *cc,
696 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
697 g_assert (cc != NULL);
698 g_assert (failed != NULL);
708 value = (red / 65535.0 * 0.30
709 + green / 65535.0 * 0.59
710 + blue / 65535.0 * 0.11);
713 return cc->white_pixel;
715 return cc->black_pixel;
718 case GDK_CC_MODE_MY_GRAY:
720 gulong ired, igreen, iblue;
722 red = red * 0.30 + green * 0.59 + blue * 0.11;
726 if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
727 ired = ccp->std_cmap.red_max;
729 ired *= ccp->std_cmap.red_mult;
731 if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
732 igreen = ccp->std_cmap.green_max;
734 igreen *= ccp->std_cmap.green_mult;
736 if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
737 iblue = ccp->std_cmap.blue_max;
739 iblue *= ccp->std_cmap.blue_mult;
741 if (cc->clut != NULL)
742 return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
744 return ccp->std_cmap.base_pixel + ired + igreen + iblue;
747 case GDK_CC_MODE_TRUE:
749 gulong ired, igreen, iblue;
751 if (cc->clut == NULL)
753 red >>= 16 - cc->bits.red;
754 green >>= 16 - cc->bits.green;
755 blue >>= 16 - cc->bits.blue;
757 ired = (red << cc->shifts.red) & cc->masks.red;
758 igreen = (green << cc->shifts.green) & cc->masks.green;
759 iblue = (blue << cc->shifts.blue) & cc->masks.blue;
761 return ired | igreen | iblue;
764 ired = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
765 igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
766 iblue = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
768 return ired | igreen | iblue;
771 case GDK_CC_MODE_PALETTE:
772 return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
774 case GDK_CC_MODE_STD_CMAP:
784 result = g_hash_table_lookup (cc->color_hash, &color);
793 if (!gdk_color_alloc (cc->colormap, &color))
799 /* XXX: the following comment comes directly from
800 * XCC.c. I don't know if it is relevant for
801 * gdk_color_alloc() as it is for XAllocColor()
805 * I can't figure this out entirely, but it *is* possible
806 * that XAllocColor succeeds, even if the number of
807 * allocations we've made exceeds the number of available
808 * colors in the current colormap. And therefore it
809 * might be necessary for us to resize the CLUT.
812 if (cc->num_allocated == cc->max_colors)
816 if (gdk_debug_level >= 1)
817 g_print ("gdk_color_context_get_pixel: "
818 "resizing CLUT to %i entries",
821 cc->clut = g_realloc (cc->clut,
822 cc->max_colors * sizeof (gulong));
825 /* Key and value are the same color structure */
827 cnew = g_new (GdkColor, 1);
829 g_hash_table_insert (cc->color_hash, cnew, cnew);
831 cc->clut[cc->num_allocated] = color.pixel;
837 return result->pixel;
843 gdk_color_context_get_pixels (GdkColorContext *cc,
852 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
853 gint bad_alloc = FALSE;
854 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
855 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
856 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
858 g_assert (cc != NULL);
859 g_assert (reds != NULL);
860 g_assert (greens != NULL);
861 g_assert (blues != NULL);
862 g_assert (colors != NULL);
863 g_assert (nallocated != NULL);
865 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
866 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
867 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
869 /* Will only have a value if used by the progressive image loader */
875 /* First allocate all pixels */
877 for (i = 0; i < ncolors; i++)
879 /* colors[i] is only zero if the pixel at that location hasn't
880 * been allocated yet. This is a sanity check required for proper
881 * color allocation by the progressive image loader
886 defs[i].red = reds[i];
887 defs[i].green = greens[i];
888 defs[i].blue = blues[i];
890 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i],
893 /* successfully allocated, store it */
897 defs[i].pixel = colors[i];
898 allocated[ncols++] = colors[i];
907 /* all colors available, all done */
909 if ((ncols == ncolors) || (nopen == 0))
911 if (gdk_debug_level >= 1)
912 g_print ("gdk_color_context_get_pixels: got all %i colors; "
913 "(%i colors allocated so far", ncolors, cc->num_allocated);
918 /* The fun part. We now try to allocate the colors we couldn't allocate
919 * directly. The first step will map a color onto its nearest color
920 * that has been allocated (either by us or someone else). If any colors
921 * remain unallocated, we map these onto the colors that we have allocated
925 /* read up to MAX_IMAGE_COLORS colors of the current colormap */
927 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
929 /* see if the colormap has any colors to read */
933 g_warning ("gdk_color_context_get_pixels: oops! no colors available, "
934 "your images will look *really* ugly.");
943 /* initialize pixels */
945 for (i = 0; i < cmapsize; i++)
948 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
951 /* read the colormap */
953 my_x_query_colors (cc->colormap, cmap, cmapsize);
955 /* get a close match for any unallocated colors */
963 gint d, j, mdist, close, ri, gi, bi;
971 /* Store these vals. Small performance increase as this skips three
972 * indexing operations in the loop code.
979 /* Walk all colors in the colormap and see which one is the
980 * closest. Uses plain least squares.
983 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
985 /* Don't replace these by shifts; the sign may get clobbered */
987 rd = (ri - cmap[j].red) / 256;
988 gd = (gi - cmap[j].green) / 256;
989 bd = (bi - cmap[j].blue) / 256;
991 d = rd * rd + gd * gd + bd * bd;
1002 rd = cmap[close].red;
1003 gd = cmap[close].green;
1004 bd = cmap[close].blue;
1008 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1014 defs[i] = cmap[close];
1015 defs[i].pixel = colors[i];
1016 allocated[ncols++] = colors[i];
1021 failed[nopen++] = i;
1023 failed[nopen++] = i;
1024 /* deal with in next stage if allocation failed */
1026 while (++idx < counter);
1028 *nallocated = ncols;
1030 /* This is the maximum no. of allocated colors. See also the nopen == 0
1034 if ((ncols == ncolors) || (nopen == 0))
1036 if (gdk_debug_level >= 1)
1037 g_print ("gdk_color_context_get_pixels: got %i colors, %i exact and "
1038 "%i close (%i colors allocated so far)",
1039 ncolors, exact_col, close_col, cc->num_allocated);
1044 /* Now map any remaining unallocated pixels into the colors we did get */
1050 gint d, mdist, close, ri, gi, bi;
1064 /* search allocated colors */
1066 for (j = 0; (j < ncols) && (mdist != 0); j++)
1070 /* Don't replace these by shifts; the sign may get clobbered */
1072 rd = (ri - defs[k].red) / 256;
1073 gd = (gi - defs[k].green) / 256;
1074 bd = (bi - defs[k].blue) / 256;
1076 d = rd * rd + gd * gd + bd * bd;
1087 /* too bad, map to black */
1089 defs[i].pixel = cc->black_pixel;
1090 defs[i].red = defs[i].green = defs[i].blue = 0;
1097 defs[i] = defs[close];
1103 colors[i] = defs[i].pixel;
1105 while (++idx < nopen);
1107 if (gdk_debug_level >= 1)
1108 g_print ("gdk_color_context_get_pixels: got %i colors, %i exact, %i close, "
1109 "%i substituted, %i to black (%i colors allocated so far)",
1110 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated);
1114 gdk_color_context_get_pixels_incremental (GdkColorContext *cc,
1124 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
1125 gint bad_alloc = FALSE;
1126 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
1127 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
1128 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 if (gdk_debug_level >= 1)
1182 g_print ("gdk_color_context_get_pixels_incremental: "
1183 "pixel at slot %i already allocated, skipping", i);
1188 *nallocated = ncols;
1190 if ((ncols == ncolors) || (nopen == 0))
1192 if (gdk_debug_level >= 1)
1193 g_print ("gdk_color_context_get_pixels_incremental: got all %i colors "
1194 "(%i colors allocated so far)",
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.");
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];
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 if (gdk_debug_level >= 1)
1299 g_print ("gdk_color_context_get_pixels_incremental: "
1300 "got %i colors, %i exact and %i close "
1301 "(%i colors allocated so far)",
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;
1359 defs[i] = defs[close];
1365 colors[i] = defs[i].pixel;
1367 while (++idx < nopen);
1369 if (gdk_debug_level >= 1)
1370 g_print ("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)",
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) * 65535 / cc->masks.red;
1411 tc->green = (tc->pixel & cc->masks.green) * 65535 / cc->masks.green;
1412 tc->blue = (tc->pixel & cc->masks.blue) * 65535 / cc->masks.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)
1485 /* GDK_CC_MODE_STD_CMAP uses a hash table, so we'd better initialize one */
1487 /* XXX: here, the hash table is already initialized */
1492 /* Initialize a hash table for this palette (we need one for allocating
1493 * the pixels in the palette using the current settings)
1496 if (cc->color_hash == NULL)
1497 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
1499 /* copy incoming palette */
1501 cc->palette = g_new0(GdkColor, num_palette);
1505 for (i = 0; i < num_palette; i++)
1510 /* try to allocate this color */
1513 g = palette[i].green;
1514 b = palette[i].blue;
1516 gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1518 /* only store if we succeed */
1522 /* store in palette */
1524 cc->palette[j].red = r;
1525 cc->palette[j].green = g;
1526 cc->palette[j].blue = b;
1527 cc->palette[j].pixel = pixel[0];
1529 /* move to next slot */
1537 if (j != num_palette)
1538 cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1540 /* clear the hash table, we don't use it when dithering */
1544 g_hash_table_destroy (cc->color_hash);
1545 cc->color_hash = NULL;
1548 /* store real palette size */
1550 cc->num_palette = j;
1552 /* switch to palette mode */
1554 cc->mode = GDK_CC_MODE_PALETTE;
1558 qsort (cc->palette, cc->num_palette, sizeof (GdkColor), pixel_sort);
1560 cc->fast_dither = NULL;
1566 gdk_color_context_init_dither (GdkColorContext *cc)
1568 gint rr, gg, bb, err, erg, erb;
1569 gint success = FALSE;
1571 g_assert (cc != NULL);
1573 /* now we can initialize the fast dither matrix */
1575 if (cc->fast_dither == NULL)
1576 cc->fast_dither = g_new (GdkColorContextDither, 1);
1578 /* Fill it. We ignore unsuccessful allocations, they are just mapped
1579 * to black instead */
1581 for (rr = 0; rr < 32; rr++)
1582 for (gg = 0; gg < 32; gg++)
1583 for (bb = 0; bb < 32; bb++)
1585 err = (rr << 3) | (rr >> 2);
1586 erg = (gg << 3) | (gg >> 2);
1587 erb = (bb << 3) | (bb >> 2);
1589 cc->fast_dither->fast_rgb[rr][gg][bb] =
1590 gdk_color_context_get_index_from_palette (cc, &err, &erg, &erb, &success);
1591 cc->fast_dither->fast_err[rr][gg][bb] = err;
1592 cc->fast_dither->fast_erg[rr][gg][bb] = erg;
1593 cc->fast_dither->fast_erb[rr][gg][bb] = erb;
1598 gdk_color_context_free_dither (GdkColorContext *cc)
1600 g_assert (cc != NULL);
1602 if (cc->fast_dither)
1603 g_free (cc->fast_dither);
1605 cc->fast_dither = NULL;
1609 gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
1616 gint dif, dr, dg, db, j = -1;
1617 gint mindif = 0x7fffffff;
1618 gint err = 0, erg = 0, erb = 0;
1621 g_assert (cc != NULL);
1622 g_assert (red != NULL);
1623 g_assert (green != NULL);
1624 g_assert (blue != NULL);
1625 g_assert (failed != NULL);
1629 for (i = 0; i < cc->num_palette; i++)
1631 dr = *red - cc->palette[i].red;
1632 dg = *green - cc->palette[i].green;
1633 db = *blue - cc->palette[i].blue;
1635 dif = dr * dr + dg * dg + db * db;
1641 pixel = cc->palette[i].pixel;
1651 /* we failed to map onto a color */
1666 gdk_color_context_get_index_from_palette (GdkColorContext *cc,
1672 gint dif, dr, dg, db, j = -1;
1673 gint mindif = 0x7fffffff;
1674 gint err = 0, erg = 0, erb = 0;
1677 g_assert (cc != NULL);
1678 g_assert (red != NULL);
1679 g_assert (green != NULL);
1680 g_assert (blue != NULL);
1681 g_assert (failed != NULL);
1685 for (i = 0; i < cc->num_palette; i++)
1687 dr = *red - cc->palette[i].red;
1688 dg = *green - cc->palette[i].green;
1689 db = *blue - cc->palette[i].blue;
1691 dif = dr * dr + dg * dg + db * db;
1706 /* we failed to map onto a color */
1715 /* return error fractions */