Modified and totally untested code to load the color tables for DirectColor and PseudoColor windows.
authorBob Pendleton
Fri, 09 Jan 2009 20:41:31 +0000
changeset 3010a6694a812119
parent 3009 546c022a9ae5
child 3011 8f4ed5ec2b06
Modified and totally untested code to load the color tables for DirectColor and PseudoColor windows.
src/video/x11/SDL_x11gamma.c
src/video/x11/SDL_x11gamma.h
     1.1 --- a/src/video/x11/SDL_x11gamma.c	Fri Jan 09 20:39:33 2009 +0000
     1.2 +++ b/src/video/x11/SDL_x11gamma.c	Fri Jan 09 20:41:31 2009 +0000
     1.3 @@ -30,8 +30,8 @@
     1.4      Display *display;
     1.5      int scrNum;
     1.6      Colormap colormap;
     1.7 -    XStandardColormap cmap;
     1.8      Visual visual;
     1.9 +    Uint16 *ramp;
    1.10  } cmapTableEntry;
    1.11  
    1.12  cmapTableEntry *cmapTable = NULL;
    1.13 @@ -49,8 +49,8 @@
    1.14      for (i = 0; i < numCmaps; i++) {
    1.15          if (cmapTable[i].display == display &&
    1.16              cmapTable[i].scrNum == scrNum &&
    1.17 -            cmapTable[i].cmap.visualid == vid) {
    1.18 -            return cmapTable[i].cmap.colormap;
    1.19 +            cmapTable[i].visual.visualid == vid) {
    1.20 +            return cmapTable[i].colormap;
    1.21          }
    1.22      }
    1.23  
    1.24 @@ -60,9 +60,11 @@
    1.25  
    1.26  void
    1.27  X11_TrackColormap(Display * display, int scrNum, Colormap colormap,
    1.28 -                  XStandardColormap * cmap, Visual * visual)
    1.29 +                  Visual * visual, XColor * ramp)
    1.30  {
    1.31      int i;
    1.32 +    Uint16 *newramp;
    1.33 +    int ncolors;
    1.34  
    1.35      /* search the table to find out if we already have this one. We
    1.36         only want one entry for each display, screen number, visualid,
    1.37 @@ -70,8 +72,8 @@
    1.38      for (i = 0; i < numCmaps; i++) {
    1.39          if (cmapTable[i].display == display &&
    1.40              cmapTable[i].scrNum == scrNum &&
    1.41 -            cmapTable[i].cmap.visualid == cmap->visualid &&
    1.42 -            cmapTable[i].cmap.colormap == colormap) {
    1.43 +            cmapTable[i].visual.visualid == visual->visualid &&
    1.44 +            cmapTable[i].colormap == colormap) {
    1.45              return;
    1.46          }
    1.47      }
    1.48 @@ -88,8 +90,24 @@
    1.49      cmapTable[numCmaps].display = display;
    1.50      cmapTable[numCmaps].scrNum = scrNum;
    1.51      cmapTable[numCmaps].colormap = colormap;
    1.52 -    SDL_memcpy(&cmapTable[numCmaps].cmap, cmap, sizeof(XStandardColormap));
    1.53      SDL_memcpy(&cmapTable[numCmaps].visual, visual, sizeof(Visual));
    1.54 +    cmapTable[numCmaps].ramp = NULL;
    1.55 +
    1.56 +    newramp = SDL_malloc(3 * 256 * sizeof(Uint16));     /* The size of *all* SDL gamma ramps */
    1.57 +    if (NULL == newramp) {
    1.58 +        SDL_SetError("Out of memory in X11_TrackColormap()");
    1.59 +        return;
    1.60 +    }
    1.61 +    SDL_memset(newramp, 0, sizeof(*newramp));
    1.62 +    cmapTable[numCmaps].ramp = newramp;
    1.63 +
    1.64 +    ncolors = cmapTable[numCmaps].visual.map_entries;
    1.65 +
    1.66 +    for (i = 0; i < ncolors; i++) {
    1.67 +        newramp[(0 * 256) + i] = ramp[i].red;
    1.68 +        newramp[(1 * 256) + i] = ramp[i].green;
    1.69 +        newramp[(2 * 256) + i] = ramp[i].blue;
    1.70 +    }
    1.71  
    1.72      numCmaps++;
    1.73  }
    1.74 @@ -103,44 +121,69 @@
    1.75  int
    1.76  X11_SetDisplayGammaRamp(_THIS, Uint16 * ramp)
    1.77  {
    1.78 +    Visual *visual;
    1.79      Display *display;
    1.80      Colormap colormap;
    1.81      XColor *colorcells;
    1.82      int ncolors;
    1.83 +    int rmask, gmask, bmask;
    1.84 +    int rshift, gshift, bshift;
    1.85      int i;
    1.86      int j;
    1.87  
    1.88 -    int rmax, gmax, bmax;
    1.89 -    int rmul, gmul, bmul;
    1.90 -
    1.91      for (j = 0; j < numCmaps; j++) {
    1.92          if (cmapTable[j].visual.class == DirectColor) {
    1.93              display = cmapTable[j].display;
    1.94              colormap = cmapTable[j].colormap;
    1.95              ncolors = cmapTable[j].visual.map_entries;
    1.96 +            visual = &cmapTable[j].visual;
    1.97  
    1.98              colorcells = SDL_malloc(ncolors * sizeof(XColor));
    1.99              if (NULL == colorcells) {
   1.100                  SDL_SetError("out of memory in X11_SetDisplayGammaRamp");
   1.101                  return -1;
   1.102              }
   1.103 +            /* remember the new ramp */
   1.104 +            SDL_memcpy(cmapTable[j].ramp, ramp, sizeof(*cmapTable[j].ramp));
   1.105  
   1.106 -            rmax = cmapTable[j].cmap.red_max + 1;
   1.107 -            gmax = cmapTable[j].cmap.blue_max + 1;
   1.108 -            bmax = cmapTable[j].cmap.green_max + 1;
   1.109 +            rshift = 0;
   1.110 +            rmask = visual->red_mask;
   1.111 +            while (0 == (rmask & 1)) {
   1.112 +                rshift++;
   1.113 +                rmask >>= 1;
   1.114 +            }
   1.115 +
   1.116 +/*             printf("rmask = %4x rshift = %4d\n", rmask, rshift); */
   1.117  
   1.118 -            rmul = cmapTable[j].cmap.red_mult;
   1.119 -            gmul = cmapTable[j].cmap.blue_mult;
   1.120 -            bmul = cmapTable[j].cmap.green_mult;
   1.121 +            gshift = 0;
   1.122 +            gmask = visual->green_mask;
   1.123 +            while (0 == (gmask & 1)) {
   1.124 +                gshift++;
   1.125 +                gmask >>= 1;
   1.126 +            }
   1.127 +
   1.128 +/*             printf("gmask = %4x gshift = %4d\n", gmask, gshift); */
   1.129 +
   1.130 +            bshift = 0;
   1.131 +            bmask = visual->blue_mask;
   1.132 +            while (0 == (bmask & 1)) {
   1.133 +                bshift++;
   1.134 +                bmask >>= 1;
   1.135 +            }
   1.136 +
   1.137 +/*             printf("bmask = %4x bshift = %4d\n", bmask, bshift); */
   1.138  
   1.139              /* build the color table pixel values */
   1.140              for (i = 0; i < ncolors; i++) {
   1.141 -                Uint32 red = (rmax * i) / ncolors;
   1.142 -                Uint32 green = (gmax * i) / ncolors;
   1.143 -                Uint32 blue = (bmax * i) / ncolors;
   1.144 +                Uint32 rbits = (rmask * i) / (ncolors - 1);
   1.145 +                Uint32 gbits = (gmask * i) / (ncolors - 1);
   1.146 +                Uint32 bbits = (bmask * i) / (ncolors - 1);
   1.147  
   1.148 -                colorcells[i].pixel =
   1.149 -                    (red * rmul) | (green * gmul) | (blue * bmul);
   1.150 +                Uint32 pix =
   1.151 +                    (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
   1.152 +
   1.153 +                colorcells[i].pixel = pix;
   1.154 +
   1.155                  colorcells[i].flags = DoRed | DoGreen | DoBlue;
   1.156  
   1.157                  colorcells[i].red = ramp[(0 * 256) + i];
   1.158 @@ -160,73 +203,17 @@
   1.159  int
   1.160  X11_GetDisplayGammaRamp(_THIS, Uint16 * ramp)
   1.161  {
   1.162 -    Display *display;
   1.163 -    Colormap colormap;
   1.164 -    XColor *colorcells;
   1.165 -    int ncolors;
   1.166 -    int dc;
   1.167      int i;
   1.168  
   1.169 -    int rmax, gmax, bmax;
   1.170 -    int rmul, gmul, bmul;
   1.171 -
   1.172      /* find the first DirectColor colormap and use it to get the gamma
   1.173         ramp */
   1.174  
   1.175 -    dc = -1;
   1.176      for (i = 0; i < numCmaps; i++) {
   1.177          if (cmapTable[i].visual.class == DirectColor) {
   1.178 -            dc = i;
   1.179 -            break;
   1.180 +            SDL_memcpy(ramp, cmapTable[i].ramp, sizeof(*cmapTable[i].ramp));
   1.181 +            return 0;
   1.182          }
   1.183      }
   1.184  
   1.185 -    if (dc < 0) {
   1.186 -        return -1;
   1.187 -    }
   1.188 -
   1.189 -    /* there is at least one DirectColor colormap in the cmapTable,
   1.190 -       let's just get the entries from that colormap */
   1.191 -
   1.192 -    display = cmapTable[dc].display;
   1.193 -    colormap = cmapTable[dc].colormap;
   1.194 -    ncolors = cmapTable[dc].visual.map_entries;
   1.195 -    colorcells = SDL_malloc(ncolors * sizeof(XColor));
   1.196 -    if (NULL == colorcells) {
   1.197 -        SDL_SetError("out of memory in X11_GetDisplayGammaRamp");
   1.198 -        return -1;
   1.199 -    }
   1.200 -
   1.201 -    rmax = cmapTable[dc].cmap.red_max + 1;
   1.202 -    gmax = cmapTable[dc].cmap.blue_max + 1;
   1.203 -    bmax = cmapTable[dc].cmap.green_max + 1;
   1.204 -
   1.205 -    rmul = cmapTable[dc].cmap.red_mult;
   1.206 -    gmul = cmapTable[dc].cmap.blue_mult;
   1.207 -    bmul = cmapTable[dc].cmap.green_mult;
   1.208 -
   1.209 -    /* build the color table pixel values */
   1.210 -    for (i = 0; i < ncolors; i++) {
   1.211 -        Uint32 red = (rmax * i) / ncolors;
   1.212 -        Uint32 green = (gmax * i) / ncolors;
   1.213 -        Uint32 blue = (bmax * i) / ncolors;
   1.214 -
   1.215 -        colorcells[i].pixel = (red * rmul) | (green * gmul) | (blue * bmul);
   1.216 -    }
   1.217 -
   1.218 -    XQueryColors(display, colormap, colorcells, ncolors);
   1.219 -
   1.220 -    /* prepare the values to be returned. Note that SDL assumes that
   1.221 -       gamma ramps are always 3 * 256 entries long with the red entries
   1.222 -       in the first 256 elements, the green in the second 256 elements
   1.223 -       and the blue in the last 256 elements */
   1.224 -
   1.225 -    for (i = 0; i < ncolors; i++) {
   1.226 -        ramp[(0 * 256) + i] = colorcells[i].red;
   1.227 -        ramp[(1 * 256) + i] = colorcells[i].green;
   1.228 -        ramp[(2 * 256) + i] = colorcells[i].blue;
   1.229 -    }
   1.230 -
   1.231 -    SDL_free(colorcells);
   1.232 -    return 0;
   1.233 +    return -1;
   1.234  }
     2.1 --- a/src/video/x11/SDL_x11gamma.h	Fri Jan 09 20:39:33 2009 +0000
     2.2 +++ b/src/video/x11/SDL_x11gamma.h	Fri Jan 09 20:41:31 2009 +0000
     2.3 @@ -27,8 +27,8 @@
     2.4  extern Colormap X11_LookupColormap(Display * display, int scrNum,
     2.5                                     VisualID vid);
     2.6  extern void X11_TrackColormap(Display * display, int scrNum,
     2.7 -                              Colormap colormap, XStandardColormap * cmap,
     2.8 -                              Visual * visual);
     2.9 +                              Colormap colormap,
    2.10 +                              Visual * visual, XColor * ramp);
    2.11  
    2.12  extern int X11_SetDisplayGammaRamp(_THIS, Uint16 * ramp);
    2.13  extern int X11_GetDisplayGammaRamp(_THIS, Uint16 * ramp);