src/video/SDL_gamma.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 6137 4720145f848b
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

also fix a changelog date.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 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@1330
    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@0
    41
static void CalculateGammaRamp(float gamma, Uint16 *ramp)
slouken@0
    42
{
slouken@0
    43
	int i;
slouken@0
    44
slouken@0
    45
	/* 0.0 gamma is all black */
slouken@1612
    46
	if ( gamma <= 0.0f ) {
slouken@0
    47
		for ( i=0; i<256; ++i ) {
slouken@0
    48
			ramp[i] = 0;
slouken@0
    49
		}
slouken@0
    50
		return;
slouken@0
    51
	} else
slouken@0
    52
	/* 1.0 gamma is identity */
slouken@1869
    53
	if ( gamma == 1.0f ) {
slouken@0
    54
		for ( i=0; i<256; ++i ) {
slouken@0
    55
			ramp[i] = (i << 8) | i;
slouken@0
    56
		}
slouken@0
    57
		return;
slouken@0
    58
	} else
slouken@0
    59
	/* Calculate a real gamma ramp */
slouken@0
    60
	{ int value;
slouken@0
    61
		gamma = 1.0f / gamma;
slouken@0
    62
		for ( i=0; i<256; ++i ) {
slouken@0
    63
			value = (int)(pow((double)i/256.0, gamma)*65535.0+0.5);
slouken@0
    64
			if ( value > 65535 ) {
slouken@0
    65
				value = 65535;
slouken@0
    66
			}
slouken@0
    67
			ramp[i] = (Uint16)value;
slouken@0
    68
		}
slouken@0
    69
	}
slouken@0
    70
}
slouken@0
    71
static void CalculateGammaFromRamp(float *gamma, Uint16 *ramp)
slouken@0
    72
{
slouken@0
    73
	/* The following is adapted from a post by Garrett Bass on OpenGL
slouken@0
    74
	   Gamedev list, March 4, 2000.
slouken@0
    75
	 */
slouken@1612
    76
	float sum = 0.0f;
slouken@0
    77
	int i, count = 0;
slouken@0
    78
slouken@0
    79
	*gamma = 1.0;
slouken@0
    80
	for ( i = 1; i < 256; ++i ) {
slouken@0
    81
	    if ( (ramp[i] != 0) && (ramp[i] != 65535) ) {
slouken@0
    82
	        double B = (double)i / 256.0;
slouken@0
    83
	        double A = ramp[i] / 65535.0;
slouken@0
    84
	        sum += (float) ( log(A) / log(B) );
slouken@0
    85
	        count++;
slouken@0
    86
	    }
slouken@0
    87
	}
slouken@1612
    88
	if ( count && sum > 0.0f ) {
slouken@0
    89
		*gamma = 1.0f / (sum / count);
slouken@0
    90
	}
slouken@0
    91
}
slouken@0
    92
slouken@0
    93
int SDL_SetGamma(float red, float green, float blue)
slouken@0
    94
{
slouken@0
    95
	int succeeded;
slouken@0
    96
	SDL_VideoDevice *video = current_video;
slouken@0
    97
	SDL_VideoDevice *this  = current_video;	
slouken@0
    98
slouken@0
    99
	succeeded = -1;
slouken@0
   100
	/* Prefer using SetGammaRamp(), as it's more flexible */
slouken@0
   101
	{
slouken@0
   102
		Uint16 ramp[3][256];
slouken@0
   103
slouken@0
   104
		CalculateGammaRamp(red, ramp[0]);
slouken@0
   105
		CalculateGammaRamp(green, ramp[1]);
slouken@0
   106
		CalculateGammaRamp(blue, ramp[2]);
slouken@0
   107
		succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
   108
	}
slouken@0
   109
	if ( (succeeded < 0) && video->SetGamma ) {
slouken@0
   110
		SDL_ClearError();
slouken@0
   111
		succeeded = video->SetGamma(this, red, green, blue);
slouken@0
   112
	}
slouken@0
   113
	return succeeded;
slouken@0
   114
}
slouken@0
   115
slouken@0
   116
/* Calculating the gamma by integrating the gamma ramps isn't exact,
slouken@0
   117
   so this function isn't officially supported.
slouken@0
   118
*/
slouken@0
   119
int SDL_GetGamma(float *red, float *green, float *blue)
slouken@0
   120
{
slouken@0
   121
	int succeeded;
slouken@0
   122
	SDL_VideoDevice *video = current_video;
slouken@0
   123
	SDL_VideoDevice *this  = current_video;	
slouken@0
   124
slouken@0
   125
	succeeded = -1;
slouken@0
   126
	/* Prefer using GetGammaRamp(), as it's more flexible */
slouken@0
   127
	{
slouken@0
   128
		Uint16 ramp[3][256];
slouken@0
   129
slouken@0
   130
		succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
   131
		if ( succeeded >= 0 ) {
slouken@0
   132
			CalculateGammaFromRamp(red, ramp[0]);
slouken@0
   133
			CalculateGammaFromRamp(green, ramp[1]);
slouken@0
   134
			CalculateGammaFromRamp(blue, ramp[2]);
slouken@0
   135
		}
slouken@0
   136
	}
slouken@0
   137
	if ( (succeeded < 0) && video->GetGamma ) {
slouken@0
   138
		SDL_ClearError();
slouken@0
   139
		succeeded = video->GetGamma(this, red, green, blue);
slouken@0
   140
	}
slouken@0
   141
	return succeeded;
slouken@0
   142
}
slouken@0
   143
slouken@669
   144
int SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue)
slouken@0
   145
{
slouken@0
   146
	int succeeded;
slouken@0
   147
	SDL_VideoDevice *video = current_video;
slouken@0
   148
	SDL_VideoDevice *this  = current_video;	
slouken@0
   149
	SDL_Surface *screen = SDL_PublicSurface;
slouken@0
   150
slouken@0
   151
	/* Verify the screen parameter */
slouken@0
   152
	if ( !screen ) {
slouken@0
   153
		SDL_SetError("No video mode has been set");
slouken@0
   154
		return -1;
slouken@0
   155
	}
slouken@0
   156
slouken@0
   157
	/* Lazily allocate the gamma tables */
slouken@0
   158
	if ( ! video->gamma ) {
slouken@0
   159
		SDL_GetGammaRamp(0, 0, 0);
slouken@0
   160
	}
slouken@0
   161
slouken@0
   162
	/* Fill the gamma table with the new values */
slouken@0
   163
	if ( red ) {
slouken@1336
   164
		SDL_memcpy(&video->gamma[0*256], red, 256*sizeof(*video->gamma));
slouken@0
   165
	}
slouken@0
   166
	if ( green ) {
slouken@1336
   167
		SDL_memcpy(&video->gamma[1*256], green, 256*sizeof(*video->gamma));
slouken@0
   168
	}
slouken@0
   169
	if ( blue ) {
slouken@1336
   170
		SDL_memcpy(&video->gamma[2*256], blue, 256*sizeof(*video->gamma));
slouken@0
   171
	}
slouken@0
   172
slouken@0
   173
	/* Gamma correction always possible on split palettes */
slouken@0
   174
	if ( (screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) {
slouken@0
   175
		SDL_Palette *pal = screen->format->palette;
slouken@0
   176
slouken@0
   177
		/* If physical palette has been set independently, use it */
slouken@0
   178
		if(video->physpal)
slouken@0
   179
		        pal = video->physpal;
slouken@0
   180
		      
slouken@0
   181
		SDL_SetPalette(screen, SDL_PHYSPAL,
slouken@0
   182
			       pal->colors, 0, pal->ncolors);
slouken@0
   183
		return 0;
slouken@0
   184
	}
slouken@0
   185
slouken@0
   186
	/* Try to set the gamma ramp in the driver */
slouken@0
   187
	succeeded = -1;
slouken@0
   188
	if ( video->SetGammaRamp ) {
slouken@0
   189
		succeeded = video->SetGammaRamp(this, video->gamma);
slouken@0
   190
	} else {
slouken@0
   191
		SDL_SetError("Gamma ramp manipulation not supported");
slouken@0
   192
	}
slouken@0
   193
	return succeeded;
slouken@0
   194
}
slouken@0
   195
slouken@0
   196
int SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
slouken@0
   197
{
slouken@0
   198
	SDL_VideoDevice *video = current_video;
slouken@0
   199
	SDL_VideoDevice *this  = current_video;	
slouken@0
   200
slouken@0
   201
	/* Lazily allocate the gamma table */
slouken@0
   202
	if ( ! video->gamma ) {
slouken@1336
   203
		video->gamma = SDL_malloc(3*256*sizeof(*video->gamma));
slouken@0
   204
		if ( ! video->gamma ) {
slouken@0
   205
			SDL_OutOfMemory();
slouken@0
   206
			return -1;
slouken@0
   207
		}
slouken@0
   208
		if ( video->GetGammaRamp ) {
slouken@0
   209
			/* Get the real hardware gamma */
slouken@0
   210
			video->GetGammaRamp(this, video->gamma);
slouken@0
   211
		} else {
slouken@0
   212
			/* Assume an identity gamma */
slouken@0
   213
			int i;
slouken@0
   214
			for ( i=0; i<256; ++i ) {
slouken@0
   215
				video->gamma[0*256+i] = (i << 8) | i;
slouken@0
   216
				video->gamma[1*256+i] = (i << 8) | i;
slouken@0
   217
				video->gamma[2*256+i] = (i << 8) | i;
slouken@0
   218
			}
slouken@0
   219
		}
slouken@0
   220
	}
slouken@0
   221
slouken@0
   222
	/* Just copy from our internal table */
slouken@0
   223
	if ( red ) {
slouken@1336
   224
		SDL_memcpy(red, &video->gamma[0*256], 256*sizeof(*red));
slouken@0
   225
	}
slouken@0
   226
	if ( green ) {
slouken@1336
   227
		SDL_memcpy(green, &video->gamma[1*256], 256*sizeof(*green));
slouken@0
   228
	}
slouken@0
   229
	if ( blue ) {
slouken@1336
   230
		SDL_memcpy(blue, &video->gamma[2*256], 256*sizeof(*blue));
slouken@0
   231
	}
slouken@0
   232
	return 0;
slouken@0
   233
}