src/video/SDL_gamma.c
author Sam Lantinga
Sat, 19 Sep 2009 13:29:40 +0000
changeset 3280 00cace2d9080
parent 3040 62d4992e5a92
child 3500 4b594623401b
permissions -rw-r--r--
Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Gamma correction support */
slouken@0
    25
slouken@0
    26
#include "SDL_sysvideo.h"
slouken@0
    27
slouken@1330
    28
slouken@1895
    29
static void
slouken@1895
    30
CalculateGammaRamp(float gamma, Uint16 * ramp)
slouken@0
    31
{
slouken@1895
    32
    int i;
slouken@0
    33
slouken@1895
    34
    /* 0.0 gamma is all black */
slouken@1895
    35
    if (gamma <= 0.0f) {
slouken@1895
    36
        for (i = 0; i < 256; ++i) {
slouken@1895
    37
            ramp[i] = 0;
slouken@1895
    38
        }
slouken@1895
    39
        return;
slouken@1895
    40
    } else
slouken@1895
    41
        /* 1.0 gamma is identity */
slouken@1895
    42
    if (gamma == 1.0f) {
slouken@1895
    43
        for (i = 0; i < 256; ++i) {
slouken@1895
    44
            ramp[i] = (i << 8) | i;
slouken@1895
    45
        }
slouken@1895
    46
        return;
slouken@1895
    47
    } else
slouken@1895
    48
        /* Calculate a real gamma ramp */
slouken@1895
    49
    {
slouken@1895
    50
        int value;
slouken@1895
    51
        gamma = 1.0f / gamma;
slouken@1895
    52
        for (i = 0; i < 256; ++i) {
slouken@3040
    53
            value =
slouken@3040
    54
                (int) (SDL_pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
slouken@1895
    55
            if (value > 65535) {
slouken@1895
    56
                value = 65535;
slouken@1895
    57
            }
slouken@1895
    58
            ramp[i] = (Uint16) value;
slouken@1895
    59
        }
slouken@1895
    60
    }
slouken@0
    61
}
slouken@2735
    62
slouken@1895
    63
static void
slouken@1895
    64
CalculateGammaFromRamp(float *gamma, Uint16 * ramp)
slouken@0
    65
{
slouken@1895
    66
    /* The following is adapted from a post by Garrett Bass on OpenGL
slouken@1895
    67
       Gamedev list, March 4, 2000.
slouken@1895
    68
     */
slouken@1895
    69
    float sum = 0.0f;
slouken@1895
    70
    int i, count = 0;
slouken@0
    71
slouken@1895
    72
    *gamma = 1.0;
slouken@1895
    73
    for (i = 1; i < 256; ++i) {
slouken@1895
    74
        if ((ramp[i] != 0) && (ramp[i] != 65535)) {
slouken@1895
    75
            double B = (double) i / 256.0;
slouken@1895
    76
            double A = ramp[i] / 65535.0;
slouken@3040
    77
            sum += (float) (SDL_log(A) / SDL_log(B));
slouken@1895
    78
            count++;
slouken@1895
    79
        }
slouken@1895
    80
    }
slouken@1895
    81
    if (count && sum > 0.0f) {
slouken@1895
    82
        *gamma = 1.0f / (sum / count);
slouken@1895
    83
    }
slouken@0
    84
}
slouken@0
    85
slouken@1895
    86
int
slouken@1895
    87
SDL_SetGamma(float red, float green, float blue)
slouken@0
    88
{
slouken@1895
    89
    Uint16 ramp[3][256];
slouken@0
    90
slouken@1895
    91
    CalculateGammaRamp(red, ramp[0]);
slouken@1895
    92
    CalculateGammaRamp(green, ramp[1]);
slouken@1895
    93
    CalculateGammaRamp(blue, ramp[2]);
slouken@1895
    94
slouken@1895
    95
    return SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
    96
}
slouken@0
    97
slouken@0
    98
/* Calculating the gamma by integrating the gamma ramps isn't exact,
slouken@0
    99
   so this function isn't officially supported.
slouken@0
   100
*/
slouken@1895
   101
int
slouken@1895
   102
SDL_GetGamma(float *red, float *green, float *blue)
slouken@0
   103
{
slouken@1895
   104
    int succeeded;
slouken@1895
   105
    Uint16 ramp[3][256];
slouken@0
   106
slouken@1895
   107
    succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@1895
   108
    if (succeeded >= 0) {
slouken@1895
   109
        CalculateGammaFromRamp(red, ramp[0]);
slouken@1895
   110
        CalculateGammaFromRamp(green, ramp[1]);
slouken@1895
   111
        CalculateGammaFromRamp(blue, ramp[2]);
slouken@1895
   112
    }
slouken@1895
   113
    return succeeded;
slouken@0
   114
}
slouken@0
   115
slouken@1895
   116
int
slouken@1895
   117
SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green,
slouken@1895
   118
                 const Uint16 * blue)
slouken@0
   119
{
slouken@1895
   120
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   121
    int succeeded;
slouken@0
   122
slouken@1895
   123
    /* Lazily allocate the gamma tables */
slouken@1895
   124
    if (!SDL_CurrentDisplay.gamma) {
slouken@1895
   125
        SDL_GetGammaRamp(NULL, NULL, NULL);
slouken@1895
   126
    }
slouken@0
   127
slouken@1895
   128
    /* Fill the gamma table with the new values */
slouken@1895
   129
    if (red) {
slouken@1895
   130
        SDL_memcpy(&SDL_CurrentDisplay.gamma[0 * 256], red,
slouken@1895
   131
                   256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@1895
   132
    }
slouken@1895
   133
    if (green) {
slouken@1895
   134
        SDL_memcpy(&SDL_CurrentDisplay.gamma[1 * 256], green,
slouken@1895
   135
                   256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@1895
   136
    }
slouken@1895
   137
    if (blue) {
slouken@1895
   138
        SDL_memcpy(&SDL_CurrentDisplay.gamma[2 * 256], blue,
slouken@1895
   139
                   256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@1895
   140
    }
slouken@0
   141
slouken@1895
   142
    /* Try to set the gamma ramp in the driver */
slouken@1895
   143
    succeeded = -1;
slouken@1895
   144
    if (_this && _this->SetDisplayGammaRamp) {
slouken@1895
   145
        if (SDL_GetFocusWindow()) {
slouken@1895
   146
            succeeded =
slouken@1895
   147
                _this->SetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
slouken@1895
   148
        } else {
slouken@1895
   149
            succeeded = 0;
slouken@1895
   150
        }
slouken@1895
   151
    } else {
slouken@1895
   152
        SDL_SetError("Gamma ramp manipulation not supported");
slouken@1895
   153
    }
slouken@1895
   154
    return succeeded;
slouken@0
   155
}
slouken@0
   156
slouken@1895
   157
int
slouken@1895
   158
SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue)
slouken@0
   159
{
slouken@1895
   160
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   161
slouken@1895
   162
    /* Lazily allocate the gamma table */
slouken@1895
   163
    if (!SDL_CurrentDisplay.gamma) {
slouken@1895
   164
        size_t rampsize = (3 * 256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@0
   165
slouken@1895
   166
        SDL_CurrentDisplay.gamma = SDL_malloc(rampsize * 2);
slouken@1895
   167
        if (!SDL_CurrentDisplay.gamma) {
slouken@1895
   168
            SDL_OutOfMemory();
slouken@1895
   169
            return -1;
slouken@1895
   170
        }
slouken@1895
   171
        if (_this && _this->GetDisplayGammaRamp) {
slouken@1895
   172
            /* Get the real hardware gamma */
slouken@1895
   173
            _this->GetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
slouken@1895
   174
        } else {
slouken@1895
   175
            /* Assume an identity gamma */
slouken@1895
   176
            int i;
slouken@1895
   177
            for (i = 0; i < 256; ++i) {
slouken@1895
   178
                SDL_CurrentDisplay.gamma[0 * 256 + i] = (i << 8) | i;
slouken@1895
   179
                SDL_CurrentDisplay.gamma[1 * 256 + i] = (i << 8) | i;
slouken@1895
   180
                SDL_CurrentDisplay.gamma[2 * 256 + i] = (i << 8) | i;
slouken@1895
   181
            }
slouken@1895
   182
        }
slouken@1895
   183
        SDL_CurrentDisplay.saved_gamma = SDL_CurrentDisplay.gamma + (3 * 256);
slouken@1895
   184
        SDL_memcpy(SDL_CurrentDisplay.saved_gamma, SDL_CurrentDisplay.gamma,
slouken@1895
   185
                   rampsize);
slouken@1895
   186
    }
slouken@0
   187
slouken@1895
   188
    /* Just copy from our internal table */
slouken@1895
   189
    if (red) {
slouken@1895
   190
        SDL_memcpy(red, &SDL_CurrentDisplay.gamma[0 * 256],
slouken@1895
   191
                   256 * sizeof(*red));
slouken@1895
   192
    }
slouken@1895
   193
    if (green) {
slouken@1895
   194
        SDL_memcpy(green, &SDL_CurrentDisplay.gamma[1 * 256],
slouken@1895
   195
                   256 * sizeof(*green));
slouken@1895
   196
    }
slouken@1895
   197
    if (blue) {
slouken@1895
   198
        SDL_memcpy(blue, &SDL_CurrentDisplay.gamma[2 * 256],
slouken@1895
   199
                   256 * sizeof(*blue));
slouken@1895
   200
    }
slouken@1895
   201
    return 0;
slouken@0
   202
}
slouken@1895
   203
slouken@1895
   204
/* vi: set ts=4 sw=4 expandtab: */