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

962 {
963  SDL_BlitMap *map;
964 
965  /* Allocate the empty map */
966  map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
967  if (map == NULL) {
968  SDL_OutOfMemory();
969  return (NULL);
970  }
971  map->info.r = 0xFF;
972  map->info.g = 0xFF;
973  map->info.b = 0xFF;
974  map->info.a = 0xFF;
975 
976  /* It's ready to go */
977  return (map);
978 }
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:164
#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:290
SDL_BlitInfo info
Definition: SDL_blit.h:92
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_CalculatePitch()

int SDL_CalculatePitch ( SDL_Surface surface)

Definition at line 755 of file SDL_pixels.c.

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

Referenced by SDL_CreateRGBSurfaceWithFormat().

756 {
757  int pitch;
758 
759  /* Surface should be 4-byte aligned for speed */
760  pitch = surface->w * surface->format->BytesPerPixel;
761  switch (surface->format->BitsPerPixel) {
762  case 1:
763  pitch = (pitch + 7) / 8;
764  break;
765  case 4:
766  pitch = (pitch + 1) / 2;
767  break;
768  default:
769  break;
770  }
771  pitch = (pitch + 3) & ~3; /* 4-byte aligning */
772  return (pitch);
773 }
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 727 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().

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

◆ SDL_FindColor()

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

Definition at line 779 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().

780 {
781  /* Do colorspace distance matching */
782  unsigned int smallest;
783  unsigned int distance;
784  int rd, gd, bd, ad;
785  int i;
786  Uint8 pixel = 0;
787 
788  smallest = ~0;
789  for (i = 0; i < pal->ncolors; ++i) {
790  rd = pal->colors[i].r - r;
791  gd = pal->colors[i].g - g;
792  bd = pal->colors[i].b - b;
793  ad = pal->colors[i].a - a;
794  distance = (rd * rd) + (gd * gd) + (bd * bd) + (ad * ad);
795  if (distance < smallest) {
796  pixel = i;
797  if (distance == 0) { /* Perfect match! */
798  break;
799  }
800  smallest = distance;
801  }
802  }
803  return (pixel);
804 }
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Uint8 g
Definition: SDL_pixels.h:296
Uint8 b
Definition: SDL_pixels.h:297
GLsizei GLsizei GLfloat distance
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:153
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 g
GLboolean GLboolean GLboolean b

◆ SDL_FreeBlitMap()

void SDL_FreeBlitMap ( SDL_BlitMap map)

Definition at line 1086 of file SDL_pixels.c.

References SDL_free(), and SDL_InvalidateMap().

Referenced by SDL_FreeSurface().

1087 {
1088  if (map) {
1089  SDL_InvalidateMap(map);
1090  SDL_free(map);
1091  }
1092 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
void SDL_free(void *mem)

◆ SDL_InitFormat()

int SDL_InitFormat ( SDL_PixelFormat format,
Uint32  pixel_format 
)

Definition at line 528 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().

529 {
530  int bpp;
531  Uint32 Rmask, Gmask, Bmask, Amask;
532  Uint32 mask;
533 
535  &Rmask, &Gmask, &Bmask, &Amask)) {
536  return -1;
537  }
538 
539  /* Set up the format */
540  SDL_zerop(format);
541  format->format = pixel_format;
542  format->BitsPerPixel = bpp;
543  format->BytesPerPixel = (bpp + 7) / 8;
544 
545  format->Rmask = Rmask;
546  format->Rshift = 0;
547  format->Rloss = 8;
548  if (Rmask) {
549  for (mask = Rmask; !(mask & 0x01); mask >>= 1)
550  ++format->Rshift;
551  for (; (mask & 0x01); mask >>= 1)
552  --format->Rloss;
553  }
554 
555  format->Gmask = Gmask;
556  format->Gshift = 0;
557  format->Gloss = 8;
558  if (Gmask) {
559  for (mask = Gmask; !(mask & 0x01); mask >>= 1)
560  ++format->Gshift;
561  for (; (mask & 0x01); mask >>= 1)
562  --format->Gloss;
563  }
564 
565  format->Bmask = Bmask;
566  format->Bshift = 0;
567  format->Bloss = 8;
568  if (Bmask) {
569  for (mask = Bmask; !(mask & 0x01); mask >>= 1)
570  ++format->Bshift;
571  for (; (mask & 0x01); mask >>= 1)
572  --format->Bloss;
573  }
574 
575  format->Amask = Amask;
576  format->Ashift = 0;
577  format->Aloss = 8;
578  if (Amask) {
579  for (mask = Amask; !(mask & 0x01); mask >>= 1)
580  ++format->Ashift;
581  for (; (mask & 0x01); mask >>= 1)
582  --format->Aloss;
583  }
584 
585  format->palette = NULL;
586  format->refcount = 1;
587  format->next = NULL;
588 
589  return 0;
590 }
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:169
#define SDL_zerop(x)
Definition: SDL_stdinc.h:370
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
GLenum GLint GLuint mask
#define NULL
Definition: begin_code.h:164
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 981 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().

982 {
983  if (!map) {
984  return;
985  }
986  if (map->dst) {
987  /* Release our reference to the surface - see the note below */
988  if (--map->dst->refcount <= 0) {
989  SDL_FreeSurface(map->dst);
990  }
991  }
992  map->dst = NULL;
993  map->src_palette_version = 0;
994  map->dst_palette_version = 0;
995  SDL_free(map->info.table);
996  map->info.table = NULL;
997 }
Uint8 * table
Definition: SDL_blit.h:67
Uint32 dst_palette_version
Definition: SDL_blit.h:96
Uint32 src_palette_version
Definition: SDL_blit.h:97
#define SDL_FreeSurface
void SDL_free(void *mem)
SDL_Surface * dst
Definition: SDL_blit.h:88
#define NULL
Definition: begin_code.h:164
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_MapSurface()

int SDL_MapSurface ( SDL_Surface src,
SDL_Surface dst 
)

Definition at line 1000 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().

1001 {
1002  SDL_PixelFormat *srcfmt;
1003  SDL_PixelFormat *dstfmt;
1004  SDL_BlitMap *map;
1005 
1006  /* Clear out any previous mapping */
1007  map = src->map;
1008  if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
1009  SDL_UnRLESurface(src, 1);
1010  }
1011  SDL_InvalidateMap(map);
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 }
Uint8 * table
Definition: SDL_blit.h:67
Uint8 r
Definition: SDL_blit.h:70
Uint32 version
Definition: SDL_pixels.h:306
Uint8 b
Definition: SDL_blit.h:70
GLenum GLenum dst
#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:96
Uint32 flags
Definition: SDL_surface.h:71
Uint32 src_palette_version
Definition: SDL_blit.h:97
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
static Uint8 * Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
Definition: SDL_pixels.c:885
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:947
SDL_Surface * dst
Definition: SDL_blit.h:88
#define NULL
Definition: begin_code.h:164
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:919
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:290
int identity
Definition: SDL_blit.h:89
SDL_BlitInfo info
Definition: SDL_blit.h:92
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint8 a
Definition: SDL_blit.h:70