home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / colors.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  63.9 KB  |  2,009 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.    colors.c --- X front-end stuff dealing with color allocation/sharing
  20.  */
  21.  
  22.  
  23. #include "mozilla.h"
  24. #include "xfe.h"
  25.  
  26. #include "libimg.h"             /* Image Library public API. */
  27. #include "il_util.h"            /* Colormap/Colorspace API. */
  28.  
  29. #include "xp_qsort.h"
  30.  
  31. /* for XP_GetString() */
  32. #include <xpgetstr.h>
  33. extern int XFE_VISUAL_GRAY_DIRECTCOLOR;
  34. extern int XFE_VISUAL_GRAY;
  35. extern int XFE_VISUAL_DIRECTCOLOR;
  36. extern int XFE_VISUAL_NORMAL;
  37. extern int  XFE_WILL_BE_DISPLAYED_IN_MONOCHROME;
  38. extern int  XFE_WILL_LOOK_BAD;
  39.  
  40. /* Convert from an 8-bit imagelib color value to 16-bit X color.
  41.  
  42.    There is a weirdness here to account for some peculiar servers that
  43.    label their colors incorrectly, e.g. AIX and at least one Linux
  44.    server.  The X spec says that the 16-bit color components range
  45.    from #0000 to #FFFF and that servers are supposed to scale
  46.    intermediate values to this range.  These weird servers, however,
  47.    which I will refer to as "broken servers" for short, label their
  48.    maximum color as #FF00 and lower intensity colors as #FE00, #FD00,
  49.    etc.  This means, for example, that if we ask for color #FDFD, some
  50.    servers will allocate #FDFD as the nearest color that the server
  51.    hardware can realize, but the broken servers will allocate #FE00 as
  52.    the closest color.
  53.    
  54.    To handle this case, we interrogate the server during
  55.    initialization to find out how it numbers its colors and construct
  56.    a mask that indicates whether or not to duplicate the 8-bit color
  57.    in both the upper and lower byte of the 16-bit X color.
  58.  */
  59. #define C8to16(c)           ((((c) << 8) | (c)) & fe_colormask)
  60.  
  61. /* Convert from 16-bit X color value to 8-bit LO color */
  62. #define C16to8(c)           ((uint16)(c) >> 8)
  63.  
  64. #define STATIC_VISUAL_P(v)  (((v) != PseudoColor)  && ((v) != GrayScale))
  65. #define GRAYSCALE_WORKS
  66.  
  67. /* This encapsulation of the server's hardware colormap keeps a
  68.    locally cached copy of the colormap data.  This cache is not kept
  69.    perfectly in sync with the server (unless this is a private
  70.    colormap) because other clients may allocate colors.  However, when
  71.    a cached copy of the server colormap is requested, the use of a
  72.    timer update ensures that it is never more than two seconds out of
  73.    date.  (Of course, cells that *we've* allocated are kept up-to-date
  74.    in the cache).  We also keep an allocation map indicating how the
  75.    individual cells in the server's colormap have been allocated
  76.    (read-write/read-only) and when they are to be freed.  */
  77. struct fe_colormap
  78. {
  79.   Display *dpy;
  80.   Visual *visual;
  81.   XVisualInfo *visual_info;
  82.   int num_cells;                /* Number of cells in the visual */
  83.   Colormap cmap;                /* Handle for X Server colormap */
  84.   XColor *cache;                /* Local cache of server colormap */
  85.   time_t cache_update_time;     /* Last time cache was updated */
  86.   uint8 *allocation;            /* What the color cells are being used for */
  87.   uint16 *transient_ref_count;  /* Reference count: # of CELL_TRANSIENT
  88.                                    color cache entries using this cell */
  89.   Pixel *mapping;               /* Image to server pixel map */
  90.   int mapping_size;             /* Allowed input range of mapping */
  91.   Boolean private_p;            /* We don't share cmap with other X clients */
  92.   int contexts;                 /* Reference count for MWContexts */
  93.   int persistent_colors;        /* CELL_PERMANENT or (CELL_PERMANENT|CELL_IMAGE) */
  94.   Boolean writeable_cells_allocated; /* Possible only if private_p is set */
  95.   IL_ColorSpace *color_space;   /* Image library color conversion state */
  96. };
  97.  
  98. /* Types of color allocations (flags are exclusive) */
  99. #define  CELL_AVAIL      0x01  /* We don't use cell (other clients might) */
  100. #define  CELL_MARKED     0x02  /* Temporary value */
  101. #define  CELL_SHARED     0x04  /* Read-only cell that we share */
  102. #define  CELL_PRIVATE    0x08  /* Read/write cell that we own */
  103.  
  104. #define  CELL_ALLOCATION  (CELL_AVAIL|CELL_MARKED|CELL_SHARED|CELL_PRIVATE)
  105.  
  106. /* Color lifetime flags (these flags are *not* exclusive):
  107.  
  108.    Permanent colors remain allocated for the duration of the
  109.    application.  Image colors are also allocated for the life of the
  110.    application, but they use writeable cells that are mutated when a
  111.    new set of images is displayed.  Transient colors are used for
  112.    text, backgrounds, etc. and are deallocated when the next page
  113.    begins loading.  */
  114. #define  CELL_PERMANENT  0x40  /* widget/icon colors */
  115. #define  CELL_TRANSIENT  0x20  /* Per-page colors, e.g. text colors */
  116. #define  CELL_IMAGE      0x10  /* May last longer than display of page */
  117.  
  118. #define  CELL_LIFETIME   (CELL_PERMANENT | CELL_TRANSIENT | CELL_IMAGE)
  119.  
  120. static int fe_colormask = 0;
  121.  
  122. static int fe_clear_colormap (fe_colormap *colormap, Boolean grab_p);
  123.   
  124. #ifdef DEBUG
  125. void
  126. fe_print_colormap_allocation(fe_colormap *colormap)
  127. {
  128.   int i;
  129.   char c;
  130.  
  131.   uint8 *allocation = colormap->allocation;
  132.   char buffer[300];
  133.  
  134.   char *b = buffer;
  135.  
  136.   /*
  137.     Map printing key:
  138.  
  139.     For shared, read-only cells:
  140.  
  141.       char  TRANSIENT     PERMANENT     IMAGE   
  142.       ---------------------------------------
  143.       '.'      0              0           0
  144.       'i'      0              0           1
  145.       'p'      0              1           0
  146.       'I'      0              1           1
  147.       't'      1              0           0
  148.       '?'      1              0           1
  149.       'T'      1              1           0
  150.       'A'      1              1           1
  151.  
  152.     Writeable cells are marked with '*' and must be image cells.
  153.     Unallocated cells are marked '.'
  154.  
  155.   */
  156.  
  157.   for (i = 0; i < colormap->num_cells; i++)
  158.     {
  159.       if (allocation[i] & CELL_PRIVATE)
  160.         {
  161.           assert (allocation[i] == (CELL_PRIVATE|CELL_IMAGE));
  162.           c = '*';
  163.         }
  164.       else if (allocation[i] & CELL_TRANSIENT)
  165.         if (allocation[i] & CELL_PERMANENT)
  166.           if (allocation[i] & CELL_IMAGE)
  167.             c = 'A';
  168.           else
  169.             c = 'T';
  170.         else
  171.           c = 't';
  172.       else if (allocation[i] & CELL_PERMANENT)
  173.         if (allocation [i] & CELL_IMAGE)
  174.           c = 'I';
  175.         else
  176.           c = 'p';
  177.       else if (allocation[i] & CELL_IMAGE)
  178.         c = 'i';
  179.       else if (allocation[i] == CELL_AVAIL)
  180.         c = '.';
  181.       else
  182.         c = '?';
  183.  
  184.       *b++ = c;
  185.         
  186.       if ((i & 15) == 15)
  187.         *b++ = '\n';
  188.     }
  189.   
  190.   *b++ = 0;
  191.  
  192.   printf("%s", buffer);
  193. }
  194. #endif /* DEBUG */
  195.  
  196. static void
  197. fe_delete_colormap(fe_colormap *colormap)
  198. {
  199.   if (colormap->visual_info)
  200.     XFree ((char *) colormap->visual_info);
  201.   if (colormap->cache)
  202.     free(colormap->cache);
  203.   if (colormap->allocation)
  204.     free(colormap->allocation);
  205.   if (colormap->transient_ref_count)
  206.     free(colormap->transient_ref_count);
  207.   if (colormap->mapping)
  208.     free(colormap->mapping);
  209.   free(colormap);
  210. }
  211.  
  212. fe_colormap *
  213. fe_NewColormap(Screen *screen, Visual *visual,
  214.                Colormap cmap, Boolean private_p)
  215. {
  216.   uint8 *allocation;
  217.   uint16 *transient_ref_count;
  218.   int i, num_cells;
  219.   int out_count;
  220.   XColor *cache;
  221.   XVisualInfo vi_in, *vi_out;
  222.   Display *dpy = DisplayOfScreen(screen);
  223.  
  224.   fe_colormap *colormap = calloc(sizeof(fe_colormap), 1);
  225.   if (!colormap)
  226.     return NULL;
  227.  
  228.   /* See if this server has screwed up color matching.
  229.      (See the comment near the C8to16() macro.) */
  230.   if (!fe_colormask)
  231.     {
  232.       XColor white_color;
  233.       white_color.red = white_color.green = white_color.blue = 0xffff;
  234.       if (XAllocColor(dpy, cmap, &white_color))
  235.         fe_colormask = white_color.red; /* either 0xffff or 0xff00 */
  236.  
  237.       /* The XLookupColor should alway succeed, but be paranoid. */
  238.       if ((fe_colormask != 0xff00) && (fe_colormask != 0xffff))
  239.         fe_colormask = 0xffff;
  240.     }
  241.       
  242.   colormap->dpy = dpy;
  243.   colormap->visual = visual;
  244.  
  245.   /* Isn't it swell that we have to go through this stuff just to
  246.      do `visual->class' portably?? */
  247.   vi_in.screen = fe_ScreenNumber (screen);
  248.   vi_in.visualid = XVisualIDFromVisual (visual);
  249.   vi_out = XGetVisualInfo (dpy, VisualScreenMask|VisualIDMask,
  250.                &vi_in, &out_count);
  251.   
  252.   colormap->visual_info = vi_out;
  253.  
  254.   colormap->cmap = cmap;
  255.   if (vi_out->class == TrueColor)
  256.       return colormap;
  257.   
  258.   colormap->num_cells = num_cells = fe_VisualCells (dpy, visual);
  259.   if (vi_out->class == PseudoColor)
  260.     colormap->private_p = private_p;
  261.   
  262.   allocation = calloc(sizeof(uint8), num_cells);
  263.   colormap->allocation = allocation;
  264.   if (!allocation)
  265.     {
  266.       fe_delete_colormap(colormap);
  267.       return NULL;
  268.     }
  269.   for (i = 0; i < num_cells; i++)
  270.     allocation[i] = CELL_AVAIL;
  271.  
  272.   transient_ref_count = (uint16*)calloc(sizeof(uint16), num_cells);
  273.   colormap->transient_ref_count = transient_ref_count;
  274.   if (!transient_ref_count)
  275.     {
  276.       fe_delete_colormap(colormap);
  277.       return NULL;
  278.     }
  279.   
  280.   /* The colormap cache itself gets allocated when it is requested. */
  281.   colormap->cache_update_time = 0;
  282.       
  283.   cache = (XColor *) calloc (sizeof (XColor), num_cells);
  284.   if (!cache)
  285.     {
  286.       fe_delete_colormap(colormap);
  287.       return NULL;
  288.     }
  289.   colormap->cache = cache;
  290.   for (i = 0; i < num_cells; i++)
  291.     cache[i].pixel = i;
  292.  
  293.   /* Image colors are treated the same as permanent icon/widget colors
  294.      for shared colormaps (once set, they are never changed).  For
  295.      private colormaps, they may be allocated as writeable cells and can
  296.      change color on-the-fly. */
  297.   if (private_p)
  298.     colormap->persistent_colors = CELL_PERMANENT;
  299.   else
  300.     colormap->persistent_colors = (CELL_IMAGE | CELL_PERMANENT);
  301.  
  302.   colormap->mapping = (Pixel *) calloc (256, sizeof (Pixel));
  303.   /* Identity mapping from image to server colormap indices */
  304.   /* XXX - this is so uncool. Why not get rid of mapping altogether ? */
  305.   for (i = 0; i < 256; i++)
  306.       colormap->mapping[i] = i;
  307.  
  308. #if 0                           /* For debug */
  309.   fe_clear_colormap(colormap, False);
  310. #endif
  311.  
  312.   return colormap;
  313. }
  314.  
  315. Colormap
  316. XFE_GetDefaultColormap()
  317. {
  318.     /* Used by AWT */
  319.     return fe_cmap(fe_all_MWContexts->context);
  320. }
  321.  
  322. int
  323. XFE_GetMaxColors()
  324. {
  325.     /* Used by AWT */
  326.     if (fe_globalData.max_image_colors > 0)
  327.         return fe_globalData.max_image_colors;
  328.     return 256;
  329. }
  330.  
  331. Colormap
  332. fe_cmap(MWContext *context)
  333. {
  334.   return CONTEXT_DATA (context)->colormap->cmap;
  335. }
  336.  
  337. Colormap
  338. fe_getColormap(fe_colormap *colormap)
  339. {
  340.   return colormap->cmap;
  341. }
  342.  
  343. Pixel *fe_ColormapMapping(MWContext *context)
  344. {
  345.   return CONTEXT_DATA (context)->colormap->mapping;
  346. }
  347.  
  348. static void
  349. fe_free_colormap_cells(MWContext *context, int lifetime)
  350. {
  351.   int fp, new_fp, i, j;
  352.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  353.   Display *dpy = colormap->dpy; 
  354.   Colormap cmap = colormap->cmap;
  355.   
  356.   if ((fp = CONTEXT_DATA (context)->color_fp))
  357.     {
  358.       unsigned long *cells_to_free = (unsigned long *)
  359.     malloc (sizeof (unsigned long) * (fp + 1));
  360.       XColor *color_data = CONTEXT_DATA (context)->color_data;
  361.       new_fp = 0;
  362.       
  363.       for (i = 0, j = 0; i < fp; i++) {
  364.         if (color_data[i].flags & lifetime)
  365.           {
  366.             unsigned long pixel = color_data[i].pixel;
  367.             cells_to_free [j++] = pixel;
  368.             if (lifetime == CELL_TRANSIENT)
  369.               {
  370.                 assert(colormap->transient_ref_count[pixel]);
  371.                 colormap->transient_ref_count[pixel]--;
  372.               }
  373.           }
  374.         else
  375.           color_data[new_fp++] = color_data[i];
  376.       }
  377.  
  378.       CONTEXT_DATA (context)->color_fp = new_fp;
  379.       if (j)
  380.         XFreeColors (dpy, cmap, cells_to_free, j, 0);
  381.       free (cells_to_free);
  382.     }
  383. }
  384.  
  385. static void
  386. fe_free_context_image_cells(MWContext *context)
  387. {
  388.   int i;
  389.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  390.   int num_cells = colormap->num_cells;
  391.   uint8 *allocation = colormap->allocation;
  392.  
  393.  if (STATIC_VISUAL_P(colormap->visual_info->class))
  394.     return;
  395.   
  396.   fe_free_colormap_cells(context, CELL_IMAGE);
  397.   
  398.   for (i = 0; i < num_cells; i++)
  399.     if (allocation[i] & CELL_IMAGE)
  400.       {
  401.         allocation[i] &= ~CELL_IMAGE;
  402.         if (!(allocation[i] & CELL_ALLOCATION))
  403.           allocation[i] = CELL_AVAIL;
  404.       }
  405. }
  406.  
  407. static void
  408. fe_free_all_image_cells(fe_colormap *colormap)
  409.   struct fe_MWContext_cons *cons;
  410.   for (cons = fe_all_MWContexts ; cons ; cons = cons->next)
  411.     {
  412.       if (CONTEXT_DATA (cons->context)->colormap == colormap)
  413.         fe_free_context_image_cells(cons->context);
  414.     }
  415. }
  416.  
  417.  
  418. /* #define PRINT_COLOR_ALLOC_MAP */
  419.  
  420. /* Called at the end of a page, this function is used to free all the
  421.    temporary colors used for fonts, links, backgrounds, etc.
  422.  */
  423. void
  424. fe_FreeTransientColors(MWContext *context)
  425. {
  426.   unsigned long i;
  427.   int num_cells;
  428.   int pixel;
  429.  
  430.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  431.  
  432.   /* colormap is shared, by the time this function is called
  433.      visual_info by be NULL because it was deleted with
  434.      earlier deletion of a context that reference to it.
  435.  
  436.      Therefore, it's better to check each pointer before
  437.      referencing it. I have found in Frame destrcution,
  438.      fe_delete_colormap was called. It will free visual_info
  439.      with one context. While other frames are hanging on
  440.      to this visual info */
  441.  
  442.   if (  colormap && colormap->visual_info &&
  443.     STATIC_VISUAL_P(colormap->visual_info->class))
  444.     return;
  445.  
  446. #ifdef PRINT_COLOR_ALLOC_MAP  
  447.   printf("Before:\n");
  448.   fe_print_colormap_allocation(colormap);
  449. #endif
  450.   
  451.   num_cells = colormap->num_cells;
  452.  
  453.   /* Compress the color_data cache to remove transient color allocations. */
  454.   fe_free_colormap_cells(context, CELL_TRANSIENT);
  455.  
  456.   /* Now, clear the CELL_TRANSIENT flag bits in the allocation map. */
  457.   for (pixel = 0; pixel < num_cells; pixel++)
  458.     {
  459.       if (!colormap->transient_ref_count[pixel])
  460.         {
  461.           uint8 *allocation = colormap->allocation;
  462.           allocation[pixel] &= ~CELL_TRANSIENT;
  463.           if (!(allocation[pixel] & CELL_LIFETIME))
  464.               allocation[pixel] = CELL_AVAIL;
  465.         }
  466.     }
  467.  
  468. #ifdef PRINT_COLOR_ALLOC_MAP  
  469.   printf("\nAfter:\n");
  470.   fe_print_colormap_allocation(colormap);
  471. #endif
  472.  
  473. #ifdef DEBUG
  474.   /* There shouldn't be any transient colors left allocated when
  475.      the calling context is the only user of the colormap. */
  476.   if (colormap->contexts == 1)
  477.     {
  478.       for (i = 0; i < num_cells; i++)
  479.         assert((colormap->allocation[i] & CELL_TRANSIENT) == 0);
  480.     }
  481. #endif /* DEBUG */
  482. }
  483.  
  484. void
  485. fe_DisposeColormap(MWContext *context)
  486. {
  487.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  488.  
  489.   fe_FreeTransientColors(context);
  490.   
  491.   /* Decrement reference count */
  492.   if (--colormap->contexts)
  493.     return;
  494.  
  495.   if (CONTEXT_DATA (context)->colormap == fe_globalData.common_colormap)
  496.     return;
  497.  
  498.   fe_DisplayFactoryColormapGoingAway(colormap);
  499.  
  500.   XFreeColormap(colormap->dpy, colormap->cmap);
  501.   fe_delete_colormap(colormap);
  502. }
  503.  
  504.  
  505. /* This allocates as many writeable colormap cells as possible.
  506.    The pixels allocated are returned in allocated and allocated_count.
  507.    This also returns a list of those pixels that *couldn't* be allocated
  508.    in unallocated and unallocated_count.  These are the pixels which are
  509.    already allocated.
  510.  
  511.    The caller must free the pixels in `allocated'.
  512.  
  513.    As a side-effect, the allocable pixels will be set to black.
  514.  */
  515. static void
  516. fe_allocate_all_cells (fe_colormap *colormap,
  517.                unsigned long **allocated, int *allocated_count,
  518.                unsigned long **unallocated, int *unallocated_count)
  519. {
  520.   int i;
  521.   Display *dpy = colormap->dpy;
  522.   XColor *blacks = 0;
  523.   Colormap cmap = colormap->cmap;
  524.   unsigned long *pixels_tail;
  525.   int num_cells = colormap->num_cells;
  526.   int requested_pixels = num_cells;
  527.   char *allocation_map = 0;
  528.  
  529.   allocation_map = (char *) calloc (sizeof (char), num_cells);
  530.   *allocated = (unsigned long *) malloc (sizeof (unsigned long) * num_cells);
  531.   *unallocated = (unsigned long *) malloc (sizeof (unsigned long) * num_cells);
  532.   *allocated_count = 0;
  533.   *unallocated_count = 0;
  534.  
  535.   pixels_tail = *allocated;
  536.  
  537.   while (requested_pixels > 0)
  538.     {
  539.       if (XAllocColorCells (dpy, cmap, False, 0, 0,
  540.                 pixels_tail, requested_pixels))
  541.     {
  542. #if 1
  543.       /* we got them all.  Set them to black.
  544.          This isn't actually necessary, but it helps debugging,
  545.          and I really like the visual effect on xcmap :-) */
  546.       if (! blacks)
  547.         {
  548.           blacks = calloc (sizeof (XColor), requested_pixels);
  549.           for (i = 0; i < requested_pixels; i++)
  550.         blacks[i].flags = DoRed|DoGreen|DoBlue;
  551.         }
  552.       for (i = 0; i < requested_pixels; i++)
  553.             blacks[i].pixel = pixels_tail[i];
  554.       XStoreColors (dpy, cmap, blacks, requested_pixels);
  555. #endif
  556.  
  557.       for (i = 0; i < requested_pixels; i++)
  558.             {
  559.               allocation_map [pixels_tail [i]] = 1;
  560.               colormap->allocation[pixels_tail[i]] = CELL_PRIVATE;
  561.             }
  562.  
  563.       *allocated_count += requested_pixels;
  564.       pixels_tail += requested_pixels;
  565.     }
  566.       else
  567.     {
  568.       /* We didn't get all/any of the pixels we asked for.  This time, ask
  569.          for half as many.  (If we do get all that we ask for, we ask for
  570.          the same number again next time, so we only do O(log(n)) server
  571.          roundtrips.)
  572.        */
  573.       requested_pixels = requested_pixels / 2;
  574.     }
  575.     }
  576.  
  577.   /* Now we have allocated as many pixels as we could.
  578.      Now build up a list of the ones we didn't allocate.
  579.    */
  580.   pixels_tail = *unallocated;
  581.   for (i = 0; i < num_cells; i++)
  582.     {
  583.       if (allocation_map [i])  /* This one was free - ignore it. */
  584.     continue;
  585.       *pixels_tail++ = i;
  586.     }
  587.   *unallocated_count = pixels_tail - *unallocated;
  588.  
  589.   free (allocation_map);
  590.   if (blacks) free (blacks);
  591. }
  592.  
  593.  
  594. /* Returns the number of unallocated cells in the map, and as
  595.    a side-effect, sets those cells to black. */
  596. static int
  597. fe_clear_colormap (fe_colormap *colormap,
  598.            Boolean grab_p)
  599. {
  600.   int i;
  601.   Display *dpy = colormap->dpy;
  602.   Colormap cmap = colormap->cmap;
  603.   unsigned long *allocated, *unallocated;
  604.   int allocated_count, unallocated_count;
  605.  
  606.   if (grab_p)
  607.     XGrabServer (dpy);
  608.  
  609.   fe_allocate_all_cells (colormap,
  610.              &allocated, &allocated_count,
  611.              &unallocated, &unallocated_count);
  612.  
  613.   if (allocated_count)
  614.     XFreeColors (dpy, cmap, allocated, allocated_count, 0);
  615.  
  616. #ifdef ASSUME_CELL_PERMANENT
  617.   for (i = 0; i < allocated_count; i++)
  618.     colormap->allocation[allocated[i]] = CELL_PERMANENT;
  619. #else
  620.   for (i = 0; i < allocated_count; i++)
  621.     colormap->allocation[allocated[i]] = CELL_AVAIL;
  622. #endif
  623.   
  624.   if (grab_p)
  625.     {
  626.       XUngrabServer (dpy);
  627.       XSync (dpy, False);
  628.     }
  629.   free (allocated);
  630.   free (unallocated);
  631.   return allocated_count;
  632. }
  633.  
  634. #if 0
  635. static void
  636. fe_interrogate_colormap (Screen *screen,
  637.              Visual *visual,
  638.              Colormap cmap,
  639.              IL_IRGB **rgb_ret,
  640.              int *rgb_count_ret,
  641.              int *free_count_ret)
  642. {
  643.   Display *dpy = DisplayOfScreen (screen);
  644.   int max, nfree, i, j;
  645.   XColor *xcolors;
  646.   IL_IRGB *ilcolors;
  647.   max = fe_VisualCells (dpy, visual);
  648.   /* #### DANGER! DANGER! DANGER! */
  649.   XGrabServer (dpy);
  650.   nfree = fe_clear_colormap (screen, visual, cmap, False);
  651.   ilcolors = (IL_IRGB *) calloc (sizeof (IL_IRGB), max - nfree);
  652.   xcolors = (XColor *) calloc (sizeof (XColor), max);
  653.   for (i = 0; i < max; i++)
  654.     xcolors[i].pixel = i;
  655.   XQueryColors (dpy, cmap, xcolors, max);
  656.   for (i = 0, j = 1; i < max; i++)
  657.     if (xcolors[i].red || xcolors[i].green || xcolors[i].blue)
  658.       {
  659.     ilcolors[j].red   = xcolors[i].red   >> 8;
  660.     ilcolors[j].green = xcolors[i].green >> 8;
  661.     ilcolors[j].blue  = xcolors[i].blue  >> 8;
  662.     ilcolors[j].index = i;
  663.     ilcolors[j].attr  = IL_ATTR_RDONLY;
  664.     j++;
  665.       }
  666.   XUngrabServer (dpy);
  667.   XSync (dpy, False);
  668.   free (xcolors);
  669.   *rgb_ret = ilcolors;
  670.   *rgb_count_ret = j;
  671.   *free_count_ret = nfree;
  672. }
  673. #endif
  674.  
  675.  
  676. /* Remembering the static, unchanging parts of a colormap for future reference.
  677.    The idea here is that there is a set of colors that we allocate once at
  678.    startup (for icons, widget backgrounds and borders, shadows, etc) and never
  679.    free or change; and then there are colors that we allocate for images, which
  680.    might change at some point.  But when we create a new window, we should
  681.    endeavor to share the same static cells to prevent flicker in those items,
  682.    whereas flicker in the images themselves is more acceptable.
  683.  */
  684.  
  685. /* XXX - do we need these variables anymore ? */
  686. static XColor *memorized_colors = 0;
  687. static int n_memorized_colors = 0;
  688.  
  689. static void
  690. fe_memorize_colormap (MWContext *context)
  691. {
  692.   int i;
  693.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  694.   Display *dpy = colormap->dpy;
  695.   unsigned long *allocated, *unallocated;
  696.   int allocated_count, unallocated_count;
  697.  
  698.   if (STATIC_VISUAL_P(colormap->visual_info->class))
  699.     return;
  700.  
  701.   fe_allocate_all_cells (colormap,
  702.              &allocated, &allocated_count,
  703.              &unallocated, &unallocated_count);
  704.  
  705.   if (allocated_count)
  706.     XFreeColors (dpy, colormap->cmap, allocated, allocated_count, 0);
  707.   for (i = 0; i < allocated_count; i++)
  708.     colormap->allocation[allocated[i]] = CELL_AVAIL;
  709.   free (allocated);
  710.  
  711.   if (memorized_colors)
  712.     free (memorized_colors);
  713.   n_memorized_colors = unallocated_count;
  714.   memorized_colors = calloc (sizeof (XColor), n_memorized_colors);
  715.  
  716. #ifdef ASSUME_CELL_PERMANENT
  717.   /* For now, we blindly assume that any color that has already been
  718.      allocated in the colormap is one that Motif has previously
  719.      allocated for us as a widget color.  Of course, that assumption
  720.      could be wrong, but we won't find that out until later when we
  721.      try to use this pixel (see fe_AllocClosestColor()). */
  722. /*  for (i = 0; i < n_memorized_colors; i++)
  723.     colormap->allocation[unallocated [i]] = CELL_AVAIL;
  724. */
  725. #endif
  726.   free (unallocated);
  727.   XQueryColors (dpy, colormap->cmap, memorized_colors, n_memorized_colors);
  728. }
  729.  
  730. static XColor *
  731. fe_get_server_colormap_cache(fe_colormap *colormap)
  732. {
  733.   int num_cells;
  734.   XColor *cache = colormap->cache;
  735.   Boolean cached;
  736.   time_t now;
  737.   int visual_class = colormap->visual_info->class;
  738.  
  739.   /* Don't bother pretending about colormaps with static visuals. */
  740.   if (visual_class == TrueColor)
  741.     return NULL;
  742.  
  743.   cached = (colormap->cache_update_time != 0);
  744.  
  745.   /* There's no point in loading a StaticColor or StaticGray map more
  746.      than once because it never changes.  Private colormaps are never
  747.      changed by anyone but us so it's pointless to contact the
  748.      server. */
  749.   if ((!cached && (now = time ((time_t) 0))) ||
  750.       ((!colormap->private_p)           &&
  751.        (!STATIC_VISUAL_P(visual_class)) &&
  752.        (now = time ((time_t) 0))        &&
  753.        (colormap->cache_update_time < now)))
  754.     {
  755.       num_cells = colormap->num_cells;
  756.       if (!colormap->private_p)
  757.         XQueryColors (colormap->dpy, colormap->cmap, cache, num_cells);
  758.       colormap->cache_update_time = now;
  759.     }
  760.   return cache;
  761. }
  762.  
  763.  
  764.  
  765.  
  766. static uint
  767. fe_FindClosestColor (fe_colormap *colormap, XColor *color_in_out,
  768.                      int allocation_flags)
  769. {
  770.   int num_cells = colormap->num_cells;
  771.   XColor color;
  772.   unsigned long distance = ~0;
  773.   int i, found = 0;
  774.   XColor *cache;
  775.  
  776.   /* Don't interrogate server's colormap about colors that we've allocated */
  777.   cache = colormap->cache;
  778.   if (!colormap->cache_update_time || (allocation_flags & CELL_AVAIL))
  779.     cache = fe_get_server_colormap_cache(colormap);
  780.  
  781.   /* Find the closest color. */
  782.   for (i = 0; i < num_cells; i++)
  783.     {
  784.       unsigned long d;
  785.       int rd, gd, bd;
  786.  
  787.       if (!(colormap->allocation[i] & allocation_flags))
  788.         continue;
  789.       
  790.       rd = C16to8(color_in_out->red)   - C16to8(cache[i].red);
  791.       gd = C16to8(color_in_out->green) - C16to8(cache[i].green);
  792.       bd = C16to8(color_in_out->blue)  - C16to8(cache[i].blue);
  793.       if (rd < 0) rd = -rd;
  794.       if (gd < 0) gd = -gd;
  795.       if (bd < 0) bd = -bd;
  796.       d = (rd << 1) + (gd << 2) + bd;
  797.       
  798.       if (d < distance)
  799.     {
  800.       distance = d;
  801.       found = i;
  802.           if (distance == 0)
  803.               break;
  804.     }
  805.     }
  806.  
  807.   if (distance != ~0)
  808.     {
  809.       color = cache[found];
  810.       color.pixel = found;
  811.       *color_in_out = color;
  812.     }
  813.  
  814.     return distance;
  815. }
  816.  
  817. /* Like XAllocColor(), but searches the current colormap for the best
  818.    match - this always successfully allocates some color, hopefully one
  819.    close to that which was requested.
  820.  
  821.    The only slot of the MWContext that is used is the `server_colormap'
  822.    cache slot - the widget is not used, so it's safe to call this early.
  823.  */
  824. void
  825. fe_AllocClosestColor (fe_colormap *colormap,
  826.                       XColor *color_in_out)
  827. {
  828.   Display *dpy = colormap->dpy;
  829.   Colormap cmap = colormap->cmap;
  830.   int num_cells = colormap->num_cells;
  831.   int retry_count = 0;
  832.   int done, i, distance, expected_pixel;
  833.   XColor color = *color_in_out;
  834.  
  835. RETRY:
  836.   /* First try to find an exact match for a color we already own.  We
  837.      don't have to interrogate the server for that case.  (Note, however,
  838.      that Motif allocates colors for us that we don't know about). */
  839.   distance = fe_FindClosestColor(colormap, &color, CELL_SHARED);
  840.  
  841.   /* An exact color match ? */
  842.   if (distance == 0)
  843.     {
  844.       expected_pixel = color.pixel;
  845.       /* XXX - fur - we can get eventually get rid of this call to
  846.          XAllocColor() since we already own this color, but we need to
  847.          adjust our accounting */
  848.       if (!XAllocColor (dpy, cmap, &color))
  849.       {
  850. #ifdef ASSUME_CELL_PERMANENT
  851.         /* Oops, if we couldn't allocate that color, it means that the
  852.            colormap cell that we thought was a permanent cell that we
  853.            had allocated ourself because we found it already allocated
  854.            when we initialized the colormap is, in fact, a read/write
  855.            cell that belongs to somebody else. */
  856.           assert (allocation & CELL_PERMANENT);
  857. #else
  858.         /* No - we don't make that assumption.  Maybe later, but not yet. */
  859.         assert(0);
  860. #endif
  861.  
  862.         /* Mark cell temporarily so that we don't try to allocate it again. */
  863.         colormap->allocation[color.pixel] = CELL_MARKED;
  864.  
  865.         retry_count++;
  866.     goto RETRY;
  867.       }
  868.     }
  869.   else
  870.     {
  871.       /* OK, *we* haven't previously allocated this exact color before.
  872.          Try to find a close color and share it. */
  873.       do
  874.         {
  875.           distance =
  876.               fe_FindClosestColor(colormap, &color, CELL_SHARED|CELL_AVAIL);
  877.           assert(distance != ~0);
  878.           expected_pixel = color.pixel;
  879.  
  880.           if (!(done = XAllocColor (dpy, cmap, &color)))
  881.             {
  882.               /* We couldn't allocate the cell.  Either
  883.  
  884.                  a) the colormap has changed since we last
  885.                     copied it into the colormap cache from the server, or
  886.  
  887.                  b) the colormap cell that we thought was a
  888.                     permanent cell that we had allocated ourself
  889.                     because we found it already allocated when we
  890.                     initialized the colormap is, in fact, a read/write
  891.                     cell that belongs to somebody else.  So mark this
  892.                     cell as temporarily unavailable and try again.  */
  893.               assert(colormap->allocation[color.pixel] & CELL_AVAIL);
  894.  
  895.               colormap->allocation[color.pixel] = CELL_MARKED;
  896.               retry_count++;
  897.             }
  898.         } while (!done && retry_count != num_cells);
  899.     }
  900.   
  901.   assert(retry_count != num_cells);
  902.   assert(expected_pixel == color.pixel);
  903.   
  904.   /* Remove all the "temporarily unavailable" marks in the allocation map. */
  905.   for (i = 0; i < num_cells; i++)
  906.     {
  907.       if (colormap->allocation[i] & CELL_MARKED)
  908.         colormap->allocation[i] = CELL_AVAIL;
  909.     }
  910.  
  911. #if 0
  912.   fprintf (real_stderr,
  913.  #ifdef OSF1
  914.           "substituted %04X %04X %04X for %04X %04X %04X (%ld)\n",
  915. #else
  916.           "substituted %04X %04X %04X for %04X %04X %04X (%ld)\n",
  917. #endif
  918.        color.red, color.green, color.blue,
  919.        color_in_out->red, color_in_out->green, color_in_out->blue,
  920.        distance);
  921. #endif
  922.  
  923.   *color_in_out = color;
  924. }
  925.  
  926. int fe_ColorDepth(fe_colormap * colormap)
  927. /*
  928.  * description:
  929.  *
  930.  * returns:
  931.  *    The number of palette entries expressed in the
  932.  *    same manner as bits per pixel, e.g. if 256 colors
  933.  *    are available the return value is 8.
  934.  */
  935. {
  936.   int result = 0;
  937.   if (colormap)
  938.     {
  939.       double x;
  940.       double y;
  941.       x = (double)(colormap->num_cells);
  942.       y = log(x)/log(2.0);
  943.       result = (int)y;
  944.     }
  945.   return result;
  946. }
  947.  
  948. Status
  949. fe_AllocColor(fe_colormap *colormap, XColor *color_in_out)
  950. {
  951.   Status status = XAllocColor(colormap->dpy, colormap->cmap, color_in_out);
  952.   
  953.   /* Update local cached copy of server-side color map. */
  954.   if (status && colormap->cache)
  955.     colormap->cache[color_in_out->pixel] = *color_in_out;
  956.  
  957.   return status;
  958. }
  959.  
  960.  
  961. /* Calls XAllocColor, caching and reusing the result (without a
  962.    server roundtrip.)  The pixel allocated is never freed until
  963.    the window is deleted.  The RGB arguments are 8 bit values, not 16.
  964.  */
  965. static Pixel
  966. fe_get_shared_pixel (MWContext *context,
  967.                      int r, int g, int b, /* 8-bit color channels */
  968.                      int lifetime)        /* CELL_TRANSIENT, CELL_PERMANENT,
  969.                                              CELL_IMAGE */
  970. {
  971.   int i, fp;
  972.   unsigned long pixel;
  973.   XColor *cd;
  974.  
  975.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  976.  
  977.   /* Get color cache data */
  978.   cd = CONTEXT_DATA (context)->color_data;
  979.   fp = CONTEXT_DATA (context)->color_fp;
  980.  
  981.   /* Create color cache if it doesn't exist. */
  982.   if (!CONTEXT_DATA (context)->color_size)
  983.     {
  984.       CONTEXT_DATA (context)->color_size = 50;
  985.       CONTEXT_DATA (context)->color_data = cd =
  986.     calloc (sizeof (XColor), CONTEXT_DATA (context)->color_size);
  987.     }
  988.  
  989.   /* Mask, in case someone gave us 16-bit color components */
  990.   r &= 0xff;
  991.   g &= 0xff;
  992.   b &= 0xff;
  993.  
  994.   /* X's pixel components are 16 bits; LO uses 8. */
  995.   r = C8to16(r);
  996.   g = C8to16(g);
  997.   b = C8to16(b);
  998.  
  999.   /* Search through the cache of colors we've previously matched */
  1000.   if (STATIC_VISUAL_P(colormap->visual_info->class))
  1001.     {
  1002.       for (i = 0; i < fp; i++) {
  1003.         if (cd [i].red == r && cd [i].green == g && cd [i].blue == b)
  1004.             return cd [i].pixel;
  1005.       }
  1006.     }
  1007.   else
  1008.     {
  1009.       for (i = 0; i < fp; i++) {
  1010.         unsigned long pixel = cd [i].pixel;
  1011.         uint8 *allocation = &colormap->allocation[pixel];
  1012.         
  1013.         /* Color cell lifetimes must be of the same class, except that
  1014.            permanent colors can always be reused */
  1015.         if ((*allocation & (lifetime | colormap->persistent_colors)) &&
  1016.             (cd [i].red == r && cd [i].green == g && cd [i].blue == b))
  1017.           {
  1018.             *allocation |= lifetime;
  1019.             return cd [i].pixel;
  1020.           }
  1021.       }
  1022.     }
  1023.  
  1024.   /* We didn't find the color in the cache.  Try to allocate it.
  1025.      Enlarge cache, if necessary. */
  1026.   if (fp >= CONTEXT_DATA (context)->color_size)
  1027.     {
  1028.       CONTEXT_DATA (context)->color_size += 50;
  1029.       CONTEXT_DATA (context)->color_data = cd =
  1030.     realloc (cd, sizeof (XColor) * CONTEXT_DATA (context)->color_size);
  1031.     }
  1032.  
  1033.   cd [fp].flags = (DoRed|DoGreen|DoBlue);
  1034.   cd [fp].red   = r;
  1035.   cd [fp].green = g;
  1036.   cd [fp].blue  = b;
  1037.  
  1038.   if (!fe_AllocColor (colormap, &cd [fp]))
  1039.     fe_AllocClosestColor (colormap, &cd [fp]);
  1040.  
  1041.   pixel = cd [fp].pixel;
  1042.   cd [fp].flags |= lifetime;
  1043.  
  1044.   /* Record the new allocation in the allocation map. */ 
  1045.   if (colormap->allocation)
  1046.     {
  1047.       colormap->allocation[pixel] &= ~CELL_AVAIL;
  1048.       colormap->allocation[pixel] |= (CELL_SHARED|lifetime);
  1049.       if (lifetime == CELL_TRANSIENT)
  1050.         colormap->transient_ref_count[pixel]++;
  1051.     }
  1052.   
  1053.   /* Either XAllocColor or fe_AllocClosestColor gave us back new rgb values
  1054.      (the "close" color.)  (XAllocColor will round to the nearest color the
  1055.      hardware supports and fail otherwise; fe_AllocClosestColor will round
  1056.      to the nearest color that can actually be allocated.  We store the
  1057.      values we actually requested back in there so that the cache works... */
  1058.  
  1059.   cd [fp].red   = r;
  1060.   cd [fp].green = g;
  1061.   cd [fp].blue  = b;
  1062.  
  1063.   CONTEXT_DATA (context)->color_fp++;
  1064.  
  1065.   return pixel;
  1066. }
  1067.  
  1068. /* Get a transient color that will be released once we're done displaying
  1069.    the current page. */
  1070. Pixel
  1071. fe_GetPixel (MWContext *context, int r, int g, int b)
  1072. {
  1073.   return fe_get_shared_pixel (context, r, g, b, CELL_TRANSIENT);
  1074. }
  1075.  
  1076. /* Get a color that will be allocated for the life of the app */
  1077. Pixel
  1078. fe_GetPermanentPixel (MWContext *context, int r, int g, int b)
  1079. {
  1080.   return fe_get_shared_pixel (context, r, g, b, CELL_PERMANENT);
  1081. }
  1082.  
  1083. Pixel
  1084. fe_GetImagePixel (MWContext *context, int r, int g, int b)
  1085. {
  1086.   return fe_get_shared_pixel (context, r, g, b, CELL_IMAGE);
  1087. }
  1088.  
  1089. #if 0
  1090. /* Check to see if all the colors in a map can be allocated */
  1091. static int
  1092. fe_unavailable_image_colors (MWContext *context, int num_colors,
  1093.                              IL_IRGB *map, int allocation_flags)
  1094. {
  1095.   XColor color;
  1096.   uint distance;
  1097.  
  1098.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  1099.   int close = 0;
  1100.   int colors_remaining = num_colors;
  1101.   IL_IRGB *c = map;
  1102.  
  1103.   while(colors_remaining--)
  1104.   {
  1105.       color.red   = c->red;
  1106.       color.blue  = c->blue;
  1107.       color.green = c->green;
  1108.       color.flags = DoRed|DoGreen|DoBlue;
  1109.       c++;
  1110.  
  1111.       distance = fe_FindClosestColor (colormap, &color, allocation_flags);
  1112.  
  1113.       if (distance < 10)
  1114.           close++;
  1115.   }
  1116.   
  1117.   return num_colors - close;
  1118. }
  1119. #endif /* 0 */
  1120.  
  1121.  
  1122. void
  1123. fe_QueryColor (MWContext *context, XColor *color)
  1124. {
  1125.   XColor *cd = CONTEXT_DATA (context)->color_data;
  1126.   int fp = CONTEXT_DATA (context)->color_fp;
  1127.   int i;
  1128.  
  1129.   /* First look for it in our cached data. */
  1130.   if (cd)
  1131.     for (i = 0; i < fp; i++)
  1132.       if (cd [i].pixel == color->pixel)
  1133.     {
  1134.       *color = cd [i];
  1135.       return;
  1136.     }
  1137.  
  1138.   /* If it's not there, then make a server round-trip. */
  1139.   XQueryColor (XtDisplay (CONTEXT_WIDGET (context)),
  1140.            fe_cmap(context), color);
  1141. }
  1142.  
  1143. #ifdef GRAYSCALE_WORKS
  1144.  
  1145. /* This remaps 8-bit gray values to server pixel values, if we're using
  1146.    a GrayScale visual, which is different from StaticGray in that the gray
  1147.    values indirect through a colormap. */
  1148. Pixel fe_gray_map [256] = { 0, };
  1149.  
  1150. static Boolean
  1151. init_gray_ramp (MWContext *context, Visual *visual)
  1152. {
  1153.   int i;
  1154.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  1155.   Display *dpy = colormap->dpy;
  1156.   int depth = fe_VisualDepth (dpy, visual);
  1157.   int cells = countof (fe_gray_map);
  1158.  
  1159.   if (depth == 1)
  1160.     return True;
  1161.  
  1162.   if ((1 << depth) < cells)
  1163.     cells = (1 << depth);
  1164.  
  1165.   for (i = 0; i < cells; i++)
  1166.     {
  1167.       int v = ((i * (countof (fe_gray_map) / cells))
  1168.            + (countof (fe_gray_map) / (cells * 2)));
  1169.       fe_gray_map [i] = fe_GetPermanentPixel(context, v, v, v);
  1170.     }
  1171.   return True;
  1172. }
  1173.  
  1174. #endif /* GRAYSCALE_WORKS */
  1175.  
  1176.  
  1177. /* We don't distinguish between colors that are "closer" together
  1178.    than this.  The appropriate setting is a subjective matter. */
  1179. #define IL_CLOSE_COLOR_THRESHOLD  6
  1180.  
  1181. /* Distance to closest color in map for a given pixel index */
  1182. struct color_distance {
  1183.   uint distance;
  1184.   int index;
  1185. };
  1186.  
  1187. /* Sorting predicate for qsort() */
  1188. static int
  1189. compare_color_distances(const void *a, const void *b)
  1190. {
  1191.     struct color_distance *a1 = (struct color_distance*)a;
  1192.     struct color_distance *b1 = (struct color_distance*)b;
  1193.  
  1194.     return (a1->distance < b1->distance) ? 1 :
  1195.         ((a1->distance > b1->distance) ? -1 : 0);
  1196. }
  1197.  
  1198. /* When allocating image colors using read/write cells, make sure that
  1199.    some are left over for text, backgrounds, etc.  This is in addition
  1200.    to any CELL_PERMANENT colors that are allocated when the program
  1201.    starts. */
  1202. #define NUM_COLORS_RESERVED_FOR_TRANSIENTS   5
  1203.  
  1204. /* Force a given palette, relying on close colors.
  1205.  
  1206.    When honor_palette_indices is FALSE, we can arbitrarily assign the
  1207.    palette index that we want the image library to use, perhaps to
  1208.    minimize palette flashing.  This flag is normally set to FALSE the
  1209.    first time an image is to be decoded.  When an image is displayed
  1210.    subsequent times, IL_ATTR_HONOR_INDEX is set, and the XFE must
  1211.    exactly match the specified index in the IL_IRGB struct.  This
  1212.    can't simply be a mapping from the image's indices to the server
  1213.    indices, because in this case the image pixmap is already resident
  1214.    on the server.  Therefore, the index must be mapped to the same
  1215.    physical hardware colormap index on the server.
  1216.  
  1217.    Image pixels are handled two different ways.  If the colormap is
  1218.    private, image colors are allocated as writeable cells (though they
  1219.    can also be read-only CELL_PERMANENT cells).  If the colormap is
  1220.    shared, image colors are treated essentially the same as
  1221.    CELL_PERMANENT colors.
  1222.  
  1223.    This function is called only once per colormap if the colormap is not
  1224.    a private one.
  1225. */
  1226. static int
  1227. fe_SetColorMap (MWContext *context,
  1228.                  IL_ColorMap *cmap,
  1229.                  int num_requested,
  1230.                  XP_Bool honor_indices)
  1231. {
  1232.   /* The image library is requesting `num_requested', possibly
  1233.      non-unique colors.
  1234.  
  1235.      Prior to this we have allocated three "kinds" of colors: those
  1236.      used for the icons/widgets themselves, and those used for text
  1237.      and background colors and those used for images.
  1238.  
  1239.      At this point, we will free the cells of the last type, while leaving
  1240.      the cells of the other types alone (since they are still in use.)  */
  1241.  
  1242.   /* Keep track of distance between requested colors and the semi-permanent
  1243.      widget colors. */
  1244.   struct color_distance color_sort_array[256];
  1245.   IL_RGB sorted_map[256], *map_in, *map_out;
  1246.   uint8 sorted_pos[256];
  1247.   XColor color;
  1248.   int i, j, free_count;
  1249.  
  1250.   int exact_matches = 0;
  1251.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  1252.   int num_cells = colormap->num_cells;
  1253.  
  1254.   map_in = map_out = cmap->map;
  1255.   colormap->mapping_size = num_requested;
  1256.   
  1257.   /* Create the array of server indices for the colormap, and initialize it
  1258.      to the index order of the IL_ColorMap. */
  1259.   if (!cmap->index) {
  1260.       cmap->index = (uint8*)XP_CALLOC(256, 1);
  1261.       if (!cmap->index)
  1262.           return 0;
  1263.       for (i = 0; i < 256; i++)
  1264.           cmap->index[i] = i;
  1265.   }
  1266.  
  1267.   if (colormap->private_p) {
  1268.     XColor colors[256];
  1269.     unsigned long *allocated, *unallocated;
  1270.     int allocated_count, unallocated_count;
  1271.     uint8 *allocation = colormap->allocation;
  1272.  
  1273.     if (!colormap->writeable_cells_allocated) {
  1274.       fe_allocate_all_cells (colormap,
  1275.                              &allocated, &allocated_count,
  1276.                              &unallocated, &unallocated_count);
  1277.  
  1278.       /* Give back some colors so we have some left for other purposes */
  1279.       if (allocated_count < NUM_COLORS_RESERVED_FOR_TRANSIENTS)
  1280.         return 0;
  1281.       XFreeColors (colormap->dpy, colormap->cmap,
  1282.                    allocated, NUM_COLORS_RESERVED_FOR_TRANSIENTS, 0);
  1283.       
  1284.       for (i = 0; i < allocated_count; i++)
  1285.         if (i < NUM_COLORS_RESERVED_FOR_TRANSIENTS)
  1286.           colormap->allocation[allocated[i]] = CELL_AVAIL;
  1287.         else
  1288.           colormap->allocation[allocated[i]] = (CELL_IMAGE | CELL_PRIVATE);
  1289.       
  1290.       free (unallocated);
  1291.       free (allocated);
  1292.       colormap->writeable_cells_allocated = True;
  1293.     }
  1294.  
  1295.     j = 0;
  1296.     for (i = 0; i < num_requested; i++)
  1297.       {
  1298.         uint distance;
  1299.         XColor tmpcolor1, tmpcolor2, tmpcolor3, outcolor;
  1300.         int red, green, blue;
  1301.         int index;
  1302.  
  1303. #ifndef M12N                    /* XXXM12N Get rid of this? */
  1304.         int index   = map_in[i].index;
  1305.  
  1306.         /* "Transparent" colors get mapped to the background */
  1307.         if (map_in[i].attr & IL_ATTR_TRANSPARENT)
  1308.           {
  1309.             map_out[i].index = CONTEXT_DATA (context)->bg_pixel;
  1310.             continue;
  1311.           }
  1312. #endif /* M12N */
  1313.  
  1314.         /* X's pixel components are 16 bits; LO uses 8. */
  1315.         red   = C8to16(map_in[i].red);
  1316.         green = C8to16(map_in[i].green);
  1317.         blue  = C8to16(map_in[i].blue);
  1318.  
  1319.         color.red   = red;
  1320.         color.green = green;
  1321.         color.blue  = blue;
  1322.         color.flags = DoRed|DoGreen|DoBlue;
  1323.  
  1324. #ifndef M12N                    /* XXXM12N Fix me. */
  1325.         /* Can we assign an arbitrary palette index to this color ? */
  1326.         if (!(map_in[i].attr & IL_ATTR_HONOR_INDEX))
  1327. #else
  1328.             /* XXXM12N The honor index attribute was used when restoring
  1329.                the default colormap after installing a custom colormap.
  1330.                We need to fix custom colormaps once we get the rest of M12N
  1331.                working. */
  1332.         if (!honor_indices)    
  1333. #endif /* M12N */
  1334.           {
  1335.             /* Make a copy to avoid fe_FindClosestColor() side-effects */
  1336.             tmpcolor1 = color;
  1337.  
  1338.             distance =
  1339.               fe_FindClosestColor (colormap, &tmpcolor1, CELL_PERMANENT);
  1340.             assert (distance != ~0);
  1341.             index = tmpcolor1.pixel;
  1342.  
  1343.             /* If we can get a very close shared color, take that instead of a 
  1344.                read/write cell */
  1345.             if (distance <= IL_CLOSE_COLOR_THRESHOLD)
  1346.               {
  1347. #ifdef DEBUG
  1348.                 Status status;
  1349.                 tmpcolor1.flags = DoRed|DoGreen|DoBlue;
  1350.                 /* We shouldn't need to allocate a color that we already own. */
  1351.                 status = fe_AllocColor(colormap, &tmpcolor1);
  1352.                 assert (status);
  1353.  
  1354.                 /* Make sure we got the one we asked for */
  1355.                 assert (tmpcolor1.pixel == index);
  1356. #endif
  1357.                 if (distance == 0)
  1358.                   exact_matches++;
  1359.                 outcolor = tmpcolor1;
  1360.               }
  1361.             else
  1362.               {
  1363.                 /* Make a copy to avoid fe_FindClosestColor() side-effects */
  1364.                 tmpcolor2 = color;
  1365.  
  1366.                 /* See if we've already allocated a private close color. */
  1367.                 distance =
  1368.                   fe_FindClosestColor (colormap, &tmpcolor2, CELL_MARKED);
  1369.  
  1370.                 index = tmpcolor2.pixel;
  1371.                 outcolor = tmpcolor2;
  1372.  
  1373.                 if (distance > IL_CLOSE_COLOR_THRESHOLD)
  1374.                   {
  1375.                     /* Try to minimize color flashing by picking a cell
  1376.                        that's close in color to the desired one.  Yes,
  1377.                        yes, I know.  A lost cause. */
  1378.                     tmpcolor3 = color;
  1379.                     distance =
  1380.                       fe_FindClosestColor (colormap, &tmpcolor3, CELL_PRIVATE);
  1381.  
  1382.                     /* Any more writeable, private colormap cells left ? */
  1383.                     if (distance != ~0)
  1384.                       {
  1385.                         index = tmpcolor3.pixel;
  1386.                 
  1387.                         /* Temporarily mark the cell */
  1388.                         allocation[index] = CELL_MARKED;
  1389.                         colors[j] = color;
  1390.                         colors[j].pixel = index;
  1391.                         colors[j].flags = DoRed|DoGreen|DoBlue;
  1392.                         outcolor = colors[j];
  1393.                         colormap->cache[index] = outcolor;
  1394.                         j++;
  1395.                         exact_matches++;
  1396.                       }
  1397.                   }
  1398.                 else if (distance == 0)
  1399.                   exact_matches++;
  1400.               }
  1401.  
  1402.             /* Reflect the actual colors and palette indices to our caller. */
  1403.             cmap->index[i] = index;
  1404.             map_out[i].red   = C16to8(outcolor.red);
  1405.             map_out[i].green = C16to8(outcolor.green);
  1406.             map_out[i].blue  = C16to8(outcolor.blue);
  1407.           }
  1408.         else                    /* IL_ATTR_HONOR_INDEX */
  1409.           {
  1410.             index = cmap->index[i];
  1411.  
  1412.             /* Is this a read-only cell ? */
  1413.             if (allocation[index] & CELL_SHARED)
  1414.               {
  1415. #ifdef DEBUG
  1416.                 /* We shouldn't need to allocate a color that we already own. */
  1417.                 Status status = fe_AllocColor(colormap, &color);
  1418.                 assert (status);
  1419.  
  1420.                 /* Make sure we got the one we asked for */
  1421.                 assert (color.pixel == index);
  1422.                 assert (allocation[index] & CELL_PERMANENT);
  1423. #endif
  1424.               }
  1425.             else 
  1426.               /* Nope, it must be a writeable cell. */
  1427.               {
  1428.                 assert(allocation[index] & CELL_PRIVATE);
  1429.                 colors[j] = color;
  1430.                 colors[j].pixel = index;
  1431.                 colors[j].flags = DoRed|DoGreen|DoBlue;
  1432.                 j++;
  1433.               }
  1434.  
  1435.             exact_matches++;
  1436.             
  1437.             /* Reflect the actual colors to our caller. */
  1438.             map_out[i].red   = C16to8(color.red);
  1439.             map_out[i].green = C16to8(color.green);
  1440.             map_out[i].blue  = C16to8(color.blue);
  1441.           }
  1442.       }
  1443.  
  1444.     if (j)
  1445.       XStoreColors (colormap->dpy, colormap->cmap, colors, j);
  1446.  
  1447.     /* Remove all the marks in the allocation map. */
  1448.     for (i = 0; i < num_cells; i++)
  1449.       {
  1450.         if (colormap->allocation[i] & CELL_MARKED)
  1451.           colormap->allocation[i] = (CELL_PRIVATE | CELL_IMAGE);
  1452.       }
  1453.   
  1454.     return exact_matches;
  1455.   }
  1456.  
  1457.   /* Colormap isn't private.  This is our one-and-only initialization call . */
  1458.  
  1459.   /* First free all of the image cells, that is, all the cells
  1460.      that were allocated by fe_GetPixel() for images.  Compress
  1461.      the color_data cache to remove those colors. */
  1462.  
  1463.   fe_free_all_image_cells(colormap);
  1464.   free_count = fe_clear_colormap(colormap, False);
  1465.  
  1466.   /* Sort the requested colors in terms of decreasing distance from any
  1467.      existing colors in the palette.  That way, if we run out of
  1468.      entries to allocate, the remaining colors, which get allocated to
  1469.      the closest existing entry, won't lose as badly. Don't bother to do
  1470.      this if there are more free color map entries than requested. */
  1471.  
  1472.   if (num_requested > free_count)
  1473.     {
  1474.       for (i = 0; i < num_requested; i++) {
  1475.         uint distance;
  1476.       
  1477.           color.red   = map_in[i].red;
  1478.           color.green = map_in[i].green;
  1479.           color.blue  = map_in[i].blue;
  1480.       
  1481.           distance =
  1482.             fe_FindClosestColor (colormap, &color, colormap->persistent_colors);
  1483.           color_sort_array[i].distance = distance;
  1484.           color_sort_array[i].index = i;
  1485.      }
  1486.  
  1487.      XP_QSORT(color_sort_array, num_requested, sizeof(struct color_distance),
  1488.               compare_color_distances);
  1489.  
  1490.      /* Reorder the request colors to correspond to the sorted order */
  1491.      for (i = 0; i < num_requested; i++) {
  1492.          j = color_sort_array[i].index;
  1493.          sorted_map[i] = map_in[j];
  1494.          sorted_pos[i] = j;
  1495.      }
  1496.      map_in = sorted_map;
  1497.   }
  1498.   else {
  1499.       for (i = 0; i < num_requested; i++)
  1500.           sorted_pos[i] = i;
  1501.   }
  1502.  
  1503.   /* Now attempt to allocate some new cells. */
  1504.   {
  1505.     XColor *server_map = fe_get_server_colormap_cache(colormap);
  1506.     IL_RGB *mp;
  1507.     int i;
  1508.     /* This will always allocate the requested number of pixels, but in
  1509.        some cases will allocate closest-match colors instead, due to the
  1510.        magic of fe_get_shared_pixel(). */
  1511.     if (num_requested > num_cells)
  1512.       abort ();
  1513.  
  1514.     /* Allocate the colors. */
  1515.     for (i = 0, mp = map_in; i < num_requested; i++, mp++) {
  1516.       Pixel pixel;
  1517.       int actual_red, actual_blue, actual_green;
  1518.       j = sorted_pos[i];
  1519.       
  1520. #ifndef M12N                    /* XXXM12N Get rid of this? */
  1521.       if (mp->attr & IL_ATTR_TRANSPARENT)
  1522.           pixel = CONTEXT_DATA (context)->bg_pixel;
  1523.       else
  1524. #endif /* M12N */
  1525.         {
  1526.           pixel = fe_GetImagePixel (context, mp->red, mp->green, mp->blue);
  1527.  
  1528.           actual_red   = C16to8(server_map[pixel].red);
  1529.           actual_green = C16to8(server_map[pixel].green);
  1530.           actual_blue  = C16to8(server_map[pixel].blue);
  1531.  
  1532.           if ((mp->red == actual_red) && (mp->green == actual_green) &&
  1533.               (mp->blue == actual_blue))
  1534.               exact_matches++;
  1535.       
  1536.           /* Reflect the actual quantized color allocated to the requestor. */
  1537.           map_out[j].red   = actual_red;
  1538.           map_out[j].green = actual_green;
  1539.           map_out[j].blue  = actual_blue;
  1540.         }
  1541.       cmap->index[j] = pixel;
  1542.  
  1543.       /* Update the image->server translation map */
  1544.       /* im [mp->index] = pixel; */
  1545.     }
  1546.   }
  1547.  
  1548.   return exact_matches;
  1549. }
  1550.  
  1551.  
  1552. /* Given a mask value, returns the index of the first set bit, and the
  1553.    number of consecutive bits set. */
  1554. static void
  1555. FindShift (unsigned long mask, uint8 *shiftp, uint8* bitsp)
  1556. {
  1557.   uint8 shift = 0;
  1558.   uint8 bits = 0;
  1559.   while ((mask & 1) == 0) {
  1560.     shift++;
  1561.     mask >>= 1;
  1562.   }
  1563.   while ((mask & 1) == 1) {
  1564.     bits++;
  1565.     mask >>= 1;
  1566.   }
  1567.   *shiftp = shift;
  1568.   *bitsp = bits;
  1569. }
  1570.  
  1571. #if 0
  1572. /* Initialize the colormap for displaying images.
  1573.  */
  1574. Colormap
  1575. fe_CopyDefaultColormap (Screen *screen, MWContext *new_context)
  1576. {
  1577.   Colormap default_cmap = DefaultColormapOfScreen (screen);
  1578.   /* First, notice which cells are allocated in the default map. */
  1579.   fe_memorize_colormap (screen, default_cmap);
  1580.   /* Then, make a new map with those same cells allocated. */
  1581.   return fe_make_new_colormap_1 (screen, new_context);
  1582. }
  1583. #endif
  1584.  
  1585.  
  1586. static IL_ColorMap *
  1587. fe_RealizeDefaultColormap(MWContext *context, int max_colors)
  1588. {
  1589.     IL_ColorMap *cmap = NULL;
  1590.     int num_exact_matches;
  1591.  
  1592.     /* XXXM12N This function should have code to pick an appropriate color
  1593.        cube, as in the old IL_RealizeDefaultColormap.  For now, we assume
  1594.        that we can install a color cube with 216 colors. */
  1595.  
  1596.     /* Minimun of max_colors is 8, so we can make a reasonable color cube.
  1597.        of size 2x2x2. */
  1598.     if(max_colors < 8)
  1599.         max_colors = 8;
  1600.  
  1601.     /* Maximum of max_colors is 216. (6x6x6 color cube) */
  1602.     if(max_colors > 216) 
  1603.         max_colors = 216;
  1604.     
  1605.     cmap = IL_NewCubeColorMap(NULL, 0, max_colors);
  1606.     
  1607.     /* This may alter colormap entries to the closest available colors.
  1608.        There is no need to honor the colormap indices the first time we
  1609.        install the colormap. */
  1610.     num_exact_matches = fe_SetColorMap(context, cmap, max_colors, FALSE);
  1611.  
  1612.     return cmap;
  1613. }
  1614.  
  1615.  
  1616. void 
  1617. fe_InitColormap (MWContext *context)
  1618. {
  1619.   Display *dpy = XtDisplay (CONTEXT_WIDGET (context));
  1620.   fe_colormap *colormap = CONTEXT_DATA (context)->colormap;
  1621.   Visual *visual = colormap->visual;
  1622.   XVisualInfo *vi_out = colormap->visual_info;
  1623.   int pixmap_depth;
  1624.  
  1625.   static Boolean warned = False;
  1626.   Boolean ugly_visual = False;
  1627.   Boolean broken_visual = False;
  1628.  
  1629.   /* Remember the state of the colormap before the first color is allocated
  1630.      for images.  We only need to do this once - for the first window
  1631.      created.  Other windows will have exactly the same non-image cells
  1632.      allocated, since we allocate all of those in the same way.
  1633.  
  1634.      The rest of this comment is hypothetical / not (yet) true:
  1635.  
  1636.      When the default visual is being used with a non-default colormap,
  1637.      fe_memorize_colormap() will actually be called twice - first, it will
  1638.      be called before we create our first widget, to make sure that we have
  1639.      duplicated all of the allocated cells in the default map into our private
  1640.      map.  Then it will be called a second time after we have initialized our
  1641.      first top-level window, to lock down all of the other cells we've
  1642.      allocated, so that subsequent windows will share those cells too.
  1643.  
  1644.      This means that any cells which are allocated in the default map at the
  1645.      time Netscape starts up will also be allocated in Netscape's map(s).
  1646.      This might not be such a good thing, as those colors might suck.
  1647.      There probably should be a resource to control this behavior.
  1648.    */
  1649.   if (!colormap->contexts && !colormap->private_p)
  1650.     fe_memorize_colormap (context);
  1651.   colormap->contexts++;
  1652.  
  1653.   /* Sharing this colormap (and thus its matching colorspace) ? */
  1654.   if (colormap->contexts > 1)
  1655.       context->color_space = colormap->color_space;
  1656.  
  1657. #ifndef M12N
  1658.   /* This no longer exists. */
  1659.   IL_InitContext (context);
  1660. #else
  1661.   /* Color spaces, i.e. palettes are one-per-window, so they're inherited from
  1662.      the parent context. */
  1663.   if (!context->color_space && context->is_grid_cell) {
  1664.       XP_ASSERT(context->grid_parent);
  1665.       context->color_space = context->grid_parent->color_space;
  1666.   }
  1667.  
  1668.   /* Increment the reference count if the colorspace exists. */
  1669.   if (context->color_space)
  1670.       IL_AddRefToColorSpace(context->color_space);
  1671. #endif /* M12N */
  1672.  
  1673.   pixmap_depth = fe_VisualPixmapDepth (dpy, visual);
  1674.  
  1675.   if (! fe_globalData.force_mono_p)
  1676.     switch (vi_out->class)
  1677.       {
  1678.       case StaticGray:
  1679.     /* All depths of gray or mono dithered visuals are fine. */
  1680.     break;
  1681.  
  1682.       case GrayScale:
  1683. #ifdef GRAYSCALE_WORKS
  1684.     /* To make GrayScale visuals work, we need to allocate a gray
  1685.        ramp in the colormap, and remap the intensity values that
  1686.        the image library gives us into that map. */
  1687.     if (! init_gray_ramp (context, visual))
  1688.       broken_visual = True;
  1689. #else /* !GRAYSCALE_WORKS */
  1690.     if (vi_out->depth > 1)
  1691.       broken_visual = True;
  1692. #endif /* !GRAYSCALE_WORKS */
  1693.     break;
  1694.  
  1695.       case TrueColor:
  1696.     /* Shallow non-colormapped visuals work but look bad. */
  1697.     if (vi_out->depth < 8)
  1698.       ugly_visual = True;
  1699.     break;
  1700.  
  1701.       case StaticColor:
  1702.     /* StaticColor visuals usually contain a color cube, so they look
  1703.        ok so long as they're not too shallow.  Let's hope no StaticGray
  1704.        visuals claim that they're StaticColor visuals. */
  1705.     if (vi_out->depth < 8)
  1706.       ugly_visual = True;
  1707.     break;
  1708.  
  1709.       case DirectColor:
  1710. #ifndef DIRECTCOLOR_WORKS
  1711.     /* #### DirectColor visuals are like TrueColor, but have three
  1712.        colormaps - one for each component of RGB.  So the RGB
  1713.        values that the image library gives us are not suitable
  1714.        unless we were to specially prepare the maps, or were
  1715.        to remap the values. */
  1716.     broken_visual = True;
  1717. #endif /* !DIRECTCOLOR_WORKS */
  1718.     break;
  1719.  
  1720.       case PseudoColor:
  1721.     /* Colormapped visuals are supported ONLY in depth 8. */
  1722.     if (vi_out->depth != 8)
  1723.       broken_visual = True;
  1724.     break;
  1725.       }
  1726.  
  1727.   if (ugly_visual || broken_visual)
  1728.     {
  1729.       char buf [2048];
  1730.         char *str = 0;
  1731.  
  1732. #ifdef GRAYSCALE_WORKS
  1733. #ifdef DIRECTCOLOR_WORKS
  1734.         str = XP_GetString( XFE_VISUAL_GRAY_DIRECTCOLOR );
  1735. #endif
  1736. #endif
  1737. #ifdef GRAYSCALE_WORKS
  1738.         if( 0 == str ) str = XP_GetString( XFE_VISUAL_GRAY );
  1739. #endif
  1740. #ifdef DIRECTCOLOR_WORKS
  1741.         if( 0 == str ) str = XP_GetString( XFE_VISUAL_DIRECTCOLOR );
  1742. #endif
  1743.         if( 0 == str ) str = XP_GetString( XFE_VISUAL_NORMAL );
  1744.  
  1745.  
  1746.       PR_snprintf (buf, sizeof (buf), str, /* #### i18n */ 
  1747.            (unsigned int) vi_out->visualid,
  1748.            (vi_out->depth == 8 ? "n" : ""),
  1749.            vi_out->depth,
  1750.            (vi_out->class == StaticGray  ? "StaticGray" :
  1751.         vi_out->class == StaticColor ? "StaticColor" :
  1752.         vi_out->class == TrueColor   ? "TrueColor" :
  1753.         vi_out->class == GrayScale   ? "GrayScale" :
  1754.         vi_out->class == PseudoColor ? "PseudoColor" :
  1755.         vi_out->class == DirectColor ? "DirectColor" : "UNKNOWN"),
  1756.            (broken_visual
  1757.         ? XP_GetString( XFE_WILL_BE_DISPLAYED_IN_MONOCHROME )
  1758.         : XP_GetString( XFE_WILL_LOOK_BAD )), fe_progclass);
  1759.  
  1760.       FE_Alert (context, buf);
  1761.       warned = True;
  1762.     }
  1763.  
  1764.   /* Interrogate X server and get definitions of the existing color map.
  1765.    */
  1766.   if (vi_out->depth == 1 || fe_globalData.force_mono_p || broken_visual)
  1767.     {
  1768.       if (colormap->contexts > 1) {
  1769.         assert(colormap->color_space);
  1770.         goto colorspace_init_complete;
  1771.       }
  1772.  
  1773.       /* Tell the image library to only ever return us depth 1 il_images.
  1774.      We will still enlarge these to visual-depth when displaying them. */
  1775.       if (!context->color_space)
  1776.           context->color_space = IL_CreateGreyScaleColorSpace(1, 1);
  1777.  
  1778.       fe_SetTransparentPixel(context, 0xff, 0xff, 0xff, 0xff);
  1779.     }
  1780.   else if (vi_out->class == StaticGray ||
  1781.        vi_out->class == GrayScale)
  1782.     {
  1783.       XColor color;
  1784.  
  1785.       if (colormap->contexts > 1) {
  1786.         assert(colormap->color_space);
  1787.         goto colorspace_init_complete;
  1788.       }
  1789.  
  1790.       if (!context->color_space)
  1791.           context->color_space = IL_CreateGreyScaleColorSpace(pixmap_depth,
  1792.                                                               pixmap_depth);
  1793.  
  1794.       color.pixel = CONTEXT_DATA (context)->default_bg_pixel;
  1795.       fe_QueryColor (context, &color);
  1796.       fe_SetTransparentPixel(context, (color.red>>8), (color.green>>8),
  1797.                              (color.blue>>8), color.pixel);
  1798.     }
  1799.   else if (vi_out->class == TrueColor ||
  1800.        vi_out->class == DirectColor)
  1801.     {
  1802.       XColor color;
  1803.       IL_RGBBits rgb;           /* RGB bit allocation and shifts. */
  1804.  
  1805.       if (colormap->contexts > 1) {
  1806.           assert(colormap->color_space);
  1807.           goto colorspace_init_complete;
  1808.       }
  1809.  
  1810.       /* Way cool. Tell image library to use true-color mode. Inform it
  1811.          of the locations of the components and the total size of the
  1812.          components. For X we need to do some work to figure out what
  1813.          the shifts are for each component. Sigh.
  1814.  
  1815.      I suppose it might be possible that some system somewhere
  1816.          interleaves the bits used in the RGB values (since the X server
  1817.          claims these are masks, not indexes) but that seems pretty unlikely.
  1818.        */
  1819.       FindShift (visual->red_mask, &rgb.red_shift, &rgb.red_bits);
  1820.       FindShift (visual->green_mask, &rgb.green_shift, &rgb.green_bits);
  1821.       FindShift (visual->blue_mask, &rgb.blue_shift, &rgb.blue_bits);
  1822.  
  1823.       if (!context->color_space)
  1824.           context->color_space = IL_CreateTrueColorSpace(&rgb, pixmap_depth);
  1825.  
  1826.       color.pixel = CONTEXT_DATA (context)->default_bg_pixel;
  1827.       fe_QueryColor (context, &color);
  1828.       fe_SetTransparentPixel(context, (color.red>>8), (color.green>>8),
  1829.                              (color.blue>>8), 0xff);
  1830.     }
  1831.   else
  1832.     {
  1833.       int max;
  1834.       XColor color;
  1835.       IL_ColorMap *cmap;
  1836.  
  1837.       /* Have we initialized this fe_colormap before ? */
  1838.       if (colormap->contexts > 1) {
  1839.         assert(colormap->color_space);
  1840.         goto colorspace_init_complete;
  1841.       }
  1842.  
  1843.       max = 256;
  1844.       /* User-specified max number of cells. */
  1845.  
  1846.       if (!colormap->private_p)
  1847.         {
  1848.           if ((fe_globalData.max_image_colors) > 0 && !colormap->private_p)
  1849.             max = fe_globalData.max_image_colors;
  1850.  
  1851. #ifdef __sgi
  1852.           if (max > 170 && fe_globalData.sgi_mode_p)
  1853.             /* When SGI "schemes" are in use, we need to leave them A LOT of
  1854.                free cells.  What a crock! */
  1855.             max = 170;
  1856. #endif /* __sgi */
  1857.         } 
  1858.  
  1859.       cmap = fe_RealizeDefaultColormap(context, max);
  1860.  
  1861.       /* If we can't get even 8 colors, revert to monochrome and tell
  1862.          the user a small lie (an exaggeration, really). */
  1863.       if (!warned && !cmap->num_colors)
  1864.     {
  1865.       char buf [2048];
  1866.       PR_snprintf (buf, sizeof (buf),
  1867.             fe_globalData.cube_too_small_message, 2);
  1868.       FE_Alert (context, buf);
  1869.       warned = True;
  1870.           colormap->contexts--;
  1871.           fe_globalData.force_mono_p = True;
  1872.           if (context->color_space)
  1873.               IL_ReleaseColorSpace(context->color_space);
  1874.           fe_InitColormap(context);
  1875.           return;
  1876.     }
  1877.  
  1878.       /* Warn the user if we couldn't allocate a decent number of colors */
  1879.       if (!warned && cmap->num_colors < 48 && 
  1880.       vi_out->class == PseudoColor)
  1881.     {
  1882.       char buf [2048];
  1883.       PR_snprintf (buf, sizeof (buf),
  1884.                        fe_globalData.cube_too_small_message,
  1885.                        cmap->num_colors);
  1886.       FE_Alert (context, buf);
  1887.       warned = True;
  1888.     }
  1889.  
  1890.       if (!context->color_space)
  1891.           context->color_space = IL_CreatePseudoColorSpace(cmap, pixmap_depth,
  1892.                                                            pixmap_depth);
  1893.       /* Get background color */
  1894.       color.pixel = CONTEXT_DATA (context)->default_bg_pixel;
  1895.       fe_QueryColor (context, &color);
  1896.       fe_SetTransparentPixel(context, (color.red>>8), (color.green>>8),
  1897.                              (color.blue>>8), color.pixel);
  1898.     }
  1899.  
  1900.   colormap->color_space = context->color_space;
  1901.  
  1902. colorspace_init_complete:
  1903.  
  1904.   if (!fe_ImagesCantWork)
  1905.     {
  1906.       extern IL_DitherMode fe_pref_string_to_dither_mode (char *s);
  1907.       uint32 display_prefs;
  1908.       IL_DisplayData dpy_data;
  1909.       char* dither_images = fe_globalPrefs.dither_images;
  1910.  
  1911. #ifndef M12N                    /* XXXM2N Fix custom colormaps. */
  1912.       IL_ColorRenderMode color_render_mode;
  1913.       
  1914.       if (colormap->private_p)
  1915.           color_render_mode = ilInstallPaletteAllowed;
  1916.       else
  1917.           color_render_mode = ilRGBColorCube;
  1918.  
  1919.       IL_SetColorRenderMode (context, color_render_mode);
  1920. #endif /* M12N */
  1921.  
  1922. #ifndef M12N                    /* XXXM12N Get rid of this.  It never did
  1923.                                   anything. */
  1924.       IL_SetByteOrder (context,
  1925.                BitmapBitOrder (dpy) == 0,
  1926.                ImageByteOrder (dpy) == 0);
  1927. #endif /* M12N */
  1928.  
  1929.       dpy_data.color_space = context->color_space;
  1930.       dpy_data.progressive_display = fe_globalPrefs.streaming_images;
  1931.       dpy_data.dither_mode =
  1932.           fe_pref_string_to_dither_mode(dither_images);
  1933.       display_prefs = IL_COLOR_SPACE | IL_PROGRESSIVE_DISPLAY | IL_DITHER_MODE;
  1934.       IL_SetDisplayMode (context->img_cx, display_prefs, &dpy_data);
  1935.     }
  1936. }
  1937.  
  1938.  
  1939. /* Set the transparent pixel color.  The transparent pixel is passed into
  1940.    calls to IL_GetImage for image requests that do not use a mask. */
  1941. XP_Bool
  1942. fe_SetTransparentPixel(MWContext *context, uint8 red, uint8 green,
  1943.                        uint8 blue, Pixel server_index)
  1944. {
  1945.     IL_ColorSpace *color_space = context->color_space;
  1946.     IL_IRGB *trans_pixel = context->transparent_pixel;
  1947.  
  1948.     if (!trans_pixel) {
  1949.         trans_pixel = context->transparent_pixel = XP_NEW_ZAP(IL_IRGB);
  1950.         if (!trans_pixel)
  1951.             return FALSE;
  1952.     }
  1953.  
  1954.     /* Set the color of the transparent pixel. */
  1955.     trans_pixel->red = red;
  1956.     trans_pixel->green = green;
  1957.     trans_pixel->blue = blue;
  1958.     
  1959.     /* For PseudoColor and GreyScale visuals, we must also provide the Image
  1960.        Library with the index of the transparent pixel.  Note that this
  1961.        must be an index into the map array of the cross-platform IL_ColorMap,
  1962.        and not an FE palette index. */
  1963.     XP_ASSERT(color_space);
  1964.     if (color_space->type == NI_PseudoColor ||
  1965.         color_space->type == NI_GreyScale) {
  1966.         int i, index, num_colors;
  1967.         IL_ColorMap *cmap = &color_space->cmap;
  1968.         IL_RGB *map;
  1969.  
  1970.         num_colors = cmap->num_colors;
  1971.         map = cmap->map;
  1972.  
  1973.     /*
  1974.      * I dont know why, but map needs to be valid or else bad things 
  1975.      * happen on monochorme displays...at this point map will be 0x0 
  1976.      * on monochrome display, but i guess the following functions assume
  1977.      * it wont, cause there are no checks....
  1978.      */
  1979.     if (!map || !num_colors)
  1980.     {
  1981.         return FALSE;
  1982.     }
  1983.  
  1984.         for (i = 0, index = -1; i < num_colors; i++, map++) {
  1985.             if ((map->red == red) &&
  1986.                 (map->green == green) &&
  1987.                 (map->blue == blue)) {
  1988.                 index = i;
  1989.                 break;
  1990.             }
  1991.         }
  1992.         if (index < 0) {
  1993.             /* The transparent pixel color isn't in the IL_ColorMap, so
  1994.                add it to the map array. */
  1995.             if (!IL_AddColorToColorMap(cmap, trans_pixel))
  1996.                 return FALSE;
  1997.  
  1998.             /* We must also update the IL_ColorMap's index array. */
  1999.             cmap->index[trans_pixel->index] = (uint8)server_index;
  2000.         }
  2001.         else {
  2002.             trans_pixel->index = index;
  2003.         }
  2004.     }
  2005.             
  2006.     return TRUE;
  2007. }
  2008.