19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Gamma correction support */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_MATH_H
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <math.h>	/* Used for calculating gamma ramps */
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Math routines from uClibc: http://www.uclibc.org */
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "math_private.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "e_sqrt.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "e_pow.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "e_log.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define pow(x, y)	__ieee754_pow(x, y)
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define log(x)		__ieee754_log(x)
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysvideo.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CalculateGammaRamp(float gamma, Uint16 *ramp)
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* 0.0 gamma is all black */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( gamma <= 0.0f ) {
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<256; ++i ) {
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ramp[i] = 0;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* 1.0 gamma is identity */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( gamma == 1.0f ) {
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<256; ++i ) {
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ramp[i] = (i << 8) | i;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Calculate a real gamma ramp */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ int value;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gamma = 1.0f / gamma;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<256; ++i ) {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			value = (int)(pow((double)i/256.0, gamma)*65535.0+0.5);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( value > 65535 ) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				value = 65535;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ramp[i] = (Uint16)value;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CalculateGammaFromRamp(float *gamma, Uint16 *ramp)
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The following is adapted from a post by Garrett Bass on OpenGL
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   Gamedev list, March 4, 2000.
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	float sum = 0.0f;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, count = 0;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*gamma = 1.0;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 1; i < 256; ++i ) {
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if ( (ramp[i] != 0) && (ramp[i] != 65535) ) {
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        double B = (double)i / 256.0;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        double A = ramp[i] / 65535.0;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        sum += (float) ( log(A) / log(B) );
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        count++;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( count && sum > 0.0f ) {
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*gamma = 1.0f / (sum / count);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SetGamma(float red, float green, float blue)
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int succeeded;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *video = current_video;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this  = current_video;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	succeeded = -1;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Prefer using SetGammaRamp(), as it's more flexible */
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint16 ramp[3][256];
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CalculateGammaRamp(red, ramp[0]);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CalculateGammaRamp(green, ramp[1]);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CalculateGammaRamp(blue, ramp[2]);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		succeeded = SDL_SetGammaRamp(ramp[0], ramp[1], ramp[2]);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (succeeded < 0) && video->SetGamma ) {
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_ClearError();
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		succeeded = video->SetGamma(this, red, green, blue);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return succeeded;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Calculating the gamma by integrating the gamma ramps isn't exact,
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   so this function isn't officially supported.
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_GetGamma(float *red, float *green, float *blue)
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int succeeded;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *video = current_video;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this  = current_video;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	succeeded = -1;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Prefer using GetGammaRamp(), as it's more flexible */
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint16 ramp[3][256];
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		succeeded = SDL_GetGammaRamp(ramp[0], ramp[1], ramp[2]);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( succeeded >= 0 ) {
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CalculateGammaFromRamp(red, ramp[0]);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CalculateGammaFromRamp(green, ramp[1]);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CalculateGammaFromRamp(blue, ramp[2]);
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (succeeded < 0) && video->GetGamma ) {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_ClearError();
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		succeeded = video->GetGamma(this, red, green, blue);
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return succeeded;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SetGammaRamp(const Uint16 *red, const Uint16 *green, const Uint16 *blue)
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int succeeded;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *video = current_video;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this  = current_video;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *screen = SDL_PublicSurface;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Verify the screen parameter */
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !screen ) {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("No video mode has been set");
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Lazily allocate the gamma tables */
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! video->gamma ) {
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_GetGammaRamp(0, 0, 0);
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill the gamma table with the new values */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( red ) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(&video->gamma[0*256], red, 256*sizeof(*video->gamma));
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( green ) {
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(&video->gamma[1*256], green, 256*sizeof(*video->gamma));
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( blue ) {
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(&video->gamma[2*256], blue, 256*sizeof(*video->gamma));
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Gamma correction always possible on split palettes */
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Palette *pal = screen->format->palette;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If physical palette has been set independently, use it */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(video->physpal)
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        pal = video->physpal;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetPalette(screen, SDL_PHYSPAL,
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			       pal->colors, 0, pal->ncolors);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try to set the gamma ramp in the driver */
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	succeeded = -1;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( video->SetGammaRamp ) {
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		succeeded = video->SetGammaRamp(this, video->gamma);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Gamma ramp manipulation not supported");
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return succeeded;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue)
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *video = current_video;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this  = current_video;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Lazily allocate the gamma table */
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! video->gamma ) {
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video->gamma = SDL_malloc(3*256*sizeof(*video->gamma));
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! video->gamma ) {
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( video->GetGammaRamp ) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Get the real hardware gamma */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			video->GetGammaRamp(this, video->gamma);
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Assume an identity gamma */
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int i;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<256; ++i ) {
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				video->gamma[0*256+i] = (i << 8) | i;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				video->gamma[1*256+i] = (i << 8) | i;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				video->gamma[2*256+i] = (i << 8) | i;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Just copy from our internal table */
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( red ) {
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(red, &video->gamma[0*256], 256*sizeof(*red));
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( green ) {
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(green, &video->gamma[1*256], 256*sizeof(*green));
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( blue ) {
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(blue, &video->gamma[2*256], 256*sizeof(*blue));
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
234