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