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)
 
int SDL_CalculatePitch (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)
 

Function Documentation

§ SDL_AllocBlitMap()

SDL_BlitMap* SDL_AllocBlitMap ( void  )

Definition at line 954 of file SDL_pixels.c.

References SDL_BlitInfo::a, SDL_BlitInfo::b, SDL_BlitInfo::g, SDL_BlitMap::info, map, NULL, SDL_BlitInfo::r, SDL_calloc(), and SDL_OutOfMemory.

Referenced by SDL_CreateRGBSurfaceWithFormat().

955 {
956  SDL_BlitMap *map;
957 
958  /* Allocate the empty map */
959  map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
960  if (map == NULL) {
961  SDL_OutOfMemory();
962  return (NULL);
963  }
964  map->info.r = 0xFF;
965  map->info.g = 0xFF;
966  map->info.b = 0xFF;
967  map->info.a = 0xFF;
968 
969  /* It's ready to go */
970  return (map);
971 }
Uint8 r
Definition: SDL_blit.h:70
Uint8 b
Definition: SDL_blit.h:70
Uint8 g
Definition: SDL_blit.h:70
void * SDL_calloc(size_t nmemb, size_t size)
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
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:289
SDL_BlitInfo info
Definition: SDL_blit.h:91
Uint8 a
Definition: SDL_blit.h:70

§ SDL_CalculatePitch()

int SDL_CalculatePitch ( SDL_Surface surface)

Definition at line 748 of file SDL_pixels.c.

References SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::BytesPerPixel, SDL_Surface::format, and SDL_Surface::w.

Referenced by SDL_CreateRGBSurfaceWithFormat().

749 {
750  int pitch;
751 
752  /* Surface should be 4-byte aligned for speed */
753  pitch = surface->w * surface->format->BytesPerPixel;
754  switch (surface->format->BitsPerPixel) {
755  case 1:
756  pitch = (pitch + 7) / 8;
757  break;
758  case 4:
759  pitch = (pitch + 1) / 2;
760  break;
761  default:
762  break;
763  }
764  pitch = (pitch + 3) & ~3; /* 4-byte aligning */
765  return (pitch);
766 }
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
SDL_PixelFormat * format
Definition: SDL_surface.h:72

§ SDL_DitherColors()

void SDL_DitherColors ( SDL_Color colors,
int  bpp 
)

Definition at line 720 of file SDL_pixels.c.

References SDL_Color::a, SDL_Color::b, SDL_Color::g, i, SDL_Color::r, and SDL_ALPHA_OPAQUE.

Referenced by MapNto1().

721 {
722  int i;
723  if (bpp != 8)
724  return; /* only 8bpp supported right now */
725 
726  for (i = 0; i < 256; i++) {
727  int r, g, b;
728  /* map each bit field to the full [0, 255] interval,
729  so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
730  r = i & 0xe0;
731  r |= r >> 3 | r >> 6;
732  colors[i].r = r;
733  g = (i << 3) & 0xe0;
734  g |= g >> 3 | g >> 6;
735  colors[i].g = g;
736  b = i & 0x3;
737  b |= b << 2;
738  b |= b << 4;
739  colors[i].b = b;
740  colors[i].a = SDL_ALPHA_OPAQUE;
741  }
742 }
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
Uint8 g
Definition: SDL_pixels.h:296
Uint8 b
Definition: SDL_pixels.h:297
GLboolean GLboolean g
Uint8 r
Definition: SDL_pixels.h:295
Uint8 a
Definition: SDL_pixels.h:298
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 SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
GLboolean GLboolean GLboolean b

§ SDL_FindColor()

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

Definition at line 772 of file SDL_pixels.c.

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().

773 {
774  /* Do colorspace distance matching */
775  unsigned int smallest;
776  unsigned int distance;
777  int rd, gd, bd, ad;
778  int i;
779  Uint8 pixel = 0;
780 
781  smallest = ~0;
782  for (i = 0; i < pal->ncolors; ++i) {
783  rd = pal->colors[i].r - r;
784  gd = pal->colors[i].g - g;
785  bd = pal->colors[i].b - b;
786  ad = pal->colors[i].a - a;
787  distance = (rd * rd) + (gd * gd) + (bd * bd) + (ad * ad);
788  if (distance < smallest) {
789  pixel = i;
790  if (distance == 0) { /* Perfect match! */
791  break;
792  }
793  smallest = distance;
794  }
795  }
796  return (pixel);
797 }
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
Uint8 g
Definition: SDL_pixels.h:296
Uint8 b
Definition: SDL_pixels.h:297
GLsizei GLsizei GLfloat distance
GLboolean GLboolean g
Uint8 r
Definition: SDL_pixels.h:295
Uint8 a
Definition: SDL_pixels.h:298
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
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
SDL_Color * colors
Definition: SDL_pixels.h:305
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b

§ SDL_FreeBlitMap()

void SDL_FreeBlitMap ( SDL_BlitMap map)

Definition at line 1079 of file SDL_pixels.c.

References SDL_free(), and SDL_InvalidateMap().

Referenced by SDL_FreeSurface().

1080 {
1081  if (map) {
1082  SDL_InvalidateMap(map);
1083  SDL_free(map);
1084  }
1085 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
void SDL_free(void *mem)

§ SDL_InitFormat()

int SDL_InitFormat ( SDL_PixelFormat format,
Uint32  pixel_format 
)

Definition at line 521 of file SDL_pixels.c.

References SDL_PixelFormat::Aloss, SDL_PixelFormat::Amask, SDL_PixelFormat::Ashift, SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::Bloss, SDL_PixelFormat::Bmask, SDL_PixelFormat::Bshift, SDL_PixelFormat::BytesPerPixel, SDL_PixelFormat::format, SDL_PixelFormat::Gloss, SDL_PixelFormat::Gmask, SDL_PixelFormat::Gshift, SDL_PixelFormat::next, NULL, SDL_PixelFormat::palette, pixel_format, SDL_PixelFormat::refcount, SDL_PixelFormat::Rloss, SDL_PixelFormat::Rmask, SDL_PixelFormat::Rshift, SDL_PixelFormatEnumToMasks(), and SDL_zerop.

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

522 {
523  int bpp;
524  Uint32 Rmask, Gmask, Bmask, Amask;
525  Uint32 mask;
526 
528  &Rmask, &Gmask, &Bmask, &Amask)) {
529  return -1;
530  }
531 
532  /* Set up the format */
533  SDL_zerop(format);
534  format->format = pixel_format;
535  format->BitsPerPixel = bpp;
536  format->BytesPerPixel = (bpp + 7) / 8;
537 
538  format->Rmask = Rmask;
539  format->Rshift = 0;
540  format->Rloss = 8;
541  if (Rmask) {
542  for (mask = Rmask; !(mask & 0x01); mask >>= 1)
543  ++format->Rshift;
544  for (; (mask & 0x01); mask >>= 1)
545  --format->Rloss;
546  }
547 
548  format->Gmask = Gmask;
549  format->Gshift = 0;
550  format->Gloss = 8;
551  if (Gmask) {
552  for (mask = Gmask; !(mask & 0x01); mask >>= 1)
553  ++format->Gshift;
554  for (; (mask & 0x01); mask >>= 1)
555  --format->Gloss;
556  }
557 
558  format->Bmask = Bmask;
559  format->Bshift = 0;
560  format->Bloss = 8;
561  if (Bmask) {
562  for (mask = Bmask; !(mask & 0x01); mask >>= 1)
563  ++format->Bshift;
564  for (; (mask & 0x01); mask >>= 1)
565  --format->Bloss;
566  }
567 
568  format->Amask = Amask;
569  format->Ashift = 0;
570  format->Aloss = 8;
571  if (Amask) {
572  for (mask = Amask; !(mask & 0x01); mask >>= 1)
573  ++format->Ashift;
574  for (; (mask & 0x01); mask >>= 1)
575  --format->Aloss;
576  }
577 
578  format->palette = NULL;
579  format->refcount = 1;
580  format->next = NULL;
581 
582  return 0;
583 }
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
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:134
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:159
#define SDL_zerop(x)
Definition: SDL_stdinc.h:360
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
GLenum GLint GLuint mask
#define NULL
Definition: begin_code.h:143
Uint32 pixel_format
Definition: testoverlay2.c:152
SDL_Palette * palette
Definition: SDL_pixels.h:316
struct SDL_PixelFormat * next
Definition: SDL_pixels.h:333

§ SDL_InvalidateMap()

void SDL_InvalidateMap ( SDL_BlitMap map)

Definition at line 974 of file SDL_pixels.c.

References SDL_BlitMap::dst, SDL_BlitMap::dst_palette_version, SDL_BlitMap::info, NULL, SDL_Surface::refcount, SDL_free(), SDL_FreeSurface, SDL_BlitMap::src_palette_version, and SDL_BlitInfo::table.

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

975 {
976  if (!map) {
977  return;
978  }
979  if (map->dst) {
980  /* Release our reference to the surface - see the note below */
981  if (--map->dst->refcount <= 0) {
982  SDL_FreeSurface(map->dst);
983  }
984  }
985  map->dst = NULL;
986  map->src_palette_version = 0;
987  map->dst_palette_version = 0;
988  SDL_free(map->info.table);
989  map->info.table = NULL;
990 }
Uint8 * table
Definition: SDL_blit.h:67
Uint32 dst_palette_version
Definition: SDL_blit.h:95
Uint32 src_palette_version
Definition: SDL_blit.h:96
#define SDL_FreeSurface
void SDL_free(void *mem)
SDL_Surface * dst
Definition: SDL_blit.h:87
#define NULL
Definition: begin_code.h:143
SDL_BlitInfo info
Definition: SDL_blit.h:91

§ SDL_MapSurface()

int SDL_MapSurface ( SDL_Surface src,
SDL_Surface dst 
)

Definition at line 993 of file SDL_pixels.c.

References SDL_BlitInfo::a, SDL_BlitInfo::b, SDL_PixelFormat::BitsPerPixel, SDL_BlitMap::dst, SDL_BlitMap::dst_palette_version, SDL_Surface::flags, SDL_Surface::format, SDL_PixelFormat::format, SDL_BlitInfo::g, SDL_BlitMap::identity, SDL_BlitMap::info, SDL_Surface::map, map, Map1to1(), Map1toN(), MapNto1(), NULL, SDL_PixelFormat::palette, SDL_BlitInfo::r, SDL_Surface::refcount, SDL_CalculateBlit(), SDL_InvalidateMap(), SDL_ISPIXELFORMAT_INDEXED, SDL_RLEACCEL, SDL_UnRLESurface(), SDL_BlitMap::src_palette_version, SDL_BlitInfo::table, and SDL_Palette::version.

Referenced by SDL_LowerBlit().

994 {
995  SDL_PixelFormat *srcfmt;
996  SDL_PixelFormat *dstfmt;
997  SDL_BlitMap *map;
998 
999  /* Clear out any previous mapping */
1000  map = src->map;
1001  if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
1002  SDL_UnRLESurface(src, 1);
1003  }
1004  SDL_InvalidateMap(map);
1005 
1006  /* Figure out what kind of mapping we're doing */
1007  map->identity = 0;
1008  srcfmt = src->format;
1009  dstfmt = dst->format;
1010  if (SDL_ISPIXELFORMAT_INDEXED(srcfmt->format)) {
1011  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1012  /* Palette --> Palette */
1013  map->info.table =
1014  Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
1015  if (!map->identity) {
1016  if (map->info.table == NULL) {
1017  return (-1);
1018  }
1019  }
1020  if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
1021  map->identity = 0;
1022  } else {
1023  /* Palette --> BitField */
1024  map->info.table =
1025  Map1toN(srcfmt, src->map->info.r, src->map->info.g,
1026  src->map->info.b, src->map->info.a, dstfmt);
1027  if (map->info.table == NULL) {
1028  return (-1);
1029  }
1030  }
1031  } else {
1032  if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
1033  /* BitField --> Palette */
1034  map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
1035  if (!map->identity) {
1036  if (map->info.table == NULL) {
1037  return (-1);
1038  }
1039  }
1040  map->identity = 0; /* Don't optimize to copy */
1041  } else {
1042  /* BitField --> BitField */
1043  if (srcfmt == dstfmt) {
1044  map->identity = 1;
1045  }
1046  }
1047  }
1048 
1049  map->dst = dst;
1050 
1051  if (map->dst) {
1052  /* Keep a reference to this surface so it doesn't get deleted
1053  while we're still pointing at it.
1054 
1055  A better method would be for the destination surface to keep
1056  track of surfaces that are mapped to it and automatically
1057  invalidate them when it is freed, but this will do for now.
1058  */
1059  ++map->dst->refcount;
1060  }
1061 
1062  if (dstfmt->palette) {
1063  map->dst_palette_version = dstfmt->palette->version;
1064  } else {
1065  map->dst_palette_version = 0;
1066  }
1067 
1068  if (srcfmt->palette) {
1069  map->src_palette_version = srcfmt->palette->version;
1070  } else {
1071  map->src_palette_version = 0;
1072  }
1073 
1074  /* Choose your blitters wisely */
1075  return (SDL_CalculateBlit(src));
1076 }
Uint8 * table
Definition: SDL_blit.h:67
GLenum GLenum dst
Uint8 r
Definition: SDL_blit.h:70
Uint32 version
Definition: SDL_pixels.h:306
Uint8 b
Definition: SDL_blit.h:70
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
Uint8 g
Definition: SDL_blit.h:70
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 dst_palette_version
Definition: SDL_blit.h:95
Uint32 flags
Definition: SDL_surface.h:71
Uint32 src_palette_version
Definition: SDL_blit.h:96
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
static Uint8 * Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
Definition: SDL_pixels.c:878
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
int SDL_CalculateBlit(SDL_Surface *surface)
Definition: SDL_blit.c:216
static Uint8 * MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
Definition: SDL_pixels.c:940
SDL_Surface * dst
Definition: SDL_blit.h:87
#define NULL
Definition: begin_code.h:143
SDL_PixelFormat * format
Definition: SDL_surface.h:72
static Uint8 * Map1toN(SDL_PixelFormat *src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod, SDL_PixelFormat *dst)
Definition: SDL_pixels.c:912
SDL_Palette * palette
Definition: SDL_pixels.h:316
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:289
int identity
Definition: SDL_blit.h:88
SDL_BlitInfo info
Definition: SDL_blit.h:91
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint8 a
Definition: SDL_blit.h:70