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/* General (mostly internal) pixel/color manipulation routines for SDL */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_endian.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysvideo.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_blit.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_pixels_c.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_RLEaccel_c.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Helper functions */
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Allocate a pixel format structure and fill it according to the given info.
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_PixelFormat *SDL_AllocFormat(int bpp,
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_PixelFormat *format;
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 mask;
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate an empty pixel format structure */
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format = SDL_malloc(sizeof(*format));
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format == NULL ) {
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(format, 0, sizeof(*format));
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format->alpha = SDL_ALPHA_OPAQUE;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the format */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format->BitsPerPixel = bpp;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format->BytesPerPixel = (bpp+7)/8;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( Rmask || Bmask || Gmask ) { /* Packed pixels with custom mask */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->palette = NULL;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rshift = 0;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rloss = 8;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( Rmask ) {
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( mask = Rmask; !(mask&0x01); mask >>= 1 )
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++format->Rshift;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( ; (mask&0x01); mask >>= 1 )
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--format->Rloss;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gshift = 0;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gloss = 8;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( Gmask ) {
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( mask = Gmask; !(mask&0x01); mask >>= 1 )
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++format->Gshift;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( ; (mask&0x01); mask >>= 1 )
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--format->Gloss;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bshift = 0;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bloss = 8;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( Bmask ) {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( mask = Bmask; !(mask&0x01); mask >>= 1 )
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++format->Bshift;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( ; (mask&0x01); mask >>= 1 )
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--format->Bloss;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Ashift = 0;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Aloss = 8;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( Amask ) {
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( mask = Amask; !(mask&0x01); mask >>= 1 )
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				++format->Ashift;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( ; (mask&0x01); mask >>= 1 )
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				--format->Aloss;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rmask = Rmask;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gmask = Gmask;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bmask = Bmask;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Amask = Amask;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if ( bpp > 8 ) {		/* Packed pixels with standard mask */
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* R-G-B */
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( bpp > 24 )
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			bpp = 24;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rloss = 8-(bpp/3);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gloss = 8-(bpp/3)-(bpp%3);
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bloss = 8-(bpp/3);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rshift = ((bpp/3)+(bpp%3))+(bpp/3);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gshift = (bpp/3);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bshift = 0;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Palettized formats have no mask info */
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rloss = 8;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gloss = 8;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bloss = 8;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Aloss = 8;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rshift = 0;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gshift = 0;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bshift = 0;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Ashift = 0;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rmask = 0;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Gmask = 0;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Bmask = 0;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Amask = 0;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( bpp <= 8 ) {			/* Palettized mode */
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int ncolors = 1<<bpp;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_PALETTE
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr,"bpp=%d ncolors=%d\n",bpp,ncolors);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->palette = (SDL_Palette *)SDL_malloc(sizeof(SDL_Palette));
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( format->palette == NULL ) {
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_FreeFormat(format);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(format->palette)->ncolors = ncolors;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(format->palette)->colors = (SDL_Color *)SDL_malloc(
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(format->palette)->ncolors*sizeof(SDL_Color));
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (format->palette)->colors == NULL ) {
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_FreeFormat(format);
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( Rmask || Bmask || Gmask ) {
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* create palette according to masks */
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int i;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int Rm=0,Gm=0,Bm=0;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int Rw=0,Gw=0,Bw=0;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef ENABLE_PALETTE_ALPHA
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int Am=0,Aw=0;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(Rmask)
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Rw=8-format->Rloss;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for(i=format->Rloss;i>0;i-=Rw)
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Rm|=1<<i;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_PALETTE
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,"Rw=%d Rm=0x%02X\n",Rw,Rm);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(Gmask)
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Gw=8-format->Gloss;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for(i=format->Gloss;i>0;i-=Gw)
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Gm|=1<<i;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_PALETTE
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,"Gw=%d Gm=0x%02X\n",Gw,Gm);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(Bmask)
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Bw=8-format->Bloss;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for(i=format->Bloss;i>0;i-=Bw)
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Bm|=1<<i;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_PALETTE
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,"Bw=%d Bm=0x%02X\n",Bw,Bm);
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef ENABLE_PALETTE_ALPHA
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(Amask)
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Aw=8-format->Aloss;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for(i=format->Aloss;i>0;i-=Aw)
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					Am|=1<<i;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# ifdef DEBUG_PALETTE
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr,"Aw=%d Am=0x%02X\n",Aw,Am);
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# endif
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for(i=0; i < ncolors; ++i) {
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int r,g,b;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				r=(i&Rmask)>>format->Rshift;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				r=(r<<format->Rloss)|((r*Rm)>>Rw);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format->palette->colors[i].r=r;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				g=(i&Gmask)>>format->Gshift;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				g=(g<<format->Gloss)|((g*Gm)>>Gw);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format->palette->colors[i].g=g;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				b=(i&Bmask)>>format->Bshift;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				b=(b<<format->Bloss)|((b*Bm)>>Bw);
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format->palette->colors[i].b=b;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef ENABLE_PALETTE_ALPHA
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				a=(i&Amask)>>format->Ashift;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				a=(a<<format->Aloss)|((a*Am)>>Aw);
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format->palette->colors[i].unused=a;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format->palette->colors[i].unused=0;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else if ( ncolors == 2 ) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Create a black and white bitmap palette */
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format->palette->colors[0].r = 0xFF;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format->palette->colors[0].g = 0xFF;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format->palette->colors[0].b = 0xFF;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format->palette->colors[1].r = 0x00;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format->palette->colors[1].g = 0x00;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			format->palette->colors[1].b = 0x00;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Create an empty palette */
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memset((format->palette)->colors, 0,
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(format->palette)->ncolors*sizeof(SDL_Color));
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(format);
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_PixelFormat *SDL_ReallocFormat(SDL_Surface *surface, int bpp,
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->format ) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeFormat(surface->format);
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FormatChanged(surface);
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return surface->format;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Change any previous mappings from/to the new surface format
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_FormatChanged(SDL_Surface *surface)
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static int format_version = 0;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++format_version;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format_version < 0 ) { /* It wrapped... */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format_version = 1;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->format_version = format_version;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_InvalidateMap(surface->map);
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Free a previously allocated format structure
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_FreeFormat(SDL_PixelFormat *format)
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format ) {
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( format->palette ) {
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( format->palette->colors ) {
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_free(format->palette->colors);
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(format->palette);
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(format);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_DitherColors(SDL_Color *colors, int bpp)
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(bpp != 8)
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;		/* only 8bpp supported right now */
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(i = 0; i < 256; i++) {
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int r, g, b;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* map each bit field to the full [0, 255] interval,
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		r = i & 0xe0;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		r |= r >> 3 | r >> 6;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		colors[i].r = r;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g = (i << 3) & 0xe0;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		g |= g >> 3 | g >> 6;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		colors[i].g = g;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		b = i & 0x3;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		b |= b << 2;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		b |= b << 4;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		colors[i].b = b;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Calculate the pad-aligned scanline width of a surface
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint16 SDL_CalculatePitch(SDL_Surface *surface)
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 pitch;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Surface should be 4-byte aligned for speed */
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pitch = surface->w*surface->format->BytesPerPixel;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (surface->format->BitsPerPixel) {
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 1:
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pitch = (pitch+7)/8;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 4:
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pitch = (pitch+1)/2;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pitch = (pitch + 3) & ~3;	/* 4-byte aligning */
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(pitch);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Match an RGB value to a particular palette index
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b)
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Do colorspace distance matching */
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int smallest;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int distance;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int rd, gd, bd;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 pixel=0;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	smallest = ~0;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<pal->ncolors; ++i ) {
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		rd = pal->colors[i].r - r;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gd = pal->colors[i].g - g;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		bd = pal->colors[i].b - b;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		distance = (rd*rd)+(gd*gd)+(bd*bd);
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( distance < smallest ) {
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pixel = i;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( distance == 0 ) { /* Perfect match! */
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			smallest = distance;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(pixel);
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Find the opaque pixel value corresponding to an RGB triple */
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_MapRGB
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall(const SDL_PixelFormat * const format,
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Uint8 r, const Uint8 g, const Uint8 b)
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format->palette == NULL ) {
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return (r >> format->Rloss) << format->Rshift
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       | (g >> format->Gloss) << format->Gshift
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       | (b >> format->Bloss) << format->Bshift
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       | format->Amask;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_FindColor(format->palette, r, g, b);
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Find the pixel value corresponding to an RGBA quadruple */
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_MapRGBA
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall(const SDL_PixelFormat * const format,
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format->palette == NULL ) {
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        return (r >> format->Rloss) << format->Rshift
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    | (g >> format->Gloss) << format->Gshift
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    | (b >> format->Bloss) << format->Bshift
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    | ((a >> format->Aloss) << format->Ashift & format->Amask);
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_FindColor(format->palette, r, g, b);
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * const fmt,
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fmt->palette == NULL ) {
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        /*
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * This makes sure that the result is mapped to the
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * interval [0..255], and the maximum value for each
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * component is 255. This is important to make sure
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * that white is indeed reported as (255, 255, 255),
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * and that opaque alpha is 255.
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * This only works for RGB bit fields at least 4 bit
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 * wide, which is almost always the case.
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        unsigned v;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		v = (pixel & fmt->Rmask) >> fmt->Rshift;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		v = (pixel & fmt->Gmask) >> fmt->Gshift;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		v = (pixel & fmt->Bmask) >> fmt->Bshift;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(fmt->Amask) {
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        v = (pixel & fmt->Amask) >> fmt->Ashift;
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1)));
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        *a = SDL_ALPHA_OPAQUE;
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*r = fmt->palette->colors[pixel].r;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*g = fmt->palette->colors[pixel].g;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*b = fmt->palette->colors[pixel].b;
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*a = SDL_ALPHA_OPAQUE;
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * const fmt,
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                Uint8 *r,Uint8 *g,Uint8 *b)
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fmt->palette == NULL ) {
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        /* the note for SDL_GetRGBA above applies here too */
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        unsigned v;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		v = (pixel & fmt->Rmask) >> fmt->Rshift;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		v = (pixel & fmt->Gmask) >> fmt->Gshift;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		v = (pixel & fmt->Bmask) >> fmt->Bshift;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*r = fmt->palette->colors[pixel].r;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*g = fmt->palette->colors[pixel].g;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*b = fmt->palette->colors[pixel].b;
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Apply gamma to a set of colors - this is easy. :) */
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_ApplyGamma(Uint16 *gamma, SDL_Color *colors, SDL_Color *output,
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							int ncolors)
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<ncolors; ++i ) {
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		output[i].r = gamma[0*256 + colors[i].r] >> 8;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		output[i].g = gamma[1*256 + colors[i].g] >> 8;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		output[i].b = gamma[2*256 + colors[i].b] >> 8;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Map from Palette to Palette */
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *map;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( identical ) {
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( src->ncolors <= dst->ncolors ) {
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* If an identical palette, no need to map */
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_memcmp(src->colors, dst->colors, src->ncolors*
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						sizeof(SDL_Color)) == 0 ) {
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*identical = 1;
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(NULL);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*identical = 0;
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map = (Uint8 *)SDL_malloc(src->ncolors);
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( map == NULL ) {
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<src->ncolors; ++i ) {
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		map[i] = SDL_FindColor(dst,
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			src->colors[i].r, src->colors[i].g, src->colors[i].b);
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(map);
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Map from Palette to BitField */
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *Map1toN(SDL_PixelFormat *src, SDL_PixelFormat *dst)
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *map;
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int  bpp;
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned alpha;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Palette *pal = src->palette;
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map = (Uint8 *)SDL_malloc(pal->ncolors*bpp);
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( map == NULL ) {
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	alpha = dst->Amask ? src->alpha : 0;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We memory copy to the pixel map so the endianness is preserved */
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<pal->ncolors; ++i ) {
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ASSEMBLE_RGBA(&map[i*bpp], dst->BytesPerPixel, dst,
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      pal->colors[i].r, pal->colors[i].g,
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			      pal->colors[i].b, alpha);
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(map);
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Map from BitField to Dithered-Palette to Palette */
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Generate a 256 color dither palette */
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Palette dithered;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Color colors[256];
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Palette *pal = dst->palette;
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* SDL_DitherColors does not initialize the 'unused' component of colors,
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   but Map1to1 compares it against pal, so we should initialize it. */
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(colors, 0, sizeof(colors));
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dithered.ncolors = 256;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DitherColors(colors, 8);
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dithered.colors = colors;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(Map1to1(&dithered, pal, identical));
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_BlitMap *SDL_AllocBlitMap(void)
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_BlitMap *map;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the empty map */
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map = (SDL_BlitMap *)SDL_malloc(sizeof(*map));
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( map == NULL ) {
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(map, 0, sizeof(*map));
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the software blit data */
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map->sw_data = (struct private_swaccel *)SDL_malloc(sizeof(*map->sw_data));
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( map->sw_data == NULL ) {
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeBlitMap(map);
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(map->sw_data, 0, sizeof(*map->sw_data));
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* It's ready to go */
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(map);
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_InvalidateMap(SDL_BlitMap *map)
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! map ) {
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map->dst = NULL;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map->format_version = (unsigned int)-1;
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( map->table ) {
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(map->table);
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		map->table = NULL;
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_MapSurface (SDL_Surface *src, SDL_Surface *dst)
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_PixelFormat *srcfmt;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_PixelFormat *dstfmt;
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_BlitMap *map;
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clear out any previous mapping */
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map = src->map;
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (src->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnRLESurface(src, 1);
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_InvalidateMap(map);
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Figure out what kind of mapping we're doing */
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map->identity = 0;
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	srcfmt = src->format;
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstfmt = dst->format;
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (srcfmt->BytesPerPixel) {
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 1:
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (dstfmt->BytesPerPixel) {
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 1:
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Palette --> Palette */
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* If both SDL_HWSURFACE, assume have same palette */
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) &&
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) ) {
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				map->identity = 1;
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				map->table = Map1to1(srcfmt->palette,
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					dstfmt->palette, &map->identity);
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ! map->identity ) {
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( map->table == NULL ) {
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return(-1);
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (srcfmt->BitsPerPixel!=dstfmt->BitsPerPixel)
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				map->identity = 0;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    default:
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Palette --> BitField */
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			map->table = Map1toN(srcfmt, dstfmt);
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( map->table == NULL ) {
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(-1);
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	default:
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (dstfmt->BytesPerPixel) {
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 1:
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* BitField --> Palette */
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			map->table = MapNto1(srcfmt, dstfmt, &map->identity);
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ! map->identity ) {
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( map->table == NULL ) {
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					return(-1);
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			map->identity = 0;	/* Don't optimize to copy */
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    default:
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* BitField --> BitField */
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( FORMAT_EQUAL(srcfmt, dstfmt) )
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				map->identity = 1;
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map->dst = dst;
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map->format_version = dst->format_version;
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Choose your blitters wisely */
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_CalculateBlit(src));
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_FreeBlitMap(SDL_BlitMap *map)
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( map ) {
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_InvalidateMap(map);
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( map->sw_data != NULL ) {
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(map->sw_data);
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(map);
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
627