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.
61 #include "gdkprivate.h"
65 #define MAX_IMAGE_COLORS 256
69 hash_color (gconstpointer key)
71 const GdkColor *color = key;
73 return (color->red * 33023 + color->green * 30013 + color->blue * 27011);
77 compare_colors (gconstpointer a,
80 const GdkColor *aa = a;
81 const GdkColor *bb = b;
83 return ((aa->red == bb->red) && (aa->green == bb->green) && (aa->blue == bb->blue));
87 free_hash_entry (gpointer key,
91 g_free (key); /* key and value are the same GdkColor */
95 pixel_sort (const void *a, const void *b)
97 return ((GdkColor *) a)->pixel - ((GdkColor *) b)->pixel;
100 /* XXX: This function does an XQueryColors() the hard way, because there is
101 * no corresponding function in Gdk.
105 my_x_query_colors (GdkColormap *colormap,
112 xcolors = g_new (XColor, ncolors);
113 for (i = 0; i < ncolors; i++)
114 xcolors[i].pixel = colors[i].pixel;
116 XQueryColors (gdk_display, GDK_COLORMAP_XCOLORMAP (colormap), xcolors, ncolors);
118 for (i = 0; i < ncolors; i++)
120 colors[i].red = xcolors[i].red;
121 colors[i].green = xcolors[i].green;
122 colors[i].blue = xcolors[i].blue;
129 query_colors (GdkColorContext *cc)
132 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
133 cc->cmap = g_new (GdkColor, cc->num_colors);
135 for (i = 0; i < cc->num_colors; i++)
136 cc->cmap[i].pixel = cc->clut ? cc->clut[i] : ccp->std_cmap.base_pixel + i;
138 my_x_query_colors (cc->colormap, cc->cmap, cc->num_colors);
140 qsort (cc->cmap, cc->num_colors, sizeof (GdkColor), pixel_sort);
144 init_bw (GdkColorContext *cc)
148 g_warning ("init_bw: failed to allocate colors, falling back to black and white");
150 cc->mode = GDK_CC_MODE_BW;
152 color.red = color.green = color.blue = 0;
154 if (!gdk_color_alloc (cc->colormap, &color))
157 cc->black_pixel = color.pixel;
159 color.red = color.green = color.blue = 0xffff;
161 if (!gdk_color_alloc (cc->colormap, &color))
162 cc->white_pixel = cc->black_pixel ? 0 : 1;
164 cc->white_pixel = color.pixel;
170 init_gray (GdkColorContext *cc)
172 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
173 GdkColor *clrs, *cstart;
177 cc->num_colors = GDK_VISUAL_XVISUAL (cc->visual)->map_entries;
179 cc->clut = g_new (gulong, cc->num_colors);
180 cstart = g_new (GdkColor, cc->num_colors);
184 dinc = 65535.0 / (cc->num_colors - 1);
188 for (i = 0; i < cc->num_colors; i++)
190 clrs->red = clrs->green = clrs->blue = dinc * i;
192 if (!gdk_color_alloc (cc->colormap, clrs))
194 gdk_colors_free (cc->colormap, cc->clut, i, 0);
198 if (cc->num_colors > 1)
210 cc->clut[i] = clrs++->pixel;
215 /* XXX: is this the right thing to do? */
216 ccp->std_cmap.colormap = GDK_COLORMAP_XCOLORMAP (cc->colormap);
217 ccp->std_cmap.base_pixel = 0;
218 ccp->std_cmap.red_max = cc->num_colors - 1;
219 ccp->std_cmap.green_max = 0;
220 ccp->std_cmap.blue_max = 0;
221 ccp->std_cmap.red_mult = 1;
222 ccp->std_cmap.green_mult = 0;
223 ccp->std_cmap.blue_mult = 0;
225 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
226 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
230 cc->mode = GDK_CC_MODE_MY_GRAY;
234 init_color (GdkColorContext *cc)
236 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
240 while ((cubeval * cubeval * cubeval) < GDK_VISUAL_XVISUAL (cc->visual)->map_entries)
244 cc->num_colors = cubeval * cubeval * cubeval;
246 ccp->std_cmap.red_max = cubeval - 1;
247 ccp->std_cmap.green_max = cubeval - 1;
248 ccp->std_cmap.blue_max = cubeval - 1;
249 ccp->std_cmap.red_mult = cubeval * cubeval;
250 ccp->std_cmap.green_mult = cubeval;
251 ccp->std_cmap.blue_mult = 1;
252 ccp->std_cmap.base_pixel = 0;
254 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
255 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
256 cc->num_colors = DisplayCells (ccp->xdisplay, gdk_screen);
258 /* a CLUT for storing allocated pixel indices */
260 cc->max_colors = cc->num_colors;
261 cc->clut = g_new (gulong, cc->max_colors);
263 for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
264 cc->clut[cubeval] = cubeval;
268 cc->mode = GDK_CC_MODE_STD_CMAP;
273 init_true_color (GdkColorContext *cc)
275 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
276 gulong rmask, gmask, bmask;
278 cc->mode = GDK_CC_MODE_TRUE;
282 rmask = cc->masks.red = cc->visual->red_mask;
301 gmask = cc->masks.green = cc->visual->green_mask;
303 cc->shifts.green = 0;
320 bmask = cc->masks.blue = cc->visual->blue_mask;
337 cc->num_colors = (cc->visual->red_mask | cc->visual->green_mask | cc->visual->blue_mask) + 1;
338 cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
339 cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
343 init_direct_color (GdkColorContext *cc)
346 GdkColor *clrs, *cstart;
347 gulong rval, gval, bval;
353 init_true_color (cc); /* for shift stuff */
355 rval = cc->visual->red_mask >> cc->shifts.red;
356 gval = cc->visual->green_mask >> cc->shifts.green;
357 bval = cc->visual->blue_mask >> cc->shifts.blue;
359 rtable = g_new (gulong, rval + 1);
360 gtable = g_new (gulong, gval + 1);
361 btable = g_new (gulong, bval + 1);
363 cc->max_entry = MAX (rval, gval);
364 cc->max_entry = MAX (cc->max_entry, bval);
366 cstart = g_new (GdkColor, cc->max_entry + 1);
367 cc->clut = g_new (gulong, cc->max_entry + 1);
371 for (n = 0; n < rval; n++)
372 rtable[n] = rval ? (65535.0 / rval * n) : 0;
374 for (n = 0; n < gval; n++)
375 gtable[n] = gval ? (65535.0 / gval * n) : 0;
377 for (n = 0; n < bval; n++)
378 btable[n] = bval ? (65535.0 / bval * n) : 0;
380 cc->max_entry = MAX (rval, gval);
381 cc->max_entry = MAX (cc->max_entry, bval);
385 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
387 for (n = 0; n < cc->max_entry; n++)
389 dinc = (double) n / cc->max_entry;
391 clrs->red = rtable[(int) (dinc * rval)];
392 clrs->green = gtable[(int) (dinc * gval)];
393 clrs->blue = btable[(int) (dinc * bval)];
395 if (gdk_color_alloc (cc->colormap, clrs))
397 cc->clut[count++] = clrs->pixel;
402 gdk_colors_free (cc->colormap, cc->clut, count, 0);
408 cc->masks.red = (cc->masks.red >> 1) & cc->visual->red_mask;
409 cc->masks.green = (cc->masks.green >> 1) & cc->visual->green_mask;
410 cc->masks.blue = (cc->masks.blue >> 1) & cc->visual->blue_mask;
420 cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
422 if (cc->num_colors >1)
434 /* Update allocated color count; original num_colors is max_entry, which
435 * is not necessarily the same as the really allocated number of colors.
438 cc->num_colors = count;
447 init_palette (GdkColorContext *cc)
449 /* restore correct mode for this cc */
451 switch (cc->visual->type)
453 case GDK_VISUAL_STATIC_GRAY:
454 case GDK_VISUAL_GRAYSCALE:
455 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
456 cc->mode = GDK_CC_MODE_BW;
458 cc->mode = GDK_CC_MODE_MY_GRAY;
461 case GDK_VISUAL_TRUE_COLOR:
462 case GDK_VISUAL_DIRECT_COLOR:
463 cc->mode = GDK_CC_MODE_TRUE;
466 case GDK_VISUAL_STATIC_COLOR:
467 case GDK_VISUAL_PSEUDO_COLOR:
468 cc->mode = GDK_CC_MODE_STD_CMAP;
472 cc->mode = GDK_CC_MODE_UNDEFINED;
476 /* previous palette */
479 g_free (cc->palette);
482 g_free (cc->fast_dither);
484 /* clear hash table if present */
488 /* XXX: quick-and-dirty way to remove everything */
490 g_hash_table_destroy (cc->color_hash);
491 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
496 cc->fast_dither = NULL;
500 gdk_color_context_new (GdkVisual *visual,
501 GdkColormap *colormap)
503 GdkColorContextPrivate *ccp;
504 gint use_private_colormap = FALSE; /* XXX: maybe restore full functionality later? */
507 GdkColormap *default_colormap;
509 g_assert (visual != NULL);
510 g_assert (colormap != NULL);
512 ccp = g_new (GdkColorContextPrivate, 1);
513 cc = (GdkColorContext *) ccp;
514 ccp->xdisplay = gdk_display;
516 cc->colormap = colormap;
519 cc->mode = GDK_CC_MODE_UNDEFINED;
520 cc->need_to_free_colormap = FALSE;
522 cc->color_hash = NULL;
525 cc->fast_dither = NULL;
527 default_colormap = gdk_colormap_get_system ();
531 while (retry_count < 2)
533 /* Only create a private colormap if the visual found isn't equal
534 * to the default visual and we don't have a private colormap,
535 * -or- if we are instructed to create a private colormap (which
536 * never is the case for XmHTML).
539 if (use_private_colormap
540 || ((cc->visual != gdk_visual_get_system ()) /* default visual? */
541 && (GDK_COLORMAP_XCOLORMAP (colormap) == GDK_COLORMAP_XCOLORMAP (default_colormap))))
543 g_warning ("gdk_color_context_new: non-default visual detected, "
544 "using private colormap");
546 cc->colormap = gdk_colormap_new (cc->visual, FALSE);
548 cc->need_to_free_colormap = (GDK_COLORMAP_XCOLORMAP (colormap)
549 != GDK_COLORMAP_XCOLORMAP (default_colormap));
552 switch (visual->type)
554 case GDK_VISUAL_STATIC_GRAY:
555 case GDK_VISUAL_GRAYSCALE:
556 GDK_NOTE (COLOR_CONTEXT,
557 g_message ("gdk_color_context_new: visual class is %s\n",
558 (visual->type == GDK_VISUAL_STATIC_GRAY) ?
559 "GDK_VISUAL_STATIC_GRAY" :
560 "GDK_VISUAL_GRAYSCALE"));
562 if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
569 case GDK_VISUAL_TRUE_COLOR: /* shifts */
570 GDK_NOTE (COLOR_CONTEXT,
571 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
573 init_true_color (cc);
576 case GDK_VISUAL_DIRECT_COLOR: /* shifts and fake CLUT */
577 GDK_NOTE (COLOR_CONTEXT,
578 g_message ("gdk_color_context_new: visual class is GDK_VISUAL_DIRECT_COLOR\n"));
580 init_direct_color (cc);
583 case GDK_VISUAL_STATIC_COLOR:
584 case GDK_VISUAL_PSEUDO_COLOR:
585 GDK_NOTE (COLOR_CONTEXT,
586 g_message ("gdk_color_context_new: visual class is %s\n",
587 (visual->type == GDK_VISUAL_STATIC_COLOR) ?
588 "GDK_VISUAL_STATIC_COLOR" :
589 "GDK_VISUAL_PSEUDO_COLOR"));
595 g_assert_not_reached ();
598 if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
600 use_private_colormap = TRUE;
607 /* no. of colors allocated yet */
609 cc->num_allocated = 0;
611 GDK_NOTE (COLOR_CONTEXT,
612 g_message ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
613 cc->visual->depth, cc->num_colors));
615 /* check if we need to initialize a hash table */
617 if ((cc->mode == GDK_CC_MODE_STD_CMAP) || (cc->mode == GDK_CC_MODE_UNDEFINED))
618 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
620 return (GdkColorContext *) cc;
624 gdk_color_context_new_mono (GdkVisual *visual,
625 GdkColormap *colormap)
627 GdkColorContextPrivate *ccp;
630 g_assert (visual != NULL);
631 g_assert (colormap != NULL);
633 cc = g_new (GdkColorContext, 1);
634 ccp = (GdkColorContextPrivate *) cc;
635 ccp->xdisplay = gdk_display;
637 cc->colormap = colormap;
640 cc->mode = GDK_CC_MODE_UNDEFINED;
641 cc->need_to_free_colormap = FALSE;
645 return (GdkColorContext *) cc;
648 /* This doesn't currently free black/white, hmm... */
651 gdk_color_context_free (GdkColorContext *cc)
653 g_assert (cc != NULL);
655 if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
656 || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
658 gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
661 else if (cc->clut != NULL)
663 gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
667 if (cc->cmap != NULL)
670 if (cc->need_to_free_colormap)
671 gdk_colormap_unref (cc->colormap);
673 /* free any palette that has been associated with this GdkColorContext */
679 g_hash_table_foreach (cc->color_hash,
682 g_hash_table_destroy (cc->color_hash);
689 gdk_color_context_get_pixel (GdkColorContext *cc,
695 GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
696 g_assert (cc != NULL);
697 g_assert (failed != NULL);
707 value = (red / 65535.0 * 0.30
708 + green / 65535.0 * 0.59
709 + blue / 65535.0 * 0.11);
712 return cc->white_pixel;
714 return cc->black_pixel;
717 case GDK_CC_MODE_MY_GRAY:
719 gulong ired, igreen, iblue;
721 red = red * 0.30 + green * 0.59 + blue * 0.11;
725 if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
726 ired = ccp->std_cmap.red_max;
728 ired *= ccp->std_cmap.red_mult;
730 if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
731 igreen = ccp->std_cmap.green_max;
733 igreen *= ccp->std_cmap.green_mult;
735 if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
736 iblue = ccp->std_cmap.blue_max;
738 iblue *= ccp->std_cmap.blue_mult;
740 if (cc->clut != NULL)
741 return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
743 return ccp->std_cmap.base_pixel + ired + igreen + iblue;
746 case GDK_CC_MODE_TRUE:
748 gulong ired, igreen, iblue;
750 if (cc->clut == NULL)
752 red >>= 16 - cc->bits.red;
753 green >>= 16 - cc->bits.green;
754 blue >>= 16 - cc->bits.blue;
756 ired = (red << cc->shifts.red) & cc->masks.red;
757 igreen = (green << cc->shifts.green) & cc->masks.green;
758 iblue = (blue << cc->shifts.blue) & cc->masks.blue;
760 return ired | igreen | iblue;
763 ired = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
764 igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
765 iblue = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
767 return ired | igreen | iblue;
770 case GDK_CC_MODE_PALETTE:
771 return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
773 case GDK_CC_MODE_STD_CMAP:
783 result = g_hash_table_lookup (cc->color_hash, &color);
792 if (!gdk_color_alloc (cc->colormap, &color))
798 /* XXX: the following comment comes directly from
799 * XCC.c. I don't know if it is relevant for
800 * gdk_color_alloc() as it is for XAllocColor()
804 * I can't figure this out entirely, but it *is* possible
805 * that XAllocColor succeeds, even if the number of
806 * allocations we've made exceeds the number of available
807 * colors in the current colormap. And therefore it
808 * might be necessary for us to resize the CLUT.
811 if (cc->num_allocated == cc->max_colors)
815 GDK_NOTE (COLOR_CONTEXT,
816 g_message ("gdk_color_context_get_pixel: "
817 "resizing CLUT to %i entries\n",
820 cc->clut = g_realloc (cc->clut,
821 cc->max_colors * sizeof (gulong));
824 /* Key and value are the same color structure */
826 cnew = g_new (GdkColor, 1);
828 g_hash_table_insert (cc->color_hash, cnew, cnew);
830 cc->clut[cc->num_allocated] = color.pixel;
836 return result->pixel;
842 gdk_color_context_get_pixels (GdkColorContext *cc,
851 gint cmapsize, ncols = 0, nopen = 0, counter = 0;
852 gint bad_alloc = FALSE;
853 gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
854 GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
855 #ifdef G_ENABLE_DEBUG
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 GDK_NOTE (COLOR_CONTEXT,
912 g_message ("gdk_color_context_get_pixels: got all %i colors; "
913 "(%i colors allocated so far)\n", 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.");
939 #ifdef G_ENABLE_DEBUG
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];
1017 #ifdef G_ENABLE_DEBUG
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 GDK_NOTE (COLOR_CONTEXT,
1037 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact and "
1038 "%i close (%i colors allocated so far)\n",
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;
1091 #ifdef G_ENABLE_DEBUG
1097 defs[i] = defs[close];
1098 #ifdef G_ENABLE_DEBUG
1103 colors[i] = defs[i].pixel;
1105 while (++idx < nopen);
1107 GDK_NOTE (COLOR_CONTEXT,
1108 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact, %i close, "
1109 "%i substituted, %i to black (%i colors allocated so far)\n",
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 #ifdef G_ENABLE_DEBUG
1129 gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
1132 g_assert (cc != NULL);
1133 g_assert (reds != NULL);
1134 g_assert (greens != NULL);
1135 g_assert (blues != NULL);
1136 g_assert (used != NULL);
1137 g_assert (colors != NULL);
1138 g_assert (nallocated != NULL);
1140 memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
1141 memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
1142 memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
1144 /* Will only have a value if used by the progressive image loader */
1146 ncols = *nallocated;
1150 /* First allocate all pixels */
1152 for (i = 0; i < ncolors; i++)
1154 /* used[i] is only -1 if the pixel at that location hasn't
1155 * been allocated yet. This is a sanity check required for proper
1156 * color allocation by the progressive image loader.
1157 * When colors[i] == 0 it indicates the slot is available for
1161 if (used[i] != FALSE)
1165 defs[i].red = reds[i];
1166 defs[i].green = greens[i];
1167 defs[i].blue = blues[i];
1169 colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i], &bad_alloc);
1171 /* successfully allocated, store it */
1175 defs[i].pixel = colors[i];
1176 allocated[ncols++] = colors[i];
1179 failed[nopen++] = i;
1183 GDK_NOTE (COLOR_CONTEXT,
1184 g_message ("gdk_color_context_get_pixels_incremental: "
1185 "pixel at slot %i already allocated, skipping\n", i));
1190 *nallocated = ncols;
1192 if ((ncols == ncolors) || (nopen == 0))
1194 GDK_NOTE (COLOR_CONTEXT,
1195 g_message ("gdk_color_context_get_pixels_incremental: got all %i colors "
1196 "(%i colors allocated so far)\n",
1197 ncolors, cc->num_allocated));
1202 cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
1206 g_warning ("gdk_color_context_get_pixels_incremental: oops! "
1207 "No colors available images will look *really* ugly.");
1211 #ifdef G_ENABLE_DEBUG
1215 /* initialize pixels */
1217 for (i = 0; i < cmapsize; i++)
1220 cmap[i].red = cmap[i].green = cmap[i].blue = 0;
1225 my_x_query_colors (cc->colormap, cmap, cmapsize);
1227 /* now match any unallocated colors */
1235 gint d, j, mdist, close, ri, gi, bi;
1249 for (j = 0; (j < cmapsize) && (mdist != 0); j++)
1251 /* Don't replace these by shifts; the sign may get clobbered */
1253 rd = (ri - cmap[j].red) / 256;
1254 gd = (gi - cmap[j].green) / 256;
1255 bd = (bi - cmap[j].blue) / 256;
1257 d = rd * rd + gd * gd + bd * bd;
1268 rd = cmap[close].red;
1269 gd = cmap[close].green;
1270 bd = cmap[close].blue;
1274 colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1280 defs[i] = cmap[close];
1281 defs[i].pixel = colors[i];
1282 allocated[ncols++] = colors[i];
1283 #ifdef G_ENABLE_DEBUG
1288 failed[nopen++] = i;
1291 failed[nopen++] = i;
1292 /* deal with in next stage if allocation failed */
1294 while (++idx < counter);
1296 *nallocated = ncols;
1298 if ((ncols == ncolors) || (nopen == 0))
1300 GDK_NOTE (COLOR_CONTEXT,
1301 g_message ("gdk_color_context_get_pixels_incremental: "
1302 "got %i colors, %i exact and %i close "
1303 "(%i colors allocated so far)\n",
1304 ncolors, exact_col, close_col, cc->num_allocated));
1309 /* map remaining unallocated pixels into colors we did get */
1315 gint d, mdist, close, ri, gi, bi;
1327 /* search allocated colors */
1329 for (j = 0; (j < ncols) && (mdist != 0); j++)
1334 /* Don't replace these by shifts; the sign may get clobbered */
1336 rd = (ri - defs[k].red) / 256;
1337 gd = (gi - defs[k].green) / 256;
1338 bd = (bi - defs[k].blue) / 256;
1340 d = rd * rd + gd * gd + bd * bd;
1351 /* too bad, map to black */
1353 defs[i].pixel = cc->black_pixel;
1354 defs[i].red = defs[i].green = defs[i].blue = 0;
1355 #ifdef G_ENABLE_DEBUG
1361 defs[i] = defs[close];
1362 #ifdef G_ENABLE_DEBUG
1367 colors[i] = defs[i].pixel;
1369 while (++idx < nopen);
1371 GDK_NOTE (COLOR_CONTEXT,
1372 g_message ("gdk_color_context_get_pixels_incremental: "
1373 "got %i colors, %i exact, %i close, %i substituted, %i to black "
1374 "(%i colors allocated so far)\n",
1375 ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1379 gdk_color_context_query_color (GdkColorContext *cc,
1382 return gdk_color_context_query_colors (cc, color, 1);
1386 gdk_color_context_query_colors (GdkColorContext *cc,
1393 g_assert (cc != NULL);
1394 g_assert (colors != NULL);
1398 case GDK_CC_MODE_BW:
1399 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1401 if (tc->pixel == cc->white_pixel)
1402 tc->red = tc->green = tc->blue = 65535;
1404 tc->red = tc->green = tc->blue = 0;
1408 case GDK_CC_MODE_TRUE:
1409 if (cc->clut == NULL)
1410 for (i = 0, tc = colors; i < num_colors; i++, tc++)
1412 tc->red = ((tc->pixel & cc->masks.red) >> cc->shifts.red) << (16 - cc->bits.red);
1413 tc->green = ((tc->pixel & cc->masks.green) >> cc->shifts.green) << (16 - cc->bits.green);
1414 tc->blue = ((tc->pixel & cc->masks.blue) >> cc->shifts.blue) << (16 - cc->bits.blue);
1418 my_x_query_colors (cc->colormap, colors, num_colors);
1423 case GDK_CC_MODE_STD_CMAP:
1425 if (cc->cmap == NULL)
1427 my_x_query_colors (cc->colormap, colors, num_colors);
1432 gint first, last, half;
1435 for (i = 0, tc = colors; i < num_colors; i++)
1438 last = cc->num_colors - 1;
1440 while (first <= last)
1442 half = (first + last) / 2;
1443 half_pixel = cc->cmap[half].pixel;
1445 if (tc->pixel == half_pixel)
1447 tc->red = cc->cmap[half].red;
1448 tc->green = cc->cmap[half].green;
1449 tc->blue = cc->cmap[half].blue;
1450 first = last + 1; /* false break */
1454 if (tc->pixel > half_pixel)
1469 gdk_color_context_add_palette (GdkColorContext *cc,
1477 g_assert (cc != NULL);
1479 /* initialize this palette (will also erase previous palette as well) */
1483 /* restore previous mode if we aren't adding a new palette */
1485 if (num_palette == 0)
1487 /* GDK_CC_MODE_STD_CMAP uses a hash table, so we'd better initialize one */
1489 /* XXX: here, the hash table is already initialized */
1494 /* Initialize a hash table for this palette (we need one for allocating
1495 * the pixels in the palette using the current settings)
1498 if (cc->color_hash == NULL)
1499 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
1501 /* copy incoming palette */
1503 cc->palette = g_new0(GdkColor, num_palette);
1507 for (i = 0; i < num_palette; i++)
1512 /* try to allocate this color */
1515 g = palette[i].green;
1516 b = palette[i].blue;
1518 gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1520 /* only store if we succeed */
1524 /* store in palette */
1526 cc->palette[j].red = r;
1527 cc->palette[j].green = g;
1528 cc->palette[j].blue = b;
1529 cc->palette[j].pixel = pixel[0];
1531 /* move to next slot */
1539 if (j != num_palette)
1540 cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1542 /* clear the hash table, we don't use it when dithering */
1546 g_hash_table_destroy (cc->color_hash);
1547 cc->color_hash = NULL;
1550 /* store real palette size */
1552 cc->num_palette = j;
1554 /* switch to palette mode */
1556 cc->mode = GDK_CC_MODE_PALETTE;
1560 qsort (cc->palette, cc->num_palette, sizeof (GdkColor), pixel_sort);
1562 cc->fast_dither = NULL;
1568 gdk_color_context_init_dither (GdkColorContext *cc)
1570 gint rr, gg, bb, err, erg, erb;
1571 gint success = FALSE;
1573 g_assert (cc != NULL);
1575 /* now we can initialize the fast dither matrix */
1577 if (cc->fast_dither == NULL)
1578 cc->fast_dither = g_new (GdkColorContextDither, 1);
1580 /* Fill it. We ignore unsuccessful allocations, they are just mapped
1581 * to black instead */
1583 for (rr = 0; rr < 32; rr++)
1584 for (gg = 0; gg < 32; gg++)
1585 for (bb = 0; bb < 32; bb++)
1587 err = (rr << 3) | (rr >> 2);
1588 erg = (gg << 3) | (gg >> 2);
1589 erb = (bb << 3) | (bb >> 2);
1591 cc->fast_dither->fast_rgb[rr][gg][bb] =
1592 gdk_color_context_get_index_from_palette (cc, &err, &erg, &erb, &success);
1593 cc->fast_dither->fast_err[rr][gg][bb] = err;
1594 cc->fast_dither->fast_erg[rr][gg][bb] = erg;
1595 cc->fast_dither->fast_erb[rr][gg][bb] = erb;
1600 gdk_color_context_free_dither (GdkColorContext *cc)
1602 g_assert (cc != NULL);
1604 if (cc->fast_dither)
1605 g_free (cc->fast_dither);
1607 cc->fast_dither = NULL;
1611 gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
1618 gint dif, dr, dg, db, j = -1;
1619 gint mindif = 0x7fffffff;
1620 gint err = 0, erg = 0, erb = 0;
1623 g_assert (cc != NULL);
1624 g_assert (red != NULL);
1625 g_assert (green != NULL);
1626 g_assert (blue != NULL);
1627 g_assert (failed != NULL);
1631 for (i = 0; i < cc->num_palette; i++)
1633 dr = *red - cc->palette[i].red;
1634 dg = *green - cc->palette[i].green;
1635 db = *blue - cc->palette[i].blue;
1637 dif = dr * dr + dg * dg + db * db;
1643 pixel = cc->palette[i].pixel;
1653 /* we failed to map onto a color */
1668 gdk_color_context_get_index_from_palette (GdkColorContext *cc,
1674 gint dif, dr, dg, db, j = -1;
1675 gint mindif = 0x7fffffff;
1676 gint err = 0, erg = 0, erb = 0;
1679 g_assert (cc != NULL);
1680 g_assert (red != NULL);
1681 g_assert (green != NULL);
1682 g_assert (blue != NULL);
1683 g_assert (failed != NULL);
1687 for (i = 0; i < cc->num_palette; i++)
1689 dr = *red - cc->palette[i].red;
1690 dg = *green - cc->palette[i].green;
1691 db = *blue - cc->palette[i].blue;
1693 dif = dr * dr + dg * dg + db * db;
1708 /* we failed to map onto a color */
1717 /* return error fractions */