1 /*
   2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifdef HEADLESS
  27     #error This file should not be included in headless library
  28 #endif
  29 
  30 #ifdef MACOSX
  31 #include <stdlib.h>
  32 #endif
  33 
  34 #include "robot_common.h"
  35 
  36 /*
  37  * QueryColorMap is taken from multiVis.c, part of the xwd distribution from
  38  * X.org. It was moved here so it can be shared with awt_DataTransferer.c
  39  */
  40 int32_t
  41 QueryColorMap(Display *disp,
  42               Colormap src_cmap,
  43               Visual *src_vis,
  44               XColor **src_colors,
  45               int32_t *rShift, int32_t *gShift, int32_t *bShift)
  46 
  47 {
  48      int32_t ncolors, i;
  49      unsigned long redMask, greenMask, blueMask;
  50      int32_t                 redShift, greenShift, blueShift;
  51      XColor *colors ;
  52 
  53      ncolors = src_vis->map_entries ;
  54      *src_colors = colors = (XColor *)calloc(ncolors,sizeof(XColor) ) ;
  55 
  56      if(src_vis->class != TrueColor && src_vis->class != DirectColor)
  57      {
  58          for(i=0 ; i < ncolors ; i++)
  59          {
  60                 colors[i].pixel = i ;
  61                 colors[i].pad = 0;
  62                 colors[i].flags = DoRed|DoGreen|DoBlue;
  63          }
  64      }
  65      else /** src is decomposed rgb ***/
  66      {
  67         /* Get the X colormap */
  68         redMask = src_vis->red_mask;
  69         greenMask = src_vis->green_mask;
  70         blueMask = src_vis->blue_mask;
  71         redShift = 0; while (!(redMask&0x1)) {
  72                 redShift++;
  73                 redMask = redMask>>1;
  74         }
  75         greenShift = 0; while (!(greenMask&0x1)) {
  76                 greenShift++;
  77                 greenMask = greenMask>>1;
  78         }
  79         blueShift = 0; while (!(blueMask&0x1)) {
  80                 blueShift++;
  81                 blueMask = blueMask>>1;
  82         }
  83         *rShift = redShift ;
  84         *gShift = greenShift ;
  85         *bShift = blueShift ;
  86         for (i=0; i<ncolors; i++) {
  87                 if( (uint32_t)i <= redMask) colors[i].pixel = (i<<redShift) ;
  88                 if( (uint32_t)i <= greenMask) colors[i].pixel |= (i<<greenShift) ;
  89                 if( (uint32_t)i <= blueMask) colors[i].pixel |= (i<<blueShift) ;
  90                 /***** example :for gecko's 3-3-2 map, blue index should be <= 3
  91 .
  92                 colors[i].pixel = (i<<redShift)|(i<<greenShift)|(i<<blueShift);
  93                 *****/
  94                 colors[i].pad = 0;
  95                 colors[i].flags = DoRed|DoGreen|DoBlue;
  96         }
  97       }
  98 
  99       XQueryColors(disp, src_cmap, colors, ncolors);
 100       return ncolors ;
 101 }