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