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