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