Back to index

lightning-sunbird  0.9+nobinonly
xlibrgb.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040  * This code is derived from GdkRgb.
00041  * For more information on GdkRgb, see http://www.levien.com/gdkrgb/
00042  * Raph Levien <raph@acm.org>
00043  */
00044 
00045 /* Ported by Christopher Blizzard to Xlib.  With permission from the
00046  * original authors and the copyright holders of this file, the
00047  * contents of this file are also redistributable under the terms of
00048  * the Mozilla Public license.  For information about the Mozilla
00049  * Public License, please see the license information at
00050  * http://www.mozilla.org/MPL/ */
00051 
00052 /* This code is copyright the following authors:
00053  * Raph Levien          <raph@acm.org>
00054  * Manish Singh         <manish@gtk.org>
00055  * Tim Janik            <timj@gtk.org>
00056  * Peter Mattis         <petm@xcf.berkeley.edu>
00057  * Spencer Kimball      <spencer@xcf.berkeley.edu>
00058  * Josh MacDonald       <jmacd@xcf.berkeley.edu>
00059  * Christopher Blizzard <blizzard@redhat.com>
00060  * Owen Taylor          <otaylor@redhat.com>
00061  * Shawn T. Amundson    <amundson@gtk.org>
00062  * Roland Mainz         <roland.mainz@informatik.med.uni-giessen.de>
00063  */
00064 
00065 #include <math.h>
00066 
00067 #if HAVE_CONFIG_H
00068 #  include <config.h>
00069 #  if STDC_HEADERS
00070 #    include <stdio.h>
00071 #    include <stdlib.h>
00072 #    include <string.h>
00073 #  endif
00074 #else
00075 #  include <stdio.h>
00076 #  include <stdlib.h>
00077 #endif /* HAVE_CONFIG_H */
00078 
00079 #define ENABLE_GRAYSCALE
00080 
00081 #define G_LITTLE_ENDIAN 1
00082 #define G_BIG_ENDIAN    2
00083 
00084 /* include this before so that we can get endian definitions if
00085    they are there... */
00086 
00087 #include "xlibrgb.h"
00088 
00089 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
00090 #include <sys/ipc.h>
00091 #include <sys/shm.h>
00092 #include <X11/extensions/XShm.h>
00093 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */
00094 
00095 /* check our endianness */
00096 #ifdef USE_MOZILLA_TYPES
00097 
00098 /* check to make sure that we don't have both types defined. */
00099 #ifdef IS_LITTLE_ENDIAN
00100 #ifdef IS_BIG_ENDIAN
00101 #error what the hell?  both endian types are defined!
00102 #endif
00103 #endif
00104 
00105 #ifdef IS_LITTLE_ENDIAN
00106 #define G_BYTE_ORDER G_LITTLE_ENDIAN
00107 #else
00108 #define G_BYTE_ORDER G_BIG_ENDIAN
00109 #endif /* IS_LITTLE_ENDIAN */
00110 
00111 #else
00112 
00113 /* XXX fix this for an autodetect for endian-ness */
00114 #define G_BYTE_ORDER G_LITTLE_ENDIAN
00115 
00116 #endif /* USE_MOZILLA_TYPES */
00117 
00118 #ifndef MIN
00119 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
00120 #endif
00121 
00122 #ifndef MAX
00123 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
00124 #endif
00125 
00126 typedef enum {
00127   LSB_FIRST,
00128   MSB_FIRST
00129 } ByteOrder;
00130 
00131 static void xxlib_deregister_handle_by_handle(XlibRgbHandle *handle);
00132 
00133 typedef void (*XlibRgbConvFunc) (XlibRgbHandle *handle,
00134                                  XImage *image,
00135                                  int ax, int ay,
00136                                  int width, int height,
00137                                  unsigned char *buf, int rowstride,
00138                                  int x_align, int y_align,
00139                                  XlibRgbCmap *cmap);
00140 
00141 #define IMAGE_WIDTH 256
00142 #define STAGE_ROWSTRIDE (IMAGE_WIDTH * 3)
00143 #define IMAGE_HEIGHT 128
00144 #define N_IMAGES 6
00145 
00146 /* Some of these fields should go, as they're not being used at all.
00147    Globals should generally migrate into here - it's very likely that
00148    we'll want to run more than one GdkRgbInfo context at the same time
00149    (i.e. some but not all windows have privately installed
00150    colormaps). */
00151 
00152 struct _XlibRgbHandle
00153 {
00154   Display          *display;
00155   Screen           *screen;
00156   int               screen_num;
00157   XVisualInfo      *x_visual_info;
00158   Colormap          cmap;
00159   Visual           *default_visualid;
00160   Colormap          default_colormap;
00161 
00162   unsigned long    *color_pixels;
00163   unsigned long    *gray_pixels;
00164   unsigned long    *reserved_pixels;
00165 
00166   unsigned long     red_shift;
00167   unsigned long     red_prec;
00168   unsigned long     blue_shift;
00169   unsigned long     blue_prec;
00170   unsigned long     green_shift;
00171   unsigned long     green_prec;
00172 
00173   unsigned int      nred_shades;
00174   unsigned int      ngreen_shades;
00175   unsigned int      nblue_shades;
00176   unsigned int      ngray_shades;
00177   unsigned int      nreserved;
00178 
00179   unsigned int      bpp; /* bytes per pixel */
00180   unsigned int      cmap_alloced;
00181   double            gamma_val;
00182 
00183   /* Generally, the stage buffer is used to convert 32bit RGB, gray,
00184      and indexed images into 24 bit packed RGB. */
00185   unsigned char *stage_buf;
00186 
00187   XlibRgbCmap *gray_cmap;
00188 
00189   Bool dith_default;
00190 
00191   Bool bitmap; /* set true if in 1 bit per pixel mode */
00192   GC own_gc;
00193 
00194   /* Convert functions */
00195   XlibRgbConvFunc conv;
00196   XlibRgbConvFunc conv_d;
00197 
00198   XlibRgbConvFunc conv_32;
00199   XlibRgbConvFunc conv_32_d;
00200 
00201   XlibRgbConvFunc conv_gray;
00202   XlibRgbConvFunc conv_gray_d;
00203 
00204   XlibRgbConvFunc conv_indexed;
00205   XlibRgbConvFunc conv_indexed_d;
00206 
00207   /* misc */
00208   Bool            xlib_rgb_install_cmap; /* default: FALSE */
00209   Bool            xlib_rgb_verbose;      /* default: FALSE */
00210   XImage         *static_image[N_IMAGES];
00211   int             static_image_idx;
00212   char           *static_buffer;
00213   
00214   uint32         *DM_565;
00215 
00216   unsigned char  *colorcube;
00217   unsigned char  *colorcube_d;
00218  
00219   int             xlib_rgb_min_colors;
00220 
00221   /* image tiling code... */
00222   Bool            disallow_image_tiling; /* default: FALSE */
00223   int             horiz_idx;
00224   int             horiz_y;
00225   int             vert_idx;
00226   int             vert_x;
00227   int             tile_idx;
00228   int             tile_x;
00229   int             tile_y1;
00230   int             tile_y2;
00231 
00232 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
00233   Bool            xlib_use_shm;
00234 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */
00235   long            max_request_size;
00236 };
00237 
00238 static XImage *
00239 xxlib_normal_ximage(XlibRgbHandle *handle)
00240 {
00241   XImage *ximage;
00242 
00243   if (handle->bitmap) {
00244     ximage = XCreateImage(handle->display,
00245                           handle->x_visual_info->visual,
00246                           1,
00247                           XYBitmap,
00248                           0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,
00249                           8,
00250                           0);
00251   }
00252   else 
00253   {  
00254     ximage = XCreateImage(handle->display,
00255                           handle->x_visual_info->visual,
00256                           (unsigned int)handle->x_visual_info->depth,
00257                           ZPixmap,
00258                           0, 0,
00259                           IMAGE_WIDTH,
00260                           IMAGE_HEIGHT,
00261                           32, 0);
00262   }
00263 
00264   if (!ximage)
00265     return NULL;
00266     
00267   ximage->data = malloc(ximage->height * ximage->bytes_per_line);
00268   if (!ximage->data)
00269   {
00270     XDestroyImage(ximage);
00271     return NULL;
00272   }
00273   
00274   ximage->bitmap_bit_order = MSBFirst;
00275   ximage->byte_order = MSBFirst;
00276     
00277   return ximage;
00278 }
00279 
00280 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
00281 static XImage *
00282 xshm_image_new(XlibRgbHandle *handle)
00283 {
00284   XShmSegmentInfo *shm_info;
00285   XImage          *ximage;
00286     
00287   shm_info = calloc(1, sizeof(XShmSegmentInfo));
00288   if (!shm_info)
00289     return NULL;
00290     
00291   shm_info->readOnly = True;
00292 
00293   if (handle->bitmap)
00294   {
00295     ximage = XShmCreateImage(handle->display,
00296                              handle->x_visual_info->visual,
00297                              1,
00298                              XYBitmap, NULL, shm_info, IMAGE_WIDTH, IMAGE_HEIGHT);
00299   }
00300   else
00301   {
00302     ximage = XShmCreateImage(handle->display,
00303                              handle->x_visual_info->visual,
00304                              (unsigned int)handle->x_visual_info->depth,
00305                              ZPixmap, NULL, shm_info, IMAGE_WIDTH, IMAGE_HEIGHT);
00306   }
00307 
00308   /* most likely shm extension is not loaded */
00309   if (!ximage)
00310   {
00311     free(shm_info);
00312     return NULL;
00313   }
00314 
00315   /* allocate a shm chunk of desired size... */
00316   shm_info->shmid = shmget(IPC_PRIVATE,
00317                            ximage->height * ximage->bytes_per_line,
00318                            IPC_CREAT | 0777);
00319 
00320   /* image is probably > SHMMAX */
00321   if (shm_info->shmid == -1)
00322   {
00323     XDestroyImage(ximage);
00324     free(shm_info);
00325     return NULL;
00326   }
00327 
00328   /* find out where in memory it was placed */
00329   shm_info->shmaddr = shmat(shm_info->shmid, NULL, 0);
00330 
00331   if (shm_info->shmaddr == (void *)-1) {
00332     XDestroyImage(ximage);
00333     shmctl(shm_info->shmid, IPC_RMID, NULL);
00334     free(shm_info);
00335     return NULL;
00336   }
00337 
00338   /* image data in shared memory */
00339   ximage->data = shm_info->shmaddr;
00340   XShmAttach(handle->display, shm_info);
00341   XSync(handle->display, False);
00342 
00343   /* set the shm segment as deleted so that it's deleted automatically */
00344   shmctl(shm_info->shmid, IPC_RMID, NULL);
00345   
00346   return ximage;
00347 }
00348 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */
00349 
00350 static void
00351 xxlib_image_put(XlibRgbHandle *handle,
00352                Drawable drawable, GC gc,
00353                XImage *image,
00354                int xsrc, int ysrc,
00355                int xdest, int ydest,
00356                int width, int height)
00357 {
00358 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
00359   if (handle->xlib_use_shm)
00360   {
00361     XShmPutImage(handle->display, drawable,
00362                  gc, image,
00363                  xsrc, ysrc, xdest, ydest, width, height, False);
00364   }  
00365   else
00366 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */  
00367   {
00368     XPutImage(handle->display, drawable, gc, image,
00369               xsrc, ysrc, xdest, ydest,
00370               (unsigned int)width, (unsigned int)height);
00371   }
00372 }
00373 
00374 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
00375 static void
00376 xshm_image_check(XlibRgbHandle *handle, XlibRgbArgs *args)
00377 {
00378   int dummy; 
00379   
00380   handle->xlib_use_shm = False;
00381 
00382   if(!args->disallow_mit_shmem)
00383     /* XlibRGB MIT-SHM support requires the image tiling hack */
00384     if(!args->disallow_image_tiling)
00385       if (getenv("MOZILLA_XXLIBRGB_DISABLE_MIT_SHM") == NULL)
00386         if (XShmQueryExtension(handle->display))
00387           if (XShmQueryVersion(handle->display,
00388                                &dummy, &dummy, &dummy) == True)
00389             handle->xlib_use_shm = True;
00390 }
00391 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */
00392 
00393 unsigned long
00394 xxlib_get_prec_from_mask(unsigned long val)
00395 {
00396   unsigned long retval = 0;
00397   unsigned int cur_bit = 0;
00398   /* walk through the number, incrementing the value if
00399      the bit in question is set. */
00400   while (cur_bit < (sizeof(unsigned long) * 8)) {
00401     if ((val >> cur_bit) & 0x1) {
00402       retval++;
00403     }
00404     cur_bit++;
00405   }
00406   return retval;
00407 }
00408 
00409 unsigned long
00410 xxlib_get_shift_from_mask(unsigned long val)
00411 {
00412   unsigned long cur_bit = 0;
00413   /* walk through the number, looking for the first 1 */
00414   while (cur_bit < (sizeof(unsigned long) * 8)) {
00415     if ((val >> cur_bit) & 0x1) {
00416       return cur_bit;
00417     }
00418     cur_bit++;
00419   }
00420   return cur_bit;
00421 }
00422 
00423 
00424 static int
00425 xxlib_rgb_cmap_fail (XlibRgbHandle *handle, const char *msg, Colormap cmap, unsigned long *pixels)
00426 {
00427   unsigned long free_pixels[256];
00428   int n_free;
00429   int i;
00430 
00431 #ifdef VERBOSE
00432   printf ("%s", msg);
00433 #endif
00434   n_free = 0;
00435   for (i = 0; i < 256; i++)
00436     if (pixels[i] < 256)
00437       free_pixels[n_free++] = pixels[i];
00438   
00439   if (n_free)
00440     XFreeColors(handle->display,
00441                 cmap,
00442                 free_pixels,
00443                 n_free,
00444                 0);
00445   return 0;
00446 }
00447 
00448 static void
00449 xxlib_rgb_make_colorcube (XlibRgbHandle *handle, unsigned long *pixels, int nr, int ng, int nb)
00450 {
00451   unsigned char rt[16], gt[16], bt[16];
00452   int i;
00453 
00454   handle->colorcube = calloc(4096, 1);
00455   for (i = 0; i < 16; i++)
00456   {
00457     rt[i] = ng * nb * ((i * 17 * (nr - 1) + 128) >> 8);
00458     gt[i] = nb * ((i * 17 * (ng - 1) + 128) >> 8);
00459     bt[i] = ((i * 17 * (nb - 1) + 128) >> 8);
00460   }
00461 
00462   for (i = 0; i < 4096; i++)
00463   {
00464     handle->colorcube[i] = pixels[rt[i >> 8] + gt[(i >> 4) & 0x0f] + bt[i & 0x0f]];
00465 #ifdef VERBOSE
00466     printf ("%03x %02x %x %x %x\n", i, handle->colorcube[i], rt[i >> 8], gt[(i >> 4) & 0x0f], bt[i & 0x0f]);
00467 #endif
00468   }
00469 }
00470 
00471 /* this is the colorcube suitable for dithering */
00472 static void
00473 xxlib_rgb_make_colorcube_d (XlibRgbHandle *handle, unsigned long *pixels, int nr, int ng, int nb)
00474 {
00475   int r, g, b;
00476   int i;
00477 
00478   handle->colorcube_d = calloc(512, 1);
00479   for (i = 0; i < 512; i++)
00480   {
00481     r = MIN (nr - 1, i >> 6);
00482     g = MIN (ng - 1, (i >> 3) & 7);
00483     b = MIN (nb - 1, i & 7);
00484     handle->colorcube_d[i] = pixels[(r * ng + g) * nb + b];
00485   }
00486 }
00487 
00488 /* Try installing a color cube of the specified size.
00489    Make the colorcube and return TRUE on success */
00490 static int
00491 xxlib_rgb_try_colormap (XlibRgbHandle *handle, int nr, int ng, int nb)
00492 {
00493   int r, g, b;
00494   int ri, gi, bi;
00495   int r0, g0, b0;
00496   Colormap     cmap;
00497   XVisualInfo *visual;
00498   XColor      *colors = NULL;
00499   XColor       color;
00500   unsigned long pixels[256];
00501   unsigned long junk[256];
00502   int i;
00503   int d2;
00504   unsigned int colors_needed;
00505   int idx;
00506   int best[256];
00507 
00508   if (nr * ng * nb < handle->xlib_rgb_min_colors)
00509     return FALSE;
00510 
00511   if (handle->cmap_alloced) {
00512     cmap = handle->cmap;
00513     visual = handle->x_visual_info;
00514   }
00515   else {
00516     cmap = handle->default_colormap;
00517     visual = handle->x_visual_info;
00518   }
00519   colors_needed = nr * ng * nb;
00520   for (i = 0; i < 256; i++)
00521   {
00522     best[i] = 192;
00523     pixels[i] = 256;
00524   }
00525 
00526 #ifndef GAMMA
00527   if (!handle->xlib_rgb_install_cmap) {
00528     /* go out and get the colors for this colormap. */
00529     colors = malloc(sizeof(XColor) * visual->colormap_size);
00530     for (i=0; i < visual->colormap_size; i++){
00531       colors[i].pixel = i;
00532     }
00533     XQueryColors (handle->display,
00534                   cmap,
00535                   colors, visual->colormap_size);
00536     /* find color cube colors that are already present */
00537     for (i = 0; i < MIN (256, visual->colormap_size); i++)
00538     {
00539       r = colors[i].red >> 8;
00540       g = colors[i].green >> 8;
00541       b = colors[i].blue >> 8;
00542       ri = (r * (nr - 1) + 128) >> 8;
00543       gi = (g * (ng - 1) + 128) >> 8;
00544       bi = (b * (nb - 1) + 128) >> 8;
00545       r0 = ri * 255 / (nr - 1);
00546       g0 = gi * 255 / (ng - 1);
00547       b0 = bi * 255 / (nb - 1);
00548       idx = ((ri * nr) + gi) * nb + bi;
00549       d2 = (r - r0) * (r - r0) + (g - g0) * (g - g0) + (b - b0) * (b - b0);
00550       if (d2 < best[idx]) 
00551       {
00552         if (pixels[idx] < 256)
00553         XFreeColors(handle->display,
00554                     cmap,
00555                     pixels + idx,
00556                     1, 0);
00557         else
00558           colors_needed--;
00559         color.pixel = colors[i].pixel;
00560         color.red = colors[i].red;
00561         color.green = colors[i].green;
00562         color.blue = colors[i].blue;
00563         color.flags = 0;
00564         if (!XAllocColor(handle->display, cmap, &color))
00565           return xxlib_rgb_cmap_fail (handle, "error allocating system color\n",
00566         cmap, pixels);
00567         pixels[idx] = color.pixel; /* which is almost certainly i */
00568         best[idx] = d2;
00569       }
00570     }
00571   }
00572 
00573 #endif /* !GAMMA */
00574 
00575   if (colors_needed)
00576   {
00577     if (!XAllocColorCells(handle->display, cmap, 0, NULL, 0, junk, colors_needed))
00578     {
00579       char tmp_str[80];
00580       
00581       sprintf (tmp_str,
00582                "%d %d %d colormap failed (in XAllocColorCells)\n",
00583                nr, ng, nb);
00584       return xxlib_rgb_cmap_fail (handle, tmp_str, cmap, pixels);
00585     }
00586     XFreeColors(handle->display, cmap, junk, (int)colors_needed, 0);
00587   }
00588 
00589   for (r = 0, i = 0; r < nr; r++)
00590     for (g = 0; g < ng; g++)
00591       for (b = 0; b < nb; b++, i++)
00592       {
00593         if (pixels[i] == 256)
00594           {
00595             color.red = r * 65535 / (nr - 1);
00596             color.green = g * 65535 / (ng - 1);
00597             color.blue = b * 65535 / (nb - 1);
00598 
00599 #ifdef GAMMA
00600             color.red = 65535 * pow (color.red / 65535.0, 0.5);
00601             color.green = 65535 * pow (color.green / 65535.0, 0.5);
00602             color.blue = 65535 * pow (color.blue / 65535.0, 0.5);
00603 #endif /* GAMMA */
00604 
00605             /* This should be a raw XAllocColor call */
00606             if (!XAllocColor(handle->display, cmap, &color))
00607             {
00608               char tmp_str[80];
00609 
00610               sprintf (tmp_str, "%d %d %d colormap failed\n",
00611                        nr, ng, nb);
00612               return xxlib_rgb_cmap_fail (handle, tmp_str,
00613                                           cmap, pixels);
00614             }
00615             pixels[i] = color.pixel;
00616           }
00617 #ifdef VERBOSE
00618           printf ("%d: %lx\n", i, pixels[i]);
00619 #endif /* VERBOSE */
00620         }
00621 
00622   handle->nred_shades = nr;
00623   handle->ngreen_shades = ng;
00624   handle->nblue_shades = nb;
00625   xxlib_rgb_make_colorcube (handle, pixels, nr, ng, nb);
00626   xxlib_rgb_make_colorcube_d (handle, pixels, nr, ng, nb);
00627   if (colors)
00628     free(colors);
00629   return TRUE;
00630 }
00631 
00632 /* Return TRUE on success. */
00633 static Bool
00634 xxlib_rgb_do_colormaps (XlibRgbHandle *handle)
00635 {
00636   static const int sizes[][3] = 
00637   {
00638     /*    { 6, 7, 6 }, */
00639     { 6, 6, 6 }, 
00640     { 6, 6, 5 }, 
00641     { 6, 6, 4 }, 
00642     { 5, 5, 5 }, 
00643     { 5, 5, 4 }, 
00644     { 4, 4, 4 }, 
00645     { 4, 4, 3 }, 
00646     { 3, 3, 3 }, 
00647     { 2, 2, 2 }
00648   };
00649   static const int n_sizes = sizeof(sizes) / (3 * sizeof(int));
00650   int i;
00651   
00652   for (i = 0; i < n_sizes; i++)
00653     if (xxlib_rgb_try_colormap (handle, sizes[i][0], sizes[i][1], sizes[i][2]))
00654       return TRUE;
00655   return FALSE;
00656 }
00657 
00658 /* Make a 2 x 2 x 2 colorcube */
00659 static void
00660 xxlib_rgb_colorcube_222 (XlibRgbHandle *handle)
00661 {
00662   int i;
00663   XColor color;
00664   Colormap cmap;
00665 
00666   if (handle->cmap_alloced)
00667     cmap = handle->cmap;
00668   else
00669     cmap = handle->default_colormap;
00670 
00671   handle->colorcube_d = malloc(sizeof(unsigned char) * 512);
00672 
00673   for (i = 0; i < 8; i++)
00674   {
00675     color.red = ((i & 4) >> 2) * 65535;
00676     color.green = ((i & 2) >> 1) * 65535;
00677     color.blue = (i & 1) * 65535;
00678     XAllocColor (handle->display, cmap, &color);
00679     handle->colorcube_d[((i & 4) << 4) | ((i & 2) << 2) | (i & 1)] = color.pixel;
00680   }
00681 }
00682 
00683 /* Make a 6 x 7 x 6 colorcube */
00684 static void
00685 xxlib_rgb_colorcube_676 (XlibRgbHandle *handle)
00686 {
00687   int i = 0;
00688   XColor color;
00689   Colormap cmap;
00690   int r, g, b, r_max, g_max, b_max;
00691   unsigned long pixels[256];
00692   
00693   r_max = 6;
00694   g_max = 7;
00695   b_max = 6;
00696 
00697   if (handle->cmap_alloced)
00698     cmap = handle->cmap;
00699   else
00700     cmap = handle->default_colormap;
00701 
00702   for (r = 0; r < r_max; r++)
00703   {
00704     for (g = 0; g < g_max; g++)
00705     {
00706       for (b = 0; b < b_max; b++)
00707       {        
00708         color.red   = r * 65535 / (r_max-1);
00709         color.green = g * 65535 / (g_max-1);
00710         color.blue  = b * 65535 / (b_max-1);
00711         XAllocColor (handle->display, cmap, &color);
00712         pixels[i++] = color.pixel;
00713       }
00714     }
00715   }    
00716       
00717   handle->nred_shades   = r_max;
00718   handle->ngreen_shades = g_max;
00719   handle->nblue_shades  = b_max;
00720   xxlib_rgb_make_colorcube   (handle, pixels, r_max, g_max, b_max);
00721   xxlib_rgb_make_colorcube_d (handle, pixels, r_max, g_max, b_max);      
00722 }
00723 
00724 void
00725 xxlib_rgb_set_verbose (XlibRgbHandle *handle, Bool verbose)
00726 {
00727   handle->xlib_rgb_verbose = verbose;
00728 }
00729 
00730 void
00731 xxlib_rgb_set_min_colors (XlibRgbHandle *handle, int min_colors)
00732 {
00733   handle->xlib_rgb_min_colors = min_colors;
00734 }
00735 
00736 /* Return a "score" based on the following criteria (in hex):
00737 
00738    x000 is the quality - 1 is 1bpp, 2 is 4bpp,
00739                          4 is 8bpp,
00740                          7 is 15bpp truecolor, 8 is 16bpp truecolor,
00741                          9 is 24bpp truecolor.
00742    0x00 is the speed - 1 is the normal case,
00743                        2 means faster than normal
00744    00x0 gets a point for being the system visual
00745    000x gets a point for being pseudocolor
00746 
00747    A caveat: in the 8bpp modes, being the system visual seems to be
00748    quite important. Thus, all of the 8bpp modes should be ranked at
00749    the same speed.
00750 */
00751 
00752 static uint32
00753 xxlib_rgb_score_visual (XlibRgbHandle *handle, XVisualInfo *visual)
00754 {
00755   uint32 quality, speed, pseudo, sys;
00756 #ifdef DEBUG
00757   static const char* visual_names[] =
00758   {
00759     "static gray",
00760     "grayscale",
00761     "static color",
00762     "pseudo color",
00763     "true color",
00764     "direct color",
00765   };
00766 #endif /* DEBUG */
00767   
00768   quality = 0;
00769   speed = 1;
00770   sys = 0;
00771   if (visual->class == TrueColor ||
00772       visual->class == DirectColor)
00773   {
00774     if (visual->depth == 24)
00775     {
00776       quality = 9;
00777       /* Should test for MSB visual here, and set speed if so. */
00778     }
00779     else if (visual->depth == 16)
00780       quality = 8;
00781     else if (visual->depth == 15)
00782       quality = 7;
00783     else if (visual->depth == 8)
00784       quality = 4;
00785     }
00786     else if (visual->class == PseudoColor ||
00787            visual->class == StaticColor)
00788     {
00789       if (visual->depth == 8)
00790         quality = 4;
00791       else if (visual->depth == 4)
00792         quality = 2;
00793       else if (visual->depth == 1)
00794         quality = 1;
00795     }
00796   else if (visual->class == StaticGray
00797 #ifdef ENABLE_GRAYSCALE
00798            || visual->class == GrayScale
00799 #endif /* ENABLE_GRAYSCALE */
00800            )
00801     {
00802       if (visual->depth == 8)
00803         quality = 4;
00804       else if (visual->depth == 4)
00805         quality = 2;
00806       else if (visual->depth == 1)
00807         quality = 1;
00808     }
00809 
00810   if (quality == 0)
00811     return 0;
00812 
00813   sys = (visual->visualid == handle->default_visualid->visualid);
00814   
00815   pseudo = (visual->class == PseudoColor || visual->class == TrueColor);
00816 
00817 #ifdef DEBUG
00818   if (handle->xlib_rgb_verbose)
00819     printf ("Visual 0x%x, type = %s, depth = %d, %ld:%ld:%ld%s; score=%x\n",
00820             (int)visual->visualid,
00821             visual_names[visual->class],
00822             visual->depth,
00823             visual->red_mask,
00824             visual->green_mask,
00825             visual->blue_mask,
00826             sys ? " (system)" : "",
00827             (quality << 12) | (speed << 8) | (sys << 4) | pseudo);
00828 #endif /* DEBUG */
00829   return (quality << 12) | (speed << 8) | (sys << 4) | pseudo;
00830 }
00831 
00832 static Bool
00833 xxlib_rgb_choose_visual (XlibRgbHandle *handle, XVisualInfo *xtemplate, long xtemplate_mask)
00834 {
00835   XVisualInfo *visuals;
00836   XVisualInfo *visual;
00837   XVisualInfo *best_visual;
00838   XVisualInfo *final_visual;
00839   int num_visuals;
00840   uint32 score, best_score;
00841   int cur_visual = 1;
00842   int i;
00843    
00844   xtemplate_mask |= VisualScreenMask;
00845   xtemplate->screen = handle->screen_num;
00846   visuals = XGetVisualInfo(handle->display, xtemplate_mask,
00847                            xtemplate, &num_visuals);
00848   if (!visuals)
00849     return False;
00850     
00851   best_visual = visuals;
00852   best_score = xxlib_rgb_score_visual (handle, best_visual);
00853 
00854   for (i = cur_visual; i < num_visuals; i++)
00855   {
00856     visual = &visuals[i];
00857     score = xxlib_rgb_score_visual (handle, visual);
00858     if (score > best_score)
00859     {
00860       best_score = score;
00861       best_visual = visual;
00862     }
00863   }
00864   /* make a copy of the visual so that we can free
00865    * the allocated visual list above. */
00866   final_visual = malloc(sizeof(XVisualInfo));
00867   memcpy(final_visual, best_visual, sizeof(XVisualInfo));
00868   handle->x_visual_info = final_visual;
00869   XFree(visuals);
00870   
00871   return True;
00872 }
00873 
00874 static void xxlib_rgb_select_conv (XlibRgbHandle *handle, XImage *image, ByteOrder byte_order);
00875 
00876 static void
00877 xxlib_rgb_set_gray_cmap (XlibRgbHandle *handle, Colormap cmap)
00878 {
00879   int i;
00880   XColor color;
00881   int status;
00882   unsigned long pixels[256];
00883   int r, g, b, gray;
00884 
00885   for (i = 0; i < 256; i++)
00886   {
00887     color.pixel = i;
00888     color.red   = i * 257;
00889     color.green = i * 257;
00890     color.blue  = i * 257;
00891     status = XAllocColor(handle->display, cmap, &color);
00892     pixels[i] = color.pixel;
00893 #ifdef VERBOSE
00894     printf ("allocating pixel %d, %x %x %x, result %d\n",
00895             color.pixel, color.red, color.green, color.blue, status);
00896 #endif /* VERBOSE */
00897   }
00898 
00899   /* Now, we make fake colorcubes - we ultimately just use the pseudocolor
00900      methods. */
00901 
00902   handle->colorcube = handle->colorcube_d = malloc(sizeof(unsigned char) * 4096);
00903 
00904   for (i = 0; i < 4096; i++)
00905   {
00906     r = (i >> 4) & 0xf0;
00907     r = r | r >> 4;
00908     g = i & 0xf0;
00909     g = g | g >> 4;
00910     b = (i << 4 & 0xf0);
00911     b = b | b >> 4;
00912     gray = (g + ((r + b) >> 1)) >> 1;
00913     handle->colorcube[i] = handle->colorcube_d[i] = pixels[gray];
00914   }
00915 }
00916 
00917 void
00918 xxlib_rgb_destroy_handle (XlibRgbHandle *handle)
00919 {
00920   int i;
00921 
00922   for( i = 0 ; i < N_IMAGES ; i++ )
00923   {
00924     if (handle->static_image[i])
00925       XDestroyImage(handle->static_image[i]);
00926   }
00927   
00928   if (handle->cmap_alloced)
00929   {
00930     XFreeColormap(handle->display, handle->cmap);
00931   }
00932   
00933   if (handle->own_gc)
00934     XFreeGC(handle->display, handle->own_gc);
00935   
00936   if (handle->colorcube)
00937     free(handle->colorcube);
00938 
00939   if (handle->colorcube_d && (handle->colorcube_d != handle->colorcube))
00940     free(handle->colorcube_d);
00941   
00942   if (handle->DM_565)
00943     free(handle->DM_565);
00944   
00945   if (handle->stage_buf)
00946     free(handle->stage_buf);
00947 
00948   xxlib_deregister_handle_by_handle(handle);
00949   
00950 #ifdef DEBUG
00951     /* force crash if someone tries to touch the handle... */
00952     memset(handle, 0, sizeof(XlibRgbHandle));
00953 #endif  
00954             
00955   /* done... */
00956   free(handle); 
00957 }
00958 
00959 /* Create a XlibRgbHandle object
00960  * It is possible to register the handle automagically with a |name|.
00961  * Handle names must be unique, but it is possible to register a handle 
00962  * with more than one name via |xxlib_register_handle()|.
00963  */                     
00964 XlibRgbHandle *
00965 xxlib_rgb_create_handle(Display *display, Screen *screen,
00966                         XlibRgbArgs *args)
00967 {
00968   XlibRgbHandle *handle = NULL;
00969   int i;
00970   static const int byte_order[1] = { 1 };
00971 
00972 #ifdef DEBUG
00973   printf("xxlib_rgb_create_handle: pseudogray=%d, install_colormap=%d, "
00974          "disallow_image_tiling=%d, disallow_mit_shmem=%d, verbose=%d\n",
00975          (int)args->pseudogray,
00976          (int)args->install_colormap,
00977          (int)args->disallow_image_tiling,
00978          (int)args->disallow_mit_shmem,
00979          (int)args->verbose);
00980 #endif /* DEBUG */
00981 
00982   /* check endian sanity */
00983 #if G_BYTE_ORDER == G_BIG_ENDIAN
00984   if (((char *)byte_order)[0] == 1) {
00985     fprintf (stderr, "xxlib_rgb_create_handle_with_depth: "
00986              "compiled for big endian, but this is a little endian machine.\n\n");
00987     abort();
00988   }
00989 #else
00990   if (((char *)byte_order)[0] != 1) {
00991     fprintf (stderr, "xxlib_rgb_create_handle_with_depth: "
00992              "compiled for little endian, but this is a big endian machine.\n\n");
00993     abort();
00994   }
00995 #endif /* G_BYTE_ORDER == G_BIG_ENDIAN */
00996 
00997 #ifndef XLIBRGB_MIT_SHM_SUPPORT_HAS_BEEN_FIXED
00998     /* We still have issues with MIT_SHM support on LSB platforms... ;-( */
00999     args->disallow_mit_shmem = True;
01000 #endif /* XLIBRGB_MIT_SHM_SUPPORT_HAS_BEEN_FIXED */    
01001        
01002     if (!(handle = malloc(sizeof(XlibRgbHandle))))
01003       return NULL;      
01004     memset(handle, 0, sizeof(XlibRgbHandle));
01005 
01006     /* should we register this handle ? */
01007     if (args->handle_name)
01008     {
01009       if (!xxlib_register_handle(args->handle_name, handle))
01010       {
01011 #ifdef DEBUG
01012         puts("xxlib_rgb_create_handle: cannot register handle");
01013 #endif /* DEBUG */      
01014         free(handle);
01015         return NULL;
01016       }  
01017     }
01018     
01019     /* Emulate StaticGray via PseudoColor visual ? */
01020     if (args->pseudogray)
01021     {
01022       args->xtemplate.class = PseudoColor;
01023       args->xtemplate.depth = 8;
01024       args->xtemplate_mask |= VisualClassMask|VisualDepthMask;
01025     } 
01026         
01027     handle->display = display;
01028     handle->screen = screen;
01029     handle->screen_num = XScreenNumberOfScreen(screen);
01030     handle->x_visual_info = NULL;
01031     handle->cmap = None;
01032     handle->default_visualid = XDefaultVisual(display, handle->screen_num);
01033     handle->default_colormap = XDefaultColormap(display, handle->screen_num);
01034     handle->max_request_size = XMaxRequestSize(display);
01035 
01036 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
01037     xshm_image_check(handle, args);
01038 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */
01039     handle->disallow_image_tiling  = args->disallow_image_tiling;
01040     handle->xlib_rgb_install_cmap  = args->install_colormap;
01041     
01042     handle->color_pixels = NULL;
01043     handle->gray_pixels = NULL;
01044     handle->reserved_pixels = NULL;
01045 
01046     handle->nred_shades = 6;
01047     handle->ngreen_shades = 6;
01048     handle->nblue_shades = 4;
01049     handle->ngray_shades = 24;
01050     handle->nreserved = 0;
01051 
01052     handle->bpp = 0;
01053     handle->cmap_alloced = False;
01054     handle->gamma_val = 1.0;
01055 
01056     handle->stage_buf = NULL;
01057 
01058     handle->own_gc = None;
01059     
01060     handle->red_shift = 0;
01061     handle->red_prec = 0;
01062     handle->green_shift = 0;
01063     handle->green_prec = 0;
01064     handle->blue_shift = 0;
01065     handle->blue_prec = 0;
01066 
01067     handle->xlib_rgb_min_colors = 5 * 5 * 5;
01068 
01069     handle->horiz_y = IMAGE_HEIGHT;
01070     handle->vert_x  = IMAGE_WIDTH;
01071     handle->tile_x  = IMAGE_WIDTH;
01072     handle->tile_y1 = IMAGE_HEIGHT;
01073     handle->tile_y2 = IMAGE_HEIGHT;  
01074 
01075     if (!xxlib_rgb_choose_visual (handle, &args->xtemplate, args->xtemplate_mask))
01076     {
01077 #ifdef DEBUG
01078       puts("xxlib_rgb_choose_visual failure");
01079 #endif /* DEBUG */
01080       xxlib_rgb_destroy_handle(handle);
01081       return NULL;
01082     }
01083 
01084     handle->bitmap = (handle->x_visual_info->depth == 1);
01085       
01086     /* set up the shift and the precision for the red, green and blue.
01087        this only applies to cool visuals like true color and direct color. */
01088     if (handle->x_visual_info->class == TrueColor ||
01089         handle->x_visual_info->class == DirectColor) {
01090       handle->red_shift   = xxlib_get_shift_from_mask(handle->x_visual_info->red_mask);
01091       handle->red_prec    = xxlib_get_prec_from_mask(handle->x_visual_info->red_mask);
01092       handle->green_shift = xxlib_get_shift_from_mask(handle->x_visual_info->green_mask);
01093       handle->green_prec  = xxlib_get_prec_from_mask(handle->x_visual_info->green_mask);
01094       handle->blue_shift  = xxlib_get_shift_from_mask(handle->x_visual_info->blue_mask);
01095       handle->blue_prec   = xxlib_get_prec_from_mask(handle->x_visual_info->blue_mask);
01096     }
01097     
01098     if (args->pseudogray)
01099     {
01100       if (handle->xlib_rgb_install_cmap)
01101       {
01102         handle->cmap = XCreateColormap(handle->display,
01103                                        XRootWindow(handle->display, handle->screen_num),
01104                                        handle->x_visual_info->visual,
01105                                        AllocNone);
01106         xxlib_rgb_set_gray_cmap (handle, handle->cmap);
01107         handle->cmap_alloced = TRUE;
01108       }
01109       else
01110       {
01111         handle->cmap = handle->default_colormap;
01112         xxlib_rgb_set_gray_cmap (handle, handle->cmap);
01113       }  
01114     }
01115     else if ((handle->x_visual_info->class == PseudoColor ||
01116            handle->x_visual_info->class == StaticColor) &&
01117            handle->x_visual_info->depth < 8 &&
01118            handle->x_visual_info->depth >= 3)
01119     {
01120       handle->cmap = handle->default_colormap;
01121       xxlib_rgb_colorcube_222 (handle);
01122     }
01123     else if (handle->x_visual_info->class == StaticColor)
01124     {
01125       handle->cmap = XCreateColormap(handle->display,
01126                                      XRootWindow(handle->display, handle->screen_num),
01127                                      handle->x_visual_info->visual,
01128                                      AllocNone);
01129       handle->cmap_alloced = TRUE;
01130       xxlib_rgb_colorcube_676(handle);
01131     }
01132     else if (handle->x_visual_info->class == PseudoColor)
01133     {
01134       if (handle->xlib_rgb_install_cmap ||
01135           handle->x_visual_info->visualid != handle->default_visualid->visualid)
01136       {
01137         handle->cmap = XCreateColormap(handle->display,
01138                                        XRootWindow(handle->display, handle->screen_num),
01139                                        handle->x_visual_info->visual,
01140                                        AllocNone);
01141         handle->cmap_alloced = TRUE;
01142       }
01143       
01144       if (!xxlib_rgb_do_colormaps (handle))
01145       {
01146         handle->cmap = XCreateColormap(handle->display,
01147                                        XRootWindow(handle->display, handle->screen_num),
01148                                        handle->x_visual_info->visual,
01149                                        AllocNone);
01150         handle->cmap_alloced = TRUE;
01151         xxlib_rgb_do_colormaps (handle);
01152       }
01153       if (handle->xlib_rgb_verbose)
01154         printf ("color cube: %d x %d x %d\n",
01155                 handle->nred_shades,
01156                 handle->ngreen_shades,
01157                 handle->nblue_shades);
01158 
01159       if (!handle->cmap_alloced)
01160         handle->cmap = handle->default_colormap;
01161      }
01162 #ifdef ENABLE_GRAYSCALE
01163     else if (handle->x_visual_info->class == GrayScale)
01164     {
01165       handle->cmap = XCreateColormap(handle->display,
01166                                      XRootWindow(handle->display, handle->screen_num),
01167                                      handle->x_visual_info->visual,
01168                                      AllocNone);
01169       xxlib_rgb_set_gray_cmap (handle, handle->cmap);
01170       handle->cmap_alloced = TRUE;
01171     }
01172 #endif /* ENABLE_GRAYSCALE */
01173     else if (handle->x_visual_info->class != DirectColor && 
01174              handle->x_visual_info->visualid == handle->default_visualid->visualid)
01175     {
01176       handle->cmap = handle->default_colormap;
01177     }  
01178     else
01179     {
01180       /* Always install colormap in direct color. */
01181       handle->cmap = XCreateColormap(handle->display,
01182                                      XRootWindow(handle->display, handle->screen_num),
01183                                      handle->x_visual_info->visual,
01184                                      AllocNone);
01185       handle->cmap_alloced = TRUE;
01186     }
01187 
01188 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
01189     if (handle->xlib_use_shm)
01190     {
01191       /* try to allocate N_IMAGES shm X images */
01192       for (i = 0; i < N_IMAGES; i++)
01193       {
01194         handle->static_image[i] = xshm_image_new(handle);
01195         if (!handle->static_image[i])
01196         {
01197           handle->xlib_use_shm = False;
01198           break;
01199         }
01200       }
01201       
01202       /* Failure ? Then clean-up and use the standard method below... */
01203       if (!handle->xlib_use_shm)
01204       {
01205 #ifdef DEBUG
01206         printf("Failed to set-up MIT-SHMEM after %d images\n", i);
01207 #endif /* DEBUG */
01208         for (i = 0; i < N_IMAGES; i++)
01209         {
01210           if (handle->static_image[i])
01211           {
01212             XDestroyImage(handle->static_image[i]);
01213             handle->static_image[i] = NULL;
01214           }  
01215         } 
01216       }
01217     }  
01218 
01219     /* Allocate "normal" images "*/
01220     if (!handle->xlib_use_shm)
01221 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */
01222     {    
01223       for (i = 0; i < N_IMAGES; i++) 
01224       {
01225         handle->static_image[i] = xxlib_normal_ximage(handle);
01226       }
01227     }  
01228 
01229     /* ok, so apparently, handle->bpp is actually
01230      * BYTES per pixel.  What fun! */
01231     handle->bpp = (handle->static_image[0]->bits_per_pixel + 7) / 8;
01232 
01233     xxlib_rgb_select_conv (handle, handle->static_image[0], MSB_FIRST);
01234     
01235     return handle;
01236 }
01237 
01238 /* convert an rgb value into an X pixel code */
01239 unsigned long
01240 xxlib_rgb_xpixel_from_rgb (XlibRgbHandle *handle, uint32 rgb)
01241 {
01242   unsigned long pixel = 0;
01243 
01244   if (handle->bitmap)
01245   {
01246     return ((rgb & 0xff0000) >> 16) +
01247            ((rgb & 0xff00) >> 7) +
01248             (rgb & 0xff) > 510;
01249   }
01250   else if (handle->x_visual_info->class == PseudoColor)
01251     pixel = handle->colorcube[((rgb & 0xf00000) >> 12) |
01252                               ((rgb & 0xf000) >> 8) |
01253                               ((rgb & 0xf0) >> 4)];
01254   else if (handle->x_visual_info->depth < 8 &&
01255            handle->x_visual_info->class == StaticColor)
01256   {
01257     pixel = handle->colorcube_d[((rgb & 0x800000) >> 17) |
01258                                 ((rgb & 0x8000) >> 12) |
01259                                 ((rgb & 0x80) >> 7)];
01260   }
01261   else if (handle->x_visual_info->class == StaticColor)
01262   {
01263     pixel = handle->colorcube[((rgb & 0xf00000) >> 12) |
01264                               ((rgb & 0xf000) >> 8) |
01265                               ((rgb & 0xf0) >> 4)];  
01266   }
01267   else if (handle->x_visual_info->class == TrueColor ||
01268            handle->x_visual_info->class == DirectColor)
01269   {
01270 #ifdef VERBOSE
01271     printf ("shift, prec: r %d %d g %d %d b %d %d\n",
01272             handle->red_shift,
01273             handle->red_prec,
01274             handle->green_shift,
01275             handle->green_prec,
01276             handle->blue_shift,
01277             handle->blue_prec);
01278 #endif
01279 
01280     pixel = (((((rgb & 0xff0000) >> 16) >>
01281                (8 - handle->red_prec)) <<
01282               handle->red_shift) +
01283              ((((rgb & 0xff00) >> 8)  >>
01284                (8 - handle->green_prec)) <<
01285               handle->green_shift) +
01286              (((rgb & 0xff) >>
01287                (8 - handle->blue_prec)) <<
01288               handle->blue_shift));
01289   }
01290   else if (handle->x_visual_info->class == StaticGray ||
01291            handle->x_visual_info->class == GrayScale)
01292   {
01293     int gray = ((rgb & 0xff0000) >> 16) +
01294                ((rgb & 0xff00) >> 7) +
01295                 (rgb & 0xff);
01296 
01297     return gray >> (10 - handle->x_visual_info->depth);
01298   }
01299 
01300   return pixel;
01301 }
01302 
01303 void
01304 xxlib_rgb_gc_set_foreground (XlibRgbHandle *handle, GC gc, uint32 rgb)
01305 {
01306   unsigned long color;
01307 
01308   color = xxlib_rgb_xpixel_from_rgb (handle, rgb);
01309   XSetForeground(handle->display, gc, color);
01310 }
01311 
01312 void
01313 xxlib_rgb_gc_set_background (XlibRgbHandle *handle, GC gc, uint32 rgb)
01314 {
01315   unsigned long color;
01316 
01317   color = xxlib_rgb_xpixel_from_rgb (handle, rgb);
01318   XSetBackground(handle->display, gc, color);
01319 }
01320 
01321 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
01322 #define HAIRY_CONVERT_8
01323 #endif
01324 
01325 #ifdef HAIRY_CONVERT_8
01326 static void
01327 xxlib_rgb_convert_8 (XlibRgbHandle *handle, XImage *image,
01328                    int ax, int ay, int width, int height,
01329                    unsigned char *buf, int rowstride,
01330                    int x_align, int y_align, XlibRgbCmap *cmap)
01331 {
01332   int x, y;
01333   int bpl;
01334   unsigned char *obuf, *obptr;
01335   unsigned char *bptr, *bp2;
01336   int r, g, b;
01337 
01338   bptr = buf;
01339   bpl = image->bytes_per_line;
01340   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
01341   for (y = 0; y < height; y++)
01342     {
01343       bp2 = bptr;
01344       obptr = obuf;
01345       if (((unsigned long)obuf | (unsigned long) bp2) & 3)
01346         {
01347           for (x = 0; x < width; x++)
01348             {
01349               r = *bp2++;
01350               g = *bp2++;
01351               b = *bp2++;
01352               obptr[0] = handle->colorcube[((r & 0xf0) << 4) |
01353                                   (g & 0xf0) |
01354                                   (b >> 4)];
01355               obptr++;
01356             }
01357         }
01358       else
01359         {
01360           for (x = 0; x < width - 3; x += 4)
01361             {
01362               uint32 r1b0g0r0;
01363               uint32 g2r2b1g1;
01364               uint32 b3g3r3b2;
01365 
01366               r1b0g0r0 = ((uint32 *)bp2)[0];
01367               g2r2b1g1 = ((uint32 *)bp2)[1];
01368               b3g3r3b2 = ((uint32 *)bp2)[2];
01369               ((uint32 *)obptr)[0] =
01370                 handle->colorcube[((r1b0g0r0 & 0xf0) << 4) | 
01371                          ((r1b0g0r0 & 0xf000) >> 8) |
01372                          ((r1b0g0r0 & 0xf00000) >> 20)] |
01373                 (handle->colorcube[((r1b0g0r0 & 0xf0000000) >> 20) |
01374                           (g2r2b1g1 & 0xf0) |
01375                           ((g2r2b1g1 & 0xf000) >> 12)] << 8) |
01376                 (handle->colorcube[((g2r2b1g1 & 0xf00000) >> 12) |
01377                           ((g2r2b1g1 & 0xf0000000) >> 24) |
01378                           ((b3g3r3b2 & 0xf0) >> 4)] << 16) |
01379                 (handle->colorcube[((b3g3r3b2 & 0xf000) >> 4) |
01380                           ((b3g3r3b2 & 0xf00000) >> 16) |
01381                           (b3g3r3b2 >> 28)] << 24);
01382               bp2 += 12;
01383               obptr += 4;
01384             }
01385           for (; x < width; x++)
01386             {
01387               r = *bp2++;
01388               g = *bp2++;
01389               b = *bp2++;
01390               obptr[0] = handle->colorcube[((r & 0xf0) << 4) |
01391                                   (g & 0xf0) |
01392                                   (b >> 4)];
01393               obptr++;
01394             }
01395         }
01396       bptr += rowstride;
01397       obuf += bpl;
01398     }
01399 }
01400 #else
01401 static void
01402 xxlib_rgb_convert_8 (XlibRgbHandle *handle, XImage *image,
01403                    int ax, int ay, int width, int height,
01404                    unsigned char *buf, int rowstride,
01405                    int x_align, int y_align, XlibRgbCmap *cmap)
01406 {
01407   int x, y;
01408   int bpl;
01409   unsigned char *obuf, *obptr;
01410   unsigned char *bptr, *bp2;
01411   int r, g, b;
01412 
01413   bptr = buf;
01414   bpl = image->bytes_per_line;
01415   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
01416   for (y = 0; y < height; y++)
01417     {
01418       bp2 = bptr;
01419       obptr = obuf;
01420       for (x = 0; x < width; x++)
01421         {
01422           r = *bp2++;
01423           g = *bp2++;
01424           b = *bp2++;
01425           obptr[0] = handle->colorcube[((r & 0xf0) << 4) |
01426                               (g & 0xf0) |
01427                               (b >> 4)];
01428           obptr++;
01429         }
01430       bptr += rowstride;
01431       obuf += bpl;
01432     }
01433 }
01434 #endif
01435 
01436 #if 1
01437 
01438 /* This dither table was generated by Raph Levien using patented
01439    technology (US Patent 5,276,535). The dither table itself is in the
01440    public domain. */
01441 
01442 #define DM_WIDTH 128
01443 #define DM_WIDTH_SHIFT 7
01444 #define DM_HEIGHT 128
01445 static const unsigned char DM[128][128] =
01446 {
01447   { 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 },
01448   { 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 },
01449   { 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 },
01450   { 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 },
01451   { 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 },
01452   { 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 },
01453   { 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 },
01454   { 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 },
01455   { 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 },
01456   { 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 },
01457   { 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 },
01458   { 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 },
01459   { 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 },
01460   { 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 },
01461   { 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 },
01462   { 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 },
01463   { 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 },
01464   { 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 },
01465   { 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 },
01466   { 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 },
01467   { 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 },
01468   { 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 },
01469   { 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 },
01470   { 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 },
01471   { 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 },
01472   { 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 },
01473   { 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 },
01474   { 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 },
01475   { 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 },
01476   { 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 },
01477   { 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, 56, 1, 24, 33, 4, 28, 62, 21, 49, 31, 2, 56, 39, 24, 58, 13, 17, 37, 21, 56, 10, 38, 0, 34, 55, 15, 43, 1, 52 },
01478   { 42, 9, 50, 6, 25, 60, 14, 38, 10, 29, 53, 18, 57, 3, 25, 51, 0, 53, 25, 17, 29, 37, 52, 46, 0, 62, 14, 37, 4, 50, 10, 44, 0, 46, 20, 25, 50, 19, 55, 0, 23, 31, 62, 34, 11, 45, 19, 32, 0, 53, 10, 59, 23, 47, 18, 60, 42, 28, 37, 3, 50, 15, 35, 44, 0, 51, 27, 60, 9, 57, 16, 58, 11, 22, 46, 15, 53, 48, 7, 42, 0, 60, 5, 49, 24, 54, 9, 17, 39, 5, 34, 62, 3, 40, 60, 31, 0, 47, 29, 16, 49, 39, 59, 17, 50, 0, 40, 13, 53, 38, 16, 46, 0, 42, 34, 60, 2, 53, 29, 31, 58, 46, 27, 6, 61, 8, 37, 28 },
01479   { 0, 63, 21, 40, 45, 18, 51, 23, 63, 34, 6, 43, 28, 38, 55, 19, 40, 35, 8, 41, 54, 10, 21, 32, 39, 23, 53, 26, 55, 28, 22, 63, 30, 34, 9, 48, 6, 38, 29, 43, 49, 6, 18, 52, 27, 61, 9, 43, 28, 42, 33, 26, 56, 3, 51, 23, 0, 48, 16, 45, 32, 25, 63, 20, 57, 17, 42, 12, 35, 47, 5, 31, 39, 56, 6, 30, 34, 21, 61, 25, 14, 40, 22, 38, 15, 6, 36, 56, 20, 60, 25, 12, 51, 27, 10, 56, 42, 20, 36, 63, 32, 6, 21, 41, 12, 34, 60, 26, 5, 48, 27, 10, 62, 19, 6, 47, 39, 14, 45, 7, 24, 17, 41, 32, 23, 51, 19, 56 },
01480   { 45, 31, 15, 59, 4, 33, 7, 47, 0, 41, 13, 61, 4, 47, 9, 23, 60, 14, 57, 31, 4, 45, 59, 6, 58, 10, 44, 20, 8, 42, 15, 6, 55, 17, 58, 31, 53, 12, 61, 10, 15, 57, 43, 2, 23, 35, 48, 14, 54, 6, 18, 49, 15, 38, 11, 34, 62, 9, 21, 58, 11, 41, 4, 31, 38, 8, 29, 55, 19, 36, 27, 52, 0, 25, 50, 43, 1, 39, 8, 55, 35, 51, 10, 30, 45, 62, 29, 2, 46, 10, 32, 48, 18, 38, 5, 22, 33, 8, 51, 3, 14, 44, 54, 25, 57, 30, 18, 52, 33, 22, 59, 28, 36, 52, 32, 21, 26, 50, 5, 55, 35, 60, 14, 54, 4, 40, 16, 33 },
01481   { 27, 3, 49, 10, 30, 40, 55, 27, 57, 24, 52, 21, 32, 17, 60, 30, 5, 44, 27, 49, 19, 34, 13, 24, 43, 36, 3, 49, 31, 59, 37, 48, 26, 41, 2, 41, 14, 36, 21, 32, 40, 26, 13, 49, 55, 5, 16, 40, 25, 60, 36, 1, 63, 29, 17, 44, 25, 40, 52, 5, 29, 47, 54, 13, 46, 24, 60, 4, 51, 22, 63, 14, 45, 18, 12, 62, 17, 57, 19, 42, 3, 26, 58, 48, 1, 21, 40, 52, 23, 37, 44, 1, 29, 58, 43, 50, 15, 61, 19, 45, 58, 28, 7, 48, 2, 46, 8, 42, 3, 55, 8, 50, 12, 4, 55, 10, 63, 33, 20, 40, 11, 3, 46, 20, 48, 26, 61, 11 },
01482   { 44, 56, 24, 36, 53, 19, 12, 37, 16, 44, 7, 36, 49, 54, 11, 37, 48, 21, 15, 1, 62, 25, 47, 56, 16, 18, 51, 12, 40, 1, 24, 11, 52, 16, 23, 59, 28, 1, 45, 53, 4, 60, 37, 21, 39, 30, 63, 20, 52, 10, 30, 45, 8, 41, 54, 4, 57, 7, 34, 55, 36, 18, 23, 59, 2, 48, 11, 32, 44, 1, 41, 8, 33, 54, 38, 23, 30, 46, 6, 29, 62, 18, 32, 16, 55, 34, 14, 11, 61, 7, 55, 16, 53, 13, 23, 2, 55, 37, 26, 10, 33, 23, 36, 16, 38, 22, 56, 15, 24, 43, 35, 17, 44, 40, 25, 46, 16, 1, 57, 25, 49, 36, 28, 62, 9, 35, 7, 53 },
01483   { 17, 38, 8, 61, 1, 50, 26, 62, 3, 31, 56, 15, 1, 26, 40, 2, 34, 51, 56, 36, 42, 9, 38, 2, 29, 60, 32, 57, 19, 62, 34, 47, 4, 57, 39, 7, 44, 63, 24, 18, 46, 28, 8, 54, 1, 34, 7, 46, 3, 37, 50, 23, 57, 21, 13, 46, 31, 20, 43, 15, 1, 61, 8, 33, 37, 17, 56, 26, 15, 49, 24, 59, 28, 3, 56, 9, 52, 32, 13, 49, 10, 43, 5, 45, 8, 25, 59, 42, 28, 33, 19, 40, 8, 63, 35, 47, 25, 4, 40, 52, 1, 60, 12, 53, 63, 9, 29, 60, 37, 19, 1, 62, 31, 20, 58, 12, 41, 30, 43, 9, 18, 52, 22, 1, 39, 30, 58, 21 },
01484   { 13, 47, 29, 18, 43, 34, 5, 48, 20, 42, 10, 45, 30, 58, 20, 63, 24, 11, 6, 28, 54, 14, 22, 52, 41, 7, 26, 5, 45, 15, 53, 13, 35, 27, 18, 50, 12, 33, 5, 56, 10, 17, 45, 24, 59, 15, 50, 26, 56, 13, 19, 5, 32, 52, 27, 36, 2, 61, 12, 26, 49, 40, 27, 52, 13, 50, 6, 39, 61, 34, 10, 37, 48, 20, 41, 27, 2, 36, 59, 24, 54, 33, 63, 20, 38, 50, 3, 17, 52, 4, 58, 27, 45, 21, 32, 11, 48, 17, 57, 20, 46, 38, 25, 43, 4, 34, 51, 6, 13, 45, 57, 26, 6, 48, 2, 35, 53, 23, 61, 34, 59, 6, 42, 56, 13, 51, 2, 41 },
01485   { 32, 5, 55, 23, 58, 14, 22, 52, 29, 15, 61, 25, 51, 8, 43, 13, 53, 41, 46, 20, 3, 33, 63, 11, 48, 21, 54, 38, 28, 3, 30, 43, 21, 62, 9, 31, 55, 22, 51, 29, 37, 62, 32, 12, 42, 29, 41, 9, 33, 44, 62, 28, 43, 1, 59, 19, 48, 30, 51, 39, 24, 4, 58, 19, 42, 29, 22, 43, 3, 18, 53, 5, 13, 50, 16, 60, 45, 21, 7, 40, 15, 0, 26, 53, 13, 31, 43, 24, 47, 31, 15, 49, 2, 41, 6, 59, 29, 42, 9, 30, 14, 7, 49, 18, 31, 47, 20, 39, 49, 32, 11, 41, 54, 15, 61, 18, 7, 38, 4, 13, 44, 28, 15, 32, 45, 19, 27, 49 },
01486   { 63, 34, 11, 39, 2, 45, 37, 8, 59, 39, 33, 4, 36, 17, 48, 5, 29, 18, 32, 61, 39, 50, 5, 27, 35, 0, 46, 12, 22, 49, 60, 6, 54, 0, 38, 49, 2, 42, 15, 40, 0, 47, 20, 51, 3, 57, 18, 61, 22, 0, 39, 16, 55, 12, 35, 8, 41, 22, 6, 59, 16, 45, 10, 36, 0, 62, 9, 54, 30, 58, 21, 43, 63, 31, 7, 35, 12, 48, 58, 28, 47, 37, 41, 9, 57, 20, 61, 0, 36, 11, 57, 35, 23, 52, 37, 18, 0, 62, 22, 55, 35, 62, 27, 54, 0, 15, 61, 28, 2, 59, 22, 9, 37, 27, 33, 51, 29, 48, 19, 50, 25, 37, 10, 57, 5, 37, 60, 8 },
01487   { 20, 25, 46, 52, 31, 60, 12, 55, 0, 19, 11, 46, 62, 35, 23, 38, 57, 0, 55, 10, 16, 30, 58, 44, 17, 59, 29, 63, 42, 8, 36, 20, 33, 46, 16, 61, 25, 35, 8, 54, 26, 7, 58, 22, 34, 6, 47, 14, 53, 31, 48, 9, 37, 25, 49, 63, 16, 55, 45, 14, 34, 63, 21, 53, 25, 33, 46, 16, 35, 7, 46, 29, 0, 39, 25, 55, 22, 34, 18, 4, 56, 11, 23, 51, 28, 6, 39, 14, 62, 44, 19, 8, 60, 12, 56, 28, 50, 34, 39, 5, 51, 3, 41, 12, 57, 35, 10, 53, 25, 17, 52, 30, 47, 0, 43, 14, 5, 57, 31, 55, 0, 63, 47, 23, 54, 24, 14, 43 },
01488   { 0, 57, 16, 6, 26, 19, 35, 28, 49, 42, 54, 26, 21, 1, 59, 27, 9, 47, 26, 44, 50, 22, 13, 40, 8, 37, 10, 34, 17, 56, 25, 58, 13, 27, 44, 9, 20, 58, 31, 17, 60, 36, 10, 41, 53, 25, 36, 39, 4, 24, 58, 17, 60, 4, 22, 38, 10, 32, 0, 50, 31, 7, 28, 47, 12, 57, 5, 26, 52, 23, 14, 40, 57, 17, 47, 5, 53, 1, 44, 31, 19, 60, 46, 2, 35, 48, 30, 54, 22, 5, 51, 39, 25, 31, 4, 43, 14, 9, 45, 16, 24, 44, 19, 29, 40, 23, 44, 7, 38, 42, 4, 63, 12, 54, 23, 59, 22, 42, 8, 15, 40, 21, 8, 34, 3, 41, 30, 50 },
01489   { 39, 10, 48, 33, 41, 54, 5, 47, 23, 13, 32, 7, 52, 44, 14, 39, 58, 18, 35, 6, 37, 2, 60, 24, 55, 19, 53, 2, 51, 32, 1, 41, 51, 4, 40, 29, 47, 3, 52, 44, 13, 49, 28, 16, 1, 62, 11, 27, 52, 35, 5, 42, 29, 47, 14, 56, 28, 53, 26, 38, 9, 56, 40, 3, 38, 15, 41, 60, 1, 37, 50, 25, 11, 28, 61, 19, 42, 62, 10, 52, 39, 6, 32, 14, 58, 17, 7, 26, 42, 34, 27, 10, 54, 40, 20, 63, 26, 53, 21, 61, 32, 7, 59, 48, 3, 56, 18, 31, 58, 14, 49, 21, 36, 16, 45, 9, 36, 24, 62, 45, 27, 31, 53, 17, 49, 12, 62, 18 },
01490   { 28, 59, 21, 58, 2, 16, 38, 9, 62, 3, 56, 41, 10, 31, 50, 4, 32, 52, 12, 63, 23, 46, 33, 31, 4, 48, 25, 43, 14, 23, 47, 11, 22, 55, 14, 60, 23, 37, 11, 39, 23, 2, 45, 56, 31, 43, 19, 55, 16, 46, 21, 51, 11, 33, 44, 2, 41, 18, 5, 52, 23, 44, 17, 60, 27, 49, 11, 32, 44, 10, 54, 2, 56, 33, 8, 38, 13, 29, 36, 16, 24, 63, 27, 51, 21, 43, 56, 12, 49, 3, 59, 48, 1, 15, 46, 7, 36, 2, 47, 11, 50, 27, 37, 13, 33, 8, 51, 46, 1, 34, 28, 40, 3, 33, 60, 29, 47, 1, 35, 11, 59, 42, 2, 60, 26, 46, 6, 35 },
01491   { 4, 43, 9, 29, 36, 63, 24, 44, 20, 50, 30, 17, 60, 22, 16, 43, 25, 3, 42, 19, 51, 15, 8, 54, 42, 15, 61, 5, 39, 57, 18, 61, 31, 48, 34, 2, 50, 19, 57, 5, 63, 33, 19, 38, 13, 27, 48, 7, 32, 61, 2, 26, 58, 6, 24, 50, 13, 61, 42, 20, 62, 2, 35, 20, 51, 4, 62, 18, 23, 58, 20, 31, 43, 15, 51, 45, 26, 50, 4, 55, 45, 3, 35, 9, 38, 1, 32, 61, 20, 45, 17, 33, 24, 57, 29, 51, 22, 58, 38, 30, 15, 1, 54, 21, 63, 43, 26, 12, 24, 56, 8, 60, 50, 19, 5, 52, 13, 54, 17, 50, 4, 16, 36, 12, 32, 56, 22, 54 },
01492   { 51, 25, 40, 53, 12, 49, 15, 57, 34, 7, 38, 47, 2, 36, 55, 8, 61, 30, 56, 7, 28, 59, 48, 11, 27, 35, 21, 45, 28, 36, 9, 38, 6, 16, 24, 63, 10, 32, 28, 43, 21, 53, 5, 60, 8, 57, 3, 45, 11, 37, 15, 54, 40, 20, 62, 36, 27, 34, 11, 48, 30, 15, 54, 8, 30, 42, 22, 34, 48, 13, 35, 63, 4, 37, 22, 2, 59, 9, 41, 23, 13, 41, 49, 18, 59, 24, 40, 5, 37, 30, 9, 61, 44, 6, 37, 11, 33, 17, 5, 55, 41, 60, 23, 39, 17, 5, 30, 62, 41, 16, 46, 25, 11, 56, 39, 26, 20, 38, 29, 39, 22, 52, 44, 20, 48, 1, 38, 14 },
01493   { 15, 33, 2, 18, 44, 6, 27, 0, 32, 61, 25, 12, 58, 28, 40, 20, 47, 13, 34, 43, 38, 1, 23, 62, 40, 0, 51, 10, 63, 3, 52, 26, 44, 30, 45, 6, 41, 54, 0, 51, 12, 30, 46, 24, 49, 22, 40, 33, 63, 23, 43, 30, 9, 47, 0, 17, 54, 7, 57, 3, 37, 47, 24, 46, 13, 55, 7, 52, 2, 42, 6, 26, 49, 18, 60, 34, 16, 57, 33, 20, 61, 30, 8, 54, 14, 46, 12, 53, 16, 55, 38, 13, 22, 53, 18, 59, 46, 27, 43, 19, 32, 10, 45, 6, 49, 36, 52, 2, 20, 55, 6, 39, 32, 15, 44, 3, 58, 10, 63, 6, 56, 30, 7, 58, 9, 40, 19, 63 },
01494   { 10, 47, 61, 23, 55, 31, 52, 42, 17, 45, 4, 51, 27, 6, 15, 53, 0, 49, 26, 10, 56, 18, 36, 6, 20, 58, 32, 30, 13, 49, 19, 56, 0, 59, 12, 53, 27, 17, 38, 25, 48, 9, 15, 36, 14, 30, 59, 17, 0, 50, 8, 58, 18, 56, 31, 45, 21, 41, 29, 19, 60, 6, 32, 59, 0, 36, 29, 39, 19, 59, 46, 12, 55, 30, 10, 47, 24, 3, 28, 48, 0, 55, 44, 27, 33, 4, 63, 29, 49, 0, 26, 50, 34, 2, 42, 14, 0, 62, 9, 56, 3, 52, 28, 34, 58, 9, 20, 48, 37, 32, 22, 53, 0, 62, 27, 49, 34, 46, 21, 33, 41, 14, 25, 37, 53, 29, 31, 45 },
01495   { 56, 28, 7, 37, 11, 36, 20, 9, 54, 14, 39, 19, 34, 63, 45, 37, 24, 17, 60, 31, 21, 45, 53, 29, 47, 15, 7, 55, 40, 23, 34, 14, 42, 20, 37, 35, 15, 59, 7, 62, 34, 40, 59, 1, 51, 42, 10, 28, 54, 21, 35, 5, 38, 13, 36, 4, 59, 12, 39, 53, 15, 43, 9, 21, 39, 62, 16, 56, 25, 9, 32, 38, 0, 41, 14, 51, 40, 53, 43, 11, 37, 17, 5, 22, 57, 39, 19, 7, 42, 21, 60, 10, 31, 63, 25, 52, 30, 49, 36, 25, 48, 17, 61, 14, 22, 42, 29, 13, 60, 11, 47, 18, 35, 41, 7, 23, 4, 16, 51, 11, 0, 48, 61, 3, 17, 50, 5, 24 },
01496   { 0, 42, 21, 49, 60, 3, 57, 40, 29, 48, 23, 56, 42, 11, 22, 5, 59, 39, 4, 50, 3, 41, 12, 57, 25, 50, 44, 18, 4, 46, 7, 62, 33, 50, 4, 56, 21, 32, 43, 18, 3, 23, 55, 34, 20, 4, 53, 38, 12, 46, 29, 52, 25, 61, 23, 51, 26, 46, 1, 34, 25, 57, 28, 51, 26, 11, 50, 3, 44, 28, 53, 21, 57, 27, 62, 6, 31, 19, 8, 63, 26, 59, 36, 47, 15, 29, 50, 25, 35, 47, 18, 41, 4, 48, 8, 40, 12, 23, 6, 44, 13, 40, 1, 31, 55, 0, 61, 43, 4, 50, 26, 58, 9, 53, 24, 61, 42, 55, 31, 43, 57, 20, 34, 27, 43, 8, 59, 39 },
01497   { 18, 51, 30, 13, 26, 16, 46, 22, 2, 59, 8, 30, 1, 48, 33, 51, 29, 9, 46, 16, 62, 14, 33, 2, 38, 9, 27, 60, 37, 26, 53, 17, 28, 10, 24, 46, 2, 49, 8, 57, 29, 45, 6, 26, 62, 44, 18, 25, 61, 3, 42, 14, 49, 10, 43, 6, 17, 32, 63, 10, 49, 4, 40, 14, 45, 33, 22, 37, 12, 61, 5, 17, 43, 7, 23, 37, 15, 58, 49, 13, 39, 21, 10, 52, 1, 62, 9, 56, 12, 2, 58, 28, 36, 16, 56, 28, 56, 35, 20, 63, 24, 37, 51, 8, 45, 25, 16, 33, 27, 38, 2, 44, 13, 30, 17, 36, 12, 26, 5, 18, 28, 47, 13, 60, 23, 45, 13, 33 },
01498   { 55, 4, 62, 34, 52, 38, 7, 63, 32, 37, 13, 53, 25, 62, 18, 12, 55, 41, 27, 35, 24, 49, 31, 52, 17, 63, 34, 1, 56, 12, 41, 2, 48, 58, 39, 16, 61, 27, 41, 52, 13, 19, 50, 39, 11, 31, 57, 6, 32, 40, 20, 55, 1, 28, 33, 57, 48, 8, 37, 22, 44, 18, 53, 1, 61, 5, 54, 16, 47, 36, 50, 24, 55, 34, 48, 45, 1, 30, 33, 46, 2, 50, 32, 42, 25, 34, 43, 21, 38, 52, 23, 45, 14, 54, 21, 4, 44, 16, 53, 29, 10, 47, 19, 57, 12, 54, 39, 10, 51, 15, 63, 21, 57, 40, 51, 1, 48, 57, 37, 62, 2, 38, 9, 52, 1, 35, 58, 22 },
01499   { 36, 46, 10, 42, 1, 27, 43, 15, 50, 21, 45, 16, 41, 3, 35, 44, 20, 1, 57, 11, 55, 7, 43, 8, 22, 42, 13, 46, 21, 39, 31, 60, 22, 5, 29, 44, 11, 35, 20, 4, 36, 58, 32, 15, 47, 2, 36, 48, 16, 60, 8, 35, 44, 63, 16, 2, 40, 26, 55, 14, 58, 35, 24, 31, 19, 42, 31, 58, 1, 29, 10, 40, 2, 19, 12, 54, 22, 61, 7, 24, 56, 5, 28, 16, 54, 3, 15, 58, 6, 30, 8, 62, 1, 43, 31, 47, 7, 59, 1, 38, 58, 4, 34, 27, 38, 5, 31, 59, 7, 46, 30, 3, 34, 6, 28, 59, 20, 8, 32, 15, 53, 24, 55, 31, 19, 49, 11, 26 },
01500   { 2, 24, 16, 58, 19, 55, 5, 35, 10, 61, 4, 28, 57, 24, 58, 7, 31, 47, 22, 38, 19, 28, 61, 36, 54, 5, 59, 29, 6, 52, 15, 11, 43, 36, 8, 54, 52, 1, 62, 25, 47, 9, 1, 60, 28, 53, 24, 14, 46, 27, 51, 22, 12, 24, 38, 53, 20, 11, 51, 3, 29, 7, 48, 63, 8, 49, 9, 21, 52, 14, 63, 32, 46, 60, 35, 4, 41, 16, 52, 35, 18, 42, 59, 7, 36, 61, 45, 27, 33, 51, 19, 39, 34, 11, 61, 18, 33, 41, 28, 15, 54, 22, 42, 3, 49, 21, 47, 18, 36, 23, 55, 19, 48, 24, 45, 10, 33, 44, 50, 40, 7, 35, 15, 41, 63, 6, 40, 54 },
01501   { 62, 41, 32, 8, 47, 28, 60, 24, 44, 30, 38, 49, 9, 33, 14, 40, 50, 14, 60, 2, 54, 40, 0, 20, 25, 39, 16, 49, 24, 35, 57, 47, 19, 61, 33, 18, 23, 37, 13, 55, 31, 43, 22, 41, 17, 8, 42, 58, 0, 37, 5, 56, 31, 54, 7, 30, 60, 33, 42, 17, 59, 39, 12, 27, 38, 17, 35, 41, 27, 45, 20, 7, 25, 15, 29, 58, 27, 47, 11, 40, 14, 54, 23, 46, 19, 31, 11, 40, 13, 49, 5, 58, 24, 51, 26, 6, 50, 20, 49, 9, 32, 46, 17, 60, 14, 63, 24, 1, 57, 41, 9, 43, 14, 62, 16, 52, 3, 27, 14, 22, 61, 45, 4, 28, 9, 47, 29, 17 },
01502   { 5, 50, 12, 53, 38, 18, 11, 51, 0, 55, 17, 6, 47, 54, 19, 63, 5, 26, 34, 45, 13, 30, 47, 58, 10, 48, 32, 3, 62, 9, 26, 0, 25, 14, 50, 3, 47, 30, 42, 16, 6, 63, 12, 49, 33, 55, 21, 10, 34, 63, 18, 41, 3, 47, 19, 43, 0, 49, 8, 28, 46, 20, 52, 0, 56, 24, 60, 3, 59, 5, 39, 57, 48, 52, 9, 38, 3, 21, 26, 60, 0, 32, 12, 38, 4, 48, 53, 0, 60, 15, 29, 44, 18, 10, 38, 57, 13, 60, 2, 26, 62, 7, 50, 29, 35, 8, 40, 53, 28, 12, 60, 33, 38, 5, 37, 29, 60, 39, 56, 0, 30, 18, 50, 34, 59, 25, 14, 44 },
01503   { 20, 31, 60, 22, 3, 49, 33, 25, 40, 13, 34, 59, 22, 36, 0, 28, 37, 56, 8, 18, 51, 16, 4, 45, 27, 12, 53, 42, 18, 44, 51, 31, 55, 40, 28, 58, 7, 60, 10, 51, 27, 37, 24, 56, 5, 26, 44, 29, 50, 23, 45, 11, 34, 15, 59, 27, 13, 23, 62, 37, 4, 57, 15, 32, 42, 6, 47, 11, 30, 43, 23, 13, 0, 36, 18, 44, 63, 51, 37, 29, 49, 20, 57, 27, 62, 9, 24, 35, 23, 53, 37, 3, 42, 55, 0, 36, 23, 39, 31, 43, 17, 37, 24, 11, 52, 43, 19, 32, 5, 50, 26, 0, 56, 21, 54, 11, 19, 6, 47, 25, 59, 42, 12, 54, 21, 3, 38, 57 },
01504   { 48, 0, 35, 27, 44, 14, 59, 7, 57, 46, 26, 2, 42, 12, 52, 43, 10, 27, 53, 42, 32, 62, 37, 21, 34, 61, 7, 23, 36, 4, 38, 12, 41, 5, 17, 45, 22, 27, 39, 21, 59, 0, 45, 18, 39, 62, 3, 38, 14, 7, 54, 26, 61, 39, 9, 52, 45, 36, 18, 50, 10, 34, 44, 22, 50, 14, 36, 55, 17, 34, 53, 62, 33, 26, 56, 6, 31, 12, 6, 53, 9, 44, 2, 50, 20, 40, 55, 17, 47, 7, 26, 63, 22, 32, 48, 16, 46, 8, 52, 12, 57, 41, 0, 56, 25, 3, 61, 14, 45, 35, 18, 44, 12, 46, 23, 42, 32, 51, 35, 10, 17, 36, 23, 1, 45, 52, 32, 10 },
01505   { 37, 15, 43, 8, 63, 39, 21, 31, 16, 37, 19, 62, 30, 46, 17, 60, 21, 48, 1, 23, 6, 25, 11, 56, 1, 40, 30, 58, 15, 54, 21, 59, 9, 63, 35, 56, 11, 51, 2, 46, 34, 14, 53, 7, 30, 11, 51, 19, 60, 40, 30, 1, 24, 50, 20, 32, 3, 56, 5, 25, 31, 13, 61, 2, 29, 60, 25, 20, 51, 2, 27, 8, 18, 42, 10, 45, 21, 34, 43, 17, 62, 29, 41, 14, 34, 6, 30, 43, 2, 57, 33, 13, 45, 12, 27, 62, 4, 55, 21, 35, 5, 27, 45, 33, 16, 47, 30, 54, 22, 10, 51, 27, 63, 7, 49, 1, 58, 22, 15, 43, 53, 7, 57, 39, 27, 12, 61, 24 },
01506   { 56, 51, 26, 56, 19, 2, 41, 54, 5, 52, 9, 48, 6, 23, 39, 4, 32, 15, 63, 35, 59, 49, 43, 15, 52, 19, 50, 9, 46, 33, 1, 29, 48, 20, 32, 1, 38, 33, 19, 54, 9, 32, 24, 48, 58, 35, 16, 48, 4, 52, 13, 57, 33, 5, 45, 59, 15, 29, 41, 55, 47, 39, 23, 53, 9, 40, 4, 57, 10, 44, 48, 40, 50, 14, 61, 24, 55, 1, 59, 22, 33, 8, 51, 25, 58, 46, 11, 59, 20, 41, 17, 51, 6, 56, 35, 25, 42, 30, 15, 58, 48, 18, 61, 9, 58, 39, 13, 2, 37, 59, 40, 2, 31, 16, 34, 41, 8, 30, 62, 3, 29, 48, 33, 5, 63, 16, 41, 7 },
01507   { 22, 4, 46, 11, 33, 51, 29, 10, 62, 24, 43, 27, 15, 58, 50, 25, 54, 44, 9, 38, 18, 3, 29, 57, 32, 5, 26, 43, 17, 61, 24, 52, 8, 42, 23, 53, 15, 61, 7, 28, 57, 43, 4, 40, 20, 2, 43, 25, 32, 35, 21, 43, 17, 48, 10, 22, 38, 54, 11, 21, 1, 58, 16, 30, 48, 18, 46, 32, 38, 13, 22, 4, 59, 35, 2, 51, 30, 39, 15, 47, 4, 56, 13, 37, 1, 28, 16, 52, 32, 9, 61, 29, 38, 19, 3, 52, 10, 48, 1, 32, 11, 40, 20, 36, 6, 22, 49, 29, 55, 6, 20, 56, 36, 52, 19, 60, 26, 46, 18, 54, 40, 13, 20, 46, 35, 19, 49, 29 },
01508   { 61, 17, 34, 53, 23, 6, 48, 35, 20, 40, 1, 56, 36, 29, 11, 34, 7, 41, 14, 30, 55, 20, 46, 8, 24, 38, 63, 2, 37, 10, 45, 14, 34, 49, 6, 13, 44, 25, 49, 41, 21, 12, 61, 15, 54, 29, 63, 12, 56, 8, 49, 2, 62, 36, 28, 61, 0, 25, 41, 63, 35, 8, 44, 6, 37, 62, 7, 21, 63, 28, 55, 31, 16, 24, 41, 19, 9, 57, 27, 36, 18, 42, 31, 62, 22, 55, 38, 4, 27, 47, 1, 40, 14, 54, 43, 20, 60, 23, 38, 63, 25, 51, 2, 53, 26, 63, 10, 42, 17, 34, 47, 25, 13, 5, 44, 11, 55, 2, 38, 27, 6, 60, 52, 25, 9, 55, 1, 40 },
01509   { 8, 30, 58, 3, 42, 61, 17, 38, 13, 59, 32, 10, 54, 3, 51, 20, 61, 26, 57, 2, 46, 33, 12, 60, 41, 13, 48, 29, 55, 20, 39, 27, 57, 18, 62, 29, 55, 2, 31, 16, 37, 50, 26, 36, 6, 46, 9, 41, 27, 57, 23, 39, 26, 6, 51, 12, 31, 46, 7, 16, 27, 52, 19, 56, 26, 12, 33, 53, 1, 41, 8, 57, 46, 7, 54, 32, 47, 5, 49, 11, 60, 23, 5, 48, 10, 43, 19, 63, 35, 24, 49, 21, 59, 5, 31, 37, 14, 44, 7, 42, 6, 30, 46, 13, 44, 32, 19, 50, 4, 58, 8, 30, 62, 38, 28, 53, 21, 36, 13, 50, 21, 33, 15, 2, 44, 31, 14, 47 },
01510   { 37, 13, 39, 16, 28, 9, 57, 0, 25, 49, 21, 45, 18, 47, 12, 42, 0, 49, 22, 39, 16, 53, 25, 36, 0, 52, 22, 16, 6, 60, 4, 51, 0, 26, 37, 47, 10, 36, 63, 5, 57, 0, 18, 59, 23, 33, 51, 19, 0, 44, 15, 11, 54, 17, 42, 35, 53, 18, 58, 33, 49, 4, 34, 42, 0, 50, 43, 25, 16, 49, 34, 20, 37, 28, 12, 63, 16, 38, 25, 44, 0, 40, 52, 17, 35, 3, 50, 14, 8, 53, 11, 36, 25, 45, 9, 62, 0, 54, 28, 17, 50, 55, 15, 24, 57, 0, 53, 34, 23, 41, 15, 45, 0, 49, 16, 4, 48, 9, 63, 45, 0, 42, 58, 37, 61, 22, 54, 26 },
01511   { 0, 50, 21, 47, 54, 36, 27, 45, 52, 4, 34, 15, 63, 29, 37, 59, 17, 31, 6, 61, 28, 5, 48, 18, 59, 27, 34, 56, 44, 31, 35, 12, 41, 59, 16, 3, 40, 20, 50, 22, 30, 40, 52, 10, 45, 3, 59, 22, 37, 61, 29, 46, 31, 58, 2, 22, 9, 43, 3, 39, 14, 61, 24, 54, 15, 29, 11, 60, 39, 17, 5, 61, 0, 44, 50, 3, 31, 14, 58, 21, 54, 28, 15, 45, 60, 26, 33, 58, 44, 22, 60, 2, 57, 34, 49, 27, 18, 34, 21, 59, 29, 4, 36, 41, 8, 39, 28, 11, 62, 26, 53, 20, 35, 24, 59, 32, 29, 39, 24, 31, 57, 23, 11, 28, 5, 36, 11, 59 },
01512   { 44, 32, 63, 5, 20, 12, 41, 7, 30, 61, 42, 8, 39, 5, 33, 8, 24, 53, 45, 11, 37, 58, 7, 44, 10, 50, 3, 40, 8, 22, 53, 19, 46, 9, 33, 52, 24, 58, 8, 44, 13, 47, 8, 34, 38, 30, 14, 47, 7, 34, 4, 55, 9, 19, 40, 49, 56, 26, 60, 21, 30, 45, 10, 19, 40, 58, 23, 36, 3, 52, 45, 23, 54, 13, 22, 42, 53, 45, 7, 33, 10, 36, 57, 6, 29, 12, 41, 0, 30, 15, 41, 30, 17, 7, 16, 53, 40, 56, 2, 39, 12, 61, 10, 52, 31, 60, 16, 45, 1, 37, 7, 61, 40, 10, 43, 17, 58, 7, 54, 14, 4, 51, 39, 49, 18, 56, 42, 20 },
01513   { 14, 6, 24, 36, 56, 49, 22, 60, 18, 14, 23, 51, 26, 57, 21, 52, 41, 14, 35, 50, 19, 31, 40, 23, 33, 14, 63, 17, 32, 47, 7, 62, 23, 30, 56, 11, 42, 27, 14, 60, 35, 19, 28, 61, 17, 55, 25, 39, 53, 17, 42, 21, 38, 63, 25, 5, 14, 36, 12, 50, 1, 37, 59, 32, 2, 51, 6, 56, 27, 32, 11, 30, 38, 26, 60, 8, 26, 19, 62, 39, 50, 2, 21, 39, 53, 23, 56, 19, 49, 39, 5, 46, 55, 23, 42, 4, 31, 11, 47, 26, 45, 22, 48, 18, 21, 5, 48, 25, 57, 14, 47, 30, 3, 56, 12, 50, 1, 42, 19, 47, 35, 17, 8, 30, 45, 25, 4, 51 },
01514   { 28, 58, 43, 1, 31, 8, 33, 2, 44, 55, 32, 1, 60, 12, 46, 27, 4, 62, 23, 1, 56, 13, 62, 2, 54, 36, 25, 51, 1, 57, 26, 42, 3, 49, 17, 38, 1, 48, 31, 4, 54, 3, 50, 24, 1, 49, 5, 63, 13, 27, 52, 1, 48, 13, 45, 33, 52, 30, 46, 20, 55, 28, 6, 48, 24, 38, 20, 47, 14, 62, 48, 9, 58, 4, 36, 30, 56, 1, 34, 12, 18, 63, 25, 48, 4, 16, 37, 7, 62, 10, 52, 28, 13, 50, 36, 63, 24, 51, 15, 58, 8, 33, 1, 38, 56, 35, 42, 9, 33, 51, 22, 18, 48, 32, 27, 37, 23, 61, 33, 11, 59, 29, 62, 1, 53, 10, 60, 33 },
01515   { 12, 39, 17, 52, 26, 46, 53, 38, 25, 11, 48, 36, 16, 43, 2, 35, 55, 17, 39, 29, 43, 9, 28, 45, 20, 5, 46, 12, 42, 28, 13, 52, 36, 6, 60, 22, 54, 17, 62, 39, 25, 42, 15, 55, 44, 20, 31, 10, 35, 57, 24, 32, 29, 6, 59, 18, 7, 62, 3, 41, 10, 44, 16, 54, 13, 62, 31, 9, 41, 1, 21, 43, 18, 47, 15, 40, 11, 49, 28, 55, 46, 30, 8, 43, 32, 61, 28, 47, 25, 34, 21, 61, 32, 1, 20, 9, 46, 6, 35, 19, 41, 54, 27, 63, 14, 3, 51, 20, 62, 2, 38, 55, 8, 21, 63, 6, 46, 9, 26, 51, 3, 24, 43, 34, 16, 41, 18, 48 },
01516   { 62, 23, 55, 9, 15, 62, 19, 13, 58, 40, 6, 30, 54, 19, 50, 31, 10, 44, 6, 59, 21, 47, 51, 15, 60, 39, 30, 54, 21, 61, 19, 33, 14, 29, 43, 11, 34, 45, 7, 21, 10, 56, 36, 6, 38, 11, 58, 42, 2, 47, 11, 60, 50, 16, 41, 28, 38, 23, 47, 17, 35, 63, 22, 33, 42, 5, 45, 17, 53, 35, 25, 56, 33, 6, 51, 19, 60, 23, 43, 15, 5, 40, 58, 13, 51, 1, 45, 11, 54, 3, 43, 8, 37, 48, 59, 29, 39, 21, 61, 43, 3, 31, 10, 44, 24, 29, 60, 12, 28, 40, 11, 25, 43, 52, 14, 41, 16, 57, 44, 20, 40, 55, 12, 21, 57, 27, 35, 2 },
01517   { 37, 6, 31, 42, 40, 4, 29, 50, 0, 20, 63, 28, 9, 58, 14, 24, 63, 26, 48, 16, 34, 4, 32, 38, 23, 11, 58, 4, 37, 9, 45, 5, 63, 48, 26, 57, 2, 28, 32, 51, 46, 29, 13, 62, 27, 46, 28, 18, 50, 15, 40, 4, 19, 34, 54, 0, 53, 9, 26, 58, 28, 5, 49, 0, 57, 27, 19, 60, 29, 8, 59, 12, 37, 63, 24, 46, 3, 37, 6, 52, 26, 32, 20, 36, 9, 22, 59, 18, 35, 51, 14, 57, 17, 24, 12, 44, 56, 0, 30, 13, 59, 20, 49, 17, 54, 43, 6, 34, 46, 17, 58, 36, 0, 34, 29, 54, 25, 2, 36, 15, 60, 6, 37, 46, 4, 50, 9, 45 },
01518   { 19, 59, 48, 3, 24, 60, 44, 22, 34, 51, 15, 45, 41, 5, 33, 47, 0, 37, 12, 55, 25, 54, 8, 57, 0, 47, 18, 34, 49, 15, 55, 24, 40, 20, 8, 35, 53, 13, 41, 18, 0, 59, 22, 33, 4, 52, 8, 60, 24, 36, 31, 56, 45, 26, 10, 43, 15, 56, 36, 4, 51, 14, 39, 30, 12, 55, 36, 2, 39, 49, 4, 44, 17, 0, 32, 13, 53, 35, 59, 17, 62, 0, 55, 24, 52, 38, 31, 6, 42, 19, 29, 40, 4, 54, 33, 5, 16, 27, 52, 37, 23, 55, 7, 37, 0, 39, 23, 49, 4, 53, 31, 15, 59, 10, 50, 4, 60, 34, 48, 7, 31, 49, 27, 14, 62, 22, 53, 29 },
01519   { 46, 21, 14, 51, 36, 17, 7, 57, 10, 32, 3, 37, 22, 60, 39, 18, 56, 20, 42, 3, 36, 10, 44, 26, 41, 29, 53, 27, 2, 39, 30, 52, 0, 59, 15, 48, 23, 61, 6, 58, 37, 12, 40, 49, 16, 39, 20, 44, 0, 62, 8, 21, 3, 59, 23, 32, 49, 31, 12, 44, 22, 59, 18, 50, 24, 7, 43, 52, 15, 23, 41, 26, 51, 28, 55, 39, 21, 27, 10, 42, 12, 45, 27, 47, 3, 15, 63, 26, 55, 0, 60, 26, 45, 18, 62, 38, 58, 49, 8, 47, 4, 33, 46, 29, 57, 13, 56, 16, 59, 21, 5, 47, 23, 39, 18, 44, 13, 22, 28, 53, 19, 0, 58, 32, 41, 7, 26, 13 },
01520   { 0, 56, 34, 28, 11, 55, 31, 47, 26, 41, 56, 13, 53, 28, 11, 49, 7, 52, 32, 61, 50, 22, 63, 17, 13, 56, 7, 19, 43, 62, 10, 21, 37, 32, 43, 4, 38, 19, 44, 25, 31, 54, 5, 23, 61, 30, 53, 12, 35, 22, 43, 53, 37, 48, 7, 62, 20, 2, 61, 41, 8, 34, 47, 9, 63, 34, 28, 10, 55, 33, 14, 57, 7, 47, 9, 61, 4, 49, 31, 50, 21, 38, 8, 16, 57, 44, 33, 5, 49, 36, 12, 50, 7, 34, 10, 25, 2, 22, 36, 15, 26, 61, 18, 9, 22, 46, 32, 8, 27, 37, 44, 30, 55, 3, 62, 24, 38, 56, 5, 45, 38, 24, 43, 10, 19, 54, 39, 61 },
01521   { 41, 30, 8, 63, 43, 23, 38, 3, 62, 19, 8, 49, 25, 1, 58, 30, 23, 40, 9, 28, 18, 40, 6, 38, 49, 22, 35, 59, 8, 27, 50, 5, 56, 17, 11, 50, 30, 9, 55, 2, 51, 19, 34, 47, 9, 41, 6, 26, 48, 57, 14, 28, 17, 12, 39, 13, 37, 46, 25, 19, 54, 27, 1, 37, 16, 45, 20, 60, 1, 48, 20, 38, 31, 22, 42, 15, 19, 44, 1, 61, 6, 34, 56, 40, 29, 10, 20, 46, 13, 22, 41, 23, 59, 42, 30, 51, 45, 13, 63, 53, 42, 12, 51, 38, 62, 2, 26, 41, 50, 1, 61, 10, 19, 42, 31, 8, 49, 32, 12, 63, 9, 52, 16, 56, 36, 2, 31, 16 },
01522   { 52, 5, 47, 20, 1, 53, 12, 50, 16, 35, 43, 21, 33, 43, 16, 44, 3, 59, 14, 46, 1, 30, 60, 33, 2, 45, 12, 42, 31, 47, 14, 33, 46, 25, 55, 27, 60, 36, 16, 42, 14, 46, 26, 1, 55, 15, 63, 32, 2, 38, 5, 47, 33, 61, 30, 52, 4, 57, 6, 38, 11, 43, 61, 24, 52, 3, 31, 22, 42, 10, 62, 3, 59, 11, 35, 57, 33, 54, 24, 14, 29, 48, 18, 2, 60, 41, 53, 24, 32, 62, 3, 53, 15, 1, 55, 17, 32, 40, 6, 31, 1, 40, 28, 5, 35, 52, 19, 63, 13, 33, 17, 41, 52, 26, 15, 57, 1, 20, 42, 17, 35, 27, 48, 5, 25, 50, 44, 11 },
01523   { 35, 25, 38, 57, 33, 17, 40, 6, 59, 27, 54, 5, 61, 10, 52, 26, 36, 19, 51, 35, 57, 48, 11, 20, 54, 25, 61, 16, 1, 58, 24, 61, 3, 39, 7, 47, 1, 22, 49, 28, 63, 10, 58, 32, 17, 36, 45, 19, 51, 29, 59, 10, 50, 1, 23, 42, 18, 29, 51, 21, 56, 32, 14, 5, 40, 58, 47, 13, 54, 35, 29, 45, 18, 52, 26, 2, 38, 8, 46, 36, 58, 11, 52, 35, 17, 28, 1, 58, 9, 39, 17, 28, 37, 48, 20, 9, 57, 24, 50, 19, 58, 16, 48, 25, 43, 11, 35, 6, 45, 24, 56, 4, 36, 7, 47, 35, 52, 28, 59, 30, 2, 61, 21, 33, 63, 12, 18, 59 },
01524   { 3, 49, 15, 10, 27, 61, 25, 45, 30, 0, 14, 47, 31, 38, 17, 62, 7, 55, 27, 4, 15, 24, 42, 52, 10, 34, 5, 51, 36, 18, 41, 11, 35, 21, 62, 13, 33, 57, 8, 35, 5, 40, 21, 43, 52, 3, 24, 56, 11, 16, 33, 25, 41, 20, 55, 8, 60, 35, 15, 48, 2, 57, 30, 49, 18, 25, 6, 39, 17, 57, 7, 25, 43, 5, 49, 16, 62, 22, 55, 4, 25, 43, 23, 7, 50, 11, 37, 48, 14, 51, 33, 57, 7, 27, 39, 46, 4, 29, 11, 43, 34, 56, 7, 60, 20, 54, 30, 57, 22, 49, 9, 33, 54, 14, 63, 23, 6, 43, 10, 40, 50, 13, 44, 8, 38, 33, 46, 23 },
01525   { 55, 39, 22, 50, 44, 4, 36, 9, 52, 23, 37, 59, 21, 2, 46, 13, 31, 41, 11, 45, 62, 29, 6, 37, 19, 48, 30, 23, 44, 7, 53, 28, 54, 16, 41, 29, 44, 18, 52, 24, 60, 15, 48, 7, 27, 59, 9, 34, 42, 54, 7, 63, 4, 46, 31, 27, 45, 0, 40, 26, 34, 17, 37, 10, 53, 29, 36, 50, 2, 27, 51, 11, 61, 37, 23, 41, 30, 7, 18, 50, 39, 14, 63, 32, 45, 61, 19, 30, 25, 44, 2, 47, 23, 63, 11, 34, 59, 37, 60, 3, 22, 14, 44, 30, 15, 0, 47, 15, 3, 38, 61, 20, 27, 45, 11, 39, 51, 16, 55, 3, 22, 54, 29, 58, 1, 57, 6, 29 },
01526   { 9, 17, 60, 2, 34, 56, 20, 62, 39, 12, 49, 6, 29, 56, 34, 48, 0, 58, 22, 38, 18, 43, 56, 0, 63, 14, 55, 3, 59, 31, 15, 45, 0, 49, 6, 58, 3, 38, 12, 45, 0, 37, 29, 57, 13, 39, 30, 49, 0, 23, 44, 36, 16, 57, 13, 54, 11, 24, 63, 9, 53, 7, 62, 42, 0, 59, 15, 23, 63, 34, 40, 16, 32, 0, 53, 12, 48, 28, 59, 33, 0, 53, 9, 27, 3, 22, 54, 5, 56, 9, 61, 13, 42, 14, 52, 19, 0, 21, 47, 27, 53, 36, 3, 50, 39, 58, 25, 40, 53, 28, 12, 50, 0, 59, 32, 2, 21, 34, 26, 46, 37, 7, 18, 47, 24, 14, 53, 42 },
01527   { 61, 32, 13, 54, 29, 7, 46, 13, 28, 57, 18, 41, 53, 15, 9, 39, 24, 49, 33, 3, 53, 9, 26, 32, 40, 28, 46, 39, 25, 9, 56, 21, 63, 37, 26, 22, 51, 27, 17, 56, 31, 53, 4, 43, 22, 46, 12, 18, 60, 40, 20, 26, 50, 21, 39, 5, 49, 33, 16, 44, 22, 46, 20, 32, 24, 45, 8, 43, 12, 46, 4, 48, 56, 20, 29, 58, 3, 40, 10, 42, 31, 21, 47, 41, 56, 38, 15, 42, 36, 27, 20, 33, 55, 3, 26, 44, 31, 54, 12, 35, 9, 63, 28, 10, 21, 32, 9, 60, 17, 8, 43, 29, 40, 16, 36, 48, 60, 7, 57, 14, 62, 31, 42, 15, 36, 40, 20, 26 },
01528   { 0, 37, 47, 23, 41, 18, 32, 48, 1, 35, 8, 25, 4, 26, 63, 20, 54, 8, 16, 61, 35, 23, 51, 15, 58, 7, 12, 20, 50, 34, 42, 4, 38, 10, 32, 47, 8, 60, 41, 20, 9, 25, 50, 19, 62, 1, 37, 56, 28, 8, 53, 11, 3, 58, 34, 43, 19, 60, 38, 4, 58, 31, 3, 51, 11, 55, 38, 30, 21, 58, 19, 26, 9, 44, 36, 13, 46, 20, 62, 24, 13, 60, 5, 28, 12, 34, 7, 59, 0, 53, 45, 6, 38, 30, 50, 7, 62, 16, 41, 5, 46, 18, 55, 42, 51, 5, 45, 23, 34, 48, 19, 58, 5, 25, 54, 19, 13, 41, 28, 21, 0, 49, 10, 60, 4, 51, 9, 45 },
01529   { 19, 28, 6, 58, 10, 51, 4, 22, 55, 42, 60, 45, 34, 51, 42, 5, 30, 45, 27, 40, 13, 47, 4, 49, 21, 38, 60, 29, 2, 57, 17, 27, 52, 19, 61, 14, 30, 34, 2, 44, 63, 33, 11, 35, 16, 51, 25, 6, 14, 47, 31, 61, 37, 29, 18, 8, 52, 2, 28, 54, 13, 41, 15, 62, 35, 18, 2, 60, 6, 33, 41, 61, 31, 6, 56, 17, 34, 50, 6, 52, 44, 35, 16, 51, 59, 24, 48, 18, 31, 40, 16, 49, 21, 60, 17, 39, 10, 49, 32, 57, 24, 39, 1, 25, 18, 62, 37, 12, 56, 1, 37, 11, 52, 44, 9, 30, 47, 4, 51, 40, 55, 25, 34, 27, 56, 30, 32, 54 },
01530   { 63, 40, 49, 15, 43, 26, 63, 38, 16, 20, 30, 12, 57, 14, 19, 60, 36, 12, 59, 2, 57, 17, 42, 31, 1, 44, 16, 35, 47, 11, 32, 48, 13, 43, 1, 39, 51, 12, 57, 23, 6, 40, 53, 3, 55, 31, 39, 60, 35, 44, 5, 15, 45, 1, 62, 41, 26, 14, 47, 22, 36, 27, 50, 9, 26, 47, 52, 28, 54, 16, 1, 13, 51, 39, 23, 63, 1, 30, 15, 26, 2, 57, 19, 37, 1, 44, 21, 50, 13, 63, 8, 24, 56, 1, 35, 25, 58, 20, 2, 28, 14, 51, 33, 59, 13, 30, 4, 49, 31, 24, 63, 26, 33, 3, 58, 38, 62, 24, 32, 8, 17, 45, 5, 48, 18, 3, 43, 11 },
01531   { 21, 4, 24, 34, 59, 1, 37, 11, 53, 5, 47, 2, 22, 40, 32, 1, 24, 50, 21, 29, 38, 25, 63, 8, 55, 24, 53, 6, 62, 23, 59, 3, 54, 20, 58, 24, 5, 46, 15, 38, 48, 14, 27, 42, 23, 7, 46, 10, 17, 58, 25, 52, 23, 32, 49, 12, 55, 30, 40, 7, 59, 1, 56, 21, 39, 4, 23, 15, 37, 46, 55, 42, 21, 4, 48, 8, 45, 54, 37, 55, 32, 8, 46, 10, 30, 54, 4, 41, 25, 29, 36, 48, 11, 43, 14, 47, 5, 43, 53, 36, 61, 10, 45, 6, 41, 54, 27, 43, 16, 55, 6, 46, 18, 42, 23, 15, 1, 45, 12, 60, 37, 22, 62, 12, 39, 59, 16, 52 },
01532   { 47, 35, 56, 7, 19, 46, 31, 50, 33, 24, 61, 35, 50, 7, 53, 44, 55, 6, 46, 10, 52, 5, 21, 43, 36, 10, 18, 41, 26, 37, 8, 29, 40, 36, 9, 49, 34, 26, 61, 21, 7, 59, 18, 62, 29, 54, 20, 32, 51, 0, 40, 10, 55, 6, 20, 36, 9, 61, 5, 51, 44, 19, 33, 43, 13, 57, 40, 63, 8, 24, 29, 10, 60, 34, 27, 40, 25, 18, 10, 42, 21, 49, 26, 62, 38, 12, 33, 61, 5, 57, 2, 19, 54, 28, 62, 22, 38, 31, 16, 7, 22, 47, 29, 17, 35, 8, 20, 51, 2, 40, 22, 50, 13, 61, 28, 53, 35, 20, 56, 30, 2, 53, 14, 41, 23, 34, 8, 31 },
01533   { 12, 2, 42, 29, 52, 13, 21, 8, 55, 14, 41, 17, 28, 58, 23, 11, 17, 36, 31, 62, 17, 34, 50, 14, 28, 61, 33, 52, 2, 51, 17, 45, 7, 25, 62, 30, 18, 55, 0, 42, 30, 35, 45, 1, 12, 48, 3, 63, 21, 36, 30, 48, 19, 59, 43, 27, 46, 17, 34, 25, 12, 29, 53, 6, 48, 31, 11, 34, 49, 3, 36, 50, 19, 47, 14, 61, 11, 36, 58, 4, 60, 14, 39, 22, 6, 52, 15, 35, 17, 46, 31, 42, 9, 34, 3, 52, 12, 60, 26, 56, 40, 2, 53, 23, 57, 38, 62, 14, 36, 59, 10, 31, 39, 6, 49, 9, 41, 26, 5, 48, 43, 27, 33, 58, 1, 50, 25, 57 },
01534   { 61, 37, 15, 61, 3, 39, 58, 43, 26, 0, 44, 10, 47, 3, 37, 63, 28, 43, 13, 39, 3, 57, 30, 59, 0, 48, 5, 43, 13, 22, 60, 33, 55, 15, 42, 4, 52, 10, 45, 13, 54, 4, 24, 49, 37, 26, 41, 14, 42, 9, 61, 13, 38, 23, 3, 53, 0, 58, 21, 42, 63, 10, 17, 61, 25, 0, 58, 28, 17, 44, 57, 12, 27, 0, 55, 5, 52, 28, 23, 47, 29, 0, 43, 17, 58, 28, 47, 23, 55, 10, 58, 23, 51, 40, 18, 33, 45, 0, 49, 8, 32, 61, 19, 48, 0, 26, 7, 47, 29, 18, 44, 0, 56, 34, 20, 59, 15, 51, 37, 18, 10, 52, 7, 20, 46, 9, 38, 17 },
01535   { 6, 27, 48, 23, 45, 29, 5, 18, 38, 62, 27, 56, 20, 32, 15, 9, 48, 0, 54, 22, 45, 20, 7, 41, 23, 39, 19, 27, 58, 31, 44, 0, 12, 50, 23, 56, 20, 39, 32, 59, 16, 52, 33, 9, 57, 22, 6, 58, 28, 50, 24, 2, 56, 35, 16, 45, 32, 38, 15, 54, 2, 38, 46, 22, 35, 45, 20, 5, 52, 25, 7, 35, 59, 32, 22, 43, 38, 3, 51, 16, 34, 53, 32, 50, 3, 40, 8, 43, 0, 39, 27, 4, 14, 61, 8, 55, 15, 41, 20, 44, 27, 13, 39, 11, 46, 42, 54, 33, 4, 52, 23, 61, 14, 25, 43, 2, 33, 11, 63, 29, 61, 17, 40, 55, 22, 62, 28, 44 },
01536   { 20, 54, 8, 56, 35, 10, 63, 31, 52, 12, 48, 6, 59, 41, 52, 33, 19, 58, 25, 49, 11, 37, 47, 12, 54, 15, 56, 35, 7, 47, 16, 53, 28, 34, 5, 37, 28, 8, 48, 3, 28, 38, 18, 61, 16, 43, 53, 32, 4, 17, 47, 27, 44, 8, 63, 10, 25, 49, 6, 37, 24, 52, 32, 3, 50, 12, 41, 56, 38, 14, 62, 20, 40, 16, 53, 31, 18, 63, 41, 9, 59, 7, 13, 25, 57, 20, 63, 26, 53, 18, 48, 62, 30, 46, 21, 25, 58, 29, 36, 4, 55, 34, 6, 60, 31, 16, 21, 12, 58, 38, 9, 29, 47, 7, 52, 30, 57, 44, 22, 0, 35, 45, 3, 31, 14, 36, 0, 51 },
01537   { 42, 14, 33, 24, 16, 49, 40, 2, 22, 33, 16, 36, 25, 1, 21, 61, 38, 8, 33, 4, 62, 26, 29, 60, 6, 46, 30, 11, 63, 4, 36, 40, 19, 57, 46, 11, 41, 63, 22, 25, 58, 10, 46, 2, 34, 27, 11, 38, 56, 34, 12, 53, 18, 33, 41, 51, 13, 28, 60, 20, 47, 14, 29, 59, 16, 62, 8, 22, 32, 47, 9, 49, 2, 44, 7, 12, 45, 6, 20, 27, 45, 24, 62, 42, 36, 11, 33, 15, 37, 7, 32, 10, 37, 1, 35, 50, 6, 11, 63, 24, 52, 15, 50, 24, 3, 37, 56, 27, 34, 22, 49, 16, 36, 62, 17, 39, 4, 15, 54, 24, 50, 8, 58, 26, 49, 54, 11, 30 },
01538   { 4, 59, 41, 1, 53, 12, 25, 45, 59, 7, 51, 39, 54, 14, 46, 4, 27, 53, 16, 44, 18, 51, 1, 32, 25, 2, 50, 40, 20, 54, 24, 9, 62, 2, 27, 60, 1, 17, 36, 50, 6, 40, 30, 55, 41, 19, 49, 1, 21, 60, 40, 5, 62, 1, 22, 30, 57, 4, 43, 31, 1, 55, 40, 7, 27, 37, 30, 54, 1, 19, 42, 30, 56, 26, 62, 49, 24, 57, 37, 56, 2, 39, 16, 5, 30, 55, 3, 49, 60, 23, 56, 44, 17, 52, 13, 42, 28, 48, 18, 45, 9, 37, 21, 41, 58, 10, 48, 1, 63, 5, 41, 57, 2, 24, 12, 48, 27, 42, 32, 46, 13, 38, 19, 34, 5, 41, 25, 60 },
01539   { 39, 28, 21, 46, 32, 57, 36, 9, 19, 42, 4, 29, 11, 43, 30, 49, 13, 42, 35, 56, 9, 39, 15, 52, 36, 61, 18, 26, 45, 14, 31, 48, 21, 43, 14, 33, 49, 54, 14, 44, 21, 62, 13, 23, 8, 62, 15, 51, 44, 7, 30, 37, 20, 42, 56, 7, 39, 18, 50, 11, 61, 9, 19, 43, 57, 2, 48, 11, 39, 60, 28, 4, 37, 17, 35, 1, 33, 11, 31, 14, 48, 19, 35, 51, 46, 21, 44, 29, 12, 41, 2, 22, 58, 26, 54, 4, 59, 38, 2, 33, 57, 1, 63, 13, 28, 51, 15, 40, 18, 45, 8, 30, 43, 37, 54, 19, 8, 59, 21, 6, 60, 29, 55, 10, 63, 15, 47, 17 },
01540   { 3, 50, 10, 62, 18, 5, 27, 49, 60, 23, 55, 18, 62, 24, 56, 10, 59, 28, 2, 23, 34, 59, 43, 20, 10, 42, 8, 49, 1, 37, 57, 6, 51, 29, 53, 7, 23, 31, 5, 32, 51, 0, 35, 54, 45, 31, 5, 26, 36, 24, 55, 15, 48, 29, 14, 48, 26, 60, 21, 41, 36, 26, 50, 33, 14, 44, 17, 24, 52, 15, 46, 23, 54, 6, 47, 21, 60, 50, 4, 53, 29, 61, 8, 23, 1, 60, 19, 6, 53, 16, 47, 34, 6, 39, 16, 31, 12, 20, 53, 22, 30, 43, 25, 46, 35, 6, 44, 32, 53, 26, 55, 19, 11, 59, 5, 33, 51, 1, 35, 53, 25, 3, 42, 23, 44, 32, 7, 53 },
01541   { 22, 44, 37, 6, 26, 51, 38, 0, 34, 13, 31, 46, 3, 37, 6, 19, 40, 21, 47, 63, 12, 5, 29, 55, 22, 58, 34, 28, 60, 22, 11, 41, 17, 38, 9, 44, 59, 39, 56, 19, 11, 47, 25, 15, 3, 39, 57, 17, 61, 11, 46, 3, 58, 9, 54, 35, 2, 34, 8, 45, 15, 56, 5, 23, 53, 33, 63, 35, 4, 59, 10, 51, 13, 61, 29, 41, 15, 25, 43, 19, 40, 10, 54, 33, 41, 12, 38, 51, 31, 26, 61, 9, 30, 45, 24, 62, 49, 40, 10, 61, 14, 49, 5, 17, 54, 20, 60, 23, 3, 13, 35, 50, 32, 23, 46, 27, 38, 63, 16, 12, 39, 48, 18, 51, 1, 27, 56, 35 },
01542   { 63, 15, 30, 55, 43, 14, 57, 17, 53, 44, 7, 48, 26, 50, 32, 60, 0, 53, 14, 31, 50, 24, 46, 0, 38, 13, 4, 52, 16, 45, 30, 59, 0, 25, 55, 35, 16, 10, 26, 42, 58, 29, 60, 38, 50, 22, 28, 47, 0, 50, 28, 19, 33, 39, 11, 44, 16, 52, 24, 59, 3, 38, 27, 51, 0, 21, 7, 42, 26, 34, 21, 40, 33, 18, 39, 3, 54, 38, 8, 59, 0, 44, 27, 15, 58, 28, 57, 9, 43, 0, 36, 50, 20, 59, 8, 34, 0, 27, 47, 7, 36, 19, 56, 32, 0, 38, 11, 29, 62, 47, 6, 61, 0, 41, 14, 56, 10, 23, 45, 31, 57, 8, 36, 13, 58, 38, 11, 19 },
01543   { 0, 34, 12, 47, 21, 2, 40, 30, 11, 25, 61, 20, 40, 15, 35, 22, 45, 36, 7, 41, 17, 57, 9, 48, 32, 62, 44, 24, 35, 3, 54, 13, 33, 63, 19, 4, 48, 22, 62, 2, 37, 8, 33, 6, 20, 52, 9, 32, 43, 13, 39, 63, 25, 4, 49, 23, 62, 32, 9, 30, 48, 18, 63, 12, 46, 29, 58, 13, 48, 8, 57, 31, 0, 51, 9, 58, 12, 22, 47, 29, 35, 22, 49, 5, 46, 4, 34, 20, 63, 24, 56, 11, 41, 3, 51, 19, 56, 35, 17, 58, 28, 42, 9, 45, 59, 26, 51, 42, 17, 36, 25, 15, 53, 21, 44, 3, 30, 55, 5, 50, 21, 28, 61, 32, 6, 49, 28, 46 },
01544   { 58, 42, 60, 4, 31, 59, 22, 63, 35, 38, 9, 54, 1, 57, 8, 51, 16, 58, 27, 53, 3, 38, 30, 15, 27, 6, 19, 56, 10, 50, 21, 36, 47, 5, 43, 28, 51, 32, 13, 46, 18, 54, 16, 43, 63, 12, 36, 59, 22, 34, 5, 52, 17, 59, 27, 41, 0, 19, 55, 37, 13, 43, 6, 34, 41, 10, 36, 55, 19, 44, 3, 16, 58, 27, 49, 25, 32, 62, 17, 55, 13, 63, 18, 52, 25, 37, 17, 48, 13, 32, 5, 46, 28, 37, 14, 43, 25, 5, 51, 39, 3, 52, 33, 22, 8, 40, 12, 4, 57, 9, 46, 39, 28, 58, 13, 62, 17, 42, 19, 36, 0, 47, 16, 43, 24, 21, 54, 13 },
01545   { 25, 9, 23, 50, 36, 8, 45, 14, 3, 51, 16, 28, 44, 12, 42, 29, 4, 26, 10, 47, 22, 61, 18, 54, 51, 39, 46, 13, 41, 26, 58, 7, 18, 39, 12, 57, 15, 1, 52, 27, 41, 23, 48, 1, 27, 45, 18, 2, 57, 26, 55, 8, 43, 31, 6, 58, 14, 51, 40, 5, 61, 31, 24, 54, 17, 60, 22, 1, 39, 30, 53, 45, 36, 13, 43, 5, 45, 2, 37, 6, 34, 42, 2, 39, 10, 62, 7, 54, 40, 18, 60, 15, 52, 21, 63, 8, 55, 46, 15, 30, 23, 13, 62, 16, 50, 24, 58, 31, 48, 21, 34, 2, 49, 7, 31, 37, 26, 48, 9, 61, 40, 11, 52, 2, 60, 40, 4, 37 },
01546   { 52, 28, 39, 16, 54, 19, 29, 55, 42, 20, 58, 33, 24, 63, 18, 55, 39, 62, 43, 34, 12, 40, 6, 35, 2, 25, 8, 62, 34, 1, 31, 42, 61, 27, 53, 24, 40, 61, 34, 8, 59, 4, 30, 56, 40, 6, 53, 42, 10, 48, 16, 37, 12, 46, 21, 36, 47, 11, 28, 45, 22, 10, 57, 2, 49, 31, 14, 44, 61, 11, 25, 6, 23, 63, 18, 36, 28, 56, 20, 51, 11, 48, 27, 56, 32, 22, 45, 30, 2, 42, 27, 39, 1, 44, 23, 31, 38, 22, 11, 61, 43, 54, 4, 47, 35, 2, 44, 16, 28, 54, 12, 62, 18, 43, 10, 52, 1, 58, 33, 15, 29, 56, 20, 34, 9, 30, 48, 17 },
01547   { 46, 2, 56, 11, 41, 1, 49, 6, 27, 47, 2, 48, 5, 32, 37, 3, 13, 19, 32, 1, 55, 28, 60, 17, 43, 59, 32, 20, 49, 16, 55, 23, 14, 46, 2, 36, 6, 30, 20, 49, 12, 47, 35, 14, 21, 60, 29, 14, 35, 24, 46, 1, 56, 29, 53, 8, 33, 23, 56, 1, 35, 46, 20, 39, 26, 4, 53, 28, 17, 38, 60, 34, 48, 9, 55, 15, 46, 7, 41, 31, 60, 24, 16, 36, 1, 59, 19, 52, 35, 6, 55, 11, 59, 33, 7, 57, 4, 29, 48, 1, 19, 26, 37, 30, 18, 63, 37, 6, 59, 1, 40, 24, 56, 33, 46, 22, 35, 7, 24, 53, 39, 5, 26, 45, 55, 18, 62, 7 },
01548   { 20, 60, 29, 34, 20, 62, 33, 52, 10, 36, 13, 60, 41, 21, 50, 27, 56, 49, 8, 51, 21, 45, 11, 48, 8, 23, 53, 3, 29, 44, 5, 52, 9, 32, 50, 17, 43, 56, 3, 38, 24, 10, 62, 25, 51, 9, 33, 49, 61, 7, 30, 62, 22, 19, 2, 42, 63, 5, 49, 18, 60, 15, 52, 7, 43, 56, 23, 50, 5, 50, 2, 20, 41, 30, 1, 52, 22, 61, 14, 26, 3, 43, 53, 7, 47, 28, 11, 14, 23, 58, 33, 25, 47, 13, 50, 17, 40, 54, 34, 60, 41, 6, 59, 14, 50, 7, 25, 55, 20, 42, 51, 8, 27, 4, 16, 60, 28, 50, 44, 3, 22, 49, 63, 12, 33, 1, 43, 31 },
01549   { 36, 5, 46, 8, 44, 24, 13, 39, 25, 57, 31, 18, 8, 52, 10, 45, 6, 30, 36, 24, 63, 4, 33, 26, 57, 40, 15, 56, 37, 12, 40, 25, 37, 58, 11, 63, 21, 45, 16, 60, 31, 53, 18, 33, 3, 45, 23, 0, 20, 54, 40, 15, 50, 38, 60, 16, 25, 42, 29, 38, 7, 41, 25, 62, 18, 33, 8, 35, 42, 16, 32, 56, 12, 39, 59, 19, 34, 9, 49, 38, 57, 12, 21, 50, 14, 40, 61, 44, 50, 9, 49, 19, 3, 29, 35, 62, 12, 24, 7, 18, 52, 32, 10, 46, 21, 41, 32, 11, 36, 29, 14, 34, 60, 38, 54, 11, 41, 14, 19, 57, 32, 16, 7, 41, 51, 25, 14, 57 },
01550   { 53, 18, 26, 50, 15, 58, 4, 63, 17, 43, 7, 40, 61, 35, 15, 41, 23, 60, 16, 38, 14, 42, 19, 50, 0, 31, 10, 46, 27, 63, 18, 60, 0, 20, 29, 39, 8, 26, 37, 5, 42, 0, 44, 39, 57, 17, 58, 41, 28, 37, 4, 32, 9, 44, 12, 31, 54, 10, 59, 14, 27, 53, 12, 36, 0, 47, 13, 63, 21, 58, 10, 24, 50, 27, 4, 26, 44, 53, 31, 0, 18, 42, 29, 33, 57, 4, 32, 26, 0, 38, 16, 61, 41, 53, 20, 0, 42, 44, 49, 27, 10, 56, 39, 0, 57, 15, 53, 49, 3, 61, 22, 47, 17, 5, 49, 26, 2, 63, 39, 10, 47, 27, 37, 23, 4, 59, 38, 10 },
01551   { 23, 39, 61, 3, 37, 28, 48, 31, 0, 34, 51, 23, 2, 26, 58, 0, 53, 11, 46, 1, 57, 29, 52, 14, 37, 61, 21, 35, 2, 49, 7, 34, 47, 55, 4, 33, 54, 13, 58, 52, 19, 50, 22, 7, 13, 29, 36, 11, 51, 17, 60, 25, 55, 4, 34, 51, 0, 35, 20, 48, 32, 3, 51, 30, 59, 28, 40, 3, 46, 29, 54, 43, 7, 62, 47, 11, 39, 4, 23, 46, 55, 8, 63, 5, 25, 37, 18, 46, 21, 56, 31, 5, 36, 8, 45, 58, 26, 15, 2, 36, 47, 21, 29, 44, 25, 34, 3, 27, 43, 10, 52, 0, 45, 30, 24, 36, 43, 18, 34, 59, 0, 52, 61, 15, 44, 19, 30, 49 },
01552   { 0, 27, 12, 43, 54, 9, 22, 53, 21, 46, 15, 55, 29, 47, 20, 33, 39, 28, 59, 35, 9, 44, 5, 24, 47, 7, 52, 17, 56, 22, 30, 42, 14, 26, 45, 18, 49, 1, 24, 34, 11, 27, 55, 32, 61, 47, 2, 56, 6, 44, 13, 47, 36, 27, 58, 22, 16, 47, 40, 4, 57, 38, 21, 45, 16, 9, 56, 26, 11, 38, 0, 22, 36, 17, 33, 57, 16, 30, 62, 15, 35, 40, 20, 45, 59, 10, 54, 8, 63, 13, 52, 27, 22, 57, 28, 12, 32, 51, 55, 22, 63, 4, 16, 54, 12, 62, 45, 19, 58, 13, 32, 40, 20, 56, 7, 57, 9, 54, 6, 29, 42, 21, 8, 55, 35, 47, 6, 41 },
01553   { 56, 33, 58, 32, 19, 35, 42, 6, 59, 11, 38, 5, 49, 12, 62, 7, 52, 17, 5, 25, 54, 20, 61, 31, 54, 27, 41, 11, 44, 5, 59, 12, 36, 51, 10, 61, 28, 41, 48, 9, 43, 63, 5, 40, 20, 8, 49, 26, 34, 21, 58, 1, 18, 45, 7, 39, 61, 26, 8, 50, 23, 10, 63, 5, 55, 37, 19, 49, 52, 15, 59, 47, 13, 54, 1, 25, 42, 58, 10, 48, 3, 27, 50, 1, 17, 48, 34, 41, 16, 40, 2, 45, 10, 39, 17, 61, 5, 38, 19, 9, 41, 31, 60, 38, 5, 23, 36, 8, 30, 55, 24, 63, 12, 48, 14, 51, 31, 20, 45, 25, 12, 50, 32, 2, 28, 11, 62, 14 },
01554   { 44, 16, 7, 48, 1, 62, 16, 50, 27, 33, 61, 25, 17, 44, 31, 14, 22, 43, 32, 48, 18, 40, 8, 36, 3, 16, 33, 62, 23, 38, 25, 53, 2, 21, 41, 6, 22, 15, 59, 29, 16, 37, 26, 15, 52, 42, 23, 15, 54, 39, 10, 30, 53, 11, 49, 24, 2, 43, 55, 17, 34, 44, 15, 31, 24, 44, 2, 32, 7, 35, 25, 5, 40, 45, 29, 51, 6, 21, 37, 52, 24, 60, 13, 31, 53, 23, 2, 28, 49, 24, 31, 60, 20, 51, 1, 34, 48, 14, 59, 33, 50, 1, 18, 33, 48, 60, 17, 51, 39, 6, 38, 2, 35, 29, 40, 23, 1, 62, 15, 53, 37, 17, 46, 57, 40, 51, 24, 22 },
01555   { 5, 37, 52, 24, 45, 13, 40, 3, 45, 9, 19, 42, 56, 4, 37, 46, 56, 2, 63, 11, 51, 1, 49, 13, 59, 45, 39, 1, 48, 15, 58, 9, 46, 31, 54, 35, 57, 38, 3, 46, 56, 4, 47, 57, 1, 30, 38, 63, 3, 46, 28, 63, 41, 14, 33, 62, 19, 32, 13, 28, 61, 1, 53, 42, 11, 60, 22, 62, 27, 42, 61, 31, 19, 8, 61, 12, 32, 55, 2, 18, 33, 12, 43, 36, 9, 62, 30, 55, 6, 58, 35, 7, 43, 29, 54, 23, 43, 30, 3, 25, 11, 45, 52, 28, 7, 14, 42, 1, 22, 50, 16, 53, 19, 59, 4, 46, 33, 41, 4, 35, 58, 5, 26, 13, 20, 2, 34, 54 },
01556   { 30, 63, 21, 10, 26, 55, 29, 59, 23, 39, 53, 1, 36, 24, 59, 27, 10, 34, 23, 38, 30, 60, 22, 42, 28, 19, 9, 57, 30, 19, 43, 33, 13, 63, 3, 19, 11, 50, 31, 20, 14, 34, 10, 35, 17, 59, 7, 31, 19, 25, 50, 5, 20, 57, 29, 6, 52, 41, 4, 46, 20, 37, 26, 17, 49, 6, 39, 18, 53, 14, 3, 49, 57, 23, 34, 48, 14, 41, 28, 38, 56, 6, 58, 25, 39, 19, 43, 15, 37, 11, 47, 18, 53, 4, 37, 9, 62, 21, 53, 40, 57, 24, 13, 40, 56, 26, 47, 31, 59, 25, 45, 27, 10, 43, 21, 61, 13, 27, 48, 9, 23, 43, 31, 62, 38, 59, 9, 47 },
01557   { 25, 4, 40, 60, 34, 6, 18, 36, 8, 57, 12, 30, 49, 14, 6, 54, 41, 16, 50, 6, 43, 15, 34, 4, 53, 24, 50, 35, 4, 51, 7, 55, 28, 24, 39, 44, 60, 7, 25, 62, 42, 53, 24, 61, 28, 45, 52, 12, 48, 37, 9, 35, 43, 3, 37, 48, 12, 58, 30, 52, 9, 59, 6, 57, 33, 29, 48, 4, 37, 45, 20, 34, 10, 39, 0, 60, 22, 45, 8, 63, 21, 42, 14, 49, 3, 56, 11, 46, 21, 61, 0, 42, 25, 13, 63, 17, 36, 8, 46, 16, 6, 35, 63, 0, 21, 37, 4, 57, 9, 34, 5, 61, 48, 32, 8, 37, 54, 17, 56, 30, 60, 0, 50, 16, 7, 29, 42, 17 },
01558   { 32, 50, 15, 48, 2, 43, 52, 25, 47, 16, 32, 63, 21, 52, 40, 19, 0, 61, 29, 58, 20, 56, 26, 46, 12, 55, 6, 22, 62, 32, 17, 40, 0, 49, 34, 8, 27, 32, 48, 0, 21, 39, 5, 44, 12, 6, 22, 40, 0, 57, 16, 60, 23, 17, 54, 22, 36, 15, 24, 39, 19, 34, 47, 23, 0, 54, 13, 51, 24, 9, 55, 16, 52, 27, 44, 20, 4, 54, 26, 49, 0, 30, 46, 16, 29, 51, 34, 4, 52, 28, 33, 15, 57, 39, 26, 49, 0, 56, 27, 31, 48, 20, 43, 29, 53, 11, 46, 19, 41, 13, 55, 18, 0, 57, 26, 51, 2, 44, 6, 38, 14, 40, 22, 45, 36, 53, 3, 57 },
01559   { 44, 12, 37, 28, 22, 57, 11, 38, 0, 51, 9, 41, 4, 29, 11, 47, 33, 45, 12, 26, 3, 36, 9, 63, 31, 16, 38, 44, 14, 47, 25, 61, 20, 58, 15, 47, 17, 57, 13, 36, 9, 51, 18, 29, 50, 36, 54, 20, 61, 27, 32, 13, 53, 44, 9, 27, 0, 63, 45, 2, 56, 10, 14, 43, 41, 28, 58, 11, 35, 60, 30, 41, 6, 63, 11, 51, 37, 32, 15, 10, 35, 53, 5, 61, 22, 7, 26, 59, 23, 9, 44, 48, 21, 3, 51, 32, 24, 41, 12, 61, 2, 55, 9, 15, 35, 58, 28, 15, 62, 30, 37, 23, 42, 29, 11, 17, 35, 24, 63, 20, 52, 28, 8, 55, 11, 23, 47, 19 },
01560   { 0, 56, 8, 53, 14, 31, 61, 20, 55, 28, 62, 18, 35, 60, 25, 57, 7, 23, 39, 54, 47, 17, 43, 0, 40, 59, 29, 2, 56, 10, 37, 5, 43, 11, 29, 52, 1, 23, 54, 41, 59, 30, 55, 1, 62, 15, 33, 4, 43, 10, 47, 39, 1, 31, 40, 60, 49, 33, 7, 55, 26, 50, 31, 61, 8, 18, 21, 32, 44, 1, 25, 47, 18, 36, 30, 23, 59, 7, 40, 59, 27, 19, 38, 32, 44, 54, 40, 17, 38, 60, 27, 6, 35, 55, 10, 14, 44, 5, 50, 17, 38, 26, 42, 50, 18, 3, 44, 52, 2, 49, 7, 52, 15, 46, 62, 39, 55, 10, 31, 48, 3, 58, 33, 18, 61, 34, 13, 59 },
01561   { 39, 27, 63, 20, 35, 41, 4, 45, 26, 5, 38, 13, 44, 2, 50, 17, 37, 52, 2, 13, 28, 58, 24, 51, 21, 8, 34, 48, 27, 42, 18, 51, 31, 56, 5, 36, 38, 44, 4, 17, 26, 11, 38, 23, 42, 8, 56, 39, 24, 51, 5, 56, 21, 59, 14, 6, 18, 42, 22, 35, 16, 37, 3, 25, 39, 46, 63, 5, 50, 17, 58, 8, 55, 3, 50, 12, 43, 17, 47, 2, 51, 9, 62, 12, 1, 35, 13, 50, 1, 37, 12, 51, 19, 29, 46, 59, 22, 58, 33, 45, 22, 60, 10, 32, 61, 39, 8, 33, 25, 36, 20, 60, 38, 4, 21, 5, 28, 45, 12, 18, 42, 11, 49, 1, 27, 40, 6, 30 },
01562   { 24, 16, 42, 1, 50, 10, 48, 17, 33, 43, 24, 48, 21, 55, 31, 42, 10, 21, 63, 35, 49, 6, 33, 13, 41, 53, 10, 20, 60, 6, 53, 26, 12, 41, 22, 60, 14, 28, 63, 33, 49, 3, 45, 16, 48, 26, 14, 46, 18, 30, 35, 26, 8, 50, 29, 51, 25, 57, 12, 47, 53, 9, 62, 20, 54, 2, 36, 15, 40, 28, 33, 13, 38, 24, 46, 1, 29, 56, 33, 20, 44, 24, 41, 26, 57, 20, 63, 8, 30, 55, 5, 41, 62, 8, 34, 2, 37, 10, 19, 6, 37, 1, 53, 23, 5, 27, 58, 22, 43, 12, 50, 26, 9, 34, 54, 32, 49, 1, 59, 37, 22, 46, 25, 36, 51, 15, 54, 46 },
01563   { 52, 7, 45, 33, 26, 58, 14, 60, 7, 54, 3, 58, 8, 34, 14, 5, 59, 30, 18, 44, 8, 22, 48, 62, 3, 26, 55, 38, 23, 16, 39, 1, 62, 24, 49, 9, 53, 19, 46, 7, 19, 60, 31, 58, 2, 34, 53, 7, 59, 2, 62, 42, 46, 19, 36, 11, 44, 4, 38, 28, 1, 43, 32, 51, 12, 29, 56, 22, 52, 2, 62, 49, 22, 60, 14, 35, 63, 5, 25, 57, 14, 53, 4, 46, 18, 31, 42, 22, 47, 20, 58, 31, 16, 43, 23, 54, 30, 42, 52, 57, 29, 49, 30, 13, 45, 48, 16, 55, 6, 63, 1, 44, 14, 58, 19, 47, 15, 24, 51, 34, 6, 55, 5, 63, 20, 41, 21, 9 },
01564   { 30, 62, 18, 55, 5, 23, 39, 29, 49, 30, 15, 36, 28, 46, 60, 25, 39, 46, 4, 32, 61, 40, 15, 30, 36, 45, 14, 2, 49, 33, 57, 45, 18, 32, 3, 45, 30, 2, 35, 52, 40, 27, 13, 21, 38, 63, 20, 28, 37, 23, 16, 10, 13, 55, 2, 62, 21, 32, 60, 17, 58, 23, 5, 40, 16, 48, 7, 45, 10, 26, 43, 19, 6, 31, 52, 21, 39, 16, 48, 9, 37, 28, 36, 55, 7, 48, 3, 59, 15, 45, 25, 1, 53, 13, 47, 7, 62, 15, 4, 25, 12, 41, 18, 60, 38, 11, 34, 19, 39, 31, 29, 56, 23, 42, 3, 27, 60, 41, 8, 16, 61, 29, 43, 9, 32, 2, 60, 34 },
01565   { 3, 38, 13, 37, 52, 44, 2, 19, 12, 42, 63, 19, 40, 1, 20, 50, 12, 55, 15, 56, 27, 1, 54, 11, 57, 18, 32, 63, 44, 4, 29, 13, 37, 61, 35, 16, 42, 57, 12, 22, 6, 55, 43, 10, 50, 5, 44, 11, 48, 52, 34, 58, 28, 41, 38, 30, 7, 52, 11, 49, 30, 14, 45, 27, 59, 34, 21, 38, 32, 58, 11, 36, 56, 42, 9, 41, 3, 54, 31, 42, 0, 60, 16, 11, 39, 24, 52, 33, 6, 36, 10, 40, 32, 60, 26, 20, 39, 28, 47, 34, 63, 8, 54, 3, 24, 56, 0, 51, 13, 47, 16, 40, 7, 35, 52, 11, 36, 4, 57, 30, 39, 13, 18, 50, 58, 28, 12, 48 },
01566   { 57, 24, 49, 21, 10, 31, 61, 36, 56, 0, 22, 53, 11, 56, 32, 7, 36, 27, 41, 9, 46, 19, 34, 42, 25, 7, 50, 9, 28, 21, 54, 8, 50, 7, 27, 59, 10, 25, 48, 62, 37, 0, 33, 58, 25, 18, 32, 61, 0, 15, 45, 5, 50, 3, 23, 55, 47, 17, 40, 6, 60, 34, 53, 8, 41, 0, 61, 13, 54, 4, 46, 28, 0, 17, 48, 27, 58, 13, 23, 61, 33, 21, 50, 30, 62, 8, 14, 29, 56, 27, 61, 49, 17, 2, 44, 11, 51, 0, 59, 17, 40, 20, 32, 47, 36, 21, 42, 28, 60, 4, 54, 10, 59, 17, 30, 62, 21, 43, 26, 48, 0, 56, 36, 25, 8, 44, 39, 17 },
01567   { 10, 42, 4, 59, 27, 47, 8, 23, 51, 32, 45, 6, 37, 26, 48, 43, 62, 0, 21, 53, 38, 12, 51, 5, 60, 47, 24, 37, 59, 15, 35, 47, 22, 55, 0, 50, 21, 40, 6, 29, 15, 52, 24, 8, 41, 55, 13, 29, 40, 56, 24, 31, 19, 33, 61, 15, 0, 35, 24, 42, 21, 2, 19, 57, 24, 15, 30, 50, 20, 25, 40, 16, 57, 34, 61, 8, 29, 45, 6, 49, 11, 47, 2, 44, 19, 57, 38, 50, 12, 42, 21, 4, 35, 52, 28, 56, 23, 36, 13, 45, 4, 52, 27, 14, 6, 62, 9, 45, 21, 37, 25, 46, 33, 49, 0, 44, 7, 53, 13, 19, 53, 31, 3, 47, 15, 56, 22, 51 },
01568   { 35, 28, 53, 32, 1, 16, 54, 40, 9, 17, 25, 58, 14, 59, 3, 22, 16, 51, 31, 5, 23, 58, 28, 17, 35, 20, 0, 42, 11, 52, 3, 31, 41, 17, 43, 13, 32, 54, 18, 60, 32, 45, 17, 49, 2, 36, 51, 22, 7, 36, 9, 63, 48, 12, 46, 26, 43, 28, 63, 13, 48, 37, 51, 33, 5, 47, 55, 9, 42, 63, 7, 51, 24, 12, 37, 19, 55, 34, 18, 38, 15, 28, 54, 34, 5, 43, 22, 0, 48, 14, 54, 24, 58, 9, 38, 5, 32, 55, 21, 30, 49, 9, 59, 43, 30, 51, 35, 26, 7, 53, 2, 22, 14, 27, 57, 18, 38, 24, 33, 45, 10, 41, 20, 60, 37, 5, 32, 0 },
01569   { 63, 19, 15, 40, 62, 35, 14, 28, 46, 61, 4, 49, 35, 10, 29, 54, 33, 8, 45, 62, 37, 1, 43, 55, 10, 52, 61, 30, 19, 40, 25, 62, 11, 38, 27, 58, 36, 3, 46, 8, 39, 4, 62, 28, 47, 20, 4, 54, 47, 27, 43, 1, 21, 38, 8, 58, 10, 54, 4, 56, 9, 26, 12, 39, 60, 27, 18, 37, 1, 31, 35, 5, 45, 50, 2, 43, 26, 1, 59, 23, 56, 40, 7, 26, 58, 17, 32, 63, 25, 39, 7, 31, 45, 19, 63, 15, 48, 8, 37, 61, 16, 34, 1, 56, 18, 3, 15, 58, 49, 32, 63, 41, 55, 5, 40, 22, 50, 6, 59, 2, 63, 23, 52, 11, 26, 61, 44, 23 },
01570   { 11, 56, 46, 6, 22, 43, 58, 3, 34, 21, 38, 30, 18, 44, 52, 13, 41, 57, 17, 28, 14, 49, 25, 7, 33, 39, 26, 6, 56, 48, 1, 20, 56, 5, 46, 9, 19, 51, 30, 25, 56, 21, 35, 14, 57, 42, 16, 33, 10, 57, 17, 59, 41, 25, 53, 37, 20, 40, 30, 18, 31, 62, 44, 22, 3, 44, 11, 48, 23, 53, 18, 60, 29, 22, 62, 15, 53, 47, 10, 41, 3, 19, 52, 36, 13, 46, 10, 35, 3, 61, 41, 16, 1, 50, 26, 42, 18, 46, 2, 25, 54, 20, 39, 23, 47, 31, 41, 12, 38, 17, 8, 19, 31, 48, 12, 61, 9, 54, 29, 35, 15, 38, 6, 43, 34, 14, 7, 47 },
01571   { 39, 2, 33, 26, 53, 8, 18, 50, 41, 12, 53, 1, 63, 24, 19, 39, 2, 24, 47, 10, 60, 38, 19, 63, 48, 4, 15, 45, 32, 14, 60, 36, 29, 53, 23, 63, 34, 12, 61, 1, 43, 11, 53, 30, 1, 26, 60, 45, 23, 39, 3, 29, 12, 50, 4, 16, 51, 3, 45, 36, 50, 1, 16, 54, 35, 14, 57, 30, 58, 9, 46, 14, 41, 10, 32, 38, 4, 30, 21, 51, 32, 63, 25, 1, 60, 27, 53, 18, 51, 22, 28, 55, 34, 12, 40, 3, 60, 29, 57, 41, 6, 44, 11, 53, 8, 61, 24, 57, 1, 28, 44, 59, 36, 3, 34, 25, 41, 31, 16, 44, 22, 47, 28, 58, 1, 49, 54, 29 },
01572   { 58, 25, 50, 13, 38, 30, 60, 24, 6, 57, 27, 42, 9, 45, 6, 61, 30, 50, 4, 34, 29, 3, 46, 13, 22, 42, 58, 28, 9, 39, 23, 44, 7, 15, 44, 2, 40, 15, 47, 41, 23, 37, 7, 59, 38, 11, 34, 6, 62, 14, 52, 35, 55, 19, 32, 61, 33, 24, 57, 6, 22, 59, 29, 7, 49, 25, 40, 3, 17, 39, 27, 52, 0, 55, 16, 57, 24, 61, 36, 6, 29, 12, 48, 39, 20, 44, 6, 40, 33, 5, 48, 10, 57, 36, 22, 51, 33, 9, 24, 12, 62, 29, 50, 35, 14, 43, 5, 33, 47, 52, 13, 23, 10, 51, 56, 16, 46, 1, 49, 4, 61, 9, 52, 18, 31, 21, 36, 17 },
01573   { 19, 42, 9, 48, 2, 44, 11, 37, 48, 20, 33, 16, 55, 35, 49, 15, 37, 20, 59, 16, 53, 22, 56, 31, 50, 11, 34, 54, 16, 51, 4, 49, 33, 53, 21, 28, 56, 24, 31, 9, 52, 16, 48, 24, 44, 13, 51, 20, 31, 49, 18, 6, 34, 2, 44, 14, 47, 8, 15, 43, 13, 41, 33, 52, 20, 61, 7, 51, 34, 62, 4, 20, 36, 33, 43, 8, 46, 13, 53, 17, 45, 42, 9, 31, 52, 11, 30, 56, 13, 59, 17, 44, 27, 6, 62, 11, 43, 17, 49, 38, 26, 2, 16, 27, 58, 21, 54, 18, 26, 5, 35, 61, 43, 27, 7, 39, 14, 58, 37, 55, 20, 33, 13, 40, 62, 10, 55, 5 },
01574   { 51, 14, 61, 29, 59, 20, 55, 31, 0, 49, 11, 60, 3, 26, 22, 56, 0, 40, 12, 43, 41, 8, 36, 0, 17, 57, 24, 2, 46, 26, 61, 18, 0, 38, 12, 59, 6, 49, 3, 57, 19, 63, 5, 33, 18, 54, 28, 56, 0, 43, 26, 46, 63, 27, 56, 22, 27, 54, 38, 28, 63, 24, 10, 45, 0, 31, 42, 21, 12, 25, 44, 49, 59, 6, 26, 50, 3, 34, 27, 59, 0, 35, 62, 16, 4, 58, 47, 0, 43, 24, 37, 2, 54, 20, 46, 31, 0, 56, 34, 5, 55, 45, 60, 37, 0, 40, 10, 38, 63, 46, 15, 20, 0, 53, 21, 62, 30, 11, 24, 27, 40, 0, 57, 26, 3, 45, 27, 35 },
01575 };
01576 
01577 #else
01578 #define DM_WIDTH 8
01579 #define DM_WIDTH_SHIFT 3
01580 #define DM_HEIGHT 8
01581 static const unsigned char DM[8][8] =
01582 {
01583   { 0,  32, 8,  40, 2,  34, 10, 42 },
01584   { 48, 16, 56, 24, 50, 18, 58, 26 },
01585   { 12, 44, 4,  36, 14, 46, 6,  38 },
01586   { 60, 28, 52, 20, 62, 30, 54, 22 },
01587   { 3,  35, 11, 43, 1,  33, 9,  41 },
01588   { 51, 19, 59, 27, 49, 17, 57, 25 },
01589   { 15, 47, 7,  39, 13, 45, 5,  37 },
01590   { 63, 31, 55, 23, 61, 29, 53, 21 }
01591 };
01592 #endif
01593 
01594 
01595 static void
01596 xxlib_rgb_preprocess_dm_565 (XlibRgbHandle *handle)
01597 {
01598   int i;
01599   uint32 dith;
01600 
01601   if (handle->DM_565 == NULL)
01602     {
01603       handle->DM_565 = malloc(sizeof(uint32) * DM_WIDTH * DM_HEIGHT);
01604       for (i = 0; i < DM_WIDTH * DM_HEIGHT; i++)
01605         {
01606           dith = DM[i / DM_WIDTH][i % DM_WIDTH] >> 3;
01607           handle->DM_565[i] = (dith << 20) | dith | (((7 - dith) >> 1) << 10);
01608 #ifdef VERBOSE
01609           printf ("%i %x %x\n", i, dith, handle->DM_565[i]);
01610 #endif
01611         }
01612     }
01613 }
01614 
01615 static void
01616 xxlib_rgb_convert_8_d666 (XlibRgbHandle *handle, XImage *image,
01617                         int ax, int ay, int width, int height,
01618                         unsigned char *buf, int rowstride,
01619                         int x_align, int y_align, XlibRgbCmap *cmap)
01620 {
01621   int x, y;
01622   int bpl;
01623   unsigned char *obuf, *obptr;
01624   unsigned char *bptr, *bp2;
01625   int r, g, b;
01626   const unsigned char *dmp;
01627   int dith;
01628 
01629   bptr = buf;
01630   bpl = image->bytes_per_line;
01631   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
01632   for (y = 0; y < height; y++)
01633     {
01634       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
01635       bp2 = bptr;
01636       obptr = obuf;
01637       for (x = 0; x < width; x++)
01638         {
01639           r = *bp2++;
01640           g = *bp2++;
01641           b = *bp2++;
01642           dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 7;
01643           r = ((r * 5) + dith) >> 8;
01644           g = ((g * 5) + (262 - dith)) >> 8;
01645           b = ((b * 5) + dith) >> 8;
01646           obptr[0] = handle->colorcube_d[(r << 6) | (g << 3) | b];
01647           obptr++;
01648         }
01649       bptr += rowstride;
01650       obuf += bpl;
01651     }
01652 }
01653 
01654 static void
01655 xxlib_rgb_convert_8_d (XlibRgbHandle *handle, XImage *image,
01656                      int ax, int ay, int width, int height,
01657                      unsigned char *buf, int rowstride,
01658                      int x_align, int y_align,
01659                      XlibRgbCmap *cmap)
01660 {
01661   int x, y;
01662   int bpl;
01663   unsigned char *obuf, *obptr;
01664   unsigned char *bptr, *bp2;
01665   int r, g, b;
01666   const unsigned char *dmp;
01667   int dith;
01668   int rs, gs, bs;
01669 
01670   bptr = buf;
01671   bpl = image->bytes_per_line;
01672   rs = handle->nred_shades - 1;
01673   gs = handle->ngreen_shades - 1;
01674   bs = handle->nblue_shades - 1;
01675   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
01676   for (y = 0; y < height; y++)
01677     {
01678       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
01679       bp2 = bptr;
01680       obptr = obuf;
01681       for (x = 0; x < width; x++)
01682         {
01683           r = *bp2++;
01684           g = *bp2++;
01685           b = *bp2++;
01686           dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 7;
01687           r = ((r * rs) + dith) >> 8;
01688           g = ((g * gs) + (262 - dith)) >> 8;
01689           b = ((b * bs) + dith) >> 8;
01690           obptr[0] = handle->colorcube_d[(r << 6) | (g << 3) | b];
01691           obptr++;
01692         }
01693       bptr += rowstride;
01694       obuf += bpl;
01695     }
01696 }
01697 
01698 static void
01699 xxlib_rgb_convert_8_indexed (XlibRgbHandle *handle, XImage *image,
01700                            int ax, int ay, int width, int height,
01701                            unsigned char *buf, int rowstride,
01702                            int x_align, int y_align, XlibRgbCmap *cmap)
01703 {
01704   int x, y;
01705   int bpl;
01706   unsigned char *obuf, *obptr;
01707   unsigned char *bptr, *bp2;
01708   unsigned char c;
01709   unsigned char *lut;
01710 
01711   lut = cmap->lut;
01712   bptr = buf;
01713   bpl = image->bytes_per_line;
01714   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
01715   for (y = 0; y < height; y++)
01716     {
01717       bp2 = bptr;
01718       obptr = obuf;
01719       for (x = 0; x < width; x++)
01720         {
01721           c = *bp2++;
01722           obptr[0] = lut[c];
01723           obptr++;
01724         }
01725       bptr += rowstride;
01726       obuf += bpl;
01727     }
01728 }
01729 
01730 static void
01731 xxlib_rgb_convert_gray8 (XlibRgbHandle *handle, XImage *image,
01732                        int ax, int ay, int width, int height,
01733                        unsigned char *buf, int rowstride,
01734                        int x_align, int y_align, XlibRgbCmap *cmap)
01735 {
01736   int x, y;
01737   int bpl;
01738   unsigned char *obuf, *obptr;
01739   unsigned char *bptr, *bp2;
01740   int r, g, b;
01741 
01742   bptr = buf;
01743   bpl = image->bytes_per_line;
01744   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
01745   for (y = 0; y < height; y++)
01746     {
01747       bp2 = bptr;
01748       obptr = obuf;
01749       for (x = 0; x < width; x++)
01750         {
01751           r = *bp2++;
01752           g = *bp2++;
01753           b = *bp2++;
01754           obptr[0] = (g + ((b + r) >> 1)) >> 1;
01755           obptr++;
01756         }
01757       bptr += rowstride;
01758       obuf += bpl;
01759     }
01760 }
01761 
01762 static void
01763 xxlib_rgb_convert_gray8_gray (XlibRgbHandle *handle, XImage *image,
01764                             int ax, int ay, int width, int height,
01765                             unsigned char *buf, int rowstride,
01766                             int x_align, int y_align, XlibRgbCmap *cmap)
01767 {
01768   int y;
01769   int bpl;
01770   unsigned char *obuf;
01771   unsigned char *bptr;
01772 
01773   bptr = buf;
01774   bpl = image->bytes_per_line;
01775   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
01776   for (y = 0; y < height; y++)
01777     {
01778       memcpy (obuf, bptr, (unsigned int)width);
01779       bptr += rowstride;
01780       obuf += bpl;
01781     }
01782 }
01783 
01784 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
01785 #define HAIRY_CONVERT_565
01786 #endif
01787 
01788 #ifdef HAIRY_CONVERT_565
01789 /* Render a 24-bit RGB image in buf into the XImage, without dithering.
01790    This assumes native byte ordering - what should really be done is to
01791    check whether handle->static_image->byte_order is consistent with 
01792    the _ENDIAN config flag, and if not, use a different function.
01793 
01794    This one is even faster than the one below - its inner loop loads 3
01795    words (i.e. 4 24-bit pixels), does a lot of shifting and masking,
01796    then writes 2 words. */
01797 static void
01798 xxlib_rgb_convert_565 (XlibRgbHandle *handle, XImage *image,
01799                      int ax, int ay, int width, int height,
01800                      unsigned char *buf, int rowstride,
01801                      int x_align, int y_align, XlibRgbCmap *cmap)
01802 {
01803   int x, y;
01804   unsigned char *obuf, *obptr;
01805   int bpl;
01806   unsigned char *bptr, *bp2;
01807   unsigned char r, g, b;
01808 
01809   bptr = buf;
01810   bpl = image->bytes_per_line;
01811   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
01812   for (y = 0; y < height; y++)
01813     {
01814       bp2 = bptr;
01815       obptr = obuf;
01816       if (((unsigned long)obuf | (unsigned long) bp2) & 3)
01817         {
01818           for (x = 0; x < width; x++)
01819             {
01820               r = *bp2++;
01821               g = *bp2++;
01822               b = *bp2++;
01823               ((uint16 *)obptr)[0] = ((r & 0xf8) << 8) |
01824                 ((g & 0xfc) << 3) |
01825                 (b >> 3);
01826               obptr += 2;
01827             }
01828         }
01829       else
01830         {
01831           for (x = 0; x < width - 3; x += 4)
01832             {
01833               uint32 r1b0g0r0;
01834               uint32 g2r2b1g1;
01835               uint32 b3g3r3b2;
01836 
01837               r1b0g0r0 = ((uint32 *)bp2)[0];
01838               g2r2b1g1 = ((uint32 *)bp2)[1];
01839               b3g3r3b2 = ((uint32 *)bp2)[2];
01840               ((uint32 *)obptr)[0] =
01841                 ((r1b0g0r0 & 0xf8) << 8) |
01842                 ((r1b0g0r0 & 0xfc00) >> 5) |
01843                 ((r1b0g0r0 & 0xf80000) >> 19) |
01844                 (r1b0g0r0 & 0xf8000000) |
01845                 ((g2r2b1g1 & 0xfc) << 19) |
01846                 ((g2r2b1g1 & 0xf800) << 5);
01847               ((uint32 *)obptr)[1] =
01848                 ((g2r2b1g1 & 0xf80000) >> 8) |
01849                 ((g2r2b1g1 & 0xfc000000) >> 21) |
01850                 ((b3g3r3b2 & 0xf8) >> 3) |
01851                 ((b3g3r3b2 & 0xf800) << 16) |
01852                 ((b3g3r3b2 & 0xfc0000) << 3) |
01853                 ((b3g3r3b2 & 0xf8000000) >> 11);
01854               bp2 += 12;
01855               obptr += 8;
01856             }
01857           for (; x < width; x++)
01858             {
01859               r = *bp2++;
01860               g = *bp2++;
01861               b = *bp2++;
01862               ((uint16 *)obptr)[0] = ((r & 0xf8) << 8) |
01863                 ((g & 0xfc) << 3) |
01864                 (b >> 3);
01865               obptr += 2;
01866             }
01867         }
01868       bptr += rowstride;
01869       obuf += bpl;
01870     }
01871 }
01872 #else
01873 /* Render a 24-bit RGB image in buf into the XImage, without dithering.
01874    This assumes native byte ordering - what should really be done is to
01875    check whether handle->static_image->byte_order is consistent with the
01876     _ENDIAN config flag, and if not, use a different function.
01877 
01878    This routine is faster than the one included with Gtk 1.0 for a number
01879    of reasons:
01880 
01881    1. Shifting instead of lookup tables (less memory traffic).
01882 
01883    2. Much less register pressure, especially because shifts are
01884    in the code.
01885 
01886    3. A memcpy is avoided (i.e. the transfer function).
01887 
01888    4. On big-endian architectures, byte swapping is avoided.
01889 
01890    That said, it wouldn't be hard to make it even faster - just make an
01891    inner loop that reads 3 words (i.e. 4 24-bit pixels), does a lot of
01892    shifting and masking, then writes 2 words.
01893 */
01894 static void
01895 xxlib_rgb_convert_565 (XlibRgbHandle *handle, XImage *image,
01896                      int ax, int ay, int width, int height,
01897                      unsigned char *buf, int rowstride,
01898                      int x_align, int y_align, XlibRgbCmap *cmap)
01899 {
01900   int x, y;
01901   unsigned char *obuf;
01902   int bpl;
01903   unsigned char *bptr, *bp2;
01904   unsigned char r, g, b;
01905 
01906   bptr = buf;
01907   bpl = image->bytes_per_line;
01908   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
01909   for (y = 0; y < height; y++)
01910     {
01911       bp2 = bptr;
01912       for (x = 0; x < width; x++)
01913         {
01914           r = *bp2++;
01915           g = *bp2++;
01916           b = *bp2++;
01917           ((unsigned short *)obuf)[x] = ((r & 0xf8) << 8) |
01918             ((g & 0xfc) << 3) |
01919             (b >> 3);
01920         }
01921       bptr += rowstride;
01922       obuf += bpl;
01923     }
01924 }
01925 #endif
01926 
01927 #ifdef HAIRY_CONVERT_565
01928 static void
01929 xxlib_rgb_convert_565_gray (XlibRgbHandle *handle, XImage *image,
01930                           int ax, int ay, int width, int height,
01931                           unsigned char *buf, int rowstride,
01932                           int x_align, int y_align, XlibRgbCmap *cmap)
01933 {
01934   int x, y;
01935   unsigned char *obuf, *obptr;
01936   int bpl;
01937   unsigned char *bptr, *bp2;
01938   unsigned char g;
01939 
01940   bptr = buf;
01941   bpl = image->bytes_per_line;
01942   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
01943   for (y = 0; y < height; y++)
01944     {
01945       bp2 = bptr;
01946       obptr = obuf;
01947       if (((unsigned long)obuf | (unsigned long) bp2) & 3)
01948         {
01949           for (x = 0; x < width; x++)
01950             {
01951               g = *bp2++;
01952               ((uint16 *)obptr)[0] = ((g & 0xf8) << 8) |
01953                 ((g & 0xfc) << 3) |
01954                 (g >> 3);
01955               obptr += 2;
01956             }
01957         }
01958       else
01959         {
01960           for (x = 0; x < width - 3; x += 4)
01961             {
01962               uint32 g3g2g1g0;
01963 
01964               g3g2g1g0 = ((uint32 *)bp2)[0];
01965               ((uint32 *)obptr)[0] =
01966                 ((g3g2g1g0 & 0xf8) << 8) |
01967                 ((g3g2g1g0 & 0xfc) << 3) |
01968                 ((g3g2g1g0 & 0xf8) >> 3) |
01969                 (g3g2g1g0 & 0xf800) << 16 |
01970                 ((g3g2g1g0 & 0xfc00) << 11) |
01971                 ((g3g2g1g0 & 0xf800) << 5);
01972               ((uint32 *)obptr)[1] =
01973                 ((g3g2g1g0 & 0xf80000) >> 8) |
01974                 ((g3g2g1g0 & 0xfc0000) >> 13) |
01975                 ((g3g2g1g0 & 0xf80000) >> 19) |
01976                 (g3g2g1g0 & 0xf8000000) |
01977                 ((g3g2g1g0 & 0xfc000000) >> 5) |
01978                 ((g3g2g1g0 & 0xf8000000) >> 11);
01979               bp2 += 4;
01980               obptr += 8;
01981             }
01982           for (; x < width; x++)
01983             {
01984               g = *bp2++;
01985               ((uint16 *)obptr)[0] = ((g & 0xf8) << 8) |
01986                 ((g & 0xfc) << 3) |
01987                 (g >> 3);
01988               obptr += 2;
01989             }
01990         }
01991       bptr += rowstride;
01992       obuf += bpl;
01993     }
01994 }
01995 #else
01996 static void
01997 xxlib_rgb_convert_565_gray (XlibRgbHandle *handle, XImage *image,
01998                           int ax, int ay, int width, int height,
01999                           unsigned char *buf, int rowstride,
02000                           int x_align, int y_align, XlibRgbCmap *cmap)
02001 {
02002   int x, y;
02003   unsigned char *obuf;
02004   int bpl;
02005   unsigned char *bptr, *bp2;
02006   unsigned char g;
02007 
02008   bptr = buf;
02009   bpl = image->bytes_per_line;
02010   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
02011   for (y = 0; y < height; y++)
02012     {
02013       bp2 = bptr;
02014       for (x = 0; x < width; x++)
02015         {
02016           g = *bp2++;
02017           ((uint16 *)obuf)[x] = ((g & 0xf8) << 8) |
02018             ((g & 0xfc) << 3) |
02019             (g >> 3);
02020         }
02021       bptr += rowstride;
02022       obuf += bpl;
02023     }
02024 }
02025 #endif
02026 
02027 static void
02028 xxlib_rgb_convert_565_br (XlibRgbHandle *handle, XImage *image,
02029                          int ax, int ay, int width, int height,
02030                          unsigned char *buf, int rowstride,
02031                          int x_align, int y_align, XlibRgbCmap *cmap)
02032 {
02033   int x, y;
02034   unsigned char *obuf;
02035   int bpl;
02036   unsigned char *bptr, *bp2;
02037   unsigned char r, g, b;
02038 
02039   bptr = buf;
02040   bpl = image->bytes_per_line;
02041   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
02042   for (y = 0; y < height; y++)
02043     {
02044       bp2 = bptr;
02045       for (x = 0; x < width; x++)
02046         {
02047           r = *bp2++;
02048           g = *bp2++;
02049           b = *bp2++;
02050           /* final word is:
02051              g4 g3 g2 b7 b6 b5 b4 b3  r7 r6 r5 r4 r3 g7 g6 g5
02052            */
02053           ((unsigned short *)obuf)[x] = (r & 0xf8) |
02054             ((g & 0xe0) >> 5) |
02055             ((g & 0x1c) << 11) |
02056             ((b & 0xf8) << 5);
02057         }
02058       bptr += rowstride;
02059       obuf += bpl;
02060     }
02061 }
02062 
02063 /* Thanks to Ray Lehtiniemi for a patch that resulted in a ~25% speedup
02064    in this mode. */
02065 #ifdef HAIRY_CONVERT_565
02066 static void
02067 xxlib_rgb_convert_565_d (XlibRgbHandle *handle, XImage *image,
02068                      int ax, int ay, int width, int height,
02069                      unsigned char *buf, int rowstride,
02070                      int x_align, int y_align, XlibRgbCmap *cmap)
02071 {
02072   /* Now this is what I'd call some highly tuned code! */
02073   int x, y;
02074   unsigned char *obuf, *obptr;
02075   int bpl;
02076   unsigned char *bptr, *bp2;
02077 
02078   width += x_align;
02079   height += y_align;
02080   
02081   bptr = buf;
02082   bpl = image->bytes_per_line;
02083   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
02084   for (y = y_align; y < height; y++)
02085     {
02086       uint32 *dmp = handle->DM_565 + ((y & (DM_HEIGHT - 1)) << DM_WIDTH_SHIFT);
02087       bp2 = bptr;
02088       obptr = obuf;
02089       if (((unsigned long)obuf | (unsigned long) bp2) & 3)
02090         {
02091           for (x = x_align; x < width; x++)
02092             {
02093               int32 rgb = *bp2++ << 20;
02094               rgb += *bp2++ << 10;
02095               rgb += *bp2++;
02096               rgb += dmp[x & (DM_WIDTH - 1)];
02097               rgb += 0x10040100
02098                 - ((rgb & 0x1e0001e0) >> 5)
02099                 - ((rgb & 0x00070000) >> 6);
02100 
02101               ((unsigned short *)obptr)[0] =
02102                 ((rgb & 0x0f800000) >> 12) |
02103                 ((rgb & 0x0003f000) >> 7) |
02104                 ((rgb & 0x000000f8) >> 3);
02105               obptr += 2;
02106             }
02107         }
02108       else
02109         {
02110           for (x = x_align; x < width - 3; x += 4)
02111             {
02112               uint32 r1b0g0r0;
02113               uint32 g2r2b1g1;
02114               uint32 b3g3r3b2;
02115               uint32 rgb02, rgb13;
02116 
02117               r1b0g0r0 = ((uint32 *)bp2)[0];
02118               g2r2b1g1 = ((uint32 *)bp2)[1];
02119               b3g3r3b2 = ((uint32 *)bp2)[2];
02120               rgb02 =
02121                 ((r1b0g0r0 & 0xff) << 20) +
02122                 ((r1b0g0r0 & 0xff00) << 2) +
02123                 ((r1b0g0r0 & 0xff0000) >> 16) +
02124                 dmp[x & (DM_WIDTH - 1)];
02125               rgb02 += 0x10040100
02126                 - ((rgb02 & 0x1e0001e0) >> 5)
02127                 - ((rgb02 & 0x00070000) >> 6);
02128               rgb13 =
02129                 ((r1b0g0r0 & 0xff000000) >> 4) +
02130                 ((g2r2b1g1 & 0xff) << 10) +
02131                 ((g2r2b1g1 & 0xff00) >> 8) +
02132                 dmp[(x + 1) & (DM_WIDTH - 1)];
02133               rgb13 += 0x10040100
02134                 - ((rgb13 & 0x1e0001e0) >> 5)
02135                 - ((rgb13 & 0x00070000) >> 6);
02136               ((uint32 *)obptr)[0] =
02137                 ((rgb02 & 0x0f800000) >> 12) |
02138                 ((rgb02 & 0x0003f000) >> 7) |
02139                 ((rgb02 & 0x000000f8) >> 3) |
02140                 ((rgb13 & 0x0f800000) << 4) |
02141                 ((rgb13 & 0x0003f000) << 9) |
02142                 ((rgb13 & 0x000000f8) << 13);
02143               rgb02 =
02144                 ((g2r2b1g1 & 0xff0000) << 4) +
02145                 ((g2r2b1g1 & 0xff000000) >> 14) +
02146                 (b3g3r3b2 & 0xff) +
02147                 dmp[(x + 2) & (DM_WIDTH - 1)];
02148               rgb02 += 0x10040100
02149                 - ((rgb02 & 0x1e0001e0) >> 5)
02150                 - ((rgb02 & 0x00070000) >> 6);
02151               rgb13 =
02152                 ((b3g3r3b2 & 0xff00) << 12) +
02153                 ((b3g3r3b2 & 0xff0000) >> 6) +
02154                 ((b3g3r3b2 & 0xff000000) >> 24) +
02155                 dmp[(x + 3) & (DM_WIDTH - 1)];
02156               rgb13 += 0x10040100
02157                 - ((rgb13 & 0x1e0001e0) >> 5)
02158                 - ((rgb13 & 0x00070000) >> 6);
02159               ((uint32 *)obptr)[1] =
02160                 ((rgb02 & 0x0f800000) >> 12) |
02161                 ((rgb02 & 0x0003f000) >> 7) |
02162                 ((rgb02 & 0x000000f8) >> 3) |
02163                 ((rgb13 & 0x0f800000) << 4) |
02164                 ((rgb13 & 0x0003f000) << 9) |
02165                 ((rgb13 & 0x000000f8) << 13);
02166               bp2 += 12;
02167               obptr += 8;
02168             }
02169           for (; x < width; x++)
02170             {
02171               int32 rgb = *bp2++ << 20;
02172               rgb += *bp2++ << 10;
02173               rgb += *bp2++;
02174               rgb += dmp[x & (DM_WIDTH - 1)];
02175               rgb += 0x10040100
02176                 - ((rgb & 0x1e0001e0) >> 5)
02177                 - ((rgb & 0x00070000) >> 6);
02178 
02179               ((unsigned short *)obptr)[0] =
02180                 ((rgb & 0x0f800000) >> 12) |
02181                 ((rgb & 0x0003f000) >> 7) |
02182                 ((rgb & 0x000000f8) >> 3);
02183               obptr += 2;
02184             }
02185         }
02186       bptr += rowstride;
02187       obuf += bpl;
02188     }
02189 }
02190 #else
02191 static void
02192 xxlib_rgb_convert_565_d (XlibRgbHandle *handle, XImage *image,
02193                        int ax, int ay, int width, int height,
02194                        unsigned char *buf, int rowstride,
02195                        int x_align, int y_align, XlibRgbCmap *cmap)
02196 {
02197   int x, y;
02198   unsigned char *obuf;
02199   int bpl;
02200   unsigned char *bptr;
02201 
02202   width += x_align;
02203   height += y_align;
02204   
02205   bptr = buf;
02206   bpl = image->bytes_per_line;
02207   obuf = ((unsigned char *)image->data) + ay * bpl + (ax - x_align) * 2;
02208 
02209   for (y = y_align; y < height; y++)
02210     {
02211       uint32 *dmp = handle->DM_565 + ((y & (DM_HEIGHT - 1)) << DM_WIDTH_SHIFT);
02212       unsigned char *bp2 = bptr;
02213 
02214       for (x = x_align; x < width; x++)
02215         {
02216           int32 rgb = *bp2++ << 20;
02217           rgb += *bp2++ << 10;
02218           rgb += *bp2++;
02219           rgb += dmp[x & (DM_WIDTH - 1)];
02220           rgb += 0x10040100
02221             - ((rgb & 0x1e0001e0) >> 5)
02222             - ((rgb & 0x00070000) >> 6);
02223 
02224           ((unsigned short *)obuf)[x] =
02225             ((rgb & 0x0f800000) >> 12) |
02226             ((rgb & 0x0003f000) >> 7) |
02227             ((rgb & 0x000000f8) >> 3);
02228         }
02229 
02230       bptr += rowstride;
02231       obuf += bpl;
02232     }
02233 }
02234 #endif
02235 
02236 static void
02237 xxlib_rgb_convert_555 (XlibRgbHandle *handle, XImage *image,
02238                      int ax, int ay, int width, int height,
02239                      unsigned char *buf, int rowstride,
02240                      int x_align, int y_align, XlibRgbCmap *cmap)
02241 {
02242   int x, y;
02243   unsigned char *obuf;
02244   int bpl;
02245   unsigned char *bptr, *bp2;
02246   unsigned char r, g, b;
02247 
02248   bptr = buf;
02249   bpl = image->bytes_per_line;
02250   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
02251   for (y = 0; y < height; y++)
02252     {
02253       bp2 = bptr;
02254       for (x = 0; x < width; x++)
02255         {
02256           r = *bp2++;
02257           g = *bp2++;
02258           b = *bp2++;
02259           ((unsigned short *)obuf)[x] = ((r & 0xf8) << 7) |
02260             ((g & 0xf8) << 2) |
02261             (b >> 3);
02262         }
02263       bptr += rowstride;
02264       obuf += bpl;
02265     }
02266 }
02267 
02268 static void
02269 xxlib_rgb_convert_555_br (XlibRgbHandle *handle, XImage *image,
02270                         int ax, int ay, int width, int height,
02271                         unsigned char *buf, int rowstride,
02272                         int x_align, int y_align, XlibRgbCmap *cmap)
02273 {
02274   int x, y;
02275   unsigned char *obuf;
02276   int bpl;
02277   unsigned char *bptr, *bp2;
02278   unsigned char r, g, b;
02279 
02280   bptr = buf;
02281   bpl = image->bytes_per_line;
02282   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 2;
02283   for (y = 0; y < height; y++)
02284     {
02285       bp2 = bptr;
02286       for (x = 0; x < width; x++)
02287         {
02288           r = *bp2++;
02289           g = *bp2++;
02290           b = *bp2++;
02291           /* final word is:
02292              g5 g4 g3 b7 b6 b5 b4 b3  0 r7 r6 r5 r4 r3 g7 g6
02293            */
02294           ((unsigned short *)obuf)[x] = ((r & 0xf8) >> 1) |
02295             ((g & 0xc0) >> 6) |
02296             ((g & 0x18) << 10) |
02297             ((b & 0xf8) << 5);
02298         }
02299       bptr += rowstride;
02300       obuf += bpl;
02301     }
02302 }
02303 
02304 static void
02305 xxlib_rgb_convert_888_msb (XlibRgbHandle *handle, XImage *image,
02306                          int ax, int ay, int width, int height,
02307                          unsigned char *buf, int rowstride,
02308                          int x_align, int y_align, XlibRgbCmap *cmap)
02309 {
02310   int y;
02311   unsigned char *obuf;
02312   int bpl;
02313   unsigned char *bptr;
02314 
02315   bptr = buf;
02316   bpl = image->bytes_per_line;
02317   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 3;
02318   for (y = 0; y < height; y++)
02319     {
02320       memcpy (obuf, bptr, (unsigned int)(width + width + width));
02321       bptr += rowstride;
02322       obuf += bpl;
02323     }
02324 }
02325 
02326 /* todo: optimize this */
02327 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
02328 #define HAIRY_CONVERT_888
02329 #endif
02330 
02331 #ifdef HAIRY_CONVERT_888
02332 static void
02333 xxlib_rgb_convert_888_lsb (XlibRgbHandle *handle, XImage *image,
02334                           int ax, int ay, int width, int height,
02335                           unsigned char *buf, int rowstride,
02336                           int x_align, int y_align, XlibRgbCmap *cmap)
02337 {
02338   int x, y;
02339   unsigned char *obuf, *obptr;
02340   int bpl;
02341   unsigned char *bptr, *bp2;
02342   int r, g, b;
02343 
02344   bptr = buf;
02345   bpl = image->bytes_per_line;
02346   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 3;
02347   for (y = 0; y < height; y++)
02348     {
02349       bp2 = bptr;
02350       obptr = obuf;
02351       if (((unsigned long)obuf | (unsigned long) bp2) & 3)
02352         {
02353           for (x = 0; x < width; x++)
02354             {
02355               r = bp2[0];
02356               g = bp2[1];
02357               b = bp2[2];
02358               *obptr++ = b;
02359               *obptr++ = g;
02360               *obptr++ = r;
02361               bp2 += 3;
02362             }
02363         }
02364       else
02365         {
02366           for (x = 0; x < width - 3; x += 4)
02367             {
02368               uint32 r1b0g0r0;
02369               uint32 g2r2b1g1;
02370               uint32 b3g3r3b2;
02371 
02372               r1b0g0r0 = ((uint32 *)bp2)[0];
02373               g2r2b1g1 = ((uint32 *)bp2)[1];
02374               b3g3r3b2 = ((uint32 *)bp2)[2];
02375               ((uint32 *)obptr)[0] =
02376                 (r1b0g0r0 & 0xff00) |
02377                 ((r1b0g0r0 & 0xff0000) >> 16) |
02378                 (((g2r2b1g1 & 0xff00) | (r1b0g0r0 & 0xff)) << 16);
02379               ((uint32 *)obptr)[1] =
02380                 (g2r2b1g1 & 0xff0000ff) |
02381                 ((r1b0g0r0 & 0xff000000) >> 16) |
02382                 ((b3g3r3b2 & 0xff) << 16);
02383               ((uint32 *)obptr)[2] =
02384                 (((g2r2b1g1 & 0xff0000) | (b3g3r3b2 & 0xff000000)) >> 16) |
02385                 ((b3g3r3b2 & 0xff00) << 16) |
02386                 ((b3g3r3b2 & 0xff0000));
02387               bp2 += 12;
02388               obptr += 12;
02389             }
02390           for (; x < width; x++)
02391             {
02392               r = bp2[0];
02393               g = bp2[1];
02394               b = bp2[2];
02395               *obptr++ = b;
02396               *obptr++ = g;
02397               *obptr++ = r;
02398               bp2 += 3;
02399             }
02400         }
02401       bptr += rowstride;
02402       obuf += bpl;
02403     }
02404 }
02405 #else
02406 static void
02407 xxlib_rgb_convert_888_lsb (XlibRgbHandle *handle, XImage *image,
02408                          int ax, int ay, int width, int height,
02409                          unsigned char *buf, int rowstride,
02410                          int x_align, int y_align, XlibRgbCmap *cmap)
02411 {
02412   int x, y;
02413   unsigned char *obuf;
02414   int bpl;
02415   unsigned char *bptr, *bp2;
02416   int r, g, b;
02417 
02418   bptr = buf;
02419   bpl = image->bytes_per_line;
02420   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 3;
02421   for (y = 0; y < height; y++)
02422     {
02423       bp2 = bptr;
02424       for (x = 0; x < width; x++)
02425         {
02426           r = bp2[0];
02427           g = bp2[1];
02428           b = bp2[2];
02429           obuf[x * 3] = b;
02430           obuf[x * 3 + 1] = g;
02431           obuf[x * 3 + 2] = r;
02432           bp2 += 3;
02433         }
02434       bptr += rowstride;
02435       obuf += bpl;
02436     }
02437 }
02438 #endif
02439 
02440 /* convert 24-bit packed to 32-bit unpacked */
02441 /* todo: optimize this */
02442 static void
02443 xxlib_rgb_convert_0888 (XlibRgbHandle *handle, XImage *image,
02444                       int ax, int ay, int width, int height,
02445                       unsigned char *buf, int rowstride,
02446                       int x_align, int y_align, XlibRgbCmap *cmap)
02447 {
02448   int x, y;
02449   unsigned char *obuf;
02450   int bpl;
02451   unsigned char *bptr, *bp2;
02452   int r, g, b;
02453 
02454   bptr = buf;
02455   bpl = image->bytes_per_line;
02456   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 4;
02457   for (y = 0; y < height; y++)
02458     {
02459       bp2 = bptr;
02460       for (x = 0; x < width; x++)
02461         {
02462           r = bp2[0];
02463           g = bp2[1];
02464           b = bp2[2];
02465           ((uint32 *)obuf)[x] = (r << 16) | (g << 8) | b;
02466           bp2 += 3;
02467         }
02468       bptr += rowstride;
02469       obuf += bpl;
02470     }
02471 }
02472 
02473 static void
02474 xxlib_rgb_convert_0888_br (XlibRgbHandle *handle, XImage *image,
02475                          int ax, int ay, int width, int height,
02476                          unsigned char *buf, int rowstride,
02477                          int x_align, int y_align, XlibRgbCmap *cmap)
02478 {
02479   int x, y;
02480   unsigned char *obuf;
02481   int bpl;
02482   unsigned char *bptr, *bp2;
02483   int r, g, b;
02484 
02485   bptr = buf;
02486   bpl = image->bytes_per_line;
02487   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 4;
02488   for (y = 0; y < height; y++)
02489     {
02490       bp2 = bptr;
02491       for (x = 0; x < width; x++)
02492         {
02493           r = bp2[0];
02494           g = bp2[1];
02495           b = bp2[2];
02496           ((uint32 *)obuf)[x] = (b << 24) | (g << 16) | (r << 8);
02497           bp2 += 3;
02498         }
02499       bptr += rowstride;
02500       obuf += bpl;
02501     }
02502 }
02503 
02504 static void
02505 xxlib_rgb_convert_8880_br (XlibRgbHandle *handle, XImage *image,
02506                          int ax, int ay, int width, int height,
02507                          unsigned char *buf, int rowstride,
02508                          int x_align, int y_align, XlibRgbCmap *cmap)
02509 {
02510   int x, y;
02511   unsigned char *obuf;
02512   int bpl;
02513   unsigned char *bptr, *bp2;
02514   int r, g, b;
02515 
02516   bptr = buf;
02517   bpl = image->bytes_per_line;
02518   obuf = ((unsigned char *)image->data) + ay * bpl + ax * 4;
02519   for (y = 0; y < height; y++)
02520     {
02521       bp2 = bptr;
02522       for (x = 0; x < width; x++)
02523         {
02524           r = bp2[0];
02525           g = bp2[1];
02526           b = bp2[2];
02527           ((uint32 *)obuf)[x] = (b << 16) | (g << 8) | r;
02528           bp2 += 3;
02529         }
02530       bptr += rowstride;
02531       obuf += bpl;
02532     }
02533 }
02534 
02535 /* Generic truecolor/directcolor conversion function. Slow, but these
02536    are oddball modes. */
02537 static void
02538 xxlib_rgb_convert_truecolor_lsb (XlibRgbHandle *handle, XImage *image,
02539                                int ax, int ay, int width, int height,
02540                                unsigned char *buf, int rowstride,
02541                                int x_align, int y_align,
02542                                XlibRgbCmap *cmap)
02543 {
02544   int x, y;
02545   unsigned char *obuf, *obptr;
02546   int bpl;
02547   unsigned char *bptr, *bp2;
02548   int r, g, b;
02549   int r_right, r_left;
02550   int g_right, g_left;
02551   int b_right, b_left;
02552   int bpp;
02553   uint32 pixel;
02554   int i;
02555 
02556   r_right = 8 - handle->red_prec;
02557   r_left = handle->red_shift;
02558   g_right = 8 - handle->green_prec;
02559   g_left = handle->green_shift;
02560   b_right = 8 - handle->blue_prec;
02561   b_left = handle->blue_shift;
02562   bpp = handle->bpp;
02563   bptr = buf;
02564   bpl = image->bytes_per_line;
02565   obuf = ((unsigned char *)image->data) + ay * bpl + ax * bpp;
02566   for (y = 0; y < height; y++)
02567     {
02568       obptr = obuf;
02569       bp2 = bptr;
02570       for (x = 0; x < width; x++)
02571         {
02572           r = bp2[0];
02573           g = bp2[1];
02574           b = bp2[2];
02575           pixel = ((r >> r_right) << r_left) |
02576             ((g >> g_right) << g_left) |
02577             ((b >> b_right) << b_left);
02578           for (i = 0; i < bpp; i++)
02579             {
02580               *obptr++ = pixel & 0xff;
02581               pixel >>= 8;
02582             }
02583           bp2 += 3;
02584         }
02585       bptr += rowstride;
02586       obuf += bpl;
02587     }
02588 }
02589 
02590 static void
02591 xxlib_rgb_convert_truecolor_lsb_d (XlibRgbHandle *handle, XImage *image,
02592                                  int ax, int ay, int width, int height,
02593                                  unsigned char *buf, int rowstride,
02594                                  int x_align, int y_align,
02595                                  XlibRgbCmap *cmap)
02596 {
02597   int x, y;
02598   unsigned char *obuf, *obptr;
02599   int bpl;
02600   unsigned char *bptr, *bp2;
02601   int r, g, b;
02602   int r_right, r_left, r_prec;
02603   int g_right, g_left, g_prec;
02604   int b_right, b_left, b_prec;
02605   int bpp;
02606   uint32 pixel;
02607   int i;
02608   int dith;
02609   int r1, g1, b1;
02610   const unsigned char *dmp;
02611 
02612   r_right = 8 - handle->red_prec;
02613   r_left = handle->red_shift;
02614   r_prec = handle->red_prec;
02615   g_right = 8 - handle->green_prec;
02616   g_left = handle->green_shift;
02617   g_prec = handle->green_prec;
02618   b_right = 8 - handle->blue_prec;
02619   b_left = handle->blue_shift;
02620   b_prec = handle->blue_prec;
02621   bpp = handle->bpp;
02622   bptr = buf;
02623   bpl = image->bytes_per_line;
02624   obuf = ((unsigned char *)image->data) + ay * bpl + ax * bpp;
02625   for (y = 0; y < height; y++)
02626     {
02627       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
02628       obptr = obuf;
02629       bp2 = bptr;
02630       for (x = 0; x < width; x++)
02631         {
02632           r = bp2[0];
02633           g = bp2[1];
02634           b = bp2[2];
02635           dith = dmp[(x_align + x) & (DM_WIDTH - 1)] << 2;
02636           r1 = r + (dith >> r_prec);
02637           g1 = g + ((252 - dith) >> g_prec);
02638           b1 = b + (dith >> b_prec);
02639           pixel = (((r1 - (r1 >> r_prec)) >> r_right) << r_left) |
02640             (((g1 - (g1 >> g_prec)) >> g_right) << g_left) |
02641             (((b1 - (b1 >> b_prec)) >> b_right) << b_left);
02642           for (i = 0; i < bpp; i++)
02643             {
02644               *obptr++ = pixel & 0xff;
02645               pixel >>= 8;
02646             }
02647           bp2 += 3;
02648         }
02649       bptr += rowstride;
02650       obuf += bpl;
02651     }
02652 }
02653 
02654 static void
02655 xxlib_rgb_convert_truecolor_msb (XlibRgbHandle *handle, XImage *image,
02656                                int ax, int ay, int width, int height,
02657                                unsigned char *buf, int rowstride,
02658                                int x_align, int y_align,
02659                                XlibRgbCmap *cmap)
02660 {
02661   int x, y;
02662   unsigned char *obuf, *obptr;
02663   int bpl;
02664   unsigned char *bptr, *bp2;
02665   int r, g, b;
02666   int r_right, r_left;
02667   int g_right, g_left;
02668   int b_right, b_left;
02669   int bpp;
02670   uint32 pixel;
02671   int shift, shift_init;
02672 
02673   r_right = 8 - handle->red_prec;
02674   r_left = handle->red_shift;
02675   g_right = 8 - handle->green_prec;
02676   g_left = handle->green_shift;
02677   b_right = 8 - handle->blue_prec;
02678   b_left = handle->blue_shift;
02679   bpp = handle->bpp;
02680   bptr = buf;
02681   bpl = image->bytes_per_line;
02682   obuf = ((unsigned char *)image->data) + ay * bpl + ax * bpp;
02683   shift_init = (bpp - 1) << 3;
02684   for (y = 0; y < height; y++)
02685     {
02686       obptr = obuf;
02687       bp2 = bptr;
02688       for (x = 0; x < width; x++)
02689         {
02690           r = bp2[0];
02691           g = bp2[1];
02692           b = bp2[2];
02693           pixel = ((r >> r_right) << r_left) |
02694             ((g >> g_right) << g_left) |
02695             ((b >> b_right) << b_left);
02696           for (shift = shift_init; shift >= 0; shift -= 8)
02697             {
02698               *obptr++ = (pixel >> shift) & 0xff;
02699             }
02700           bp2 += 3;
02701         }
02702       bptr += rowstride;
02703       obuf += bpl;
02704     }
02705 }
02706 
02707 static void
02708 xxlib_rgb_convert_truecolor_msb_d (XlibRgbHandle *handle, XImage *image,
02709                                  int ax, int ay, int width, int height,
02710                                  unsigned char *buf, int rowstride,
02711                                  int x_align, int y_align,
02712                                  XlibRgbCmap *cmap)
02713 {
02714   int x, y;
02715   unsigned char *obuf, *obptr;
02716   int bpl;
02717   unsigned char *bptr, *bp2;
02718   int r, g, b;
02719   int r_right, r_left, r_prec;
02720   int g_right, g_left, g_prec;
02721   int b_right, b_left, b_prec;
02722   int bpp;
02723   uint32 pixel;
02724   int shift, shift_init;
02725   int dith;
02726   int r1, g1, b1;
02727   const unsigned char *dmp;
02728 
02729   r_right = 8 - handle->red_prec;
02730   r_left = handle->red_shift;
02731   r_prec = handle->red_prec;
02732   g_right = 8 - handle->green_prec;
02733   g_left = handle->green_shift;
02734   g_prec = handle->green_prec;
02735   b_right = 8 - handle->blue_prec;
02736   b_left = handle->blue_shift;
02737   b_prec = handle->blue_prec;
02738   bpp = handle->bpp;
02739   bptr = buf;
02740   bpl = image->bytes_per_line;
02741   obuf = ((unsigned char *)image->data) + ay * bpl + ax * bpp;
02742   shift_init = (bpp - 1) << 3;
02743   for (y = 0; y < height; y++)
02744     {
02745       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
02746       obptr = obuf;
02747       bp2 = bptr;
02748       for (x = 0; x < width; x++)
02749         {
02750           r = bp2[0];
02751           g = bp2[1];
02752           b = bp2[2];
02753           dith = dmp[(x_align + x) & (DM_WIDTH - 1)] << 2;
02754           r1 = r + (dith >> r_prec);
02755           g1 = g + ((252 - dith) >> g_prec);
02756           b1 = b + (dith >> b_prec);
02757           pixel = (((r1 - (r1 >> r_prec)) >> r_right) << r_left) |
02758             (((g1 - (g1 >> g_prec)) >> g_right) << g_left) |
02759             (((b1 - (b1 >> b_prec)) >> b_right) << b_left);
02760           for (shift = shift_init; shift >= 0; shift -= 8)
02761             {
02762               *obptr++ = (pixel >> shift) & 0xff;
02763             }
02764           bp2 += 3;
02765         }
02766       bptr += rowstride;
02767       obuf += bpl;
02768     }
02769 }
02770 
02771 /* This actually works for depths from 3 to 7 */
02772 static void
02773 xxlib_rgb_convert_4 (XlibRgbHandle *handle, XImage *image,
02774                    int ax, int ay, int width, int height,
02775                    unsigned char *buf, int rowstride,
02776                    int x_align, int y_align,
02777                    XlibRgbCmap *cmap)
02778 {
02779   int x, y;
02780   int bpl;
02781   unsigned char *obuf, *obptr;
02782   unsigned char *bptr, *bp2;
02783   int r, g, b;
02784   const unsigned char *dmp;
02785   int dith;
02786 
02787   bptr = buf;
02788   bpl = image->bytes_per_line;
02789   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
02790   for (y = 0; y < height; y++)
02791     {
02792       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
02793       bp2 = bptr;
02794       obptr = obuf;
02795       for (x = 0; x < width; x += 1)
02796         {
02797           r = *bp2++;
02798           g = *bp2++;
02799           b = *bp2++;
02800           dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) | 3;
02801           obptr[0] = handle->colorcube_d[(((r + dith) & 0x100) >> 2) |
02802                                 (((g + 258 - dith) & 0x100) >> 5) |
02803                                 (((b + dith) & 0x100) >> 8)];
02804           obptr++;
02805         }
02806       bptr += rowstride;
02807       obuf += bpl;
02808     }
02809 }
02810 
02811 /* This actually works for depths from 3 to 7 */
02812 static void
02813 xxlib_rgb_convert_gray4 (XlibRgbHandle *handle, XImage *image,
02814                        int ax, int ay, int width, int height,
02815                        unsigned char *buf, int rowstride,
02816                        int x_align, int y_align, XlibRgbCmap *cmap)
02817 {
02818   int x, y;
02819   int bpl;
02820   unsigned char *obuf, *obptr;
02821   unsigned char *bptr, *bp2;
02822   int r, g, b;
02823   int shift;
02824 
02825   bptr = buf;
02826   bpl = image->bytes_per_line;
02827   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
02828   shift = 9 - handle->x_visual_info->depth;
02829   for (y = 0; y < height; y++)
02830     {
02831       bp2 = bptr;
02832       obptr = obuf;
02833       for (x = 0; x < width; x++)
02834         {
02835           r = *bp2++;
02836           g = *bp2++;
02837           b = *bp2++;
02838           obptr[0] = (g + ((b + r) >> 1)) >> shift;
02839           obptr++;
02840         }
02841       bptr += rowstride;
02842       obuf += bpl;
02843     }
02844 }
02845 
02846 static void
02847 xxlib_rgb_convert_gray4_pack (XlibRgbHandle *handle, XImage *image,
02848                             int ax, int ay, int width, int height,
02849                             unsigned char *buf, int rowstride,
02850                             int x_align, int y_align, XlibRgbCmap *cmap)
02851 {
02852   int x, y;
02853   int bpl;
02854   unsigned char *obuf, *obptr;
02855   unsigned char *bptr, *bp2;
02856   int r, g, b;
02857   int shift;
02858   unsigned char pix0, pix1;
02859   /* todo: this is hardcoded to big-endian. Make endian-agile. */
02860 
02861   bptr = buf;
02862   bpl = image->bytes_per_line;
02863   obuf = ((unsigned char *)image->data) + ay * bpl + (ax >> 1);
02864   shift = 9 - handle->x_visual_info->depth;
02865   for (y = 0; y < height; y++)
02866     {
02867       bp2 = bptr;
02868       obptr = obuf;
02869       for (x = 0; x < width; x += 2)
02870         {
02871           r = *bp2++;
02872           g = *bp2++;
02873           b = *bp2++;
02874           pix0 = (g + ((b + r) >> 1)) >> shift;
02875           r = *bp2++;
02876           g = *bp2++;
02877           b = *bp2++;
02878           pix1 = (g + ((b + r) >> 1)) >> shift;
02879           obptr[0] = (pix0 << 4) | pix1;
02880           obptr++;
02881         }
02882       if (width & 1)
02883         {
02884           r = *bp2++;
02885           g = *bp2++;
02886           b = *bp2++;
02887           pix0 = (g + ((b + r) >> 1)) >> shift;
02888           obptr[0] = (pix0 << 4);
02889         }
02890       bptr += rowstride;
02891       obuf += bpl;
02892     }
02893 }
02894 
02895 /* This actually works for depths from 3 to 7 */
02896 static void
02897 xxlib_rgb_convert_gray4_d (XlibRgbHandle *handle, XImage *image,
02898                        int ax, int ay, int width, int height,
02899                        unsigned char *buf, int rowstride,
02900                        int x_align, int y_align, XlibRgbCmap *cmap)
02901 {
02902   int x, y;
02903   int bpl;
02904   unsigned char *obuf, *obptr;
02905   unsigned char *bptr, *bp2;
02906   int r, g, b;
02907   const unsigned char *dmp;
02908   int prec, right;
02909   int gray;
02910 
02911   bptr = buf;
02912   bpl = image->bytes_per_line;
02913   obuf = ((unsigned char *)image->data) + ay * bpl + ax;
02914   prec = handle->x_visual_info->depth;
02915   right = 8 - prec;
02916   for (y = 0; y < height; y++)
02917     {
02918       bp2 = bptr;
02919       obptr = obuf;
02920       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
02921       for (x = 0; x < width; x++)
02922         {
02923           r = *bp2++;
02924           g = *bp2++;
02925           b = *bp2++;
02926           gray = (g + ((b + r) >> 1)) >> 1;
02927           gray += (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) >> prec;
02928           obptr[0] = (gray - (gray >> prec)) >> right;
02929           obptr++;
02930         }
02931       bptr += rowstride;
02932       obuf += bpl;
02933     }
02934 }
02935 
02936 static void
02937 xxlib_rgb_convert_gray4_d_pack (XlibRgbHandle *handle, XImage *image,
02938                               int ax, int ay, int width, int height,
02939                               unsigned char *buf, int rowstride,
02940                               int x_align, int y_align, XlibRgbCmap *cmap)
02941 {
02942   int x, y;
02943   int bpl;
02944   unsigned char *obuf, *obptr;
02945   unsigned char *bptr, *bp2;
02946   int r, g, b;
02947   const unsigned char *dmp;
02948   int prec, right;
02949   int gray;
02950   unsigned char pix0, pix1;
02951   /* todo: this is hardcoded to big-endian. Make endian-agile. */
02952 
02953   bptr = buf;
02954   bpl = image->bytes_per_line;
02955   obuf = ((unsigned char *)image->data) + ay * bpl + (ax >> 1);
02956   prec = handle->x_visual_info->depth;
02957   right = 8 - prec;
02958   for (y = 0; y < height; y++)
02959     {
02960       bp2 = bptr;
02961       obptr = obuf;
02962       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
02963       for (x = 0; x < width; x += 2)
02964         {
02965           r = *bp2++;
02966           g = *bp2++;
02967           b = *bp2++;
02968           gray = (g + ((b + r) >> 1)) >> 1;
02969           gray += (dmp[(x_align + x) & (DM_WIDTH - 1)] << 2) >> prec;
02970           pix0 = (gray - (gray >> prec)) >> right;
02971           r = *bp2++;
02972           g = *bp2++;
02973           b = *bp2++;
02974           gray = (g + ((b + r) >> 1)) >> 1;
02975           gray += (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) >> prec;
02976           pix1 = (gray - (gray >> prec)) >> right;
02977           obptr[0] = (pix0 << 4) | pix1;
02978           obptr++;
02979         }
02980       if (width & 1)
02981         {
02982           r = *bp2++;
02983           g = *bp2++;
02984           b = *bp2++;
02985           gray = (g + ((b + r) >> 1)) >> 1;
02986           gray += (dmp[(x_align + x + 1) & (DM_WIDTH - 1)] << 2) >> prec;
02987           pix0 = (gray - (gray >> prec)) >> right;
02988           obptr[0] = (pix0 << 4);
02989         }
02990       bptr += rowstride;
02991       obuf += bpl;
02992     }
02993 }
02994 
02995 static void
02996 xxlib_rgb_convert_1 (XlibRgbHandle *handle, XImage *image,
02997                    int ax, int ay, int width, int height,
02998                    unsigned char *buf, int rowstride,
02999                    int x_align, int y_align,
03000                    XlibRgbCmap *cmap)
03001 {
03002   int x, y;
03003   int bpl;
03004   unsigned char *obuf, *obptr;
03005   unsigned char *bptr, *bp2;
03006   int r, g, b;
03007   const unsigned char *dmp;
03008   int dith;
03009   unsigned char byte;
03010 
03011   bptr = buf;
03012   bpl = image->bytes_per_line;
03013   obuf = ((unsigned char *)image->data) + ay * bpl + (ax >> 3);
03014   byte = 0; /* unnecessary, but it keeps gcc from complaining */
03015   for (y = 0; y < height; y++)
03016     {
03017       dmp = DM[(y_align + y) & (DM_HEIGHT - 1)];
03018       bp2 = bptr;
03019       obptr = obuf;
03020       for (x = 0; x < width; x++)
03021         {
03022           r = *bp2++;
03023           g = *bp2++;
03024           b = *bp2++;
03025           dith = (dmp[(x_align + x) & (DM_WIDTH - 1)] << 4) | 4;
03026           byte += byte + (r + g + g + b + dith > 1020);
03027           if ((x & 7) == 7)
03028             {
03029               obptr[0] = byte;
03030               obptr++;
03031             }
03032         }
03033       if (x & 7)
03034         obptr[0] = byte << (8 - (x & 7));
03035       bptr += rowstride;
03036       obuf += bpl;
03037     }
03038 }
03039 
03040 /* Returns a pointer to the stage buffer. */
03041 static unsigned char *
03042 xxlib_rgb_ensure_stage (XlibRgbHandle *handle)
03043 {
03044   if (handle->stage_buf == NULL)
03045     handle->stage_buf = malloc (IMAGE_HEIGHT * STAGE_ROWSTRIDE);
03046   return handle->stage_buf;
03047 }
03048 
03049 /* This is slow. Speed me up, please. */
03050 static void
03051 xxlib_rgb_32_to_stage (XlibRgbHandle *handle, unsigned char *buf, int rowstride, int width, int height)
03052 {
03053   int x, y;
03054   unsigned char *pi_start, *po_start;
03055   unsigned char *pi, *po;
03056 
03057   pi_start = buf;
03058   po_start = xxlib_rgb_ensure_stage (handle);
03059   for (y = 0; y < height; y++)
03060     {
03061       pi = pi_start;
03062       po = po_start;
03063       for (x = 0; x < width; x++)
03064         {
03065           *po++ = *pi++;
03066           *po++ = *pi++;
03067           *po++ = *pi++;
03068           pi++;
03069         }
03070       pi_start += rowstride;
03071       po_start += STAGE_ROWSTRIDE;
03072     }
03073 }
03074 
03075 /* Generic 32bit RGB conversion function - convert to 24bit packed, then
03076    go from there. */
03077 static void
03078 xxlib_rgb_convert_32_generic (XlibRgbHandle *handle, XImage *image,
03079                             int ax, int ay, int width, int height,
03080                             unsigned char *buf, int rowstride,
03081                             int x_align, int y_align, XlibRgbCmap *cmap)
03082 {
03083   xxlib_rgb_32_to_stage (handle, buf, rowstride, width, height);
03084 
03085   (*handle->conv) (handle, image, ax, ay, width, height,
03086                        handle->stage_buf, STAGE_ROWSTRIDE,
03087                        x_align, y_align, cmap);
03088 }
03089 
03090 /* Generic 32bit RGB conversion function - convert to 24bit packed, then
03091    go from there. */
03092 static void
03093 xxlib_rgb_convert_32_generic_d (XlibRgbHandle *handle, XImage *image,
03094                               int ax, int ay, int width, int height,
03095                               unsigned char *buf, int rowstride,
03096                               int x_align, int y_align, XlibRgbCmap *cmap)
03097 {
03098   xxlib_rgb_32_to_stage (handle, buf, rowstride, width, height);
03099 
03100   (*handle->conv_d) (handle, image, ax, ay, width, height,
03101                          handle->stage_buf, STAGE_ROWSTRIDE,
03102                          x_align, y_align, cmap);
03103 }
03104 
03105 /* This is slow. Speed me up, please. */
03106 static void
03107 xxlib_rgb_gray_to_stage (XlibRgbHandle *handle, unsigned char *buf, int rowstride, int width, int height)
03108 {
03109   int x, y;
03110   unsigned char *pi_start, *po_start;
03111   unsigned char *pi, *po;
03112   unsigned char gray;
03113 
03114   pi_start = buf;
03115   po_start = xxlib_rgb_ensure_stage (handle);
03116   for (y = 0; y < height; y++)
03117     {
03118       pi = pi_start;
03119       po = po_start;
03120       for (x = 0; x < width; x++)
03121         {
03122           gray = *pi++;
03123           *po++ = gray;
03124           *po++ = gray;
03125           *po++ = gray;
03126         }
03127       pi_start += rowstride;
03128       po_start += STAGE_ROWSTRIDE;
03129     }
03130 }
03131 
03132 /* Generic gray conversion function - convert to 24bit packed, then go
03133    from there. */
03134 static void
03135 xxlib_rgb_convert_gray_generic (XlibRgbHandle *handle, XImage *image,
03136                               int ax, int ay, int width, int height,
03137                               unsigned char *buf, int rowstride,
03138                               int x_align, int y_align, XlibRgbCmap *cmap)
03139 {
03140   xxlib_rgb_gray_to_stage (handle, buf, rowstride, width, height);
03141 
03142   (*handle->conv) (handle, image, ax, ay, width, height,
03143                        handle->stage_buf, STAGE_ROWSTRIDE,
03144                        x_align, y_align, cmap);
03145 }
03146 
03147 static void
03148 xxlib_rgb_convert_gray_generic_d (XlibRgbHandle *handle, XImage *image,
03149                                 int ax, int ay, int width, int height,
03150                                 unsigned char *buf, int rowstride,
03151                                 int x_align, int y_align, XlibRgbCmap *cmap)
03152 {
03153   xxlib_rgb_gray_to_stage (handle, buf, rowstride, width, height);
03154 
03155   (*handle->conv_d) (handle, image, ax, ay, width, height,
03156                          handle->stage_buf, STAGE_ROWSTRIDE,
03157                          x_align, y_align, cmap);
03158 }
03159 
03160 /* Render grayscale using indexed method. */
03161 static void
03162 xxlib_rgb_convert_gray_cmap (XlibRgbHandle *handle, XImage *image,
03163                            int ax, int ay, int width, int height,
03164                            unsigned char *buf, int rowstride,
03165                            int x_align, int y_align, XlibRgbCmap *cmap)
03166 {
03167   (*handle->conv_indexed) (handle, image, ax, ay, width, height,
03168                                buf, rowstride,
03169                                x_align, y_align, handle->gray_cmap);
03170 }
03171 
03172 #if 0
03173 static void
03174 xxlib_rgb_convert_gray_cmap_d (XlibRgbHandle *handle, XImage *image,
03175                                 int ax, int ay, int width, int height,
03176                                 unsigned char *buf, int rowstride,
03177                                 int x_align, int y_align, XlibRgbCmap *cmap)
03178 {
03179   (*handle->conv_indexed_d) (handle, image, ax, ay, width, height,
03180                                  buf, rowstride,
03181                                  x_align, y_align, handle->gray_cmap);
03182 }
03183 #endif
03184 
03185 /* This is slow. Speed me up, please. */
03186 static void
03187 xxlib_rgb_indexed_to_stage (XlibRgbHandle *handle, unsigned char *buf, int rowstride, int width, int height,
03188                           XlibRgbCmap *cmap)
03189 {
03190   int x, y;
03191   unsigned char *pi_start, *po_start;
03192   unsigned char *pi, *po;
03193   int rgb;
03194 
03195   pi_start = buf;
03196   po_start = xxlib_rgb_ensure_stage (handle);
03197   for (y = 0; y < height; y++)
03198     {
03199       pi = pi_start;
03200       po = po_start;
03201       for (x = 0; x < width; x++)
03202         {
03203           rgb = cmap->colors[*pi++];
03204           *po++ = rgb >> 16;
03205           *po++ = (rgb >> 8) & 0xff;
03206           *po++ = rgb & 0xff;
03207         }
03208       pi_start += rowstride;
03209       po_start += STAGE_ROWSTRIDE;
03210     }
03211 }
03212 
03213 /* Generic gray conversion function - convert to 24bit packed, then go
03214    from there. */
03215 static void
03216 xxlib_rgb_convert_indexed_generic (XlibRgbHandle *handle, XImage *image,
03217                                  int ax, int ay, int width, int height,
03218                                  unsigned char *buf, int rowstride,
03219                                  int x_align, int y_align, XlibRgbCmap *cmap)
03220 {
03221   xxlib_rgb_indexed_to_stage (handle, buf, rowstride, width, height, cmap);
03222 
03223   (*handle->conv) (handle, image, ax, ay, width, height,
03224                        handle->stage_buf, STAGE_ROWSTRIDE,
03225                        x_align, y_align, cmap);
03226 }
03227 
03228 static void
03229 xxlib_rgb_convert_indexed_generic_d (XlibRgbHandle *handle, XImage *image,
03230                                    int ax, int ay, int width, int height,
03231                                    unsigned char *buf, int rowstride,
03232                                    int x_align, int y_align,
03233                                    XlibRgbCmap *cmap)
03234 {
03235   xxlib_rgb_indexed_to_stage (handle, buf, rowstride, width, height, cmap);
03236 
03237   (*handle->conv_d) (handle, image, ax, ay, width, height,
03238                          handle->stage_buf, STAGE_ROWSTRIDE,
03239                          x_align, y_align, cmap);
03240 }
03241 
03242 /* Select a conversion function based on the visual and a
03243    representative image. */
03244 static void
03245 xxlib_rgb_select_conv (XlibRgbHandle *handle, XImage *image, ByteOrder byte_order)
03246 {
03247   int depth, byterev;
03248   int vtype; /* visual type */
03249   int bpp; /* bits per pixel - from the visual */
03250   uint32 red_mask, green_mask, blue_mask;
03251   XlibRgbConvFunc conv, conv_d;
03252   XlibRgbConvFunc conv_32, conv_32_d;
03253   XlibRgbConvFunc conv_gray, conv_gray_d;
03254   XlibRgbConvFunc conv_indexed, conv_indexed_d;
03255   Bool mask_rgb, mask_bgr;
03256 
03257   depth = handle->x_visual_info->depth;
03258   bpp = image->bits_per_pixel;
03259   if (handle->xlib_rgb_verbose)
03260     printf ("Chose visual 0x%x, image bpp=%d, %s first\n",
03261             (int)handle->x_visual_info->visual->visualid,
03262             bpp, byte_order == LSB_FIRST ? "lsb" : "msb");
03263 
03264 #if G_BYTE_ORDER == G_BIG_ENDIAN
03265   byterev = (byte_order == LSB_FIRST);
03266 #else
03267   byterev = (byte_order == MSB_FIRST);
03268 #endif
03269 
03270   vtype = handle->x_visual_info->class;
03271   if (vtype == DirectColor)
03272     vtype = TrueColor;
03273 
03274   red_mask = handle->x_visual_info->red_mask;
03275   green_mask = handle->x_visual_info->green_mask;
03276   blue_mask = handle->x_visual_info->blue_mask;
03277 
03278   mask_rgb = red_mask == 0xff0000 && green_mask == 0xff00 && blue_mask == 0xff;
03279   mask_bgr = red_mask == 0xff && green_mask == 0xff00 && blue_mask == 0xff0000;
03280 
03281   conv   = NULL;
03282   conv_d = NULL;
03283 
03284   conv_32     = xxlib_rgb_convert_32_generic;
03285   conv_32_d   = xxlib_rgb_convert_32_generic_d;
03286 
03287   conv_gray   = xxlib_rgb_convert_gray_generic;
03288   conv_gray_d = xxlib_rgb_convert_gray_generic_d;
03289 
03290   conv_indexed   = xxlib_rgb_convert_indexed_generic;
03291   conv_indexed_d = xxlib_rgb_convert_indexed_generic_d;
03292 
03293   handle->dith_default = FALSE;
03294 
03295   if (handle->bitmap)
03296     conv = xxlib_rgb_convert_1;
03297   else if (bpp == 16 && depth == 16 && !byterev &&
03298       red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f)
03299     {
03300       conv = xxlib_rgb_convert_565;
03301       conv_d = xxlib_rgb_convert_565_d;
03302       conv_gray = xxlib_rgb_convert_565_gray;
03303       xxlib_rgb_preprocess_dm_565 (handle);
03304     }
03305   else if (bpp == 16 && depth == 16 &&
03306            vtype == TrueColor&& byterev &&
03307       red_mask == 0xf800 && green_mask == 0x7e0 && blue_mask == 0x1f)
03308     conv = xxlib_rgb_convert_565_br;
03309 
03310   else if (bpp == 16 && depth == 15 &&
03311            vtype == TrueColor && !byterev &&
03312       red_mask == 0x7c00 && green_mask == 0x3e0 && blue_mask == 0x1f)
03313     conv = xxlib_rgb_convert_555;
03314 
03315   else if (bpp == 16 && depth == 15 &&
03316            vtype == TrueColor && byterev &&
03317       red_mask == 0x7c00 && green_mask == 0x3e0 && blue_mask == 0x1f)
03318     conv = xxlib_rgb_convert_555_br;
03319 
03320   /* I'm not 100% sure about the 24bpp tests - but testing will show*/
03321   else if (bpp == 24 && depth == 24 && vtype == TrueColor &&
03322            ((mask_rgb && byte_order == LSB_FIRST) ||
03323             (mask_bgr && byte_order == MSB_FIRST)))
03324     conv = xxlib_rgb_convert_888_lsb;
03325   else if (bpp == 24 && depth == 24 && vtype == TrueColor &&
03326            ((mask_rgb && byte_order == MSB_FIRST) ||
03327             (mask_bgr && byte_order == LSB_FIRST)))
03328     conv = xxlib_rgb_convert_888_msb;
03329 #if G_BYTE_ORDER == G_BIG_ENDIAN
03330   else if (bpp == 32 && depth == 24 && vtype == TrueColor &&
03331            (mask_rgb && byte_order == LSB_FIRST))
03332     conv = xxlib_rgb_convert_0888_br;
03333   else if (bpp == 32 && depth == 24 && vtype == TrueColor &&
03334            (mask_rgb && byte_order == MSB_FIRST))
03335     conv = xxlib_rgb_convert_0888;
03336   else if (bpp == 32 && depth == 24 && vtype == TrueColor &&
03337            (mask_bgr && byte_order == MSB_FIRST))
03338     conv = xxlib_rgb_convert_8880_br;
03339 #else
03340   else if (bpp == 32 && depth == 24 && vtype == TrueColor &&
03341            (mask_rgb && byte_order == MSB_FIRST))
03342     conv = xxlib_rgb_convert_0888_br;
03343   else if (bpp == 32 && (depth == 32 || depth == 24) && vtype == TrueColor &&
03344            (mask_rgb && byte_order == LSB_FIRST))
03345     conv = xxlib_rgb_convert_0888;
03346   else if (bpp == 32 && depth == 24 && vtype == TrueColor &&
03347            (mask_bgr && byte_order == LSB_FIRST))
03348     conv = xxlib_rgb_convert_8880_br;
03349 #endif
03350 
03351   else if (vtype == TrueColor && byte_order == LSB_FIRST)
03352     {
03353       conv = xxlib_rgb_convert_truecolor_lsb;
03354       conv_d = xxlib_rgb_convert_truecolor_lsb_d;
03355     }
03356   else if (vtype == TrueColor && byte_order == MSB_FIRST)
03357     {
03358       conv = xxlib_rgb_convert_truecolor_msb;
03359       conv_d = xxlib_rgb_convert_truecolor_msb_d;
03360     }
03361   else if (bpp == 8 && depth == 8 && (vtype == PseudoColor || vtype == StaticColor
03362 #ifdef ENABLE_GRAYSCALE
03363                                       || vtype == GrayScale
03364 #endif
03365                                       ))
03366     {
03367       handle->dith_default = TRUE;
03368       conv = xxlib_rgb_convert_8;
03369       if (vtype != GrayScale)
03370         {
03371           if (handle->nred_shades == 6 &&
03372               handle->ngreen_shades == 6 &&
03373               handle->nblue_shades == 6)
03374             conv_d = xxlib_rgb_convert_8_d666;
03375           else
03376             conv_d = xxlib_rgb_convert_8_d;
03377         }
03378       conv_indexed = xxlib_rgb_convert_8_indexed;
03379       conv_gray = xxlib_rgb_convert_gray_cmap;
03380     }
03381   else if (bpp == 8 && depth == 8 && (vtype == StaticGray
03382 #ifdef not_ENABLE_GRAYSCALE
03383                                       || vtype == GrayScale
03384 #endif
03385                                       ))
03386     {
03387       conv = xxlib_rgb_convert_gray8;
03388       conv_gray = xxlib_rgb_convert_gray8_gray;
03389     }
03390   else if (bpp == 8 && depth < 8 && depth >= 2 &&
03391            (vtype == StaticGray
03392             || vtype == GrayScale))
03393     {
03394       conv = xxlib_rgb_convert_gray4;
03395       conv_d = xxlib_rgb_convert_gray4_d;
03396     }
03397   else if (bpp == 8 && depth < 8 && depth >= 3)
03398     {
03399       conv = xxlib_rgb_convert_4;
03400     }
03401   else if (bpp == 4 && depth <= 4 && depth >= 2 &&
03402            (vtype == StaticGray
03403             || vtype == GrayScale))
03404     {
03405       conv = xxlib_rgb_convert_gray4_pack;
03406       conv_d = xxlib_rgb_convert_gray4_d_pack;
03407     }
03408 
03409   if (conv_d == NULL)
03410     conv_d = conv;
03411 
03412   handle->conv = conv;
03413   handle->conv_d = conv_d;
03414 
03415   handle->conv_32 = conv_32;
03416   handle->conv_32_d = conv_32_d;
03417 
03418   handle->conv_gray = conv_gray;
03419   handle->conv_gray_d = conv_gray_d;
03420 
03421   handle->conv_indexed = conv_indexed;
03422   handle->conv_indexed_d = conv_indexed_d;
03423 }
03424 
03425 static int
03426 xxlib_rgb_alloc_scratch_image (XlibRgbHandle *handle)
03427 {
03428   if (handle->static_image_idx == N_IMAGES)
03429   {
03430 #ifdef XLIBRGB_ENABLE_MITSHM_SUPPORT
03431     if (handle->xlib_use_shm)
03432       XFlush(handle->display);
03433 #endif /* XLIBRGB_ENABLE_MITSHM_SUPPORT */
03434       
03435     handle->static_image_idx = 0;
03436     handle->horiz_y = IMAGE_HEIGHT;
03437     handle->vert_x  = IMAGE_WIDTH;
03438     handle->tile_x  = IMAGE_WIDTH;
03439     handle->tile_y1 = handle->tile_y2 = IMAGE_HEIGHT;
03440   }
03441   
03442   return handle->static_image_idx++;
03443 }
03444 
03445 static XImage *
03446 xxlib_rgb_alloc_scratch (XlibRgbHandle *handle, int width, int height, int *ax, int *ay)
03447 {
03448   XImage *image;
03449   int idx;
03450 
03451   if (width >= (IMAGE_WIDTH >> 1))
03452   {
03453     if (height >= (IMAGE_HEIGHT >> 1))
03454     {
03455       idx = xxlib_rgb_alloc_scratch_image (handle);
03456       *ax = 0;
03457       *ay = 0;
03458     }
03459     else
03460     {
03461       if (height + handle->horiz_y > IMAGE_HEIGHT)
03462       {
03463         handle->horiz_idx = xxlib_rgb_alloc_scratch_image (handle);
03464         handle->horiz_y = 0;
03465       }
03466       idx = handle->horiz_idx;
03467       *ax = 0;
03468       *ay = handle->horiz_y;
03469       handle->horiz_y += height;
03470     }
03471   }
03472   else
03473   {
03474     if (height >= (IMAGE_HEIGHT >> 1))
03475     {
03476       if (width + handle->vert_x > IMAGE_WIDTH)
03477       {
03478         handle->vert_idx = xxlib_rgb_alloc_scratch_image (handle);
03479         handle->vert_x = 0;
03480       }
03481       idx = handle->vert_idx;
03482       *ax = handle->vert_x;
03483       *ay = 0;
03484       /* using 3 and -4 would be slightly more efficient on 32-bit machines
03485          with > 1bpp displays */
03486       handle->vert_x += (width + 7) & -8;
03487     }
03488     else
03489     {
03490       if (width + handle->tile_x > IMAGE_WIDTH)
03491       {
03492         handle->tile_y1 = handle->tile_y2;
03493         handle->tile_x = 0;
03494       }
03495       if (height + handle->tile_y1 > IMAGE_HEIGHT)
03496       {
03497         handle->tile_idx = xxlib_rgb_alloc_scratch_image (handle);
03498         handle->tile_x = 0;
03499         handle->tile_y1 = 0;
03500         handle->tile_y2 = 0;
03501       }
03502       if (height + handle->tile_y1 > handle->tile_y2)
03503         handle->tile_y2 = height + handle->tile_y1;
03504       idx = handle->tile_idx;
03505       *ax = handle->tile_x;
03506       *ay = handle->tile_y1;
03507       handle->tile_x += (width + 7) & -8;
03508     }
03509   }
03510   image = handle->static_image[idx];
03511 
03512   return image;
03513 }
03514 
03515 
03516 static int 
03517 xxlib_get_bits_per_pixel (XlibRgbHandle *handle, int depth)
03518 {
03519   if (depth <= 4)
03520       return 4;
03521   if (depth <= 8)
03522       return 8;
03523   if (depth <= 16)
03524       return 16;
03525   return 32;
03526 }
03527 
03528 /* Use optimized code... */   
03529 #define XLIB_USE_FAST_BUT_UGLY_CODE 1
03530 
03531 /* size of static buffer to store images in non-tiling mode
03532  * (see xlib_disallow_image_tiling()). If this buffer is too small to store
03533  * the whole image into it a new buffer is allocated for that image 
03534  */
03535 #define XLIB_STATIC_IMAGE_BUFFER_SIZE (256 * 128 * (32/4))   
03536 
03537 static void
03538 xxlib_draw_rgb_image_core (XlibRgbHandle *handle, 
03539                            Drawable drawable,
03540                            GC gc,
03541                            int x,
03542                            int y,
03543                            int width,
03544                            int height,
03545                            unsigned char *buf,
03546                            int pixstride,
03547                            int rowstride,
03548                            XlibRgbConvFunc conv,
03549                            XlibRgbCmap *cmap,
03550                            int xdith,
03551                            int ydith)
03552 {
03553   Bool do_tile;
03554   
03555   if (handle->bitmap)
03556   {
03557     if (handle->own_gc == None)
03558     {
03559       XColor color;
03560 
03561       handle->own_gc = XCreateGC(handle->display,
03562                                  drawable,
03563                                  0, NULL);
03564       color.pixel = XWhitePixel(handle->display,
03565                                 handle->screen_num);
03566       XSetForeground(handle->display, handle->own_gc, color.pixel);
03567       color.pixel = XBlackPixel(handle->display,
03568                                 handle->screen_num);
03569       XSetBackground(handle->display, handle->own_gc, color.pixel);
03570     }
03571     gc = handle->own_gc;
03572   }
03573 
03574   /* Avoid image tiling for small images (1/32 of the maximum request size -
03575    * guranteed to be at least 16384 bytes). Tiling buffers are rare - and 
03576    * in the case of MIT-SHM require a XFlush() if all tile images have been used-up.
03577    */
03578   do_tile = ((handle->x_visual_info->depth+7)/8 * width * height) > (handle->max_request_size/32);
03579       
03580   if( (!handle->disallow_image_tiling) && do_tile )
03581   {
03582     int ay, ax;
03583     int xs0, ys0;
03584     int width1, height1;
03585     unsigned char *buf_ptr;
03586     XImage *image;
03587           
03588     for (ay = 0; ay < height; ay += IMAGE_HEIGHT)
03589     {
03590       height1 = MIN (height - ay, IMAGE_HEIGHT);
03591       for (ax = 0; ax < width; ax += IMAGE_WIDTH)
03592       {
03593         width1 = MIN (width - ax, IMAGE_WIDTH);
03594         buf_ptr = buf + ay * rowstride + ax * pixstride;
03595 
03596         image = xxlib_rgb_alloc_scratch (handle, width1, height1, &xs0, &ys0);
03597 
03598         conv (handle, image, xs0, ys0, width1, height1, buf_ptr, rowstride,
03599               x + ax + xdith, y + ay + ydith, cmap);
03600 
03601 #ifndef DONT_ACTUALLY_DRAW
03602         xxlib_image_put(handle, drawable, gc, image,
03603                        xs0, ys0, x + ax, y + ay,
03604                        (unsigned int)width1, (unsigned int)height1);
03605 #endif /* !DONT_ACTUALLY_DRAW */
03606       }
03607     }
03608   }
03609   else
03610 #ifdef XLIB_USE_FAST_BUT_UGLY_CODE
03611   /* This is a heavily optimized version of the implementation used when  
03612    * XLIB_USE_FAST_BUT_UGLY_CODE is not set. It tries to avoid malloc()/XCreateImage() 
03613    * by using a static buffer if possible - but uses malloc() when the image data are 
03614    * too large to fit info that buffer...
03615    */
03616   {    
03617     XImage ximage;
03618     int    format;
03619     int    depth;
03620     int    xpad;
03621     long   image_data_size;
03622     
03623     /* allocate static buffer if we do not have one yet... */      
03624     if( handle->static_buffer == NULL )
03625     {
03626       handle->static_buffer = malloc(XLIB_STATIC_IMAGE_BUFFER_SIZE);
03627       if(!handle->static_buffer)
03628         return; /* error - no memory */
03629     }
03630     
03631     /* fill the XImage structure... */
03632     memset(&ximage, 0, sizeof(ximage));
03633     
03634     if (handle->bitmap) 
03635     {
03636       format = XYBitmap;
03637       depth  = 1;
03638       xpad   = 8;
03639     }
03640     else 
03641     {
03642       format = ZPixmap;
03643       depth  = handle->x_visual_info->depth;
03644       xpad   = 32;
03645     }
03646 
03647     ximage.width            = width;
03648     ximage.height           = height;
03649     ximage.format           = format;
03650     ximage.byte_order       = XImageByteOrder(handle->display);
03651     ximage.bitmap_unit      = XBitmapUnit(handle->display);
03652     ximage.bitmap_bit_order = XBitmapBitOrder(handle->display);
03653     ximage.red_mask         = handle->x_visual_info->visual->red_mask;
03654     ximage.green_mask       = handle->x_visual_info->visual->green_mask;
03655     ximage.blue_mask        = handle->x_visual_info->visual->blue_mask;
03656     if (format == ZPixmap) 
03657       ximage.bits_per_pixel = xxlib_get_bits_per_pixel(handle, depth);
03658     ximage.xoffset          = 0;
03659     ximage.bitmap_pad       = xpad;
03660     ximage.depth            = depth;
03661     ximage.data             = NULL;
03662     ximage.bytes_per_line   = 0; /* let XInitImage() calculate that... */
03663     ximage.obdata           = NULL;
03664     ximage.bitmap_bit_order = MSBFirst;
03665     ximage.byte_order       = MSBFirst;
03666 
03667     if(!XInitImage(&ximage))
03668     {
03669 #ifdef DSIABLED_FOR_NOW_DUE_LINK_ISSUES
03670       NS_ERROR("xlib_draw_rgb_image_core: XInitImage() failure - should not happen...\n");
03671 #endif /* DSIABLED_FOR_NOW_DUE_LINK_ISSUES */    
03672       return; /* error - should not happen... */
03673     }
03674     
03675     image_data_size = height*ximage.bytes_per_line;     
03676 
03677     if (image_data_size > XLIB_STATIC_IMAGE_BUFFER_SIZE)
03678     {
03679 #ifdef VERBOSE
03680       printf("xlib_draw_rgb_image_core: Allocating extra large buffer(%ld > %ld)...\n",
03681              (long)image_data_size, (long)XLIB_STATIC_IMAGE_BUFFER_SIZE);
03682 #endif /* VERBOSE */
03683       ximage.data = malloc(image_data_size);
03684       if (!ximage.data)
03685         return; /* error - no memory */
03686     }
03687     else
03688     {
03689       ximage.data = handle->static_buffer; 
03690     }
03691 
03692     conv (handle, &ximage, 0, 0, width, height, buf, rowstride,
03693           0, 0, cmap);
03694 
03695 #ifndef DONT_ACTUALLY_DRAW
03696     XPutImage(handle->display, drawable, gc, &ximage,
03697     0, 0, x, y, (unsigned int)width, (unsigned int)height);
03698 #endif
03699 
03700     /* free any extra memory we may have allocated above */
03701     if (ximage.data != handle->static_buffer)
03702       free(ximage.data);
03703   }
03704 #else
03705   /* this is the clean&pretty version of the code above... */
03706   {
03707     XImage *image;
03708     
03709     if (handle->bitmap) 
03710     {
03711       image = XCreateImage(handle->display,
03712                            handle->x_visual_info->visual,
03713                            1,
03714                            XYBitmap,
03715                            0, 0, width, height,
03716                            8, 0);
03717     }
03718     else 
03719     {
03720       image = XCreateImage(handle->display,
03721                            handle->x_visual_info->visual,
03722                            (unsigned int)handle->x_visual_info->depth,
03723                            ZPixmap,
03724                            0, 0, width, height,
03725                            32, 0);
03726     }
03727 
03728     /* Use malloc() instead of g_malloc since X will free() this mem */
03729     image->data = malloc(height*image->bytes_per_line);
03730     image->bitmap_bit_order = MSBFirst;
03731     image->byte_order       = MSBFirst;      
03732 
03733     conv( handle, image, 0, 0, width, height, buf, rowstride,
03734           0, 0, cmap);
03735 
03736 #ifndef DONT_ACTUALLY_DRAW
03737     XPutImage(handle->display, drawable, gc, image,
03738     0, 0, x, y, (unsigned int)width, (unsigned int)height);
03739 #endif
03740 
03741     XDestroyImage(image);
03742   }
03743 #endif /* XLIB_USE_FAST_BUT_UGLY_CODE */  
03744 }
03745 
03746 
03747 void
03748 xxlib_draw_rgb_image (XlibRgbHandle *handle, Drawable drawable,
03749                      GC gc,
03750                      int x,
03751                      int y,
03752                      int width,
03753                      int height,
03754                      XlibRgbDither dith,
03755                      unsigned char *rgb_buf,
03756                      int rowstride)
03757 {
03758   if (dith == XLIB_RGB_DITHER_NONE || (dith == XLIB_RGB_DITHER_NORMAL &&
03759                                       !handle->dith_default))
03760     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03761                               rgb_buf, 3, rowstride, handle->conv, NULL,
03762                               0, 0);
03763   else
03764     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03765                               rgb_buf, 3, rowstride, handle->conv_d, NULL,
03766                               0, 0);
03767 }
03768 
03769 void
03770 xxlib_draw_rgb_image_dithalign (XlibRgbHandle *handle, Drawable drawable,
03771                               GC gc,
03772                               int x,
03773                               int y,
03774                               int width,
03775                               int height,
03776                               XlibRgbDither dith,
03777                               unsigned char *rgb_buf,
03778                               int rowstride,
03779                               int xdith,
03780                               int ydith)
03781 {
03782   if (dith == XLIB_RGB_DITHER_NONE || (dith == XLIB_RGB_DITHER_NORMAL &&
03783                                        !handle->dith_default))
03784     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03785                               rgb_buf, 3, rowstride, handle->conv, NULL,
03786                               xdith, ydith);
03787   else
03788     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03789                                rgb_buf, 3, rowstride, handle->conv_d, NULL,
03790                                xdith, ydith);
03791 }
03792 
03793 void
03794 xxlib_draw_rgb_32_image (XlibRgbHandle *handle, Drawable drawable,
03795                         GC gc,
03796                         int x,
03797                         int y,
03798                         int width,
03799                         int height,
03800                         XlibRgbDither dith,
03801                         unsigned char *buf,
03802                         int rowstride)
03803 {
03804   if (dith == XLIB_RGB_DITHER_NONE || (dith == XLIB_RGB_DITHER_NORMAL &&
03805                                        !handle->dith_default))
03806     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03807                                buf, 4, rowstride,
03808                                handle->conv_32, NULL, 0, 0);
03809   else
03810     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03811                                buf, 4, rowstride,
03812                                handle->conv_32_d, NULL, 0, 0);
03813 }
03814 
03815 static void
03816 xxlib_rgb_make_gray_cmap (XlibRgbHandle *handle)
03817 {
03818   uint32 rgb[256];
03819   int i;
03820 
03821   for (i = 0; i < 256; i++)
03822     rgb[i] = (i << 16)  | (i << 8) | i;
03823   handle->gray_cmap = xxlib_rgb_cmap_new (handle, rgb, 256);
03824 }
03825 
03826 void
03827 xxlib_draw_gray_image (XlibRgbHandle *handle, Drawable drawable,
03828                       GC gc,
03829                       int x,
03830                       int y,
03831                       int width,
03832                       int height,
03833                       XlibRgbDither dith,
03834                       unsigned char *buf,
03835                       int rowstride)
03836 {
03837   if (handle->bpp == 1 &&
03838       handle->gray_cmap == NULL &&
03839       (handle->x_visual_info->class == PseudoColor ||
03840        handle->x_visual_info->class == GrayScale))
03841     xxlib_rgb_make_gray_cmap (handle);
03842   
03843   if (dith == XLIB_RGB_DITHER_NONE || (dith == XLIB_RGB_DITHER_NORMAL &&
03844                                       !handle->dith_default))
03845     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03846                               buf, 1, rowstride,
03847                               handle->conv_gray, NULL, 0, 0);
03848   else
03849     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03850                               buf, 1, rowstride,
03851                               handle->conv_gray_d, NULL, 0, 0);
03852 }
03853 
03854 XlibRgbCmap *
03855 xxlib_rgb_cmap_new (XlibRgbHandle *handle, uint32 *colors, int n_colors)
03856 {
03857   XlibRgbCmap *cmap;
03858   int i, j;
03859   uint32 rgb;
03860 
03861   if (n_colors < 0)
03862     return NULL;
03863   if (n_colors > 256)
03864     return NULL;
03865   cmap = malloc(sizeof(XlibRgbCmap));
03866   memcpy (cmap->colors, colors, n_colors * sizeof(uint32));
03867   if (handle->bpp == 1 &&
03868       (handle->x_visual_info->class == PseudoColor ||
03869        handle->x_visual_info->class == GrayScale))
03870     for (i = 0; i < n_colors; i++)
03871       {
03872         rgb = colors[i];
03873         j = ((rgb & 0xf00000) >> 12) |
03874                    ((rgb & 0xf000) >> 8) |
03875                    ((rgb & 0xf0) >> 4);
03876 #ifdef VERBOSE
03877         printf ("%d %x %x %d\n", i, j, handle->colorcube[j]);
03878 #endif
03879         cmap->lut[i] = handle->colorcube[j];
03880       }
03881   return cmap;
03882 }
03883 
03884 void
03885 xxlib_rgb_cmap_free (XlibRgbHandle *handle, XlibRgbCmap *cmap)
03886 {
03887   free (cmap);
03888 }
03889 
03890 void
03891 xxlib_draw_indexed_image (XlibRgbHandle *handle, Drawable drawable,
03892                         GC gc,
03893                         int x,
03894                         int y,
03895                         int width,
03896                         int height,
03897                         XlibRgbDither dith,
03898                         unsigned char *buf,
03899                         int rowstride,
03900                         XlibRgbCmap *cmap)
03901 {
03902   if (dith == XLIB_RGB_DITHER_NONE || (dith == XLIB_RGB_DITHER_NORMAL &&
03903                                        !handle->dith_default))
03904     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03905                                buf, 1, rowstride,
03906                                handle->conv_indexed, cmap, 0, 0);
03907   else
03908     xxlib_draw_rgb_image_core (handle, drawable, gc, x, y, width, height,
03909                                buf, 1, rowstride,
03910                                handle->conv_indexed_d, cmap, 0, 0);
03911 }
03912 
03913 void
03914 xxlib_draw_xprint_scaled_rgb_image( XlibRgbHandle *handle,
03915                                     Drawable drawable,
03916                                     long paper_resolution,
03917                                     long image_resolution,
03918                                     GC gc,
03919                                     int x,
03920                                     int y,
03921                                     int width,
03922                                     int height,
03923                                     XlibRgbDither dith,
03924                                     unsigned char *rgb_buf,
03925                                     int rowstride)
03926 {
03927   long available = ((65536 < handle->max_request_size) ? 
03928                     (65536 << 1):(handle->max_request_size << 1))
03929                  - 512 /* SIZEOF(xPutImageReq) */;
03930                 
03931   if (image_resolution == 0)
03932     image_resolution = paper_resolution;
03933 
03934   if( (rowstride * height) < available )
03935   {
03936     xxlib_draw_rgb_image(handle,
03937                          drawable,
03938                          gc,
03939                          x, y, width, height,
03940                          dith,
03941                          rgb_buf, rowstride);
03942   }
03943   else
03944   {
03945     int    subimageheight = available / rowstride;
03946     double scaling_factor = (double)paper_resolution / (double)image_resolution;
03947 
03948 #ifdef DEBUG    
03949     printf("xxlib_draw_xprint_scaled_rgb_image: "
03950            "splitting image x=%d, y=%d, width=%d, height=%d, subimageheight=%d, scaling_factor=%g, max_request_size=%ld, available=%ld\n",
03951            x, y, width, height, subimageheight, scaling_factor, (long)handle->max_request_size, (long)available);
03952 #endif /* DEBUG */
03953 
03954     if (subimageheight == 0)
03955         subimageheight = 1;
03956 
03957       xxlib_draw_xprint_scaled_rgb_image(handle,
03958                                          drawable,
03959                                          paper_resolution, image_resolution,
03960                                          gc,
03961                                          x, y,
03962                                          width, subimageheight,
03963                                          dith,
03964                                          rgb_buf,
03965                                          rowstride);
03966 
03967       xxlib_draw_xprint_scaled_rgb_image(handle,
03968                                          drawable,
03969                                          paper_resolution, image_resolution,
03970                                          gc,
03971                                          x, y+(int)((double)subimageheight*scaling_factor), 
03972                                          width, (height-subimageheight),
03973                                          dith,
03974                                          (rgb_buf+(subimageheight*rowstride)),
03975                                          rowstride);
03976   }
03977 }
03978 
03979 Bool
03980 xxlib_rgb_ditherable (XlibRgbHandle *handle)
03981 {
03982   return (handle->conv != handle->conv_d);
03983 }
03984 
03985 Colormap
03986 xxlib_rgb_get_cmap (XlibRgbHandle *handle)
03987 {
03988   return (handle)?(handle->cmap):(None);
03989 }
03990 
03991 Visual *
03992 xxlib_rgb_get_visual (XlibRgbHandle *handle)
03993 {
03994   /* xxlib_rgb_init (); */
03995   return (handle)?(handle->x_visual_info->visual):(NULL);
03996 }
03997 
03998 XVisualInfo *
03999 xxlib_rgb_get_visual_info (XlibRgbHandle *handle)
04000 {
04001   /* xxlib_rgb_init (); */
04002   return (handle)?(handle->x_visual_info):(NULL);
04003 }
04004 
04005 int
04006 xxlib_rgb_get_depth (XlibRgbHandle *handle)
04007 {
04008   XVisualInfo *v = xxlib_rgb_get_visual_info(handle);
04009 
04010   return (v)?(v->depth):(0);
04011 }
04012 
04013 Display *
04014 xxlib_rgb_get_display (XlibRgbHandle *handle)
04015 { 
04016   return (handle)?(handle->display):(NULL);
04017 }
04018 
04019 Screen *
04020 xxlib_rgb_get_screen (XlibRgbHandle *handle)
04021 {
04022   return (handle)?(handle->screen):(NULL);
04023 }
04024 
04025 typedef struct _RegisteredHandle
04026 {
04027   const char    *name;
04028   XlibRgbHandle *handle;
04029 } RegisteredHandle;
04030 
04031 static RegisteredHandle *registered_handles      = NULL;
04032 static unsigned int      registered_handles_size = 0;
04033 
04034 /* find registered handle by name */
04035 static
04036 RegisteredHandle *xxlib_find_registered_handle(const char *name)
04037 {
04038   unsigned int i;
04039   
04040   for( i=0 ; i < registered_handles_size ; i++ )
04041   {
04042     if (name && registered_handles[i].name && 
04043         !strcmp(name, registered_handles[i].name))
04044       return &registered_handles[i];
04045   }
04046   
04047   return NULL;
04048 }
04049 
04050 /* register handle.
04051  * This API is public to register a handle more then once (=alias), or if the handle 
04052  * has not been registered yet (e.g. |xxlib_rgb_create_handle()| called with
04053  * a NULL-|name| argument).
04054  */
04055 Bool xxlib_register_handle(const char *name, XlibRgbHandle *handle)
04056 {
04057   RegisteredHandle *entry = NULL;
04058   unsigned int i;
04059   
04060   if (xxlib_find_handle(name) || (handle == NULL))
04061     return False;
04062   
04063   /* search for a free slot... */  
04064   for( i=0 ; i < registered_handles_size ; i++ )
04065   {
04066     if (!registered_handles[i].handle)
04067     {
04068       entry = &registered_handles[i];
04069       break;
04070     }
04071   }
04072   
04073   /* ... no unused entry found ? Then allocate a new one... */
04074   if (!entry)
04075   {
04076     registered_handles_size++;
04077     registered_handles = realloc(registered_handles, sizeof(RegisteredHandle)*registered_handles_size);
04078   
04079     if (!registered_handles)
04080       abort();
04081     entry = &registered_handles[registered_handles_size-1];
04082   }
04083 
04084   entry->name   = strdup(name);
04085   entry->handle = handle;
04086   
04087   return True;
04088 }
04089 
04090 /* unregister the given handle by name. 
04091  * Return |True| if the |name| was found, |False| otherwise.
04092  * Note that |xxlib_rgb_destroy_handle()| unregisters the handle 
04093  * automagically (including all aliases).
04094  */
04095 Bool xxlib_deregister_handle(const char *name)
04096 {
04097   RegisteredHandle *entry;
04098   if ((entry = xxlib_find_registered_handle(name)) != NULL)
04099   {
04100     free((void *)entry->name);
04101     entry->name   = NULL;
04102     entry->handle = NULL;
04103     return True;
04104   }
04105   
04106   return False;  
04107 }
04108 
04109 /* used by |xxlib_rgb_destroy_handle()| to deregister handle and all
04110  * it's aliases... */
04111 static
04112 void xxlib_deregister_handle_by_handle(XlibRgbHandle *handle)
04113 {
04114   unsigned int i;
04115   
04116   if (!handle)
04117     return;
04118   
04119   for( i=0 ; i < registered_handles_size ; i++ )
04120   {
04121     if (registered_handles[i].handle == handle)
04122     {
04123       free((void *)registered_handles[i].name);
04124       registered_handles[i].name   = NULL;
04125       registered_handles[i].handle = NULL;    
04126     }
04127   }
04128 }
04129 
04130 /* find a registered handle by |name| */
04131 XlibRgbHandle *xxlib_find_handle(const char *name)
04132 {
04133   RegisteredHandle *entry = xxlib_find_registered_handle(name);
04134   return (entry)?(entry->handle):(NULL);
04135 }
04136 
04137