SDL  2.0
SDL_pixels.c File Reference
#include "../SDL_internal.h"
#include "SDL_endian.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_pixels_c.h"
#include "SDL_RLEaccel_c.h"
+ Include dependency graph for SDL_pixels.c:

Go to the source code of this file.

Data Structures

struct  SDL_ListNode
 

Macros

#define CASE(X)   case X: return #X;
 

Functions

const char * SDL_GetPixelFormatName (Uint32 format)
 Get the human readable name of a pixel format. More...
 
SDL_bool SDL_PixelFormatEnumToMasks (Uint32 format, int *bpp, Uint32 *Rmask, Uint32 *Gmask, Uint32 *Bmask, Uint32 *Amask)
 Convert one of the enumerated pixel formats to a bpp and RGBA masks. More...
 
Uint32 SDL_MasksToPixelFormatEnum (int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
 Convert a bpp and RGBA masks to an enumerated pixel format. More...
 
SDL_PixelFormatSDL_AllocFormat (Uint32 pixel_format)
 Create an SDL_PixelFormat structure from a pixel format enum. More...
 
int SDL_InitFormat (SDL_PixelFormat *format, Uint32 pixel_format)
 
void SDL_FreeFormat (SDL_PixelFormat *format)
 Free an SDL_PixelFormat structure. More...
 
SDL_PaletteSDL_AllocPalette (int ncolors)
 Create a palette structure with the specified number of color entries. More...
 
int SDL_SetPixelFormatPalette (SDL_PixelFormat *format, SDL_Palette *palette)
 Set the palette for a pixel format structure. More...
 
int SDL_SetPaletteColors (SDL_Palette *palette, const SDL_Color *colors, int firstcolor, int ncolors)
 Set a range of colors in a palette. More...
 
void SDL_FreePalette (SDL_Palette *palette)
 Free a palette created with SDL_AllocPalette(). More...
 
void SDL_DitherColors (SDL_Color *colors, int bpp)
 
Uint8 SDL_FindColor (SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
 
void SDL_DetectPalette (SDL_Palette *pal, SDL_bool *is_opaque, SDL_bool *has_alpha_channel)
 
Uint32 SDL_MapRGB (const SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b)
 Maps an RGB triple to an opaque pixel value for a given pixel format. More...
 
Uint32 SDL_MapRGBA (const SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
 Maps an RGBA quadruple to a pixel value for a given pixel format. More...
 
void SDL_GetRGB (Uint32 pixel, const SDL_PixelFormat *format, Uint8 *r, Uint8 *g, Uint8 *b)
 Get the RGB components from a pixel of the specified format. More...
 
void SDL_GetRGBA (Uint32 pixel, const SDL_PixelFormat *format, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
 Get the RGBA components from a pixel of the specified format. More...
 
static Uint8Map1to1 (SDL_Palette *src, SDL_Palette *dst, int *identical)
 
static Uint8Map1toN (SDL_PixelFormat *src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod, SDL_PixelFormat *dst)
 
static Uint8MapNto1 (SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
 
SDL_BlitMapSDL_AllocBlitMap (void)
 
void SDL_InvalidateAllBlitMap (SDL_Surface *surface)
 
static void SDL_ListAdd (SDL_ListNode **head, void *ent)
 
static void SDL_ListRemove (SDL_ListNode **head, void *ent)
 
void SDL_InvalidateMap (SDL_BlitMap *map)
 
int SDL_MapSurface (SDL_Surface *src, SDL_Surface *dst)
 
void SDL_FreeBlitMap (SDL_BlitMap *map)
 
void SDL_CalculateGammaRamp (float gamma, Uint16 *ramp)
 Calculate a 256 entry gamma ramp for a gamma value. More...
 

Variables

static Uint8 lookup_0 []
 
static Uint8 lookup_1 []
 
static Uint8 lookup_2 []
 
static Uint8 lookup_3 []
 
static Uint8 lookup_4 []
 
static Uint8 lookup_5 []
 
static Uint8 lookup_6 []
 
static Uint8 lookup_7 []
 
static Uint8 lookup_8 []
 
Uint8SDL_expand_byte [9]
 
static SDL_PixelFormatformats
 
static SDL_SpinLock formats_lock = 0
 

Macro Definition Documentation

◆ CASE

#define CASE (   X)    case X: return #X;

Function Documentation

◆ Map1to1()

static Uint8* Map1to1 ( SDL_Palette src,
SDL_Palette dst,
int identical 
)
static

Definition at line 931 of file SDL_pixels.c.

932 {
933  Uint8 *map;
934  int i;
935 
936  if (identical) {
937  if (src->ncolors <= dst->ncolors) {
938  /* If an identical palette, no need to map */
939  if (src == dst
940  ||
941  (SDL_memcmp
942  (src->colors, dst->colors,
943  src->ncolors * sizeof(SDL_Color)) == 0)) {
944  *identical = 1;
945  return (NULL);
946  }
947  }
948  *identical = 0;
949  }
950  map = (Uint8 *) SDL_malloc(src->ncolors);
951  if (map == NULL) {
952  SDL_OutOfMemory();
953  return (NULL);
954  }
955  for (i = 0; i < src->ncolors; ++i) {
956  map[i] = SDL_FindColor(dst,
957  src->colors[i].r, src->colors[i].g,
958  src->colors[i].b, src->colors[i].a);
959  }
960  return (map);
961 }
#define SDL_malloc
#define SDL_memcmp
#define SDL_OutOfMemory()
Definition: SDL_error.h:88
const GLubyte GLuint GLuint GLuint GLuint alpha GLboolean GLboolean GLboolean GLboolean alpha GLint GLint GLsizei GLsizei GLenum type GLenum GLint GLenum GLint GLint GLsizei GLsizei GLint border GLenum GLint GLint GLint GLint GLint GLsizei GLsizei height GLsizei GLsizei GLenum GLenum const GLvoid *pixels GLenum GLint GLint GLint GLint j2 GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLfloat *params GLenum GLint GLenum GLenum GLvoid *pixels GLenum GLint GLenum GLint *params GLenum GLenum GLint *params GLenum GLsizei const GLvoid *pointer GLenum GLenum const GLint *params GLenum GLfloat GLfloat GLint GLint const GLfloat *points GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat *points GLint GLfloat GLfloat GLint GLfloat GLfloat v2 GLenum GLenum const GLint *params GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum map
Definition: SDL_glfuncs.h:291
GLenum src
GLenum GLenum dst
Uint8 SDL_FindColor(SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: SDL_pixels.c:777
uint8_t Uint8
Definition: SDL_stdinc.h:185
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:163

References i, map, NULL, SDL_FindColor(), SDL_malloc, SDL_memcmp, and SDL_OutOfMemory.

Referenced by MapNto1(), and SDL_MapSurface().

◆ Map1toN()

static Uint8* Map1toN ( SDL_PixelFormat src,
Uint8  Rmod,
Uint8  Gmod,
Uint8  Bmod,
Uint8  Amod,
SDL_PixelFormat dst 
)
static

Definition at line 965 of file SDL_pixels.c.

967 {
968  Uint8 *map;
969  int i;
970  int bpp;
971  SDL_Palette *pal = src->palette;
972 
973  bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
974  map = (Uint8 *) SDL_malloc(pal->ncolors * bpp);
975  if (map == NULL) {
976  SDL_OutOfMemory();
977  return (NULL);
978  }
979 
980  /* We memory copy to the pixel map so the endianness is preserved */
981  for (i = 0; i < pal->ncolors; ++i) {
982  Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
983  Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
984  Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
985  Uint8 A = (Uint8) ((pal->colors[i].a * Amod) / 255);
986  ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
987  }
988  return (map);
989 }
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)
Definition: SDL_blit.h:403
#define G(x, y, z)
Definition: SDL_test_md5.c:74
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp
Uint8 r
Definition: SDL_pixels.h:306
Uint8 b
Definition: SDL_pixels.h:308
Uint8 a
Definition: SDL_pixels.h:309
Uint8 g
Definition: SDL_pixels.h:307
SDL_Color * colors
Definition: SDL_pixels.h:316

References SDL_Color::a, ASSEMBLE_RGBA, SDL_Color::b, bpp, SDL_Palette::colors, SDL_Color::g, G, i, map, SDL_Palette::ncolors, NULL, SDL_Color::r, SDL_malloc, and SDL_OutOfMemory.

Referenced by SDL_MapSurface().

◆ MapNto1()

static Uint8* MapNto1 ( SDL_PixelFormat src,
SDL_PixelFormat dst,
int identical 
)
static

Definition at line 993 of file SDL_pixels.c.

994 {
995  /* Generate a 256 color dither palette */
996  SDL_Palette dithered;
997  SDL_Color colors[256];
998  SDL_Palette *pal = dst->palette;
999 
1000  dithered.ncolors = 256;
1002  dithered.colors = colors;
1003  return (Map1to1(&dithered, pal, identical));
1004 }
void SDL_DitherColors(SDL_Color *colors, int bpp)
Definition: SDL_pixels.c:749
static Uint8 * Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
Definition: SDL_pixels.c:931
static int colors[7]
Definition: testgesture.c:41

References SDL_Palette::colors, colors, Map1to1(), SDL_Palette::ncolors, and SDL_DitherColors().

Referenced by SDL_MapSurface().

◆ SDL_AllocBlitMap()

SDL_BlitMap* SDL_AllocBlitMap ( void  )

Definition at line 1007 of file SDL_pixels.c.

1008 {
1009  SDL_BlitMap *map;
1010 
1011  /* Allocate the empty map */
1012  map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
1013  if (map == NULL) {
1014  SDL_OutOfMemory();
1015  return (NULL);
1016  }
1017  map->info.r = 0xFF;
1018  map->info.g = 0xFF;
1019  map->info.b = 0xFF;
1020  map->info.a = 0xFF;
1021 
1022  /* It's ready to go */
1023  return (map);
1024 }
#define SDL_calloc

References map, NULL, SDL_calloc, and SDL_OutOfMemory.

Referenced by SDL_CreateRGBSurfaceWithFormat().

◆ SDL_AllocFormat()

SDL_PixelFormat* SDL_AllocFormat ( Uint32  pixel_format)

Create an SDL_PixelFormat structure from a pixel format enum.

Definition at line 503 of file SDL_pixels.c.

504 {
506 
508 
509  /* Look it up in our list of previously allocated formats */
510  for (format = formats; format; format = format->next) {
511  if (pixel_format == format->format) {
512  ++format->refcount;
514  return format;
515  }
516  }
517 
518  /* Allocate an empty pixel format structure, and initialize it */
519  format = SDL_malloc(sizeof(*format));
520  if (format == NULL) {
522  SDL_OutOfMemory();
523  return NULL;
524  }
525  if (SDL_InitFormat(format, pixel_format) < 0) {
527  SDL_free(format);
528  SDL_InvalidParamError("format");
529  return NULL;
530  }
531 
533  /* Cache the RGB formats */
534  format->next = formats;
535  formats = format;
536  }
537 
539 
540  return format;
541 }
#define SDL_AtomicLock
#define SDL_AtomicUnlock
#define SDL_free
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:90
int uint32_t uint32_t uint32_t pixel_format
Definition: SDL_kmsdrmsym.h:52
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
static SDL_PixelFormat * formats
Definition: SDL_pixels.c:499
static SDL_SpinLock formats_lock
Definition: SDL_pixels.c:500
int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format)
Definition: SDL_pixels.c:544
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
EGLint EGLint * formats
Definition: eglext.h:718

References formats, formats_lock, NULL, pixel_format, SDL_AtomicLock, SDL_AtomicUnlock, SDL_free, SDL_InitFormat(), SDL_InvalidParamError, SDL_ISPIXELFORMAT_INDEXED, SDL_malloc, and SDL_OutOfMemory.

◆ SDL_AllocPalette()

SDL_Palette* SDL_AllocPalette ( int  ncolors)

Create a palette structure with the specified number of color entries.

Returns
A new palette, or NULL if there wasn't enough memory.
Note
The palette entries are initialized to white.
See also
SDL_FreePalette()

Definition at line 646 of file SDL_pixels.c.

647 {
648  SDL_Palette *palette;
649 
650  /* Input validation */
651  if (ncolors < 1) {
652  SDL_InvalidParamError("ncolors");
653  return NULL;
654  }
655 
656  palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
657  if (!palette) {
658  SDL_OutOfMemory();
659  return NULL;
660  }
661  palette->colors =
662  (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
663  if (!palette->colors) {
664  SDL_free(palette);
665  return NULL;
666  }
667  palette->ncolors = ncolors;
668  palette->version = 1;
669  palette->refcount = 1;
670 
671  SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
672 
673  return palette;
674 }
#define SDL_memset
Uint32 version
Definition: SDL_pixels.h:317

References SDL_Palette::colors, SDL_Palette::ncolors, NULL, SDL_Palette::refcount, SDL_free, SDL_InvalidParamError, SDL_malloc, SDL_memset, SDL_OutOfMemory, and SDL_Palette::version.

◆ SDL_CalculateGammaRamp()

void SDL_CalculateGammaRamp ( float  gamma,
Uint16 ramp 
)

Calculate a 256 entry gamma ramp for a gamma value.

Definition at line 1191 of file SDL_pixels.c.

1192 {
1193  int i;
1194 
1195  /* Input validation */
1196  if (gamma < 0.0f ) {
1197  SDL_InvalidParamError("gamma");
1198  return;
1199  }
1200  if (ramp == NULL) {
1201  SDL_InvalidParamError("ramp");
1202  return;
1203  }
1204 
1205  /* 0.0 gamma is all black */
1206  if (gamma == 0.0f) {
1207  SDL_memset(ramp, 0, 256 * sizeof(Uint16));
1208  return;
1209  } else if (gamma == 1.0f) {
1210  /* 1.0 gamma is identity */
1211  for (i = 0; i < 256; ++i) {
1212  ramp[i] = (i << 8) | i;
1213  }
1214  return;
1215  } else {
1216  /* Calculate a real gamma ramp */
1217  int value;
1218  gamma = 1.0f / gamma;
1219  for (i = 0; i < 256; ++i) {
1220  value =
1221  (int) (SDL_pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
1222  if (value > 65535) {
1223  value = 65535;
1224  }
1225  ramp[i] = (Uint16) value;
1226  }
1227  }
1228 }
#define SDL_pow
GLsizei const GLfloat * value
uint16_t Uint16
Definition: SDL_stdinc.h:197
typedef int(__stdcall *FARPROC)()

References i, int(), NULL, SDL_InvalidParamError, SDL_memset, and SDL_pow.

◆ SDL_DetectPalette()

void SDL_DetectPalette ( SDL_Palette pal,
SDL_bool is_opaque,
SDL_bool has_alpha_channel 
)

Definition at line 806 of file SDL_pixels.c.

807 {
808  int i;
809 
810  {
811  SDL_bool all_opaque = SDL_TRUE;
812  for (i = 0; i < pal->ncolors; i++) {
813  Uint8 alpha_value = pal->colors[i].a;
814  if (alpha_value != SDL_ALPHA_OPAQUE) {
815  all_opaque = SDL_FALSE;
816  break;
817  }
818  }
819 
820  if (all_opaque) {
821  /* Palette is opaque, with an alpha channel */
822  *is_opaque = SDL_TRUE;
823  *has_alpha_channel = SDL_TRUE;
824  return;
825  }
826  }
827 
828  {
829  SDL_bool all_transparent = SDL_TRUE;
830  for (i = 0; i < pal->ncolors; i++) {
831  Uint8 alpha_value = pal->colors[i].a;
832  if (alpha_value != SDL_ALPHA_TRANSPARENT) {
833  all_transparent = SDL_FALSE;
834  break;
835  }
836  }
837 
838  if (all_transparent) {
839  /* Palette is opaque, without an alpha channel */
840  *is_opaque = SDL_TRUE;
841  *has_alpha_channel = SDL_FALSE;
842  return;
843  }
844  }
845 
846  /* Palette has alpha values */
847  *is_opaque = SDL_FALSE;
848  *has_alpha_channel = SDL_TRUE;
849 }
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
#define SDL_ALPHA_TRANSPARENT
Definition: SDL_pixels.h:47
SDL_bool
Definition: SDL_stdinc.h:168
@ SDL_TRUE
Definition: SDL_stdinc.h:170
@ SDL_FALSE
Definition: SDL_stdinc.h:169

References SDL_Color::a, SDL_Palette::colors, i, SDL_Palette::ncolors, SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_ConvertSurface(), and SDL_CreateTextureFromSurface().

◆ SDL_DitherColors()

void SDL_DitherColors ( SDL_Color colors,
int  bpp 
)

Definition at line 749 of file SDL_pixels.c.

750 {
751  int i;
752  if (bpp != 8)
753  return; /* only 8bpp supported right now */
754 
755  for (i = 0; i < 256; i++) {
756  int r, g, b;
757  /* map each bit field to the full [0, 255] interval,
758  so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
759  r = i & 0xe0;
760  r |= r >> 3 | r >> 6;
761  colors[i].r = r;
762  g = (i << 3) & 0xe0;
763  g |= g >> 3 | g >> 6;
764  colors[i].g = g;
765  b = i & 0x3;
766  b |= b << 2;
767  b |= b << 4;
768  colors[i].b = b;
770  }
771 }
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
GLboolean GLboolean GLboolean b
GLboolean GLboolean g

References bpp, colors, i, and SDL_ALPHA_OPAQUE.

Referenced by MapNto1().

◆ SDL_FindColor()

Uint8 SDL_FindColor ( SDL_Palette pal,
Uint8  r,
Uint8  g,
Uint8  b,
Uint8  a 
)

Definition at line 777 of file SDL_pixels.c.

778 {
779  /* Do colorspace distance matching */
780  unsigned int smallest;
781  unsigned int distance;
782  int rd, gd, bd, ad;
783  int i;
784  Uint8 pixel = 0;
785 
786  smallest = ~0;
787  for (i = 0; i < pal->ncolors; ++i) {
788  rd = pal->colors[i].r - r;
789  gd = pal->colors[i].g - g;
790  bd = pal->colors[i].b - b;
791  ad = pal->colors[i].a - a;
792  distance = (rd * rd) + (gd * gd) + (bd * bd) + (ad * ad);
793  if (distance < smallest) {
794  pixel = i;
795  if (distance == 0) { /* Perfect match! */
796  break;
797  }
798  smallest = distance;
799  }
800  }
801  return (pixel);
802 }
GLboolean GLboolean GLboolean GLboolean a
GLsizei GLsizei GLfloat distance

References SDL_Color::a, SDL_Color::b, SDL_Palette::colors, SDL_Color::g, i, SDL_Palette::ncolors, and SDL_Color::r.

Referenced by Map1to1(), SDL_MapRGB(), and SDL_MapRGBA().

◆ SDL_FreeBlitMap()

void SDL_FreeBlitMap ( SDL_BlitMap map)

Definition at line 1182 of file SDL_pixels.c.

1183 {
1184  if (map) {
1186  SDL_free(map);
1187  }
1188 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1083

References map, SDL_free, and SDL_InvalidateMap().

Referenced by SDL_FreeSurface().

◆ SDL_FreeFormat()

void SDL_FreeFormat ( SDL_PixelFormat format)

Free an SDL_PixelFormat structure.

Definition at line 609 of file SDL_pixels.c.

610 {
611  SDL_PixelFormat *prev;
612 
613  if (!format) {
614  SDL_InvalidParamError("format");
615  return;
616  }
617 
619 
620  if (--format->refcount > 0) {
622  return;
623  }
624 
625  /* Remove this format from our list */
626  if (format == formats) {
627  formats = format->next;
628  } else if (formats) {
629  for (prev = formats; prev->next; prev = prev->next) {
630  if (prev->next == format) {
631  prev->next = format->next;
632  break;
633  }
634  }
635  }
636 
638 
639  if (format->palette) {
640  SDL_FreePalette(format->palette);
641  }
642  SDL_free(format);
643 }
void SDL_FreePalette(SDL_Palette *palette)
Free a palette created with SDL_AllocPalette().
Definition: SDL_pixels.c:732
struct SDL_PixelFormat * next
Definition: SDL_pixels.h:344

References formats_lock, SDL_PixelFormat::next, SDL_AtomicLock, SDL_AtomicUnlock, SDL_free, SDL_FreePalette(), and SDL_InvalidParamError.

◆ SDL_FreePalette()

void SDL_FreePalette ( SDL_Palette palette)

Free a palette created with SDL_AllocPalette().

See also
SDL_AllocPalette()

Definition at line 732 of file SDL_pixels.c.

733 {
734  if (!palette) {
735  SDL_InvalidParamError("palette");
736  return;
737  }
738  if (--palette->refcount > 0) {
739  return;
740  }
741  SDL_free(palette->colors);
742  SDL_free(palette);
743 }

References SDL_Palette::colors, SDL_Palette::refcount, SDL_free, and SDL_InvalidParamError.

Referenced by SDL_FreeFormat(), and SDL_SetPixelFormatPalette().

◆ SDL_GetPixelFormatName()

const char* SDL_GetPixelFormatName ( Uint32  format)

Get the human readable name of a pixel format.

Definition at line 86 of file SDL_pixels.c.

87 {
88  switch (format) {
89 #define CASE(X) case X: return #X;
128 #undef CASE
129  default:
130  return "SDL_PIXELFORMAT_UNKNOWN";
131  }
132 }
#define CASE(X)
@ SDL_PIXELFORMAT_BGR565
Definition: SDL_pixels.h:234
@ SDL_PIXELFORMAT_YVYU
Definition: SDL_pixels.h:294
@ SDL_PIXELFORMAT_RGB332
Definition: SDL_pixels.h:188
@ SDL_PIXELFORMAT_INDEX1LSB
Definition: SDL_pixels.h:174
@ SDL_PIXELFORMAT_BGR444
Definition: SDL_pixels.h:198
@ SDL_PIXELFORMAT_ABGR4444
Definition: SDL_pixels.h:213
@ SDL_PIXELFORMAT_BGRA4444
Definition: SDL_pixels.h:216
@ SDL_PIXELFORMAT_BGR24
Definition: SDL_pixels.h:240
@ SDL_PIXELFORMAT_INDEX4MSB
Definition: SDL_pixels.h:183
@ SDL_PIXELFORMAT_RGB555
Definition: SDL_pixels.h:202
@ SDL_PIXELFORMAT_RGB444
Definition: SDL_pixels.h:194
@ SDL_PIXELFORMAT_RGBA8888
Definition: SDL_pixels.h:260
@ SDL_PIXELFORMAT_RGBA5551
Definition: SDL_pixels.h:222
@ SDL_PIXELFORMAT_ARGB1555
Definition: SDL_pixels.h:219
@ SDL_PIXELFORMAT_INDEX8
Definition: SDL_pixels.h:186
@ SDL_PIXELFORMAT_UYVY
Definition: SDL_pixels.h:292
@ SDL_PIXELFORMAT_BGR888
Definition: SDL_pixels.h:253
@ SDL_PIXELFORMAT_YV12
Definition: SDL_pixels.h:286
@ SDL_PIXELFORMAT_BGRX8888
Definition: SDL_pixels.h:254
@ SDL_PIXELFORMAT_RGB24
Definition: SDL_pixels.h:237
@ SDL_PIXELFORMAT_RGB888
Definition: SDL_pixels.h:246
@ SDL_PIXELFORMAT_ABGR8888
Definition: SDL_pixels.h:263
@ SDL_PIXELFORMAT_YUY2
Definition: SDL_pixels.h:290
@ SDL_PIXELFORMAT_NV12
Definition: SDL_pixels.h:296
@ SDL_PIXELFORMAT_BGRA8888
Definition: SDL_pixels.h:266
@ SDL_PIXELFORMAT_ABGR1555
Definition: SDL_pixels.h:225
@ SDL_PIXELFORMAT_NV21
Definition: SDL_pixels.h:298
@ SDL_PIXELFORMAT_IYUV
Definition: SDL_pixels.h:288
@ SDL_PIXELFORMAT_ARGB8888
Definition: SDL_pixels.h:257
@ SDL_PIXELFORMAT_ARGB4444
Definition: SDL_pixels.h:207
@ SDL_PIXELFORMAT_INDEX1MSB
Definition: SDL_pixels.h:177
@ SDL_PIXELFORMAT_INDEX4LSB
Definition: SDL_pixels.h:180
@ SDL_PIXELFORMAT_RGBX8888
Definition: SDL_pixels.h:247
@ SDL_PIXELFORMAT_BGRA5551
Definition: SDL_pixels.h:228
@ SDL_PIXELFORMAT_RGB565
Definition: SDL_pixels.h:231
@ SDL_PIXELFORMAT_ARGB2101010
Definition: SDL_pixels.h:269
@ SDL_PIXELFORMAT_BGR555
Definition: SDL_pixels.h:206
@ SDL_PIXELFORMAT_RGBA4444
Definition: SDL_pixels.h:210

References CASE, SDL_PIXELFORMAT_ABGR1555, SDL_PIXELFORMAT_ABGR4444, SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_ARGB1555, SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ARGB4444, SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_BGR24, SDL_PIXELFORMAT_BGR444, SDL_PIXELFORMAT_BGR555, SDL_PIXELFORMAT_BGR565, SDL_PIXELFORMAT_BGR888, SDL_PIXELFORMAT_BGRA4444, SDL_PIXELFORMAT_BGRA5551, SDL_PIXELFORMAT_BGRA8888, SDL_PIXELFORMAT_BGRX8888, SDL_PIXELFORMAT_INDEX1LSB, SDL_PIXELFORMAT_INDEX1MSB, SDL_PIXELFORMAT_INDEX4LSB, SDL_PIXELFORMAT_INDEX4MSB, SDL_PIXELFORMAT_INDEX8, SDL_PIXELFORMAT_IYUV, SDL_PIXELFORMAT_NV12, SDL_PIXELFORMAT_NV21, SDL_PIXELFORMAT_RGB24, SDL_PIXELFORMAT_RGB332, SDL_PIXELFORMAT_RGB444, SDL_PIXELFORMAT_RGB555, SDL_PIXELFORMAT_RGB565, SDL_PIXELFORMAT_RGB888, SDL_PIXELFORMAT_RGBA4444, SDL_PIXELFORMAT_RGBA5551, SDL_PIXELFORMAT_RGBA8888, SDL_PIXELFORMAT_RGBX8888, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_YUY2, SDL_PIXELFORMAT_YV12, and SDL_PIXELFORMAT_YVYU.

◆ SDL_GetRGB()

void SDL_GetRGB ( Uint32  pixel,
const SDL_PixelFormat format,
Uint8 r,
Uint8 g,
Uint8 b 
)

Get the RGB components from a pixel of the specified format.

See also
SDL_GetRGBA

Definition at line 881 of file SDL_pixels.c.

883 {
884  if (format->palette == NULL) {
885  unsigned v;
886  v = (pixel & format->Rmask) >> format->Rshift;
887  *r = SDL_expand_byte[format->Rloss][v];
888  v = (pixel & format->Gmask) >> format->Gshift;
889  *g = SDL_expand_byte[format->Gloss][v];
890  v = (pixel & format->Bmask) >> format->Bshift;
891  *b = SDL_expand_byte[format->Bloss][v];
892  } else {
893  if (pixel < (unsigned)format->palette->ncolors) {
894  *r = format->palette->colors[pixel].r;
895  *g = format->palette->colors[pixel].g;
896  *b = format->palette->colors[pixel].b;
897  } else {
898  *r = *g = *b = 0;
899  }
900  }
901 }
const GLdouble * v
Definition: SDL_opengl.h:2064
Uint8 * SDL_expand_byte[9]
Definition: SDL_pixels.c:71

References NULL, and SDL_expand_byte.

◆ SDL_GetRGBA()

void SDL_GetRGBA ( Uint32  pixel,
const SDL_PixelFormat format,
Uint8 r,
Uint8 g,
Uint8 b,
Uint8 a 
)

Get the RGBA components from a pixel of the specified format.

See also
SDL_GetRGB

Definition at line 904 of file SDL_pixels.c.

906 {
907  if (format->palette == NULL) {
908  unsigned v;
909  v = (pixel & format->Rmask) >> format->Rshift;
910  *r = SDL_expand_byte[format->Rloss][v];
911  v = (pixel & format->Gmask) >> format->Gshift;
912  *g = SDL_expand_byte[format->Gloss][v];
913  v = (pixel & format->Bmask) >> format->Bshift;
914  *b = SDL_expand_byte[format->Bloss][v];
915  v = (pixel & format->Amask) >> format->Ashift;
916  *a = SDL_expand_byte[format->Aloss][v];
917  } else {
918  if (pixel < (unsigned)format->palette->ncolors) {
919  *r = format->palette->colors[pixel].r;
920  *g = format->palette->colors[pixel].g;
921  *b = format->palette->colors[pixel].b;
922  *a = format->palette->colors[pixel].a;
923  } else {
924  *r = *g = *b = *a = 0;
925  }
926  }
927 }

References NULL, and SDL_expand_byte.

◆ SDL_InitFormat()

int SDL_InitFormat ( SDL_PixelFormat format,
Uint32  pixel_format 
)

Definition at line 544 of file SDL_pixels.c.

545 {
546  int bpp;
547  Uint32 Rmask, Gmask, Bmask, Amask;
548  Uint32 mask;
549 
551  &Rmask, &Gmask, &Bmask, &Amask)) {
552  return -1;
553  }
554 
555  /* Set up the format */
556  SDL_zerop(format);
557  format->format = pixel_format;
558  format->BitsPerPixel = bpp;
559  format->BytesPerPixel = (bpp + 7) / 8;
560 
561  format->Rmask = Rmask;
562  format->Rshift = 0;
563  format->Rloss = 8;
564  if (Rmask) {
565  for (mask = Rmask; !(mask & 0x01); mask >>= 1)
566  ++format->Rshift;
567  for (; (mask & 0x01); mask >>= 1)
568  --format->Rloss;
569  }
570 
571  format->Gmask = Gmask;
572  format->Gshift = 0;
573  format->Gloss = 8;
574  if (Gmask) {
575  for (mask = Gmask; !(mask & 0x01); mask >>= 1)
576  ++format->Gshift;
577  for (; (mask & 0x01); mask >>= 1)
578  --format->Gloss;
579  }
580 
581  format->Bmask = Bmask;
582  format->Bshift = 0;
583  format->Bloss = 8;
584  if (Bmask) {
585  for (mask = Bmask; !(mask & 0x01); mask >>= 1)
586  ++format->Bshift;
587  for (; (mask & 0x01); mask >>= 1)
588  --format->Bloss;
589  }
590 
591  format->Amask = Amask;
592  format->Ashift = 0;
593  format->Aloss = 8;
594  if (Amask) {
595  for (mask = Amask; !(mask & 0x01); mask >>= 1)
596  ++format->Ashift;
597  for (; (mask & 0x01); mask >>= 1)
598  --format->Aloss;
599  }
600 
601  format->palette = NULL;
602  format->refcount = 1;
603  format->next = NULL;
604 
605  return 0;
606 }
GLenum GLint GLuint mask
SDL_bool SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 *Rmask, Uint32 *Gmask, Uint32 *Bmask, Uint32 *Amask)
Convert one of the enumerated pixel formats to a bpp and RGBA masks.
Definition: SDL_pixels.c:135
#define SDL_zerop(x)
Definition: SDL_stdinc.h:427
uint32_t Uint32
Definition: SDL_stdinc.h:209

References bpp, NULL, pixel_format, SDL_PixelFormatEnumToMasks(), and SDL_zerop.

Referenced by SDL_AllocFormat(), SDL_CreateSurfaceOnStack(), and SDL_SaveBMP_RW().

◆ SDL_InvalidateAllBlitMap()

void SDL_InvalidateAllBlitMap ( SDL_Surface surface)

Definition at line 1034 of file SDL_pixels.c.

1035 {
1036  SDL_ListNode *l = surface->list_blitmap;
1037 
1038  surface->list_blitmap = NULL;
1039 
1040  while (l) {
1041  SDL_ListNode *tmp = l;
1042  SDL_InvalidateMap((SDL_BlitMap *)l->entry);
1043  l = l->next;
1044  SDL_free(tmp);
1045  }
1046 }
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int int return Display Window Cursor return Display Window return Display Drawable GC int int unsigned int unsigned int return Display Drawable GC int int _Xconst char int return Display Drawable GC int int unsigned int unsigned int return Display return Display Cursor return Display GC return XModifierKeymap return char Display Window int return Display return Display int int int return Display long XVisualInfo int return Display Window Atom long long Bool Atom Atom int unsigned long unsigned long unsigned char * l)
Definition: SDL_x11sym.h:80
EGLSurface surface
Definition: eglext.h:248

References l, NULL, SDL_free, and SDL_InvalidateMap().

Referenced by SDL_FreeSurface().

◆ SDL_InvalidateMap()

void SDL_InvalidateMap ( SDL_BlitMap map)

Definition at line 1083 of file SDL_pixels.c.

1084 {
1085  if (!map) {
1086  return;
1087  }
1088  if (map->dst) {
1089  /* Un-register from the destination surface */
1090  SDL_ListRemove((SDL_ListNode **)&(map->dst->list_blitmap), map);
1091  }
1092  map->dst = NULL;
1093  map->src_palette_version = 0;
1094  map->dst_palette_version = 0;
1095  SDL_free(map->info.table);
1096  map->info.table = NULL;
1097 }
static void SDL_ListRemove(SDL_ListNode **head, void *ent)
Definition: SDL_pixels.c:1067

References map, NULL, SDL_free, and SDL_ListRemove().

Referenced by SDL_CalculateBlit(), SDL_ConvertPixels(), SDL_ConvertSurface(), SDL_FreeBlitMap(), SDL_FreeSurface(), SDL_InvalidateAllBlitMap(), SDL_LowerBlitScaled(), SDL_MapSurface(), SDL_SetColorKey(), SDL_SetSurfaceAlphaMod(), SDL_SetSurfaceBlendMode(), SDL_SetSurfaceColorMod(), SDL_SetSurfacePalette(), SDL_SetSurfaceRLE(), and SDL_UpperBlit().

◆ SDL_ListAdd()

void SDL_ListAdd ( SDL_ListNode **  head,
void ent 
)
static

Definition at line 1052 of file SDL_pixels.c.

1053 {
1054  SDL_ListNode *node = SDL_malloc(sizeof (*node));
1055 
1056  if (node == NULL) {
1057  SDL_OutOfMemory();
1058  return;
1059  }
1060 
1061  node->entry = ent;
1062  node->next = *head;
1063  *head = node;
1064 }
SDL_EventEntry * head
Definition: SDL_events.c:87
void * entry
Definition: SDL_pixels.c:1029
struct SDL_ListNode * next
Definition: SDL_pixels.c:1030

References SDL_ListNode::entry, head, SDL_ListNode::next, NULL, SDL_malloc, and SDL_OutOfMemory.

Referenced by SDL_MapSurface().

◆ SDL_ListRemove()

void SDL_ListRemove ( SDL_ListNode **  head,
void ent 
)
static

Definition at line 1067 of file SDL_pixels.c.

1068 {
1069  SDL_ListNode **ptr = head;
1070 
1071  while (*ptr) {
1072  if ((*ptr)->entry == ent) {
1073  SDL_ListNode *tmp = *ptr;
1074  *ptr = (*ptr)->next;
1075  SDL_free(tmp);
1076  return;
1077  }
1078  ptr = &(*ptr)->next;
1079  }
1080 }
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF ptr

References head, ptr, and SDL_free.

Referenced by SDL_InvalidateMap().

◆ SDL_MapRGB()

Uint32 SDL_MapRGB ( const SDL_PixelFormat format,
Uint8  r,
Uint8  g,
Uint8  b 
)

Maps an RGB triple to an opaque pixel value for a given pixel format.

See also
SDL_MapRGBA

Definition at line 854 of file SDL_pixels.c.

855 {
856  if (format->palette == NULL) {
857  return (r >> format->Rloss) << format->Rshift
858  | (g >> format->Gloss) << format->Gshift
859  | (b >> format->Bloss) << format->Bshift | format->Amask;
860  } else {
861  return SDL_FindColor(format->palette, r, g, b, SDL_ALPHA_OPAQUE);
862  }
863 }

References NULL, SDL_ALPHA_OPAQUE, and SDL_FindColor().

◆ SDL_MapRGBA()

Uint32 SDL_MapRGBA ( const SDL_PixelFormat format,
Uint8  r,
Uint8  g,
Uint8  b,
Uint8  a 
)

Maps an RGBA quadruple to a pixel value for a given pixel format.

See also
SDL_MapRGB

Definition at line 867 of file SDL_pixels.c.

869 {
870  if (format->palette == NULL) {
871  return (r >> format->Rloss) << format->Rshift
872  | (g >> format->Gloss) << format->Gshift
873  | (b >> format->Bloss) << format->Bshift
874  | ((a >> format->Aloss) << format->Ashift & format->Amask);
875  } else {
876  return SDL_FindColor(format->palette, r, g, b, a);
877  }
878 }

References NULL, and SDL_FindColor().

◆ SDL_MapSurface()

int SDL_MapSurface ( SDL_Surface src,
SDL_Surface dst 
)

Definition at line 1100 of file SDL_pixels.c.

1101 {
1102  SDL_PixelFormat *srcfmt;
1103  SDL_PixelFormat *dstfmt;
1104  SDL_BlitMap *map;
1105 
1106  /* Clear out any previous mapping */
1107  map = src->map;
1108 #if SDL_HAVE_RLE
1109  if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
1110  SDL_UnRLESurface(src, 1);
1111  }
1112 #endif
1114 
1115  /* Figure out what kind of mapping we're doing */
1116  map->identity = 0;
1117  srcfmt = src->format;
1118  dstfmt = dst->format;
1119  if (SDL_ISPIXELFORMAT_INDEXED(srcfmt->format)) {
1120  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1121  /* Palette --> Palette */
1122  map->info.table =
1123  Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
1124  if (!map->identity) {
1125  if (map->info.table == NULL) {
1126  return (-1);
1127  }
1128  }
1129  if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
1130  map->identity = 0;
1131  } else {
1132  /* Palette --> BitField */
1133  map->info.table =
1134  Map1toN(srcfmt, src->map->info.r, src->map->info.g,
1135  src->map->info.b, src->map->info.a, dstfmt);
1136  if (map->info.table == NULL) {
1137  return (-1);
1138  }
1139  }
1140  } else {
1141  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1142  /* BitField --> Palette */
1143  map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
1144  if (!map->identity) {
1145  if (map->info.table == NULL) {
1146  return (-1);
1147  }
1148  }
1149  map->identity = 0; /* Don't optimize to copy */
1150  } else {
1151  /* BitField --> BitField */
1152  if (srcfmt == dstfmt) {
1153  map->identity = 1;
1154  }
1155  }
1156  }
1157 
1158  map->dst = dst;
1159 
1160  if (map->dst) {
1161  /* Register BlitMap to the destination surface, to be invalidated when needed */
1162  SDL_ListAdd((SDL_ListNode **)&(map->dst->list_blitmap), map);
1163  }
1164 
1165  if (dstfmt->palette) {
1166  map->dst_palette_version = dstfmt->palette->version;
1167  } else {
1168  map->dst_palette_version = 0;
1169  }
1170 
1171  if (srcfmt->palette) {
1172  map->src_palette_version = srcfmt->palette->version;
1173  } else {
1174  map->src_palette_version = 0;
1175  }
1176 
1177  /* Choose your blitters wisely */
1178  return (SDL_CalculateBlit(src));
1179 }
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
int SDL_CalculateBlit(SDL_Surface *surface)
Definition: SDL_blit.c:196
static Uint8 * Map1toN(SDL_PixelFormat *src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod, SDL_PixelFormat *dst)
Definition: SDL_pixels.c:965
static void SDL_ListAdd(SDL_ListNode **head, void *ent)
Definition: SDL_pixels.c:1052
static Uint8 * MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
Definition: SDL_pixels.c:993
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint8 BitsPerPixel
Definition: SDL_pixels.h:328
SDL_Palette * palette
Definition: SDL_pixels.h:327

References SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::format, map, Map1to1(), Map1toN(), MapNto1(), NULL, SDL_PixelFormat::palette, SDL_CalculateBlit(), SDL_InvalidateMap(), SDL_ISPIXELFORMAT_INDEXED, SDL_ListAdd(), SDL_RLEACCEL, SDL_UnRLESurface(), and SDL_Palette::version.

Referenced by SDL_LowerBlit().

◆ SDL_MasksToPixelFormatEnum()

Uint32 SDL_MasksToPixelFormatEnum ( int  bpp,
Uint32  Rmask,
Uint32  Gmask,
Uint32  Bmask,
Uint32  Amask 
)

Convert a bpp and RGBA masks to an enumerated pixel format.

Returns
The pixel format, or SDL_PIXELFORMAT_UNKNOWN if the conversion wasn't possible.
See also
SDL_PixelFormatEnumToMasks()

Definition at line 294 of file SDL_pixels.c.

296 {
297  switch (bpp) {
298  case 1:
299  /* SDL defaults to MSB ordering */
301  case 4:
302  /* SDL defaults to MSB ordering */
304  case 8:
305  if (Rmask == 0) {
306  return SDL_PIXELFORMAT_INDEX8;
307  }
308  if (Rmask == 0xE0 &&
309  Gmask == 0x1C &&
310  Bmask == 0x03 &&
311  Amask == 0x00) {
312  return SDL_PIXELFORMAT_RGB332;
313  }
314  break;
315  case 12:
316  if (Rmask == 0) {
317  return SDL_PIXELFORMAT_RGB444;
318  }
319  if (Rmask == 0x0F00 &&
320  Gmask == 0x00F0 &&
321  Bmask == 0x000F &&
322  Amask == 0x0000) {
323  return SDL_PIXELFORMAT_RGB444;
324  }
325  if (Rmask == 0x000F &&
326  Gmask == 0x00F0 &&
327  Bmask == 0x0F00 &&
328  Amask == 0x0000) {
329  return SDL_PIXELFORMAT_BGR444;
330  }
331  break;
332  case 15:
333  if (Rmask == 0) {
334  return SDL_PIXELFORMAT_RGB555;
335  }
336  /* fallthrough */
337  case 16:
338  if (Rmask == 0) {
339  return SDL_PIXELFORMAT_RGB565;
340  }
341  if (Rmask == 0x7C00 &&
342  Gmask == 0x03E0 &&
343  Bmask == 0x001F &&
344  Amask == 0x0000) {
345  return SDL_PIXELFORMAT_RGB555;
346  }
347  if (Rmask == 0x001F &&
348  Gmask == 0x03E0 &&
349  Bmask == 0x7C00 &&
350  Amask == 0x0000) {
351  return SDL_PIXELFORMAT_BGR555;
352  }
353  if (Rmask == 0x0F00 &&
354  Gmask == 0x00F0 &&
355  Bmask == 0x000F &&
356  Amask == 0xF000) {
358  }
359  if (Rmask == 0xF000 &&
360  Gmask == 0x0F00 &&
361  Bmask == 0x00F0 &&
362  Amask == 0x000F) {
364  }
365  if (Rmask == 0x000F &&
366  Gmask == 0x00F0 &&
367  Bmask == 0x0F00 &&
368  Amask == 0xF000) {
370  }
371  if (Rmask == 0x00F0 &&
372  Gmask == 0x0F00 &&
373  Bmask == 0xF000 &&
374  Amask == 0x000F) {
376  }
377  if (Rmask == 0x7C00 &&
378  Gmask == 0x03E0 &&
379  Bmask == 0x001F &&
380  Amask == 0x8000) {
382  }
383  if (Rmask == 0xF800 &&
384  Gmask == 0x07C0 &&
385  Bmask == 0x003E &&
386  Amask == 0x0001) {
388  }
389  if (Rmask == 0x001F &&
390  Gmask == 0x03E0 &&
391  Bmask == 0x7C00 &&
392  Amask == 0x8000) {
394  }
395  if (Rmask == 0x003E &&
396  Gmask == 0x07C0 &&
397  Bmask == 0xF800 &&
398  Amask == 0x0001) {
400  }
401  if (Rmask == 0xF800 &&
402  Gmask == 0x07E0 &&
403  Bmask == 0x001F &&
404  Amask == 0x0000) {
405  return SDL_PIXELFORMAT_RGB565;
406  }
407  if (Rmask == 0x001F &&
408  Gmask == 0x07E0 &&
409  Bmask == 0xF800 &&
410  Amask == 0x0000) {
411  return SDL_PIXELFORMAT_BGR565;
412  }
413  if (Rmask == 0x003F &&
414  Gmask == 0x07C0 &&
415  Bmask == 0xF800 &&
416  Amask == 0x0000) {
417  /* Technically this would be BGR556, but Witek says this works in bug 3158 */
418  return SDL_PIXELFORMAT_RGB565;
419  }
420  break;
421  case 24:
422  switch (Rmask) {
423  case 0:
424  case 0x00FF0000:
425 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
426  return SDL_PIXELFORMAT_RGB24;
427 #else
428  return SDL_PIXELFORMAT_BGR24;
429 #endif
430  case 0x000000FF:
431 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
432  return SDL_PIXELFORMAT_BGR24;
433 #else
434  return SDL_PIXELFORMAT_RGB24;
435 #endif
436  }
437  case 32:
438  if (Rmask == 0) {
439  return SDL_PIXELFORMAT_RGB888;
440  }
441  if (Rmask == 0x00FF0000 &&
442  Gmask == 0x0000FF00 &&
443  Bmask == 0x000000FF &&
444  Amask == 0x00000000) {
445  return SDL_PIXELFORMAT_RGB888;
446  }
447  if (Rmask == 0xFF000000 &&
448  Gmask == 0x00FF0000 &&
449  Bmask == 0x0000FF00 &&
450  Amask == 0x00000000) {
452  }
453  if (Rmask == 0x000000FF &&
454  Gmask == 0x0000FF00 &&
455  Bmask == 0x00FF0000 &&
456  Amask == 0x00000000) {
457  return SDL_PIXELFORMAT_BGR888;
458  }
459  if (Rmask == 0x0000FF00 &&
460  Gmask == 0x00FF0000 &&
461  Bmask == 0xFF000000 &&
462  Amask == 0x00000000) {
464  }
465  if (Rmask == 0x00FF0000 &&
466  Gmask == 0x0000FF00 &&
467  Bmask == 0x000000FF &&
468  Amask == 0xFF000000) {
470  }
471  if (Rmask == 0xFF000000 &&
472  Gmask == 0x00FF0000 &&
473  Bmask == 0x0000FF00 &&
474  Amask == 0x000000FF) {
476  }
477  if (Rmask == 0x000000FF &&
478  Gmask == 0x0000FF00 &&
479  Bmask == 0x00FF0000 &&
480  Amask == 0xFF000000) {
482  }
483  if (Rmask == 0x0000FF00 &&
484  Gmask == 0x00FF0000 &&
485  Bmask == 0xFF000000 &&
486  Amask == 0x000000FF) {
488  }
489  if (Rmask == 0x3FF00000 &&
490  Gmask == 0x000FFC00 &&
491  Bmask == 0x000003FF &&
492  Amask == 0xC0000000) {
494  }
495  }
497 }
@ SDL_PIXELFORMAT_UNKNOWN
Definition: SDL_pixels.h:173

References bpp, SDL_PIXELFORMAT_ABGR1555, SDL_PIXELFORMAT_ABGR4444, SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_ARGB1555, SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ARGB4444, SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_BGR24, SDL_PIXELFORMAT_BGR444, SDL_PIXELFORMAT_BGR555, SDL_PIXELFORMAT_BGR565, SDL_PIXELFORMAT_BGR888, SDL_PIXELFORMAT_BGRA4444, SDL_PIXELFORMAT_BGRA5551, SDL_PIXELFORMAT_BGRA8888, SDL_PIXELFORMAT_BGRX8888, SDL_PIXELFORMAT_INDEX1MSB, SDL_PIXELFORMAT_INDEX4MSB, SDL_PIXELFORMAT_INDEX8, SDL_PIXELFORMAT_RGB24, SDL_PIXELFORMAT_RGB332, SDL_PIXELFORMAT_RGB444, SDL_PIXELFORMAT_RGB555, SDL_PIXELFORMAT_RGB565, SDL_PIXELFORMAT_RGB888, SDL_PIXELFORMAT_RGBA4444, SDL_PIXELFORMAT_RGBA5551, SDL_PIXELFORMAT_RGBA8888, SDL_PIXELFORMAT_RGBX8888, and SDL_PIXELFORMAT_UNKNOWN.

◆ SDL_PixelFormatEnumToMasks()

SDL_bool SDL_PixelFormatEnumToMasks ( Uint32  format,
int bpp,
Uint32 Rmask,
Uint32 Gmask,
Uint32 Bmask,
Uint32 Amask 
)

Convert one of the enumerated pixel formats to a bpp and RGBA masks.

Returns
SDL_TRUE, or SDL_FALSE if the conversion wasn't possible.
See also
SDL_MasksToPixelFormatEnum()

Definition at line 135 of file SDL_pixels.c.

137 {
138  Uint32 masks[4];
139 
140  /* This function doesn't work with FourCC pixel formats */
142  SDL_SetError("FOURCC pixel formats are not supported");
143  return SDL_FALSE;
144  }
145 
146  /* Initialize the values here */
147  if (SDL_BYTESPERPIXEL(format) <= 2) {
149  } else {
150  *bpp = SDL_BYTESPERPIXEL(format) * 8;
151  }
152  *Rmask = *Gmask = *Bmask = *Amask = 0;
153 
154  if (format == SDL_PIXELFORMAT_RGB24) {
155 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
156  *Rmask = 0x00FF0000;
157  *Gmask = 0x0000FF00;
158  *Bmask = 0x000000FF;
159 #else
160  *Rmask = 0x000000FF;
161  *Gmask = 0x0000FF00;
162  *Bmask = 0x00FF0000;
163 #endif
164  return SDL_TRUE;
165  }
166 
167  if (format == SDL_PIXELFORMAT_BGR24) {
168 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
169  *Rmask = 0x000000FF;
170  *Gmask = 0x0000FF00;
171  *Bmask = 0x00FF0000;
172 #else
173  *Rmask = 0x00FF0000;
174  *Gmask = 0x0000FF00;
175  *Bmask = 0x000000FF;
176 #endif
177  return SDL_TRUE;
178  }
179 
183  /* Not a format that uses masks */
184  return SDL_TRUE;
185  }
186 
187  switch (SDL_PIXELLAYOUT(format)) {
189  masks[0] = 0x00000000;
190  masks[1] = 0x000000E0;
191  masks[2] = 0x0000001C;
192  masks[3] = 0x00000003;
193  break;
195  masks[0] = 0x0000F000;
196  masks[1] = 0x00000F00;
197  masks[2] = 0x000000F0;
198  masks[3] = 0x0000000F;
199  break;
201  masks[0] = 0x00008000;
202  masks[1] = 0x00007C00;
203  masks[2] = 0x000003E0;
204  masks[3] = 0x0000001F;
205  break;
207  masks[0] = 0x0000F800;
208  masks[1] = 0x000007C0;
209  masks[2] = 0x0000003E;
210  masks[3] = 0x00000001;
211  break;
213  masks[0] = 0x00000000;
214  masks[1] = 0x0000F800;
215  masks[2] = 0x000007E0;
216  masks[3] = 0x0000001F;
217  break;
219  masks[0] = 0xFF000000;
220  masks[1] = 0x00FF0000;
221  masks[2] = 0x0000FF00;
222  masks[3] = 0x000000FF;
223  break;
225  masks[0] = 0xC0000000;
226  masks[1] = 0x3FF00000;
227  masks[2] = 0x000FFC00;
228  masks[3] = 0x000003FF;
229  break;
231  masks[0] = 0xFFC00000;
232  masks[1] = 0x003FF000;
233  masks[2] = 0x00000FFC;
234  masks[3] = 0x00000003;
235  break;
236  default:
237  SDL_SetError("Unknown pixel format");
238  return SDL_FALSE;
239  }
240 
241  switch (SDL_PIXELORDER(format)) {
243  *Rmask = masks[1];
244  *Gmask = masks[2];
245  *Bmask = masks[3];
246  break;
248  *Rmask = masks[0];
249  *Gmask = masks[1];
250  *Bmask = masks[2];
251  break;
253  *Amask = masks[0];
254  *Rmask = masks[1];
255  *Gmask = masks[2];
256  *Bmask = masks[3];
257  break;
259  *Rmask = masks[0];
260  *Gmask = masks[1];
261  *Bmask = masks[2];
262  *Amask = masks[3];
263  break;
265  *Bmask = masks[1];
266  *Gmask = masks[2];
267  *Rmask = masks[3];
268  break;
270  *Bmask = masks[0];
271  *Gmask = masks[1];
272  *Rmask = masks[2];
273  break;
275  *Bmask = masks[0];
276  *Gmask = masks[1];
277  *Rmask = masks[2];
278  *Amask = masks[3];
279  break;
281  *Amask = masks[0];
282  *Bmask = masks[1];
283  *Gmask = masks[2];
284  *Rmask = masks[3];
285  break;
286  default:
287  SDL_SetError("Unknown pixel format");
288  return SDL_FALSE;
289  }
290  return SDL_TRUE;
291 }
#define SDL_SetError
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:128
@ SDL_PIXELTYPE_PACKED32
Definition: SDL_pixels.h:59
@ SDL_PIXELTYPE_PACKED8
Definition: SDL_pixels.h:57
@ SDL_PIXELTYPE_PACKED16
Definition: SDL_pixels.h:58
#define SDL_PIXELORDER(X)
Definition: SDL_pixels.h:125
#define SDL_BITSPERPIXEL(X)
Definition: SDL_pixels.h:127
@ SDL_PACKEDLAYOUT_8888
Definition: SDL_pixels.h:112
@ SDL_PACKEDLAYOUT_1010102
Definition: SDL_pixels.h:114
@ SDL_PACKEDLAYOUT_565
Definition: SDL_pixels.h:111
@ SDL_PACKEDLAYOUT_332
Definition: SDL_pixels.h:107
@ SDL_PACKEDLAYOUT_5551
Definition: SDL_pixels.h:110
@ SDL_PACKEDLAYOUT_1555
Definition: SDL_pixels.h:109
@ SDL_PACKEDLAYOUT_4444
Definition: SDL_pixels.h:108
@ SDL_PACKEDLAYOUT_2101010
Definition: SDL_pixels.h:113
#define SDL_PIXELLAYOUT(X)
Definition: SDL_pixels.h:126
#define SDL_PIXELTYPE(X)
Definition: SDL_pixels.h:124
@ SDL_PACKEDORDER_RGBX
Definition: SDL_pixels.h:80
@ SDL_PACKEDORDER_BGRX
Definition: SDL_pixels.h:84
@ SDL_PACKEDORDER_XBGR
Definition: SDL_pixels.h:83
@ SDL_PACKEDORDER_RGBA
Definition: SDL_pixels.h:82
@ SDL_PACKEDORDER_ABGR
Definition: SDL_pixels.h:85
@ SDL_PACKEDORDER_BGRA
Definition: SDL_pixels.h:86
@ SDL_PACKEDORDER_XRGB
Definition: SDL_pixels.h:79
@ SDL_PACKEDORDER_ARGB
Definition: SDL_pixels.h:81
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:167

References bpp, SDL_BITSPERPIXEL, SDL_BYTESPERPIXEL, SDL_FALSE, SDL_ISPIXELFORMAT_FOURCC, SDL_PACKEDLAYOUT_1010102, SDL_PACKEDLAYOUT_1555, SDL_PACKEDLAYOUT_2101010, SDL_PACKEDLAYOUT_332, SDL_PACKEDLAYOUT_4444, SDL_PACKEDLAYOUT_5551, SDL_PACKEDLAYOUT_565, SDL_PACKEDLAYOUT_8888, SDL_PACKEDORDER_ABGR, SDL_PACKEDORDER_ARGB, SDL_PACKEDORDER_BGRA, SDL_PACKEDORDER_BGRX, SDL_PACKEDORDER_RGBA, SDL_PACKEDORDER_RGBX, SDL_PACKEDORDER_XBGR, SDL_PACKEDORDER_XRGB, SDL_PIXELFORMAT_BGR24, SDL_PIXELFORMAT_RGB24, SDL_PIXELLAYOUT, SDL_PIXELORDER, SDL_PIXELTYPE, SDL_PIXELTYPE_PACKED16, SDL_PIXELTYPE_PACKED32, SDL_PIXELTYPE_PACKED8, SDL_SetError, and SDL_TRUE.

Referenced by SDL_InitFormat().

◆ SDL_SetPaletteColors()

int SDL_SetPaletteColors ( SDL_Palette palette,
const SDL_Color colors,
int  firstcolor,
int  ncolors 
)

Set a range of colors in a palette.

Parameters
paletteThe palette to modify.
colorsAn array of colors to copy into the palette.
firstcolorThe index of the first palette entry to modify.
ncolorsThe number of entries to modify.
Returns
0 on success, or -1 if not all of the colors could be set.

Definition at line 705 of file SDL_pixels.c.

707 {
708  int status = 0;
709 
710  /* Verify the parameters */
711  if (!palette) {
712  return -1;
713  }
714  if (ncolors > (palette->ncolors - firstcolor)) {
715  ncolors = (palette->ncolors - firstcolor);
716  status = -1;
717  }
718 
719  if (colors != (palette->colors + firstcolor)) {
720  SDL_memcpy(palette->colors + firstcolor, colors,
721  ncolors * sizeof(*colors));
722  }
723  ++palette->version;
724  if (!palette->version) {
725  palette->version = 1;
726  }
727 
728  return status;
729 }
#define SDL_memcpy

References SDL_Palette::colors, colors, SDL_Palette::ncolors, SDL_memcpy, and SDL_Palette::version.

◆ SDL_SetPixelFormatPalette()

int SDL_SetPixelFormatPalette ( SDL_PixelFormat format,
SDL_Palette palette 
)

Set the palette for a pixel format structure.

Definition at line 677 of file SDL_pixels.c.

678 {
679  if (!format) {
680  return SDL_SetError("SDL_SetPixelFormatPalette() passed NULL format");
681  }
682 
683  if (palette && palette->ncolors > (1 << format->BitsPerPixel)) {
684  return SDL_SetError("SDL_SetPixelFormatPalette() passed a palette that doesn't match the format");
685  }
686 
687  if (format->palette == palette) {
688  return 0;
689  }
690 
691  if (format->palette) {
692  SDL_FreePalette(format->palette);
693  }
694 
695  format->palette = palette;
696 
697  if (format->palette) {
698  ++format->palette->refcount;
699  }
700 
701  return 0;
702 }

References SDL_Palette::ncolors, SDL_Palette::refcount, SDL_FreePalette(), and SDL_SetError.

Variable Documentation

◆ formats

Definition at line 499 of file SDL_pixels.c.

Referenced by SDL_AllocFormat().

◆ formats_lock

SDL_SpinLock formats_lock = 0
static

Definition at line 500 of file SDL_pixels.c.

Referenced by SDL_AllocFormat(), and SDL_FreeFormat().

◆ lookup_0

Uint8 lookup_0[]
static
Initial value:
= {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
}

Definition at line 35 of file SDL_pixels.c.

◆ lookup_1

Uint8 lookup_1[]
static
Initial value:
= {
0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 255
}

Definition at line 39 of file SDL_pixels.c.

◆ lookup_2

Uint8 lookup_2[]
static
Initial value:
= {
0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 129, 133, 137, 141, 145, 149, 153, 157, 161, 165, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 214, 218, 222, 226, 230, 234, 238, 242, 246, 250, 255
}

Definition at line 43 of file SDL_pixels.c.

◆ lookup_3

Uint8 lookup_3[]
static
Initial value:
= {
0, 8, 16, 24, 32, 41, 49, 57, 65, 74, 82, 90, 98, 106, 115, 123, 131, 139, 148, 156, 164, 172, 180, 189, 197, 205, 213, 222, 230, 238, 246, 255
}

Definition at line 47 of file SDL_pixels.c.

◆ lookup_4

Uint8 lookup_4[]
static
Initial value:
= {
0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255
}

Definition at line 51 of file SDL_pixels.c.

◆ lookup_5

Uint8 lookup_5[]
static
Initial value:
= {
0, 36, 72, 109, 145, 182, 218, 255
}

Definition at line 55 of file SDL_pixels.c.

◆ lookup_6

Uint8 lookup_6[]
static
Initial value:
= {
0, 85, 170, 255
}

Definition at line 59 of file SDL_pixels.c.

◆ lookup_7

Uint8 lookup_7[]
static
Initial value:
= {
0, 255
}

Definition at line 63 of file SDL_pixels.c.

◆ lookup_8

Uint8 lookup_8[]
static
Initial value:
= {
255
}

Definition at line 67 of file SDL_pixels.c.

◆ SDL_expand_byte

Uint8* SDL_expand_byte[9]
Initial value:
= {
}
static Uint8 lookup_8[]
Definition: SDL_pixels.c:67
static Uint8 lookup_1[]
Definition: SDL_pixels.c:39
static Uint8 lookup_4[]
Definition: SDL_pixels.c:51
static Uint8 lookup_0[]
Definition: SDL_pixels.c:35
static Uint8 lookup_3[]
Definition: SDL_pixels.c:47
static Uint8 lookup_5[]
Definition: SDL_pixels.c:55
static Uint8 lookup_6[]
Definition: SDL_pixels.c:59
static Uint8 lookup_2[]
Definition: SDL_pixels.c:43
static Uint8 lookup_7[]
Definition: SDL_pixels.c:63

Definition at line 71 of file SDL_pixels.c.

Referenced by SDL_GetRGB(), and SDL_GetRGBA().