src/video/SDL_pixels.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 6137 4720145f848b
child 12653 9b0e5c555c0f
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

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