src/video/x11/SDL_x11gamma.c
author Bob Pendleton <bob@pendleton.com>
Wed, 25 Jul 2007 21:22:55 +0000
changeset 2214 e7164a4dac62
parent 2213 59a667370c57
child 2216 82a133b784c9
permissions -rw-r--r--
Added gamma table support to X11. Also now supports DirectColor visuals.
bob@2162
     1
/*
bob@2162
     2
    SDL - Simple DirectMedia Layer
bob@2162
     3
    Copyright (C) 1997-2006 Sam Lantinga
bob@2162
     4
bob@2162
     5
    This library is free software; you can redistribute it and/or
bob@2162
     6
    modify it under the terms of the GNU Lesser General Public
bob@2162
     7
    License as published by the Free Software Foundation; either
bob@2162
     8
    version 2.1 of the License, or (at your option) any later version.
bob@2162
     9
bob@2162
    10
    This library is distributed in the hope that it will be useful,
bob@2162
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bob@2162
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bob@2162
    13
    Lesser General Public License for more details.
bob@2162
    14
bob@2162
    15
    You should have received a copy of the GNU Lesser General Public
bob@2162
    16
    License along with this library; if not, write to the Free Software
bob@2162
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bob@2162
    18
bob@2162
    19
    Sam Lantinga
bob@2162
    20
    slouken@libsdl.org
bob@2162
    21
*/
bob@2162
    22
#include "SDL_config.h"
bob@2162
    23
#include "../SDL_sysvideo.h"
bob@2162
    24
#include "SDL_x11video.h"
bob@2162
    25
bob@2213
    26
static int numCmaps = 0;
bob@2213
    27
bob@2213
    28
typedef struct
bob@2213
    29
{
bob@2213
    30
    Display *display;
bob@2213
    31
    int scrNum;
bob@2214
    32
    Colormap colormap;
bob@2213
    33
    XStandardColormap cmap;
bob@2213
    34
    Visual visual;
bob@2213
    35
} cmapTableEntry;
bob@2213
    36
bob@2213
    37
cmapTableEntry *cmapTable = NULL;
bob@2213
    38
bob@2214
    39
/* To reduce the overhead as much as possible lets do as little as
bob@2214
    40
   possible. When we do have to create a colormap keep track of it and
bob@2214
    41
   reuse it. We're going to do this for both DirectColor and
bob@2214
    42
   PseudoColor colormaps. */
bob@2214
    43
bob@2214
    44
Colormap
bob@2214
    45
X11_LookupColormap(Display * display, int scrNum, VisualID vid)
bob@2214
    46
{
bob@2214
    47
    int i;
bob@2214
    48
bob@2214
    49
    for (i = 0; i < numCmaps; i++) {
bob@2214
    50
        if (cmapTable[i].display == display &&
bob@2214
    51
            cmapTable[i].scrNum == scrNum &&
bob@2214
    52
            cmapTable[i].cmap.visualid == vid) {
bob@2214
    53
            return cmapTable[i].cmap.colormap;
bob@2214
    54
        }
bob@2214
    55
    }
bob@2214
    56
bob@2214
    57
    return 0;
bob@2214
    58
}
bob@2214
    59
bob@2214
    60
bob@2213
    61
void
bob@2214
    62
X11_TrackColormap(Display * display, int scrNum, Colormap colormap,
bob@2213
    63
                  XStandardColormap * cmap, Visual * visual)
bob@2213
    64
{
bob@2213
    65
    int i;
bob@2213
    66
    cmapTableEntry *newTable = NULL;
bob@2213
    67
bob@2214
    68
    /* search the table to find out if we already have this one. We
bob@2214
    69
       only want one entry for each display, screen number, visualid,
bob@2214
    70
       and colormap combination */
bob@2213
    71
    for (i = 0; i < numCmaps; i++) {
bob@2213
    72
        if (cmapTable[i].display == display &&
bob@2213
    73
            cmapTable[i].scrNum == scrNum &&
bob@2214
    74
            cmapTable[i].cmap.visualid == cmap->visualid &&
bob@2214
    75
            cmapTable[i].cmap.colormap == colormap) {
bob@2213
    76
            return;
bob@2213
    77
        }
bob@2213
    78
    }
bob@2213
    79
bob@2213
    80
    /* increase the table by one entry. If the table is NULL create the
bob@2213
    81
       first entrty */
bob@2213
    82
    newTable = SDL_malloc((numCmaps + 1) * sizeof(cmapTableEntry));
bob@2213
    83
    if (NULL == newTable) {
bob@2213
    84
        SDL_SetError("Out of memory in X11_TrackColormap()");
bob@2213
    85
        return;
bob@2213
    86
    }
bob@2213
    87
bob@2213
    88
    if (NULL != cmapTable) {
bob@2213
    89
        SDL_memcpy(newTable, cmapTable, numCmaps * sizeof(cmapTableEntry));
bob@2213
    90
        SDL_free(cmapTable);
bob@2213
    91
    }
bob@2213
    92
    cmapTable = newTable;
bob@2213
    93
bob@2213
    94
    cmapTable[numCmaps].display = display;
bob@2213
    95
    cmapTable[numCmaps].scrNum = scrNum;
bob@2214
    96
    cmapTable[numCmaps].colormap = colormap;
bob@2213
    97
    SDL_memcpy(&cmapTable[numCmaps].cmap, cmap, sizeof(XStandardColormap));
bob@2213
    98
    SDL_memcpy(&cmapTable[numCmaps].visual, visual, sizeof(Visual));
bob@2213
    99
bob@2213
   100
    numCmaps++;
bob@2213
   101
}
bob@2213
   102
bob@2214
   103
/* The problem is that you have to have at least one DirectColor
bob@2214
   104
   colormap before you can set the gamma ramps or read the gamma
bob@2214
   105
   ramps. If the application has created a DirectColor window then the
bob@2214
   106
   cmapTable will have at least one colormap in it and everything is
bob@2214
   107
   cool. If not, then we just fail  */
bob@2214
   108
bob@2162
   109
int
bob@2162
   110
X11_SetDisplayGammaRamp(_THIS, Uint16 * ramp)
bob@2162
   111
{
bob@2214
   112
    Display *display;
bob@2214
   113
    Colormap colormap;
bob@2214
   114
    XColor *colorcells;
bob@2214
   115
    int ncolors;
bob@2214
   116
    int i;
bob@2214
   117
    int j;
bob@2214
   118
bob@2214
   119
    int rmax, gmax, bmax;
bob@2214
   120
    int rmul, gmul, bmul;
bob@2214
   121
bob@2214
   122
    for (j = 0; j < numCmaps; j++) {
bob@2214
   123
        if (cmapTable[j].visual.class == DirectColor) {
bob@2214
   124
            display = cmapTable[j].display;
bob@2214
   125
            colormap = cmapTable[j].colormap;
bob@2214
   126
            ncolors = cmapTable[j].visual.map_entries;
bob@2214
   127
bob@2214
   128
            colorcells = SDL_malloc(ncolors * sizeof(XColor));
bob@2214
   129
            if (NULL == colorcells) {
bob@2214
   130
                SDL_SetError("out of memory in X11_SetDisplayGammaRamp");
bob@2214
   131
                return -1;
bob@2214
   132
            }
bob@2214
   133
bob@2214
   134
            rmax = cmapTable[j].cmap.red_max + 1;
bob@2214
   135
            gmax = cmapTable[j].cmap.blue_max + 1;
bob@2214
   136
            bmax = cmapTable[j].cmap.green_max + 1;
bob@2214
   137
bob@2214
   138
            rmul = cmapTable[j].cmap.red_mult;
bob@2214
   139
            gmul = cmapTable[j].cmap.blue_mult;
bob@2214
   140
            bmul = cmapTable[j].cmap.green_mult;
bob@2214
   141
bob@2214
   142
            /* build the color table pixel values */
bob@2214
   143
            for (i = 0; i < ncolors; i++) {
bob@2214
   144
                Uint32 red = (rmax * i) / ncolors;
bob@2214
   145
                Uint32 green = (gmax * i) / ncolors;
bob@2214
   146
                Uint32 blue = (bmax * i) / ncolors;
bob@2214
   147
bob@2214
   148
                colorcells[i].pixel =
bob@2214
   149
                    (red * rmul) | (green * gmul) | (blue * bmul);
bob@2214
   150
                colorcells[i].flags = DoRed | DoGreen | DoBlue;
bob@2214
   151
bob@2214
   152
                colorcells[i].red = ramp[(0 * 256) + i];
bob@2214
   153
                colorcells[i].green = ramp[(1 * 256) + i];
bob@2214
   154
                colorcells[i].blue = ramp[(2 * 256) + i];
bob@2214
   155
            }
bob@2214
   156
bob@2214
   157
            XStoreColors(display, colormap, colorcells, ncolors);
bob@2214
   158
            XFlush(display);
bob@2214
   159
            SDL_free(colorcells);
bob@2214
   160
        }
bob@2214
   161
    }
bob@2214
   162
bob@2214
   163
    return 0;
bob@2162
   164
}
bob@2162
   165
bob@2162
   166
int
bob@2162
   167
X11_GetDisplayGammaRamp(_THIS, Uint16 * ramp)
bob@2162
   168
{
bob@2214
   169
    Display *display;
bob@2214
   170
    Colormap colormap;
bob@2214
   171
    XColor *colorcells;
bob@2214
   172
    int ncolors;
bob@2214
   173
    int dc;
bob@2214
   174
    int i;
bob@2214
   175
bob@2214
   176
    int rmax, gmax, bmax;
bob@2214
   177
    int rmul, gmul, bmul;
bob@2214
   178
bob@2214
   179
    /* find the first DirectColor colormap and use it to get the gamma
bob@2214
   180
       ramp */
bob@2214
   181
bob@2214
   182
    dc = -1;
bob@2214
   183
    for (i = 0; i < numCmaps; i++) {
bob@2214
   184
        if (cmapTable[i].visual.class == DirectColor) {
bob@2214
   185
            dc = i;
bob@2214
   186
            break;
bob@2214
   187
        }
bob@2214
   188
    }
bob@2214
   189
bob@2214
   190
    if (dc < 0) {
bob@2214
   191
        return -1;
bob@2214
   192
    }
bob@2214
   193
bob@2214
   194
    /* there is at least one DirectColor colormap in the cmapTable,
bob@2214
   195
       let's just get the entries from that colormap */
bob@2214
   196
bob@2214
   197
    display = cmapTable[dc].display;
bob@2214
   198
    colormap = cmapTable[dc].colormap;
bob@2214
   199
    ncolors = cmapTable[dc].visual.map_entries;
bob@2214
   200
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
bob@2214
   201
    if (NULL == colorcells) {
bob@2214
   202
        SDL_SetError("out of memory in X11_GetDisplayGammaRamp");
bob@2214
   203
        return -1;
bob@2214
   204
    }
bob@2214
   205
bob@2214
   206
    rmax = cmapTable[dc].cmap.red_max + 1;
bob@2214
   207
    gmax = cmapTable[dc].cmap.blue_max + 1;
bob@2214
   208
    bmax = cmapTable[dc].cmap.green_max + 1;
bob@2214
   209
bob@2214
   210
    rmul = cmapTable[dc].cmap.red_mult;
bob@2214
   211
    gmul = cmapTable[dc].cmap.blue_mult;
bob@2214
   212
    bmul = cmapTable[dc].cmap.green_mult;
bob@2214
   213
bob@2214
   214
    /* build the color table pixel values */
bob@2214
   215
    for (i = 0; i < ncolors; i++) {
bob@2214
   216
        Uint32 red = (rmax * i) / ncolors;
bob@2214
   217
        Uint32 green = (gmax * i) / ncolors;
bob@2214
   218
        Uint32 blue = (bmax * i) / ncolors;
bob@2214
   219
bob@2214
   220
        colorcells[i].pixel = (red * rmul) | (green * gmul) | (blue * bmul);
bob@2214
   221
    }
bob@2214
   222
bob@2214
   223
    XQueryColors(display, colormap, colorcells, ncolors);
bob@2214
   224
bob@2214
   225
    /* prepare the values to be returned. Note that SDL assumes that
bob@2214
   226
       gamma ramps are always 3 * 256 entries long with the red entries
bob@2214
   227
       in the first 256 elements, the green in the second 256 elements
bob@2214
   228
       and the blue in the last 256 elements */
bob@2214
   229
bob@2214
   230
    for (i = 0; i < ncolors; i++) {
bob@2214
   231
        ramp[(0 * 256) + i] = colorcells[i].red;
bob@2214
   232
        ramp[(1 * 256) + i] = colorcells[i].green;
bob@2214
   233
        ramp[(2 * 256) + i] = colorcells[i].blue;
bob@2214
   234
    }
bob@2214
   235
bob@2214
   236
    SDL_free(colorcells);
bob@2214
   237
    return 0;
bob@2162
   238
}