gdkcc-x11.c 38.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 */

/* Color Context module
 * Copyright 1994,1995 John L. Cwikla
 * Copyright (C) 1997 by Ripley Software Development
 * Copyright (C) 1997 by Federico Mena (port to Gtk/Gdk)
 */

/* Copyright 1994,1995 John L. Cwikla
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of John L. Cwikla or
 * Wolfram Research, Inc not be used in advertising or publicity
 * pertaining to distribution of the software without specific, written
 * prior permission.  John L. Cwikla and Wolfram Research, Inc make no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * John L. Cwikla and Wolfram Research, Inc disclaim all warranties with
 * regard to this software, including all implied warranties of
 * merchantability and fitness, in no event shall John L. Cwikla or
 * Wolfram Research, Inc be liable for any special, indirect or
 * consequential damages or any damages whatsoever resulting from loss of
 * use, data or profits, whether in an action of contract, negligence or
 * other tortious action, arising out of or in connection with the use or
 * performance of this software.
 *
 * Author:
 *  John L. Cwikla
 *  X Programmer
 *  Wolfram Research Inc.
 *
 *  cwikla@wri.com
 */


#include <X11/Xlib.h>
#include <stdlib.h>
#include <string.h>
#include "gdk.h"
#include "gdkprivate.h"
#include "gdkx.h"


#define MAX_IMAGE_COLORS 256


static guint
69
hash_color (gpointer key)
70
{
71
  GdkColor *color = key;
72

73
  return (color->red * 33023 + color->green * 30013 + color->blue * 27011);
74 75 76
}

static gint
77 78
compare_colors (gpointer a,
		gpointer b)
79
{
80 81
  GdkColor *aa = a;
  GdkColor *bb = b;
82

83
  return ((aa->red == bb->red) && (aa->green == bb->green) && (aa->blue == bb->blue));
84 85 86
}

static void
87 88 89
free_hash_entry (gpointer key,
		 gpointer value,
		 gpointer user_data)
90
{
91
  g_free (key); /* key and value are the same GdkColor */
92 93 94
}

static int
95
pixel_sort (const void *a, const void *b)
96
{
97
  return ((GdkColor *) a)->pixel - ((GdkColor *) b)->pixel;
98 99 100 101 102 103 104
}

/* XXX: This function does an XQueryColors() the hard way, because there is
 * no corresponding function in Gdk.
 */

static void
105 106 107
my_x_query_colors (GdkColormap *colormap,
		   GdkColor    *colors,
		   gint         ncolors)
108
{
109 110
  XColor *xcolors;
  gint    i;
111

112 113 114
  xcolors = g_new (XColor, ncolors);
  for (i = 0; i < ncolors; i++)
    xcolors[i].pixel = colors[i].pixel;
115

116
  XQueryColors (gdk_display, GDK_COLORMAP_XCOLORMAP (colormap), xcolors, ncolors);
117

118 119 120 121 122 123
  for (i = 0; i < ncolors; i++)
    {
      colors[i].red   = xcolors[i].red;
      colors[i].green = xcolors[i].green;
      colors[i].blue  = xcolors[i].blue;
    }
124

125
  g_free (xcolors);
126 127 128
}

static void
129
query_colors (GdkColorContext *cc)
130
{
131 132 133
  gint i;
  GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
  cc->cmap = g_new (GdkColor, cc->num_colors);
134

135 136
  for (i = 0; i < cc->num_colors; i++)
    cc->cmap[i].pixel = cc->clut ? cc->clut[i] : ccp->std_cmap.base_pixel + i;
137

138
  my_x_query_colors (cc->colormap, cc->cmap, cc->num_colors);
139
	
140
  qsort (cc->cmap, cc->num_colors, sizeof (GdkColor), pixel_sort);
141 142 143
}

static void
144
init_bw (GdkColorContext *cc)
145
{
146
  GdkColor color;
147

148
  g_warning ("init_bw: failed to allocate colors, falling back to black and white");
149

150
  cc->mode = GDK_CC_MODE_BW;
151

152 153 154 155 156 157
  color.red = color.green = color.blue = 0;
  
  if (!gdk_color_alloc (cc->colormap, &color))
    cc->black_pixel = 0;
  else
    cc->black_pixel = color.pixel;
158

159 160 161 162 163 164
  color.red = color.green = color.blue = 0xffff;
  
  if (!gdk_color_alloc (cc->colormap, &color))
    cc->white_pixel = cc->black_pixel ? 0 : 1;
  else
    cc->white_pixel = color.pixel;
165

166
  cc->num_colors = 2;
167 168 169
}

static void
170
init_gray (GdkColorContext *cc)
171
{
172 173 174 175
  GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
  GdkColor *clrs, *cstart;
  gint i;
  gdouble dinc;
176
	
177
  cc->num_colors = GDK_VISUAL_XVISUAL (cc->visual)->map_entries;
178

179 180
  cc->clut = g_new (gulong, cc->num_colors);
  cstart = g_new (GdkColor, cc->num_colors);
181 182 183

retrygray:

184
  dinc = 65535.0 / (cc->num_colors - 1);
185

186
  clrs = cstart;
187

188 189 190
  for (i = 0; i < cc->num_colors; i++)
    {
      clrs->red = clrs->green = clrs->blue = dinc * i;
191

192 193 194
      if (!gdk_color_alloc (cc->colormap, clrs))
	{
	  gdk_colors_free (cc->colormap, cc->clut, i, 0);
195

196
	  cc->num_colors /= 2;
197

198 199 200 201 202 203 204 205 206 207
	  if (cc->num_colors > 1)
	    goto retrygray;
	  else
	    {
	      g_free (cc->clut);
	      cc->clut = NULL;
	      init_bw (cc);
	      g_free (cstart);
	      return;
	    }
208 209
	}

210 211
      cc->clut[i] = clrs++->pixel;
    }
212

213
  g_free (cstart);
214

215 216 217 218 219 220 221 222 223
  /* XXX: is this the right thing to do? */
  ccp->std_cmap.colormap = GDK_COLORMAP_XCOLORMAP (cc->colormap);
  ccp->std_cmap.base_pixel = 0;
  ccp->std_cmap.red_max = cc->num_colors - 1;
  ccp->std_cmap.green_max = 0;
  ccp->std_cmap.blue_max = 0;
  ccp->std_cmap.red_mult = 1;
  ccp->std_cmap.green_mult = 0;
  ccp->std_cmap.blue_mult = 0;
224

225 226
  cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
  cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
227

228 229 230
  query_colors (cc);

  cc->mode = GDK_CC_MODE_MY_GRAY;
231 232 233
}

static void
234
init_color (GdkColorContext *cc)
235
{
236 237
  GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
  gint cubeval;
238

239 240 241 242
  cubeval = 1;
  while ((cubeval * cubeval * cubeval) < GDK_VISUAL_XVISUAL (cc->visual)->map_entries)
    cubeval++;
  cubeval--;
243

244
  cc->num_colors = cubeval * cubeval * cubeval;
245

246 247 248 249 250 251 252
  ccp->std_cmap.red_max    = cubeval - 1;
  ccp->std_cmap.green_max  = cubeval - 1;
  ccp->std_cmap.blue_max   = cubeval - 1;
  ccp->std_cmap.red_mult   = cubeval * cubeval;
  ccp->std_cmap.green_mult = cubeval;
  ccp->std_cmap.blue_mult  = 1;
  ccp->std_cmap.base_pixel = 0;
253

254 255 256
  cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
  cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
  cc->num_colors = DisplayCells (ccp->xdisplay, gdk_screen);
257

258
  /* a CLUT for storing allocated pixel indices */
259

260 261
  cc->max_colors = cc->num_colors;
  cc->clut = g_new (gulong, cc->max_colors);
262

263 264
  for (cubeval = 0; cubeval < cc->max_colors; cubeval++)
    cc->clut[cubeval] = cubeval;
265

266
  query_colors (cc);
267

268
  cc->mode = GDK_CC_MODE_STD_CMAP;
269 270 271 272
}


static void
273
init_true_color (GdkColorContext *cc)
274
{
275 276
  GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
  gulong rmask, gmask, bmask;
277

278
  cc->mode = GDK_CC_MODE_TRUE;
279

280
  /* Red */
281

282
  rmask = cc->masks.red = cc->visual->red_mask;
283

284 285
  cc->shifts.red = 0;
  cc->bits.red = 0;
286

287 288 289 290 291
  while (!(rmask & 1))
    {
      rmask >>= 1;
      cc->shifts.red++;
    }
292

293 294 295 296 297
  while (rmask & 1)
    {
      rmask >>= 1;
      cc->bits.red++;
    }
298

299
  /* Green */
300

301
  gmask = cc->masks.green = cc->visual->green_mask;
302

303 304
  cc->shifts.green = 0;
  cc->bits.green = 0;
305

306 307 308 309 310
  while (!(gmask & 1))
    {
      gmask >>= 1;
      cc->shifts.green++;
    }
311

312 313 314 315 316
  while (gmask & 1)
    {
      gmask >>= 1;
      cc->bits.green++;
    }
317

318
  /* Blue */
319

320
  bmask = cc->masks.blue = cc->visual->blue_mask;
321

322 323
  cc->shifts.blue = 0;
  cc->bits.blue = 0;
324

325 326 327 328 329
  while (!(bmask & 1))
    {
      bmask >>= 1;
      cc->shifts.blue++;
    }
330

331 332 333 334 335
  while (bmask & 1)
    {
      bmask >>= 1;
      cc->bits.blue++;
    }
336

337 338 339
  cc->num_colors = (cc->visual->red_mask | cc->visual->green_mask | cc->visual->blue_mask) + 1;
  cc->white_pixel = WhitePixel (ccp->xdisplay, gdk_screen);
  cc->black_pixel = BlackPixel (ccp->xdisplay, gdk_screen);
340 341 342
}

static void
343
init_direct_color (GdkColorContext *cc)
344
{
345 346 347 348 349 350 351
  gint n, count;
  GdkColor *clrs, *cstart;
  gulong rval, gval, bval;
  gulong *rtable;
  gulong *gtable;
  gulong *btable;
  gdouble dinc;
352

353
  init_true_color (cc); /* for shift stuff */
354

355 356 357
  rval = cc->visual->red_mask >> cc->shifts.red;
  gval = cc->visual->green_mask >> cc->shifts.green;
  bval = cc->visual->blue_mask >> cc->shifts.blue;
358

359 360 361
  rtable = g_new (gulong, rval + 1);
  gtable = g_new (gulong, gval + 1);
  btable = g_new (gulong, bval + 1);
362

363 364
  cc->max_entry = MAX (rval, gval);
  cc->max_entry = MAX (cc->max_entry, bval);
365

366 367
  cstart = g_new (GdkColor, cc->max_entry + 1);
  cc->clut = g_new (gulong, cc->max_entry + 1);
368 369 370

retrydirect:

371 372
  for (n = 0; n < rval; n++)
    rtable[n] = rval ? (65535.0 / rval * n) : 0;
373

374 375
  for (n = 0; n < gval; n++)
    gtable[n] = gval ? (65535.0 / gval * n) : 0;
376

377 378
  for (n = 0; n < bval; n++)
    btable[n] = bval ? (65535.0 / bval * n) : 0;
379

380 381
  cc->max_entry = MAX (rval, gval);
  cc->max_entry = MAX (cc->max_entry, bval);
382

383 384 385
  count = 0;
  clrs = cstart;
  cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);
386

387 388 389
  for (n = 0; n < cc->max_entry; n++)
    {
      dinc = (double) n / cc->max_entry;
390

391 392 393
      clrs->red   = rtable[(int) (dinc * rval)];
      clrs->green = gtable[(int) (dinc * gval)];
      clrs->blue  = btable[(int) (dinc * bval)];
394

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
      if (gdk_color_alloc (cc->colormap, clrs))
	{
	  cc->clut[count++] = clrs->pixel;
	  clrs++;
	}
      else
	{
	  gdk_colors_free (cc->colormap, cc->clut, count, 0);

	  rval >>= 1;
	  gval >>= 1;
	  bval >>= 1;

	  cc->masks.red   = (cc->masks.red >> 1) & cc->visual->red_mask;
	  cc->masks.green = (cc->masks.green >> 1) & cc->visual->green_mask;
	  cc->masks.blue  = (cc->masks.blue >> 1) & cc->visual->blue_mask;

	  cc->shifts.red++;
	  cc->shifts.green++;
	  cc->shifts.blue++;

	  cc->bits.red--;
	  cc->bits.green--;
	  cc->bits.blue--;

	  cc->num_colors = (rval + 1) * (gval + 1) * (bval + 1);

	  if (cc->num_colors >1)
	    goto retrydirect;
	  else
	    {
	      g_free (cc->clut);
	      cc->clut = NULL;
	      init_bw (cc);
	      break;
	    }
431
	}
432
    }
433

434 435 436
  /* Update allocated color count; original num_colors is max_entry, which
   * is not necessarily the same as the really allocated number of colors.
   */
437

438
  cc->num_colors = count;
439

440 441 442 443
  g_free (rtable);
  g_free (gtable);
  g_free (btable);
  g_free (cstart);
444 445 446
}

static void
447
init_palette (GdkColorContext *cc)
448
{
449
  /* restore correct mode for this cc */
450
	
451 452 453 454 455 456 457 458 459
  switch (cc->visual->type)
    {
    case GDK_VISUAL_STATIC_GRAY:
    case GDK_VISUAL_GRAYSCALE:
      if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
	cc->mode = GDK_CC_MODE_BW;
      else
	cc->mode = GDK_CC_MODE_MY_GRAY;
      break;
460

461 462 463 464
    case GDK_VISUAL_TRUE_COLOR:
    case GDK_VISUAL_DIRECT_COLOR:
      cc->mode = GDK_CC_MODE_TRUE;
      break;
465

466 467 468 469
    case GDK_VISUAL_STATIC_COLOR:
    case GDK_VISUAL_PSEUDO_COLOR:
      cc->mode = GDK_CC_MODE_STD_CMAP;
      break;
470

471 472 473 474
    default:
      cc->mode = GDK_CC_MODE_UNDEFINED;
      break;
    }
475

476
  /* previous palette */
477

478 479
  if (cc->num_palette)
    g_free (cc->palette);
480

481 482 483 484 485 486 487 488
  if (cc->fast_dither)
    g_free (cc->fast_dither);

  /* clear hash table if present */

  if (cc->color_hash)
    {
      /* XXX: quick-and-dirty way to remove everything */
489

490 491 492 493 494 495 496
      g_hash_table_destroy (cc->color_hash);
      cc->color_hash = g_hash_table_new (hash_color, compare_colors);
    }

  cc->palette = NULL;
  cc->num_palette = 0;
  cc->fast_dither = NULL;
497 498 499
}

GdkColorContext *
500 501
gdk_color_context_new (GdkVisual   *visual,
		       GdkColormap *colormap)
502
{
503 504 505 506 507 508 509 510
  GdkColorContextPrivate *ccp;
  gint use_private_colormap = FALSE; /* XXX: maybe restore full functionality later? */
  GdkColorContext *cc;
  gint retry_count;
  GdkColormap *default_colormap;

  g_assert (visual != NULL);
  g_assert (colormap != NULL);
511 512 513

  ccp = g_new (GdkColorContextPrivate, 1);
  cc = (GdkColorContext *) ccp;
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  ccp->xdisplay = gdk_display;
  cc->visual = visual;
  cc->colormap = colormap;
  cc->clut = NULL;
  cc->cmap = NULL;
  cc->mode = GDK_CC_MODE_UNDEFINED;
  cc->need_to_free_colormap = FALSE;

  cc->color_hash = NULL;
  cc->palette = NULL;
  cc->num_palette = 0;
  cc->fast_dither = NULL;

  default_colormap = gdk_colormap_get_system ();

  retry_count = 0;

  while (retry_count < 2)
    {
      /* Only create a private colormap if the visual found isn't equal
       * to the default visual and we don't have a private colormap,
       * -or- if we are instructed to create a private colormap (which
       * never is the case for XmHTML).
       */

      if (use_private_colormap
	  || ((cc->visual != gdk_visual_get_system ()) /* default visual? */
	      && (GDK_COLORMAP_XCOLORMAP (colormap) == GDK_COLORMAP_XCOLORMAP (default_colormap))))
	{
	  g_warning ("gdk_color_context_new: non-default visual detected, "
		     "using private colormap");

	  cc->colormap = gdk_colormap_new (cc->visual, FALSE);

	  cc->need_to_free_colormap = (GDK_COLORMAP_XCOLORMAP (colormap)
				       != GDK_COLORMAP_XCOLORMAP (default_colormap));
	}
551

552 553 554 555
      switch (visual->type)
	{
	case GDK_VISUAL_STATIC_GRAY:
	case GDK_VISUAL_GRAYSCALE:
556
	  GDK_NOTE (COLOR_CONTEXT,
557
	    g_print ("gdk_color_context_new: visual class is %s\n",
558 559
		     (visual->type == GDK_VISUAL_STATIC_GRAY) ?
		     "GDK_VISUAL_STATIC_GRAY" :
560
		     "GDK_VISUAL_GRAYSCALE"));
561 562 563 564 565 566 567 568 569

	  if (GDK_VISUAL_XVISUAL (cc->visual)->map_entries == 2)
	    init_bw (cc);
	  else
	    init_gray (cc);

	  break;

	case GDK_VISUAL_TRUE_COLOR: /* shifts */
570 571
	  GDK_NOTE (COLOR_CONTEXT,
	    g_print ("gdk_color_context_new: visual class is GDK_VISUAL_TRUE_COLOR\n"));
572 573 574 575 576

	  init_true_color (cc);
	  break;

	case GDK_VISUAL_DIRECT_COLOR: /* shifts and fake CLUT */
577 578
	  GDK_NOTE (COLOR_CONTEXT,
	    g_print ("gdk_color_context_new: visual class is GDK_VISUAL_DIRECT_COLOR\n"));
579 580 581 582 583 584

	  init_direct_color (cc);
	  break;

	case GDK_VISUAL_STATIC_COLOR:
	case GDK_VISUAL_PSEUDO_COLOR:
585
	  GDK_NOTE (COLOR_CONTEXT,
586
	    g_print ("gdk_color_context_new: visual class is %s\n",
587 588
		     (visual->type == GDK_VISUAL_STATIC_COLOR) ?
		     "GDK_VISUAL_STATIC_COLOR" :
589
		     "GDK_VISUAL_PSEUDO_COLOR"));
590 591 592 593 594 595 596

	  init_color (cc);
	  break;

	default:
	  g_assert_not_reached ();
	}
597

598 599 600 601
      if ((cc->mode == GDK_CC_MODE_BW) && (cc->visual->depth > 1))
	{
	  use_private_colormap = TRUE;
	  retry_count++;
602
	}
603 604 605
      else
	break;
    }
606

607
  /* no. of colors allocated yet */
608

609
  cc->num_allocated = 0;
610

611
  GDK_NOTE (COLOR_CONTEXT,
612
    g_print ("gdk_color_context_new: screen depth is %i, no. of colors is %i\n",
613
	     cc->visual->depth, cc->num_colors));
614

615
  /* check if we need to initialize a hash table */
616

617 618
  if ((cc->mode == GDK_CC_MODE_STD_CMAP) || (cc->mode == GDK_CC_MODE_UNDEFINED))
    cc->color_hash = g_hash_table_new (hash_color, compare_colors);
619

620
  return (GdkColorContext *) cc;
621 622 623
}

GdkColorContext *
624 625
gdk_color_context_new_mono (GdkVisual   *visual,
			    GdkColormap *colormap)
626
{
627 628
  GdkColorContextPrivate *ccp;
  GdkColorContext *cc;
629

630 631
  g_assert (visual != NULL);
  g_assert (colormap != NULL);
632

633 634 635 636 637 638 639 640 641
  cc = g_new (GdkColorContext, 1);
  ccp = (GdkColorContextPrivate *) cc;
  ccp->xdisplay = gdk_display;
  cc->visual = visual;
  cc->colormap = colormap;
  cc->clut = NULL;
  cc->cmap = NULL;
  cc->mode = GDK_CC_MODE_UNDEFINED;
  cc->need_to_free_colormap = FALSE;
642

643
  init_bw (cc);
644

645
  return (GdkColorContext *) cc;
646 647 648 649 650
}

/* This doesn't currently free black/white, hmm... */

void
651
gdk_color_context_free (GdkColorContext *cc)
652
{
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
  g_assert (cc != NULL);

  if ((cc->visual->type == GDK_VISUAL_STATIC_COLOR)
      || (cc->visual->type == GDK_VISUAL_PSEUDO_COLOR))
    {
      gdk_colors_free (cc->colormap, cc->clut, cc->num_allocated, 0);
      g_free (cc->clut);
    }
  else if (cc->clut != NULL)
    {
      gdk_colors_free (cc->colormap, cc->clut, cc->num_colors, 0);
      g_free (cc->clut);
    }

  if (cc->cmap != NULL)
    g_free (cc->cmap);

  if (cc->need_to_free_colormap)
    gdk_colormap_unref (cc->colormap);

  /* free any palette that has been associated with this GdkColorContext */

  init_palette (cc);

  if (cc->color_hash)
    {
      g_hash_table_foreach (cc->color_hash,
			    free_hash_entry,
			    NULL);
      g_hash_table_destroy (cc->color_hash);
    }

  g_free (cc);
686 687 688
}

gulong
689 690 691 692 693
gdk_color_context_get_pixel (GdkColorContext *cc,
			     gushort          red,
			     gushort          green,
			     gushort          blue,
			     gint            *failed)
694
{
695 696 697
  GdkColorContextPrivate *ccp = (GdkColorContextPrivate *) cc;
  g_assert (cc != NULL);
  g_assert (failed != NULL);
698

699
  *failed = FALSE;
700

701 702 703 704 705
  switch (cc->mode)
    {
    case GDK_CC_MODE_BW:
      {
	gdouble value;
706

707 708 709
	value = (red / 65535.0 * 0.30
		 + green / 65535.0 * 0.59
		 + blue / 65535.0 * 0.11);
710

711 712
	if (value > 0.5)
	  return cc->white_pixel;
713

714 715
	return cc->black_pixel;
      }
716

717 718 719
    case GDK_CC_MODE_MY_GRAY:
      {
	gulong ired, igreen, iblue;
720

721 722 723
	red   = red * 0.30 + green * 0.59 + blue * 0.11;
	green = 0;
	blue  = 0;
724

725
	if ((ired = red * (ccp->std_cmap.red_max + 1) / 0xffff) > ccp->std_cmap.red_max)
726
	  ired = ccp->std_cmap.red_max;
727

728
	ired *= ccp->std_cmap.red_mult;
729

730
	if ((igreen = green * (ccp->std_cmap.green_max + 1) / 0xffff) > ccp->std_cmap.green_max)
731
	  igreen = ccp->std_cmap.green_max;
732

733
	igreen *= ccp->std_cmap.green_mult;
734

735
	if ((iblue = blue * (ccp->std_cmap.blue_max + 1) / 0xffff) > ccp->std_cmap.blue_max)
736
	  iblue = ccp->std_cmap.blue_max;
737

738
	iblue *= ccp->std_cmap.blue_mult;
739

740 741
	if (cc->clut != NULL)
	  return cc->clut[ccp->std_cmap.base_pixel + ired + igreen + iblue];
742

743 744
	return ccp->std_cmap.base_pixel + ired + igreen + iblue;
      }
745

746 747 748
    case GDK_CC_MODE_TRUE:
      {
	gulong ired, igreen, iblue;
749

750 751 752 753 754
	if (cc->clut == NULL)
	  {
	    red   >>= 16 - cc->bits.red;
	    green >>= 16 - cc->bits.green;
	    blue  >>= 16 - cc->bits.blue;
755

756 757 758
	    ired   = (red << cc->shifts.red) & cc->masks.red;
	    igreen = (green << cc->shifts.green) & cc->masks.green;
	    iblue  = (blue << cc->shifts.blue) & cc->masks.blue;
759

760 761
	    return ired | igreen | iblue;
	  }
762

763 764 765
	ired   = cc->clut[red * cc->max_entry / 65535] & cc->masks.red;
	igreen = cc->clut[green * cc->max_entry / 65535] & cc->masks.green;
	iblue  = cc->clut[blue * cc->max_entry / 65535] & cc->masks.blue;
766

767 768
	return ired | igreen | iblue;
      }
769

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
    case GDK_CC_MODE_PALETTE:
      return gdk_color_context_get_pixel_from_palette (cc, &red, &green, &blue, failed);

    case GDK_CC_MODE_STD_CMAP:
    default:
      {
	GdkColor color;
	GdkColor *result;

	color.red   = red;
	color.green = green;
	color.blue  = blue;

	result = g_hash_table_lookup (cc->color_hash, &color);

	if (!result)
	  {
	    color.red   = red;
	    color.green = green;
	    color.blue  = blue;
	    color.pixel = 0;

	    if (!gdk_color_alloc (cc->colormap, &color))
	      *failed = TRUE;
	    else
	      {
		GdkColor *cnew;
					
		/* XXX: the following comment comes directly from
		 * XCC.c.  I don't know if it is relevant for
		 * gdk_color_alloc() as it is for XAllocColor()
		 * - Federico
		 */
		/*
		 * I can't figure this out entirely, but it *is* possible
		 * that XAllocColor succeeds, even if the number of
		 * allocations we've made exceeds the number of available
		 * colors in the current colormap. And therefore it
		 * might be necessary for us to resize the CLUT.
		 */
810

811 812 813
		if (cc->num_allocated == cc->max_colors)
		  {
		    cc->max_colors *= 2;
814

815
		    GDK_NOTE (COLOR_CONTEXT,
816
		      g_print ("gdk_color_context_get_pixel: "
817
			       "resizing CLUT to %i entries\n",
818
			       cc->max_colors));
819

820 821 822
		    cc->clut = g_realloc (cc->clut,
					  cc->max_colors * sizeof (gulong));
		  }
823

824
		/* Key and value are the same color structure */
825

826 827 828
		cnew = g_new (GdkColor, 1);
		*cnew = color;
		g_hash_table_insert (cc->color_hash, cnew, cnew);
829

830 831 832 833 834
		cc->clut[cc->num_allocated] = color.pixel;
		cc->num_allocated++;
		return color.pixel;
	      }
	  }
835
			
836 837 838
	return result->pixel;
      }
    }
839 840 841
}

void
842 843 844 845 846 847 848
gdk_color_context_get_pixels (GdkColorContext *cc,
			      gushort         *reds,
			      gushort         *greens,
			      gushort         *blues,
			      gint             ncolors,
			      gulong          *colors,
			      gint            *nallocated)
849
{
850 851 852 853 854
  gint i, k, idx;
  gint cmapsize, ncols = 0, nopen = 0, counter = 0;
  gint bad_alloc = FALSE;
  gint failed[MAX_IMAGE_COLORS], allocated[MAX_IMAGE_COLORS];
  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;
857
#endif
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
  g_assert (cc != NULL);
  g_assert (reds != NULL);
  g_assert (greens != NULL);
  g_assert (blues != NULL);
  g_assert (colors != NULL);
  g_assert (nallocated != NULL);

  memset (defs, 0, MAX_IMAGE_COLORS * sizeof (GdkColor));
  memset (failed, 0, MAX_IMAGE_COLORS * sizeof (gint));
  memset (allocated, 0, MAX_IMAGE_COLORS * sizeof (gint));

  /* Will only have a value if used by the progressive image loader */

  ncols = *nallocated;

  *nallocated = 0;

  /* First allocate all pixels */

  for (i = 0; i < ncolors; i++)
    {
      /* colors[i] is only zero if the pixel at that location hasn't
       * been allocated yet.  This is a sanity check required for proper
       * color allocation by the progressive image loader
       */

      if (colors[i] == 0)
	{
	  defs[i].red   = reds[i];
	  defs[i].green = greens[i];
	  defs[i].blue  = blues[i];

	  colors[i] = gdk_color_context_get_pixel (cc, reds[i], greens[i], blues[i],
						   &bad_alloc);

	  /* successfully allocated, store it */

	  if (!bad_alloc)
	    {
	      defs[i].pixel = colors[i];
	      allocated[ncols++] = colors[i];
	    }
	  else
	    failed[nopen++] = i;
902
	}
903
    }
904

905
  *nallocated = ncols;
906

907
  /* all colors available, all done */
908

909 910
  if ((ncols == ncolors) || (nopen == 0))
    {
911
      GDK_NOTE (COLOR_CONTEXT,
912
	g_print ("gdk_color_context_get_pixels: got all %i colors; "
913
		 "(%i colors allocated so far)\n", ncolors, cc->num_allocated));
914

915 916
      return;
    }
917

918 919 920 921 922 923
  /* The fun part.  We now try to allocate the colors we couldn't allocate
   * directly.  The first step will map a color onto its nearest color
   * that has been allocated (either by us or someone else).  If any colors
   * remain unallocated, we map these onto the colors that we have allocated
   * ourselves.
   */
924

925
  /* read up to MAX_IMAGE_COLORS colors of the current colormap */
926

927
  cmapsize = MIN (cc->num_colors, MAX_IMAGE_COLORS);
928