]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkcc-win32.c
configure.in gdk/Makefile.am gdk/x11/Makefile.am Small fixes, thanks to
[~andy/gtk] / gdk / win32 / gdkcc-win32.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
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)
24  */
25
26 /* Copyright 1994,1995 John L. Cwikla
27  *
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.
38  *
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.
47  *
48  * Author:
49  *  John L. Cwikla
50  *  X Programmer
51  *  Wolfram Research Inc.
52  *
53  *  cwikla@wri.com
54  */
55
56 /*
57  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
58  * file for a list of people on the GTK+ Team.  See the ChangeLog
59  * files for a list of changes.  These files are distributed with
60  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
61  */
62
63 #include "config.h"
64
65 #include <stdlib.h>
66 #include <string.h>
67
68 #include "gdkcc.h"
69 #include "gdkcolor.h"
70 #include "gdkwin32.h"
71
72 #define MAX_IMAGE_COLORS 256
73
74 typedef struct _GdkColorContextPrivate GdkColorContextPrivate;
75
76 struct _GdkColorContextPrivate
77 {
78   GdkColorContext color_context;
79   XStandardColormap std_cmap;
80 };
81
82 static guint
83 hash_color (gconstpointer key)
84 {
85   const GdkColor *color = key;
86   
87   return (color->red * 33023 + color->green * 30013 + color->blue * 27011);
88 }
89
90 static gint
91 compare_colors (gconstpointer a,
92                 gconstpointer b)
93 {
94   const GdkColor *aa = a;
95   const GdkColor *bb = b;
96   
97   return ((aa->red == bb->red) && (aa->green == bb->green) && (aa->blue == bb->blue));
98 }
99
100 static void
101 free_hash_entry (gpointer key,
102                  gpointer value,
103                  gpointer user_data)
104 {
105   g_free (key); /* key and value are the same GdkColor */
106 }
107
108 static int
109 pixel_sort (const void *a, const void *b)
110 {
111   return ((GdkColor *) a)->pixel - ((GdkColor *) b)->pixel;
112 }
113
114 static void
115 my_x_query_colors (GdkColormap *colormap,
116                    GdkColor    *colors,
117                    gint         ncolors)
118 {
119   gint    i;
120   
121   for (i = 0; i < ncolors; i++)
122     {
123       PALETTEENTRY palentry;
124       
125       GetPaletteEntries (GDK_COLORMAP_WIN32COLORMAP (colormap)->palette, colors[i].pixel, 1, &palentry);
126       colors[i].red = (palentry.peRed * 65535) / 255;
127       colors[i].green = (palentry.peGreen * 65535) / 255;
128       colors[i].blue = (palentry.peBlue * 65535) / 255;
129     }
130 }
131
132 static void
133 query_colors (GdkColorContext *cc)
134 {
135   gint i;
136   GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
137   cc->cmap = g_new (GdkColor, cc->num_colors);
138   
139   for (i = 0; i < cc->num_colors; i++)
140     cc->cmap[i].pixel = cc->clut ? cc->clut[i] : ccp->std_cmap.base_pixel + i;
141   
142   my_x_query_colors (cc->colormap, cc->cmap, cc->num_colors);
143   
144   qsort (cc->cmap, cc->num_colors, sizeof (GdkColor), pixel_sort);
145 }
146
147 static void
148 init_bw (GdkColorContext *cc)
149 {
150   GdkColor color;
151   
152   g_warning ("init_bw: failed to allocate colors, falling back to black and white");
153   
154   cc->mode = GDK_CC_MODE_BW;
155   
156   color.red = color.green = color.blue = 0;
157   
158   if (!gdk_color_alloc (cc->colormap, &color))
159     cc->black_pixel = 0;
160   else
161     cc->black_pixel = color.pixel;
162   
163   color.red = color.green = color.blue = 0xffff;
164   
165   if (!gdk_color_alloc (cc->colormap, &color))
166     cc->white_pixel = cc->black_pixel ? 0 : 1;
167   else
168     cc->white_pixel = color.pixel;
169   
170   cc->num_colors = 2;
171 }
172
173 static void
174 init_gray (GdkColorContext *cc)
175 {
176   GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
177   GdkColor *clrs, *cstart;
178   gint i;
179   gdouble dinc;
180   
181   cc->num_colors = 256;         /* Bogus, but will never get here anyway? */
182   
183   cc->clut = g_new (gulong, cc->num_colors);
184   cstart = g_new (GdkColor, cc->num_colors);
185   
186  retrygray:
187   
188   dinc = 65535.0 / (cc->num_colors - 1);
189   
190   clrs = cstart;
191   
192   for (i = 0; i < cc->num_colors; i++)
193     {
194       clrs->red = clrs->green = clrs->blue = dinc * i;
195       
196       if (!gdk_color_alloc (cc->colormap, clrs))
197         {
198           gdk_colors_free (cc->colormap, cc->clut, i, 0);
199           
200           cc->num_colors /= 2;
201           
202           if (cc->num_colors > 1)
203             goto retrygray;
204           else
205             {
206               g_free (cc->clut);
207               cc->clut = NULL;
208               init_bw (cc);
209               g_free (cstart);
210               return;
211             }
212         }
213       
214       cc->clut[i] = clrs++->pixel;
215     }
216   
217   g_free (cstart);
218   
219   /* XXX: is this the right thing to do? */
220   ccp->std_cmap.colormap = GDK_COLORMAP_WIN32COLORMAP (cc->colormap);
221   ccp->std_cmap.base_pixel = 0;
222   ccp->std_cmap.red_max = cc->num_colors - 1;
223   ccp->std_cmap.green_max = 0;
224   ccp->std_cmap.blue_max = 0;
225   ccp->std_cmap.red_mult = 1;
226   ccp->std_cmap.green_mult = 0;
227   ccp->std_cmap.blue_mult = 0;
228   
229   cc->white_pixel = 255;
230   cc->black_pixel = 0;
231
232   query_colors (cc);
233   
234   cc->mode = GDK_CC_MODE_MY_GRAY;
235 }
236
237 static void
238 init_color (GdkColorContext *cc)
239 {
240   GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
241   gint cubeval;
242   
243   cubeval = 1;
244   while ((cubeval * cubeval * cubeval) < GDK_VISUAL_XVISUAL (cc->visual)->map_entries)
245     cubeval++;
246   cubeval--;
247   
248   cc->num_colors = cubeval * cubeval * cubeval;
249   
250   ccp->std_cmap.red_max    = cubeval - 1;
251   ccp->std_cmap.green_max  = cubeval - 1;
252   ccp->std_cmap.blue_max   = cubeval - 1;
253   ccp->std_cmap.red_mult   = cubeval * cubeval;
254   ccp->std_cmap.green_mult = cubeval;
255   ccp->std_cmap.blue_mult  = 1;
256   ccp->std_cmap.base_pixel = 0;
257   
258   cc->white_pixel = 255;        /* ??? */
259   cc->black_pixel = 0;          /* ??? */
260   
261   /* a CLUT for storing allocated pixel indices */
262   
263   cc->max_colors = cc->num_colors;
264   cc->clut = g_new (gulong, cc->max_colors);
265   
266   for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
267     cc->clut[cubeval] = cubeval;
268   
269   query_colors (cc);
270   
271   cc->mode = GDK_CC_MODE_STD_CMAP;
272 }
273
274
275 static void
276 init_true_color (GdkColorContext *cc)
277 {
278   GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
279   gulong rmask, gmask, bmask;
280   
281   cc->mode = GDK_CC_MODE_TRUE;
282   
283   /* Red */
284   
285   rmask = cc->masks.red = cc->visual->red_mask;
286   
287   cc->shifts.red = 0;
288   cc->bits.red = 0;
289   
290   while (!(rmask & 1))
291     {
292       rmask >>= 1;
293       cc->shifts.red++;
294     }
295   
296   while (rmask & 1)
297     {
298       rmask >>= 1;
299       cc->bits.red++;
300     }
301   
302   /* Green */
303   
304   gmask = cc->masks.green = cc->visual->green_mask;
305
306   cc->shifts.green = 0;
307   cc->bits.green = 0;
308   
309   while (!(gmask & 1))
310     {
311       gmask >>= 1;
312       cc->shifts.green++;
313     }
314   
315   while (gmask & 1)
316     {
317       gmask >>= 1;
318       cc->bits.green++;
319     }
320   
321   /* Blue */
322   
323   bmask = cc->masks.blue = cc->visual->blue_mask;
324   
325   cc->shifts.blue = 0;
326   cc->bits.blue = 0;
327   
328   while (!(bmask & 1))
329     {
330       bmask >>= 1;
331       cc->shifts.blue++;
332     }
333   
334   while (bmask & 1)
335     {
336       bmask >>= 1;
337       cc->bits.blue++;
338     }
339   
340   cc->num_colors = (cc->visual->red_mask | cc->visual->green_mask | cc->visual->blue_mask) + 1;
341   
342   cc->white_pixel = 0xffffff;
343   cc->black_pixel = 0;
344 }
345
346 static void
347 init_palette (GdkColorContext *cc)
348 {
349   /* restore correct mode for this cc */
350  
351   switch (cc->visual->type)
352     {
353     case GDK_VISUAL_STATIC_GRAY:
354     case GDK_VISUAL_GRAYSCALE:
355       if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
356         cc->mode = GDK_CC_MODE_BW;
357       else
358         cc->mode = GDK_CC_MODE_MY_GRAY;
359       break;
360       
361     case GDK_VISUAL_TRUE_COLOR:
362       cc->mode = GDK_CC_MODE_TRUE;
363       break;
364       
365     case GDK_VISUAL_STATIC_COLOR:
366     case GDK_VISUAL_PSEUDO_COLOR:
367       cc->mode = GDK_CC_MODE_STD_CMAP;
368       break;
369       
370     default:
371       cc->mode = GDK_CC_MODE_UNDEFINED;
372       break;
373     }
374   
375   /* previous palette */
376   
377   if (cc->num_palette)
378     g_free (cc->palette);
379   
380   if (cc->fast_dither)
381     g_free (cc->fast_dither);
382   
383   /* clear hash table if present */
384   
385   if (cc->color_hash)
386     {
387       g_hash_table_foreach (cc->color_hash,
388                             free_hash_entry,
389                             NULL);
390       g_hash_table_destroy (cc->color_hash);
391       cc->color_hash = g_hash_table_new (hash_color, compare_colors);
392     }
393   
394   cc->palette = NULL;
395   cc->num_palette = 0;
396   cc->fast_dither = NULL;
397 }
398
399 GdkColorContext *
400 gdk_color_context_new (GdkVisual   *visual,
401                        GdkColormap *colormap)
402 {
403   GdkColorContextPrivate *ccp;
404   gint use_private_colormap = FALSE; /* XXX: maybe restore full functionality later? */
405   GdkColorContext *cc;
406   gint retry_count;
407   GdkColormap *default_colormap;
408   
409   g_assert (visual != NULL);
410   g_assert (colormap != NULL);
411   
412   ccp = g_new (GdkColorContextPrivate, 1);
413   cc = (GdkColorContext *) ccp;
414   cc->visual = visual;
415   cc->colormap = colormap;
416   cc->clut = NULL;
417   cc->cmap = NULL;
418   cc->mode = GDK_CC_MODE_UNDEFINED;
419   cc->need_to_free_colormap = FALSE;
420   
421   cc->color_hash = NULL;
422   cc->palette = NULL;
423   cc->num_palette = 0;
424   cc->fast_dither = NULL;
425   
426   default_colormap = gdk_colormap_get_system ();
427   
428   retry_count = 0;
429   
430   while (retry_count < 2)
431     {
432       /* Only create a private colormap if the visual found isn't equal
433        * to the default visual and we don't have a private colormap,
434        * -or- if we are instructed to create a private colormap (which
435        * never is the case for XmHTML).
436        */
437       
438       if (use_private_colormap
439           || ((cc->visual != gdk_visual_get_system ()) /* default visual? */
440               && (GDK_COLORMAP_WIN32COLORMAP (colormap) ==
441                   GDK_COLORMAP_WIN32COLORMAP (default_colormap))))
442         {
443           g_warning ("gdk_color_context_new: non-default visual detected, "
444                      "using private colormap");
445           
446           cc->colormap = gdk_colormap_new (cc->visual, FALSE);
447           
448           cc->need_to_free_colormap =
449             (GDK_COLORMAP_WIN32COLORMAP (colormap)
450              != GDK_COLORMAP_WIN32COLORMAP (default_colormap));
451         }
452       
453       switch (visual->type)
454         {
455         case GDK_VISUAL_STATIC_GRAY:
456         case GDK_VISUAL_GRAYSCALE:
457           GDK_NOTE (COLOR_CONTEXT,
458                     g_message ("gdk_color_context_new: visual class is %s\n",
459                                (visual->type == GDK_VISUAL_STATIC_GRAY) ?
460                                "GDK_VISUAL_STATIC_GRAY" :
461                                "GDK_VISUAL_GRAYSCALE"));
462           
463           if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
464             init_bw (cc);
465           else
466             init_gray (cc);
467           
468           break;
469           
470         case GDK_VISUAL_TRUE_COLOR: /* shifts */
471           GDK_NOTE (COLOR_CONTEXT,
472                     g_message ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
473           
474           init_true_color (cc);
475           break;
476           
477         case GDK_VISUAL_STATIC_COLOR:
478         case GDK_VISUAL_PSEUDO_COLOR:
479           GDK_NOTE (COLOR_CONTEXT,
480                     g_message ("gdk_color_context_new: visual class is %s\n",
481                                (visual->type == GDK_VISUAL_STATIC_COLOR) ?
482                                "GDK_VISUAL_STATIC_COLOR" :
483                                "GDK_VISUAL_PSEUDO_COLOR"));
484           
485           init_color (cc);
486           break;
487           
488         default:
489           g_assert_not_reached ();
490         }
491       
492       if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
493         {
494           use_private_colormap = TRUE;
495           retry_count++;
496         }
497       else
498         break;
499     }
500   
501   /* no. of colors allocated yet */
502   
503   cc->num_allocated = 0;
504   
505   GDK_NOTE (COLOR_CONTEXT,
506             g_message ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
507                        cc->visual->depth, cc->num_colors));
508   
509   return (GdkColorContext *) cc;
510 }
511
512 GdkColorContext *
513 gdk_color_context_new_mono (GdkVisual   *visual,
514                             GdkColormap *colormap)
515 {
516   GdkColorContextPrivate *ccp;
517   GdkColorContext *cc;
518   
519   g_assert (visual != NULL);
520   g_assert (colormap != NULL);
521   
522   cc = g_new (GdkColorContext, 1);
523   ccp = (GdkColorContextPrivate *) cc;
524   cc->visual = visual;
525   cc->colormap = colormap;
526   cc->clut = NULL;
527   cc->cmap = NULL;
528   cc->mode = GDK_CC_MODE_UNDEFINED;
529   cc->need_to_free_colormap = FALSE;
530   
531   init_bw (cc);
532   
533   return (GdkColorContext *) cc;
534 }
535
536 /* This doesn't currently free black/white, hmm... */
537
538 void
539 gdk_color_context_free (GdkColorContext *cc)
540 {
541   g_assert (cc != NULL);
542   
543   if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
544       || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
545     {
546       gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
547       g_free (cc->clut);
548     }
549   else if (cc->clut != NULL)
550     {
551       gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
552       g_free (cc->clut);
553     }
554   
555   if (cc->cmap != NULL)
556     g_free (cc->cmap);
557   
558   if (cc->need_to_free_colormap)
559     gdk_colormap_unref (cc->colormap);
560   
561   /* free any palette that has been associated with this GdkColorContext */
562
563   init_palette (cc);
564   
565   g_free (cc);
566 }
567
568 gulong
569 gdk_color_context_get_pixel (GdkColorContext *cc,
570                              gushort          red,
571                              gushort          green,
572                              gushort          blue,
573                              gint            *failed)
574 {
575   GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
576   g_assert (cc != NULL);
577   g_assert (failed != NULL);
578   
579   *failed = FALSE;
580   
581   switch (cc->mode)
582     {
583     case GDK_CC_MODE_BW:
584     {
585       gdouble value;
586       
587       value = (red / 65535.0 * 0.30
588                + green / 65535.0 * 0.59
589                + blue / 65535.0 * 0.11);
590       
591       if (value > 0.5)
592         return cc->white_pixel;
593       
594       return cc->black_pixel;
595     }
596     
597     case GDK_CC_MODE_MY_GRAY:
598     {
599       gulong ired, igreen, iblue;
600       
601       red   = red * 0.30 + green * 0.59 + blue * 0.11;
602       green = 0;
603       blue  = 0;
604       
605       if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
606         ired = ccp->std_cmap.red_max;
607       
608       ired *= ccp->std_cmap.red_mult;
609       
610       if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
611         igreen = ccp->std_cmap.green_max;
612       
613       igreen *= ccp->std_cmap.green_mult;
614       
615       if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
616         iblue = ccp->std_cmap.blue_max;
617       
618       iblue *= ccp->std_cmap.blue_mult;
619       
620       if (cc->clut != NULL)
621         return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
622       
623       return ccp->std_cmap.base_pixel + ired + igreen + iblue;
624     }
625     
626     case GDK_CC_MODE_TRUE:
627     {
628       gulong ired, igreen, iblue;
629       
630       if (cc->clut == NULL)
631         {
632           red   >>= 16 - cc->bits.red;
633           green >>= 16 - cc->bits.green;
634           blue  >>= 16 - cc->bits.blue;
635           
636           ired   = (red << cc->shifts.red) & cc->masks.red;
637           igreen = (green << cc->shifts.green) & cc->masks.green;
638           iblue  = (blue << cc->shifts.blue) & cc->masks.blue;
639           
640           return ired | igreen | iblue;
641         }
642       
643       ired   = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
644       igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
645       iblue  = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
646       
647       return ired | igreen | iblue;
648     }
649     
650     case GDK_CC_MODE_PALETTE:
651       return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);
652       
653     case GDK_CC_MODE_STD_CMAP:
654     default:
655     {
656       GdkColor color;
657       GdkColor *result = NULL;
658       
659       color.red   = red;
660       color.green = green;
661       color.blue  = blue;
662
663       if (cc->color_hash)
664         result = g_hash_table_lookup (cc->color_hash, &color);
665       
666       if (!result)
667         {
668           color.red   = red;
669           color.green = green;
670           color.blue  = blue;
671           color.pixel = 0;
672           
673           if (!gdk_color_alloc (cc->colormap, &color))
674             *failed = TRUE;
675           else
676             {
677               GdkColor *cnew;
678               
679               /* XXX: the following comment comes directly from
680                * XCC.c.  I don't know if it is relevant for
681                * gdk_color_alloc() as it is for XAllocColor()
682                * - Federico
683                */
684               /*
685                * I can't figure this out entirely, but it *is* possible
686                * that XAllocColor succeeds, even if the number of
687                * allocations we've made exceeds the number of available
688                * colors in the current colormap. And therefore it
689                * might be necessary for us to resize the CLUT.
690                */
691               
692               if (cc->num_allocated == cc->max_colors)
693                 {
694                   cc->max_colors *= 2;
695                   
696                   GDK_NOTE (COLOR_CONTEXT,
697                             g_message ("gdk_color_context_get_pixel: "
698                                        "resizing CLUT to %i entries\n",
699                                        cc->max_colors));
700                   
701                   cc->clut = g_realloc (cc->clut,
702                                         cc->max_colors * sizeof (gulong));
703                 }
704               
705               /* Key and value are the same color structure */
706               
707               cnew = g_new (GdkColor, 1);
708               *cnew = color;
709               
710               if (!cc->color_hash)
711                 cc->color_hash = g_hash_table_new (hash_color, compare_colors);
712               g_hash_table_insert (cc->color_hash, cnew, cnew);
713               
714               cc->clut[cc->num_allocated] = color.pixel;
715               cc->num_allocated++;
716               return color.pixel;
717             }
718         }
719       
720       return result->pixel;
721     }
722     }
723 }
724
725 void
726 gdk_color_context_get_pixels (GdkColorContext *cc,
727                               gushort         *reds,
728                               gushort         *greens,
729                               gushort         *blues,
730                               gint             ncolors,
731                               gulong          *colors,
732                               gint            *nallocated)
733 {
734   gint i, k, idx;
735   gint cmapsize, ncols = 0, nopen = 0, counter = 0;
736   gint bad_alloc = FALSE;
737   gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
738   GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
739 #ifdef G_ENABLE_DEBUG  
740   gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
741 #endif
742   g_assert (cc != NULL);
743   g_assert (reds != NULL);
744   g_assert (greens != NULL);
745   g_assert (blues != NULL);
746   g_assert (colors != NULL);
747   g_assert (nallocated != NULL);
748   
749   memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
750   memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
751   memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
752   
753   /* Will only have a value if used by the progressive image loader */
754   
755   ncols = *nallocated;
756   
757   *nallocated = 0;
758   
759   /* First allocate all pixels */
760   
761   for (i = 0; i < ncolors; i++)
762     {
763       /* colors[i] is only zero if the pixel at that location hasn't
764        * been allocated yet.  This is a sanity check required for proper
765        * color allocation by the progressive image loader
766        */
767       
768       if (colors[i] == 0)
769         {
770           defs[i].red   = reds[i];
771           defs[i].green = greens[i];
772           defs[i].blue  = blues[i];
773           
774           colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i],
775                                                    &bad_alloc);
776           
777           /* successfully allocated, store it */
778           
779           if (!bad_alloc)
780             {
781               defs[i].pixel = colors[i];
782               allocated[ncols++] = colors[i];
783             }
784           else
785             failed[nopen++] = i;
786         }
787     }
788   
789   *nallocated = ncols;
790   
791   /* all colors available, all done */
792   
793   if ((ncols == ncolors) || (nopen == 0))
794     {
795       GDK_NOTE (COLOR_CONTEXT,
796                 g_message ("gdk_color_context_get_pixels: got all %i colors; "
797                            "(%i colors allocated so far)\n", ncolors, cc->num_allocated));
798       
799       return;
800     }
801   
802   /* The fun part.  We now try to allocate the colors we couldn't allocate
803    * directly.  The first step will map a color onto its nearest color
804    * that has been allocated (either by us or someone else).  If any colors
805    * remain unallocated, we map these onto the colors that we have allocated
806    * ourselves.
807    */
808   
809   /* read up to MAX_IMAGE_COLORS colors of the current colormap */
810   
811   cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
812   
813   /* see if the colormap has any colors to read */
814   
815   if (cmapsize < 0)
816     {
817       g_warning ("gdk_color_context_get_pixels: oops!  no colors available, "
818                  "your images will look *really* ugly.");
819       
820       return;
821     }
822   
823 #ifdef G_ENABLE_DEBUG
824   exact_col = ncols;
825 #endif
826   
827   /* initialize pixels */
828   
829   for (i = 0; i < cmapsize; i++)
830     {
831       cmap[i].pixel = i;
832       cmap[i].red = cmap[i].green = cmap[i].blue = 0;
833     }
834   
835   /* read the colormap */
836   
837   my_x_query_colors (cc->colormap, cmap, cmapsize);
838   
839   /* get a close match for any unallocated colors */
840   
841   counter = nopen;
842   nopen = 0;
843   idx = 0;
844   
845   do
846     {
847       gint d, j, mdist, close, ri, gi, bi;
848       gint rd, gd, bd;
849       
850       i = failed[idx];
851       
852       mdist = 0x1000000;
853       close = -1;
854       
855       /* Store these vals.  Small performance increase as this skips three
856        * indexing operations in the loop code.
857        */
858       
859       ri = reds[i];
860       gi = greens[i];
861       bi = blues[i];
862       
863       /* Walk all colors in the colormap and see which one is the
864        * closest.  Uses plain least squares.
865        */
866       
867       for (j = 0; (j < cmapsize) && (mdist != 0); j++)
868         {
869           /* Don't replace these by shifts; the sign may get clobbered */
870           
871           rd = (ri - cmap[j].red) / 256;
872           gd = (gi - cmap[j].green) / 256;
873           bd = (bi - cmap[j].blue) / 256;
874           
875           d = rd * rd + gd * gd + bd * bd;
876           
877           if (d < mdist)
878             {
879               close = j;
880               mdist = d;
881             }
882         }
883       
884       if (close != -1)
885         {
886           rd = cmap[close].red;
887           gd = cmap[close].green;
888           bd = cmap[close].blue;
889           
890           /* allocate */
891           
892           colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
893           
894           /* store */
895           
896           if (!bad_alloc)
897             {
898               defs[i] = cmap[close];
899               defs[i].pixel = colors[i];
900               allocated[ncols++] = colors[i];
901 #ifdef G_ENABLE_DEBUG
902               close_col++;
903 #endif
904             } else
905               failed[nopen++] = i;
906         } else
907           failed[nopen++] = i;
908       /* deal with in next stage if allocation failed */
909     }
910   while (++idx < counter);
911   
912   *nallocated = ncols;
913   
914   /* This is the maximum no. of allocated colors.  See also the nopen == 0
915    * note above.
916    */
917   
918   if ((ncols == ncolors) || (nopen == 0))
919     {
920       GDK_NOTE (COLOR_CONTEXT,
921                 g_message ("gdk_color_context_get_pixels: got %i colors, %i exact and "
922                            "%i close (%i colors allocated so far)\n",
923                            ncolors, exact_col, close_col, cc->num_allocated));
924       
925       return;
926     }
927   
928   /* Now map any remaining unallocated pixels into the colors we did get */
929   
930   idx = 0;
931   
932   do
933     {
934       gint d, mdist, close, ri, gi, bi;
935       gint j, rd, gd, bd;
936       
937       i = failed[idx];
938       
939       mdist = 0x1000000;
940       close = -1;
941       
942       /* store */
943       
944       ri = reds[i];
945       gi = greens[i];
946       bi = blues[i];
947       
948       /* search allocated colors */
949       
950       for (j = 0; (j < ncols) && (mdist != 0); j++)
951         {
952           k = allocated[j];
953           
954           /* Don't replace these by shifts; the sign may get clobbered */
955           
956           rd = (ri - defs[k].red) / 256;
957           gd = (gi - defs[k].green) / 256;
958           bd = (bi - defs[k].blue) / 256;
959           
960           d = rd * rd + gd * gd + bd * bd;
961           
962           if (d < mdist)
963             {
964               close = k;
965               mdist = d;
966             }
967         }
968       
969       if (close < 0)
970         {
971           /* too bad, map to black */
972           
973           defs[i].pixel = cc->black_pixel;
974           defs[i].red = defs[i].green = defs[i].blue = 0;
975 #ifdef G_ENABLE_DEBUG
976           black_col++;
977 #endif
978         }
979       else
980         {
981           defs[i] = defs[close];
982 #ifdef G_ENABLE_DEBUG
983           subst_col++;
984 #endif
985         }
986       
987       colors[i] = defs[i].pixel;
988     }
989   while (++idx < nopen);
990   
991   GDK_NOTE (COLOR_CONTEXT,
992             g_message ("gdk_color_context_get_pixels: got %i colors, %i exact, %i close, "
993                        "%i substituted, %i to black (%i colors allocated so far)\n",
994                        ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
995 }
996
997 void
998 gdk_color_context_get_pixels_incremental (GdkColorContext *cc,
999                                           gushort         *reds,
1000                                           gushort         *greens,
1001                                           gushort         *blues,
1002                                           gint             ncolors,
1003                                           gint            *used,
1004                                           gulong          *colors,
1005                                           gint            *nallocated)
1006 {
1007   gint i, k, idx;
1008   gint cmapsize, ncols = 0, nopen = 0, counter = 0;
1009   gint bad_alloc = FALSE;
1010   gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
1011   GdkColor defs[MAX_IMAGE_COLORS], cmap[MAX_IMAGE_COLORS];
1012 #ifdef G_ENABLE_DEBUG  
1013   gint exact_col = 0, subst_col = 0, close_col = 0, black_col = 0;
1014 #endif  
1015   
1016   g_assert (cc != NULL);
1017   g_assert (reds != NULL);
1018   g_assert (greens != NULL);
1019   g_assert (blues != NULL);
1020   g_assert (used != NULL);
1021   g_assert (colors != NULL);
1022   g_assert (nallocated != NULL);
1023   
1024   memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
1025   memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
1026   memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));
1027   
1028   /* Will only have a value if used by the progressive image loader */
1029   
1030   ncols = *nallocated;
1031   
1032   *nallocated = 0;
1033   
1034   /* First allocate all pixels */
1035   
1036   for (i = 0; i < ncolors; i++)
1037     {
1038       /* used[i] is only -1 if the pixel at that location hasn't
1039        * been allocated yet.  This is a sanity check required for proper
1040        * color allocation by the progressive image loader.
1041        * When colors[i] == 0 it indicates the slot is available for
1042        * allocation.
1043        */
1044       
1045       if (used[i] != FALSE)
1046         {
1047           if (colors[i] == 0)
1048             {
1049               defs[i].red   = reds[i];
1050               defs[i].green = greens[i];
1051               defs[i].blue  = blues[i];
1052               
1053               colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i], &bad_alloc);
1054               
1055               /* successfully allocated, store it */
1056               
1057               if (!bad_alloc)
1058                 {
1059                   defs[i].pixel = colors[i];
1060                   allocated[ncols++] = colors[i];
1061                 }
1062               else
1063                 failed[nopen++] = i;
1064             }
1065 #ifdef DEBUG
1066           else
1067             GDK_NOTE (COLOR_CONTEXT,
1068                       g_message ("gdk_color_context_get_pixels_incremental: "
1069                                  "pixel at slot %i already allocated, skipping\n", i));
1070 #endif
1071         }
1072     }
1073   
1074   *nallocated = ncols;
1075   
1076   if ((ncols == ncolors) || (nopen == 0))
1077     {
1078       GDK_NOTE (COLOR_CONTEXT,
1079                 g_message ("gdk_color_context_get_pixels_incremental: got all %i colors "
1080                            "(%i colors allocated so far)\n",
1081                            ncolors, cc->num_allocated));
1082       
1083       return;
1084     }
1085   
1086   cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
1087   
1088   if (cmapsize < 0)
1089     {
1090       g_warning ("gdk_color_context_get_pixels_incremental: oops!  "
1091                  "No colors available images will look *really* ugly.");
1092       return;
1093     }
1094   
1095 #ifdef G_ENABLE_DEBUG
1096   exact_col = ncols;
1097 #endif
1098   
1099   /* initialize pixels */
1100   
1101   for (i = 0; i < cmapsize; i++)
1102     {
1103       cmap[i].pixel = i;
1104       cmap[i].red = cmap[i].green = cmap[i].blue = 0;
1105     }
1106   
1107   /* read */
1108   
1109   my_x_query_colors (cc->colormap, cmap, cmapsize);
1110   
1111   /* now match any unallocated colors */
1112   
1113   counter = nopen;
1114   nopen = 0;
1115   idx = 0;
1116   
1117   do
1118     {
1119       gint d, j, mdist, close, ri, gi, bi;
1120       gint rd, gd, bd;
1121       
1122       i = failed[idx];
1123       
1124       mdist = 0x1000000;
1125       close = -1;
1126       
1127       /* store */
1128       
1129       ri = reds[i];
1130       gi = greens[i];
1131       bi = blues[i];
1132       
1133       for (j = 0; (j < cmapsize) && (mdist != 0); j++)
1134         {
1135           /* Don't replace these by shifts; the sign may get clobbered */
1136           
1137           rd = (ri - cmap[j].red) / 256;
1138           gd = (gi - cmap[j].green) / 256;
1139           bd = (bi - cmap[j].blue) / 256;
1140           
1141           d = rd * rd + gd * gd + bd * bd;
1142           
1143           if (d < mdist)
1144             {
1145               close = j;
1146               mdist = d;
1147             }
1148         }
1149       
1150       if (close != -1)
1151         {
1152           rd = cmap[close].red;
1153           gd = cmap[close].green;
1154           bd = cmap[close].blue;
1155           
1156           /* allocate */
1157           
1158           colors[i] = gdk_color_context_get_pixel (cc, rd, gd, bd, &bad_alloc);
1159           
1160           /* store */
1161           
1162           if (!bad_alloc)
1163             {
1164               defs[i] = cmap[close];
1165               defs[i].pixel = colors[i];
1166               allocated[ncols++] = colors[i];
1167 #ifdef G_ENABLE_DEBUG
1168               close_col++;
1169 #endif
1170             }
1171           else
1172             failed[nopen++] = i;
1173         }
1174       else
1175         failed[nopen++] = i;
1176       /* deal with in next stage if allocation failed */
1177     }
1178   while (++idx < counter);
1179   
1180   *nallocated = ncols;
1181   
1182   if ((ncols == ncolors) || (nopen == 0))
1183     {
1184       GDK_NOTE (COLOR_CONTEXT,
1185                 g_message ("gdk_color_context_get_pixels_incremental: "
1186                            "got %i colors, %i exact and %i close "
1187                            "(%i colors allocated so far)\n",
1188                            ncolors, exact_col, close_col, cc->num_allocated));
1189       
1190       return;
1191     }
1192   
1193   /* map remaining unallocated pixels into colors we did get */
1194   
1195   idx = 0;
1196   
1197   do
1198     {
1199       gint d, mdist, close, ri, gi, bi;
1200       gint j, rd, gd, bd;
1201       
1202       i = failed[idx];
1203       
1204       mdist = 0x1000000;
1205       close = -1;
1206       
1207       ri = reds[i];
1208       gi = greens[i];
1209       bi = blues[i];
1210       
1211       /* search allocated colors */
1212       
1213       for (j = 0; (j < ncols) && (mdist != 0); j++)
1214         {
1215           k = allocated[j];
1216           
1217           /* downscale */
1218           /* Don't replace these by shifts; the sign may get clobbered */
1219           
1220           rd = (ri - defs[k].red) / 256;
1221           gd = (gi - defs[k].green) / 256;
1222           bd = (bi - defs[k].blue) / 256;
1223           
1224           d = rd * rd + gd * gd + bd * bd;
1225           
1226           if (d < mdist)
1227             {
1228               close = k;
1229               mdist = d;
1230             }
1231         }
1232       
1233       if (close < 0)
1234         {
1235           /* too bad, map to black */
1236           
1237           defs[i].pixel = cc->black_pixel;
1238           defs[i].red = defs[i].green = defs[i].blue = 0;
1239 #ifdef G_ENABLE_DEBUG
1240           black_col++;
1241 #endif
1242         }
1243       else
1244         {
1245           defs[i] = defs[close];
1246 #ifdef G_ENABLE_DEBUG
1247           subst_col++;
1248 #endif
1249         }
1250       
1251       colors[i] = defs[i].pixel;
1252     }
1253   while (++idx < nopen);
1254   
1255   GDK_NOTE (COLOR_CONTEXT,
1256             g_message ("gdk_color_context_get_pixels_incremental: "
1257                        "got %i colors, %i exact, %i close, %i substituted, %i to black "
1258                        "(%i colors allocated so far)\n",
1259                        ncolors, exact_col, close_col, subst_col, black_col, cc->num_allocated));
1260 }
1261
1262 gint
1263 gdk_color_context_query_color (GdkColorContext *cc,
1264                                GdkColor        *color)
1265 {
1266   return gdk_color_context_query_colors (cc, color, 1);
1267 }
1268
1269 gint
1270 gdk_color_context_query_colors (GdkColorContext *cc,
1271                                 GdkColor        *colors,
1272                                 gint             num_colors)
1273 {
1274   gint i;
1275   GdkColor *tc;
1276   
1277   g_assert (cc != NULL);
1278   g_assert (colors != NULL);
1279   
1280   switch (cc->mode)
1281     {
1282     case GDK_CC_MODE_BW:
1283       for (i = 0, tc = colors; i < num_colors; i++, tc++)
1284         {
1285           if (tc->pixel == cc->white_pixel)
1286             tc->red = tc->green = tc->blue = 65535;
1287           else
1288             tc->red = tc->green = tc->blue = 0;
1289         }
1290       break;
1291       
1292     case GDK_CC_MODE_TRUE:
1293       if (cc->clut == NULL)
1294         for (i = 0, tc = colors; i < num_colors; i++, tc++)
1295           {
1296             tc->red   = ((tc->pixel & cc->masks.red) >> cc->shifts.red) << (16 - cc->bits.red);
1297             tc->green = ((tc->pixel & cc->masks.green) >> cc->shifts.green) << (16 - cc->bits.green);
1298             tc->blue  = ((tc->pixel & cc->masks.blue) >> cc->shifts.blue) << (16 - cc->bits.blue);
1299           }
1300       else
1301         {
1302           my_x_query_colors (cc->colormap, colors, num_colors);
1303           return 1;
1304         }
1305       break;
1306       
1307     case GDK_CC_MODE_STD_CMAP:
1308     default:
1309       if (cc->cmap == NULL)
1310         {
1311           my_x_query_colors (cc->colormap, colors, num_colors);
1312           return 1;
1313         }
1314       else
1315         {
1316           gint first, last, half;
1317           gulong half_pixel;
1318           
1319           for (i = 0, tc = colors; i < num_colors; i++)
1320             {
1321               first = 0;
1322               last = cc->num_colors - 1;
1323               
1324               while (first <= last)
1325                 {
1326                   half = (first + last) / 2;
1327                   half_pixel = cc->cmap[half].pixel;
1328                   
1329                   if (tc->pixel == half_pixel)
1330                     {
1331                       tc->red   = cc->cmap[half].red;
1332                       tc->green = cc->cmap[half].green;
1333                       tc->blue  = cc->cmap[half].blue;
1334                       first = last + 1; /* false break */
1335                     }
1336                   else
1337                     {
1338                       if (tc->pixel > half_pixel)
1339                         first = half + 1;
1340                       else
1341                         last = half - 1;
1342                     }
1343                 }
1344             }
1345           return 1;
1346         }
1347       break;
1348     }
1349   return 1;
1350 }
1351
1352 gint
1353 gdk_color_context_add_palette (GdkColorContext *cc,
1354                                GdkColor        *palette,
1355                                gint             num_palette)
1356 {
1357   gint i, j, erg;
1358   gushort r, g, b;
1359   gulong pixel[1];
1360   
1361   g_assert (cc != NULL);
1362   
1363   /* initialize this palette (will also erase previous palette as well) */
1364   
1365   init_palette (cc);
1366   
1367   /* restore previous mode if we aren't adding a new palette */
1368   
1369   if (num_palette == 0)
1370     return 0;
1371   
1372   /* copy incoming palette */
1373   
1374   cc->palette = g_new0(GdkColor, num_palette);
1375   
1376   j = 0;
1377   
1378   for (i = 0; i < num_palette; i++)
1379     {
1380       erg = 0;
1381       pixel[0] = 0;
1382       
1383       /* try to allocate this color */
1384       
1385       r = palette[i].red;
1386       g = palette[i].green;
1387       b = palette[i].blue;
1388       
1389       gdk_color_context_get_pixels (cc, &r, &g, &b, 1, pixel, &erg);
1390       
1391       /* only store if we succeed */
1392       
1393       if (erg)
1394         {
1395           /* store in palette */
1396           
1397           cc->palette[j].red   = r;
1398           cc->palette[j].green = g;
1399           cc->palette[j].blue  = b;
1400           cc->palette[j].pixel = pixel[0];
1401           
1402           /* move to next slot */
1403           
1404           j++;
1405         }
1406     }
1407   
1408   /* resize to fit */
1409   
1410   if (j != num_palette)
1411     cc->palette = g_realloc (cc->palette, j * sizeof (GdkColor));
1412   
1413   /* clear the hash table, we don't use it when dithering */
1414   
1415   if (cc->color_hash)
1416     {
1417       g_hash_table_foreach (cc->color_hash,
1418                             free_hash_entry,
1419                             NULL);
1420       g_hash_table_destroy (cc->color_hash);
1421       cc->color_hash = NULL;
1422     }
1423   
1424   /* store real palette size */
1425   
1426   cc->num_palette = j;
1427   
1428   /* switch to palette mode */
1429   
1430   cc->mode = GDK_CC_MODE_PALETTE;
1431   
1432   /* sort palette */
1433   
1434   qsort (cc->palette, cc->num_palette, sizeof (GdkColor), pixel_sort);
1435   
1436   cc->fast_dither = NULL;
1437   
1438   return j;
1439 }
1440
1441 void
1442 gdk_color_context_init_dither (GdkColorContext *cc)
1443 {
1444   gint rr, gg, bb, err, erg, erb;
1445   gint success = FALSE;
1446   
1447   g_assert (cc != NULL);
1448   
1449   /* now we can initialize the fast dither matrix */
1450   
1451   if (cc->fast_dither == NULL)
1452     cc->fast_dither = g_new (GdkColorContextDither, 1);
1453   
1454   /* Fill it.  We ignore unsuccessful allocations, they are just mapped
1455    * to black instead */
1456   
1457   for (rr = 0; rr < 32; rr++)
1458     for (gg = 0; gg < 32; gg++)
1459       for (bb = 0; bb < 32; bb++)
1460         {
1461           err = (rr << 3) | (rr >> 2);
1462           erg = (gg << 3) | (gg >> 2);
1463           erb = (bb << 3) | (bb >> 2);
1464           
1465           cc->fast_dither->fast_rgb[rr][gg][bb] =
1466             gdk_color_context_get_index_from_palette (cc, &err, &erg, &erb, &success);
1467           cc->fast_dither->fast_err[rr][gg][bb] = err;
1468           cc->fast_dither->fast_erg[rr][gg][bb] = erg;
1469           cc->fast_dither->fast_erb[rr][gg][bb] = erb;
1470         }
1471 }
1472
1473 void
1474 gdk_color_context_free_dither (GdkColorContext *cc)
1475 {
1476   g_assert (cc != NULL);
1477   
1478   if (cc->fast_dither)
1479     g_free (cc->fast_dither);
1480   
1481   cc->fast_dither = NULL;
1482 }
1483
1484 gulong
1485 gdk_color_context_get_pixel_from_palette (GdkColorContext *cc,
1486                                           gushort         *red,
1487                                           gushort         *green,
1488                                           gushort         *blue,
1489                                           gint            *failed)
1490 {
1491   gulong pixel = 0;
1492   gint dif, dr, dg, db, j = -1;
1493   gint mindif = 0x7fffffff;
1494   gint err = 0, erg = 0, erb = 0;
1495   gint i;
1496   
1497   g_assert (cc != NULL);
1498   g_assert (red != NULL);
1499   g_assert (green != NULL);
1500   g_assert (blue != NULL);
1501   g_assert (failed != NULL);
1502   
1503   *failed = FALSE;
1504   
1505   for (i = 0; i < cc->num_palette; i++)
1506     {
1507       dr = *red - cc->palette[i].red;
1508       dg = *green - cc->palette[i].green;
1509       db = *blue - cc->palette[i].blue;
1510       
1511       dif = dr * dr + dg * dg + db * db;
1512       
1513       if (dif < mindif)
1514         {
1515           mindif = dif;
1516           j = i;
1517           pixel = cc->palette[i].pixel;
1518           err = dr;
1519           erg = dg;
1520           erb = db;
1521           
1522           if (mindif == 0)
1523             break;
1524         }
1525     }
1526   
1527   /* we failed to map onto a color */
1528   
1529   if (j == -1)
1530     *failed = TRUE;
1531   else
1532     {
1533       *red   = ABS (err);
1534       *green = ABS (erg);
1535       *blue  = ABS (erb);
1536     }
1537   
1538   return pixel;
1539 }
1540
1541 guchar
1542 gdk_color_context_get_index_from_palette (GdkColorContext *cc,
1543                                           gint            *red,
1544                                           gint            *green,
1545                                           gint            *blue,
1546                                           gint            *failed)
1547 {
1548   gint dif, dr, dg, db, j = -1;
1549   gint mindif = 0x7fffffff;
1550   gint err = 0, erg = 0, erb = 0;
1551   gint i;
1552   
1553   g_assert (cc != NULL);
1554   g_assert (red != NULL);
1555   g_assert (green != NULL);
1556   g_assert (blue != NULL);
1557   g_assert (failed != NULL);
1558   
1559   *failed = FALSE;
1560   
1561   for (i = 0; i < cc->num_palette; i++)
1562     {
1563       dr = *red - cc->palette[i].red;
1564       dg = *green - cc->palette[i].green;
1565       db = *blue - cc->palette[i].blue;
1566       
1567       dif = dr * dr + dg * dg + db * db;
1568       
1569       if (dif < mindif)
1570         {
1571           mindif = dif;
1572           j = i;
1573           err = dr;
1574           erg = dg;
1575           erb = db;
1576           
1577           if (mindif == 0)
1578             break;
1579         }
1580     }
1581   
1582   /* we failed to map onto a color */
1583   
1584   if (j == -1)
1585     {
1586       *failed = TRUE;
1587       j = 0;
1588     }
1589   else
1590     {
1591       /* return error fractions */
1592       
1593       *red   = err;
1594       *green = erg;
1595       *blue  = erb;
1596     }
1597   
1598   return j;
1599 }