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_DitherColors (SDL_Color *colors, int bpp)
 
Uint8 SDL_FindColor (SDL_Palette *pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
 

Function Documentation

◆ SDL_AllocBlitMap()

SDL_BlitMap* SDL_AllocBlitMap ( void  )

Definition at line 959 of file SDL_pixels.c.

960 {
961  SDL_BlitMap *map;
962 
963  /* Allocate the empty map */
964  map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
965  if (map == NULL) {
966  SDL_OutOfMemory();
967  return (NULL);
968  }
969  map->info.r = 0xFF;
970  map->info.g = 0xFF;
971  map->info.b = 0xFF;
972  map->info.a = 0xFF;
973 
974  /* It's ready to go */
975  return (map);
976 }

References map, NULL, SDL_calloc, and SDL_OutOfMemory.

Referenced by SDL_CreateRGBSurfaceWithFormat().

◆ 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 }

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 }

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 1086 of file SDL_pixels.c.

1087 {
1088  if (map) {
1090  SDL_free(map);
1091  }
1092 }

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 
550  if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
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 }

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

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

◆ SDL_InvalidateMap()

void SDL_InvalidateMap ( SDL_BlitMap map)

Definition at line 979 of file SDL_pixels.c.

980 {
981  if (!map) {
982  return;
983  }
984  if (map->dst) {
985  /* Release our reference to the surface - see the note below */
986  if (--map->dst->refcount <= 0) {
987  SDL_FreeSurface(map->dst);
988  }
989  }
990  map->dst = NULL;
991  map->src_palette_version = 0;
992  map->dst_palette_version = 0;
993  SDL_free(map->info.table);
994  map->info.table = NULL;
995 }

References map, NULL, SDL_free, and SDL_FreeSurface.

Referenced by SDL_CalculateBlit(), SDL_ConvertSurface(), SDL_FreeBlitMap(), SDL_FreeSurface(), 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 998 of file SDL_pixels.c.

999 {
1000  SDL_PixelFormat *srcfmt;
1001  SDL_PixelFormat *dstfmt;
1002  SDL_BlitMap *map;
1003 
1004  /* Clear out any previous mapping */
1005  map = src->map;
1006 #if SDL_HAVE_RLE
1007  if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
1008  SDL_UnRLESurface(src, 1);
1009  }
1010 #endif
1012 
1013  /* Figure out what kind of mapping we're doing */
1014  map->identity = 0;
1015  srcfmt = src->format;
1016  dstfmt = dst->format;
1017  if (SDL_ISPIXELFORMAT_INDEXED(srcfmt->format)) {
1018  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1019  /* Palette --> Palette */
1020  map->info.table =
1021  Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
1022  if (!map->identity) {
1023  if (map->info.table == NULL) {
1024  return (-1);
1025  }
1026  }
1027  if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
1028  map->identity = 0;
1029  } else {
1030  /* Palette --> BitField */
1031  map->info.table =
1032  Map1toN(srcfmt, src->map->info.r, src->map->info.g,
1033  src->map->info.b, src->map->info.a, dstfmt);
1034  if (map->info.table == NULL) {
1035  return (-1);
1036  }
1037  }
1038  } else {
1039  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1040  /* BitField --> Palette */
1041  map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
1042  if (!map->identity) {
1043  if (map->info.table == NULL) {
1044  return (-1);
1045  }
1046  }
1047  map->identity = 0; /* Don't optimize to copy */
1048  } else {
1049  /* BitField --> BitField */
1050  if (srcfmt == dstfmt) {
1051  map->identity = 1;
1052  }
1053  }
1054  }
1055 
1056  map->dst = dst;
1057 
1058  if (map->dst) {
1059  /* Keep a reference to this surface so it doesn't get deleted
1060  while we're still pointing at it.
1061 
1062  A better method would be for the destination surface to keep
1063  track of surfaces that are mapped to it and automatically
1064  invalidate them when it is freed, but this will do for now.
1065  */
1066  ++map->dst->refcount;
1067  }
1068 
1069  if (dstfmt->palette) {
1070  map->dst_palette_version = dstfmt->palette->version;
1071  } else {
1072  map->dst_palette_version = 0;
1073  }
1074 
1075  if (srcfmt->palette) {
1076  map->src_palette_version = srcfmt->palette->version;
1077  } else {
1078  map->src_palette_version = 0;
1079  }
1080 
1081  /* Choose your blitters wisely */
1082  return (SDL_CalculateBlit(src));
1083 }

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

Referenced by SDL_LowerBlit().

format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_PixelFormat::BitsPerPixel
Uint8 BitsPerPixel
Definition: SDL_pixels.h:322
mask
GLenum GLint GLuint mask
Definition: SDL_opengl_glext.h:660
SDL_Palette::ncolors
int ncolors
Definition: SDL_pixels.h:309
SDL_Color::b
Uint8 b
Definition: SDL_pixels.h:302
SDL_CalculateBlit
int SDL_CalculateBlit(SDL_Surface *surface)
Definition: SDL_blit.c:196
SDL_BlitMap
Definition: SDL_blit.h:87
SDL_RLEACCEL
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
NULL
#define NULL
Definition: begin_code.h:167
SDL_PixelFormat::format
Uint32 format
Definition: SDL_pixels.h:320
SDL_ALPHA_OPAQUE
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1112
g
GLboolean GLboolean g
Definition: SDL_opengl_glext.h:1112
SDL_zerop
#define SDL_zerop(x)
Definition: SDL_stdinc.h:419
SDL_UnRLESurface
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Definition: SDL_RLEaccel.c:1547
SDL_Color::r
Uint8 r
Definition: SDL_pixels.h:300
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
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.
Definition: SDL_pixels.c:135
MapNto1
static Uint8 * MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
Definition: SDL_pixels.c:945
SDL_ISPIXELFORMAT_INDEXED
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1112
map
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
SDL_Color::a
Uint8 a
Definition: SDL_pixels.h:303
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1740
SDL_Palette::colors
SDL_Color * colors
Definition: SDL_pixels.h:310
SDL_Color::g
Uint8 g
Definition: SDL_pixels.h:301
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:446
SDL_PixelFormat::palette
SDL_Palette * palette
Definition: SDL_pixels.h:321
SDL_PixelFormat
Definition: SDL_pixels.h:318
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
bpp
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
Definition: pixman-arm-neon-asm.h:146
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
distance
GLsizei GLsizei GLfloat distance
Definition: SDL_opengl_glext.h:9203
src
GLenum src
Definition: SDL_opengl_glext.h:1740
SDL_InvalidateMap
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:979
Map1toN
static Uint8 * Map1toN(SDL_PixelFormat *src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod, SDL_PixelFormat *dst)
Definition: SDL_pixels.c:917
SDL_Palette::version
Uint32 version
Definition: SDL_pixels.h:311
Map1to1
static Uint8 * Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
Definition: SDL_pixels.c:883
i
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
colors
static int colors[7]
Definition: testgesture.c:41
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179