gdkrgb.c 290 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GDK - The GIMP Drawing Kit
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19 20
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* For more information on GdkRgb, see http://www.levien.com/gdkrgb/
21

22
   Raph Levien <raph@acm.org>
23
   */
24

25
/*
26
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
27 28 29 30 31
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

32
#include "config.h"
33
#include <math.h>
34 35
#include <stdlib.h>
#include <string.h>
36 37 38

#define ENABLE_GRAYSCALE

39
#include "gdkinternals.h"	/* _gdk_windowing_get_bits_for_depth() */
40

Owen Taylor's avatar
Owen Taylor committed
41
#include "gdkrgb.h"
42
#include "gdkscreen.h"
43
#include "gdkalias.h"
44
#include <glib/gprintf.h>
45

46 47
typedef struct _GdkRgbInfo     GdkRgbInfo;
typedef struct _GdkRgbCmapInfo GdkRgbCmapInfo;
48

49
typedef void (*GdkRgbConvFunc) (GdkRgbInfo *image_info, GdkImage *image,
50 51
				gint x0, gint y0,
				gint width, gint height,
52
				const guchar *buf, int rowstride,
53 54 55
				gint x_align, gint y_align,
				GdkRgbCmap *cmap);

56
#define STAGE_ROWSTRIDE (GDK_SCRATCH_IMAGE_WIDTH * 3)
57

58
/* Some of these fields should go, as they're not being used at all. (?)
59
 */
60 61 62 63
struct _GdkRgbInfo
{
  GdkVisual *visual;
  GdkColormap *cmap;
64

65 66 67 68 69
  guint nred_shades;
  guint ngreen_shades;
  guint nblue_shades;
  guint ngray_shades;
  guint nreserved;
70

71 72 73
  guint bpp;
  gint cmap_alloced;
  gdouble gamma;
74

75 76 77
  /* Generally, the stage buffer is used to convert 32bit RGB, gray,
     and indexed images into 24 bit packed RGB. */
  guchar *stage_buf;
78

79
  GdkRgbCmap *gray_cmap;
80

81
  gboolean dith_default;
82

83 84 85
  gboolean bitmap; /* set true if in 1 bit per pixel mode */
  GdkGC *own_gc;

86 87 88
  /* Convert functions */
  GdkRgbConvFunc conv;
  GdkRgbConvFunc conv_d;
89

90 91
  GdkRgbConvFunc conv_32;
  GdkRgbConvFunc conv_32_d;
92

93 94
  GdkRgbConvFunc conv_gray;
  GdkRgbConvFunc conv_gray_d;
95

96 97
  GdkRgbConvFunc conv_indexed;
  GdkRgbConvFunc conv_indexed_d;
98 99 100

  guchar *colorcube;
  guchar *colorcube_d;
101 102 103 104 105 106 107 108 109 110 111 112 113 114

  /* We need to track LUT's for pairs of GdkRgbInfo / GdkRgbCmap, so we
   * keep a list of pointers to GdkRgbCmapInfo on both structures so we
   * can remove as necessary when freeing a GdkRgbInfo or GdkRgbCmap
   */
  GSList *cmap_info_list;
};

struct _GdkRgbCmapInfo
{
  GdkRgbInfo *image_info;
  GdkRgbCmap *cmap;

  guchar lut[256];		/* For 8-bit modes */
115 116
};

117 118
static GdkRgbCmapInfo *gdk_rgb_cmap_get_info (GdkRgbCmap *cmap, GdkRgbInfo *image_info);

119
static const char gdk_rgb_key[] = "gdk-rgb-info";
120 121
static GQuark gdk_rgb_quark = 0;

122 123 124 125 126 127 128
static gboolean gdk_rgb_install_cmap = FALSE;
static gint gdk_rgb_min_colors = 5 * 5 * 5;
static gboolean gdk_rgb_verbose = FALSE;

static gint
gdk_rgb_cmap_fail (const char *msg, GdkColormap *cmap, gulong *pixels)
{
Manish Singh's avatar
Manish Singh committed
129
  GdkColor free_colors[256];
130 131
  gint n_free;
  gint i;
132

133
#ifdef VERBOSE
134
  g_print ("%s", msg);
135 136 137 138
#endif
  n_free = 0;
  for (i = 0; i < 256; i++)
    if (pixels[i] < 256)
Manish Singh's avatar
Manish Singh committed
139
      free_colors[n_free++].pixel = pixels[i];
140
  if (n_free)
Manish Singh's avatar
Manish Singh committed
141
    gdk_colormap_free_colors (cmap, free_colors, n_free);
142 143 144 145
  return 0;
}

static void
146 147
gdk_rgb_make_colorcube (GdkRgbInfo *image_info, gulong *pixels,
			gint nr, gint ng, gint nb)
148 149 150
{
  guchar rt[16], gt[16], bt[16];
  gint i;
151

152
  image_info->colorcube = g_new (guchar, 4096);
153 154 155 156 157 158
  for (i = 0; i < 16; i++)
    {
      rt[i] = ng * nb * ((i * 17 * (nr - 1) + 128) >> 8);
      gt[i] = nb * ((i * 17 * (ng - 1) + 128) >> 8);
      bt[i] = ((i * 17 * (nb - 1) + 128) >> 8);
    }
159

160 161
  for (i = 0; i < 4096; i++)
    {
162
      image_info->colorcube[i] = pixels[rt[i >> 8] + gt[(i >> 4) & 0x0f] + bt[i & 0x0f]];
163
#ifdef VERBOSE
Sven Neumann's avatar
Sven Neumann committed
164
      g_print ("%03x %02x %x %x %x\n", i, image_info->colorcube[i], rt[i >> 8], gt[(i >> 4) & 0x0f], bt[i & 0x0f]);
165 166 167 168 169 170
#endif
    }
}

/* this is the colorcube suitable for dithering */
static void
171 172
gdk_rgb_make_colorcube_d (GdkRgbInfo *image_info, gulong *pixels,
			  gint nr, gint ng, gint nb)
173 174 175
{
  gint r, g, b;
  gint i;
176

177
  image_info->colorcube_d = g_new (guchar, 512);
178 179 180 181 182
  for (i = 0; i < 512; i++)
    {
      r = MIN (nr - 1, i >> 6);
      g = MIN (ng - 1, (i >> 3) & 7);
      b = MIN (nb - 1, i & 7);
183
      image_info->colorcube_d[i] = pixels[(r * ng + g) * nb + b];
184 185 186 187 188
    }
}

/* Try installing a color cube of the specified size.
   Make the colorcube and return TRUE on success */
189
static gboolean
190 191
gdk_rgb_try_colormap (GdkRgbInfo *image_info, gboolean force,
		      gint nr, gint ng, gint nb)
192 193 194 195 196 197 198 199 200 201 202 203
{
  gint r, g, b;
  gint ri, gi, bi;
  gint r0, g0, b0;
  GdkColormap *cmap;
  GdkColor color;
  gulong pixels[256];
  gint i;
  gint d2;
  gint colors_needed;
  gint idx;
  gint best[256];
204
  GdkScreen *screen;
205

206
  if (!force && nr * ng * nb < gdk_rgb_min_colors)
207
    return FALSE;
208

209 210
  screen = gdk_visual_get_screen (image_info->visual);

211
  if (image_info->cmap)
212 213
    cmap = image_info->cmap;
  else
214
    cmap = gdk_screen_get_system_colormap (screen);
215

216 217 218 219 220 221
  colors_needed = nr * ng * nb;
  for (i = 0; i < 256; i++)
    {
      best[i] = 192;
      pixels[i] = 256;
    }
222

223
#ifndef GAMMA
224
  if (cmap == gdk_screen_get_system_colormap (screen))
225 226 227 228 229 230 231 232 233 234 235 236 237 238
    /* find color cube colors that are already present */
    for (i = 0; i < MIN (256, cmap->size); i++)
      {
	r = cmap->colors[i].red >> 8;
	g = cmap->colors[i].green >> 8;
	b = cmap->colors[i].blue >> 8;
	ri = (r * (nr - 1) + 128) >> 8;
	gi = (g * (ng - 1) + 128) >> 8;
	bi = (b * (nb - 1) + 128) >> 8;
	r0 = ri * 255 / (nr - 1);
	g0 = gi * 255 / (ng - 1);
	b0 = bi * 255 / (nb - 1);
	idx = ((ri * nr) + gi) * nb + bi;
	d2 = (r - r0) * (r - r0) + (g - g0) * (g - g0) + (b - b0) * (b - b0);
Manish Singh's avatar
Manish Singh committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	if (d2 < best[idx])
	  {
	    if (pixels[idx] < 256)
	      {
		color.pixel = pixels[idx];
		gdk_colormap_free_colors (cmap, &color, 1);
	      }
	    else
	      colors_needed--;
	    color = cmap->colors[i];
	    if (!gdk_colormap_alloc_color (cmap, &color, FALSE, FALSE))
	      return gdk_rgb_cmap_fail ("error allocating system color\n",
					cmap, pixels);
	    pixels[idx] = color.pixel; /* which is almost certainly i */
	    best[idx] = d2;
	  }
255 256
      }
#endif
257

258 259 260 261 262 263 264 265 266
  for (r = 0, i = 0; r < nr; r++)
    for (g = 0; g < ng; g++)
      for (b = 0; b < nb; b++, i++)
	{
	  if (pixels[i] == 256)
	    {
	      color.red = r * 65535 / (nr - 1);
	      color.green = g * 65535 / (ng - 1);
	      color.blue = b * 65535 / (nb - 1);
267

268 269 270 271 272
#ifdef GAMMA
	      color.red = 65535 * pow (color.red / 65535.0, 0.5);
	      color.green = 65535 * pow (color.green / 65535.0, 0.5);
	      color.blue = 65535 * pow (color.blue / 65535.0, 0.5);
#endif
273

274
	      if (!gdk_colormap_alloc_color (cmap, &color, FALSE, force))
275 276
		{
		  char tmp_str[80];
277

278
		  g_snprintf (tmp_str, 80, "%d %d %d colormap failed\n",
279 280 281 282 283 284 285
			   nr, ng, nb);
		  return gdk_rgb_cmap_fail (tmp_str,
					    cmap, pixels);
		}
	      pixels[i] = color.pixel;
	    }
#ifdef VERBOSE
286
	  g_print ("%d: %lx\n", i, pixels[i]);
287 288
#endif
	}
289

290 291 292
  image_info->nred_shades = nr;
  image_info->ngreen_shades = ng;
  image_info->nblue_shades = nb;
293 294
  gdk_rgb_make_colorcube (image_info, pixels, nr, ng, nb);
  gdk_rgb_make_colorcube_d (image_info, pixels, nr, ng, nb);
295 296 297 298 299
  return TRUE;
}

/* Return TRUE on success. */
static gboolean
300
gdk_rgb_do_colormaps (GdkRgbInfo *image_info, gboolean force)
301
{
302
  static const gint sizes[][3] = {
303
    /*    { 6, 7, 6 }, */
304 305 306 307 308 309 310 311 312 313
    { 6, 6, 6 }, 
    { 6, 6, 5 }, 
    { 6, 6, 4 }, 
    { 5, 5, 5 }, 
    { 5, 5, 4 }, 
    { 4, 4, 4 }, 
    { 4, 4, 3 }, 
    { 3, 3, 3 }, 
    { 2, 2, 2 }
  };
314
  static const gint n_sizes = G_N_ELEMENTS (sizes);
315
  gint i;
316

317 318 319 320 321
  /* Try the possible sizes. If the force parameter is set to TRUE
   * and all larger sizes fail, force the larger size to succeed -
   * this will involve allowing closest matches when allocating the
   * colors
   */
322
  for (i = 0; i < n_sizes; i++)
323 324 325
    if (gdk_rgb_try_colormap (image_info,
			      (i == n_sizes - 1 ) && force,
			      sizes[i][0], sizes[i][1], sizes[i][2]))
326 327 328 329
      return TRUE;
  return FALSE;
}

330 331
/* Make a 2 x 2 x 2 colorcube */
static void
332
gdk_rgb_colorcube_222 (GdkRgbInfo *image_info)
333 334 335 336
{
  int i;
  GdkColor color;

337
  image_info->colorcube_d = g_new (guchar, 512);
338 339 340 341 342 343

  for (i = 0; i < 8; i++)
    {
      color.red = ((i & 4) >> 2) * 65535;
      color.green = ((i & 2) >> 1) * 65535;
      color.blue = (i & 1) * 65535;
344
      gdk_colormap_alloc_color (image_info->cmap, &color, FALSE, TRUE);
345
      image_info->colorcube_d[((i & 4) << 4) | ((i & 2) << 2) | (i & 1)] = color.pixel;
346 347 348
    }
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
void
gdk_rgb_set_verbose (gboolean verbose)
{
  gdk_rgb_verbose = verbose;
}

void
gdk_rgb_set_install (gboolean install)
{
  gdk_rgb_install_cmap = install;
}

void
gdk_rgb_set_min_colors (gint min_colors)
{
  gdk_rgb_min_colors = min_colors;
}

/* Return a "score" based on the following criteria (in hex):
368

369
   x000 is the quality - 1 is 1bpp, 2 is 4bpp,
370 371 372
                         4 is 8bpp,
			 7 is 15bpp truecolor, 8 is 16bpp truecolor,
			 9 is 24bpp truecolor.
373
   0x00 is the speed - 1 is the normal case,
374
                       2 means faster than normal
375 376
   00x0 gets a point for being the system visual
   000x gets a point for being pseudocolor
377

378 379 380 381 382 383 384 385
   A caveat: in the 8bpp modes, being the system visual seems to be
   quite important. Thus, all of the 8bpp modes should be ranked at
   the same speed.
*/
static guint32
gdk_rgb_score_visual (GdkVisual *visual)
{
  guint32 quality, speed, sys, pseudo;
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  quality = 0;
  speed = 1;
  sys = 0;
  if (visual->type == GDK_VISUAL_TRUE_COLOR ||
      visual->type == GDK_VISUAL_DIRECT_COLOR)
    {
      if (visual->depth == 24)
	{
	  quality = 9;
	  /* Should test for MSB visual here, and set speed if so. */
	}
      else if (visual->depth == 16)
	quality = 8;
      else if (visual->depth == 15)
	quality = 7;
      else if (visual->depth == 8)
	quality = 4;
    }
405 406
  else if (visual->type == GDK_VISUAL_PSEUDO_COLOR ||
	   visual->type == GDK_VISUAL_STATIC_COLOR)
407 408 409
    {
      if (visual->depth == 8)
	quality = 4;
410 411 412 413
      else if (visual->depth == 4)
	quality = 2;
      else if (visual->depth == 1)
	quality = 1;
414 415 416 417 418 419 420 421 422 423 424 425 426 427
    }
  else if (visual->type == GDK_VISUAL_STATIC_GRAY
#ifdef ENABLE_GRAYSCALE
	   || visual->type == GDK_VISUAL_GRAYSCALE
#endif
	   )
    {
      if (visual->depth == 8)
	quality = 4;
      else if (visual->depth == 4)
	quality = 2;
      else if (visual->depth == 1)
	quality = 1;
    }
428

429 430
  if (quality == 0)
    return 0;
431

432
  sys = (visual == gdk_screen_get_system_visual (gdk_visual_get_screen (visual)));
433

Manish Singh's avatar
Manish Singh committed
434
  pseudo = (visual->type == GDK_VISUAL_PSEUDO_COLOR || visual->type == GDK_VISUAL_TRUE_COLOR);
435

436
  if (gdk_rgb_verbose)
Matthias Clasen's avatar
Matthias Clasen committed
437 438
    g_print ("Visual type = %d, depth = %d, %x:%x:%x%s; score=%x\n",
	     visual->type,
439 440 441 442 443 444 445
	     visual->depth,
	     visual->red_mask,
	     visual->green_mask,
	     visual->blue_mask,
	     sys ? " (system)" : "",
	     (quality << 12) | (speed << 8) | (sys << 4) | pseudo);

446 447 448
  return (quality << 12) | (speed << 8) | (sys << 4) | pseudo;
}

449
static GdkVisual *
450
gdk_rgb_choose_visual (GdkScreen *screen)
451
{
452
  GList *visuals, *tmp_list;
453 454
  guint32 score, best_score;
  GdkVisual *visual, *best_visual;
455

456
  visuals = gdk_screen_list_visuals (screen);
457
  tmp_list = visuals;
458

459
  best_visual = tmp_list->data;
460
  best_score = gdk_rgb_score_visual (best_visual);
461 462
  tmp_list = tmp_list->next;
  while (tmp_list)
463
    {
464
      visual = tmp_list->data;
465 466 467 468 469 470
      score = gdk_rgb_score_visual (visual);
      if (score > best_score)
	{
	  best_score = score;
	  best_visual = visual;
	}
471
      tmp_list = tmp_list->next;
472
    }
473

474 475
  g_list_free (visuals);

476
  return best_visual;
477 478
}

479
static void gdk_rgb_select_conv (GdkRgbInfo *image_info);
480 481

static void
482 483
gdk_rgb_set_gray_cmap (GdkRgbInfo  *image_info,
		       GdkColormap *cmap)
484 485 486
{
  gint i;
  GdkColor color;
Matthias Clasen's avatar
Matthias Clasen committed
487
  gboolean status;
488 489
  gulong pixels[256];
  gint r, g, b, gray;
490

491 492 493 494 495 496
  for (i = 0; i < 256; i++)
    {
      color.pixel = i;
      color.red = i * 257;
      color.green = i * 257;
      color.blue = i * 257;
497
      status = gdk_colormap_alloc_color (cmap, &color, FALSE, TRUE);
498 499
      pixels[i] = color.pixel;
#ifdef VERBOSE
500 501
      g_print ("allocating pixel %d, %x %x %x, result %d\n",
	       color.pixel, color.red, color.green, color.blue, status);
502 503
#endif
    }
504

505 506
  /* Now, we make fake colorcubes - we ultimately just use the pseudocolor
     methods. */
507

508
  image_info->colorcube = g_new (guchar, 4096);
509

510 511 512 513 514 515 516 517 518
  for (i = 0; i < 4096; i++)
    {
      r = (i >> 4) & 0xf0;
      r = r | r >> 4;
      g = i & 0xf0;
      g = g | g >> 4;
      b = (i << 4 & 0xf0);
      b = b | b >> 4;
      gray = (g + ((r + b) >> 1)) >> 1;
519 520 521 522
      image_info->colorcube[i] = pixels[gray];
    }
}

523 524
static void
gdk_rgb_free_info (GdkRgbInfo *image_info)
525
{
526 527
  GSList *tmp_list;
  
528
  g_free (image_info->stage_buf);
529 530 531 532 533
  
  if (image_info->gray_cmap)
    gdk_rgb_cmap_free (image_info->gray_cmap);

  if (image_info->own_gc)
534
    g_object_unref (image_info->own_gc);
535

536
  g_free (image_info->colorcube);
537
  
538
  g_free (image_info->colorcube_d);
539 540 541

  tmp_list = image_info->cmap_info_list;
  while (tmp_list)
542
    {
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
      GdkRgbCmapInfo *cmap_info = tmp_list->data;
      cmap_info->cmap->info_list = g_slist_remove (cmap_info->cmap->info_list, cmap_info);
      g_free (cmap_info);
    }
  g_slist_free (image_info->cmap_info_list);
  
  g_free (image_info);
}

/* Create a GdkRgbInfo for the given visual/colormap pair. If colormap
 * is NULL, it will be determined and stored in image_info->cmap. 
 * In this case, image_info->cmap will have an extra refcount which
 * is owned by the caller. 
 */
static GdkRgbInfo *
gdk_rgb_create_info (GdkVisual *visual, GdkColormap *colormap)
{
  GdkRgbInfo *image_info;
561
  GdkScreen *screen = gdk_visual_get_screen (visual);
562

563
  image_info = g_new0 (GdkRgbInfo, 1);
564

565 566
  image_info->visual = visual;
  image_info->cmap = NULL;
567

568 569 570 571 572
  image_info->nred_shades = 6;
  image_info->ngreen_shades = 6;
  image_info->nblue_shades = 4;
  image_info->ngray_shades = 24;
  image_info->nreserved = 0;
573

574 575 576
  image_info->bpp = 0;
  image_info->cmap_alloced = FALSE;
  image_info->gamma = 1.0;
577

578
  image_info->stage_buf = NULL;
579

580
  image_info->own_gc = NULL;
581

582 583
  image_info->cmap = colormap;

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
  /* We used to use the 2x2x2 color cube for pseudo-color with depths
   * 5, 6, 7 as well but now only use it for depths (3 and) 4 in
   * pseudo-color. The reason for this is that on Win32 we let the
   * user restrict the color allocation for PSEUDO_COLOR visuals
   * (i.e., 256-color mode) and we probably want to do the full
   * gdk_rgb_do_colormaps() if we are doing that. (Though the color
   * sharing code won't really be right.)
   *
   * (The actual usefulness of this user-requested restriction remains
   * to be seen, but the code is there in gdkvisual-win32.c. The
   * thought is that it might occasionally be useful to restrict the
   * palette size in a GTK application in order to reduce color
   * flashing.)
   */
  if ((image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR &&
       image_info->visual->depth <= 4 &&
       image_info->visual->depth >= 3) ||
      (image_info->visual->type == GDK_VISUAL_STATIC_COLOR &&
       image_info->visual->depth < 8 &&
       image_info->visual->depth >= 3))
604 605
    {
      if (!image_info->cmap)
606
	image_info->cmap = g_object_ref (gdk_screen_get_system_colormap (screen));
607 608 609
      
      gdk_rgb_colorcube_222 (image_info);
    }
610 611
  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR
    || image_info->visual->type == GDK_VISUAL_STATIC_COLOR)
612 613
    {
      if (!image_info->cmap &&
614
	  (gdk_rgb_install_cmap || image_info->visual != gdk_screen_get_system_visual (screen)))
615
	{
616 617
	  image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	  image_info->cmap_alloced = TRUE;
618
	}
619
      if (!gdk_rgb_do_colormaps (image_info, image_info->cmap != NULL))
620
	{
621 622 623
	  image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	  image_info->cmap_alloced = TRUE;
	  gdk_rgb_do_colormaps (image_info, TRUE);
624
	}
625 626 627 628 629 630 631
      if (gdk_rgb_verbose)
	g_print ("color cube: %d x %d x %d\n",
		 image_info->nred_shades,
		 image_info->ngreen_shades,
		 image_info->nblue_shades);

      if (!image_info->cmap)
632
	image_info->cmap = g_object_ref (gdk_screen_get_system_colormap (screen));
633
    }
634
#ifdef ENABLE_GRAYSCALE
635 636 637
  else if (image_info->visual->type == GDK_VISUAL_GRAYSCALE)
    {
      if (!image_info->cmap)
638 639 640
	{
	  image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	  image_info->cmap_alloced = TRUE;
641 642 643 644
	}
      
      gdk_rgb_set_gray_cmap (image_info, image_info->cmap);
    }
645
#endif
646 647 648
  else
    {
      if (!image_info->cmap)
649 650 651
	{
	  /* Always install colormap in direct color. */
	  if (image_info->visual->type != GDK_VISUAL_DIRECT_COLOR &&
652
	      image_info->visual == gdk_screen_get_system_visual (screen))
653
	    image_info->cmap = g_object_ref (gdk_screen_get_system_colormap (screen));
654 655 656 657 658 659
	  else
	    {
	      image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	      image_info->cmap_alloced = TRUE;
	    }
	}
660
    }
661

662
  image_info->bitmap = (image_info->visual->depth == 1);
663

664
  image_info->bpp = (_gdk_windowing_get_bits_for_depth (gdk_screen_get_display (screen), image_info->visual->depth) + 7) / 8;
665
  gdk_rgb_select_conv (image_info);
666 667 668 669 670 671 672

  if (!gdk_rgb_quark)
    gdk_rgb_quark = g_quark_from_static_string (gdk_rgb_key);

  g_object_set_qdata_full (G_OBJECT (image_info->cmap), gdk_rgb_quark,
			   image_info, (GDestroyNotify)gdk_rgb_free_info);
  return image_info;
673 674
}

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
void
gdk_rgb_init (void)
{
  static const gint byte_order[1] = { 1 };

  /* check endian sanity */
#if G_BYTE_ORDER == G_BIG_ENDIAN
  if (((char *)byte_order)[0] == 1)
    g_error ("gdk_rgb_init: compiled for big endian, but this is a little endian machine.\n\n");
#else
  if (((char *)byte_order)[0] != 1)
    g_error ("gdk_rgb_init: compiled for little endian, but this is a big endian machine.\n\n");
#endif
}

static GdkRgbInfo *
gdk_rgb_get_info_from_colormap (GdkColormap *cmap)
{
  GdkRgbInfo *image_info;

  if (!gdk_rgb_quark)
    gdk_rgb_quark = g_quark_from_static_string (gdk_rgb_key);

  image_info = g_object_get_qdata (G_OBJECT (cmap), gdk_rgb_quark);
  if (!image_info)
    image_info = gdk_rgb_create_info (gdk_colormap_get_visual (cmap), cmap);

  return image_info;
}

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
static guint32
gdk_rgb_alpha_mask (GdkRgbInfo *image_info)
{
  guint padding;
  
  /* Shifting by >= width-of-type isn't defined in C */
  if (image_info->visual->depth >= 32)
    padding = 0;
  else
    padding = ((~(guint32)0)) << image_info->visual->depth;
	  
  return ~(image_info->visual->red_mask |
	   image_info->visual->green_mask |
	   image_info->visual->blue_mask |
	   padding);
}

722 723 724
static gulong
gdk_rgb_xpixel_from_rgb_internal (GdkColormap *colormap,
				  guint16 r, guint16 g, guint16 b)
725
{
726
  gulong pixel = 0;
727

728 729
  GdkRgbInfo *image_info = gdk_rgb_get_info_from_colormap (colormap);

730 731
  if (image_info->bitmap)
    {
732
      return (r + (g << 1) + b) > 131070;
733 734
    }
  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
735 736 737
    pixel = image_info->colorcube[((r & 0xf000) >> 4) |
				  ((g & 0xf000) >> 8) |
				  ((b & 0xf000) >> 12)];
738
  else if (image_info->visual->depth < 8 &&
739 740
	   image_info->visual->type == GDK_VISUAL_STATIC_COLOR)
    {
741 742 743
      pixel = image_info->colorcube_d[((r & 0x8000) >> 9) |
				      ((g & 0x8000) >> 12) |
				      ((b & 0x8000) >> 15)];
744
    }
745 746
  else if (image_info->visual->type == GDK_VISUAL_TRUE_COLOR ||
	   image_info->visual->type == GDK_VISUAL_DIRECT_COLOR)
747
    {
748 749
      guint32 unused;

750
#ifdef VERBOSE
751 752 753 754 755 756 757
      g_print ("shift, prec: r %d %d g %d %d b %d %d\n",
	       image_info->visual->red_shift,
	       image_info->visual->red_prec,
	       image_info->visual->green_shift,
	       image_info->visual->green_prec,
	       image_info->visual->blue_shift,
	       image_info->visual->blue_prec);
758
#endif
759 760 761 762 763 764 765 766 767
      /* If bits not used for color are used for something other than padding,
       * it's likely alpha, so we set them to 1s.
       */
      unused = ~ (image_info->visual->red_mask | 
		  image_info->visual->green_mask | 
		  image_info->visual->blue_mask |
		  (((~(guint32)0)) << image_info->visual->depth));

      pixel = (unused + ((r >> (16 - image_info->visual->red_prec)) << image_info->visual->red_shift) +
768 769
	       ((g >> (16 - image_info->visual->green_prec)) << image_info->visual->green_shift) +
	       ((b >> (16 - image_info->visual->blue_prec)) << image_info->visual->blue_shift));
770
      pixel |= gdk_rgb_alpha_mask (image_info);
771
    }
772 773 774
  else if (image_info->visual->type == GDK_VISUAL_STATIC_GRAY ||
	   image_info->visual->type == GDK_VISUAL_GRAYSCALE)
    {
775 776
      int gray = r + g * 2 + b;
      return gray >> (18 - image_info->visual->depth);
777
    }
778

779 780 781
  return pixel;
}

782 783 784 785 786 787 788 789
/* convert an rgb value into an X pixel code */
gulong
gdk_rgb_xpixel_from_rgb (guint32 rgb)
{
  guint32 r = rgb & 0xff0000;
  guint32 g = rgb & 0xff00;
  guint32 b = rgb & 0xff;

Owen Taylor's avatar
Owen Taylor committed
790
  return gdk_rgb_xpixel_from_rgb_internal (gdk_screen_get_rgb_colormap (gdk_screen_get_default ()),
791 792 793
					   (r >> 8) + (r >> 16), g + (g >> 8), b + (b << 8));
}

794 795 796 797
void
gdk_rgb_gc_set_foreground (GdkGC *gc, guint32 rgb)
{
  GdkColor color;
798

799 800 801 802 803 804 805 806
  color.pixel = gdk_rgb_xpixel_from_rgb (rgb);
  gdk_gc_set_foreground (gc, &color);
}

void
gdk_rgb_gc_set_background (GdkGC *gc, guint32 rgb)
{
  GdkColor color;
807

808 809 810 811
  color.pixel = gdk_rgb_xpixel_from_rgb (rgb);
  gdk_gc_set_background (gc, &color);
}

Havoc Pennington's avatar
Havoc Pennington committed
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
/**
 * gdk_rgb_find_color:
 * @colormap: a #GdkColormap
 * @color: a #GdkColor
 *
 * @colormap should be the colormap for the graphics context and
 * drawable you're using to draw. If you're drawing to a #GtkWidget,
 * call gtk_widget_get_colormap().
 *
 * @color should have its %red, %green, and %blue fields initialized;
 * gdk_rgb_find_color() will fill in the %pixel field with the best
 * matching pixel from a color cube. The color is then ready to be
 * used for drawing, e.g. you can call gdk_gc_set_foreground() which
 * expects %pixel to be initialized.
 *
 * In many cases, you can avoid this whole issue by calling
 * gdk_gc_set_rgb_fg_color() or gdk_gc_set_rgb_bg_color(), which
 * do not expect %pixel to be initialized in advance. If you use those
 * functions, there's no need for gdk_rgb_find_color().
 * 
 **/
833 834 835 836 837 838 839
void
gdk_rgb_find_color (GdkColormap *colormap, GdkColor *color)
{
  color->pixel = gdk_rgb_xpixel_from_rgb_internal (colormap,
						   color->red, color->green, color->blue);
}

840
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
841 842 843 844 845
#define HAIRY_CONVERT_8
#endif

#ifdef HAIRY_CONVERT_8
static void
846
gdk_rgb_convert_8 (GdkRgbInfo *image_info, GdkImage *image,
847
		   gint x0, gint y0, gint width, gint height,
848
		   const guchar *buf, int rowstride,
849 850 851 852 853
		   gint x_align, gint y_align, GdkRgbCmap *cmap)
{
  int x, y;
  gint bpl;
  guchar *obuf, *obptr;
854
  const guchar *bptr, *bp2;
855
  gint r, g, b;
856
  guchar *colorcube = image_info->colorcube;
857

858 859 860 861 862 863 864
  bptr = buf;
  bpl = image->bpl;
  obuf = ((guchar *)image->mem) + y0 * bpl + x0;
  for (y = 0; y < height; y++)
    {
      bp2 = bptr;
      obptr = obuf;
865
      if (((guintptr)obuf | (guintptr) bp2) & 3)
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
	{
	  for (x = 0; x < width; x++)
	    {
	      r = *bp2++;
	      g = *bp2++;
	      b = *bp2++;
	      obptr[0] = colorcube[((r & 0xf0) << 4) |
				  (g & 0xf0) |
				  (b >> 4)];
	      obptr++;
	    }
	}
      else
	{
	  for (x = 0; x < width - 3; x += 4)
	    {
	      guint32 r1b0g0r0;
	      guint32 g2r2b1g1;
	      guint32 b3g3r3b2;
885

886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	      r1b0g0r0 = ((guint32 *)bp2)[0];
	      g2r2b1g1 = ((guint32 *)bp2)[1];
	      b3g3r3b2 = ((guint32 *)bp2)[2];
	      ((guint32 *)obptr)[0] =
		colorcube[((r1b0g0r0 & 0xf0) << 4) | 
			 ((r1b0g0r0 & 0xf000) >> 8) |
			 ((r1b0g0r0 & 0xf00000) >> 20)] |
		(colorcube[((r1b0g0r0 & 0xf0000000) >> 20) |
			  (g2r2b1g1 & 0xf0) |
			  ((g2r2b1g1 & 0xf000) >> 12)] << 8) |
		(colorcube[((g2r2b1g1 & 0xf00000) >> 12) |
			  ((g2r2b1g1 & 0xf0000000) >> 24) |
			  ((b3g3r3b2 & 0xf0) >> 4)] << 16) |
		(colorcube[((b3g3r3b2 & 0xf000) >> 4) |
			  ((b3g3r3b2 & 0xf00000) >> 16) |
			  (b3g3r3b2 >> 28)] << 24);
	      bp2 += 12;
	      obptr += 4;
	    }
	  for (; x < width; x++)
	    {
	      r = *bp2++;
	      g = *bp2++;
	      b = *bp2++;
	      obptr[0] = colorcube[((r & 0xf0) << 4) |
				  (g & 0xf0) |
				  (b >> 4)];
	      obptr++;
	    }
	}
      bptr += rowstride;
      obuf += bpl;
    }
}
#else
static void
922
gdk_rgb_convert_8 (GdkRgbInfo *image_info, GdkImage *image,
923
		   gint x0, gint y0, gint width, gint height,
924
		   const guchar *buf, int rowstride,
925 926 927 928 929
		   gint x_align, gint y_align, GdkRgbCmap *cmap)
{
  int x, y;
  gint bpl;
  guchar *obuf, *obptr;
930
  const guchar *bptr, *bp2;
931
  gint r, g, b;
932
  guchar *colorcube = image_info->colorcube;
933

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
  bptr = buf;
  bpl = image->bpl;
  obuf = ((guchar *)image->mem) + y0 * bpl + x0;
  for (y = 0; y < height; y++)
    {
      bp2 = bptr;
      obptr = obuf;
      for (x = 0; x < width; x++)
	{
	  r = *bp2++;
	  g = *bp2++;
	  b = *bp2++;
	  obptr[0] = colorcube[((r & 0xf0) << 4) |
			      (g & 0xf0) |
			      (b >> 4)];
	  obptr++;
	}
      bptr += rowstride;
      obuf += bpl;
    }
}
#endif

957 958 959 960 961 962 963 964 965 966


/* Dither matrices
 * ---------------
 *
 * The original matrices are called "DM" below.  The preprocessed matrices for
 * 5-6-5 RGB displays are called "DM_565".  These preprocessed tables can be
 * generated with the gdk_rgb_preprocess_dm_565() function below.
 */

967 968 969 970 971 972 973
#if 1

/* This dither table was generated by Raph Levien using patented
   technology (US Patent 5,276,535). The dither table itself is in the
   public domain. */

#define DM_WIDTH 128
974
#define DM_WIDTH_SHIFT 7
975
#define DM_HEIGHT 128
Owen Taylor's avatar
Owen Taylor committed
976
static const guchar DM[128][128] =
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
{
  { 0, 41, 23, 5, 17, 39, 7, 15, 62, 23, 40, 51, 31, 47, 9, 32, 52, 27, 57, 25, 6, 61, 27, 52, 37, 7, 40, 63, 18, 36, 10, 42, 25, 62, 45, 34, 20, 42, 37, 14, 35, 29, 50, 10, 61, 2, 40, 8, 37, 12, 58, 22, 5, 41, 10, 39, 0, 60, 11, 46, 2, 55, 38, 17, 36, 59, 13, 54, 37, 56, 8, 29, 16, 13, 63, 22, 41, 55, 7, 20, 49, 14, 23, 55, 37, 23, 19, 36, 15, 49, 23, 63, 30, 14, 38, 27, 53, 13, 22, 41, 19, 31, 7, 19, 50, 30, 49, 16, 3, 32, 56, 40, 29, 34, 8, 48, 19, 45, 4, 51, 12, 46, 35, 49, 16, 42, 12, 62 },
  { 30, 57, 36, 54, 47, 34, 52, 27, 43, 4, 28, 7, 17, 36, 62, 13, 44, 7, 18, 48, 33, 21, 44, 14, 30, 47, 12, 33, 5, 55, 31, 58, 13, 30, 4, 17, 52, 10, 60, 26, 46, 0, 39, 27, 42, 22, 47, 25, 60, 32, 9, 38, 48, 17, 59, 30, 49, 18, 34, 25, 51, 19, 5, 48, 21, 8, 28, 46, 1, 32, 41, 19, 54, 47, 37, 18, 28, 11, 44, 30, 39, 56, 2, 33, 8, 42, 61, 28, 58, 8, 46, 9, 41, 4, 58, 7, 21, 48, 59, 10, 52, 14, 42, 57, 12, 25, 7, 53, 42, 24, 11, 50, 17, 59, 42, 2, 36, 60, 32, 17, 63, 29, 21, 7, 59, 32, 24, 39 },
  { 22, 8, 16, 32, 3, 25, 13, 57, 18, 45, 58, 39, 55, 20, 5, 42, 23, 34, 63, 1, 51, 10, 58, 4, 60, 23, 53, 27, 44, 21, 3, 48, 8, 50, 43, 54, 27, 32, 5, 55, 21, 58, 12, 53, 6, 36, 14, 50, 17, 29, 53, 15, 24, 52, 7, 36, 13, 42, 4, 53, 9, 35, 61, 26, 56, 32, 49, 15, 62, 23, 6, 60, 2, 31, 4, 48, 58, 38, 15, 61, 5, 25, 47, 28, 50, 15, 7, 40, 3, 32, 33, 52, 25, 50, 35, 42, 61, 3, 28, 36, 23, 63, 4, 33, 46, 62, 36, 23, 60, 6, 54, 28, 4, 37, 23, 55, 25, 8, 42, 54, 14, 6, 56, 38, 19, 52, 4, 46 },
  { 48, 53, 43, 12, 45, 63, 30, 37, 9, 34, 21, 1, 25, 47, 29, 58, 3, 54, 15, 39, 29, 17, 38, 35, 20, 43, 1, 49, 15, 59, 29, 39, 22, 35, 16, 23, 1, 47, 39, 18, 8, 44, 25, 31, 57, 19, 63, 4, 45, 3, 42, 61, 1, 31, 45, 20, 57, 29, 62, 21, 32, 41, 14, 44, 3, 39, 5, 34, 10, 43, 51, 35, 23, 52, 40, 10, 21, 1, 53, 18, 51, 43, 12, 62, 18, 54, 26, 51, 20, 57, 14, 1, 62, 16, 11, 18, 32, 39, 17, 44, 1, 48, 26, 37, 18, 2, 51, 14, 28, 45, 35, 18, 57, 13, 47, 11, 51, 20, 2, 39, 31, 47, 25, 1, 50, 11, 60, 7 },
  { 18, 28, 1, 56, 21, 10, 51, 2, 46, 54, 14, 61, 11, 50, 13, 38, 19, 31, 45, 9, 55, 24, 47, 5, 54, 9, 62, 11, 35, 8, 51, 14, 57, 6, 63, 40, 58, 14, 51, 28, 62, 34, 15, 48, 1, 41, 30, 35, 55, 21, 34, 11, 49, 37, 8, 52, 4, 23, 15, 43, 1, 58, 11, 23, 53, 16, 55, 26, 58, 18, 27, 12, 45, 14, 25, 63, 42, 33, 27, 35, 9, 31, 21, 38, 1, 44, 34, 12, 48, 38, 21, 44, 29, 47, 26, 53, 1, 46, 54, 8, 59, 29, 11, 55, 22, 41, 33, 20, 39, 1, 48, 9, 44, 32, 5, 62, 29, 44, 57, 23, 10, 58, 34, 43, 15, 37, 26, 33 },
  { 51, 38, 59, 24, 35, 42, 19, 60, 5, 32, 41, 26, 43, 33, 7, 53, 48, 11, 59, 23, 42, 2, 61, 30, 16, 40, 32, 24, 56, 41, 19, 33, 37, 26, 47, 9, 31, 22, 2, 45, 9, 54, 4, 37, 21, 52, 11, 23, 7, 57, 16, 25, 55, 18, 63, 27, 46, 39, 56, 10, 50, 37, 29, 47, 19, 63, 24, 9, 46, 2, 39, 60, 9, 57, 30, 7, 49, 11, 59, 3, 45, 57, 5, 60, 29, 22, 5, 60, 30, 9, 59, 18, 40, 6, 57, 36, 30, 12, 24, 34, 15, 40, 52, 6, 49, 9, 58, 4, 63, 12, 26, 61, 22, 53, 38, 16, 35, 14, 28, 50, 42, 17, 5, 28, 62, 20, 54, 12 },
  { 26, 6, 31, 15, 49, 6, 38, 27, 22, 49, 16, 56, 2, 62, 30, 21, 0, 36, 28, 6, 49, 32, 13, 52, 26, 50, 19, 46, 3, 26, 62, 0, 53, 12, 29, 3, 53, 41, 60, 24, 38, 13, 58, 16, 43, 9, 59, 39, 46, 28, 44, 40, 2, 33, 13, 41, 16, 6, 47, 31, 26, 17, 57, 6, 38, 0, 42, 36, 29, 52, 20, 31, 48, 0, 34, 56, 20, 36, 23, 54, 14, 41, 24, 37, 10, 55, 46, 25, 16, 45, 36, 4, 55, 23, 15, 8, 50, 62, 5, 56, 44, 20, 13, 28, 59, 31, 24, 47, 31, 52, 37, 17, 40, 0, 26, 49, 3, 60, 7, 33, 0, 61, 53, 40, 8, 45, 2, 41 },
  { 16, 63, 43, 4, 61, 24, 56, 13, 53, 8, 36, 12, 24, 41, 16, 46, 60, 26, 52, 39, 14, 57, 21, 37, 0, 45, 7, 59, 38, 17, 43, 10, 45, 20, 61, 43, 19, 11, 33, 17, 50, 32, 23, 61, 28, 49, 26, 0, 18, 51, 5, 60, 22, 58, 29, 0, 59, 34, 19, 62, 3, 52, 7, 44, 30, 59, 13, 50, 15, 62, 7, 17, 38, 22, 44, 15, 40, 4, 47, 28, 33, 17, 49, 16, 51, 40, 10, 56, 0, 53, 13, 49, 28, 38, 60, 21, 43, 19, 37, 27, 3, 51, 34, 39, 0, 45, 15, 43, 10, 21, 3, 55, 8, 33, 59, 10, 41, 18, 52, 24, 46, 20, 30, 13, 58, 22, 36, 57 },
  { 50, 34, 11, 47, 29, 17, 44, 0, 33, 63, 28, 46, 52, 5, 57, 10, 42, 18, 4, 63, 20, 8, 44, 10, 56, 34, 14, 29, 5, 54, 23, 59, 32, 49, 7, 34, 49, 27, 56, 0, 42, 7, 46, 3, 40, 6, 54, 32, 62, 13, 36, 10, 47, 8, 35, 49, 24, 51, 12, 40, 22, 35, 60, 12, 22, 51, 33, 4, 40, 25, 43, 55, 5, 54, 12, 61, 26, 51, 8, 62, 0, 53, 7, 63, 2, 32, 19, 34, 42, 24, 31, 63, 2, 10, 45, 33, 0, 48, 9, 61, 22, 47, 8, 62, 18, 56, 7, 54, 27, 57, 46, 30, 50, 19, 45, 30, 56, 36, 22, 47, 11, 38, 3, 51, 32, 48, 18, 9 },
  { 0, 21, 40, 19, 52, 9, 37, 48, 20, 40, 3, 18, 27, 38, 35, 22, 31, 56, 13, 35, 46, 28, 60, 40, 27, 18, 61, 50, 41, 30, 7, 36, 2, 25, 16, 57, 5, 15, 47, 29, 55, 19, 30, 52, 15, 34, 20, 12, 43, 30, 20, 54, 25, 44, 53, 12, 38, 5, 55, 27, 48, 15, 33, 27, 45, 8, 19, 28, 56, 11, 33, 49, 18, 36, 29, 2, 45, 16, 39, 19, 31, 43, 27, 35, 20, 52, 26, 6, 61, 11, 41, 17, 29, 51, 20, 56, 25, 32, 41, 17, 53, 31, 25, 14, 42, 23, 35, 16, 38, 6, 34, 12, 15, 62, 6, 21, 13, 1, 63, 9, 55, 27, 43, 25, 14, 4, 31, 55 },
  { 44, 29, 61, 2, 35, 58, 26, 15, 60, 10, 51, 59, 14, 55, 8, 50, 2, 44, 25, 51, 1, 33, 16, 4, 48, 36, 2, 21, 12, 57, 48, 13, 51, 55, 40, 28, 37, 62, 8, 39, 12, 63, 36, 10, 59, 24, 56, 47, 9, 50, 41, 1, 32, 17, 6, 21, 61, 30, 9, 43, 1, 54, 41, 2, 54, 37, 48, 61, 1, 46, 21, 3, 58, 24, 50, 32, 60, 10, 57, 25, 46, 12, 59, 4, 45, 13, 57, 47, 27, 39, 5, 58, 47, 14, 35, 4, 52, 13, 60, 6, 36, 10, 45, 55, 4, 50, 29, 2, 61, 50, 25, 58, 44, 24, 36, 42, 54, 28, 40, 32, 16, 56, 6, 62, 46, 39, 60, 23 },
  { 7, 48, 14, 54, 23, 40, 4, 45, 30, 22, 42, 32, 1, 44, 20, 29, 58, 8, 37, 19, 41, 54, 24, 58, 9, 53, 25, 46, 34, 16, 23, 38, 27, 11, 18, 1, 52, 21, 35, 22, 48, 5, 25, 45, 18, 38, 2, 27, 35, 4, 57, 15, 62, 39, 57, 28, 42, 16, 36, 60, 24, 18, 10, 63, 20, 5, 16, 23, 37, 14, 59, 27, 41, 8, 13, 42, 21, 35, 6, 50, 3, 38, 15, 48, 30, 39, 17, 3, 49, 14, 53, 33, 24, 7, 61, 44, 11, 39, 23, 49, 19, 58, 1, 32, 36, 12, 60, 41, 20, 13, 41, 4, 39, 1, 48, 8, 18, 51, 14, 44, 5, 37, 21, 34, 1, 26, 10, 37 },
  { 53, 36, 27, 9, 50, 12, 32, 55, 2, 57, 7, 17, 48, 34, 63, 15, 40, 26, 62, 11, 49, 6, 31, 39, 22, 42, 6, 63, 1, 39, 60, 4, 42, 61, 32, 45, 24, 44, 2, 60, 16, 41, 53, 1, 33, 61, 49, 17, 63, 23, 45, 26, 33, 3, 23, 46, 2, 50, 20, 4, 45, 34, 49, 30, 39, 58, 44, 31, 53, 34, 6, 52, 30, 47, 63, 1, 53, 22, 42, 31, 58, 23, 54, 22, 61, 8, 36, 59, 22, 35, 21, 1, 55, 40, 27, 16, 30, 54, 2, 29, 43, 16, 39, 63, 21, 46, 26, 10, 48, 32, 19, 53, 30, 56, 26, 60, 33, 4, 61, 23, 49, 59, 15, 53, 19, 58, 42, 16 },
  { 20, 5, 59, 46, 25, 62, 7, 19, 43, 25, 37, 61, 11, 24, 4, 54, 12, 52, 3, 32, 17, 61, 12, 47, 15, 55, 18, 31, 53, 28, 9, 50, 21, 6, 55, 9, 58, 14, 54, 26, 33, 7, 31, 58, 13, 21, 8, 42, 29, 6, 37, 11, 48, 52, 14, 60, 11, 39, 56, 32, 14, 58, 7, 26, 17, 4, 42, 8, 11, 47, 19, 38, 10, 17, 26, 37, 9, 55, 28, 13, 18, 40, 6, 33, 1, 43, 25, 11, 51, 7, 62, 43, 18, 37, 3, 57, 45, 9, 38, 58, 5, 52, 27, 7, 17, 53, 5, 57, 37, 2, 63, 9, 22, 15, 11, 38, 25, 45, 35, 0, 28, 10, 41, 30, 50, 8, 31, 57 },
  { 49, 33, 16, 38, 1, 42, 51, 34, 53, 14, 28, 49, 30, 56, 36, 23, 43, 20, 38, 56, 22, 45, 28, 0, 62, 35, 26, 44, 11, 19, 52, 35, 44, 15, 30, 38, 10, 31, 40, 4, 46, 50, 20, 40, 27, 44, 51, 14, 56, 53, 19, 59, 7, 29, 41, 19, 35, 25, 8, 52, 22, 44, 13, 53, 50, 32, 61, 24, 56, 25, 63, 0, 45, 57, 33, 59, 16, 46, 4, 62, 50, 11, 60, 37, 52, 19, 55, 29, 37, 46, 13, 26, 48, 10, 50, 34, 21, 63, 26, 13, 42, 33, 22, 55, 35, 28, 43, 15, 24, 51, 27, 34, 46, 49, 58, 3, 52, 9, 57, 19, 48, 55, 3, 35, 12, 45, 24, 3 },
  { 41, 11, 56, 28, 18, 31, 22, 10, 37, 6, 47, 13, 3, 41, 9, 46, 0, 48, 29, 6, 34, 10, 55, 37, 20, 8, 49, 3, 41, 59, 14, 25, 0, 63, 19, 47, 27, 51, 17, 57, 23, 10, 61, 6, 54, 3, 38, 31, 0, 22, 34, 43, 20, 55, 31, 0, 49, 63, 29, 38, 3, 62, 28, 40, 0, 22, 14, 35, 2, 48, 15, 43, 23, 14, 3, 29, 49, 20, 39, 34, 0, 44, 29, 9, 15, 47, 5, 42, 0, 31, 58, 5, 31, 61, 23, 15, 0, 47, 19, 50, 24, 3, 59, 11, 44, 0, 31, 59, 6, 42, 17, 60, 0, 39, 20, 31, 43, 17, 29, 40, 12, 25, 60, 22, 52, 15, 63, 29 },
  { 20, 52, 8, 44, 62, 4, 59, 49, 17, 63, 21, 39, 60, 18, 52, 27, 33, 59, 14, 51, 59, 43, 24, 5, 51, 30, 57, 17, 32, 5, 37, 56, 48, 34, 42, 3, 60, 5, 36, 13, 43, 37, 18, 34, 25, 12, 59, 24, 47, 36, 11, 50, 3, 38, 9, 58, 16, 5, 43, 18, 47, 10, 37, 18, 59, 46, 29, 52, 40, 12, 34, 28, 56, 36, 53, 7, 43, 8, 24, 52, 26, 17, 56, 43, 24, 32, 63, 20, 57, 16, 22, 52, 36, 8, 41, 56, 29, 32, 54, 7, 35, 57, 14, 48, 20, 62, 13, 39, 53, 29, 8, 45, 13, 29, 7, 61, 14, 54, 6, 63, 38, 32, 18, 43, 2, 39, 6, 47 },
  { 0, 58, 23, 35, 13, 46, 12, 39, 0, 31, 55, 24, 5, 35, 15, 61, 17, 5, 39, 25, 18, 2, 50, 33, 41, 13, 39, 23, 62, 46, 29, 12, 22, 8, 56, 25, 20, 49, 32, 62, 0, 56, 11, 46, 63, 42, 9, 16, 55, 5, 60, 15, 62, 26, 45, 21, 36, 51, 13, 57, 31, 24, 55, 6, 35, 9, 57, 5, 20, 60, 7, 51, 5, 19, 40, 25, 61, 32, 56, 12, 36, 48, 21, 2, 58, 12, 39, 28, 9, 50, 40, 12, 44, 18, 25, 49, 6, 38, 11, 62, 18, 46, 30, 9, 40, 25, 49, 19, 10, 36, 55, 22, 33, 52, 41, 18, 37, 27, 49, 21, 2, 46, 7, 53, 33, 61, 27, 35 },
  { 41, 31, 5, 39, 51, 26, 33, 57, 27, 41, 9, 44, 54, 29, 48, 7, 44, 36, 57, 10, 31, 63, 16, 45, 11, 60, 1, 47, 7, 20, 43, 3, 58, 36, 13, 52, 39, 7, 15, 28, 22, 48, 30, 21, 1, 29, 49, 44, 27, 17, 40, 30, 24, 42, 12, 53, 33, 7, 47, 20, 1, 42, 11, 49, 25, 43, 17, 32, 45, 27, 41, 21, 31, 62, 11, 49, 2, 15, 42, 5, 63, 7, 41, 27, 49, 6, 54, 23, 46, 34, 2, 28, 54, 3, 59, 12, 46, 17, 42, 28, 40, 1, 37, 51, 5, 55, 2, 34, 47, 16, 3, 62, 47, 5, 23, 56, 1, 44, 12, 34, 51, 16, 57, 11, 25, 17, 54, 13 },
  { 60, 26, 55, 18, 3, 60, 20, 6, 52, 15, 50, 19, 32, 11, 23, 53, 26, 21, 1, 47, 42, 27, 8, 58, 21, 27, 53, 36, 26, 54, 31, 50, 17, 30, 45, 1, 29, 59, 44, 53, 41, 4, 35, 58, 51, 19, 32, 4, 52, 34, 48, 8, 51, 5, 56, 2, 25, 61, 27, 38, 54, 27, 62, 21, 51, 1, 39, 62, 10, 50, 1, 58, 13, 47, 38, 18, 35, 54, 22, 51, 30, 19, 59, 34, 14, 32, 44, 4, 60, 15, 52, 62, 20, 43, 30, 35, 21, 60, 4, 52, 12, 24, 61, 18, 30, 42, 23, 61, 25, 50, 27, 38, 11, 59, 12, 35, 50, 30, 59, 24, 8, 42, 28, 37, 48, 9, 44, 21 },
  { 10, 47, 15, 50, 30, 43, 8, 45, 29, 2, 36, 59, 1, 58, 41, 3, 63, 31, 54, 20, 13, 55, 35, 38, 4, 44, 15, 9, 61, 2, 14, 38, 61, 10, 23, 54, 18, 12, 24, 2, 14, 55, 16, 8, 38, 14, 41, 60, 10, 23, 1, 58, 32, 17, 28, 37, 41, 15, 3, 60, 15, 33, 4, 36, 16, 59, 28, 14, 23, 55, 37, 18, 44, 28, 2, 57, 30, 10, 27, 46, 14, 38, 3, 53, 21, 61, 17, 35, 10, 41, 26, 7, 33, 9, 57, 1, 53, 37, 26, 20, 56, 48, 9, 33, 58, 16, 37, 7, 45, 1, 57, 15, 32, 26, 42, 23, 7, 20, 4, 54, 31, 62, 22, 1, 59, 30, 4, 51 },
  { 36, 2, 38, 11, 24, 36, 54, 22, 62, 47, 25, 8, 28, 45, 16, 38, 12, 43, 9, 37, 49, 3, 23, 52, 18, 30, 50, 33, 19, 42, 49, 26, 6, 40, 47, 35, 63, 38, 50, 33, 60, 26, 36, 47, 24, 57, 6, 26, 39, 63, 19, 44, 14, 46, 61, 9, 50, 30, 45, 23, 10, 50, 44, 8, 31, 54, 6, 46, 36, 4, 30, 54, 8, 52, 22, 41, 4, 60, 40, 0, 58, 24, 45, 10, 37, 1, 48, 30, 56, 17, 38, 48, 24, 47, 19, 39, 14, 8, 45, 32, 2, 34, 27, 44, 4, 52, 11, 56, 31, 21, 40, 19, 44, 51, 2, 63, 46, 58, 36, 43, 14, 5, 50, 38, 14, 56, 40, 23 },
  { 61, 46, 32, 63, 54, 1, 14, 34, 12, 40, 18, 49, 37, 10, 61, 30, 51, 24, 60, 7, 29, 40, 62, 11, 46, 58, 6, 56, 24, 10, 34, 52, 21, 59, 16, 3, 27, 5, 20, 46, 9, 40, 7, 62, 2, 30, 53, 15, 48, 10, 28, 35, 54, 6, 21, 34, 18, 55, 7, 40, 57, 19, 26, 60, 41, 13, 24, 51, 19, 61, 9, 25, 34, 15, 63, 11, 45, 17, 20, 47, 33, 8, 31, 62, 43, 26, 53, 7, 24, 59, 0, 13, 55, 4, 62, 27, 51, 31, 63, 15, 58, 7, 54, 14, 46, 22, 28, 43, 12, 63, 8, 54, 5, 17, 39, 33, 15, 10, 27, 17, 47, 34, 19, 45, 27, 12, 33, 17 },
  { 5, 28, 21, 7, 17, 48, 42, 58, 23, 4, 63, 14, 55, 21, 34, 5, 19, 0, 45, 17, 52, 15, 25, 32, 0, 22, 40, 13, 45, 62, 18, 0, 43, 11, 33, 55, 30, 42, 57, 19, 51, 31, 22, 43, 18, 45, 34, 0, 43, 31, 56, 3, 23, 40, 59, 0, 44, 13, 48, 35, 2, 32, 46, 0, 21, 48, 35, 3, 40, 32, 43, 59, 0, 48, 33, 26, 53, 36, 55, 12, 51, 16, 55, 5, 18, 29, 11, 39, 51, 19, 45, 31, 42, 21, 35, 6, 22, 47, 10, 38, 23, 50, 20, 36, 0, 60, 38, 4, 50, 35, 48, 34, 24, 57, 9, 53, 28, 48, 61, 0, 56, 24, 53, 3, 63, 6, 42, 57 },
  { 13, 53, 45, 40, 58, 27, 6, 16, 38, 51, 33, 30, 43, 2, 47, 56, 40, 50, 33, 57, 27, 5, 47, 42, 60, 36, 16, 54, 28, 4, 37, 57, 28, 51, 22, 8, 45, 14, 6, 39, 0, 54, 11, 59, 28, 12, 50, 21, 61, 13, 19, 38, 49, 11, 25, 37, 58, 29, 22, 63, 14, 56, 12, 53, 30, 63, 9, 57, 26, 12, 47, 16, 23, 39, 50, 6, 31, 2, 25, 6, 28, 41, 36, 22, 50, 57, 42, 3, 34, 8, 28, 61, 11, 50, 16, 54, 41, 0, 55, 43, 5, 29, 41, 63, 25, 16, 53, 18, 26, 10, 21, 0, 61, 30, 41, 22, 3, 38, 20, 39, 29, 8, 41, 16, 36, 52, 22, 19 },
  { 55, 34, 0, 25, 10, 32, 56, 44, 28, 0, 57, 7, 26, 53, 23, 8, 13, 35, 22, 12, 36, 60, 20, 8, 14, 29, 48, 2, 41, 49, 23, 13, 39, 7, 48, 58, 25, 53, 34, 62, 28, 16, 48, 4, 37, 56, 27, 5, 36, 52, 46, 7, 62, 33, 52, 11, 17, 53, 5, 28, 41, 24, 38, 17, 5, 39, 20, 45, 15, 56, 5, 38, 60, 8, 14, 57, 21, 48, 62, 39, 59, 13, 1, 60, 9, 32, 16, 63, 44, 25, 52, 15, 36, 2, 60, 29, 12, 33, 25, 17, 59, 45, 13, 8, 49, 32, 6, 40, 59, 29, 45, 37, 13, 47, 6, 55, 30, 45, 9, 52, 13, 59, 25, 47, 32, 1, 49, 30 },
  { 9, 39, 14, 61, 49, 37, 3, 20, 50, 13, 41, 19, 46, 17, 38, 59, 28, 62, 4, 44, 54, 1, 34, 51, 55, 7, 63, 32, 21, 8, 56, 31, 62, 19, 36, 1, 41, 17, 24, 12, 42, 35, 25, 52, 20, 8, 44, 59, 25, 2, 22, 42, 16, 29, 4, 46, 20, 36, 43, 9, 51, 8, 49, 26, 58, 33, 54, 1, 37, 29, 52, 20, 27, 45, 19, 35, 42, 16, 10, 32, 20, 49, 46, 27, 40, 4, 47, 22, 13, 55, 4, 47, 26, 44, 23, 40, 58, 19, 48, 13, 31, 2, 57, 34, 42, 19, 61, 32, 14, 55, 5, 51, 26, 19, 58, 16, 49, 14, 62, 5, 33, 44, 21, 7, 60, 26, 11, 41 },
  { 62, 24, 47, 29, 8, 19, 53, 11, 60, 24, 32, 61, 4, 55, 31, 2, 49, 16, 39, 9, 31, 24, 43, 17, 26, 38, 11, 25, 58, 43, 12, 35, 3, 46, 15, 32, 63, 4, 49, 56, 2, 60, 10, 32, 63, 17, 39, 12, 55, 30, 57, 9, 48, 55, 39, 24, 60, 2, 58, 31, 19