src/video/SDL_pixels.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Aug 2003 04:53:40 +0000
changeset 695 8f579a0e17e6
parent 688 c0522010bb6d
child 769 b8d311d90021
permissions -rw-r--r--
Whitespace
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* General (mostly internal) pixel/color manipulation routines for SDL */
slouken@0
    29
slouken@0
    30
#include <stdio.h>
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <string.h>
slouken@0
    33
slouken@0
    34
#include "SDL_error.h"
slouken@0
    35
#include "SDL_endian.h"
slouken@0
    36
#include "SDL_video.h"
slouken@0
    37
#include "SDL_sysvideo.h"
slouken@0
    38
#include "SDL_blit.h"
slouken@0
    39
#include "SDL_pixels_c.h"
slouken@0
    40
#include "SDL_RLEaccel_c.h"
slouken@0
    41
slouken@0
    42
/* Helper functions */
slouken@0
    43
/*
slouken@0
    44
 * Allocate a pixel format structure and fill it according to the given info.
slouken@0
    45
 */
slouken@0
    46
SDL_PixelFormat *SDL_AllocFormat(int bpp,
slouken@0
    47
			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
    48
{
slouken@0
    49
	SDL_PixelFormat *format;
slouken@0
    50
	Uint32 mask;
slouken@0
    51
slouken@0
    52
	/* Allocate an empty pixel format structure */
slouken@0
    53
	format = malloc(sizeof(*format));
slouken@0
    54
	if ( format == NULL ) {
slouken@0
    55
		SDL_OutOfMemory();
slouken@0
    56
		return(NULL);
slouken@0
    57
	}
slouken@0
    58
	memset(format, 0, sizeof(*format));
slouken@0
    59
	format->alpha = SDL_ALPHA_OPAQUE;
slouken@0
    60
slouken@0
    61
	/* Set up the format */
slouken@0
    62
	format->BitsPerPixel = bpp;
slouken@0
    63
	format->BytesPerPixel = (bpp+7)/8;
slouken@0
    64
	switch (bpp) {
slouken@0
    65
		case 1:
slouken@0
    66
			/* Create the 2 color black-white palette */
slouken@0
    67
			format->palette = (SDL_Palette *)malloc(
slouken@0
    68
							sizeof(SDL_Palette));
slouken@0
    69
			if ( format->palette == NULL ) {
slouken@0
    70
				SDL_FreeFormat(format);
slouken@0
    71
				SDL_OutOfMemory();
slouken@0
    72
				return(NULL);
slouken@0
    73
			}
slouken@0
    74
			(format->palette)->ncolors = 2;
slouken@0
    75
			(format->palette)->colors = (SDL_Color *)malloc(
slouken@0
    76
				(format->palette)->ncolors*sizeof(SDL_Color));
slouken@0
    77
			if ( (format->palette)->colors == NULL ) {
slouken@0
    78
				SDL_FreeFormat(format);
slouken@0
    79
				SDL_OutOfMemory();
slouken@0
    80
				return(NULL);
slouken@0
    81
			}
slouken@0
    82
			format->palette->colors[0].r = 0xFF;
slouken@0
    83
			format->palette->colors[0].g = 0xFF;
slouken@0
    84
			format->palette->colors[0].b = 0xFF;
slouken@0
    85
			format->palette->colors[1].r = 0x00;
slouken@0
    86
			format->palette->colors[1].g = 0x00;
slouken@0
    87
			format->palette->colors[1].b = 0x00;
slouken@0
    88
			format->Rloss = 8;
slouken@0
    89
			format->Gloss = 8;
slouken@0
    90
			format->Bloss = 8;
slouken@0
    91
			format->Aloss = 8;
slouken@0
    92
			format->Rshift = 0;
slouken@0
    93
			format->Gshift = 0;
slouken@0
    94
			format->Bshift = 0;
slouken@0
    95
			format->Ashift = 0;
slouken@0
    96
			format->Rmask = 0;
slouken@0
    97
			format->Gmask = 0;
slouken@0
    98
			format->Bmask = 0;
slouken@0
    99
			format->Amask = 0;
slouken@0
   100
			break;
slouken@0
   101
slouken@0
   102
		case 4:
slouken@0
   103
			/* Create the 16 color VGA palette */
slouken@0
   104
			format->palette = (SDL_Palette *)malloc(
slouken@0
   105
							sizeof(SDL_Palette));
slouken@0
   106
			if ( format->palette == NULL ) {
slouken@0
   107
				SDL_FreeFormat(format);
slouken@0
   108
				SDL_OutOfMemory();
slouken@0
   109
				return(NULL);
slouken@0
   110
			}
slouken@0
   111
			(format->palette)->ncolors = 16;
slouken@0
   112
			(format->palette)->colors = (SDL_Color *)malloc(
slouken@0
   113
				(format->palette)->ncolors*sizeof(SDL_Color));
slouken@0
   114
			if ( (format->palette)->colors == NULL ) {
slouken@0
   115
				SDL_FreeFormat(format);
slouken@0
   116
				SDL_OutOfMemory();
slouken@0
   117
				return(NULL);
slouken@0
   118
			}
slouken@0
   119
			/* Punt for now, will this ever be used? */
slouken@0
   120
			memset((format->palette)->colors, 0,
slouken@0
   121
				(format->palette)->ncolors*sizeof(SDL_Color));
slouken@0
   122
slouken@0
   123
			/* Palettized formats have no mask info */
slouken@0
   124
			format->Rloss = 8;
slouken@0
   125
			format->Gloss = 8;
slouken@0
   126
			format->Bloss = 8;
slouken@0
   127
			format->Aloss = 8;
slouken@0
   128
			format->Rshift = 0;
slouken@0
   129
			format->Gshift = 0;
slouken@0
   130
			format->Bshift = 0;
slouken@0
   131
			format->Ashift = 0;
slouken@0
   132
			format->Rmask = 0;
slouken@0
   133
			format->Gmask = 0;
slouken@0
   134
			format->Bmask = 0;
slouken@0
   135
			format->Amask = 0;
slouken@0
   136
			break;
slouken@0
   137
slouken@0
   138
		case 8:
slouken@0
   139
			/* Create an empty 256 color palette */
slouken@0
   140
			format->palette = (SDL_Palette *)malloc(
slouken@0
   141
							sizeof(SDL_Palette));
slouken@0
   142
			if ( format->palette == NULL ) {
slouken@0
   143
				SDL_FreeFormat(format);
slouken@0
   144
				SDL_OutOfMemory();
slouken@0
   145
				return(NULL);
slouken@0
   146
			}
slouken@0
   147
			(format->palette)->ncolors = 256;
slouken@0
   148
			(format->palette)->colors = (SDL_Color *)malloc(
slouken@0
   149
				(format->palette)->ncolors*sizeof(SDL_Color));
slouken@0
   150
			if ( (format->palette)->colors == NULL ) {
slouken@0
   151
				SDL_FreeFormat(format);
slouken@0
   152
				SDL_OutOfMemory();
slouken@0
   153
				return(NULL);
slouken@0
   154
			}
slouken@0
   155
			memset((format->palette)->colors, 0,
slouken@0
   156
				(format->palette)->ncolors*sizeof(SDL_Color));
slouken@0
   157
slouken@0
   158
			/* Palettized formats have no mask info */
slouken@0
   159
			format->Rloss = 8;
slouken@0
   160
			format->Gloss = 8;
slouken@0
   161
			format->Bloss = 8;
slouken@0
   162
			format->Aloss = 8;
slouken@0
   163
			format->Rshift = 0;
slouken@0
   164
			format->Gshift = 0;
slouken@0
   165
			format->Bshift = 0;
slouken@0
   166
			format->Ashift = 0;
slouken@0
   167
			format->Rmask = 0;
slouken@0
   168
			format->Gmask = 0;
slouken@0
   169
			format->Bmask = 0;
slouken@0
   170
			format->Amask = 0;
slouken@0
   171
			break;
slouken@0
   172
slouken@0
   173
		default:
slouken@0
   174
			/* No palette, just packed pixel info */
slouken@0
   175
			format->palette = NULL;
slouken@0
   176
			format->Rshift = 0;
slouken@0
   177
			format->Rloss = 8;
slouken@0
   178
			if ( Rmask ) {
slouken@0
   179
				for ( mask = Rmask; !(mask&0x01); mask >>= 1 )
slouken@0
   180
					++format->Rshift;
slouken@0
   181
				for ( ; (mask&0x01); mask >>= 1 )
slouken@0
   182
					--format->Rloss;
slouken@0
   183
			}
slouken@0
   184
			format->Gshift = 0;
slouken@0
   185
			format->Gloss = 8;
slouken@0
   186
			if ( Gmask ) {
slouken@0
   187
				for ( mask = Gmask; !(mask&0x01); mask >>= 1 )
slouken@0
   188
					++format->Gshift;
slouken@0
   189
				for ( ; (mask&0x01); mask >>= 1 )
slouken@0
   190
					--format->Gloss;
slouken@0
   191
			}
slouken@0
   192
			format->Bshift = 0;
slouken@0
   193
			format->Bloss = 8;
slouken@0
   194
			if ( Bmask ) {
slouken@0
   195
				for ( mask = Bmask; !(mask&0x01); mask >>= 1 )
slouken@0
   196
					++format->Bshift;
slouken@0
   197
				for ( ; (mask&0x01); mask >>= 1 )
slouken@0
   198
					--format->Bloss;
slouken@0
   199
			}
slouken@0
   200
			format->Ashift = 0;
slouken@0
   201
			format->Aloss = 8;
slouken@0
   202
			if ( Amask ) {
slouken@0
   203
				for ( mask = Amask; !(mask&0x01); mask >>= 1 )
slouken@0
   204
					++format->Ashift;
slouken@0
   205
				for ( ; (mask&0x01); mask >>= 1 )
slouken@0
   206
					--format->Aloss;
slouken@0
   207
			}
slouken@0
   208
			format->Rmask = Rmask;
slouken@0
   209
			format->Gmask = Gmask;
slouken@0
   210
			format->Bmask = Bmask;
slouken@0
   211
			format->Amask = Amask;
slouken@0
   212
			break;
slouken@0
   213
	}
slouken@0
   214
	/* Calculate some standard bitmasks, if necessary 
slouken@0
   215
	 * Note:  This could conflict with an alpha mask, if given.
slouken@0
   216
	 */
slouken@0
   217
	if ( (bpp > 8) && !format->Rmask && !format->Gmask && !format->Bmask ) {
slouken@0
   218
		/* R-G-B */
slouken@0
   219
		if ( bpp > 24 )
slouken@0
   220
			bpp = 24;
slouken@0
   221
		format->Rloss = 8-(bpp/3);
slouken@0
   222
		format->Gloss = 8-(bpp/3)-(bpp%3);
slouken@0
   223
		format->Bloss = 8-(bpp/3);
slouken@0
   224
		format->Rshift = ((bpp/3)+(bpp%3))+(bpp/3);
slouken@0
   225
		format->Gshift = (bpp/3);
slouken@0
   226
		format->Bshift = 0;
slouken@0
   227
		format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift);
slouken@0
   228
		format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift);
slouken@0
   229
		format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift);
slouken@0
   230
	}
slouken@0
   231
	return(format);
slouken@0
   232
}
slouken@0
   233
SDL_PixelFormat *SDL_ReallocFormat(SDL_Surface *surface, int bpp,
slouken@0
   234
			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
   235
{
slouken@0
   236
	if ( surface->format ) {
slouken@0
   237
		SDL_FreeFormat(surface->format);
slouken@0
   238
		SDL_FormatChanged(surface);
slouken@0
   239
	}
slouken@0
   240
	surface->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
slouken@0
   241
	return surface->format;
slouken@0
   242
}
slouken@0
   243
slouken@0
   244
/*
slouken@0
   245
 * Change any previous mappings from/to the new surface format
slouken@0
   246
 */
slouken@0
   247
void SDL_FormatChanged(SDL_Surface *surface)
slouken@0
   248
{
slouken@0
   249
	surface->format_version++;
slouken@0
   250
	SDL_InvalidateMap(surface->map);
slouken@0
   251
}
slouken@0
   252
/*
slouken@0
   253
 * Free a previously allocated format structure
slouken@0
   254
 */
slouken@0
   255
void SDL_FreeFormat(SDL_PixelFormat *format)
slouken@0
   256
{
slouken@0
   257
	if ( format ) {
slouken@0
   258
		if ( format->palette ) {
slouken@0
   259
			if ( format->palette->colors ) {
slouken@0
   260
				free(format->palette->colors);
slouken@0
   261
			}
slouken@0
   262
			free(format->palette);
slouken@0
   263
		}
slouken@0
   264
		free(format);
slouken@0
   265
	}
slouken@0
   266
}
slouken@0
   267
/*
slouken@0
   268
 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
slouken@0
   269
 */
slouken@0
   270
void SDL_DitherColors(SDL_Color *colors, int bpp)
slouken@0
   271
{
slouken@0
   272
	int i;
slouken@0
   273
	if(bpp != 8)
slouken@0
   274
		return;		/* only 8bpp supported right now */
slouken@0
   275
slouken@0
   276
	for(i = 0; i < 256; i++) {
slouken@0
   277
		int r, g, b;
slouken@0
   278
		/* map each bit field to the full [0, 255] interval,
slouken@0
   279
		   so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
slouken@0
   280
		r = i & 0xe0;
slouken@0
   281
		r |= r >> 3 | r >> 6;
slouken@0
   282
		colors[i].r = r;
slouken@0
   283
		g = (i << 3) & 0xe0;
slouken@0
   284
		g |= g >> 3 | g >> 6;
slouken@0
   285
		colors[i].g = g;
slouken@0
   286
		b = i & 0x3;
slouken@0
   287
		b |= b << 2;
slouken@0
   288
		b |= b << 4;
slouken@0
   289
		colors[i].b = b;
slouken@0
   290
	}
slouken@0
   291
}
slouken@0
   292
/* 
slouken@0
   293
 * Calculate the pad-aligned scanline width of a surface
slouken@0
   294
 */
slouken@0
   295
Uint16 SDL_CalculatePitch(SDL_Surface *surface)
slouken@0
   296
{
slouken@0
   297
	Uint16 pitch;
slouken@0
   298
slouken@0
   299
	/* Surface should be 4-byte aligned for speed */
slouken@0
   300
	pitch = surface->w*surface->format->BytesPerPixel;
slouken@0
   301
	switch (surface->format->BitsPerPixel) {
slouken@0
   302
		case 1:
slouken@0
   303
			pitch = (pitch+7)/8;
slouken@0
   304
			break;
slouken@0
   305
		case 4:
slouken@0
   306
			pitch = (pitch+1)/2;
slouken@0
   307
			break;
slouken@0
   308
		default:
slouken@0
   309
			break;
slouken@0
   310
	}
slouken@0
   311
	pitch = (pitch + 3) & ~3;	/* 4-byte aligning */
slouken@0
   312
	return(pitch);
slouken@0
   313
}
slouken@0
   314
/*
slouken@0
   315
 * Match an RGB value to a particular palette index
slouken@0
   316
 */
slouken@0
   317
Uint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   318
{
slouken@0
   319
	/* Do colorspace distance matching */
slouken@0
   320
	unsigned int smallest;
slouken@0
   321
	unsigned int distance;
slouken@0
   322
	int rd, gd, bd;
slouken@0
   323
	int i;
slouken@0
   324
	Uint8 pixel=0;
slouken@0
   325
		
slouken@0
   326
	smallest = ~0;
slouken@0
   327
	for ( i=0; i<pal->ncolors; ++i ) {
slouken@0
   328
		rd = pal->colors[i].r - r;
slouken@0
   329
		gd = pal->colors[i].g - g;
slouken@0
   330
		bd = pal->colors[i].b - b;
slouken@0
   331
		distance = (rd*rd)+(gd*gd)+(bd*bd);
slouken@0
   332
		if ( distance < smallest ) {
slouken@0
   333
			pixel = i;
slouken@0
   334
			if ( distance == 0 ) { /* Perfect match! */
slouken@0
   335
				break;
slouken@0
   336
			}
slouken@0
   337
			smallest = distance;
slouken@0
   338
		}
slouken@0
   339
	}
slouken@0
   340
	return(pixel);
slouken@0
   341
}
slouken@0
   342
slouken@0
   343
/* Find the opaque pixel value corresponding to an RGB triple */
slouken@0
   344
Uint32 SDL_MapRGB(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   345
{
slouken@0
   346
	if ( format->palette == NULL ) {
slouken@0
   347
		return (r >> format->Rloss) << format->Rshift
slouken@0
   348
		       | (g >> format->Gloss) << format->Gshift
slouken@0
   349
		       | (b >> format->Bloss) << format->Bshift
slouken@0
   350
		       | format->Amask;
slouken@0
   351
	} else {
slouken@0
   352
		return SDL_FindColor(format->palette, r, g, b);
slouken@0
   353
	}
slouken@0
   354
}
slouken@0
   355
slouken@0
   356
/* Find the pixel value corresponding to an RGBA quadruple */
slouken@0
   357
Uint32 SDL_MapRGBA(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@0
   358
{
slouken@0
   359
	if ( format->palette == NULL ) {
slouken@0
   360
	        return (r >> format->Rloss) << format->Rshift
slouken@0
   361
		    | (g >> format->Gloss) << format->Gshift
slouken@0
   362
		    | (b >> format->Bloss) << format->Bshift
slouken@0
   363
		    | ((a >> format->Aloss) << format->Ashift & format->Amask);
slouken@0
   364
	} else {
slouken@0
   365
		return SDL_FindColor(format->palette, r, g, b);
slouken@0
   366
	}
slouken@0
   367
}
slouken@0
   368
slouken@0
   369
void SDL_GetRGBA(Uint32 pixel, SDL_PixelFormat *fmt,
slouken@0
   370
		 Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
slouken@0
   371
{
slouken@0
   372
	if ( fmt->palette == NULL ) {
slouken@0
   373
	        /*
slouken@0
   374
		 * This makes sure that the result is mapped to the
slouken@0
   375
		 * interval [0..255], and the maximum value for each
slouken@0
   376
		 * component is 255. This is important to make sure
slouken@0
   377
		 * that white is indeed reported as (255, 255, 255),
slouken@0
   378
		 * and that opaque alpha is 255.
slouken@0
   379
		 * This only works for RGB bit fields at least 4 bit
slouken@0
   380
		 * wide, which is almost always the case.
slouken@0
   381
		 */
slouken@628
   382
	        unsigned v;
slouken@628
   383
		v = (pixel & fmt->Rmask) >> fmt->Rshift;
slouken@688
   384
		*r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
slouken@628
   385
		v = (pixel & fmt->Gmask) >> fmt->Gshift;
slouken@688
   386
		*g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
slouken@628
   387
		v = (pixel & fmt->Bmask) >> fmt->Bshift;
slouken@688
   388
		*b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
slouken@0
   389
		if(fmt->Amask) {
slouken@628
   390
		        v = (pixel & fmt->Amask) >> fmt->Ashift;
slouken@688
   391
			*a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1)));
slouken@695
   392
		} else {
slouken@0
   393
		        *a = SDL_ALPHA_OPAQUE;
slouken@695
   394
                }
slouken@0
   395
	} else {
slouken@0
   396
		*r = fmt->palette->colors[pixel].r;
slouken@0
   397
		*g = fmt->palette->colors[pixel].g;
slouken@0
   398
		*b = fmt->palette->colors[pixel].b;
slouken@0
   399
		*a = SDL_ALPHA_OPAQUE;
slouken@0
   400
	}
slouken@0
   401
}
slouken@0
   402
slouken@0
   403
void SDL_GetRGB(Uint32 pixel, SDL_PixelFormat *fmt, Uint8 *r,Uint8 *g,Uint8 *b)
slouken@0
   404
{
slouken@0
   405
	if ( fmt->palette == NULL ) {
slouken@0
   406
	        /* the note for SDL_GetRGBA above applies here too */
slouken@628
   407
	        unsigned v;
slouken@628
   408
		v = (pixel & fmt->Rmask) >> fmt->Rshift;
slouken@688
   409
		*r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
slouken@628
   410
		v = (pixel & fmt->Gmask) >> fmt->Gshift;
slouken@688
   411
		*g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
slouken@628
   412
		v = (pixel & fmt->Bmask) >> fmt->Bshift;
slouken@688
   413
		*b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
slouken@0
   414
	} else {
slouken@0
   415
		*r = fmt->palette->colors[pixel].r;
slouken@0
   416
		*g = fmt->palette->colors[pixel].g;
slouken@0
   417
		*b = fmt->palette->colors[pixel].b;
slouken@0
   418
	}
slouken@0
   419
}
slouken@0
   420
slouken@0
   421
/* Apply gamma to a set of colors - this is easy. :) */
slouken@0
   422
void SDL_ApplyGamma(Uint16 *gamma, SDL_Color *colors, SDL_Color *output,
slouken@0
   423
							int ncolors)
slouken@0
   424
{
slouken@0
   425
	int i;
slouken@0
   426
slouken@0
   427
	for ( i=0; i<ncolors; ++i ) {
slouken@0
   428
		output[i].r = gamma[0*256 + colors[i].r] >> 8;
slouken@0
   429
		output[i].g = gamma[1*256 + colors[i].g] >> 8;
slouken@0
   430
		output[i].b = gamma[2*256 + colors[i].b] >> 8;
slouken@0
   431
	}
slouken@0
   432
}
slouken@0
   433
slouken@0
   434
/* Map from Palette to Palette */
slouken@0
   435
static Uint8 *Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
slouken@0
   436
{
slouken@0
   437
	Uint8 *map;
slouken@0
   438
	int i;
slouken@0
   439
slouken@0
   440
	if ( identical ) {
slouken@0
   441
		if ( src->ncolors <= dst->ncolors ) {
slouken@0
   442
			/* If an identical palette, no need to map */
slouken@0
   443
			if ( memcmp(src->colors, dst->colors, src->ncolors*
slouken@0
   444
						sizeof(SDL_Color)) == 0 ) {
slouken@0
   445
				*identical = 1;
slouken@0
   446
				return(NULL);
slouken@0
   447
			}
slouken@0
   448
		}
slouken@0
   449
		*identical = 0;
slouken@0
   450
	}
slouken@0
   451
	map = (Uint8 *)malloc(src->ncolors);
slouken@0
   452
	if ( map == NULL ) {
slouken@0
   453
		SDL_OutOfMemory();
slouken@0
   454
		return(NULL);
slouken@0
   455
	}
slouken@0
   456
	for ( i=0; i<src->ncolors; ++i ) {
slouken@0
   457
		map[i] = SDL_FindColor(dst,
slouken@0
   458
			src->colors[i].r, src->colors[i].g, src->colors[i].b);
slouken@0
   459
	}
slouken@0
   460
	return(map);
slouken@0
   461
}
slouken@0
   462
/* Map from Palette to BitField */
slouken@0
   463
static Uint8 *Map1toN(SDL_Palette *src, SDL_PixelFormat *dst)
slouken@0
   464
{
slouken@0
   465
	Uint8 *map;
slouken@0
   466
	int i;
slouken@0
   467
	int  bpp;
slouken@50
   468
	unsigned alpha;
slouken@0
   469
slouken@0
   470
	bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
slouken@0
   471
	map = (Uint8 *)malloc(src->ncolors*bpp);
slouken@0
   472
	if ( map == NULL ) {
slouken@0
   473
		SDL_OutOfMemory();
slouken@0
   474
		return(NULL);
slouken@0
   475
	}
slouken@0
   476
slouken@50
   477
	alpha = dst->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
   478
	/* We memory copy to the pixel map so the endianness is preserved */
slouken@0
   479
	for ( i=0; i<src->ncolors; ++i ) {
slouken@0
   480
		ASSEMBLE_RGBA(&map[i*bpp], dst->BytesPerPixel, dst,
slouken@0
   481
			      src->colors[i].r, src->colors[i].g,
slouken@50
   482
			      src->colors[i].b, alpha);
slouken@0
   483
	}
slouken@0
   484
	return(map);
slouken@0
   485
}
slouken@0
   486
/* Map from BitField to Dithered-Palette to Palette */
slouken@0
   487
static Uint8 *MapNto1(SDL_PixelFormat *src, SDL_Palette *dst, int *identical)
slouken@0
   488
{
slouken@0
   489
	/* Generate a 256 color dither palette */
slouken@0
   490
	SDL_Palette dithered;
slouken@0
   491
	SDL_Color colors[256];
slouken@0
   492
slouken@0
   493
	dithered.ncolors = 256;
slouken@0
   494
	SDL_DitherColors(colors, 8);
slouken@0
   495
	dithered.colors = colors;
slouken@0
   496
	return(Map1to1(&dithered, dst, identical));
slouken@0
   497
}
slouken@0
   498
slouken@0
   499
SDL_BlitMap *SDL_AllocBlitMap(void)
slouken@0
   500
{
slouken@0
   501
	SDL_BlitMap *map;
slouken@0
   502
slouken@0
   503
	/* Allocate the empty map */
slouken@0
   504
	map = (SDL_BlitMap *)malloc(sizeof(*map));
slouken@0
   505
	if ( map == NULL ) {
slouken@0
   506
		SDL_OutOfMemory();
slouken@0
   507
		return(NULL);
slouken@0
   508
	}
slouken@0
   509
	memset(map, 0, sizeof(*map));
slouken@0
   510
slouken@0
   511
	/* Allocate the software blit data */
slouken@0
   512
	map->sw_data = (struct private_swaccel *)malloc(sizeof(*map->sw_data));
slouken@0
   513
	if ( map->sw_data == NULL ) {
slouken@0
   514
		SDL_FreeBlitMap(map);
slouken@0
   515
		SDL_OutOfMemory();
slouken@0
   516
		return(NULL);
slouken@0
   517
	}
slouken@0
   518
	memset(map->sw_data, 0, sizeof(*map->sw_data));
slouken@0
   519
slouken@0
   520
	/* It's ready to go */
slouken@0
   521
	return(map);
slouken@0
   522
}
slouken@0
   523
void SDL_InvalidateMap(SDL_BlitMap *map)
slouken@0
   524
{
slouken@0
   525
	if ( ! map ) {
slouken@0
   526
		return;
slouken@0
   527
	}
slouken@0
   528
	map->dst = NULL;
slouken@0
   529
	map->format_version = (unsigned int)-1;
slouken@0
   530
	if ( map->table ) {
slouken@0
   531
		free(map->table);
slouken@0
   532
		map->table = NULL;
slouken@0
   533
	}
slouken@0
   534
}
slouken@0
   535
int SDL_MapSurface (SDL_Surface *src, SDL_Surface *dst)
slouken@0
   536
{
slouken@0
   537
	SDL_PixelFormat *srcfmt;
slouken@0
   538
	SDL_PixelFormat *dstfmt;
slouken@0
   539
	SDL_BlitMap *map;
slouken@0
   540
slouken@0
   541
	/* Clear out any previous mapping */
slouken@0
   542
	map = src->map;
slouken@0
   543
	if ( (src->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
   544
		SDL_UnRLESurface(src, 1);
slouken@0
   545
	}
slouken@0
   546
	SDL_InvalidateMap(map);
slouken@0
   547
slouken@0
   548
	/* Figure out what kind of mapping we're doing */
slouken@0
   549
	map->identity = 0;
slouken@0
   550
	srcfmt = src->format;
slouken@0
   551
	dstfmt = dst->format;
slouken@0
   552
	switch (srcfmt->BytesPerPixel) {
slouken@0
   553
	    case 1:
slouken@0
   554
		switch (dstfmt->BytesPerPixel) {
slouken@0
   555
		    case 1:
slouken@0
   556
			/* Palette --> Palette */
slouken@0
   557
			/* If both SDL_HWSURFACE, assume have same palette */
slouken@0
   558
			if ( ((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) &&
slouken@0
   559
			     ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) ) {
slouken@0
   560
				map->identity = 1;
slouken@0
   561
			} else {
slouken@0
   562
				map->table = Map1to1(srcfmt->palette,
slouken@0
   563
					dstfmt->palette, &map->identity);
slouken@0
   564
			}
slouken@0
   565
			if ( ! map->identity ) {
slouken@0
   566
				if ( map->table == NULL ) {
slouken@0
   567
					return(-1);
slouken@0
   568
				}
slouken@0
   569
			}
slouken@0
   570
			if (srcfmt->BitsPerPixel!=dstfmt->BitsPerPixel)
slouken@0
   571
				map->identity = 0;
slouken@0
   572
			break;
slouken@0
   573
slouken@0
   574
		    default:
slouken@0
   575
			/* Palette --> BitField */
slouken@0
   576
			map->table = Map1toN(srcfmt->palette, dstfmt);
slouken@0
   577
			if ( map->table == NULL ) {
slouken@0
   578
				return(-1);
slouken@0
   579
			}
slouken@0
   580
			break;
slouken@0
   581
		}
slouken@0
   582
		break;
slouken@0
   583
	default:
slouken@0
   584
		switch (dstfmt->BytesPerPixel) {
slouken@0
   585
		    case 1:
slouken@0
   586
			/* BitField --> Palette */
slouken@0
   587
			map->table = MapNto1(srcfmt,
slouken@0
   588
					dstfmt->palette, &map->identity);
slouken@0
   589
			if ( ! map->identity ) {
slouken@0
   590
				if ( map->table == NULL ) {
slouken@0
   591
					return(-1);
slouken@0
   592
				}
slouken@0
   593
			}
slouken@0
   594
			map->identity = 0;	/* Don't optimize to copy */
slouken@0
   595
			break;
slouken@0
   596
		    default:
slouken@0
   597
			/* BitField --> BitField */
slouken@0
   598
			if ( FORMAT_EQUAL(srcfmt, dstfmt) )
slouken@0
   599
				map->identity = 1;
slouken@0
   600
			break;
slouken@0
   601
		}
slouken@0
   602
		break;
slouken@0
   603
	}
slouken@0
   604
slouken@0
   605
	map->dst = dst;
slouken@0
   606
	map->format_version = dst->format_version;
slouken@0
   607
slouken@0
   608
	/* Choose your blitters wisely */
slouken@0
   609
	return(SDL_CalculateBlit(src));
slouken@0
   610
}
slouken@0
   611
void SDL_FreeBlitMap(SDL_BlitMap *map)
slouken@0
   612
{
slouken@0
   613
	if ( map ) {
slouken@0
   614
		SDL_InvalidateMap(map);
slouken@0
   615
		if ( map->sw_data != NULL ) {
slouken@0
   616
			free(map->sw_data);
slouken@0
   617
		}
slouken@0
   618
		free(map);
slouken@0
   619
	}
slouken@0
   620
}