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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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-2000. 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/.
64 #include <X11/Xutil.h>
71 #include "gdkinternals.h"
73 #define MAX_IMAGE_COLORS 256
75 typedef struct _GdkColorContextPrivate GdkColorContextPrivate;
77 struct _GdkColorContextPrivate
79 GdkColorContext color_context;
81 XStandardColormap std_cmap;
85 hash_color (gconstpointer key)
87 const GdkColor *color = key;
89 return (color->red * 33023 + color->green * 30013 + color->blue * 27011);
93 compare_colors (gconstpointer a,
96 const GdkColor *aa = a;
97 const GdkColor *bb = b;
99 return ((aa->red == bb->red) && (aa->green == bb->green) && (aa->blue == bb->blue));
103 free_hash_entry (gpointer key,
107 g_free (key); /* key and value are the same GdkColor */
111 pixel_sort (const void *a, const void *b)
113 return ((GdkColor *) a)->pixel - ((GdkColor *) b)->pixel;
116 /* XXX: This function does an XQueryColors() the hard way, because there is
117 * no corresponding function in Gdk.
121 my_x_query_colors (GdkColormap *colormap,
128 xcolors = g_new (XColor, ncolors);
129 for (i = 0; i < ncolors; i++)
130 xcolors[i].pixel = colors[i].pixel;
132 XQueryColors (gdk_display, GDK_COLORMAP_XCOLORMAP (colormap), xcolors, ncolors);
134 for (i = 0; i < ncolors; i++)
136 colors[i].red = xcolors[i].red;
137 colors[i].green = xcolors[i].green;
138 colors[i].blue = xcolors[i].blue;
145 query_colors (GdkColorContext *cc)
148 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
149 cc->cmap = g_new (GdkColor, cc->num_colors);
151 for (i = 0; i < cc->num_colors; i++)
152 cc->cmap[i].pixel = cc->clut ? cc->clut[i] : ccp->std_cmap.base_pixel + i;
154 my_x_query_colors (cc->colormap, cc->cmap, cc->num_colors);
156 qsort (cc->cmap, cc->num_colors, sizeof (GdkColor), pixel_sort);
160 init_bw (GdkColorContext *cc)
164 g_warning ("init_bw: failed to allocate colors, falling back to black and white");
166 cc->mode = GDK_CC_MODE_BW;
168 color.red = color.green = color.blue = 0;
170 if (!gdk_color_alloc (cc->colormap, &color))
173 cc->black_pixel = color.pixel;
175 color.red = color.green = color.blue = 0xffff;
177 if (!gdk_color_alloc (cc->colormap, &color))
178 cc->white_pixel = cc->black_pixel ? 0 : 1;
180 cc->white_pixel = color.pixel;
186 init_gray (GdkColorContext *cc)
188 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
189 GdkColor *clrs, *cstart;
193 cc->num_colors = GDK_VISUAL_XVISUAL (cc->visual)->map_entries;
195 cc->clut = g_new (gulong, cc->num_colors);
196 cstart = g_new (GdkColor, cc->num_colors);
200 dinc = 65535.0 / (cc->num_colors - 1);
204 for (i = 0; i < cc->num_colors; i++)
206 clrs->red = clrs->green = clrs->blue = dinc * i;
208 if (!gdk_color_alloc (cc->colormap, clrs))
210 gdk_colors_free (cc->colormap, cc->clut, i, 0);
214 if (cc->num_colors > 1)
226 cc->clut[i] = clrs++->pixel;
231 /* XXX: is this the right thing to do? */
232 ccp->std_cmap.colormap = GDK_COLORMAP_XCOLORMAP (cc->colormap);
233 ccp->std_cmap.base_pixel = 0;
234 ccp->std_cmap.red_max = cc->num_colors - 1;
235 ccp->std_cmap.green_max = 0;
236 ccp->std_cmap.blue_max = 0;
237 ccp->std_cmap.red_mult = 1;
238 ccp->std_cmap.green_mult = 0;
239 ccp->std_cmap.blue_mult = 0;
241 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
242 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
246 cc->mode = GDK_CC_MODE_MY_GRAY;
250 init_color (GdkColorContext *cc)
252 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
256 while ((cubeval * cubeval * cubeval) < GDK_VISUAL_XVISUAL (cc->visual)->map_entries)
260 cc->num_colors = cubeval * cubeval * cubeval;
262 ccp->std_cmap.red_max = cubeval - 1;
263 ccp->std_cmap.green_max = cubeval - 1;
264 ccp->std_cmap.blue_max = cubeval - 1;
265 ccp->std_cmap.red_mult = cubeval * cubeval;
266 ccp->std_cmap.green_mult = cubeval;
267 ccp->std_cmap.blue_mult = 1;
268 ccp->std_cmap.base_pixel = 0;
270 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
271 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
272 cc->num_colors = DisplayCells (ccp->xdisplay, gdk_screen);
274 /* a CLUT for storing allocated pixel indices */
276 cc->max_colors = cc->num_colors;
277 cc->clut = g_new (gulong, cc->max_colors);
279 for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
280 cc->clut[cubeval] = cubeval;
284 cc->mode = GDK_CC_MODE_STD_CMAP;
289 init_true_color (GdkColorContext *cc)
291 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
292 gulong rmask, gmask, bmask;
294 cc->mode = GDK_CC_MODE_TRUE;
298 rmask = cc->masks.red = cc->visual->red_mask;
317 gmask = cc->masks.green = cc->visual->green_mask;
319 cc->shifts.green = 0;
336 bmask = cc->masks.blue = cc->visual->blue_mask;
353 cc->num_colors = (cc->visual->red_mask | cc->visual->green_mask | cc->visual->blue_mask) + 1;
354 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
355 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
359 init_direct_color (GdkColorContext *cc)
362 GdkColor *clrs, *cstart;
363 gulong rval, gval, bval;
369 init_true_color (cc); /* for shift stuff */
371 rval = cc->visual->red_mask >> cc->shifts.red;
372 gval = cc->visual->green_mask >> cc->shifts.green;
373 bval = cc->visual->blue_mask >> cc->shifts.blue;
375 rtable = g_new (gulong, rval + 1);
376 gtable = g_new (gulong, gval + 1);
377 btable = g_new (gulong, bval + 1);
379 cc->max_entry = MAX (rval, gval);
380 cc->max_entry = MAX (cc->max_entry, bval);
382 cstart = g_new (GdkColor, cc->max_entry + 1);
383 cc->clut = g_new (gulong, cc->max_entry + 1);
387 for (n = 0; n < rval; n++)
388 rtable[n] = rval ? (65535.0 / rval * n) : 0;
390 for (n = 0; n < gval; n++)
391 gtable[n] = gval ? (65535.0 / gval * n) : 0;
393 for (n = 0; n < bval; n++)
394 btable[n] = bval ? (65535.0 / bval * n) : 0;
396 cc->max_entry = MAX (rval, gval);
397 cc->max_entry = MAX (cc->max_entry, bval);
401 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
403 for (n = 0; n < cc->max_entry; n++)
405 dinc = (double) n / cc->max_entry;
407 clrs->red = rtable[(int) (dinc * rval)];
408 clrs->green = gtable[(int) (dinc * gval)];
409 clrs->blue = btable[(int) (dinc * bval)];
411 if (gdk_color_alloc (cc->colormap, clrs))
413 cc->clut[count++] = clrs->pixel;
418 gdk_colors_free (cc->colormap, cc->clut, count, 0);
424 cc->masks.red = (cc->masks.red >> 1) & cc->visual->red_mask;
425 cc->masks.green = (cc->masks.green >> 1) & cc->visual->green_mask;
426 cc->masks.blue = (cc->masks.blue >> 1) & cc->visual->blue_mask;
436 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
438 if (cc->num_colors >1)
450 /* Update allocated color count; original num_colors is max_entry, which
451 * is not necessarily the same as the really allocated number of colors.
454 cc->num_colors = count;
463 init_palette (GdkColorContext *cc)
465 /* restore correct mode for this cc */
467 switch (cc->visual->type)
469 case GDK_VISUAL_STATIC_GRAY:
470 case GDK_VISUAL_GRAYSCALE:
471 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
472 cc->mode = GDK_CC_MODE_BW;
474 cc->mode = GDK_CC_MODE_MY_GRAY;
477 case GDK_VISUAL_TRUE_COLOR:
478 case GDK_VISUAL_DIRECT_COLOR:
479 cc->mode = GDK_CC_MODE_TRUE;
482 case GDK_VISUAL_STATIC_COLOR:
483 case GDK_VISUAL_PSEUDO_COLOR:
484 cc->mode = GDK_CC_MODE_STD_CMAP;
488 cc->mode = GDK_CC_MODE_UNDEFINED;
492 /* previous palette */
495 g_free (cc->palette);
498 g_free (cc->fast_dither);
500 /* clear hash table if present */
504 g_hash_table_foreach (cc->color_hash,
507 g_hash_table_destroy (cc->color_hash);
508 cc->color_hash = NULL;
513 cc->fast_dither = NULL;
517 gdk_color_context_new (GdkVisual *visual,
518 GdkColormap *colormap)
520 GdkColorContextPrivate *ccp;
521 gint use_private_colormap = FALSE; /* XXX: maybe restore full functionality later? */
524 GdkColormap *default_colormap;
526 g_assert (visual != NULL);
527 g_assert (colormap != NULL);
529 ccp = g_new (GdkColorContextPrivate, 1);
530 cc = (GdkColorContext *) ccp;
531 ccp->xdisplay = gdk_display;
533 cc->colormap = colormap;
536 cc->mode = GDK_CC_MODE_UNDEFINED;
537 cc->need_to_free_colormap = FALSE;
539 cc->color_hash = NULL;
542 cc->fast_dither = NULL;
544 default_colormap = gdk_colormap_get_system ();
548 while (retry_count < 2)
550 /* Only create a private colormap if the visual found isn't equal
551 * to the default visual and we don't have a private colormap,
552 * -or- if we are instructed to create a private colormap (which
553 * never is the case for XmHTML).
556 if (use_private_colormap
557 || ((cc->visual != gdk_visual_get_system ()) /* default visual? */
558 && (GDK_COLORMAP_XCOLORMAP (colormap) == GDK_COLORMAP_XCOLORMAP (default_colormap))))
560 g_warning ("gdk_color_context_new: non-default visual detected, "
561 "using private colormap");
563 cc->colormap = gdk_colormap_new (cc->visual, FALSE);
565 cc->need_to_free_colormap = (GDK_COLORMAP_XCOLORMAP (colormap)
566 != GDK_COLORMAP_XCOLORMAP (default_colormap));
569 switch (visual->type)
571 case GDK_VISUAL_STATIC_GRAY:
572 case GDK_VISUAL_GRAYSCALE:
573 GDK_NOTE (COLOR_CONTEXT,
574 g_message ("gdk_color_context_new: visual class is %s\n",
575 (visual->type == GDK_VISUAL_STATIC_GRAY) ?
576 "GDK_VISUAL_STATIC_GRAY" :
577 "GDK_VISUAL_GRAYSCALE"));
579 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
586 case GDK_VISUAL_TRUE_COLOR: /* shifts */
587 GDK_NOTE (COLOR_CONTEXT,
588 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
590 init_true_color (cc);
593 case GDK_VISUAL_DIRECT_COLOR: /* shifts and fake CLUT */
594 GDK_NOTE (COLOR_CONTEXT,
595 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_DIRECT_COLOR\n"));
597 init_direct_color (cc);
600 case GDK_VISUAL_STATIC_COLOR:
601 case GDK_VISUAL_PSEUDO_COLOR:
602 GDK_NOTE (COLOR_CONTEXT,
603 g_message ("gdk_color_context_new: visual class is %s\n",
604 (visual->type == GDK_VISUAL_STATIC_COLOR) ?
605 "GDK_VISUAL_STATIC_COLOR" :
606 "GDK_VISUAL_PSEUDO_COLOR"));
612 g_assert_not_reached ();
615 if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
617 use_private_colormap = TRUE;
624 /* no. of colors allocated yet */
626 cc->num_allocated = 0;
628 GDK_NOTE (COLOR_CONTEXT,
629 g_message ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
630 cc->visual->depth, cc->num_colors));
632 return (GdkColorContext *) cc;
636 gdk_color_context_new_mono (GdkVisual *visual,
637 GdkColormap *colormap)
639 GdkColorContextPrivate *ccp;
642 g_assert (visual != NULL);
643 g_assert (colormap != NULL);
645 cc = g_new (GdkColorContext, 1);
646 ccp = (GdkColorContextPrivate *) cc;
647 ccp->xdisplay = gdk_display;
649 cc->colormap = colormap;
652 cc->mode = GDK_CC_MODE_UNDEFINED;
653 cc->need_to_free_colormap = FALSE;
657 return (GdkColorContext *) cc;
660 /* This doesn't currently free black/white, hmm... */
663 gdk_color_context_free (GdkColorContext *cc)
665 g_assert (cc != NULL);
667 if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
668 || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
670 gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
673 else if (cc->clut != NULL)
675 gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
679 if (cc->cmap != NULL)
682 if (cc->need_to_free_colormap)
683 gdk_colormap_unref (cc->colormap);
685 /* free any palette that has been associated with this GdkColorContext */
693 gdk_color_context_get_pixel (GdkColorContext *cc,
699 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
700 g_assert (cc != NULL);
701 g_assert (failed != NULL);
711 value = (red / 65535.0 * 0.30
712 + green / 65535.0 * 0.59
713 + blue / 65535.0 * 0.11);
716 return cc->white_pixel;
718 return cc->black_pixel;
721 case GDK_CC_MODE_MY_GRAY:
723 gulong ired, igreen, iblue;
725 red = red * 0.30 + green * 0.59 + blue * 0.11;
729 if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
730 ired = ccp->std_cmap.red_max;
732 ired *= ccp->std_cmap.red_mult;
734 if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
735 igreen = ccp->std_cmap.green_max;
737 igreen *= ccp->std_cmap.green_mult;
739 if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
740 iblue = ccp->std_cmap.blue_max;
742 iblue *= ccp->std_cmap.blue_mult;
744 if (cc->clut != NULL)
745 return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
747 return ccp->std_cmap.base_pixel + ired + igreen + iblue;
750 case GDK_CC_MODE_TRUE:
752 gulong ired, igreen, iblue;
754 if (cc->clut == NULL)
756 red >>= 16 - cc->bits.red;
757 green >>= 16 - cc->bits.green;
758 blue >>= 16 - cc->bits.blue;
760 ired = (red << cc->shifts.red) & cc->masks.red;
761 igreen = (green << cc->shifts.green) & cc->masks.green;
762 iblue = (blue << cc->shifts.blue) & cc->masks.blue;
764 return ired | igreen | iblue;
767 ired = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
768 igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
769 iblue = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
771 return ired | igreen | iblue;
774 case GDK_CC_MODE_PALETTE:
775 return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
777 case GDK_CC_MODE_STD_CMAP:
781 GdkColor *result = NULL;
788 result = g_hash_table_lookup (cc->color_hash, &color);
797 if (!gdk_color_alloc (cc->colormap, &color))
803 /* XXX: the following comment comes directly from
804 * XCC.c. I don't know if it is relevant for
805 * gdk_color_alloc() as it is for XAllocColor()
809 * I can't figure this out entirely, but it *is* possible
810 * that XAllocColor succeeds, even if the number of
811 * allocations we've made exceeds the number of available
812 * colors in the current colormap. And therefore it
813 * might be necessary for us to resize the CLUT.
816 if (cc->num_allocated == cc->max_colors)
820 GDK_NOTE (COLOR_CONTEXT,
821 g_message ("gdk_color_context_get_pixel: "
822 "resizing CLUT to %i entries\n",
825 cc->clut = g_realloc (cc->clut,
826 cc->max_colors * sizeof (gulong));
829 /* Key and value are the same color structure */
831 cnew = g_new (GdkColor, 1);
835 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
836 g_hash_table_insert (cc->color_hash, cnew, cnew);
838 cc->clut[cc->num_allocated] = color.pixel;
844 return result->pixel;
850 gdk_color_context_get_pixels (GdkColorContext *cc,
859 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
860 gint bad_alloc = FALSE;
861 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
862 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
863 #ifdef G_ENABLE_DEBUG
864 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
866 g_assert (cc != NULL);
867 g_assert (reds != NULL);
868 g_assert (greens != NULL);
869 g_assert (blues != NULL);
870 g_assert (colors != NULL);
871 g_assert (nallocated != NULL);
873 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
874 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
875 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
877 /* Will only have a value if used by the progressive image loader */
883 /* First allocate all pixels */
885 for (i = 0; i < ncolors; i++)
887 /* colors[i] is only zero if the pixel at that location hasn't
888 * been allocated yet. This is a sanity check required for proper
889 * color allocation by the progressive image loader
894 defs[i].red = reds[i];
895 defs[i].green = greens[i];
896 defs[i].blue = blues[i];
898 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i],
901 /* successfully allocated, store it */
905 defs[i].pixel = colors[i];
906 allocated[ncols++] = colors[i];
915 /* all colors available, all done */
917 if ((ncols == ncolors) || (nopen == 0))
919 GDK_NOTE (COLOR_CONTEXT,
920 g_message ("gdk_color_context_get_pixels: got all %i colors; "
921 "(%i colors allocated so far)\n", ncolors, cc->num_allocated));
926 /* The fun part. We now try to allocate the colors we couldn't allocate
927 * directly. The first step will map a color onto its nearest color
928 * that has been allocated (either by us or someone else). If any colors
929 * remain unallocated, we map these onto the colors that we have allocated
933 /* read up to MAX_IMAGE_COLORS colors of the current colormap */
935 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
937 /* see if the colormap has any colors to read */
941 g_warning ("gdk_color_context_get_pixels: oops! no colors available, "
942 "your images will look *really* ugly.");
947 #ifdef G_ENABLE_DEBUG
951 /* initialize pixels */
953 for (i = 0; i < cmapsize; i++)
956 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
959 /* read the colormap */
961 my_x_query_colors (cc->colormap, cmap, cmapsize);
963 /* get a close match for any unallocated colors */
971 gint d, j, mdist, close, ri, gi, bi;
979 /* Store these vals. Small performance increase as this skips three
980 * indexing operations in the loop code.
987 /* Walk all colors in the colormap and see which one is the
988 * closest. Uses plain least squares.
991 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
993 /* Don't replace these by shifts; the sign may get clobbered */
995 rd = (ri - cmap[j].red) / 256;
996 gd = (gi - cmap[j].green) / 256;
997 bd = (bi - cmap[j].blue) / 256;
999 d = rd * rd + gd * gd + bd * bd;
1010 rd = cmap[close].red;
1011 gd = cmap[close].green;
1012 bd = cmap[close].blue;
1016 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1022 defs[i] = cmap[close];
1023 defs[i].pixel = colors[i];
1024 allocated[ncols++] = colors[i];
1025 #ifdef G_ENABLE_DEBUG
1029 failed[nopen++] = i;
1031 failed[nopen++] = i;
1032 /* deal with in next stage if allocation failed */
1034 while (++idx < counter);
1036 *nallocated = ncols;
1038 /* This is the maximum no. of allocated colors. See also the nopen == 0
1042 if ((ncols == ncolors) || (nopen == 0))
1044 GDK_NOTE (COLOR_CONTEXT,
1045 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact and "
1046 "%i close (%i colors allocated so far)\n",
1047 ncolors, exact_col, close_col, cc->num_allocated));
1052 /* Now map any remaining unallocated pixels into the colors we did get */
1058 gint d, mdist, close, ri, gi, bi;
1072 /* search allocated colors */
1074 for (j = 0; (j < ncols) && (mdist != 0); j++)
1078 /* Don't replace these by shifts; the sign may get clobbered */
1080 rd = (ri - defs[k].red) / 256;
1081 gd = (gi - defs[k].green) / 256;
1082 bd = (bi - defs[k].blue) / 256;
1084 d = rd * rd + gd * gd + bd * bd;
1095 /* too bad, map to black */
1097 defs[i].pixel = cc->black_pixel;
1098 defs[i].red = defs[i].green = defs[i].blue = 0;
1099 #ifdef G_ENABLE_DEBUG
1105 defs[i] = defs[close];
1106 #ifdef G_ENABLE_DEBUG
1111 colors[i] = defs[i].pixel;
1113 while (++idx < nopen);
1115 GDK_NOTE (COLOR_CONTEXT,
1116 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact, %i close, "
1117 "%i substituted, %i to black (%i colors allocated so far)\n",
1118 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1122 gdk_color_context_get_pixels_incremental (GdkColorContext *cc,
1132 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
1133 gint bad_alloc = FALSE;
1134 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
1135 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
1136 #ifdef G_ENABLE_DEBUG
1137 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
1140 g_assert (cc != NULL);
1141 g_assert (reds != NULL);
1142 g_assert (greens != NULL);
1143 g_assert (blues != NULL);
1144 g_assert (used != NULL);
1145 g_assert (colors != NULL);
1146 g_assert (nallocated != NULL);
1148 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
1149 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
1150 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
1152 /* Will only have a value if used by the progressive image loader */
1154 ncols = *nallocated;
1158 /* First allocate all pixels */
1160 for (i = 0; i < ncolors; i++)
1162 /* used[i] is only -1 if the pixel at that location hasn't
1163 * been allocated yet. This is a sanity check required for proper
1164 * color allocation by the progressive image loader.
1165 * When colors[i] == 0 it indicates the slot is available for
1169 if (used[i] != FALSE)
1173 defs[i].red = reds[i];
1174 defs[i].green = greens[i];
1175 defs[i].blue = blues[i];
1177 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i], &bad_alloc);
1179 /* successfully allocated, store it */
1183 defs[i].pixel = colors[i];
1184 allocated[ncols++] = colors[i];
1187 failed[nopen++] = i;
1191 GDK_NOTE (COLOR_CONTEXT,
1192 g_message ("gdk_color_context_get_pixels_incremental: "
1193 "pixel at slot %i already allocated, skipping\n", i));
1198 *nallocated = ncols;
1200 if ((ncols == ncolors) || (nopen == 0))
1202 GDK_NOTE (COLOR_CONTEXT,
1203 g_message ("gdk_color_context_get_pixels_incremental: got all %i colors "
1204 "(%i colors allocated so far)\n",
1205 ncolors, cc->num_allocated));
1210 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
1214 g_warning ("gdk_color_context_get_pixels_incremental: oops! "
1215 "No colors available images will look *really* ugly.");
1219 #ifdef G_ENABLE_DEBUG
1223 /* initialize pixels */
1225 for (i = 0; i < cmapsize; i++)
1228 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
1233 my_x_query_colors (cc->colormap, cmap, cmapsize);
1235 /* now match any unallocated colors */
1243 gint d, j, mdist, close, ri, gi, bi;
1257 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
1259 /* Don't replace these by shifts; the sign may get clobbered */
1261 rd = (ri - cmap[j].red) / 256;
1262 gd = (gi - cmap[j].green) / 256;
1263 bd = (bi - cmap[j].blue) / 256;
1265 d = rd * rd + gd * gd + bd * bd;
1276 rd = cmap[close].red;
1277 gd = cmap[close].green;
1278 bd = cmap[close].blue;
1282 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1288 defs[i] = cmap[close];
1289 defs[i].pixel = colors[i];
1290 allocated[ncols++] = colors[i];
1291 #ifdef G_ENABLE_DEBUG
1296 failed[nopen++] = i;
1299 failed[nopen++] = i;
1300 /* deal with in next stage if allocation failed */
1302 while (++idx < counter);
1304 *nallocated = ncols;
1306 if ((ncols == ncolors) || (nopen == 0))
1308 GDK_NOTE (COLOR_CONTEXT,
1309 g_message ("gdk_color_context_get_pixels_incremental: "
1310 "got %i colors, %i exact and %i close "
1311 "(%i colors allocated so far)\n",
1312 ncolors, exact_col, close_col, cc->num_allocated));
1317 /* map remaining unallocated pixels into colors we did get */
1323 gint d, mdist, close, ri, gi, bi;
1335 /* search allocated colors */
1337 for (j = 0; (j < ncols) && (mdist != 0); j++)
1342 /* Don't replace these by shifts; the sign may get clobbered */
1344 rd = (ri - defs[k].red) / 256;
1345 gd = (gi - defs[k].green) / 256;
1346 bd = (bi - defs[k].blue) / 256;
1348 d = rd * rd + gd * gd + bd * bd;
1359 /* too bad, map to black */
1361 defs[i].pixel = cc->black_pixel;
1362 defs[i].red = defs[i].green = defs[i].blue = 0;
1363 #ifdef G_ENABLE_DEBUG
1369 defs[i] = defs[close];
1370 #ifdef G_ENABLE_DEBUG
1375 colors[i] = defs[i].pixel;
1377 while (++idx < nopen);
1379 GDK_NOTE (COLOR_CONTEXT,
1380 g_message ("gdk_color_context_get_pixels_incremental: "
1381 "got %i colors, %i exact, %i close, %i substituted, %i to black "
1382 "(%i colors allocated so far)\n",
1383 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1387 gdk_color_context_query_color (GdkColorContext *cc,
1390 return gdk_color_context_query_colors (cc, color, 1);
1394 gdk_color_context_query_colors (GdkColorContext *cc,
1401 g_assert (cc != NULL);
1402 g_assert (colors != NULL);
1406 case GDK_CC_MODE_BW:
1407 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1409 if (tc->pixel == cc->white_pixel)
1410 tc->red = tc->green = tc->blue = 65535;
1412 tc->red = tc->green = tc->blue = 0;
1416 case GDK_CC_MODE_TRUE:
1417 if (cc->clut == NULL)
1418 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1420 tc->red = ((tc->pixel & cc->masks.red) >> cc->shifts.red) << (16 - cc->bits.red);
1421 tc->green = ((tc->pixel & cc->masks.green) >> cc->shifts.green) << (16 - cc->bits.green);
1422 tc->blue = ((tc->pixel & cc->masks.blue) >> cc->shifts.blue) << (16 - cc->bits.blue);
1426 my_x_query_colors (cc->colormap, colors, num_colors);
1431 case GDK_CC_MODE_STD_CMAP:
1433 if (cc->cmap == NULL)
1435 my_x_query_colors (cc->colormap, colors, num_colors);
1440 gint first, last, half;
1443 for (i = 0, tc = colors; i < num_colors; i++)
1446 last = cc->num_colors - 1;
1448 while (first <= last)
1450 half = (first + last) / 2;
1451 half_pixel = cc->cmap[half].pixel;
1453 if (tc->pixel == half_pixel)
1455 tc->red = cc->cmap[half].red;
1456 tc->green = cc->cmap[half].green;
1457 tc->blue = cc->cmap[half].blue;
1458 first = last + 1; /* false break */
1462 if (tc->pixel > half_pixel)
1477 gdk_color_context_add_palette (GdkColorContext *cc,
1485 g_assert (cc != NULL);
1487 /* initialize this palette (will also erase previous palette as well) */
1491 /* restore previous mode if we aren't adding a new palette */
1493 if (num_palette == 0)
1496 /* copy incoming palette */
1498 cc->palette = g_new0(GdkColor, num_palette);
1502 for (i = 0; i < num_palette; i++)
1507 /* try to allocate this color */
1510 g = palette[i].green;
1511 b = palette[i].blue;
1513 gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1515 /* only store if we succeed */
1519 /* store in palette */
1521 cc->palette[j].red = r;
1522 cc->palette[j].green = g;
1523 cc->palette[j].blue = b;
1524 cc->palette[j].pixel = pixel[0];
1526 /* move to next slot */
1534 if (j != num_palette)
1535 cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1537 /* clear the hash table, we don't use it when dithering */
1541 g_hash_table_foreach (cc->color_hash,
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 */