src/video/SDL_gamma.c
author Edgar Simo <bobbens@gmail.com>
Sun, 06 Jul 2008 17:06:37 +0000
branchgsoc2008_force_feedback
changeset 2498 ab567bd667bf
parent 1895 c121d94672cb
child 2698 e1da92da346c
permissions -rw-r--r--
Fixed various mistakes in the doxygen.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@1330
    26
#ifdef HAVE_MATH_H
slouken@1895
    27
#include <math.h>               /* Used for calculating gamma ramps */
slouken@1334
    28
#else
slouken@1334
    29
/* Math routines from uClibc: http://www.uclibc.org */
slouken@1334
    30
#include "math_private.h"
slouken@1334
    31
#include "e_sqrt.h"
slouken@1334
    32
#include "e_pow.h"
slouken@1334
    33
#include "e_log.h"
slouken@1334
    34
#define pow(x, y)	__ieee754_pow(x, y)
slouken@1334
    35
#define log(x)		__ieee754_log(x)
slouken@0
    36
#endif
slouken@0
    37
slouken@0
    38
#include "SDL_sysvideo.h"
slouken@0
    39
slouken@1330
    40
slouken@1895
    41
static void
slouken@1895
    42
CalculateGammaRamp(float gamma, Uint16 * ramp)
slouken@0
    43
{
slouken@1895
    44
    int i;
slouken@0
    45
slouken@1895
    46
    /* 0.0 gamma is all black */
slouken@1895
    47
    if (gamma <= 0.0f) {
slouken@1895
    48
        for (i = 0; i < 256; ++i) {
slouken@1895
    49
            ramp[i] = 0;
slouken@1895
    50
        }
slouken@1895
    51
        return;
slouken@1895
    52
    } else
slouken@1895
    53
        /* 1.0 gamma is identity */
slouken@1895
    54
    if (gamma == 1.0f) {
slouken@1895
    55
        for (i = 0; i < 256; ++i) {
slouken@1895
    56
            ramp[i] = (i << 8) | i;
slouken@1895
    57
        }
slouken@1895
    58
        return;
slouken@1895
    59
    } else
slouken@1895
    60
        /* Calculate a real gamma ramp */
slouken@1895
    61
    {
slouken@1895
    62
        int value;
slouken@1895
    63
        gamma = 1.0f / gamma;
slouken@1895
    64
        for (i = 0; i < 256; ++i) {
slouken@1895
    65
            value = (int) (pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
slouken@1895
    66
            if (value > 65535) {
slouken@1895
    67
                value = 65535;
slouken@1895
    68
            }
slouken@1895
    69
            ramp[i] = (Uint16) value;
slouken@1895
    70
        }
slouken@1895
    71
    }
slouken@0
    72
}
slouken@1895
    73
static void
slouken@1895
    74
CalculateGammaFromRamp(float *gamma, Uint16 * ramp)
slouken@0
    75
{
slouken@1895
    76
    /* The following is adapted from a post by Garrett Bass on OpenGL
slouken@1895
    77
       Gamedev list, March 4, 2000.
slouken@1895
    78
     */
slouken@1895
    79
    float sum = 0.0f;
slouken@1895
    80
    int i, count = 0;
slouken@0
    81
slouken@1895
    82
    *gamma = 1.0;
slouken@1895
    83
    for (i = 1; i < 256; ++i) {
slouken@1895
    84
        if ((ramp[i] != 0) && (ramp[i] != 65535)) {
slouken@1895
    85
            double B = (double) i / 256.0;
slouken@1895
    86
            double A = ramp[i] / 65535.0;
slouken@1895
    87
            sum += (float) (log(A) / log(B));
slouken@1895
    88
            count++;
slouken@1895
    89
        }
slouken@1895
    90
    }
slouken@1895
    91
    if (count && sum > 0.0f) {
slouken@1895
    92
        *gamma = 1.0f / (sum / count);
slouken@1895
    93
    }
slouken@0
    94
}
slouken@0
    95
slouken@1895
    96
int
slouken@1895
    97
SDL_SetGamma(float red, float green, float blue)
slouken@0
    98
{
slouken@1895
    99
    Uint16 ramp[3][256];
slouken@0
   100
slouken@1895
   101
    CalculateGammaRamp(red, ramp[0]);
slouken@1895
   102
    CalculateGammaRamp(green, ramp[1]);
slouken@1895
   103
    CalculateGammaRamp(blue, ramp[2]);
slouken@1895
   104
slouken@1895
   105
    return SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
   106
}
slouken@0
   107
slouken@0
   108
/* Calculating the gamma by integrating the gamma ramps isn't exact,
slouken@0
   109
   so this function isn't officially supported.
slouken@0
   110
*/
slouken@1895
   111
int
slouken@1895
   112
SDL_GetGamma(float *red, float *green, float *blue)
slouken@0
   113
{
slouken@1895
   114
    int succeeded;
slouken@1895
   115
    Uint16 ramp[3][256];
slouken@0
   116
slouken@1895
   117
    succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@1895
   118
    if (succeeded >= 0) {
slouken@1895
   119
        CalculateGammaFromRamp(red, ramp[0]);
slouken@1895
   120
        CalculateGammaFromRamp(green, ramp[1]);
slouken@1895
   121
        CalculateGammaFromRamp(blue, ramp[2]);
slouken@1895
   122
    }
slouken@1895
   123
    return succeeded;
slouken@0
   124
}
slouken@0
   125
slouken@1895
   126
int
slouken@1895
   127
SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green,
slouken@1895
   128
                 const Uint16 * blue)
slouken@0
   129
{
slouken@1895
   130
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   131
    int succeeded;
slouken@0
   132
slouken@1895
   133
    /* Lazily allocate the gamma tables */
slouken@1895
   134
    if (!SDL_CurrentDisplay.gamma) {
slouken@1895
   135
        SDL_GetGammaRamp(NULL, NULL, NULL);
slouken@1895
   136
    }
slouken@0
   137
slouken@1895
   138
    /* Fill the gamma table with the new values */
slouken@1895
   139
    if (red) {
slouken@1895
   140
        SDL_memcpy(&SDL_CurrentDisplay.gamma[0 * 256], red,
slouken@1895
   141
                   256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@1895
   142
    }
slouken@1895
   143
    if (green) {
slouken@1895
   144
        SDL_memcpy(&SDL_CurrentDisplay.gamma[1 * 256], green,
slouken@1895
   145
                   256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@1895
   146
    }
slouken@1895
   147
    if (blue) {
slouken@1895
   148
        SDL_memcpy(&SDL_CurrentDisplay.gamma[2 * 256], blue,
slouken@1895
   149
                   256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@1895
   150
    }
slouken@0
   151
slouken@1895
   152
    /* Try to set the gamma ramp in the driver */
slouken@1895
   153
    succeeded = -1;
slouken@1895
   154
    if (_this && _this->SetDisplayGammaRamp) {
slouken@1895
   155
        if (SDL_GetFocusWindow()) {
slouken@1895
   156
            succeeded =
slouken@1895
   157
                _this->SetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
slouken@1895
   158
        } else {
slouken@1895
   159
            succeeded = 0;
slouken@1895
   160
        }
slouken@1895
   161
    } else {
slouken@1895
   162
        SDL_SetError("Gamma ramp manipulation not supported");
slouken@1895
   163
    }
slouken@1895
   164
    return succeeded;
slouken@0
   165
}
slouken@0
   166
slouken@1895
   167
int
slouken@1895
   168
SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue)
slouken@0
   169
{
slouken@1895
   170
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1895
   171
slouken@1895
   172
    /* Lazily allocate the gamma table */
slouken@1895
   173
    if (!SDL_CurrentDisplay.gamma) {
slouken@1895
   174
        size_t rampsize = (3 * 256 * sizeof(*SDL_CurrentDisplay.gamma));
slouken@0
   175
slouken@1895
   176
        SDL_CurrentDisplay.gamma = SDL_malloc(rampsize * 2);
slouken@1895
   177
        if (!SDL_CurrentDisplay.gamma) {
slouken@1895
   178
            SDL_OutOfMemory();
slouken@1895
   179
            return -1;
slouken@1895
   180
        }
slouken@1895
   181
        if (_this && _this->GetDisplayGammaRamp) {
slouken@1895
   182
            /* Get the real hardware gamma */
slouken@1895
   183
            _this->GetDisplayGammaRamp(_this, SDL_CurrentDisplay.gamma);
slouken@1895
   184
        } else {
slouken@1895
   185
            /* Assume an identity gamma */
slouken@1895
   186
            int i;
slouken@1895
   187
            for (i = 0; i < 256; ++i) {
slouken@1895
   188
                SDL_CurrentDisplay.gamma[0 * 256 + i] = (i << 8) | i;
slouken@1895
   189
                SDL_CurrentDisplay.gamma[1 * 256 + i] = (i << 8) | i;
slouken@1895
   190
                SDL_CurrentDisplay.gamma[2 * 256 + i] = (i << 8) | i;
slouken@1895
   191
            }
slouken@1895
   192
        }
slouken@1895
   193
        SDL_CurrentDisplay.saved_gamma = SDL_CurrentDisplay.gamma + (3 * 256);
slouken@1895
   194
        SDL_memcpy(SDL_CurrentDisplay.saved_gamma, SDL_CurrentDisplay.gamma,
slouken@1895
   195
                   rampsize);
slouken@1895
   196
    }
slouken@0
   197
slouken@1895
   198
    /* Just copy from our internal table */
slouken@1895
   199
    if (red) {
slouken@1895
   200
        SDL_memcpy(red, &SDL_CurrentDisplay.gamma[0 * 256],
slouken@1895
   201
                   256 * sizeof(*red));
slouken@1895
   202
    }
slouken@1895
   203
    if (green) {
slouken@1895
   204
        SDL_memcpy(green, &SDL_CurrentDisplay.gamma[1 * 256],
slouken@1895
   205
                   256 * sizeof(*green));
slouken@1895
   206
    }
slouken@1895
   207
    if (blue) {
slouken@1895
   208
        SDL_memcpy(blue, &SDL_CurrentDisplay.gamma[2 * 256],
slouken@1895
   209
                   256 * sizeof(*blue));
slouken@1895
   210
    }
slouken@1895
   211
    return 0;
slouken@0
   212
}
slouken@1895
   213
slouken@1895
   214
/* vi: set ts=4 sw=4 expandtab: */