src/video/SDL_gamma.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 769 b8d311d90021
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@0
    22
slouken@0
    23
/* Gamma correction support */
slouken@0
    24
slouken@0
    25
#define USE_MATH_H	/* Used for calculating gamma ramps */
slouken@0
    26
slouken@0
    27
#ifdef USE_MATH_H
slouken@0
    28
#include <math.h>
slouken@0
    29
#endif
slouken@0
    30
#include <stdlib.h>
slouken@152
    31
#include <string.h>
slouken@0
    32
slouken@0
    33
#include "SDL_error.h"
slouken@0
    34
#include "SDL_sysvideo.h"
slouken@0
    35
slouken@0
    36
#ifdef USE_MATH_H
slouken@0
    37
static void CalculateGammaRamp(float gamma, Uint16 *ramp)
slouken@0
    38
{
slouken@0
    39
	int i;
slouken@0
    40
slouken@0
    41
	/* 0.0 gamma is all black */
slouken@0
    42
	if ( gamma <= 0.0 ) {
slouken@0
    43
		for ( i=0; i<256; ++i ) {
slouken@0
    44
			ramp[i] = 0;
slouken@0
    45
		}
slouken@0
    46
		return;
slouken@0
    47
	} else
slouken@0
    48
	/* 1.0 gamma is identity */
slouken@0
    49
	if ( gamma == 1.0 ) {
slouken@0
    50
		for ( i=0; i<256; ++i ) {
slouken@0
    51
			ramp[i] = (i << 8) | i;
slouken@0
    52
		}
slouken@0
    53
		return;
slouken@0
    54
	} else
slouken@0
    55
	/* Calculate a real gamma ramp */
slouken@0
    56
	{ int value;
slouken@0
    57
		gamma = 1.0f / gamma;
slouken@0
    58
		for ( i=0; i<256; ++i ) {
slouken@0
    59
			value = (int)(pow((double)i/256.0, gamma)*65535.0+0.5);
slouken@0
    60
			if ( value > 65535 ) {
slouken@0
    61
				value = 65535;
slouken@0
    62
			}
slouken@0
    63
			ramp[i] = (Uint16)value;
slouken@0
    64
		}
slouken@0
    65
	}
slouken@0
    66
}
slouken@0
    67
static void CalculateGammaFromRamp(float *gamma, Uint16 *ramp)
slouken@0
    68
{
slouken@0
    69
	/* The following is adapted from a post by Garrett Bass on OpenGL
slouken@0
    70
	   Gamedev list, March 4, 2000.
slouken@0
    71
	 */
slouken@0
    72
	float sum = 0.0;
slouken@0
    73
	int i, count = 0;
slouken@0
    74
slouken@0
    75
	*gamma = 1.0;
slouken@0
    76
	for ( i = 1; i < 256; ++i ) {
slouken@0
    77
	    if ( (ramp[i] != 0) && (ramp[i] != 65535) ) {
slouken@0
    78
	        double B = (double)i / 256.0;
slouken@0
    79
	        double A = ramp[i] / 65535.0;
slouken@0
    80
	        sum += (float) ( log(A) / log(B) );
slouken@0
    81
	        count++;
slouken@0
    82
	    }
slouken@0
    83
	}
slouken@0
    84
	if ( count && sum ) {
slouken@0
    85
		*gamma = 1.0f / (sum / count);
slouken@0
    86
	}
slouken@0
    87
}
slouken@0
    88
#endif /* USE_MATH_H */
slouken@0
    89
slouken@0
    90
int SDL_SetGamma(float red, float green, float blue)
slouken@0
    91
{
slouken@0
    92
	int succeeded;
slouken@0
    93
	SDL_VideoDevice *video = current_video;
slouken@0
    94
	SDL_VideoDevice *this  = current_video;	
slouken@0
    95
slouken@0
    96
	succeeded = -1;
slouken@0
    97
#ifdef USE_MATH_H
slouken@0
    98
	/* Prefer using SetGammaRamp(), as it's more flexible */
slouken@0
    99
	{
slouken@0
   100
		Uint16 ramp[3][256];
slouken@0
   101
slouken@0
   102
		CalculateGammaRamp(red, ramp[0]);
slouken@0
   103
		CalculateGammaRamp(green, ramp[1]);
slouken@0
   104
		CalculateGammaRamp(blue, ramp[2]);
slouken@0
   105
		succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
   106
	}
slouken@0
   107
#else
slouken@0
   108
	SDL_SetError("Gamma correction not supported");
slouken@0
   109
#endif
slouken@0
   110
	if ( (succeeded < 0) && video->SetGamma ) {
slouken@0
   111
		SDL_ClearError();
slouken@0
   112
		succeeded = video->SetGamma(this, red, green, blue);
slouken@0
   113
	}
slouken@0
   114
	return succeeded;
slouken@0
   115
}
slouken@0
   116
slouken@0
   117
/* Calculating the gamma by integrating the gamma ramps isn't exact,
slouken@0
   118
   so this function isn't officially supported.
slouken@0
   119
*/
slouken@0
   120
int SDL_GetGamma(float *red, float *green, float *blue)
slouken@0
   121
{
slouken@0
   122
	int succeeded;
slouken@0
   123
	SDL_VideoDevice *video = current_video;
slouken@0
   124
	SDL_VideoDevice *this  = current_video;	
slouken@0
   125
slouken@0
   126
	succeeded = -1;
slouken@0
   127
#ifdef USE_MATH_H
slouken@0
   128
	/* Prefer using GetGammaRamp(), as it's more flexible */
slouken@0
   129
	{
slouken@0
   130
		Uint16 ramp[3][256];
slouken@0
   131
slouken@0
   132
		succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
   133
		if ( succeeded >= 0 ) {
slouken@0
   134
			CalculateGammaFromRamp(red, ramp[0]);
slouken@0
   135
			CalculateGammaFromRamp(green, ramp[1]);
slouken@0
   136
			CalculateGammaFromRamp(blue, ramp[2]);
slouken@0
   137
		}
slouken@0
   138
	}
slouken@0
   139
#else
slouken@0
   140
	SDL_SetError("Gamma correction not supported");
slouken@0
   141
#endif
slouken@0
   142
	if ( (succeeded < 0) && video->GetGamma ) {
slouken@0
   143
		SDL_ClearError();
slouken@0
   144
		succeeded = video->GetGamma(this, red, green, blue);
slouken@0
   145
	}
slouken@0
   146
	return succeeded;
slouken@0
   147
}
slouken@0
   148
slouken@669
   149
int SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue)
slouken@0
   150
{
slouken@0
   151
	int succeeded;
slouken@0
   152
	SDL_VideoDevice *video = current_video;
slouken@0
   153
	SDL_VideoDevice *this  = current_video;	
slouken@0
   154
	SDL_Surface *screen = SDL_PublicSurface;
slouken@0
   155
slouken@0
   156
	/* Verify the screen parameter */
slouken@0
   157
	if ( !screen ) {
slouken@0
   158
		SDL_SetError("No video mode has been set");
slouken@0
   159
		return -1;
slouken@0
   160
	}
slouken@0
   161
slouken@0
   162
	/* Lazily allocate the gamma tables */
slouken@0
   163
	if ( ! video->gamma ) {
slouken@0
   164
		SDL_GetGammaRamp(0, 0, 0);
slouken@0
   165
	}
slouken@0
   166
slouken@0
   167
	/* Fill the gamma table with the new values */
slouken@0
   168
	if ( red ) {
slouken@0
   169
		memcpy(&video->gamma[0*256], red, 256*sizeof(*video->gamma));
slouken@0
   170
	}
slouken@0
   171
	if ( green ) {
slouken@0
   172
		memcpy(&video->gamma[1*256], green, 256*sizeof(*video->gamma));
slouken@0
   173
	}
slouken@0
   174
	if ( blue ) {
slouken@0
   175
		memcpy(&video->gamma[2*256], blue, 256*sizeof(*video->gamma));
slouken@0
   176
	}
slouken@0
   177
slouken@0
   178
	/* Gamma correction always possible on split palettes */
slouken@0
   179
	if ( (screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) {
slouken@0
   180
		SDL_Palette *pal = screen->format->palette;
slouken@0
   181
slouken@0
   182
		/* If physical palette has been set independently, use it */
slouken@0
   183
		if(video->physpal)
slouken@0
   184
		        pal = video->physpal;
slouken@0
   185
		      
slouken@0
   186
		SDL_SetPalette(screen, SDL_PHYSPAL,
slouken@0
   187
			       pal->colors, 0, pal->ncolors);
slouken@0
   188
		return 0;
slouken@0
   189
	}
slouken@0
   190
slouken@0
   191
	/* Try to set the gamma ramp in the driver */
slouken@0
   192
	succeeded = -1;
slouken@0
   193
	if ( video->SetGammaRamp ) {
slouken@0
   194
		succeeded = video->SetGammaRamp(this, video->gamma);
slouken@0
   195
	} else {
slouken@0
   196
		SDL_SetError("Gamma ramp manipulation not supported");
slouken@0
   197
	}
slouken@0
   198
	return succeeded;
slouken@0
   199
}
slouken@0
   200
slouken@0
   201
int SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
slouken@0
   202
{
slouken@0
   203
	SDL_VideoDevice *video = current_video;
slouken@0
   204
	SDL_VideoDevice *this  = current_video;	
slouken@0
   205
slouken@0
   206
	/* Lazily allocate the gamma table */
slouken@0
   207
	if ( ! video->gamma ) {
slouken@0
   208
		video->gamma = malloc(3*256*sizeof(*video->gamma));
slouken@0
   209
		if ( ! video->gamma ) {
slouken@0
   210
			SDL_OutOfMemory();
slouken@0
   211
			return -1;
slouken@0
   212
		}
slouken@0
   213
		if ( video->GetGammaRamp ) {
slouken@0
   214
			/* Get the real hardware gamma */
slouken@0
   215
			video->GetGammaRamp(this, video->gamma);
slouken@0
   216
		} else {
slouken@0
   217
			/* Assume an identity gamma */
slouken@0
   218
			int i;
slouken@0
   219
			for ( i=0; i<256; ++i ) {
slouken@0
   220
				video->gamma[0*256+i] = (i << 8) | i;
slouken@0
   221
				video->gamma[1*256+i] = (i << 8) | i;
slouken@0
   222
				video->gamma[2*256+i] = (i << 8) | i;
slouken@0
   223
			}
slouken@0
   224
		}
slouken@0
   225
	}
slouken@0
   226
slouken@0
   227
	/* Just copy from our internal table */
slouken@0
   228
	if ( red ) {
slouken@0
   229
		memcpy(red, &video->gamma[0*256], 256*sizeof(*red));
slouken@0
   230
	}
slouken@0
   231
	if ( green ) {
slouken@0
   232
		memcpy(green, &video->gamma[1*256], 256*sizeof(*green));
slouken@0
   233
	}
slouken@0
   234
	if ( blue ) {
slouken@0
   235
		memcpy(blue, &video->gamma[2*256], 256*sizeof(*blue));
slouken@0
   236
	}
slouken@0
   237
	return 0;
slouken@0
   238
}