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 GDK_NOTE (COLOR_CONTEXT,
556 g_print ("gdk_color_context_new: visual class is %s\n",
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 GDK_NOTE (COLOR_CONTEXT,
570 g_print ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
572 init_true_color (cc);
575 case GDK_VISUAL_DIRECT_COLOR: /* shifts and fake CLUT */
576 GDK_NOTE (COLOR_CONTEXT,
577 g_print ("gdk_color_context_new: visual class is GDK_VISUAL_DIRECT_COLOR\n"));
579 init_direct_color (cc);
582 case GDK_VISUAL_STATIC_COLOR:
583 case GDK_VISUAL_PSEUDO_COLOR:
584 GDK_NOTE (COLOR_CONTEXT,
585 g_print ("gdk_color_context_new: visual class is %s\n",
586 (visual->type == GDK_VISUAL_STATIC_COLOR) ?
587 "GDK_VISUAL_STATIC_COLOR" :
588 "GDK_VISUAL_PSEUDO_COLOR"));
594 g_assert_not_reached ();
597 if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
599 use_private_colormap = TRUE;
606 /* no. of colors allocated yet */
608 cc->num_allocated = 0;
610 GDK_NOTE (COLOR_CONTEXT,
611 g_print ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
612 cc->visual->depth, cc->num_colors));
614 /* check if we need to initialize a hash table */
616 if ((cc->mode == GDK_CC_MODE_STD_CMAP) || (cc->mode == GDK_CC_MODE_UNDEFINED))
617 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
619 return (GdkColorContext *) cc;
623 gdk_color_context_new_mono (GdkVisual *visual,
624 GdkColormap *colormap)
626 GdkColorContextPrivate *ccp;
629 g_assert (visual != NULL);
630 g_assert (colormap != NULL);
632 cc = g_new (GdkColorContext, 1);
633 ccp = (GdkColorContextPrivate *) cc;
634 ccp->xdisplay = gdk_display;
636 cc->colormap = colormap;
639 cc->mode = GDK_CC_MODE_UNDEFINED;
640 cc->need_to_free_colormap = FALSE;
644 return (GdkColorContext *) cc;
647 /* This doesn't currently free black/white, hmm... */
650 gdk_color_context_free (GdkColorContext *cc)
652 g_assert (cc != NULL);
654 if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
655 || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
657 gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
660 else if (cc->clut != NULL)
662 gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
666 if (cc->cmap != NULL)
669 if (cc->need_to_free_colormap)
670 gdk_colormap_unref (cc->colormap);
672 /* free any palette that has been associated with this GdkColorContext */
678 g_hash_table_foreach (cc->color_hash,
681 g_hash_table_destroy (cc->color_hash);
688 gdk_color_context_get_pixel (GdkColorContext *cc,
694 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
695 g_assert (cc != NULL);
696 g_assert (failed != NULL);
706 value = (red / 65535.0 * 0.30
707 + green / 65535.0 * 0.59
708 + blue / 65535.0 * 0.11);
711 return cc->white_pixel;
713 return cc->black_pixel;
716 case GDK_CC_MODE_MY_GRAY:
718 gulong ired, igreen, iblue;
720 red = red * 0.30 + green * 0.59 + blue * 0.11;
724 if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
725 ired = ccp->std_cmap.red_max;
727 ired *= ccp->std_cmap.red_mult;
729 if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
730 igreen = ccp->std_cmap.green_max;
732 igreen *= ccp->std_cmap.green_mult;
734 if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
735 iblue = ccp->std_cmap.blue_max;
737 iblue *= ccp->std_cmap.blue_mult;
739 if (cc->clut != NULL)
740 return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
742 return ccp->std_cmap.base_pixel + ired + igreen + iblue;
745 case GDK_CC_MODE_TRUE:
747 gulong ired, igreen, iblue;
749 if (cc->clut == NULL)
751 red >>= 16 - cc->bits.red;
752 green >>= 16 - cc->bits.green;
753 blue >>= 16 - cc->bits.blue;
755 ired = (red << cc->shifts.red) & cc->masks.red;
756 igreen = (green << cc->shifts.green) & cc->masks.green;
757 iblue = (blue << cc->shifts.blue) & cc->masks.blue;
759 return ired | igreen | iblue;
762 ired = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
763 igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
764 iblue = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
766 return ired | igreen | iblue;
769 case GDK_CC_MODE_PALETTE:
770 return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
772 case GDK_CC_MODE_STD_CMAP:
782 result = g_hash_table_lookup (cc->color_hash, &color);
791 if (!gdk_color_alloc (cc->colormap, &color))
797 /* XXX: the following comment comes directly from
798 * XCC.c. I don't know if it is relevant for
799 * gdk_color_alloc() as it is for XAllocColor()
803 * I can't figure this out entirely, but it *is* possible
804 * that XAllocColor succeeds, even if the number of
805 * allocations we've made exceeds the number of available
806 * colors in the current colormap. And therefore it
807 * might be necessary for us to resize the CLUT.
810 if (cc->num_allocated == cc->max_colors)
814 GDK_NOTE (COLOR_CONTEXT,
815 g_print ("gdk_color_context_get_pixel: "
816 "resizing CLUT to %i entries\n",
819 cc->clut = g_realloc (cc->clut,
820 cc->max_colors * sizeof (gulong));
823 /* Key and value are the same color structure */
825 cnew = g_new (GdkColor, 1);
827 g_hash_table_insert (cc->color_hash, cnew, cnew);
829 cc->clut[cc->num_allocated] = color.pixel;
835 return result->pixel;
841 gdk_color_context_get_pixels (GdkColorContext *cc,
850 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
851 gint bad_alloc = FALSE;
852 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
853 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
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_print ("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.");
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];
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_print ("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;
1095 defs[i] = defs[close];
1101 colors[i] = defs[i].pixel;
1103 while (++idx < nopen);
1105 GDK_NOTE (COLOR_CONTEXT,
1106 g_print ("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 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
1128 g_assert (cc != NULL);
1129 g_assert (reds != NULL);
1130 g_assert (greens != NULL);
1131 g_assert (blues != NULL);
1132 g_assert (used != NULL);
1133 g_assert (colors != NULL);
1134 g_assert (nallocated != NULL);
1136 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
1137 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
1138 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
1140 /* Will only have a value if used by the progressive image loader */
1142 ncols = *nallocated;
1146 /* First allocate all pixels */
1148 for (i = 0; i < ncolors; i++)
1150 /* used[i] is only -1 if the pixel at that location hasn't
1151 * been allocated yet. This is a sanity check required for proper
1152 * color allocation by the progressive image loader.
1153 * When colors[i] == 0 it indicates the slot is available for
1157 if (used[i] != FALSE)
1161 defs[i].red = reds[i];
1162 defs[i].green = greens[i];
1163 defs[i].blue = blues[i];
1165 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i], &bad_alloc);
1167 /* successfully allocated, store it */
1171 defs[i].pixel = colors[i];
1172 allocated[ncols++] = colors[i];
1175 failed[nopen++] = i;
1179 GDK_NOTE (COLOR_CONTEXT,
1180 g_print ("gdk_color_context_get_pixels_incremental: "
1181 "pixel at slot %i already allocated, skipping\n", i));
1186 *nallocated = ncols;
1188 if ((ncols == ncolors) || (nopen == 0))
1190 GDK_NOTE (COLOR_CONTEXT,
1191 g_print ("gdk_color_context_get_pixels_incremental: got all %i colors "
1192 "(%i colors allocated so far)\n",
1193 ncolors, cc->num_allocated));
1198 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
1202 g_warning ("gdk_color_context_get_pixels_incremental: oops! "
1203 "No colors available images will look *really* ugly.");
1211 /* initialize pixels */
1213 for (i = 0; i < cmapsize; i++)
1216 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
1221 my_x_query_colors (cc->colormap, cmap, cmapsize);
1223 /* now match any unallocated colors */
1231 gint d, j, mdist, close, ri, gi, bi;
1245 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
1247 /* Don't replace these by shifts; the sign may get clobbered */
1249 rd = (ri - cmap[j].red) / 256;
1250 gd = (gi - cmap[j].green) / 256;
1251 bd = (bi - cmap[j].blue) / 256;
1253 d = rd * rd + gd * gd + bd * bd;
1264 rd = cmap[close].red;
1265 gd = cmap[close].green;
1266 bd = cmap[close].blue;
1270 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1276 defs[i] = cmap[close];
1277 defs[i].pixel = colors[i];
1278 allocated[ncols++] = colors[i];
1284 failed[nopen++] = i;
1287 failed[nopen++] = i;
1288 /* deal with in next stage if allocation failed */
1290 while (++idx < counter);
1292 *nallocated = ncols;
1294 if ((ncols == ncolors) || (nopen == 0))
1296 GDK_NOTE (COLOR_CONTEXT,
1297 g_print ("gdk_color_context_get_pixels_incremental: "
1298 "got %i colors, %i exact and %i close "
1299 "(%i colors allocated so far)\n",
1300 ncolors, exact_col, close_col, cc->num_allocated));
1305 /* map remaining unallocated pixels into colors we did get */
1311 gint d, mdist, close, ri, gi, bi;
1323 /* search allocated colors */
1325 for (j = 0; (j < ncols) && (mdist != 0); j++)
1330 /* Don't replace these by shifts; the sign may get clobbered */
1332 rd = (ri - defs[k].red) / 256;
1333 gd = (gi - defs[k].green) / 256;
1334 bd = (bi - defs[k].blue) / 256;
1336 d = rd * rd + gd * gd + bd * bd;
1347 /* too bad, map to black */
1349 defs[i].pixel = cc->black_pixel;
1350 defs[i].red = defs[i].green = defs[i].blue = 0;
1357 defs[i] = defs[close];
1363 colors[i] = defs[i].pixel;
1365 while (++idx < nopen);
1367 GDK_NOTE (COLOR_CONTEXT,
1368 g_print ("gdk_color_context_get_pixels_incremental: "
1369 "got %i colors, %i exact, %i close, %i substituted, %i to black "
1370 "(%i colors allocated so far)\n",
1371 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1375 gdk_color_context_query_color (GdkColorContext *cc,
1378 return gdk_color_context_query_colors (cc, color, 1);
1382 gdk_color_context_query_colors (GdkColorContext *cc,
1389 g_assert (cc != NULL);
1390 g_assert (colors != NULL);
1394 case GDK_CC_MODE_BW:
1395 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1397 if (tc->pixel == cc->white_pixel)
1398 tc->red = tc->green = tc->blue = 65535;
1400 tc->red = tc->green = tc->blue = 0;
1404 case GDK_CC_MODE_TRUE:
1405 if (cc->clut == NULL)
1406 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1408 tc->red = ((tc->pixel & cc->masks.red) >> cc->shifts.red) << (16 - cc->bits.red);
1409 tc->green = ((tc->pixel & cc->masks.green) >> cc->shifts.green) << (16 - cc->bits.green);
1410 tc->blue = ((tc->pixel & cc->masks.blue) >> cc->shifts.blue) << (16 - cc->bits.blue);
1414 my_x_query_colors (cc->colormap, colors, num_colors);
1419 case GDK_CC_MODE_STD_CMAP:
1421 if (cc->cmap == NULL)
1423 my_x_query_colors (cc->colormap, colors, num_colors);
1428 gint first, last, half;
1431 for (i = 0, tc = colors; i < num_colors; i++)
1434 last = cc->num_colors - 1;
1436 while (first <= last)
1438 half = (first + last) / 2;
1439 half_pixel = cc->cmap[half].pixel;
1441 if (tc->pixel == half_pixel)
1443 tc->red = cc->cmap[half].red;
1444 tc->green = cc->cmap[half].green;
1445 tc->blue = cc->cmap[half].blue;
1446 first = last + 1; /* false break */
1450 if (tc->pixel > half_pixel)
1465 gdk_color_context_add_palette (GdkColorContext *cc,
1473 g_assert (cc != NULL);
1475 /* initialize this palette (will also erase previous palette as well) */
1479 /* restore previous mode if we aren't adding a new palette */
1481 if (num_palette == 0)
1483 /* GDK_CC_MODE_STD_CMAP uses a hash table, so we'd better initialize one */
1485 /* XXX: here, the hash table is already initialized */
1490 /* Initialize a hash table for this palette (we need one for allocating
1491 * the pixels in the palette using the current settings)
1494 if (cc->color_hash == NULL)
1495 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
1497 /* copy incoming palette */
1499 cc->palette = g_new0(GdkColor, num_palette);
1503 for (i = 0; i < num_palette; i++)
1508 /* try to allocate this color */
1511 g = palette[i].green;
1512 b = palette[i].blue;
1514 gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1516 /* only store if we succeed */
1520 /* store in palette */
1522 cc->palette[j].red = r;
1523 cc->palette[j].green = g;
1524 cc->palette[j].blue = b;
1525 cc->palette[j].pixel = pixel[0];
1527 /* move to next slot */
1535 if (j != num_palette)
1536 cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1538 /* clear the hash table, we don't use it when dithering */
1542 g_hash_table_destroy (cc->color_hash);
1543 cc->color_hash = NULL;
1546 /* store real palette size */
1548 cc->num_palette = j;
1550 /* switch to palette mode */
1552 cc->mode = GDK_CC_MODE_PALETTE;
1556 qsort (cc->palette, cc->num_palette, sizeof (GdkColor), pixel_sort);
1558 cc->fast_dither = NULL;
1564 gdk_color_context_init_dither (GdkColorContext *cc)
1566 gint rr, gg, bb, err, erg, erb;
1567 gint success = FALSE;
1569 g_assert (cc != NULL);
1571 /* now we can initialize the fast dither matrix */
1573 if (cc->fast_dither == NULL)
1574 cc->fast_dither = g_new (GdkColorContextDither, 1);
1576 /* Fill it. We ignore unsuccessful allocations, they are just mapped
1577 * to black instead */
1579 for (rr = 0; rr < 32; rr++)
1580 for (gg = 0; gg < 32; gg++)
1581 for (bb = 0; bb < 32; bb++)
1583 err = (rr << 3) | (rr >> 2);
1584 erg = (gg << 3) | (gg >> 2);
1585 erb = (bb << 3) | (bb >> 2);
1587 cc->fast_dither->fast_rgb[rr][gg][bb] =
1588 gdk_color_context_get_index_from_palette (cc, &err, &erg, &erb, &success);
1589 cc->fast_dither->fast_err[rr][gg][bb] = err;
1590 cc->fast_dither->fast_erg[rr][gg][bb] = erg;
1591 cc->fast_dither->fast_erb[rr][gg][bb] = erb;
1596 gdk_color_context_free_dither (GdkColorContext *cc)
1598 g_assert (cc != NULL);
1600 if (cc->fast_dither)
1601 g_free (cc->fast_dither);
1603 cc->fast_dither = NULL;
1607 gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
1614 gint dif, dr, dg, db, j = -1;
1615 gint mindif = 0x7fffffff;
1616 gint err = 0, erg = 0, erb = 0;
1619 g_assert (cc != NULL);
1620 g_assert (red != NULL);
1621 g_assert (green != NULL);
1622 g_assert (blue != NULL);
1623 g_assert (failed != NULL);
1627 for (i = 0; i < cc->num_palette; i++)
1629 dr = *red - cc->palette[i].red;
1630 dg = *green - cc->palette[i].green;
1631 db = *blue - cc->palette[i].blue;
1633 dif = dr * dr + dg * dg + db * db;
1639 pixel = cc->palette[i].pixel;
1649 /* we failed to map onto a color */
1664 gdk_color_context_get_index_from_palette (GdkColorContext *cc,
1670 gint dif, dr, dg, db, j = -1;
1671 gint mindif = 0x7fffffff;
1672 gint err = 0, erg = 0, erb = 0;
1675 g_assert (cc != NULL);
1676 g_assert (red != NULL);
1677 g_assert (green != NULL);
1678 g_assert (blue != NULL);
1679 g_assert (failed != NULL);
1683 for (i = 0; i < cc->num_palette; i++)
1685 dr = *red - cc->palette[i].red;
1686 dg = *green - cc->palette[i].green;
1687 db = *blue - cc->palette[i].blue;
1689 dif = dr * dr + dg * dg + db * db;
1704 /* we failed to map onto a color */
1713 /* return error fractions */