src/video/SDL_gamma.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
child 1672 8e754b82cecc
permissions -rw-r--r--
more tweaking indent options
     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     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   100     int succeeded;
   101 
   102     succeeded = -1;
   103     /* Prefer using SetGammaRamp(), as it's more flexible */
   104     {
   105         Uint16 ramp[3][256];
   106 
   107         CalculateGammaRamp(red, ramp[0]);
   108         CalculateGammaRamp(green, ramp[1]);
   109         CalculateGammaRamp(blue, ramp[2]);
   110         succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
   111     }
   112     if ((succeeded < 0) && _this->SetGamma) {
   113         SDL_ClearError();
   114         succeeded = _this->SetGamma(_this, red, green, blue);
   115     }
   116     return succeeded;
   117 }
   118 
   119 /* Calculating the gamma by integrating the gamma ramps isn't exact,
   120    so this function isn't officially supported.
   121 */
   122 int
   123 SDL_GetGamma(float *red, float *green, float *blue)
   124 {
   125     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   126     int succeeded;
   127 
   128     succeeded = -1;
   129     /* Prefer using GetGammaRamp(), as it's more flexible */
   130     {
   131         Uint16 ramp[3][256];
   132 
   133         succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
   134         if (succeeded >= 0) {
   135             CalculateGammaFromRamp(red, ramp[0]);
   136             CalculateGammaFromRamp(green, ramp[1]);
   137             CalculateGammaFromRamp(blue, ramp[2]);
   138         }
   139     }
   140     if ((succeeded < 0) && _this->GetGamma) {
   141         SDL_ClearError();
   142         succeeded = _this->GetGamma(_this, red, green, blue);
   143     }
   144     return succeeded;
   145 }
   146 
   147 int
   148 SDL_SetGammaRamp(const Uint16 * red, const Uint16 * green,
   149                  const Uint16 * blue)
   150 {
   151     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   152     int succeeded;
   153     SDL_Surface *screen = SDL_PublicSurface;
   154 
   155     /* Verify the screen parameter */
   156     if (!screen) {
   157         SDL_SetError("No video mode has been set");
   158         return -1;
   159     }
   160 
   161     /* Lazily allocate the gamma tables */
   162     if (!SDL_CurrentWindow.gamma) {
   163         SDL_GetGammaRamp(0, 0, 0);
   164     }
   165 
   166     /* Fill the gamma table with the new values */
   167     if (red) {
   168         SDL_memcpy(&SDL_CurrentWindow.gamma[0 * 256], red,
   169                    256 * sizeof(*SDL_CurrentWindow.gamma));
   170     }
   171     if (green) {
   172         SDL_memcpy(&SDL_CurrentWindow.gamma[1 * 256], green,
   173                    256 * sizeof(*SDL_CurrentWindow.gamma));
   174     }
   175     if (blue) {
   176         SDL_memcpy(&SDL_CurrentWindow.gamma[2 * 256], blue,
   177                    256 * sizeof(*SDL_CurrentWindow.gamma));
   178     }
   179 
   180     /* Try to set the gamma ramp in the driver */
   181     succeeded = -1;
   182     if (_this->SetGammaRamp) {
   183         succeeded = _this->SetGammaRamp(_this, SDL_CurrentWindow.gamma);
   184     } else {
   185         SDL_SetError("Gamma ramp manipulation not supported");
   186     }
   187     return succeeded;
   188 }
   189 
   190 int
   191 SDL_GetGammaRamp(Uint16 * red, Uint16 * green, Uint16 * blue)
   192 {
   193     SDL_VideoDevice *_this = SDL_GetVideoDevice();
   194 
   195     /* Lazily allocate the gamma table */
   196     if (!SDL_CurrentWindow.gamma) {
   197         SDL_CurrentWindow.gamma =
   198             SDL_malloc(3 * 256 * sizeof(*SDL_CurrentWindow.gamma));
   199         if (!SDL_CurrentWindow.gamma) {
   200             SDL_OutOfMemory();
   201             return -1;
   202         }
   203         if (_this->GetGammaRamp) {
   204             /* Get the real hardware gamma */
   205             _this->GetGammaRamp(_this, SDL_CurrentWindow.gamma);
   206         } else {
   207             /* Assume an identity gamma */
   208             int i;
   209             for (i = 0; i < 256; ++i) {
   210                 SDL_CurrentWindow.gamma[0 * 256 + i] = (i << 8) | i;
   211                 SDL_CurrentWindow.gamma[1 * 256 + i] = (i << 8) | i;
   212                 SDL_CurrentWindow.gamma[2 * 256 + i] = (i << 8) | i;
   213             }
   214         }
   215     }
   216 
   217     /* Just copy from our internal table */
   218     if (red) {
   219         SDL_memcpy(red, &SDL_CurrentWindow.gamma[0 * 256],
   220                    256 * sizeof(*red));
   221     }
   222     if (green) {
   223         SDL_memcpy(green, &SDL_CurrentWindow.gamma[1 * 256],
   224                    256 * sizeof(*green));
   225     }
   226     if (blue) {
   227         SDL_memcpy(blue, &SDL_CurrentWindow.gamma[2 * 256],
   228                    256 * sizeof(*blue));
   229     }
   230     return 0;
   231 }
   232 
   233 /* vi: set ts=4 sw=4 expandtab: */