SDL  2.0
SDL_pixels_c.h File Reference
#include "../SDL_internal.h"
#include "SDL_blit.h"
+ Include dependency graph for SDL_pixels_c.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int SDL_InitFormat (SDL_PixelFormat *format, Uint32 pixel_format)
 
SDL_BlitMapSDL_AllocBlitMap (void)
 
void SDL_InvalidateMap (SDL_BlitMap *map)
 
int SDL_MapSurface (SDL_Surface *src, SDL_Surface *dst)
 
void SDL_FreeBlitMap (SDL_BlitMap *map)
 
void SDL_InvalidateAllBlitMap (SDL_Surface *surface)
 
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)
 

Function Documentation

◆ 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
#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
#define NULL
Definition: begin_code.h:163

References map, NULL, SDL_calloc, and SDL_OutOfMemory.

Referenced by SDL_CreateRGBSurfaceWithFormat().

◆ 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
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
Uint8 a
Definition: SDL_pixels.h:309
SDL_Color * colors
Definition: SDL_pixels.h:316

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
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
static int colors[7]
Definition: testgesture.c:41

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
Uint8 r
Definition: SDL_pixels.h:306
Uint8 b
Definition: SDL_pixels.h:308
Uint8 g
Definition: SDL_pixels.h:307

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 }
#define SDL_free
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1083

References map, SDL_free, and SDL_InvalidateMap().

Referenced by SDL_FreeSurface().

◆ 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 }
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
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_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
GLenum src
GLenum GLenum dst
static Uint8 * Map1toN(SDL_PixelFormat *src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod, SDL_PixelFormat *dst)
Definition: SDL_pixels.c:965
static Uint8 * Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
Definition: SDL_pixels.c:931
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_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint32 version
Definition: SDL_pixels.h:317
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().