gdkrgb.c 147 KB
Newer Older
1 2 3 4
/* 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
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 <math.h>
33 34 35 36 37 38 39 40 41 42 43 44 45

#if HAVE_CONFIG_H
#  include <config.h>
#  if STDC_HEADERS
#    include <stdio.h>
#    include <stdlib.h>
#    include <string.h>
#  endif
#else
#  include <stdio.h>
#  include <stdlib.h>
#endif

46 47 48

#define ENABLE_GRAYSCALE

49 50 51 52 53 54 55 56 57 58
#ifdef GDK_RGB_STANDALONE

/* Compiling as a standalone module (i.e. with Gtk 1.0) */
/* gtk/gtk.h is already included in gdkrgbstub.c */
#include "config.h"
#include <gdk/gdkprivate.h>

#else

/* Compiling as a part of Gtk 1.1 or later */
59
#include "config.h"
60 61 62 63
#include "gdkprivate.h"

#endif

Owen Taylor's avatar
Owen Taylor committed
64 65
#include "gdk.h"		/* For gdk_flush() */
#include "gdkrgb.h"
66

67 68
typedef struct _GdkRgbInfo     GdkRgbInfo;
typedef struct _GdkRgbCmapInfo GdkRgbCmapInfo;
69

70
typedef void (*GdkRgbConvFunc) (GdkRgbInfo *image_info, GdkImage *image,
71 72 73 74 75 76
				gint x0, gint y0,
				gint width, gint height,
				guchar *buf, int rowstride,
				gint x_align, gint y_align,
				GdkRgbCmap *cmap);

77 78 79 80 81 82 83 84 85 86
static const gchar* visual_names[] =
{
  "static gray",
  "grayscale",
  "static color",
  "pseudo color",
  "true color",
  "direct color",
};

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#define REGION_WIDTH 256
#define STAGE_ROWSTRIDE (REGION_WIDTH * 3)
#define REGION_HEIGHT 64

/* We have N_REGION REGION_WIDTH x REGION_HEIGHT regions divided
 * up between n_images different images. possible_n_images gives
 * various divisors of N_REGIONS. The reason for allowing this
 * flexibility is that we want to create as few images as possible,
 * but we want to deal with the abberant systems that have a SHMMAX
 * limit less than
 *
 * REGION_WIDTH * REGION_HEIGHT * N_REGIONS * 4 (384k)
 *
 * (Are there any such?)
 */
#define N_REGIONS 6
static const int possible_n_images[] = { 1, 2, 3, 6 };

105 106 107 108 109 110 111 112 113 114
/* Some of these fields should go, as they're not being used at all.
   Globals should generally migrate into here - it's very likely that
   we'll want to run more than one GdkRgbInfo context at the same time
   (i.e. some but not all windows have privately installed
   colormaps). */

struct _GdkRgbInfo
{
  GdkVisual *visual;
  GdkColormap *cmap;
115

116 117 118 119 120
  guint nred_shades;
  guint ngreen_shades;
  guint nblue_shades;
  guint ngray_shades;
  guint nreserved;
121

122 123 124
  guint bpp;
  gint cmap_alloced;
  gdouble gamma;
125

126 127 128
  /* Generally, the stage buffer is used to convert 32bit RGB, gray,
     and indexed images into 24 bit packed RGB. */
  guchar *stage_buf;
129

130
  GdkRgbCmap *gray_cmap;
131

132
  gboolean dith_default;
133

134 135 136
  gboolean bitmap; /* set true if in 1 bit per pixel mode */
  GdkGC *own_gc;

137 138 139
  /* Convert functions */
  GdkRgbConvFunc conv;
  GdkRgbConvFunc conv_d;
140

141 142
  GdkRgbConvFunc conv_32;
  GdkRgbConvFunc conv_32_d;
143

144 145
  GdkRgbConvFunc conv_gray;
  GdkRgbConvFunc conv_gray_d;
146

147 148
  GdkRgbConvFunc conv_indexed;
  GdkRgbConvFunc conv_indexed_d;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

  gint n_images;
  GdkImage *static_image[N_REGIONS];
  gint static_image_idx;

  /* In order to optimize filling fractions, we simultaneously fill in up
   * to three regions of size REGION_WIDTH * REGION_HEIGHT: one
   * for images that are taller than REGION_HEIGHT / 2, and must
   * be tiled horizontally. One for images that are wider than
   * REGION_WIDTH / 2 and must be tiled vertically, and a third
   * for images smaller than REGION_HEIGHT / 2 x REGION_WIDTH x 2
   * that we tile in horizontal rows.
   */
  gint horiz_idx;
  gint horiz_y;
  gint vert_idx;
  gint vert_x;
  
  /* tile_y1 and tile_y2 define the horizontal band into
   * which we are tiling images. tile_x is the x extent to
   * which that is filled
   */
  gint tile_idx;
  gint tile_x;
  gint tile_y1;
  gint tile_y2;

  guchar *colorcube;
  guchar *colorcube_d;
178 179 180 181 182 183 184 185 186 187 188 189 190 191

  /* 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 */
192 193
};

194 195 196 197 198
static GdkRgbCmapInfo *gdk_rgb_cmap_get_info (GdkRgbCmap *cmap, GdkRgbInfo *image_info);

static const char *gdk_rgb_key = "gdk-rgb-info";
static GQuark gdk_rgb_quark = 0;

199 200 201 202 203 204 205 206 207 208
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)
{
  gulong free_pixels[256];
  gint n_free;
  gint i;
209

210
#ifdef VERBOSE
211
  g_print ("%s", msg);
212 213 214 215 216 217 218 219 220 221 222
#endif
  n_free = 0;
  for (i = 0; i < 256; i++)
    if (pixels[i] < 256)
      free_pixels[n_free++] = pixels[i];
  if (n_free)
    gdk_colors_free (cmap, free_pixels, n_free, 0);
  return 0;
}

static void
223 224
gdk_rgb_make_colorcube (GdkRgbInfo *image_info, gulong *pixels,
			gint nr, gint ng, gint nb)
225 226 227
{
  guchar rt[16], gt[16], bt[16];
  gint i;
228

229
  image_info->colorcube = g_new (guchar, 4096);
230 231 232 233 234 235
  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);
    }
236

237 238
  for (i = 0; i < 4096; i++)
    {
239
      image_info->colorcube[i] = pixels[rt[i >> 8] + gt[(i >> 4) & 0x0f] + bt[i & 0x0f]];
240
#ifdef VERBOSE
241
      g_print ("%03x %02x %x %x %x\n", i, image-info->colorcube[i], rt[i >> 8], gt[(i >> 4) & 0x0f], bt[i & 0x0f]);
242 243 244 245 246 247
#endif
    }
}

/* this is the colorcube suitable for dithering */
static void
248 249
gdk_rgb_make_colorcube_d (GdkRgbInfo *image_info, gulong *pixels,
			  gint nr, gint ng, gint nb)
250 251 252
{
  gint r, g, b;
  gint i;
253

254
  image_info->colorcube_d = g_new (guchar, 512);
255 256 257 258 259
  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);
260
      image_info->colorcube_d[i] = pixels[(r * ng + g) * nb + b];
261 262 263 264 265 266
    }
}

/* Try installing a color cube of the specified size.
   Make the colorcube and return TRUE on success */
static gint
267 268
gdk_rgb_try_colormap (GdkRgbInfo *image_info, gboolean force,
		      gint nr, gint ng, gint nb)
269 270 271 272 273 274 275 276 277 278 279 280 281
{
  gint r, g, b;
  gint ri, gi, bi;
  gint r0, g0, b0;
  GdkColormap *cmap;
  GdkColor color;
  gulong pixels[256];
  gulong junk[256];
  gint i;
  gint d2;
  gint colors_needed;
  gint idx;
  gint best[256];
282

283
  if (!force && nr * ng * nb < gdk_rgb_min_colors)
284
    return FALSE;
285

286
  if (image_info->cmap)
287 288 289
    cmap = image_info->cmap;
  else
    cmap = gdk_colormap_get_system ();
290

291 292 293 294 295 296
  colors_needed = nr * ng * nb;
  for (i = 0; i < 256; i++)
    {
      best[i] = 192;
      pixels[i] = 256;
    }
297

298
#ifndef GAMMA
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  if (cmap == gdk_colormap_get_system())
    /* 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);
	if (d2 < best[idx]) {
	  if (pixels[idx] < 256)
	    gdk_colors_free (cmap, pixels + idx, 1, 0);
	  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;
	}
326 327
      }
#endif
328

329 330 331 332 333 334 335
  if (colors_needed)
    {
      if (!gdk_colors_alloc (cmap, 0, NULL, 0, junk, colors_needed))
	{
	  char tmp_str[80];
	  
	  sprintf (tmp_str,
336
		   "%d %d %d colormap failed (in gdk_colors_alloc)\n",
337 338 339
		   nr, ng, nb);
	  return gdk_rgb_cmap_fail (tmp_str, cmap, pixels);
	}
340

341 342
      gdk_colors_free (cmap, junk, colors_needed, 0);
    }
343

344 345 346 347 348 349 350 351 352
  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);
353

354 355 356 357 358
#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
359

360
	      if (!gdk_colormap_alloc_color (cmap, &color, FALSE, force))
361 362
		{
		  char tmp_str[80];
363 364

		  sprintf (tmp_str, "%d %d %d colormap failed\n",
365 366 367 368 369 370 371
			   nr, ng, nb);
		  return gdk_rgb_cmap_fail (tmp_str,
					    cmap, pixels);
		}
	      pixels[i] = color.pixel;
	    }
#ifdef VERBOSE
372
	  g_print ("%d: %lx\n", i, pixels[i]);
373 374
#endif
	}
375

376 377 378
  image_info->nred_shades = nr;
  image_info->ngreen_shades = ng;
  image_info->nblue_shades = nb;
379 380
  gdk_rgb_make_colorcube (image_info, pixels, nr, ng, nb);
  gdk_rgb_make_colorcube_d (image_info, pixels, nr, ng, nb);
381 382 383 384 385
  return TRUE;
}

/* Return TRUE on success. */
static gboolean
386
gdk_rgb_do_colormaps (GdkRgbInfo *image_info, gboolean force)
387
{
388
  static const gint sizes[][3] = {
389
    /*    { 6, 7, 6 }, */
390 391 392 393 394 395 396 397 398 399
    { 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 }
  };
400
  static const gint n_sizes = sizeof(sizes) / (3 * sizeof(gint));
401
  gint i;
402

403 404 405 406 407
  /* 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
   */
408
  for (i = 0; i < n_sizes; i++)
409 410 411
    if (gdk_rgb_try_colormap (image_info,
			      (i == n_sizes - 1 ) && force,
			      sizes[i][0], sizes[i][1], sizes[i][2]))
412 413 414 415
      return TRUE;
  return FALSE;
}

416 417
/* Make a 2 x 2 x 2 colorcube */
static void
418
gdk_rgb_colorcube_222 (GdkRgbInfo *image_info)
419 420 421 422
{
  int i;
  GdkColor color;

423
  image_info->colorcube_d = g_new (guchar, 512);
424 425 426 427 428 429

  for (i = 0; i < 8; i++)
    {
      color.red = ((i & 4) >> 2) * 65535;
      color.green = ((i & 2) >> 1) * 65535;
      color.blue = (i & 1) * 65535;
430
      gdk_colormap_alloc_color (image_info->cmap, &color, FALSE, TRUE);
431
      image_info->colorcube_d[((i & 4) << 4) | ((i & 2) << 2) | (i & 1)] = color.pixel;
432 433 434
    }
}

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
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):
454

455
   x000 is the quality - 1 is 1bpp, 2 is 4bpp,
456 457 458
                         4 is 8bpp,
			 7 is 15bpp truecolor, 8 is 16bpp truecolor,
			 9 is 24bpp truecolor.
459
   0x00 is the speed - 1 is the normal case,
460
                       2 means faster than normal
461 462
   00x0 gets a point for being the system visual
   000x gets a point for being pseudocolor
463

464 465 466 467 468 469 470 471
   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;
472

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  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;
    }
491 492
  else if (visual->type == GDK_VISUAL_PSEUDO_COLOR ||
	   visual->type == GDK_VISUAL_STATIC_COLOR)
493 494 495
    {
      if (visual->depth == 8)
	quality = 4;
496 497 498 499
      else if (visual->depth == 4)
	quality = 2;
      else if (visual->depth == 1)
	quality = 1;
500 501 502 503 504 505 506 507 508 509 510 511 512 513
    }
  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;
    }
514

515 516
  if (quality == 0)
    return 0;
517

518
  sys = (visual == gdk_visual_get_system ());
519

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

522
  if (gdk_rgb_verbose)
523
    g_print ("Visual type = %s, depth = %d, %x:%x:%x%s; score=%x\n",
524 525 526 527 528 529 530 531
	     visual_names[visual->type],
	     visual->depth,
	     visual->red_mask,
	     visual->green_mask,
	     visual->blue_mask,
	     sys ? " (system)" : "",
	     (quality << 12) | (speed << 8) | (sys << 4) | pseudo);

532 533 534
  return (quality << 12) | (speed << 8) | (sys << 4) | pseudo;
}

535
static GdkVisual *
536 537
gdk_rgb_choose_visual (void)
{
538
  GList *visuals, *tmp_list;
539 540
  guint32 score, best_score;
  GdkVisual *visual, *best_visual;
541

542
  visuals = gdk_list_visuals ();
543
  tmp_list = visuals;
544

545
  best_visual = tmp_list->data;
546
  best_score = gdk_rgb_score_visual (best_visual);
547 548
  tmp_list = tmp_list->next;
  while (tmp_list)
549
    {
550
      visual = tmp_list->data;
551 552 553 554 555 556
      score = gdk_rgb_score_visual (visual);
      if (score > best_score)
	{
	  best_score = score;
	  best_visual = visual;
	}
557
      tmp_list = tmp_list->next;
558
    }
559

560 561
  g_list_free (visuals);

562
  return best_visual;
563 564
}

565
static void gdk_rgb_select_conv (GdkRgbInfo *image_info, GdkImage *image);
566 567

static void
568 569
gdk_rgb_set_gray_cmap (GdkRgbInfo  *image_info,
		       GdkColormap *cmap)
570 571 572 573 574 575
{
  gint i;
  GdkColor color;
  gint status;
  gulong pixels[256];
  gint r, g, b, gray;
576

577 578 579 580 581 582
  for (i = 0; i < 256; i++)
    {
      color.pixel = i;
      color.red = i * 257;
      color.green = i * 257;
      color.blue = i * 257;
583
      status = gdk_colormap_alloc_color (cmap, &color, FALSE, TRUE);
584 585
      pixels[i] = color.pixel;
#ifdef VERBOSE
586 587
      g_print ("allocating pixel %d, %x %x %x, result %d\n",
	       color.pixel, color.red, color.green, color.blue, status);
588 589
#endif
    }
590

591 592
  /* Now, we make fake colorcubes - we ultimately just use the pseudocolor
     methods. */
593

594
  image_info->colorcube = g_new (guchar, 4096);
595

596 597 598 599 600 601 602 603 604
  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;
605 606 607 608 609
      image_info->colorcube[i] = pixels[gray];
    }
}

static gboolean
610 611 612
gdk_rgb_allocate_images (GdkRgbInfo *image_info,
			 gint        n_images,
			 gboolean    shared)
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
{
  gint i;
  
  for (i = 0; i < n_images; i++)
    {
      if (image_info->bitmap)
	/* Use malloc() instead of g_malloc since X will free() this mem */
	image_info->static_image[i] = gdk_image_new_bitmap (image_info->visual,
							    (gpointer) malloc (REGION_WIDTH * REGION_HEIGHT >> 3),
							    REGION_WIDTH * (N_REGIONS / n_images), REGION_HEIGHT);
      else
	image_info->static_image[i] = gdk_image_new (shared ? GDK_IMAGE_SHARED : GDK_IMAGE_NORMAL,
						     image_info->visual,
						     REGION_WIDTH * (N_REGIONS / n_images), REGION_HEIGHT);

      if (!image_info->static_image[i])
	{
	  gint j;

632
	  for (j = 0; j < i; j++)
633 634 635 636
	    gdk_image_unref (image_info->static_image[i]);

	  return FALSE;
	}
637
    }
638 639
  
  return TRUE;
640 641
}

642 643
static void
gdk_rgb_free_info (GdkRgbInfo *image_info)
644 645
{
  gint i;
646
  GSList *tmp_list;
647

648 649 650 651 652 653 654 655 656 657 658 659
  
  if (image_info->stage_buf)
    g_free (image_info->stage_buf);
  
  if (image_info->gray_cmap)
    gdk_rgb_cmap_free (image_info->gray_cmap);

  if (image_info->own_gc)
    gdk_gc_unref (image_info->own_gc);

  for (i = 0; i < image_info->n_images; i++)
    gdk_image_unref (image_info->static_image[i]);
660

661 662 663 664 665 666 667 668
  if (image_info->colorcube)
    g_free (image_info->colorcube);
  
  if (image_info->colorcube_d)
    g_free (image_info->colorcube_d);

  tmp_list = image_info->cmap_info_list;
  while (tmp_list)
669
    {
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
      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;
  gint i;
689

690
  image_info = g_new0 (GdkRgbInfo, 1);
691

692 693
  image_info->visual = visual;
  image_info->cmap = NULL;
694

695 696 697 698 699
  image_info->nred_shades = 6;
  image_info->ngreen_shades = 6;
  image_info->nblue_shades = 4;
  image_info->ngray_shades = 24;
  image_info->nreserved = 0;
700

701 702 703
  image_info->bpp = 0;
  image_info->cmap_alloced = FALSE;
  image_info->gamma = 1.0;
704

705
  image_info->stage_buf = NULL;
706

707
  image_info->own_gc = NULL;
708

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
  image_info->cmap = colormap;

  if ((image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
       image_info->visual->type == GDK_VISUAL_STATIC_COLOR) &&
      image_info->visual->depth < 8 &&
      image_info->visual->depth >= 3)
    {
      if (!image_info->cmap)
	image_info->cmap = gdk_colormap_ref (gdk_colormap_get_system ());
      
      gdk_rgb_colorcube_222 (image_info);
    }
  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
    {
      if (!image_info->cmap &&
	  (gdk_rgb_install_cmap || image_info->visual != gdk_visual_get_system ()))
725
	{
726 727
	  image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	  image_info->cmap_alloced = TRUE;
728
	}
729
      if (!gdk_rgb_do_colormaps (image_info, image_info->cmap != NULL))
730
	{
731 732 733
	  image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	  image_info->cmap_alloced = TRUE;
	  gdk_rgb_do_colormaps (image_info, TRUE);
734
	}
735 736 737 738 739 740 741 742 743
      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)
	image_info->cmap = gdk_colormap_ref (gdk_colormap_get_system ());
    }
744
#ifdef ENABLE_GRAYSCALE
745 746 747
  else if (image_info->visual->type == GDK_VISUAL_GRAYSCALE)
    {
      if (!image_info->cmap)
748 749 750
	{
	  image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	  image_info->cmap_alloced = TRUE;
751 752 753 754
	}
      
      gdk_rgb_set_gray_cmap (image_info, image_info->cmap);
    }
755
#endif
756 757 758
  else
    {
      if (!image_info->cmap)
759 760 761 762
	{
	  /* Always install colormap in direct color. */
	  if (image_info->visual->type != GDK_VISUAL_DIRECT_COLOR &&
	      image_info->visual == gdk_visual_get_system ())
763
	    image_info->cmap = gdk_colormap_ref (gdk_colormap_get_system ());
764 765 766 767 768 769
	  else
	    {
	      image_info->cmap = gdk_colormap_new (image_info->visual, FALSE);
	      image_info->cmap_alloced = TRUE;
	    }
	}
770
    }
771

772
  image_info->bitmap = (image_info->visual->depth == 1);
773

774
  /* Try to allocate as few possible shared images */
775 776 777
  for (i=0; i < G_N_ELEMENTS (possible_n_images); i++)
    {
      if (gdk_rgb_allocate_images (image_info, possible_n_images[i], TRUE))
778
	{
779 780
	  image_info->n_images = possible_n_images[i];
	  break;
781
	}
782
    }
783

784 785 786 787 788 789
  /* If that fails, just allocate N_REGIONS normal images */
  if (i == G_N_ELEMENTS (possible_n_images))
    {
      gdk_rgb_allocate_images (image_info, N_REGIONS, FALSE);
      image_info->n_images = N_REGIONS;
    }
790

791
  image_info->bpp = image_info->static_image[0]->bpp;
792

793
  image_info->static_image_idx = 0;
794

795 796 797 798
  image_info->horiz_y = REGION_HEIGHT;
  image_info->vert_x = REGION_WIDTH;
  image_info->tile_x = REGION_WIDTH;
  image_info->tile_y1 = image_info->tile_y2 = REGION_HEIGHT;
799

800 801 802 803 804 805 806 807
  gdk_rgb_select_conv (image_info, image_info->static_image[0]);

  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;
808 809
}

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
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;
}

static gulong
gdk_rgb_xpixel_from_rgb_internal (GdkColormap *colormap,
				  guint16 r, guint16 g, guint16 b)
843
{
844
  gulong pixel = 0;
845

846 847
  GdkRgbInfo *image_info = gdk_rgb_get_info_from_colormap (colormap);

848 849
  if (image_info->bitmap)
    {
850
      return (r + (g << 1) + b) > 131070;
851 852
    }
  else if (image_info->visual->type == GDK_VISUAL_PSEUDO_COLOR)
853 854 855
    pixel = image_info->colorcube[((r & 0xf000) >> 4) |
				  ((g & 0xf000) >> 8) |
				  ((b & 0xf000) >> 12)];
856
  else if (image_info->visual->depth < 8 &&
857 858
	   image_info->visual->type == GDK_VISUAL_STATIC_COLOR)
    {
859 860 861
      pixel = image_info->colorcube_d[((r & 0x8000) >> 9) |
				      ((g & 0x8000) >> 12) |
				      ((b & 0x8000) >> 15)];
862
    }
863 864
  else if (image_info->visual->type == GDK_VISUAL_TRUE_COLOR ||
	   image_info->visual->type == GDK_VISUAL_DIRECT_COLOR)
865
    {
866
#ifdef VERBOSE
867 868 869 870 871 872 873
      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);
874
#endif
875

876 877 878
      pixel = (((r >> (16 - image_info->visual->red_prec)) << image_info->visual->red_shift) +
	       ((g >> (16 - image_info->visual->green_prec)) << image_info->visual->green_shift) +
	       ((b >> (16 - image_info->visual->blue_prec)) << image_info->visual->blue_shift));
879
    }
880 881 882
  else if (image_info->visual->type == GDK_VISUAL_STATIC_GRAY ||
	   image_info->visual->type == GDK_VISUAL_GRAYSCALE)
    {
883 884
      int gray = r + g * 2 + b;
      return gray >> (18 - image_info->visual->depth);
885
    }
886

887 888 889
  return pixel;
}

890 891 892 893 894 895 896 897 898 899 900 901
/* 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;

  return gdk_rgb_xpixel_from_rgb_internal (gdk_rgb_get_colormap(),
					   (r >> 8) + (r >> 16), g + (g >> 8), b + (b << 8));
}

902 903 904 905
void
gdk_rgb_gc_set_foreground (GdkGC *gc, guint32 rgb)
{
  GdkColor color;
906

907 908 909 910 911 912 913 914
  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;
915

916 917 918 919
  color.pixel = gdk_rgb_xpixel_from_rgb (rgb);
  gdk_gc_set_background (gc, &color);
}

920 921 922 923 924 925 926
void
gdk_rgb_find_color (GdkColormap *colormap, GdkColor *color)
{
  color->pixel = gdk_rgb_xpixel_from_rgb_internal (colormap,
						   color->red, color->green, color->blue);
}

927
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
928 929 930 931 932
#define HAIRY_CONVERT_8
#endif

#ifdef HAIRY_CONVERT_8
static void
933
gdk_rgb_convert_8 (GdkRgbInfo *image_info, GdkImage *image,
934 935 936 937 938 939 940 941 942
		   gint x0, gint y0, gint width, gint height,
		   guchar *buf, int rowstride,
		   gint x_align, gint y_align, GdkRgbCmap *cmap)
{
  int x, y;
  gint bpl;
  guchar *obuf, *obptr;
  guchar *bptr, *bp2;
  gint r, g, b;
943
  guchar *colorcube = image_info->colorcube;
944

945 946 947 948 949 950 951
  bptr = buf;
  bpl = image->bpl;
  obuf = ((guchar *)image->mem) + y0 * bpl + x0;
  for (y = 0; y < height; y++)
    {
      bp2 = bptr;
      obptr = obuf;
952
      if (((unsigned long)obuf | (unsigned long) bp2) & 3)
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
	{
	  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;
972

973 974 975 976 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
	      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
1009
gdk_rgb_convert_8 (GdkRgbInfo *image_info, GdkImage *image,
1010 1011 1012 1013 1014 1015 1016 1017 1018
		   gint x0, gint y0, gint width, gint height,
		   guchar *buf, int rowstride,
		   gint x_align, gint y_align, GdkRgbCmap *cmap)
{
  int x, y;
  gint bpl;
  guchar *obuf, *obptr;
  guchar *bptr, *bp2;
  gint r, g, b;
1019
  guchar *colorcube = image_info->colorcube;
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
  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

#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
1051
#define DM_WIDTH_SHIFT 7
1052
#define DM_HEIGHT 128
Owen Taylor's avatar
Owen Taylor committed
1053
static const guchar DM[128][128] =
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 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
{
  { 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, 61, 34, 3, 42, 11, 22, 46, 7, 61, 10, 42, 3, 55, 32, 1, 58, 28, 44, 54, 4, 34, 23, 15, 56, 20, 37, 58, 6, 30, 38, 18, 63, 9, 32, 5, 51, 3, 62, 37, 52, 18, 39, 23, 3, 51, 9, 47, 1, 23, 43, 15, 60, 35, 11, 40, 1, 36, 31, 26, 57, 2, 37, 54, 18, 44, 58, 16 },
  { 5, 51, 3, 33, 43, 62, 21, 42, 35, 9, 48, 15, 36, 10, 22, 42, 20, 46, 26, 56, 50, 12, 59, 3, 48, 19, 45, 53, 1, 27, 47, 17, 52, 24, 56, 11, 51, 21, 37, 30, 20, 46, 14, 41, 1, 47, 33, 7, 41, 17, 35, 27, 20, 1, 14, 54, 26, 33, 18, 47, 1, 44, 14, 59, 16, 52, 28, 18, 49, 31, 25, 34, 63, 13, 51, 24, 9, 50, 3, 23, 38, 63, 7, 52, 29, 46, 11, 33, 50, 22, 57, 36, 1, 57, 49, 17, 39, 28, 9, 35, 6, 27, 53, 15, 55, 30, 24, 58, 36, 41, 11, 52, 32, 3, 44, 25, 62, 23, 51, 15, 42, 22, 50, 10, 39, 4, 31, 35 },
  { 46, 22, 57, 17, 12, 39, 26, 5, 31, 59, 1, 45, 27, 62, 52, 7, 58, 33, 6, 18, 39, 22, 33, 41, 57, 5, 35, 18, 40, 16, 60, 5, 29, 42, 7, 39, 27, 44, 9, 47, 8, 26, 54, 22, 51, 29, 24, 49, 15, 61, 4, 51, 31, 63, 43, 6, 50, 8, 39, 12, 53, 37, 23, 30, 40, 6, 62, 43, 14, 53, 2, 49, 7, 36, 17, 41, 61, 37, 18, 56, 11, 18, 44, 35, 2, 19, 61, 0, 41, 14, 8, 30, 43, 12, 24, 46, 14, 54, 42, 21, 44, 61, 10, 46, 37, 11, 44, 7, 18, 63, 20, 29, 7, 49, 28, 54, 8, 43, 4, 48, 18, 63, 12, 29, 48, 24, 59, 20 },
  { 13, 36, 28, 54, 35, 2, 56, 46, 16, 49, 22, 40, 11, 34, 14, 43, 29, 12, 63, 48, 2, 61, 7, 15, 28, 30, 50, 9, 61, 33, 38, 23, 54, 13, 61, 33, 3, 59, 16, 35, 58, 40, 5, 38, 13, 57, 3, 58, 37, 21, 45, 12, 39, 7, 35, 30, 13, 56, 22, 62, 27, 6, 55, 10, 48, 21, 33, 2, 38, 23, 40, 20, 44, 29, 59, 4, 26, 12, 33, 47, 28, 53, 31, 13, 59, 41, 27, 49, 26, 54, 45, 16, 53, 21, 35, 7, 59, 26, 11,