2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* General (mostly internal) pixel/color manipulation routines for SDL */
26 #include "SDL_endian.h"
27 #include "SDL_video.h"
28 #include "SDL_sysvideo.h"
30 #include "SDL_pixels_c.h"
31 #include "SDL_RLEaccel_c.h"
33 /* Helper functions */
35 * Allocate a pixel format structure and fill it according to the given info.
37 SDL_PixelFormat *SDL_AllocFormat(int bpp,
38 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
40 SDL_PixelFormat *format;
43 /* Allocate an empty pixel format structure */
44 format = SDL_malloc(sizeof(*format));
45 if ( format == NULL ) {
49 SDL_memset(format, 0, sizeof(*format));
50 format->alpha = SDL_ALPHA_OPAQUE;
52 /* Set up the format */
53 format->BitsPerPixel = bpp;
54 format->BytesPerPixel = (bpp+7)/8;
55 if ( Rmask || Bmask || Gmask ) { /* Packed pixels with custom mask */
56 format->palette = NULL;
60 for ( mask = Rmask; !(mask&0x01); mask >>= 1 )
62 for ( ; (mask&0x01); mask >>= 1 )
68 for ( mask = Gmask; !(mask&0x01); mask >>= 1 )
70 for ( ; (mask&0x01); mask >>= 1 )
76 for ( mask = Bmask; !(mask&0x01); mask >>= 1 )
78 for ( ; (mask&0x01); mask >>= 1 )
84 for ( mask = Amask; !(mask&0x01); mask >>= 1 )
86 for ( ; (mask&0x01); mask >>= 1 )
89 format->Rmask = Rmask;
90 format->Gmask = Gmask;
91 format->Bmask = Bmask;
92 format->Amask = Amask;
93 } else if ( bpp > 8 ) { /* Packed pixels with standard mask */
97 format->Rloss = 8-(bpp/3);
98 format->Gloss = 8-(bpp/3)-(bpp%3);
99 format->Bloss = 8-(bpp/3);
100 format->Rshift = ((bpp/3)+(bpp%3))+(bpp/3);
101 format->Gshift = (bpp/3);
103 format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift);
104 format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift);
105 format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift);
107 /* Palettized formats have no mask info */
121 if ( bpp <= 8 ) { /* Palettized mode */
122 int ncolors = 1<<bpp;
124 fprintf(stderr,"bpp=%d ncolors=%d\n",bpp,ncolors);
126 format->palette = (SDL_Palette *)SDL_malloc(sizeof(SDL_Palette));
127 if ( format->palette == NULL ) {
128 SDL_FreeFormat(format);
132 (format->palette)->ncolors = ncolors;
133 (format->palette)->colors = (SDL_Color *)SDL_malloc(
134 (format->palette)->ncolors*sizeof(SDL_Color));
135 if ( (format->palette)->colors == NULL ) {
136 SDL_FreeFormat(format);
140 if ( Rmask || Bmask || Gmask ) {
141 /* create palette according to masks */
145 #ifdef ENABLE_PALETTE_ALPHA
151 for(i=format->Rloss;i>0;i-=Rw)
155 fprintf(stderr,"Rw=%d Rm=0x%02X\n",Rw,Rm);
160 for(i=format->Gloss;i>0;i-=Gw)
164 fprintf(stderr,"Gw=%d Gm=0x%02X\n",Gw,Gm);
169 for(i=format->Bloss;i>0;i-=Bw)
173 fprintf(stderr,"Bw=%d Bm=0x%02X\n",Bw,Bm);
175 #ifdef ENABLE_PALETTE_ALPHA
179 for(i=format->Aloss;i>0;i-=Aw)
182 # ifdef DEBUG_PALETTE
183 fprintf(stderr,"Aw=%d Am=0x%02X\n",Aw,Am);
186 for(i=0; i < ncolors; ++i) {
188 r=(i&Rmask)>>format->Rshift;
189 r=(r<<format->Rloss)|((r*Rm)>>Rw);
190 format->palette->colors[i].r=r;
192 g=(i&Gmask)>>format->Gshift;
193 g=(g<<format->Gloss)|((g*Gm)>>Gw);
194 format->palette->colors[i].g=g;
196 b=(i&Bmask)>>format->Bshift;
197 b=(b<<format->Bloss)|((b*Bm)>>Bw);
198 format->palette->colors[i].b=b;
200 #ifdef ENABLE_PALETTE_ALPHA
201 a=(i&Amask)>>format->Ashift;
202 a=(a<<format->Aloss)|((a*Am)>>Aw);
203 format->palette->colors[i].unused=a;
205 format->palette->colors[i].unused=0;
208 } else if ( ncolors == 2 ) {
209 /* Create a black and white bitmap palette */
210 format->palette->colors[0].r = 0xFF;
211 format->palette->colors[0].g = 0xFF;
212 format->palette->colors[0].b = 0xFF;
213 format->palette->colors[1].r = 0x00;
214 format->palette->colors[1].g = 0x00;
215 format->palette->colors[1].b = 0x00;
217 /* Create an empty palette */
218 SDL_memset((format->palette)->colors, 0,
219 (format->palette)->ncolors*sizeof(SDL_Color));
224 SDL_PixelFormat *SDL_ReallocFormat(SDL_Surface *surface, int bpp,
225 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
227 if ( surface->format ) {
228 SDL_FreeFormat(surface->format);
229 SDL_FormatChanged(surface);
231 surface->format = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
232 return surface->format;
236 * Change any previous mappings from/to the new surface format
238 void SDL_FormatChanged(SDL_Surface *surface)
240 static int format_version = 0;
242 if ( format_version < 0 ) { /* It wrapped... */
245 surface->format_version = format_version;
246 SDL_InvalidateMap(surface->map);
249 * Free a previously allocated format structure
251 void SDL_FreeFormat(SDL_PixelFormat *format)
254 if ( format->palette ) {
255 if ( format->palette->colors ) {
256 SDL_free(format->palette->colors);
258 SDL_free(format->palette);
264 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
266 void SDL_DitherColors(SDL_Color *colors, int bpp)
270 return; /* only 8bpp supported right now */
272 for(i = 0; i < 256; i++) {
274 /* map each bit field to the full [0, 255] interval,
275 so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
277 r |= r >> 3 | r >> 6;
280 g |= g >> 3 | g >> 6;
289 * Calculate the pad-aligned scanline width of a surface
291 Uint16 SDL_CalculatePitch(SDL_Surface *surface)
295 /* Surface should be 4-byte aligned for speed */
296 pitch = surface->w*surface->format->BytesPerPixel;
297 switch (surface->format->BitsPerPixel) {
307 pitch = (pitch + 3) & ~3; /* 4-byte aligning */
311 * Match an RGB value to a particular palette index
313 Uint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b)
315 /* Do colorspace distance matching */
316 unsigned int smallest;
317 unsigned int distance;
323 for ( i=0; i<pal->ncolors; ++i ) {
324 rd = pal->colors[i].r - r;
325 gd = pal->colors[i].g - g;
326 bd = pal->colors[i].b - b;
327 distance = (rd*rd)+(gd*gd)+(bd*bd);
328 if ( distance < smallest ) {
330 if ( distance == 0 ) { /* Perfect match! */
339 /* Find the opaque pixel value corresponding to an RGB triple */
341 (const SDL_PixelFormat * const format,
342 const Uint8 r, const Uint8 g, const Uint8 b)
344 if ( format->palette == NULL ) {
345 return (r >> format->Rloss) << format->Rshift
346 | (g >> format->Gloss) << format->Gshift
347 | (b >> format->Bloss) << format->Bshift
350 return SDL_FindColor(format->palette, r, g, b);
354 /* Find the pixel value corresponding to an RGBA quadruple */
356 (const SDL_PixelFormat * const format,
357 const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
359 if ( format->palette == NULL ) {
360 return (r >> format->Rloss) << format->Rshift
361 | (g >> format->Gloss) << format->Gshift
362 | (b >> format->Bloss) << format->Bshift
363 | ((a >> format->Aloss) << format->Ashift & format->Amask);
365 return SDL_FindColor(format->palette, r, g, b);
369 void SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * const fmt,
370 Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
372 if ( fmt->palette == NULL ) {
374 * This makes sure that the result is mapped to the
375 * interval [0..255], and the maximum value for each
376 * component is 255. This is important to make sure
377 * that white is indeed reported as (255, 255, 255),
378 * and that opaque alpha is 255.
379 * This only works for RGB bit fields at least 4 bit
380 * wide, which is almost always the case.
383 v = (pixel & fmt->Rmask) >> fmt->Rshift;
384 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
385 v = (pixel & fmt->Gmask) >> fmt->Gshift;
386 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
387 v = (pixel & fmt->Bmask) >> fmt->Bshift;
388 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
390 v = (pixel & fmt->Amask) >> fmt->Ashift;
391 *a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1)));
393 *a = SDL_ALPHA_OPAQUE;
396 *r = fmt->palette->colors[pixel].r;
397 *g = fmt->palette->colors[pixel].g;
398 *b = fmt->palette->colors[pixel].b;
399 *a = SDL_ALPHA_OPAQUE;
403 void SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * const fmt,
404 Uint8 *r,Uint8 *g,Uint8 *b)
406 if ( fmt->palette == NULL ) {
407 /* the note for SDL_GetRGBA above applies here too */
409 v = (pixel & fmt->Rmask) >> fmt->Rshift;
410 *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
411 v = (pixel & fmt->Gmask) >> fmt->Gshift;
412 *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
413 v = (pixel & fmt->Bmask) >> fmt->Bshift;
414 *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
416 *r = fmt->palette->colors[pixel].r;
417 *g = fmt->palette->colors[pixel].g;
418 *b = fmt->palette->colors[pixel].b;
422 /* Apply gamma to a set of colors - this is easy. :) */
423 void SDL_ApplyGamma(Uint16 *gamma, SDL_Color *colors, SDL_Color *output,
428 for ( i=0; i<ncolors; ++i ) {
429 output[i].r = gamma[0*256 + colors[i].r] >> 8;
430 output[i].g = gamma[1*256 + colors[i].g] >> 8;
431 output[i].b = gamma[2*256 + colors[i].b] >> 8;
435 /* Map from Palette to Palette */
436 static Uint8 *Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
442 if ( src->ncolors <= dst->ncolors ) {
443 /* If an identical palette, no need to map */
444 if ( SDL_memcmp(src->colors, dst->colors, src->ncolors*
445 sizeof(SDL_Color)) == 0 ) {
452 map = (Uint8 *)SDL_malloc(src->ncolors);
457 for ( i=0; i<src->ncolors; ++i ) {
458 map[i] = SDL_FindColor(dst,
459 src->colors[i].r, src->colors[i].g, src->colors[i].b);
463 /* Map from Palette to BitField */
464 static Uint8 *Map1toN(SDL_PixelFormat *src, SDL_PixelFormat *dst)
470 SDL_Palette *pal = src->palette;
472 bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
473 map = (Uint8 *)SDL_malloc(pal->ncolors*bpp);
479 alpha = dst->Amask ? src->alpha : 0;
480 /* We memory copy to the pixel map so the endianness is preserved */
481 for ( i=0; i<pal->ncolors; ++i ) {
482 ASSEMBLE_RGBA(&map[i*bpp], dst->BytesPerPixel, dst,
483 pal->colors[i].r, pal->colors[i].g,
484 pal->colors[i].b, alpha);
488 /* Map from BitField to Dithered-Palette to Palette */
489 static Uint8 *MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
491 /* Generate a 256 color dither palette */
492 SDL_Palette dithered;
493 SDL_Color colors[256];
494 SDL_Palette *pal = dst->palette;
496 /* SDL_DitherColors does not initialize the 'unused' component of colors,
497 but Map1to1 compares it against pal, so we should initialize it. */
498 SDL_memset(colors, 0, sizeof(colors));
500 dithered.ncolors = 256;
501 SDL_DitherColors(colors, 8);
502 dithered.colors = colors;
503 return(Map1to1(&dithered, pal, identical));
506 SDL_BlitMap *SDL_AllocBlitMap(void)
510 /* Allocate the empty map */
511 map = (SDL_BlitMap *)SDL_malloc(sizeof(*map));
516 SDL_memset(map, 0, sizeof(*map));
518 /* Allocate the software blit data */
519 map->sw_data = (struct private_swaccel *)SDL_malloc(sizeof(*map->sw_data));
520 if ( map->sw_data == NULL ) {
521 SDL_FreeBlitMap(map);
525 SDL_memset(map->sw_data, 0, sizeof(*map->sw_data));
527 /* It's ready to go */
530 void SDL_InvalidateMap(SDL_BlitMap *map)
536 map->format_version = (unsigned int)-1;
538 SDL_free(map->table);
542 int SDL_MapSurface (SDL_Surface *src, SDL_Surface *dst)
544 SDL_PixelFormat *srcfmt;
545 SDL_PixelFormat *dstfmt;
548 /* Clear out any previous mapping */
550 if ( (src->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
551 SDL_UnRLESurface(src, 1);
553 SDL_InvalidateMap(map);
555 /* Figure out what kind of mapping we're doing */
557 srcfmt = src->format;
558 dstfmt = dst->format;
559 switch (srcfmt->BytesPerPixel) {
561 switch (dstfmt->BytesPerPixel) {
563 /* Palette --> Palette */
564 /* If both SDL_HWSURFACE, assume have same palette */
565 if ( ((src->flags & SDL_HWSURFACE) == SDL_HWSURFACE) &&
566 ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) ) {
569 map->table = Map1to1(srcfmt->palette,
570 dstfmt->palette, &map->identity);
572 if ( ! map->identity ) {
573 if ( map->table == NULL ) {
577 if (srcfmt->BitsPerPixel!=dstfmt->BitsPerPixel)
582 /* Palette --> BitField */
583 map->table = Map1toN(srcfmt, dstfmt);
584 if ( map->table == NULL ) {
591 switch (dstfmt->BytesPerPixel) {
593 /* BitField --> Palette */
594 map->table = MapNto1(srcfmt, dstfmt, &map->identity);
595 if ( ! map->identity ) {
596 if ( map->table == NULL ) {
600 map->identity = 0; /* Don't optimize to copy */
603 /* BitField --> BitField */
604 if ( FORMAT_EQUAL(srcfmt, dstfmt) )
612 map->format_version = dst->format_version;
614 /* Choose your blitters wisely */
615 return(SDL_CalculateBlit(src));
617 void SDL_FreeBlitMap(SDL_BlitMap *map)
620 SDL_InvalidateMap(map);
621 if ( map->sw_data != NULL ) {
622 SDL_free(map->sw_data);