src/video/SDL_gamma.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 152 2cf88672fc7f
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Gamma correction support */
slouken@0
    29
slouken@0
    30
#define USE_MATH_H	/* Used for calculating gamma ramps */
slouken@0
    31
slouken@0
    32
#ifdef USE_MATH_H
slouken@0
    33
#include <math.h>
slouken@0
    34
#endif
slouken@0
    35
#include <stdlib.h>
slouken@0
    36
slouken@0
    37
#include "SDL_error.h"
slouken@0
    38
#include "SDL_sysvideo.h"
slouken@0
    39
slouken@0
    40
#ifdef USE_MATH_H
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@0
    46
	if ( gamma <= 0.0 ) {
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@0
    53
	if ( gamma == 1.0 ) {
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@0
    76
	float sum = 0.0;
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@0
    88
	if ( count && sum ) {
slouken@0
    89
		*gamma = 1.0f / (sum / count);
slouken@0
    90
	}
slouken@0
    91
}
slouken@0
    92
#endif /* USE_MATH_H */
slouken@0
    93
slouken@0
    94
int SDL_SetGamma(float red, float green, float blue)
slouken@0
    95
{
slouken@0
    96
	int succeeded;
slouken@0
    97
	SDL_VideoDevice *video = current_video;
slouken@0
    98
	SDL_VideoDevice *this  = current_video;	
slouken@0
    99
slouken@0
   100
	succeeded = -1;
slouken@0
   101
#ifdef USE_MATH_H
slouken@0
   102
	/* Prefer using SetGammaRamp(), as it's more flexible */
slouken@0
   103
	{
slouken@0
   104
		Uint16 ramp[3][256];
slouken@0
   105
slouken@0
   106
		CalculateGammaRamp(red, ramp[0]);
slouken@0
   107
		CalculateGammaRamp(green, ramp[1]);
slouken@0
   108
		CalculateGammaRamp(blue, ramp[2]);
slouken@0
   109
		succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
   110
	}
slouken@0
   111
#else
slouken@0
   112
	SDL_SetError("Gamma correction not supported");
slouken@0
   113
#endif
slouken@0
   114
	if ( (succeeded < 0) && video->SetGamma ) {
slouken@0
   115
		SDL_ClearError();
slouken@0
   116
		succeeded = video->SetGamma(this, red, green, blue);
slouken@0
   117
	}
slouken@0
   118
	return succeeded;
slouken@0
   119
}
slouken@0
   120
slouken@0
   121
/* Calculating the gamma by integrating the gamma ramps isn't exact,
slouken@0
   122
   so this function isn't officially supported.
slouken@0
   123
*/
slouken@0
   124
int SDL_GetGamma(float *red, float *green, float *blue)
slouken@0
   125
{
slouken@0
   126
	int succeeded;
slouken@0
   127
	SDL_VideoDevice *video = current_video;
slouken@0
   128
	SDL_VideoDevice *this  = current_video;	
slouken@0
   129
slouken@0
   130
	succeeded = -1;
slouken@0
   131
#ifdef USE_MATH_H
slouken@0
   132
	/* Prefer using GetGammaRamp(), as it's more flexible */
slouken@0
   133
	{
slouken@0
   134
		Uint16 ramp[3][256];
slouken@0
   135
slouken@0
   136
		succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
slouken@0
   137
		if ( succeeded >= 0 ) {
slouken@0
   138
			CalculateGammaFromRamp(red, ramp[0]);
slouken@0
   139
			CalculateGammaFromRamp(green, ramp[1]);
slouken@0
   140
			CalculateGammaFromRamp(blue, ramp[2]);
slouken@0
   141
		}
slouken@0
   142
	}
slouken@0
   143
#else
slouken@0
   144
	SDL_SetError("Gamma correction not supported");
slouken@0
   145
#endif
slouken@0
   146
	if ( (succeeded < 0) && video->GetGamma ) {
slouken@0
   147
		SDL_ClearError();
slouken@0
   148
		succeeded = video->GetGamma(this, red, green, blue);
slouken@0
   149
	}
slouken@0
   150
	return succeeded;
slouken@0
   151
}
slouken@0
   152
slouken@0
   153
int SDL_SetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
slouken@0
   154
{
slouken@0
   155
	int succeeded;
slouken@0
   156
	SDL_VideoDevice *video = current_video;
slouken@0
   157
	SDL_VideoDevice *this  = current_video;	
slouken@0
   158
	SDL_Surface *screen = SDL_PublicSurface;
slouken@0
   159
slouken@0
   160
	/* Verify the screen parameter */
slouken@0
   161
	if ( !screen ) {
slouken@0
   162
		SDL_SetError("No video mode has been set");
slouken@0
   163
		return -1;
slouken@0
   164
	}
slouken@0
   165
slouken@0
   166
	/* Lazily allocate the gamma tables */
slouken@0
   167
	if ( ! video->gamma ) {
slouken@0
   168
		SDL_GetGammaRamp(0, 0, 0);
slouken@0
   169
	}
slouken@0
   170
slouken@0
   171
	/* Fill the gamma table with the new values */
slouken@0
   172
	if ( red ) {
slouken@0
   173
		memcpy(&video->gamma[0*256], red, 256*sizeof(*video->gamma));
slouken@0
   174
	}
slouken@0
   175
	if ( green ) {
slouken@0
   176
		memcpy(&video->gamma[1*256], green, 256*sizeof(*video->gamma));
slouken@0
   177
	}
slouken@0
   178
	if ( blue ) {
slouken@0
   179
		memcpy(&video->gamma[2*256], blue, 256*sizeof(*video->gamma));
slouken@0
   180
	}
slouken@0
   181
slouken@0
   182
	/* Gamma correction always possible on split palettes */
slouken@0
   183
	if ( (screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) {
slouken@0
   184
		SDL_Palette *pal = screen->format->palette;
slouken@0
   185
slouken@0
   186
		/* If physical palette has been set independently, use it */
slouken@0
   187
		if(video->physpal)
slouken@0
   188
		        pal = video->physpal;
slouken@0
   189
		      
slouken@0
   190
		SDL_SetPalette(screen, SDL_PHYSPAL,
slouken@0
   191
			       pal->colors, 0, pal->ncolors);
slouken@0
   192
		return 0;
slouken@0
   193
	}
slouken@0
   194
slouken@0
   195
	/* Try to set the gamma ramp in the driver */
slouken@0
   196
	succeeded = -1;
slouken@0
   197
	if ( video->SetGammaRamp ) {
slouken@0
   198
		succeeded = video->SetGammaRamp(this, video->gamma);
slouken@0
   199
	} else {
slouken@0
   200
		SDL_SetError("Gamma ramp manipulation not supported");
slouken@0
   201
	}
slouken@0
   202
	return succeeded;
slouken@0
   203
}
slouken@0
   204
slouken@0
   205
int SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
slouken@0
   206
{
slouken@0
   207
	SDL_VideoDevice *video = current_video;
slouken@0
   208
	SDL_VideoDevice *this  = current_video;	
slouken@0
   209
slouken@0
   210
	/* Lazily allocate the gamma table */
slouken@0
   211
	if ( ! video->gamma ) {
slouken@0
   212
		video->gamma = malloc(3*256*sizeof(*video->gamma));
slouken@0
   213
		if ( ! video->gamma ) {
slouken@0
   214
			SDL_OutOfMemory();
slouken@0
   215
			return -1;
slouken@0
   216
		}
slouken@0
   217
		if ( video->GetGammaRamp ) {
slouken@0
   218
			/* Get the real hardware gamma */
slouken@0
   219
			video->GetGammaRamp(this, video->gamma);
slouken@0
   220
		} else {
slouken@0
   221
			/* Assume an identity gamma */
slouken@0
   222
			int i;
slouken@0
   223
			for ( i=0; i<256; ++i ) {
slouken@0
   224
				video->gamma[0*256+i] = (i << 8) | i;
slouken@0
   225
				video->gamma[1*256+i] = (i << 8) | i;
slouken@0
   226
				video->gamma[2*256+i] = (i << 8) | i;
slouken@0
   227
			}
slouken@0
   228
		}
slouken@0
   229
	}
slouken@0
   230
slouken@0
   231
	/* Just copy from our internal table */
slouken@0
   232
	if ( red ) {
slouken@0
   233
		memcpy(red, &video->gamma[0*256], 256*sizeof(*red));
slouken@0
   234
	}
slouken@0
   235
	if ( green ) {
slouken@0
   236
		memcpy(green, &video->gamma[1*256], 256*sizeof(*green));
slouken@0
   237
	}
slouken@0
   238
	if ( blue ) {
slouken@0
   239
		memcpy(blue, &video->gamma[2*256], 256*sizeof(*blue));
slouken@0
   240
	}
slouken@0
   241
	return 0;
slouken@0
   242
}