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#if defined(__APPLE__) && defined(__MACH__)
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Carbon/Carbon.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Carbon.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Windows.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Strings.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_MACCLASSIC_GAMMA_SUPPORT
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Devices.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Files.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <MacTypes.h>
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <QDOffscreen.h>
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Quickdraw.h>
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Video.h>
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_stdinc.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_macwm_c.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_SetCaption(_THIS, const char *title, const char *icon)
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't convert C to P string in place, because it may be read-only */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Str255		ptitle; /* MJS */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ptitle[0] = strlen (title);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memcpy(ptitle+1, title, ptitle[0]); /* MJS */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_Window)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SetWTitle(SDL_Window, ptitle); /* MJS */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_MACCLASSIC_GAMMA_SUPPORT
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * ADC Gamma Ramp support...
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Mac Gamma Ramp code was originally from sample code provided by
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  Apple Developer Connection, and not written specifically for SDL:
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * "Contains: Functions to enable Mac OS device gamma adjustments using 3 channel 256 element 8 bit gamma ramps
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  Written by: Geoff Stahl (ggs)
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  Copyright: Copyright (c) 1999 Apple Computer, Inc., All Rights Reserved
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *  Disclaimer: You may incorporate this sample code into your applications without
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *              restriction, though the sample code has been provided "AS IS" and the
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *              responsibility for its operation is 100% yours.  However, what you are
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *              not permitted to do is to redistribute the source as "DSC Sample Code"
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *              after having made changes. If you're going to re-distribute the source,
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *              we require that you make it clear in the source that the code was
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *              descended from Apple Sample Code, but that you've made changes."
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * (The sample code has been integrated into this file, and thus is modified from the original Apple sources.)
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct recDeviceGamma											/* storage for device handle and gamma table */
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GDHandle hGD;												/* handle to device */
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr pDeviceGamma;									/* pointer to device gamma table */
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} recDeviceGamma;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef recDeviceGamma * precDeviceGamma;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct recSystemGamma											/* storage for system devices and gamma tables */
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short numDevices;											/* number of devices */
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	precDeviceGamma * devGamma;									/* array of pointers to device gamma records */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} recSystemGamma;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef recSystemGamma * precSystemGamma;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Ptr CopyGammaTable (GammaTblPtr pTableGammaIn)
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr		pTableGammaOut = NULL;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short			tableSize, dataWidth;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pTableGammaIn)												/* if there is a table to copy  */
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dataWidth = (pTableGammaIn->gDataWidth + 7) / 8;			/* number of bytes per entry */
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		tableSize = sizeof (GammaTbl) + pTableGammaIn->gFormulaSize +
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					(pTableGammaIn->gChanCnt * pTableGammaIn->gDataCnt * dataWidth);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaOut = (GammaTblPtr) NewPtr (tableSize);			/* allocate new table */
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (pTableGammaOut)
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			BlockMove( (Ptr)pTableGammaIn, (Ptr)pTableGammaOut, tableSize);	/* move everything */
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (Ptr)pTableGammaOut;										/* return whatever we allocated, could be NULL */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic OSErr GetGammaTable (GDHandle hGD, GammaTblPtr * ppTableGammaOut)
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VDGammaRecord   DeviceGammaRec;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CntrlParam		cParam;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	OSErr			err;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cParam.ioCompletion = NULL;										/* set up control params */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cParam.ioNamePtr = NULL;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cParam.ioVRefNum = 0;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cParam.ioCRefNum = (**hGD).gdRefNum;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cParam.csCode = cscGetGamma;									/* Get Gamma commnd to device */
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*(Ptr *)cParam.csParam = (Ptr) &DeviceGammaRec;					/* record for gamma */
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	err = PBStatusSync( (ParmBlkPtr)&cParam );						/* get gamma */
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*ppTableGammaOut = (GammaTblPtr)(DeviceGammaRec.csGTable);		/* pull table out of record */
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return err;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Ptr GetDeviceGamma (GDHandle hGD)
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr		pTableGammaDevice = NULL;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr		pTableGammaReturn = NULL;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	OSErr			err;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	err = GetGammaTable (hGD, &pTableGammaDevice);					/* get a pointer to the devices table */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((noErr == err) && pTableGammaDevice)						/* if succesful */
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaReturn = (GammaTblPtr) CopyGammaTable (pTableGammaDevice); /* copy to global */
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (Ptr) pTableGammaReturn;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DisposeGammaTable (Ptr pGamma)
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pGamma)
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DisposePtr((Ptr) pGamma);									/* get rid of it */
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DisposeSystemGammas (Ptr* ppSystemGammas)
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	precSystemGamma pSysGammaIn;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (ppSystemGammas)
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pSysGammaIn = (precSystemGamma) *ppSystemGammas;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (pSysGammaIn)
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			short i;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (i = 0; i < pSysGammaIn->numDevices; i++)		/* for all devices */
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (pSysGammaIn->devGamma [i])						/* if pointer is valid */
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					DisposeGammaTable ((Ptr) pSysGammaIn->devGamma [i]->pDeviceGamma); /* dump gamma table */
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					DisposePtr ((Ptr) pSysGammaIn->devGamma [i]);					   /* dump device info */
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DisposePtr ((Ptr) pSysGammaIn->devGamma);				/* dump device pointer array		 */
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DisposePtr ((Ptr) pSysGammaIn);							/* dump system structure */
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*ppSystemGammas = NULL;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Boolean GetDeviceGammaRampGD (GDHandle hGD, Ptr pRamp)
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr		pTableGammaTemp = NULL;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long 			indexChan, indexEntry;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	OSErr			err;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pRamp)															/* ensure pRamp is allocated */
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		err = GetGammaTable (hGD, &pTableGammaTemp);					/* get a pointer to the current gamma */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((noErr == err) && pTableGammaTemp)							/* if successful */
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* fill ramp */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			unsigned char * pEntry = (unsigned char *) &pTableGammaTemp->gFormulaData + pTableGammaTemp->gFormulaSize; /* base of table */
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			short bytesPerEntry = (pTableGammaTemp->gDataWidth + 7) / 8; /* size, in bytes, of the device table entries */
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			short shiftRightValue = pTableGammaTemp->gDataWidth - 8; 	 /* number of right shifts device -> ramp */
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			short channels = pTableGammaTemp->gChanCnt;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			short entries = pTableGammaTemp->gDataCnt;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (3 == channels)											/* RGB format */
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{															/* note, this will create runs of entries if dest. is bigger (not linear interpolate) */
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for (indexChan = 0; indexChan < channels; indexChan++)
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					for (indexEntry = 0; indexEntry < 256; indexEntry++)
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*((unsigned char *) pRamp + (indexChan * 256) + indexEntry) =
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						  *(pEntry + indexChan * entries * bytesPerEntry + indexEntry * entries * bytesPerEntry / 256) >> shiftRightValue;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else														/* single channel format */
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for (indexChan = 0; indexChan < 768; indexChan += 256)	/* repeat for all 3 channels (step by ramp size) */
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					for (indexEntry = 0; indexEntry < 256; indexEntry++) /* for all entries set vramp value */
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						*((unsigned char *) pRamp + indexChan + indexEntry) =
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						  *(pEntry + indexEntry * entries * bytesPerEntry / 256) >> shiftRightValue;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return true;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return false;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Ptr GetSystemGammas (void)
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	precSystemGamma pSysGammaOut;									/* return pointer to system device gamma info */
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short devCount = 0;												/* number of devices attached */
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Boolean fail = false;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GDHandle hGDevice;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pSysGammaOut = (precSystemGamma) NewPtr (sizeof (recSystemGamma)); /* allocate for structure */
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hGDevice = GetDeviceList ();							/* top of device list */
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do																/* iterate */
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		devCount++;													/* count devices					 */
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hGDevice = GetNextDevice (hGDevice);						/* next device */
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} while (hGDevice);
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pSysGammaOut->devGamma = (precDeviceGamma *) NewPtr (sizeof (precDeviceGamma) * devCount); /* allocate for array of pointers to device records */
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pSysGammaOut)
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pSysGammaOut->numDevices = devCount;						/* stuff count */
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		devCount = 0;												/* reset iteration */
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hGDevice = GetDeviceList ();
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		do
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pSysGammaOut->devGamma [devCount] = (precDeviceGamma) NewPtr (sizeof (recDeviceGamma));	  /* new device record */
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (pSysGammaOut->devGamma [devCount])					/* if we actually allocated memory */
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pSysGammaOut->devGamma [devCount]->hGD = hGDevice;										  /* stuff handle */
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pSysGammaOut->devGamma [devCount]->pDeviceGamma = (GammaTblPtr)GetDeviceGamma (hGDevice); /* copy gamma table */
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else													/* otherwise dump record on exit */
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 fail = true;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			devCount++;												/* next device */
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hGDevice = GetNextDevice (hGDevice);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} while (hGDevice);
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!fail)														/* if we did not fail */
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return (Ptr) pSysGammaOut;									/* return pointer to structure */
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DisposeSystemGammas ((Ptr *) &pSysGammaOut);					/* otherwise dump the current structures (dispose does error checking) */
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return NULL;												/* could not complete */
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void RestoreDeviceGamma (GDHandle hGD, Ptr pGammaTable)
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VDSetEntryRecord setEntriesRec;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VDGammaRecord	gameRecRestore;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CTabHandle      hCTabDeviceColors;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Ptr				csPtr;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	OSErr			err = noErr;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pGammaTable)												/* if we have a table to restore								 */
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gameRecRestore.csGTable = pGammaTable;						/* setup restore record */
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		csPtr = (Ptr) &gameRecRestore;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		err = Control((**hGD).gdRefNum, cscSetGamma, (Ptr) &csPtr);	/* restore gamma */
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((noErr == err) && (8 == (**(**hGD).gdPMap).pixelSize))	/* if successful and on an 8 bit device */
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hCTabDeviceColors = (**(**hGD).gdPMap).pmTable;			/* do SetEntries to force CLUT update */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			setEntriesRec.csTable = (ColorSpec *) &(**hCTabDeviceColors).ctTable;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			setEntriesRec.csStart = 0;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			setEntriesRec.csCount = (**hCTabDeviceColors).ctSize;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			csPtr = (Ptr) &setEntriesRec;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			err = Control((**hGD).gdRefNum, cscSetEntries, (Ptr) &csPtr); /* SetEntries in CLUT */
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void RestoreSystemGammas (Ptr pSystemGammas)
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short i;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	precSystemGamma pSysGammaIn = (precSystemGamma) pSystemGammas;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pSysGammaIn)
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (i = 0; i < pSysGammaIn->numDevices; i++)			/* for all devices */
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			RestoreDeviceGamma (pSysGammaIn->devGamma [i]->hGD, (Ptr) pSysGammaIn->devGamma [i]->pDeviceGamma);	/* restore gamma */
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Ptr CreateEmptyGammaTable (short channels, short entries, short bits)
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr		pTableGammaOut = NULL;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short			tableSize, dataWidth;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dataWidth = (bits + 7) / 8;										/* number of bytes per entry */
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tableSize = sizeof (GammaTbl) + (channels * entries * dataWidth);
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pTableGammaOut = (GammaTblPtr) NewPtrClear (tableSize);			/* allocate new tabel */
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pTableGammaOut)												/* if we successfully allocated */
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaOut->gVersion = 0;								/* set parameters based on input */
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaOut->gType = 0;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaOut->gFormulaSize = 0;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaOut->gChanCnt = channels;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaOut->gDataCnt = entries;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pTableGammaOut->gDataWidth = bits;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (Ptr)pTableGammaOut;										/* return whatever we allocated */
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Boolean SetDeviceGammaRampGD (GDHandle hGD, Ptr pRamp)
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VDSetEntryRecord setEntriesRec;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	VDGammaRecord	gameRecRestore;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr		pTableGammaNew;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GammaTblPtr		pTableGammaCurrent = NULL;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CTabHandle      hCTabDeviceColors;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Ptr				csPtr;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	OSErr			err;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	short 			dataBits, entries, channels = 3;						/* force three channels in the gamma table */
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pRamp)																/* ensure pRamp is allocated */
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		err= GetGammaTable (hGD, &pTableGammaCurrent);						/* get pointer to current table */
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((noErr == err) && pTableGammaCurrent)
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dataBits = pTableGammaCurrent->gDataWidth;						/* table must have same data width */
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entries = pTableGammaCurrent->gDataCnt;							/* table must be same size */
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pTableGammaNew = (GammaTblPtr) CreateEmptyGammaTable (channels, entries, dataBits); /* our new table */
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (pTableGammaNew)												/* if successful fill table */
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				unsigned char * pGammaBase = (unsigned char *) &pTableGammaNew->gFormulaData + pTableGammaNew->gFormulaSize; /* base of table */
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ((256 == entries) && (8 == dataBits)) 						/* simple case: direct mapping */
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					BlockMove ((Ptr)pRamp, (Ptr)pGammaBase, channels * entries); /* move everything */
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				else														/* tough case handle entry, channel and data size disparities */
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					short indexChan, indexEntry;
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					short bytesPerEntry = (dataBits + 7) / 8; 				/* size, in bytes, of the device table entries */
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					short shiftRightValue = 8 - dataBits;					/* number of right shifts ramp -> device */
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					shiftRightValue += ((bytesPerEntry - 1) * 8);  			/* multibyte entries and the need to map a byte at a time most sig. to least sig. */
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					for (indexChan = 0; indexChan < channels; indexChan++) /* for all the channels */
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						for (indexEntry = 0; indexEntry < entries; indexEntry++) /* for all the entries */
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						{
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							short currentShift = shiftRightValue;			/* reset current bit shift */
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							long temp = *((unsigned char *)pRamp + (indexChan << 8) + (indexEntry << 8) / entries); /* get data from ramp */
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							short indexByte;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							for (indexByte = 0; indexByte < bytesPerEntry; indexByte++) /* for all bytes */
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							{
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								if (currentShift < 0)						/* shift data correctly for current byte */
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									*(pGammaBase++) = temp << -currentShift;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								else
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									*(pGammaBase++) = temp >> currentShift;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								currentShift -= 8;							/* increment shift to align to next less sig. byte */
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							}
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* set gamma */
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				gameRecRestore.csGTable = (Ptr) pTableGammaNew;				/* setup restore record */
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				csPtr = (Ptr) &gameRecRestore;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				err = Control((**hGD).gdRefNum, cscSetGamma, (Ptr) &csPtr);	/* restore gamma (note, display drivers may delay returning from this until VBL) */
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ((8 == (**(**hGD).gdPMap).pixelSize) && (noErr == err))	/* if successful and on an 8 bit device */
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					hCTabDeviceColors = (**(**hGD).gdPMap).pmTable;			/* do SetEntries to force CLUT update */
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					setEntriesRec.csTable = (ColorSpec *) &(**hCTabDeviceColors).ctTable;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					setEntriesRec.csStart = 0;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					setEntriesRec.csCount = (**hCTabDeviceColors).ctSize;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					csPtr = (Ptr) &setEntriesRec;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					err = Control((**hGD).gdRefNum, cscSetEntries, (Ptr) &csPtr);	/* SetEntries in CLUT */
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				DisposeGammaTable ((Ptr) pTableGammaNew);					/* dump table */
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (noErr == err)
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return true;
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else																	/* set NULL gamma -> results in linear map */
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gameRecRestore.csGTable = (Ptr) NULL;								/* setup restore record */
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		csPtr = (Ptr) &gameRecRestore;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		err = Control((**hGD).gdRefNum, cscSetGamma, (Ptr) &csPtr);			/* restore gamma */
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((8 == (**(**hGD).gdPMap).pixelSize) && (noErr == err))			/* if successful and on an 8 bit device */
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hCTabDeviceColors = (**(**hGD).gdPMap).pmTable;					/* do SetEntries to force CLUT update */
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			setEntriesRec.csTable = (ColorSpec *) &(**hCTabDeviceColors).ctTable;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			setEntriesRec.csStart = 0;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			setEntriesRec.csCount = (**hCTabDeviceColors).ctSize;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			csPtr = (Ptr) &setEntriesRec;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			err = Control((**hGD).gdRefNum, cscSetEntries, (Ptr) &csPtr);	/* SetEntries in CLUT */
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (noErr == err)
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return true;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return false;															/* memory allocation or device control failed if we get here */
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* end of ADC Gamma Ramp support code... */
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Ptr systemGammaPtr;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Mac_QuitGamma(_THIS)
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (systemGammaPtr)
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		RestoreSystemGammas(systemGammaPtr);
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DisposeSystemGammas(&systemGammaPtr);
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned char shiftedRamp[3 * 256];
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint Mac_SetGammaRamp(_THIS, Uint16 *ramp)
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!systemGammaPtr)
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		systemGammaPtr = GetSystemGammas();
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i = 0; i < 3 * 256; i++)
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		shiftedRamp[i] = ramp[i] >> 8;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SetDeviceGammaRampGD(GetMainDevice(), (Ptr) shiftedRamp))
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint Mac_GetGammaRamp(_THIS, Uint16 *ramp)
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (GetDeviceGammaRampGD(GetMainDevice(), (Ptr) shiftedRamp))
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int i;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (i = 0; i < 3 * 256; i++)
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ramp[i] = shiftedRamp[i] << 8;
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif  /* SDL_MACCLASSIC_GAMMA_SUPPORT */
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
443