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.

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 }

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

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;
763  }
764 }

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

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 }

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

1078 {
1079  if (map) {
1081  SDL_free(map);
1082  }
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 537 of file SDL_pixels.c.

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 }

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

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 }

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

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

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:319
mask
GLenum GLint GLuint mask
Definition: SDL_opengl_glext.h:657
SDL_Palette::ncolors
int ncolors
Definition: SDL_pixels.h:306
SDL_Color::b
Uint8 b
Definition: SDL_pixels.h:299
SDL_CalculateBlit
int SDL_CalculateBlit(SDL_Surface *surface)
Definition: SDL_blit.c:216
SDL_BlitMap
Definition: SDL_blit.h:86
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:317
SDL_ALPHA_OPAQUE
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
g
GLboolean GLboolean g
Definition: SDL_opengl_glext.h:1109
SDL_zerop
#define SDL_zerop(x)
Definition: SDL_stdinc.h:417
SDL_UnRLESurface
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Definition: SDL_RLEaccel.c:1545
SDL_Color::r
Uint8 r
Definition: SDL_pixels.h:297
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:134
MapNto1
static Uint8 * MapNto1(SDL_PixelFormat *src, SDL_PixelFormat *dst, int *identical)
Definition: SDL_pixels.c:938
SDL_ISPIXELFORMAT_INDEXED
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
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:300
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1737
SDL_Palette::colors
SDL_Color * colors
Definition: SDL_pixels.h:307
SDL_Color::g
Uint8 g
Definition: SDL_pixels.h:298
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:318
SDL_PixelFormat
Definition: SDL_pixels.h:315
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
distance
GLsizei GLsizei GLfloat distance
Definition: SDL_opengl_glext.h:9200
src
GLenum src
Definition: SDL_opengl_glext.h:1737
SDL_InvalidateMap
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:972
Map1toN
static Uint8 * Map1toN(SDL_PixelFormat *src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod, SDL_PixelFormat *dst)
Definition: SDL_pixels.c:910
SDL_Palette::version
Uint32 version
Definition: SDL_pixels.h:308
Map1to1
static Uint8 * Map1to1(SDL_Palette *src, SDL_Palette *dst, int *identical)
Definition: SDL_pixels.c:876
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