src/video/x11/SDL_x11gamma.c
author Bob Pendleton <bob@pendleton.com>
Thu, 26 Jul 2007 17:58:17 +0000
changeset 2216 82a133b784c9
parent 2214 e7164a4dac62
child 2221 1d75c38e1e5c
permissions -rw-r--r--
changed to use SDL_realloc()
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
bob@2214
    67
    /* search the table to find out if we already have this one. We
bob@2214
    68
       only want one entry for each display, screen number, visualid,
bob@2214
    69
       and colormap combination */
bob@2213
    70
    for (i = 0; i < numCmaps; i++) {
bob@2213
    71
        if (cmapTable[i].display == display &&
bob@2213
    72
            cmapTable[i].scrNum == scrNum &&
bob@2214
    73
            cmapTable[i].cmap.visualid == cmap->visualid &&
bob@2214
    74
            cmapTable[i].cmap.colormap == colormap) {
bob@2213
    75
            return;
bob@2213
    76
        }
bob@2213
    77
    }
bob@2213
    78
bob@2213
    79
    /* increase the table by one entry. If the table is NULL create the
bob@2213
    80
       first entrty */
bob@2216
    81
    cmapTable = SDL_realloc(cmapTable, (numCmaps + 1) * sizeof(cmapTableEntry));
bob@2216
    82
    if (NULL == cmapTable) {
bob@2213
    83
        SDL_SetError("Out of memory in X11_TrackColormap()");
bob@2213
    84
        return;
bob@2213
    85
    }
bob@2213
    86
bob@2213
    87
    cmapTable[numCmaps].display = display;
bob@2213
    88
    cmapTable[numCmaps].scrNum = scrNum;
bob@2214
    89
    cmapTable[numCmaps].colormap = colormap;
bob@2213
    90
    SDL_memcpy(&cmapTable[numCmaps].cmap, cmap, sizeof(XStandardColormap));
bob@2213
    91
    SDL_memcpy(&cmapTable[numCmaps].visual, visual, sizeof(Visual));
bob@2213
    92
bob@2213
    93
    numCmaps++;
bob@2213
    94
}
bob@2213
    95
bob@2214
    96
/* The problem is that you have to have at least one DirectColor
bob@2214
    97
   colormap before you can set the gamma ramps or read the gamma
bob@2214
    98
   ramps. If the application has created a DirectColor window then the
bob@2214
    99
   cmapTable will have at least one colormap in it and everything is
bob@2214
   100
   cool. If not, then we just fail  */
bob@2214
   101
bob@2162
   102
int
bob@2162
   103
X11_SetDisplayGammaRamp(_THIS, Uint16 * ramp)
bob@2162
   104
{
bob@2214
   105
    Display *display;
bob@2214
   106
    Colormap colormap;
bob@2214
   107
    XColor *colorcells;
bob@2214
   108
    int ncolors;
bob@2214
   109
    int i;
bob@2214
   110
    int j;
bob@2214
   111
bob@2214
   112
    int rmax, gmax, bmax;
bob@2214
   113
    int rmul, gmul, bmul;
bob@2214
   114
bob@2214
   115
    for (j = 0; j < numCmaps; j++) {
bob@2214
   116
        if (cmapTable[j].visual.class == DirectColor) {
bob@2214
   117
            display = cmapTable[j].display;
bob@2214
   118
            colormap = cmapTable[j].colormap;
bob@2214
   119
            ncolors = cmapTable[j].visual.map_entries;
bob@2214
   120
bob@2214
   121
            colorcells = SDL_malloc(ncolors * sizeof(XColor));
bob@2214
   122
            if (NULL == colorcells) {
bob@2214
   123
                SDL_SetError("out of memory in X11_SetDisplayGammaRamp");
bob@2214
   124
                return -1;
bob@2214
   125
            }
bob@2214
   126
bob@2214
   127
            rmax = cmapTable[j].cmap.red_max + 1;
bob@2214
   128
            gmax = cmapTable[j].cmap.blue_max + 1;
bob@2214
   129
            bmax = cmapTable[j].cmap.green_max + 1;
bob@2214
   130
bob@2214
   131
            rmul = cmapTable[j].cmap.red_mult;
bob@2214
   132
            gmul = cmapTable[j].cmap.blue_mult;
bob@2214
   133
            bmul = cmapTable[j].cmap.green_mult;
bob@2214
   134
bob@2214
   135
            /* build the color table pixel values */
bob@2214
   136
            for (i = 0; i < ncolors; i++) {
bob@2214
   137
                Uint32 red = (rmax * i) / ncolors;
bob@2214
   138
                Uint32 green = (gmax * i) / ncolors;
bob@2214
   139
                Uint32 blue = (bmax * i) / ncolors;
bob@2214
   140
bob@2214
   141
                colorcells[i].pixel =
bob@2214
   142
                    (red * rmul) | (green * gmul) | (blue * bmul);
bob@2214
   143
                colorcells[i].flags = DoRed | DoGreen | DoBlue;
bob@2214
   144
bob@2214
   145
                colorcells[i].red = ramp[(0 * 256) + i];
bob@2214
   146
                colorcells[i].green = ramp[(1 * 256) + i];
bob@2214
   147
                colorcells[i].blue = ramp[(2 * 256) + i];
bob@2214
   148
            }
bob@2214
   149
bob@2214
   150
            XStoreColors(display, colormap, colorcells, ncolors);
bob@2214
   151
            XFlush(display);
bob@2214
   152
            SDL_free(colorcells);
bob@2214
   153
        }
bob@2214
   154
    }
bob@2214
   155
bob@2214
   156
    return 0;
bob@2162
   157
}
bob@2162
   158
bob@2162
   159
int
bob@2162
   160
X11_GetDisplayGammaRamp(_THIS, Uint16 * ramp)
bob@2162
   161
{
bob@2214
   162
    Display *display;
bob@2214
   163
    Colormap colormap;
bob@2214
   164
    XColor *colorcells;
bob@2214
   165
    int ncolors;
bob@2214
   166
    int dc;
bob@2214
   167
    int i;
bob@2214
   168
bob@2214
   169
    int rmax, gmax, bmax;
bob@2214
   170
    int rmul, gmul, bmul;
bob@2214
   171
bob@2214
   172
    /* find the first DirectColor colormap and use it to get the gamma
bob@2214
   173
       ramp */
bob@2214
   174
bob@2214
   175
    dc = -1;
bob@2214
   176
    for (i = 0; i < numCmaps; i++) {
bob@2214
   177
        if (cmapTable[i].visual.class == DirectColor) {
bob@2214
   178
            dc = i;
bob@2214
   179
            break;
bob@2214
   180
        }
bob@2214
   181
    }
bob@2214
   182
bob@2214
   183
    if (dc < 0) {
bob@2214
   184
        return -1;
bob@2214
   185
    }
bob@2214
   186
bob@2214
   187
    /* there is at least one DirectColor colormap in the cmapTable,
bob@2214
   188
       let's just get the entries from that colormap */
bob@2214
   189
bob@2214
   190
    display = cmapTable[dc].display;
bob@2214
   191
    colormap = cmapTable[dc].colormap;
bob@2214
   192
    ncolors = cmapTable[dc].visual.map_entries;
bob@2214
   193
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
bob@2214
   194
    if (NULL == colorcells) {
bob@2214
   195
        SDL_SetError("out of memory in X11_GetDisplayGammaRamp");
bob@2214
   196
        return -1;
bob@2214
   197
    }
bob@2214
   198
bob@2214
   199
    rmax = cmapTable[dc].cmap.red_max + 1;
bob@2214
   200
    gmax = cmapTable[dc].cmap.blue_max + 1;
bob@2214
   201
    bmax = cmapTable[dc].cmap.green_max + 1;
bob@2214
   202
bob@2214
   203
    rmul = cmapTable[dc].cmap.red_mult;
bob@2214
   204
    gmul = cmapTable[dc].cmap.blue_mult;
bob@2214
   205
    bmul = cmapTable[dc].cmap.green_mult;
bob@2214
   206
bob@2214
   207
    /* build the color table pixel values */
bob@2214
   208
    for (i = 0; i < ncolors; i++) {
bob@2214
   209
        Uint32 red = (rmax * i) / ncolors;
bob@2214
   210
        Uint32 green = (gmax * i) / ncolors;
bob@2214
   211
        Uint32 blue = (bmax * i) / ncolors;
bob@2214
   212
bob@2214
   213
        colorcells[i].pixel = (red * rmul) | (green * gmul) | (blue * bmul);
bob@2214
   214
    }
bob@2214
   215
bob@2214
   216
    XQueryColors(display, colormap, colorcells, ncolors);
bob@2214
   217
bob@2214
   218
    /* prepare the values to be returned. Note that SDL assumes that
bob@2214
   219
       gamma ramps are always 3 * 256 entries long with the red entries
bob@2214
   220
       in the first 256 elements, the green in the second 256 elements
bob@2214
   221
       and the blue in the last 256 elements */
bob@2214
   222
bob@2214
   223
    for (i = 0; i < ncolors; i++) {
bob@2214
   224
        ramp[(0 * 256) + i] = colorcells[i].red;
bob@2214
   225
        ramp[(1 * 256) + i] = colorcells[i].green;
bob@2214
   226
        ramp[(2 * 256) + i] = colorcells[i].blue;
bob@2214
   227
    }
bob@2214
   228
bob@2214
   229
    SDL_free(colorcells);
bob@2214
   230
    return 0;
bob@2162
   231
}