src/video/SDL_gamma.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3500 4b594623401b
child 3685 64ce267332c6
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
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@0
    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@3500
   116
static void
slouken@3500
   117
SDL_UninitializedVideo()
slouken@3500
   118
{
slouken@3500
   119
    SDL_SetError("Video subsystem has not been initialized");
slouken@3500
   120
}
slouken@3500
   121
slouken@1895
   122
int
slouken@3500
   123
SDL_SetGammaRampForDisplay(SDL_VideoDisplay * display, const Uint16 * red, const Uint16 * green, const Uint16 * blue)
slouken@0
   124
{
slouken@1895
   125
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   126
    int succeeded;
slouken@0
   127
slouken@3500
   128
    if (!_this) {
slouken@3500
   129
        SDL_UninitializedVideo();
slouken@3500
   130
        return -1;
slouken@3500
   131
    }
slouken@3500
   132
slouken@1895
   133
    /* Lazily allocate the gamma tables */
slouken@3500
   134
    if (!display->gamma) {
slouken@3500
   135
        if (SDL_GetGammaRampForDisplay(display, NULL, NULL, NULL) < 0) {
slouken@3500
   136
            return -1;
slouken@3500
   137
        }
slouken@1895
   138
    }
slouken@0
   139
slouken@1895
   140
    /* Fill the gamma table with the new values */
slouken@1895
   141
    if (red) {
slouken@3500
   142
        SDL_memcpy(&display->gamma[0 * 256], red, 256 * sizeof(*display->gamma));
slouken@1895
   143
    }
slouken@1895
   144
    if (green) {
slouken@3500
   145
        SDL_memcpy(&display->gamma[1 * 256], green, 256 * sizeof(*display->gamma));
slouken@1895
   146
    }
slouken@1895
   147
    if (blue) {
slouken@3500
   148
        SDL_memcpy(&display->gamma[2 * 256], blue, 256 * sizeof(*display->gamma));
slouken@1895
   149
    }
slouken@0
   150
slouken@1895
   151
    /* Try to set the gamma ramp in the driver */
slouken@1895
   152
    succeeded = -1;
slouken@1895
   153
    if (_this && _this->SetDisplayGammaRamp) {
slouken@1895
   154
        if (SDL_GetFocusWindow()) {
slouken@1895
   155
            succeeded =
slouken@3500
   156
                _this->SetDisplayGammaRamp(_this, display, display->gamma);
slouken@1895
   157
        } else {
slouken@1895
   158
            succeeded = 0;
slouken@1895
   159
        }
slouken@1895
   160
    } else {
slouken@1895
   161
        SDL_SetError("Gamma ramp manipulation not supported");
slouken@1895
   162
    }
slouken@1895
   163
    return succeeded;
slouken@0
   164
}
slouken@0
   165
slouken@1895
   166
int
slouken@3500
   167
SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green, const Uint16 * blue)
slouken@3500
   168
{
slouken@3500
   169
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@3500
   170
    if (!_this) {
slouken@3500
   171
        SDL_UninitializedVideo();
slouken@3500
   172
        return -1;
slouken@3500
   173
    }
slouken@3500
   174
    return SDL_SetGammaRampForDisplay(&SDL_CurrentDisplay, red, green, blue);
slouken@3500
   175
}
slouken@3500
   176
slouken@3500
   177
int
slouken@3500
   178
SDL_GetGammaRampForDisplay(SDL_VideoDisplay * display, Uint16 * red, Uint16 * green, Uint16 * blue)
slouken@0
   179
{
slouken@1895
   180
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@0
   181
slouken@3500
   182
    if (!_this) {
slouken@3500
   183
        SDL_UninitializedVideo();
slouken@3500
   184
        return -1;
slouken@3500
   185
    }
slouken@3500
   186
slouken@1895
   187
    /* Lazily allocate the gamma table */
slouken@3500
   188
    if (!display->gamma) {
slouken@3500
   189
        size_t rampsize = (3 * 256 * sizeof(*display->gamma));
slouken@0
   190
slouken@3500
   191
        display->gamma = SDL_malloc(rampsize * 2);
slouken@3500
   192
        if (!display->gamma) {
slouken@1895
   193
            SDL_OutOfMemory();
slouken@1895
   194
            return -1;
slouken@1895
   195
        }
slouken@1895
   196
        if (_this && _this->GetDisplayGammaRamp) {
slouken@1895
   197
            /* Get the real hardware gamma */
slouken@3500
   198
            _this->GetDisplayGammaRamp(_this, display, display->gamma);
slouken@1895
   199
        } else {
slouken@1895
   200
            /* Assume an identity gamma */
slouken@1895
   201
            int i;
slouken@1895
   202
            for (i = 0; i < 256; ++i) {
slouken@3500
   203
                display->gamma[0 * 256 + i] = (i << 8) | i;
slouken@3500
   204
                display->gamma[1 * 256 + i] = (i << 8) | i;
slouken@3500
   205
                display->gamma[2 * 256 + i] = (i << 8) | i;
slouken@1895
   206
            }
slouken@1895
   207
        }
slouken@3500
   208
        display->saved_gamma = display->gamma + (3 * 256);
slouken@3500
   209
        SDL_memcpy(display->saved_gamma, display->gamma, rampsize);
slouken@1895
   210
    }
slouken@1895
   211
slouken@1895
   212
    /* Just copy from our internal table */
slouken@1895
   213
    if (red) {
slouken@3500
   214
        SDL_memcpy(red, &display->gamma[0 * 256], 256 * sizeof(*red));
slouken@1895
   215
    }
slouken@1895
   216
    if (green) {
slouken@3500
   217
        SDL_memcpy(green, &display->gamma[1 * 256], 256 * sizeof(*green));
slouken@1895
   218
    }
slouken@1895
   219
    if (blue) {
slouken@3500
   220
        SDL_memcpy(blue, &display->gamma[2 * 256], 256 * sizeof(*blue));
slouken@1895
   221
    }
slouken@1895
   222
    return 0;
slouken@0
   223
}
slouken@1895
   224
slouken@3500
   225
int
slouken@3500
   226
SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue)
slouken@3500
   227
{
slouken@3500
   228
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@3500
   229
    if (!_this) {
slouken@3500
   230
        SDL_UninitializedVideo();
slouken@3500
   231
        return -1;
slouken@3500
   232
    }
slouken@3500
   233
    return SDL_GetGammaRampForDisplay(&SDL_CurrentDisplay, red, green, blue);
slouken@3500
   234
}
slouken@3500
   235
slouken@1895
   236
/* vi: set ts=4 sw=4 expandtab: */