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

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

void SDL_DitherColors ( SDL_Color colors,
int  bpp 
)

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

743 {
744  int i;
745  if (bpp != 8)
746  return; /* only 8bpp supported right now */
747 
748  for (i = 0; i < 256; i++) {
749  int r, g, b;
750  /* map each bit field to the full [0, 255] interval,
751  so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
752  r = i & 0xe0;
753  r |= r >> 3 | r >> 6;
754  colors[i].r = r;
755  g = (i << 3) & 0xe0;
756  g |= g >> 3 | g >> 6;
757  colors[i].g = g;
758  b = i & 0x3;
759  b |= b << 2;
760  b |= b << 4;
761  colors[i].b = b;
762  colors[i].a = SDL_ALPHA_OPAQUE;
763  }
764 }
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Uint8 g
Definition: SDL_pixels.h:298
Uint8 b
Definition: SDL_pixels.h:299
Uint8 r
Definition: SDL_pixels.h:297
Uint8 a
Definition: SDL_pixels.h:300
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 770 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().

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

◆ SDL_FreeBlitMap()

void SDL_FreeBlitMap ( SDL_BlitMap map)

Definition at line 1077 of file SDL_pixels.c.

References SDL_free, and SDL_InvalidateMap().

Referenced by SDL_FreeSurface().

1078 {
1079  if (map) {
1080  SDL_InvalidateMap(map);
1081  SDL_free(map);
1082  }
1083 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
#define SDL_free

◆ SDL_InitFormat()

int SDL_InitFormat ( SDL_PixelFormat format,
Uint32  pixel_format 
)

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

538 {
539  int bpp;
540  Uint32 Rmask, Gmask, Bmask, Amask;
541  Uint32 mask;
542 
543  if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
544  &Rmask, &Gmask, &Bmask, &Amask)) {
545  return -1;
546  }
547 
548  /* Set up the format */
549  SDL_zerop(format);
550  format->format = pixel_format;
551  format->BitsPerPixel = bpp;
552  format->BytesPerPixel = (bpp + 7) / 8;
553 
554  format->Rmask = Rmask;
555  format->Rshift = 0;
556  format->Rloss = 8;
557  if (Rmask) {
558  for (mask = Rmask; !(mask & 0x01); mask >>= 1)
559  ++format->Rshift;
560  for (; (mask & 0x01); mask >>= 1)
561  --format->Rloss;
562  }
563 
564  format->Gmask = Gmask;
565  format->Gshift = 0;
566  format->Gloss = 8;
567  if (Gmask) {
568  for (mask = Gmask; !(mask & 0x01); mask >>= 1)
569  ++format->Gshift;
570  for (; (mask & 0x01); mask >>= 1)
571  --format->Gloss;
572  }
573 
574  format->Bmask = Bmask;
575  format->Bshift = 0;
576  format->Bloss = 8;
577  if (Bmask) {
578  for (mask = Bmask; !(mask & 0x01); mask >>= 1)
579  ++format->Bshift;
580  for (; (mask & 0x01); mask >>= 1)
581  --format->Bloss;
582  }
583 
584  format->Amask = Amask;
585  format->Ashift = 0;
586  format->Aloss = 8;
587  if (Amask) {
588  for (mask = Amask; !(mask & 0x01); mask >>= 1)
589  ++format->Ashift;
590  for (; (mask & 0x01); mask >>= 1)
591  --format->Aloss;
592  }
593 
594  format->palette = NULL;
595  format->refcount = 1;
596  format->next = NULL;
597 
598  return 0;
599 }
Uint8 BytesPerPixel
Definition: SDL_pixels.h:320
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
Definition: SDL_stdinc.h:181
#define SDL_zerop(x)
Definition: SDL_stdinc.h:417
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
GLenum GLint GLuint mask
#define NULL
Definition: begin_code.h:164
SDL_Palette * palette
Definition: SDL_pixels.h:318
struct SDL_PixelFormat * next
Definition: SDL_pixels.h:335

◆ SDL_InvalidateMap()

void SDL_InvalidateMap ( SDL_BlitMap map)

Definition at line 972 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_FreeSurface(), SDL_LowerBlitScaled(), SDL_MapSurface(), SDL_SetColorKey(), SDL_SetSurfaceAlphaMod(), SDL_SetSurfaceBlendMode(), SDL_SetSurfaceColorMod(), SDL_SetSurfacePalette(), SDL_SetSurfaceRLE(), and SDL_UpperBlit().

973 {
974  if (!map) {
975  return;
976  }
977  if (map->dst) {
978  /* Release our reference to the surface - see the note below */
979  if (--map->dst->refcount <= 0) {
980  SDL_FreeSurface(map->dst);
981  }
982  }
983  map->dst = NULL;
984  map->src_palette_version = 0;
985  map->dst_palette_version = 0;
986  SDL_free(map->info.table);
987  map->info.table = NULL;
988 }
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
#define SDL_free
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 991 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().

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