SDL  2.0
SDL_surface.h File Reference
#include "SDL_stdinc.h"
#include "SDL_pixels.h"
#include "SDL_rect.h"
#include "SDL_blendmode.h"
#include "SDL_rwops.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_surface.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_Surface
 A collection of pixels used in software blitting. More...
 

Macros

#define SDL_MUSTLOCK(S)   (((S)->flags & SDL_RLEACCEL) != 0)
 
#define SDL_LoadBMP(file)   SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
 
#define SDL_SaveBMP(surface, file)   SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
 
#define SDL_BlitSurface   SDL_UpperBlit
 
#define SDL_BlitScaled   SDL_UpperBlitScaled
 
Surface flags

These are the currently supported flags for the SDL_Surface.

Used internally (read-only).

#define SDL_SWSURFACE   0
 
#define SDL_PREALLOC   0x00000001
 
#define SDL_RLEACCEL   0x00000002
 
#define SDL_DONTFREE   0x00000004
 

Typedefs

typedef int(* SDL_blit) (struct SDL_Surface *src, SDL_Rect *srcrect, struct SDL_Surface *dst, SDL_Rect *dstrect)
 The type of function used for surface blitting functions. More...
 

Functions

SDL_SurfaceSDL_CreateRGBSurface (Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
 
SDL_SurfaceSDL_CreateRGBSurfaceFrom (void *pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
 
void SDL_FreeSurface (SDL_Surface *surface)
 
int SDL_SetSurfacePalette (SDL_Surface *surface, SDL_Palette *palette)
 Set the palette used by a surface. More...
 
int SDL_LockSurface (SDL_Surface *surface)
 Sets up a surface for directly accessing the pixels. More...
 
void SDL_UnlockSurface (SDL_Surface *surface)
 
SDL_SurfaceSDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
 
int SDL_SaveBMP_RW (SDL_Surface *surface, SDL_RWops *dst, int freedst)
 
int SDL_SetSurfaceRLE (SDL_Surface *surface, int flag)
 Sets the RLE acceleration hint for a surface. More...
 
int SDL_SetColorKey (SDL_Surface *surface, int flag, Uint32 key)
 Sets the color key (transparent pixel) in a blittable surface. More...
 
int SDL_GetColorKey (SDL_Surface *surface, Uint32 *key)
 Gets the color key (transparent pixel) in a blittable surface. More...
 
int SDL_SetSurfaceColorMod (SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
 Set an additional color value used in blit operations. More...
 
int SDL_GetSurfaceColorMod (SDL_Surface *surface, Uint8 *r, Uint8 *g, Uint8 *b)
 Get the additional color value used in blit operations. More...
 
int SDL_SetSurfaceAlphaMod (SDL_Surface *surface, Uint8 alpha)
 Set an additional alpha value used in blit operations. More...
 
int SDL_GetSurfaceAlphaMod (SDL_Surface *surface, Uint8 *alpha)
 Get the additional alpha value used in blit operations. More...
 
int SDL_SetSurfaceBlendMode (SDL_Surface *surface, SDL_BlendMode blendMode)
 Set the blend mode used for blit operations. More...
 
int SDL_GetSurfaceBlendMode (SDL_Surface *surface, SDL_BlendMode *blendMode)
 Get the blend mode used for blit operations. More...
 
SDL_bool SDL_SetClipRect (SDL_Surface *surface, const SDL_Rect *rect)
 
void SDL_GetClipRect (SDL_Surface *surface, SDL_Rect *rect)
 
SDL_SurfaceSDL_ConvertSurface (SDL_Surface *src, const SDL_PixelFormat *fmt, Uint32 flags)
 
SDL_SurfaceSDL_ConvertSurfaceFormat (SDL_Surface *src, Uint32 pixel_format, Uint32 flags)
 
int SDL_ConvertPixels (int width, int height, Uint32 src_format, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch)
 Copy a block of pixels of one format to another format. More...
 
int SDL_FillRect (SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
 
int SDL_FillRects (SDL_Surface *dst, const SDL_Rect *rects, int count, Uint32 color)
 
int SDL_UpperBlit (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_SoftStretch (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect)
 Perform a fast, low quality, stretch blit between two surfaces of the same pixel format. More...
 
int SDL_UpperBlitScaled (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_LowerBlitScaled (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 

Detailed Description

Header file for SDL_Surface definition and management functions.

Definition in file SDL_surface.h.

Macro Definition Documentation

#define SDL_BlitScaled   SDL_UpperBlitScaled

Definition at line 476 of file SDL_surface.h.

Referenced by SW_RenderCopy(), and SW_RenderCopyEx().

#define SDL_BlitSurface   SDL_UpperBlit

Performs a fast blit from the source surface to the destination surface.

This assumes that the source and destination rectangles are the same size. If either srcrect or dstrect are NULL, the entire surface (src or dst) is copied. The final blit rectangles are saved in srcrect and dstrect after all clipping is performed.

Returns
If the blit is successful, it returns 0, otherwise it returns -1.

The blit function should not be called on a locked surface.

The blit semantics for surfaces with and without blending and colorkey are defined as follows:

RGBA->RGB:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source alpha-channel and per-surface alpha)
    SDL_SRCCOLORKEY ignored.
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy RGB.
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    RGB values of the source color key, ignoring alpha in the
    comparison.

RGB->RGBA:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source per-surface alpha)
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy RGB, set destination alpha to source per-surface alpha value.
  both:
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    source color key.

RGBA->RGBA:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source alpha-channel and per-surface alpha)
    SDL_SRCCOLORKEY ignored.
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy all of RGBA to the destination.
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    RGB values of the source color key, ignoring alpha in the
    comparison.

RGB->RGB:
  Source surface blend mode set to SDL_BLENDMODE_BLEND:
    alpha-blend (using the source per-surface alpha)
  Source surface blend mode set to SDL_BLENDMODE_NONE:
    copy RGB.
  both:
    if SDL_SRCCOLORKEY set, only copy the pixels matching the
    source color key.

You should call SDL_BlitSurface() unless you know exactly how SDL blitting works internally and how to use the other blit functions.

Definition at line 447 of file SDL_surface.h.

Referenced by _testBlitBlendMode(), SDL_UpperBlitScaled(), SW_RenderCopy(), and SW_RenderCopyEx().

#define SDL_DONTFREE   0x00000004

Surface is referenced internally

Definition at line 55 of file SDL_surface.h.

Referenced by SDL_DestroyWindow(), SDL_FreeSurface(), SDL_GetWindowSurface(), and SDL_RecreateWindow().

#define SDL_LoadBMP (   file)    SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)

Load a surface from a file.

Convenience macro.

Definition at line 182 of file SDL_surface.h.

Referenced by LoadSprite(), LoadTexture(), main(), SDLTest_LoadIcon(), surface_testLoadFailure(), and surface_testSaveLoadBitmap().

#define SDL_MUSTLOCK (   S)    (((S)->flags & SDL_RLEACCEL) != 0)

Evaluates to true if the surface needs to be locked before access.

Definition at line 61 of file SDL_surface.h.

Referenced by SDL_CalculateShapeBitmap(), SDL_CalculateShapeTree(), SDL_CreateTextureFromSurface(), SDL_RLEAlphaBlit(), SDL_RLEBlit(), SDL_SoftBlit(), SDL_SoftStretch(), SDLgfx_rotateSurface(), and SW_UpdateTexture().

#define SDL_PREALLOC   0x00000001

Surface uses preallocated memory

Definition at line 53 of file SDL_surface.h.

Referenced by RLEAlphaSurface(), RLEColorkeySurface(), SDL_CreateRGBSurfaceFrom(), SDL_CreateSurfaceOnStack(), SDL_FreeSurface(), and SDL_UnRLESurface().

#define SDL_SaveBMP (   surface,
  file 
)    SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)

Save a surface to a file.

Convenience macro.

Definition at line 199 of file SDL_surface.h.

Referenced by SDLTest_CompareSurfaces(), SDLTest_ScreenShot(), and surface_testSaveLoadBitmap().

#define SDL_SWSURFACE   0

Just here for compatibility

Definition at line 52 of file SDL_surface.h.

Referenced by _surfaceSetUp(), SDLgfx_rotateSurface(), SDLTest_DrawCharacter(), and SW_RenderCopyEx().

Typedef Documentation

typedef int(* SDL_blit) (struct SDL_Surface *src, SDL_Rect *srcrect, struct SDL_Surface *dst, SDL_Rect *dstrect)

The type of function used for surface blitting functions.

Definition at line 97 of file SDL_surface.h.

Function Documentation

int SDL_ConvertPixels ( int  width,
int  height,
Uint32  src_format,
const void src,
int  src_pitch,
Uint32  dst_format,
void dst,
int  dst_pitch 
)

Copy a block of pixels of one format to another format.

Returns
0 on success, or -1 if there was an error

Definition at line 1022 of file SDL_surface.c.

References SDL_Rect::h, i, rect, SDL_BYTESPERPIXEL, SDL_CreateSurfaceOnStack(), SDL_InvalidParamError, SDL_ISPIXELFORMAT_FOURCC, SDL_LowerBlit(), SDL_memcpy, SDL_PIXELFORMAT_IYUV, SDL_PIXELFORMAT_NV12, SDL_PIXELFORMAT_NV21, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_YUY2, SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_YVYU, SDL_SetError, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

1025 {
1026  SDL_Surface src_surface, dst_surface;
1027  SDL_PixelFormat src_fmt, dst_fmt;
1028  SDL_BlitMap src_blitmap, dst_blitmap;
1029  SDL_Rect rect;
1030  void *nonconst_src = (void *) src;
1031 
1032  /* Check to make sure we are blitting somewhere, so we don't crash */
1033  if (!dst) {
1034  return SDL_InvalidParamError("dst");
1035  }
1036  if (!dst_pitch) {
1037  return SDL_InvalidParamError("dst_pitch");
1038  }
1039 
1040  /* Fast path for same format copy */
1041  if (src_format == dst_format) {
1042  int bpp, i;
1043 
1044  if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
1045  switch (src_format) {
1046  case SDL_PIXELFORMAT_YUY2:
1047  case SDL_PIXELFORMAT_UYVY:
1048  case SDL_PIXELFORMAT_YVYU:
1049  bpp = 2;
1050  break;
1051  case SDL_PIXELFORMAT_YV12:
1052  case SDL_PIXELFORMAT_IYUV:
1053  case SDL_PIXELFORMAT_NV12:
1054  case SDL_PIXELFORMAT_NV21:
1055  bpp = 1;
1056  break;
1057  default:
1058  return SDL_SetError("Unknown FOURCC pixel format");
1059  }
1060  } else {
1061  bpp = SDL_BYTESPERPIXEL(src_format);
1062  }
1063  width *= bpp;
1064 
1065  for (i = height; i--;) {
1066  SDL_memcpy(dst, src, width);
1067  src = (Uint8*)src + src_pitch;
1068  dst = (Uint8*)dst + dst_pitch;
1069  }
1070 
1071  if (src_format == SDL_PIXELFORMAT_YV12 || src_format == SDL_PIXELFORMAT_IYUV) {
1072  /* U and V planes are a quarter the size of the Y plane */
1073  width /= 2;
1074  height /= 2;
1075  src_pitch /= 2;
1076  dst_pitch /= 2;
1077  for (i = height * 2; i--;) {
1078  SDL_memcpy(dst, src, width);
1079  src = (Uint8*)src + src_pitch;
1080  dst = (Uint8*)dst + dst_pitch;
1081  }
1082  } else if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
1083  /* U/V plane is half the height of the Y plane */
1084  height /= 2;
1085  for (i = height; i--;) {
1086  SDL_memcpy(dst, src, width);
1087  src = (Uint8*)src + src_pitch;
1088  dst = (Uint8*)dst + dst_pitch;
1089  }
1090  }
1091  return 0;
1092  }
1093 
1094  if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
1095  src_pitch,
1096  &src_surface, &src_fmt, &src_blitmap)) {
1097  return -1;
1098  }
1099  if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
1100  &dst_surface, &dst_fmt, &dst_blitmap)) {
1101  return -1;
1102  }
1103 
1104  /* Set up the rect and go! */
1105  rect.x = 0;
1106  rect.y = 0;
1107  rect.w = width;
1108  rect.h = height;
1109  return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
1110 }
GLenum GLenum dst
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
SDL_Rect rect
Definition: testrelative.c:27
static SDL_INLINE SDL_bool SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format, void *pixels, int pitch, SDL_Surface *surface, SDL_PixelFormat *format, SDL_BlitMap *blitmap)
Definition: SDL_surface.c:984
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:127
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:500
#define SDL_memcpy
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
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:42
#define SDL_SetError
int h
Definition: SDL_rect.h:67
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:166
GLenum src
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
SDL_Surface* SDL_ConvertSurface ( SDL_Surface src,
const SDL_PixelFormat fmt,
Uint32  flags 
)

Creates a new surface of the specified format, and then copies and maps the given surface to it so the blit of the converted surface will be as fast as possible. If this function fails, it returns NULL.

The flags parameter is passed to SDL_CreateRGBSurface() and has those semantics. You can also pass SDL_RLEACCEL in the flags parameter and SDL will try to RLE accelerate colorkey and alpha blits in the resulting surface.

Definition at line 842 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_Color::a, SDL_PixelFormat::Amask, SDL_BlitInfo::b, SDL_Color::b, SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::Bmask, SDL_Surface::clip_rect, SDL_BlitInfo::colorkey, SDL_Palette::colors, SDL_BlitInfo::flags, SDL_Surface::format, SDL_BlitInfo::g, SDL_Color::g, SDL_PixelFormat::Gmask, SDL_Rect::h, SDL_Surface::h, i, SDL_BlitMap::info, SDL_Surface::map, SDL_Palette::ncolors, NULL, SDL_PixelFormat::palette, SDL_BlitInfo::r, SDL_Color::r, SDL_PixelFormat::Rmask, SDL_BLENDMODE_BLEND, SDL_ConvertColorkeyToAlpha(), SDL_COPY_BLEND, SDL_COPY_COLORKEY, SDL_COPY_MODULATE_ALPHA, SDL_COPY_RLE_ALPHAKEY, SDL_COPY_RLE_COLORKEY, SDL_COPY_RLE_DESIRED, SDL_CreateRGBSurface(), SDL_FALSE, SDL_GetRGBA, SDL_InvalidateMap(), SDL_LowerBlit(), SDL_MapRGBA, SDL_memcmp, SDL_memcpy, SDL_RLEACCEL, SDL_SetClipRect(), SDL_SetColorKey(), SDL_SetError, SDL_SetSurfaceBlendMode(), SDL_SetSurfaceRLE(), SDL_TRUE, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_ConvertSurfaceFormat().

844 {
845  SDL_Surface *convert;
846  Uint32 copy_flags;
847  SDL_Color copy_color;
848  SDL_Rect bounds;
849 
850  /* Check for empty destination palette! (results in empty image) */
851  if (format->palette != NULL) {
852  int i;
853  for (i = 0; i < format->palette->ncolors; ++i) {
854  if ((format->palette->colors[i].r != 0xFF) ||
855  (format->palette->colors[i].g != 0xFF) ||
856  (format->palette->colors[i].b != 0xFF))
857  break;
858  }
859  if (i == format->palette->ncolors) {
860  SDL_SetError("Empty destination palette");
861  return (NULL);
862  }
863  }
864 
865  /* Create a new surface with the desired format */
866  convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
867  format->BitsPerPixel, format->Rmask,
868  format->Gmask, format->Bmask,
869  format->Amask);
870  if (convert == NULL) {
871  return (NULL);
872  }
873 
874  /* Copy the palette if any */
875  if (format->palette && convert->format->palette) {
876  SDL_memcpy(convert->format->palette->colors,
877  format->palette->colors,
878  format->palette->ncolors * sizeof(SDL_Color));
879  convert->format->palette->ncolors = format->palette->ncolors;
880  }
881 
882  /* Save the original copy flags */
883  copy_flags = surface->map->info.flags;
884  copy_color.r = surface->map->info.r;
885  copy_color.g = surface->map->info.g;
886  copy_color.b = surface->map->info.b;
887  copy_color.a = surface->map->info.a;
888  surface->map->info.r = 0xFF;
889  surface->map->info.g = 0xFF;
890  surface->map->info.b = 0xFF;
891  surface->map->info.a = 0xFF;
892  surface->map->info.flags = 0;
893  SDL_InvalidateMap(surface->map);
894 
895  /* Copy over the image data */
896  bounds.x = 0;
897  bounds.y = 0;
898  bounds.w = surface->w;
899  bounds.h = surface->h;
900  SDL_LowerBlit(surface, &bounds, convert, &bounds);
901 
902  /* Clean up the original surface, and update converted surface */
903  convert->map->info.r = copy_color.r;
904  convert->map->info.g = copy_color.g;
905  convert->map->info.b = copy_color.b;
906  convert->map->info.a = copy_color.a;
907  convert->map->info.flags =
908  (copy_flags &
912  surface->map->info.r = copy_color.r;
913  surface->map->info.g = copy_color.g;
914  surface->map->info.b = copy_color.b;
915  surface->map->info.a = copy_color.a;
916  surface->map->info.flags = copy_flags;
917  SDL_InvalidateMap(surface->map);
918  if (copy_flags & SDL_COPY_COLORKEY) {
919  SDL_bool set_colorkey_by_color = SDL_FALSE;
920 
921  if (surface->format->palette) {
922  if (format->palette &&
923  surface->format->palette->ncolors <= format->palette->ncolors &&
924  (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
925  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
926  /* The palette is identical, just set the same colorkey */
927  SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
928  } else if (format->Amask) {
929  /* The alpha was set in the destination from the palette */
930  } else {
931  set_colorkey_by_color = SDL_TRUE;
932  }
933  } else {
934  set_colorkey_by_color = SDL_TRUE;
935  }
936 
937  if (set_colorkey_by_color) {
938  /* Set the colorkey by color, which needs to be unique */
939  Uint8 keyR, keyG, keyB, keyA;
940 
941  SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
942  &keyG, &keyB, &keyA);
943  SDL_SetColorKey(convert, 1,
944  SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
945  /* This is needed when converting for 3D texture upload */
947  }
948  }
949  SDL_SetClipRect(convert, &surface->clip_rect);
950 
951  /* Enable alpha blending by default if the new surface has an
952  * alpha channel or alpha modulation */
953  if ((surface->format->Amask && format->Amask) ||
954  (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
956  }
957  if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
958  SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
959  }
960 
961  /* We're ready to go! */
962  return (convert);
963 }
Uint8 r
Definition: SDL_blit.h:70
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
Uint8 b
Definition: SDL_blit.h:70
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:457
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
Sets the RLE acceleration hint for a surface.
Definition: SDL_surface.c:156
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
Uint8 g
Definition: SDL_pixels.h:282
#define SDL_MapRGBA
Uint8 g
Definition: SDL_blit.h:70
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:391
static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
Definition: SDL_surface.c:240
Uint8 b
Definition: SDL_pixels.h:283
#define SDL_COPY_RLE_COLORKEY
Definition: SDL_blit.h:42
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:500
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:177
Uint32 colorkey
Definition: SDL_blit.h:69
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
SDL_bool
Definition: SDL_stdinc.h:126
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_memcpy
Uint8 r
Definition: SDL_pixels.h:281
Uint8 a
Definition: SDL_pixels.h:284
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
#define SDL_memcmp
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define SDL_GetRGBA
SDL_Rect clip_rect
Definition: SDL_surface.h:85
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:42
#define NULL
Definition: begin_code.h:143
SDL_Color * colors
Definition: SDL_pixels.h:291
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:34
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
int h
Definition: SDL_rect.h:67
#define SDL_COPY_RLE_ALPHAKEY
Definition: SDL_blit.h:43
GLbitfield flags
SDL_Palette * palette
Definition: SDL_pixels.h:302
int y
Definition: SDL_rect.h:66
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:91
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
Uint8 a
Definition: SDL_blit.h:70
SDL_Surface* SDL_ConvertSurfaceFormat ( SDL_Surface src,
Uint32  pixel_format,
Uint32  flags 
)

Definition at line 966 of file SDL_surface.c.

References NULL, SDL_AllocFormat, SDL_ConvertSurface(), SDL_FreeFormat, and SDL_INLINE.

968 {
969  SDL_PixelFormat *fmt;
970  SDL_Surface *convert = NULL;
971 
973  if (fmt) {
974  convert = SDL_ConvertSurface(surface, fmt, flags);
975  SDL_FreeFormat(fmt);
976  }
977  return convert;
978 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:842
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_AllocFormat
#define SDL_FreeFormat
#define NULL
Definition: begin_code.h:143
Uint32 pixel_format
Definition: testoverlay2.c:152
GLbitfield flags
SDL_Surface* SDL_CreateRGBSurface ( Uint32  flags,
int  width,
int  height,
int  depth,
Uint32  Rmask,
Uint32  Gmask,
Uint32  Bmask,
Uint32  Amask 
)

Allocate and free an RGB surface.

If the depth is 4 or 8 bits, an empty palette is allocated for the surface. If the depth is greater than 8 bits, the pixel format is set using the flags '[RGB]mask'.

If the function runs out of memory, it will return NULL.

Parameters
flagsThe flags are obsolete and should be set to 0.
widthThe width in pixels of the surface to create.
heightThe height in pixels of the surface to create.
depthThe depth in bits of the surface to create.
RmaskThe red mask of the surface to create.
GmaskThe green mask of the surface to create.
BmaskThe blue mask of the surface to create.
AmaskThe alpha mask of the surface to create.

Definition at line 34 of file SDL_surface.c.

References SDL_Color::b, SDL_PixelFormat::BitsPerPixel, SDL_Palette::colors, SDL_Surface::format, SDL_PixelFormat::format, SDL_Color::g, SDL_Surface::h, SDL_Surface::map, SDL_Palette::ncolors, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_Color::r, SDL_Surface::refcount, SDL_AllocBlitMap(), SDL_AllocFormat, SDL_AllocPalette, SDL_BLENDMODE_BLEND, SDL_CalculatePitch(), SDL_calloc(), SDL_FreePalette, SDL_FreeSurface(), SDL_ISPIXELFORMAT_INDEXED, SDL_malloc, SDL_MasksToPixelFormatEnum, SDL_memset, SDL_OutOfMemory, SDL_PIXELFORMAT_UNKNOWN, SDL_SetClipRect(), SDL_SetError, SDL_SetSurfaceBlendMode(), SDL_SetSurfacePalette(), void, and SDL_Surface::w.

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceFrom().

37 {
38  SDL_Surface *surface;
39  Uint32 format;
40 
41  /* The flags are no longer used, make the compiler happy */
42  (void)flags;
43 
44  /* Get the pixel format */
45  format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
46  if (format == SDL_PIXELFORMAT_UNKNOWN) {
47  SDL_SetError("Unknown pixel format");
48  return NULL;
49  }
50 
51  /* Allocate the surface */
52  surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
53  if (surface == NULL) {
55  return NULL;
56  }
57 
58  surface->format = SDL_AllocFormat(format);
59  if (!surface->format) {
60  SDL_FreeSurface(surface);
61  return NULL;
62  }
63  surface->w = width;
64  surface->h = height;
65  surface->pitch = SDL_CalculatePitch(surface);
66  SDL_SetClipRect(surface, NULL);
67 
68  if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
69  SDL_Palette *palette =
70  SDL_AllocPalette((1 << surface->format->BitsPerPixel));
71  if (!palette) {
72  SDL_FreeSurface(surface);
73  return NULL;
74  }
75  if (palette->ncolors == 2) {
76  /* Create a black and white bitmap palette */
77  palette->colors[0].r = 0xFF;
78  palette->colors[0].g = 0xFF;
79  palette->colors[0].b = 0xFF;
80  palette->colors[1].r = 0x00;
81  palette->colors[1].g = 0x00;
82  palette->colors[1].b = 0x00;
83  }
84  SDL_SetSurfacePalette(surface, palette);
85  SDL_FreePalette(palette);
86  }
87 
88  /* Get the pixels */
89  if (surface->w && surface->h) {
90  surface->pixels = SDL_malloc(surface->h * surface->pitch);
91  if (!surface->pixels) {
92  SDL_FreeSurface(surface);
94  return NULL;
95  }
96  /* This is important for bitmaps */
97  SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
98  }
99 
100  /* Allocate an empty mapping */
101  surface->map = SDL_AllocBlitMap();
102  if (!surface->map) {
103  SDL_FreeSurface(surface);
104  return NULL;
105  }
106 
107  /* By default surface with an alpha mask are set up for blending */
108  if (Amask) {
110  }
111 
112  /* The surface is ready to go */
113  surface->refcount = 1;
114  return surface;
115 }
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:457
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
Uint8 g
Definition: SDL_pixels.h:282
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:133
#define SDL_MasksToPixelFormatEnum
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:391
Uint8 b
Definition: SDL_pixels.h:283
#define SDL_AllocFormat
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * SDL_calloc(size_t nmemb, size_t size)
Uint8 r
Definition: SDL_pixels.h:281
void * pixels
Definition: SDL_surface.h:75
Uint8 BitsPerPixel
Definition: SDL_pixels.h:303
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:142
#define SDL_AllocPalette
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1116
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_Color * colors
Definition: SDL_pixels.h:291
SDL_PixelFormat * format
Definition: SDL_surface.h:72
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1565
#define SDL_FreePalette
#define SDL_SetError
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
SDL_BlitMap * SDL_AllocBlitMap(void)
Definition: SDL_pixels.c:954
GLbitfield flags
int SDL_CalculatePitch(SDL_Surface *surface)
Definition: SDL_pixels.c:748
#define SDL_malloc
#define SDL_memset
SDL_Surface* SDL_CreateRGBSurfaceFrom ( void pixels,
int  width,
int  height,
int  depth,
int  pitch,
Uint32  Rmask,
Uint32  Gmask,
Uint32  Bmask,
Uint32  Amask 
)

Definition at line 121 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::h, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_CreateRGBSurface(), SDL_PREALLOC, SDL_SetClipRect(), and SDL_Surface::w.

125 {
126  SDL_Surface *surface;
127 
128  surface =
129  SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
130  if (surface != NULL) {
131  surface->flags |= SDL_PREALLOC;
132  surface->pixels = pixels;
133  surface->w = width;
134  surface->h = height;
135  surface->pitch = pitch;
136  SDL_SetClipRect(surface, NULL);
137  }
138  return surface;
139 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:457
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1565
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
Uint32 flags
Definition: SDL_surface.h:71
void * pixels
Definition: SDL_surface.h:75
#define NULL
Definition: begin_code.h:143
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1565
SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:34
#define SDL_PREALLOC
Definition: SDL_surface.h:53
int SDL_FillRect ( SDL_Surface dst,
const SDL_Rect rect,
Uint32  color 
)

Performs a fast fill of the given rectangle with color.

If rect is NULL, the whole surface will be filled with color.

The color should be a pixel of the format used by the surface, and can be generated by the SDL_MapRGB() function.

Returns
0 on success, or -1 on error.

Definition at line 237 of file SDL_fillrect.c.

References SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::BytesPerPixel, SDL_Surface::clip_rect, SDL_Surface::format, SDL_Rect::h, SDL_Surface::pitch, SDL_Surface::pixels, SDL_FillRect1(), SDL_FillRect2(), SDL_FillRect3(), SDL_FillRect4(), SDL_HasSSE, SDL_IntersectRect, SDL_RectEmpty(), SDL_SetError, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_FillRects().

238 {
239  SDL_Rect clipped;
240  Uint8 *pixels;
241 
242  if (!dst) {
243  return SDL_SetError("Passed NULL destination surface");
244  }
245 
246  /* This function doesn't work on surfaces < 8 bpp */
247  if (dst->format->BitsPerPixel < 8) {
248  return SDL_SetError("SDL_FillRect(): Unsupported surface format");
249  }
250 
251  /* If 'rect' == NULL, then fill the whole surface */
252  if (rect) {
253  /* Perform clipping */
254  if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
255  return 0;
256  }
257  rect = &clipped;
258  } else {
259  rect = &dst->clip_rect;
260  /* Don't attempt to fill if the surface's clip_rect is empty */
261  if (SDL_RectEmpty(rect)) {
262  return 0;
263  }
264  }
265 
266  /* Perform software fill */
267  if (!dst->pixels) {
268  return SDL_SetError("SDL_FillRect(): You must lock the surface");
269  }
270 
271  pixels = (Uint8 *) dst->pixels + rect->y * dst->pitch +
272  rect->x * dst->format->BytesPerPixel;
273 
274  switch (dst->format->BytesPerPixel) {
275  case 1:
276  {
277  color |= (color << 8);
278  color |= (color << 16);
279 #ifdef __SSE__
280  if (SDL_HasSSE()) {
281  SDL_FillRect1SSE(pixels, dst->pitch, color, rect->w, rect->h);
282  break;
283  }
284 #endif
285  SDL_FillRect1(pixels, dst->pitch, color, rect->w, rect->h);
286  break;
287  }
288 
289  case 2:
290  {
291  color |= (color << 16);
292 #ifdef __SSE__
293  if (SDL_HasSSE()) {
294  SDL_FillRect2SSE(pixels, dst->pitch, color, rect->w, rect->h);
295  break;
296  }
297 #endif
298  SDL_FillRect2(pixels, dst->pitch, color, rect->w, rect->h);
299  break;
300  }
301 
302  case 3:
303  /* 24-bit RGB is a slow path, at least for now. */
304  {
305  SDL_FillRect3(pixels, dst->pitch, color, rect->w, rect->h);
306  break;
307  }
308 
309  case 4:
310  {
311 #ifdef __SSE__
312  if (SDL_HasSSE()) {
313  SDL_FillRect4SSE(pixels, dst->pitch, color, rect->w, rect->h);
314  break;
315  }
316 #endif
317  SDL_FillRect4(pixels, dst->pitch, color, rect->w, rect->h);
318  break;
319  }
320  }
321 
322  /* We're done! */
323  return 0;
324 }
Uint8 BytesPerPixel
Definition: SDL_pixels.h:304
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1565
#define SDL_IntersectRect
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:82
static void SDL_FillRect3(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:197
void * pixels
Definition: SDL_surface.h:75
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
Uint8 BitsPerPixel
Definition: SDL_pixels.h:303
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
static void SDL_FillRect2(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:173
int h
Definition: SDL_rect.h:67
GLuint color
static void SDL_FillRect4(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:225
static void SDL_FillRect1(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:134
#define SDL_HasSSE
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
int SDL_FillRects ( SDL_Surface dst,
const SDL_Rect rects,
int  count,
Uint32  color 
)

Definition at line 327 of file SDL_fillrect.c.

References i, SDL_FillRect(), and SDL_SetError.

329 {
330  int i;
331  int status = 0;
332 
333  if (!rects) {
334  return SDL_SetError("SDL_FillRects() passed NULL rects");
335  }
336 
337  for (i = 0; i < count; ++i) {
338  status += SDL_FillRect(dst, &rects[i], color);
339  }
340  return status;
341 }
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1564
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:42
int SDL_FillRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
Definition: SDL_fillrect.c:237
#define SDL_SetError
GLuint color
void SDL_FreeSurface ( SDL_Surface surface)

Definition at line 1116 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::format, SDL_Surface::locked, SDL_Surface::map, NULL, SDL_Surface::pixels, SDL_Surface::refcount, SDL_DONTFREE, SDL_free(), SDL_FreeBlitMap(), SDL_FreeFormat, SDL_PREALLOC, SDL_RLEACCEL, SDL_SetSurfacePalette(), SDL_UnlockSurface(), and SDL_UnRLESurface().

Referenced by SDL_CreateRGBSurface().

1117 {
1118  if (surface == NULL) {
1119  return;
1120  }
1121  if (surface->flags & SDL_DONTFREE) {
1122  return;
1123  }
1124  if (--surface->refcount > 0) {
1125  return;
1126  }
1127  while (surface->locked > 0) {
1128  SDL_UnlockSurface(surface);
1129  }
1130  if (surface->flags & SDL_RLEACCEL) {
1131  SDL_UnRLESurface(surface, 0);
1132  }
1133  if (surface->format) {
1134  SDL_SetSurfacePalette(surface, NULL);
1135  SDL_FreeFormat(surface->format);
1136  surface->format = NULL;
1137  }
1138  if (surface->map != NULL) {
1139  SDL_FreeBlitMap(surface->map);
1140  surface->map = NULL;
1141  }
1142  if (!(surface->flags & SDL_PREALLOC)) {
1143  SDL_free(surface->pixels);
1144  }
1145  SDL_free(surface);
1146 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:824
#define SDL_DONTFREE
Definition: SDL_surface.h:55
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 flags
Definition: SDL_surface.h:71
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:142
void SDL_free(void *mem)
#define SDL_FreeFormat
#define NULL
Definition: begin_code.h:143
SDL_PixelFormat * format
Definition: SDL_surface.h:72
void SDL_FreeBlitMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1079
#define SDL_PREALLOC
Definition: SDL_surface.h:53
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
void SDL_GetClipRect ( SDL_Surface surface,
SDL_Rect rect 
)

Gets the clipping rectangle for the destination surface in a blit.

rect must be a pointer to a valid rectangle which will be filled with the correct values.

Definition at line 481 of file SDL_surface.c.

References SDL_Surface::clip_rect.

482 {
483  if (surface && rect) {
484  *rect = surface->clip_rect;
485  }
486 }
SDL_Rect clip_rect
Definition: SDL_surface.h:85
int SDL_GetColorKey ( SDL_Surface surface,
Uint32 key 
)

Gets the color key (transparent pixel) in a blittable surface.

Parameters
surfaceThe surface to update
keyA pointer filled in with the transparent pixel in the native surface format
Returns
0 on success, or -1 if the surface is not valid or colorkey is not enabled.

Definition at line 222 of file SDL_surface.c.

References SDL_BlitInfo::colorkey, SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, and SDL_COPY_COLORKEY.

223 {
224  if (!surface) {
225  return -1;
226  }
227 
228  if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
229  return -1;
230  }
231 
232  if (key) {
233  *key = surface->map->info.colorkey;
234  }
235  return 0;
236 }
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
Uint32 colorkey
Definition: SDL_blit.h:69
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_BlitInfo info
Definition: SDL_blit.h:91
int SDL_GetSurfaceAlphaMod ( SDL_Surface surface,
Uint8 alpha 
)

Get the additional alpha value used in blit operations.

Parameters
surfaceThe surface to query.
alphaA pointer filled in with the current alpha value.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_SetSurfaceAlphaMod()

Definition at line 378 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_BlitMap::info, and SDL_Surface::map.

379 {
380  if (!surface) {
381  return -1;
382  }
383 
384  if (alpha) {
385  *alpha = surface->map->info.a;
386  }
387  return 0;
388 }
GLfloat GLfloat GLfloat alpha
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_BlitInfo info
Definition: SDL_blit.h:91
Uint8 a
Definition: SDL_blit.h:70
int SDL_GetSurfaceBlendMode ( SDL_Surface surface,
SDL_BlendMode blendMode 
)

Get the blend mode used for blit operations.

Parameters
surfaceThe surface to query.
blendModeA pointer filled in with the current blend mode.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_SetSurfaceBlendMode()

Definition at line 428 of file SDL_surface.c.

References SDL_Surface::map, SDL_BLENDMODE_ADD, SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD, SDL_BLENDMODE_NONE, SDL_COPY_ADD, SDL_COPY_BLEND, and SDL_COPY_MOD.

429 {
430  if (!surface) {
431  return -1;
432  }
433 
434  if (!blendMode) {
435  return 0;
436  }
437 
438  switch (surface->map->
439  info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
440  case SDL_COPY_BLEND:
442  break;
443  case SDL_COPY_ADD:
445  break;
446  case SDL_COPY_MOD:
448  break;
449  default:
451  break;
452  }
453  return 0;
454 }
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
int SDL_GetSurfaceColorMod ( SDL_Surface surface,
Uint8 r,
Uint8 g,
Uint8 b 
)

Get the additional color value used in blit operations.

Parameters
surfaceThe surface to query.
rA pointer filled in with the current red color value.
gA pointer filled in with the current green color value.
bA pointer filled in with the current blue color value.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_SetSurfaceColorMod()

Definition at line 336 of file SDL_surface.c.

References SDL_BlitInfo::b, SDL_BlitInfo::g, SDL_BlitMap::info, SDL_Surface::map, and SDL_BlitInfo::r.

337 {
338  if (!surface) {
339  return -1;
340  }
341 
342  if (r) {
343  *r = surface->map->info.r;
344  }
345  if (g) {
346  *g = surface->map->info.g;
347  }
348  if (b) {
349  *b = surface->map->info.b;
350  }
351  return 0;
352 }
Uint8 r
Definition: SDL_blit.h:70
Uint8 b
Definition: SDL_blit.h:70
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
Uint8 g
Definition: SDL_blit.h:70
GLboolean GLboolean g
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:91
SDL_Surface* SDL_LoadBMP_RW ( SDL_RWops src,
int  freesrc 
)

Load a surface from a seekable SDL data stream (memory or file).

If freesrc is non-zero, the stream will be closed after being read.

The new surface should be freed with SDL_FreeSurface().

Returns
the new surface, or NULL if there was an error.

Definition at line 81 of file SDL_bmp.c.

References SDL_Color::a, SDL_Color::b, BI_BITFIELDS, BI_RGB, colors, SDL_Palette::colors, CorrectAlphaChannel(), done, SDL_Surface::format, SDL_Color::g, SDL_Surface::h, i, SDL_Palette::ncolors, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_Color::r, RW_SEEK_CUR, RW_SEEK_SET, SDL_ALPHA_OPAQUE, SDL_assert, SDL_ClearError, SDL_CreateRGBSurface, SDL_EFREAD, SDL_EFSEEK, SDL_Error, SDL_FALSE, SDL_FreeSurface, SDL_GetError, SDL_OutOfMemory, SDL_ReadLE16, SDL_ReadLE32, SDL_realloc, SDL_RWclose, SDL_RWread, SDL_RWseek, SDL_RWtell, SDL_SetError, SDL_strcmp, SDL_strncmp, SDL_Swap16(), SDL_Swap32(), SDL_TRUE, and SDL_Surface::w.

82 {
83  SDL_bool was_error;
84  Sint64 fp_offset = 0;
85  int bmpPitch;
86  int i, pad;
87  SDL_Surface *surface;
88  Uint32 Rmask = 0;
89  Uint32 Gmask = 0;
90  Uint32 Bmask = 0;
91  Uint32 Amask = 0;
92  SDL_Palette *palette;
93  Uint8 *bits;
94  Uint8 *top, *end;
95  SDL_bool topDown;
96  int ExpandBMP;
97  SDL_bool haveRGBMasks = SDL_FALSE;
98  SDL_bool haveAlphaMask = SDL_FALSE;
99  SDL_bool correctAlpha = SDL_FALSE;
100 
101  /* The Win32 BMP file header (14 bytes) */
102  char magic[2];
103  /* Uint32 bfSize = 0; */
104  /* Uint16 bfReserved1 = 0; */
105  /* Uint16 bfReserved2 = 0; */
106  Uint32 bfOffBits = 0;
107 
108  /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
109  Uint32 biSize = 0;
110  Sint32 biWidth = 0;
111  Sint32 biHeight = 0;
112  /* Uint16 biPlanes = 0; */
113  Uint16 biBitCount = 0;
114  Uint32 biCompression = 0;
115  /* Uint32 biSizeImage = 0; */
116  /* Sint32 biXPelsPerMeter = 0; */
117  /* Sint32 biYPelsPerMeter = 0; */
118  Uint32 biClrUsed = 0;
119  /* Uint32 biClrImportant = 0; */
120 
121  /* Make sure we are passed a valid data source */
122  surface = NULL;
123  was_error = SDL_FALSE;
124  if (src == NULL) {
125  was_error = SDL_TRUE;
126  goto done;
127  }
128 
129  /* Read in the BMP file header */
130  fp_offset = SDL_RWtell(src);
131  SDL_ClearError();
132  if (SDL_RWread(src, magic, 1, 2) != 2) {
134  was_error = SDL_TRUE;
135  goto done;
136  }
137  if (SDL_strncmp(magic, "BM", 2) != 0) {
138  SDL_SetError("File is not a Windows BMP file");
139  was_error = SDL_TRUE;
140  goto done;
141  }
142  /* bfSize = */ SDL_ReadLE32(src);
143  /* bfReserved1 = */ SDL_ReadLE16(src);
144  /* bfReserved2 = */ SDL_ReadLE16(src);
145  bfOffBits = SDL_ReadLE32(src);
146 
147  /* Read the Win32 BITMAPINFOHEADER */
148  biSize = SDL_ReadLE32(src);
149  if (biSize == 12) { /* really old BITMAPCOREHEADER */
150  biWidth = (Uint32) SDL_ReadLE16(src);
151  biHeight = (Uint32) SDL_ReadLE16(src);
152  /* biPlanes = */ SDL_ReadLE16(src);
153  biBitCount = SDL_ReadLE16(src);
154  biCompression = BI_RGB;
155  } else if (biSize >= 40) { /* some version of BITMAPINFOHEADER */
156  Uint32 headerSize;
157  biWidth = SDL_ReadLE32(src);
158  biHeight = SDL_ReadLE32(src);
159  /* biPlanes = */ SDL_ReadLE16(src);
160  biBitCount = SDL_ReadLE16(src);
161  biCompression = SDL_ReadLE32(src);
162  /* biSizeImage = */ SDL_ReadLE32(src);
163  /* biXPelsPerMeter = */ SDL_ReadLE32(src);
164  /* biYPelsPerMeter = */ SDL_ReadLE32(src);
165  biClrUsed = SDL_ReadLE32(src);
166  /* biClrImportant = */ SDL_ReadLE32(src);
167 
168  /* 64 == BITMAPCOREHEADER2, an incompatible OS/2 2.x extension. Skip this stuff for now. */
169  if (biSize == 64) {
170  /* ignore these extra fields. */
171  if (biCompression == BI_BITFIELDS) {
172  /* this value is actually huffman compression in this variant. */
173  SDL_SetError("Compressed BMP files not supported");
174  was_error = SDL_TRUE;
175  goto done;
176  }
177  } else {
178  /* This is complicated. If compression is BI_BITFIELDS, then
179  we have 3 DWORDS that specify the RGB masks. This is either
180  stored here in an BITMAPV2INFOHEADER (which only differs in
181  that it adds these RGB masks) and biSize >= 52, or we've got
182  these masks stored in the exact same place, but strictly
183  speaking, this is the bmiColors field in BITMAPINFO immediately
184  following the legacy v1 info header, just past biSize. */
185  if (biCompression == BI_BITFIELDS) {
186  haveRGBMasks = SDL_TRUE;
187  Rmask = SDL_ReadLE32(src);
188  Gmask = SDL_ReadLE32(src);
189  Bmask = SDL_ReadLE32(src);
190 
191  /* ...v3 adds an alpha mask. */
192  if (biSize >= 56) { /* BITMAPV3INFOHEADER; adds alpha mask */
193  haveAlphaMask = SDL_TRUE;
194  Amask = SDL_ReadLE32(src);
195  }
196  } else {
197  /* the mask fields are ignored for v2+ headers if not BI_BITFIELD. */
198  if (biSize >= 52) { /* BITMAPV2INFOHEADER; adds RGB masks */
199  /*Rmask = */ SDL_ReadLE32(src);
200  /*Gmask = */ SDL_ReadLE32(src);
201  /*Bmask = */ SDL_ReadLE32(src);
202  }
203  if (biSize >= 56) { /* BITMAPV3INFOHEADER; adds alpha mask */
204  /*Amask = */ SDL_ReadLE32(src);
205  }
206  }
207 
208  /* Insert other fields here; Wikipedia and MSDN say we're up to
209  v5 of this header, but we ignore those for now (they add gamma,
210  color spaces, etc). Ignoring the weird OS/2 2.x format, we
211  currently parse up to v3 correctly (hopefully!). */
212  }
213 
214  /* skip any header bytes we didn't handle... */
215  headerSize = (Uint32) (SDL_RWtell(src) - (fp_offset + 14));
216  if (biSize > headerSize) {
217  SDL_RWseek(src, (biSize - headerSize), RW_SEEK_CUR);
218  }
219  }
220  if (biHeight < 0) {
221  topDown = SDL_TRUE;
222  biHeight = -biHeight;
223  } else {
224  topDown = SDL_FALSE;
225  }
226 
227  /* Check for read error */
228  if (SDL_strcmp(SDL_GetError(), "") != 0) {
229  was_error = SDL_TRUE;
230  goto done;
231  }
232 
233  /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
234  switch (biBitCount) {
235  case 1:
236  case 4:
237  ExpandBMP = biBitCount;
238  biBitCount = 8;
239  break;
240  default:
241  ExpandBMP = 0;
242  break;
243  }
244 
245  /* We don't support any BMP compression right now */
246  switch (biCompression) {
247  case BI_RGB:
248  /* If there are no masks, use the defaults */
249  SDL_assert(!haveRGBMasks);
250  SDL_assert(!haveAlphaMask);
251  /* Default values for the BMP format */
252  switch (biBitCount) {
253  case 15:
254  case 16:
255  Rmask = 0x7C00;
256  Gmask = 0x03E0;
257  Bmask = 0x001F;
258  break;
259  case 24:
260 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
261  Rmask = 0x000000FF;
262  Gmask = 0x0000FF00;
263  Bmask = 0x00FF0000;
264 #else
265  Rmask = 0x00FF0000;
266  Gmask = 0x0000FF00;
267  Bmask = 0x000000FF;
268 #endif
269  break;
270  case 32:
271  /* We don't know if this has alpha channel or not */
272  correctAlpha = SDL_TRUE;
273  Amask = 0xFF000000;
274  Rmask = 0x00FF0000;
275  Gmask = 0x0000FF00;
276  Bmask = 0x000000FF;
277  break;
278  default:
279  break;
280  }
281  break;
282 
283  case BI_BITFIELDS:
284  break; /* we handled this in the info header. */
285 
286  default:
287  SDL_SetError("Compressed BMP files not supported");
288  was_error = SDL_TRUE;
289  goto done;
290  }
291 
292  /* Create a compatible surface, note that the colors are RGB ordered */
293  surface =
294  SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
295  Bmask, Amask);
296  if (surface == NULL) {
297  was_error = SDL_TRUE;
298  goto done;
299  }
300 
301  /* Load the palette, if any */
302  palette = (surface->format)->palette;
303  if (palette) {
304  SDL_assert(biBitCount <= 8);
305  if (biClrUsed == 0) {
306  biClrUsed = 1 << biBitCount;
307  }
308  if ((int) biClrUsed > palette->ncolors) {
309  SDL_Color *colors;
310  int ncolors = biClrUsed;
311  colors =
312  (SDL_Color *) SDL_realloc(palette->colors,
313  ncolors *
314  sizeof(*palette->colors));
315  if (!colors) {
316  SDL_OutOfMemory();
317  was_error = SDL_TRUE;
318  goto done;
319  }
320  palette->ncolors = ncolors;
321  palette->colors = colors;
322  } else if ((int) biClrUsed < palette->ncolors) {
323  palette->ncolors = biClrUsed;
324  }
325  if (biSize == 12) {
326  for (i = 0; i < (int) biClrUsed; ++i) {
327  SDL_RWread(src, &palette->colors[i].b, 1, 1);
328  SDL_RWread(src, &palette->colors[i].g, 1, 1);
329  SDL_RWread(src, &palette->colors[i].r, 1, 1);
330  palette->colors[i].a = SDL_ALPHA_OPAQUE;
331  }
332  } else {
333  for (i = 0; i < (int) biClrUsed; ++i) {
334  SDL_RWread(src, &palette->colors[i].b, 1, 1);
335  SDL_RWread(src, &palette->colors[i].g, 1, 1);
336  SDL_RWread(src, &palette->colors[i].r, 1, 1);
337  SDL_RWread(src, &palette->colors[i].a, 1, 1);
338 
339  /* According to Microsoft documentation, the fourth element
340  is reserved and must be zero, so we shouldn't treat it as
341  alpha.
342  */
343  palette->colors[i].a = SDL_ALPHA_OPAQUE;
344  }
345  }
346  }
347 
348  /* Read the surface pixels. Note that the bmp image is upside down */
349  if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
351  was_error = SDL_TRUE;
352  goto done;
353  }
354  top = (Uint8 *)surface->pixels;
355  end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
356  switch (ExpandBMP) {
357  case 1:
358  bmpPitch = (biWidth + 7) >> 3;
359  pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
360  break;
361  case 4:
362  bmpPitch = (biWidth + 1) >> 1;
363  pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
364  break;
365  default:
366  pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
367  break;
368  }
369  if (topDown) {
370  bits = top;
371  } else {
372  bits = end - surface->pitch;
373  }
374  while (bits >= top && bits < end) {
375  switch (ExpandBMP) {
376  case 1:
377  case 4:{
378  Uint8 pixel = 0;
379  int shift = (8 - ExpandBMP);
380  for (i = 0; i < surface->w; ++i) {
381  if (i % (8 / ExpandBMP) == 0) {
382  if (!SDL_RWread(src, &pixel, 1, 1)) {
383  SDL_SetError("Error reading from BMP");
384  was_error = SDL_TRUE;
385  goto done;
386  }
387  }
388  *(bits + i) = (pixel >> shift);
389  pixel <<= ExpandBMP;
390  }
391  }
392  break;
393 
394  default:
395  if (SDL_RWread(src, bits, 1, surface->pitch)
396  != surface->pitch) {
398  was_error = SDL_TRUE;
399  goto done;
400  }
401 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
402  /* Byte-swap the pixels if needed. Note that the 24bpp
403  case has already been taken care of above. */
404  switch (biBitCount) {
405  case 15:
406  case 16:{
407  Uint16 *pix = (Uint16 *) bits;
408  for (i = 0; i < surface->w; i++)
409  pix[i] = SDL_Swap16(pix[i]);
410  break;
411  }
412 
413  case 32:{
414  Uint32 *pix = (Uint32 *) bits;
415  for (i = 0; i < surface->w; i++)
416  pix[i] = SDL_Swap32(pix[i]);
417  break;
418  }
419  }
420 #endif
421  break;
422  }
423  /* Skip padding bytes, ugh */
424  if (pad) {
425  Uint8 padbyte;
426  for (i = 0; i < pad; ++i) {
427  SDL_RWread(src, &padbyte, 1, 1);
428  }
429  }
430  if (topDown) {
431  bits += surface->pitch;
432  } else {
433  bits -= surface->pitch;
434  }
435  }
436  if (correctAlpha) {
437  CorrectAlphaChannel(surface);
438  }
439  done:
440  if (was_error) {
441  if (src) {
442  SDL_RWseek(src, fp_offset, RW_SEEK_SET);
443  }
444  SDL_FreeSurface(surface);
445  surface = NULL;
446  }
447  if (freesrc && src) {
448  SDL_RWclose(src);
449  }
450  return (surface);
451 }
#define BI_RGB
Definition: SDL_bmp.c:44
#define SDL_ClearError
#define SDL_GetError
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
Uint8 g
Definition: SDL_pixels.h:282
GLuint GLuint end
Definition: SDL_opengl.h:1564
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_ReadLE32
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
Uint8 b
Definition: SDL_pixels.h:283
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
#define SDL_realloc
static void CorrectAlphaChannel(SDL_Surface *surface)
Definition: SDL_bmp.c:51
GLdouble GLdouble GLdouble GLdouble top
#define SDL_strncmp
#define SDL_Error
#define SDL_RWseek(ctx, offset, whence)
Definition: SDL_rwops.h:185
#define SDL_ReadLE16
SDL_FORCE_INLINE Uint32 SDL_Swap32(Uint32 x)
Definition: SDL_endian.h:141
SDL_bool
Definition: SDL_stdinc.h:126
Uint8 r
Definition: SDL_pixels.h:281
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
Uint8 a
Definition: SDL_pixels.h:284
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
int done
Definition: checkkeys.c:28
SDL_FORCE_INLINE Uint16 SDL_Swap16(Uint16 x)
Definition: SDL_endian.h:101
int32_t Sint32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:151
#define BI_BITFIELDS
Definition: SDL_bmp.c:47
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:42
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_Color * colors
Definition: SDL_pixels.h:291
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define SDL_SetError
#define SDL_CreateRGBSurface
#define RW_SEEK_SET
Definition: SDL_rwops.h:174
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:147
#define SDL_strcmp
#define RW_SEEK_CUR
Definition: SDL_rwops.h:175
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:160
static int colors[7]
Definition: testgesture.c:40
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:45
#define SDL_RWtell(ctx)
Definition: SDL_rwops.h:186
int SDL_LockSurface ( SDL_Surface surface)

Sets up a surface for directly accessing the pixels.

Between calls to SDL_LockSurface() / SDL_UnlockSurface(), you can write to and read from surface->pixels, using the pixel format stored in surface->format. Once you are done accessing the surface, you should use SDL_UnlockSurface() to release it.

Not all surfaces require locking. If SDL_MUSTLOCK(surface) evaluates to 0, then you can read and write to the surface at any time, and the pixel format of the surface will not change.

No operating system or library calls should be made between lock/unlock pairs, as critical system locks may be held during this time.

SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.

See also
SDL_UnlockSurface()

Definition at line 803 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::locked, SDL_RLEACCEL, and SDL_UnRLESurface().

Referenced by SDL_ConvertColorkeyToAlpha().

804 {
805  if (!surface->locked) {
806  /* Perform the lock */
807  if (surface->flags & SDL_RLEACCEL) {
808  SDL_UnRLESurface(surface, 1);
809  surface->flags |= SDL_RLEACCEL; /* save accel'd state */
810  }
811  }
812 
813  /* Increment the surface lock count, for recursive locks */
814  ++surface->locked;
815 
816  /* Ready to go.. */
817  return (0);
818 }
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 flags
Definition: SDL_surface.h:71
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
int SDL_LowerBlit ( SDL_Surface src,
SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is a semi-private blit function and it performs low-level surface blitting only.

Definition at line 500 of file SDL_surface.c.

References SDL_BlitMap::blit, SDL_BlitMap::dst, SDL_BlitMap::dst_palette_version, SDL_Surface::format, SDL_Surface::map, SDL_PixelFormat::palette, SDL_MapSurface(), SDL_BlitMap::src_palette_version, and SDL_Palette::version.

Referenced by SDL_ConvertPixels(), SDL_ConvertSurface(), SDL_LowerBlitScaled(), and SDL_UpperBlit().

502 {
503  /* Check to make sure the blit mapping is valid */
504  if ((src->map->dst != dst) ||
505  (dst->format->palette &&
506  src->map->dst_palette_version != dst->format->palette->version) ||
507  (src->format->palette &&
508  src->map->src_palette_version != src->format->palette->version)) {
509  if (SDL_MapSurface(src, dst) < 0) {
510  return (-1);
511  }
512  /* just here for debugging */
513 /* printf */
514 /* ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
515 /* src, dst->flags, src->map->info.flags, dst, dst->flags, */
516 /* dst->map->info.flags, src->map->blit); */
517  }
518  return (src->map->blit(src, srcrect, dst, dstrect));
519 }
Uint32 version
Definition: SDL_pixels.h:292
SDL_blit blit
Definition: SDL_blit.h:89
Uint32 dst_palette_version
Definition: SDL_blit.h:95
Uint32 src_palette_version
Definition: SDL_blit.h:96
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
int SDL_MapSurface(SDL_Surface *src, SDL_Surface *dst)
Definition: SDL_pixels.c:993
SDL_Surface * dst
Definition: SDL_blit.h:87
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_Palette * palette
Definition: SDL_pixels.h:302
int SDL_LowerBlitScaled ( SDL_Surface src,
SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is a semi-private blit function and it performs low-level surface scaled blitting only.

Definition at line 776 of file SDL_surface.c.

References SDL_BlitInfo::flags, SDL_Surface::format, SDL_PixelFormat::format, SDL_BlitMap::info, SDL_Surface::map, SDL_COPY_ADD, SDL_COPY_BLEND, SDL_COPY_COLORKEY, SDL_COPY_MOD, SDL_COPY_MODULATE_ALPHA, SDL_COPY_MODULATE_COLOR, SDL_COPY_NEAREST, SDL_InvalidateMap(), SDL_ISPIXELFORMAT_INDEXED, SDL_LowerBlit(), and SDL_SoftStretch.

Referenced by SDL_UpperBlitScaled().

778 {
779  static const Uint32 complex_copy_flags = (
783  );
784 
785  if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
786  src->map->info.flags |= SDL_COPY_NEAREST;
787  SDL_InvalidateMap(src->map);
788  }
789 
790  if ( !(src->map->info.flags & complex_copy_flags) &&
791  src->format->format == dst->format->format &&
793  return SDL_SoftStretch( src, srcrect, dst, dstrect );
794  } else {
795  return SDL_LowerBlit( src, srcrect, dst, dstrect );
796  }
797 }
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
#define SDL_SoftStretch
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:133
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:500
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
#define SDL_COPY_NEAREST
Definition: SDL_blit.h:40
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:91
int SDL_SaveBMP_RW ( SDL_Surface surface,
SDL_RWops dst,
int  freedst 
)

Save a surface to a seekable SDL data stream (memory or file).

If freedst is non-zero, the stream will be closed after being written.

Returns
0 if successful or -1 if there was an error.

Definition at line 454 of file SDL_bmp.c.

References SDL_PixelFormat::Amask, BI_RGB, SDL_PixelFormat::BitsPerPixel, SDL_PixelFormat::Bmask, SDL_PixelFormat::BytesPerPixel, colors, SDL_Palette::colors, SDL_BlitInfo::flags, SDL_Surface::format, SDL_PixelFormat::Gmask, SDL_Surface::h, i, SDL_BlitMap::info, SDL_Surface::map, SDL_Palette::ncolors, NULL, SDL_PixelFormat::palette, SDL_Surface::pitch, SDL_Surface::pixels, SDL_PixelFormat::Rmask, RW_SEEK_SET, SDL_BYTEORDER, SDL_ClearError, SDL_ConvertSurface, SDL_COPY_COLORKEY, SDL_EFSEEK, SDL_EFWRITE, SDL_Error, SDL_FALSE, SDL_FreeSurface, SDL_GetError, SDL_InitFormat(), SDL_LIL_ENDIAN, SDL_LockSurface, SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_BGR24, SDL_PIXELFORMAT_BGRA8888, SDL_RWclose, SDL_RWseek, SDL_RWtell, SDL_RWwrite, SDL_SetError, SDL_strcmp, SDL_TRUE, SDL_UnlockSurface, SDL_WriteLE16, SDL_WriteLE32, and SDL_Surface::w.

455 {
456  Sint64 fp_offset;
457  int i, pad;
458  SDL_Surface *surface;
459  Uint8 *bits;
460 
461  /* The Win32 BMP file header (14 bytes) */
462  char magic[2] = { 'B', 'M' };
463  Uint32 bfSize;
464  Uint16 bfReserved1;
465  Uint16 bfReserved2;
466  Uint32 bfOffBits;
467 
468  /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
469  Uint32 biSize;
470  Sint32 biWidth;
471  Sint32 biHeight;
472  Uint16 biPlanes;
473  Uint16 biBitCount;
474  Uint32 biCompression;
475  Uint32 biSizeImage;
476  Sint32 biXPelsPerMeter;
477  Sint32 biYPelsPerMeter;
478  Uint32 biClrUsed;
479  Uint32 biClrImportant;
480 
481  /* Make sure we have somewhere to save */
482  surface = NULL;
483  if (dst) {
484  SDL_bool save32bit = SDL_FALSE;
485 #ifdef SAVE_32BIT_BMP
486  /* We can save alpha information in a 32-bit BMP */
487  if (saveme->map->info.flags & SDL_COPY_COLORKEY ||
488  saveme->format->Amask) {
489  save32bit = SDL_TRUE;
490  }
491 #endif /* SAVE_32BIT_BMP */
492 
493  if (saveme->format->palette && !save32bit) {
494  if (saveme->format->BitsPerPixel == 8) {
495  surface = saveme;
496  } else {
497  SDL_SetError("%d bpp BMP files not supported",
498  saveme->format->BitsPerPixel);
499  }
500  } else if ((saveme->format->BitsPerPixel == 24) &&
502  (saveme->format->Rmask == 0x00FF0000) &&
503  (saveme->format->Gmask == 0x0000FF00) &&
504  (saveme->format->Bmask == 0x000000FF)
505 #else
506  (saveme->format->Rmask == 0x000000FF) &&
507  (saveme->format->Gmask == 0x0000FF00) &&
508  (saveme->format->Bmask == 0x00FF0000)
509 #endif
510  ) {
511  surface = saveme;
512  } else {
514 
515  /* If the surface has a colorkey or alpha channel we'll save a
516  32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
517  if (save32bit) {
518  SDL_InitFormat(&format,
521 #else
523 #endif
524  );
525  } else {
527  }
528  surface = SDL_ConvertSurface(saveme, &format, 0);
529  if (!surface) {
530  SDL_SetError("Couldn't convert image to %d bpp",
531  format.BitsPerPixel);
532  }
533  }
534  } else {
535  /* Set no error here because it may overwrite a more useful message from
536  SDL_RWFromFile() if SDL_SaveBMP_RW() is called from SDL_SaveBMP(). */
537  return -1;
538  }
539 
540  if (surface && (SDL_LockSurface(surface) == 0)) {
541  const int bw = surface->w * surface->format->BytesPerPixel;
542 
543  /* Set the BMP file header values */
544  bfSize = 0; /* We'll write this when we're done */
545  bfReserved1 = 0;
546  bfReserved2 = 0;
547  bfOffBits = 0; /* We'll write this when we're done */
548 
549  /* Write the BMP file header values */
550  fp_offset = SDL_RWtell(dst);
551  SDL_ClearError();
552  SDL_RWwrite(dst, magic, 2, 1);
553  SDL_WriteLE32(dst, bfSize);
554  SDL_WriteLE16(dst, bfReserved1);
555  SDL_WriteLE16(dst, bfReserved2);
556  SDL_WriteLE32(dst, bfOffBits);
557 
558  /* Set the BMP info values */
559  biSize = 40;
560  biWidth = surface->w;
561  biHeight = surface->h;
562  biPlanes = 1;
563  biBitCount = surface->format->BitsPerPixel;
564  biCompression = BI_RGB;
565  biSizeImage = surface->h * surface->pitch;
566  biXPelsPerMeter = 0;
567  biYPelsPerMeter = 0;
568  if (surface->format->palette) {
569  biClrUsed = surface->format->palette->ncolors;
570  } else {
571  biClrUsed = 0;
572  }
573  biClrImportant = 0;
574 
575  /* Write the BMP info values */
576  SDL_WriteLE32(dst, biSize);
577  SDL_WriteLE32(dst, biWidth);
578  SDL_WriteLE32(dst, biHeight);
579  SDL_WriteLE16(dst, biPlanes);
580  SDL_WriteLE16(dst, biBitCount);
581  SDL_WriteLE32(dst, biCompression);
582  SDL_WriteLE32(dst, biSizeImage);
583  SDL_WriteLE32(dst, biXPelsPerMeter);
584  SDL_WriteLE32(dst, biYPelsPerMeter);
585  SDL_WriteLE32(dst, biClrUsed);
586  SDL_WriteLE32(dst, biClrImportant);
587 
588  /* Write the palette (in BGR color order) */
589  if (surface->format->palette) {
590  SDL_Color *colors;
591  int ncolors;
592 
593  colors = surface->format->palette->colors;
594  ncolors = surface->format->palette->ncolors;
595  for (i = 0; i < ncolors; ++i) {
596  SDL_RWwrite(dst, &colors[i].b, 1, 1);
597  SDL_RWwrite(dst, &colors[i].g, 1, 1);
598  SDL_RWwrite(dst, &colors[i].r, 1, 1);
599  SDL_RWwrite(dst, &colors[i].a, 1, 1);
600  }
601  }
602 
603  /* Write the bitmap offset */
604  bfOffBits = (Uint32)(SDL_RWtell(dst) - fp_offset);
605  if (SDL_RWseek(dst, fp_offset + 10, RW_SEEK_SET) < 0) {
607  }
608  SDL_WriteLE32(dst, bfOffBits);
609  if (SDL_RWseek(dst, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
611  }
612 
613  /* Write the bitmap image upside down */
614  bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
615  pad = ((bw % 4) ? (4 - (bw % 4)) : 0);
616  while (bits > (Uint8 *) surface->pixels) {
617  bits -= surface->pitch;
618  if (SDL_RWwrite(dst, bits, 1, bw) != bw) {
620  break;
621  }
622  if (pad) {
623  const Uint8 padbyte = 0;
624  for (i = 0; i < pad; ++i) {
625  SDL_RWwrite(dst, &padbyte, 1, 1);
626  }
627  }
628  }
629 
630  /* Write the BMP file size */
631  bfSize = (Uint32)(SDL_RWtell(dst) - fp_offset);
632  if (SDL_RWseek(dst, fp_offset + 2, RW_SEEK_SET) < 0) {
634  }
635  SDL_WriteLE32(dst, bfSize);
636  if (SDL_RWseek(dst, fp_offset + bfSize, RW_SEEK_SET) < 0) {
638  }
639 
640  /* Close it up.. */
641  SDL_UnlockSurface(surface);
642  if (surface != saveme) {
643  SDL_FreeSurface(surface);
644  }
645  }
646 
647  if (freedst && dst) {
648  SDL_RWclose(dst);
649  }
650  return ((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
651 }
#define BI_RGB
Definition: SDL_bmp.c:44
#define SDL_ClearError
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
#define SDL_GetError
#define SDL_UnlockSurface
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
Uint8 BytesPerPixel
Definition: SDL_pixels.h:304
#define SDL_ConvertSurface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_WriteLE16
#define SDL_LIL_ENDIAN
Definition: SDL_endian.h:37
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
#define SDL_Error
#define SDL_RWseek(ctx, offset, whence)
Definition: SDL_rwops.h:185
SDL_bool
Definition: SDL_stdinc.h:126
GLboolean GLboolean g
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
Uint8 BitsPerPixel
Definition: SDL_pixels.h:303
int32_t Sint32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:151
int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format)
Definition: SDL_pixels.c:521
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:42
#define NULL
Definition: begin_code.h:143
SDL_Color * colors
Definition: SDL_pixels.h:291
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define SDL_SetError
#define SDL_LockSurface
#define SDL_WriteLE32
#define RW_SEEK_SET
Definition: SDL_rwops.h:174
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:147
SDL_Palette * palette
Definition: SDL_pixels.h:302
#define SDL_strcmp
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:160
static int colors[7]
Definition: testgesture.c:40
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean GLboolean b
#define SDL_RWtell(ctx)
Definition: SDL_rwops.h:186
#define SDL_BYTEORDER
SDL_bool SDL_SetClipRect ( SDL_Surface surface,
const SDL_Rect rect 
)

Sets the clipping rectangle for the destination surface in a blit.

If the clip rectangle is NULL, clipping will be disabled.

If the clip rectangle doesn't intersect the surface, the function will return SDL_FALSE and blits will be completely clipped. Otherwise the function returns SDL_TRUE and blits to the surface will be clipped to the intersection of the surface area and the clipping rectangle.

Note that blits are automatically clipped to the edges of the source and destination surfaces.

Definition at line 457 of file SDL_surface.c.

References SDL_Surface::clip_rect, SDL_Rect::h, SDL_Surface::h, SDL_FALSE, SDL_IntersectRect, SDL_TRUE, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_ConvertSurface(), SDL_CreateRGBSurface(), and SDL_CreateRGBSurfaceFrom().

458 {
459  SDL_Rect full_rect;
460 
461  /* Don't do anything if there's no surface to act on */
462  if (!surface) {
463  return SDL_FALSE;
464  }
465 
466  /* Set up the full surface rectangle */
467  full_rect.x = 0;
468  full_rect.y = 0;
469  full_rect.w = surface->w;
470  full_rect.h = surface->h;
471 
472  /* Set the clipping rectangle */
473  if (!rect) {
474  surface->clip_rect = full_rect;
475  return SDL_TRUE;
476  }
477  return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
478 }
#define SDL_IntersectRect
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
int h
Definition: SDL_rect.h:67
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
int SDL_SetColorKey ( SDL_Surface surface,
int  flag,
Uint32  key 
)

Sets the color key (transparent pixel) in a blittable surface.

Parameters
surfaceThe surface to update
flagNon-zero to enable colorkey and 0 to disable colorkey
keyThe transparent pixel in the native surface format
Returns
0 on success, or -1 if the surface is not valid

You can pass SDL_RLEACCEL to enable RLE accelerated blits.

Definition at line 177 of file SDL_surface.c.

References SDL_Color::a, SDL_BlitInfo::colorkey, SDL_Palette::colors, SDL_BlitInfo::flags, SDL_Surface::format, SDL_BlitMap::info, SDL_Surface::map, SDL_Palette::ncolors, SDL_PixelFormat::palette, SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, SDL_COPY_COLORKEY, SDL_InvalidateMap(), SDL_InvalidParamError, SDL_RLEACCEL, SDL_SetSurfaceRLE(), and SDL_Palette::version.

Referenced by SDL_ConvertColorkeyToAlpha(), and SDL_ConvertSurface().

178 {
179  int flags;
180 
181  if (!surface) {
182  return SDL_InvalidParamError("surface");
183  }
184 
185  if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
186  return SDL_InvalidParamError("key");
187  }
188 
189  if (flag & SDL_RLEACCEL) {
190  SDL_SetSurfaceRLE(surface, 1);
191  }
192 
193  flags = surface->map->info.flags;
194  if (flag) {
195  surface->map->info.flags |= SDL_COPY_COLORKEY;
196  surface->map->info.colorkey = key;
197  if (surface->format->palette) {
198  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
199  ++surface->format->palette->version;
200  if (!surface->format->palette->version) {
201  surface->format->palette->version = 1;
202  }
203  }
204  } else {
205  if (surface->format->palette) {
206  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
207  ++surface->format->palette->version;
208  if (!surface->format->palette->version) {
209  surface->format->palette->version = 1;
210  }
211  }
212  surface->map->info.flags &= ~SDL_COPY_COLORKEY;
213  }
214  if (surface->map->info.flags != flags) {
215  SDL_InvalidateMap(surface->map);
216  }
217 
218  return 0;
219 }
Uint32 version
Definition: SDL_pixels.h:292
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
Sets the RLE acceleration hint for a surface.
Definition: SDL_surface.c:156
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
Uint32 colorkey
Definition: SDL_blit.h:69
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
#define SDL_ALPHA_TRANSPARENT
Definition: SDL_pixels.h:46
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
Uint8 a
Definition: SDL_pixels.h:284
SDL_Color * colors
Definition: SDL_pixels.h:291
SDL_PixelFormat * format
Definition: SDL_surface.h:72
GLbitfield flags
SDL_Palette * palette
Definition: SDL_pixels.h:302
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:45
SDL_BlitInfo info
Definition: SDL_blit.h:91
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
int SDL_SetSurfaceAlphaMod ( SDL_Surface surface,
Uint8  alpha 
)

Set an additional alpha value used in blit operations.

Parameters
surfaceThe surface to update.
alphaThe alpha value multiplied into blit operations.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_GetSurfaceAlphaMod()

Definition at line 355 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, SDL_COPY_MODULATE_ALPHA, and SDL_InvalidateMap().

356 {
357  int flags;
358 
359  if (!surface) {
360  return -1;
361  }
362 
363  surface->map->info.a = alpha;
364 
365  flags = surface->map->info.flags;
366  if (alpha != 0xFF) {
367  surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
368  } else {
369  surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
370  }
371  if (surface->map->info.flags != flags) {
372  SDL_InvalidateMap(surface->map);
373  }
374  return 0;
375 }
GLfloat GLfloat GLfloat alpha
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
GLbitfield flags
SDL_BlitInfo info
Definition: SDL_blit.h:91
Uint8 a
Definition: SDL_blit.h:70
int SDL_SetSurfaceBlendMode ( SDL_Surface surface,
SDL_BlendMode  blendMode 
)

Set the blend mode used for blit operations.

Parameters
surfaceThe surface to update.
blendModeSDL_BlendMode to use for blit blending.
Returns
0 on success, or -1 if the parameters are not valid.
See also
SDL_GetSurfaceBlendMode()

Definition at line 391 of file SDL_surface.c.

References SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, SDL_BLENDMODE_ADD, SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD, SDL_BLENDMODE_NONE, SDL_COPY_ADD, SDL_COPY_BLEND, SDL_COPY_MOD, SDL_InvalidateMap(), and SDL_Unsupported.

Referenced by SDL_ConvertColorkeyToAlpha(), SDL_ConvertSurface(), and SDL_CreateRGBSurface().

392 {
393  int flags, status;
394 
395  if (!surface) {
396  return -1;
397  }
398 
399  status = 0;
400  flags = surface->map->info.flags;
401  surface->map->info.flags &=
403  switch (blendMode) {
404  case SDL_BLENDMODE_NONE:
405  break;
406  case SDL_BLENDMODE_BLEND:
407  surface->map->info.flags |= SDL_COPY_BLEND;
408  break;
409  case SDL_BLENDMODE_ADD:
410  surface->map->info.flags |= SDL_COPY_ADD;
411  break;
412  case SDL_BLENDMODE_MOD:
413  surface->map->info.flags |= SDL_COPY_MOD;
414  break;
415  default:
416  status = SDL_Unsupported();
417  break;
418  }
419 
420  if (surface->map->info.flags != flags) {
421  SDL_InvalidateMap(surface->map);
422  }
423 
424  return status;
425 }
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
#define SDL_Unsupported()
Definition: SDL_error.h:53
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:91
int SDL_SetSurfaceColorMod ( SDL_Surface surface,
Uint8  r,
Uint8  g,
Uint8  b 
)

Set an additional color value used in blit operations.

Parameters
surfaceThe surface to update.
rThe red color value multiplied into blit operations.
gThe green color value multiplied into blit operations.
bThe blue color value multiplied into blit operations.
Returns
0 on success, or -1 if the surface is not valid.
See also
SDL_GetSurfaceColorMod()

Definition at line 310 of file SDL_surface.c.

References SDL_BlitInfo::b, SDL_BlitInfo::flags, SDL_BlitInfo::g, SDL_BlitMap::info, SDL_Surface::map, SDL_BlitInfo::r, SDL_COPY_MODULATE_COLOR, and SDL_InvalidateMap().

311 {
312  int flags;
313 
314  if (!surface) {
315  return -1;
316  }
317 
318  surface->map->info.r = r;
319  surface->map->info.g = g;
320  surface->map->info.b = b;
321 
322  flags = surface->map->info.flags;
323  if (r != 0xFF || g != 0xFF || b != 0xFF) {
324  surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
325  } else {
326  surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
327  }
328  if (surface->map->info.flags != flags) {
329  SDL_InvalidateMap(surface->map);
330  }
331  return 0;
332 }
Uint8 r
Definition: SDL_blit.h:70
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
Uint8 b
Definition: SDL_blit.h:70
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
Uint8 g
Definition: SDL_blit.h:70
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
GLboolean GLboolean g
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:91
int SDL_SetSurfacePalette ( SDL_Surface surface,
SDL_Palette palette 
)

Set the palette used by a surface.

Returns
0, or -1 if the surface format doesn't use a palette.
Note
A single palette can be shared with many surfaces.

Definition at line 142 of file SDL_surface.c.

References SDL_Surface::format, SDL_Surface::map, SDL_InvalidateMap(), SDL_SetError, and SDL_SetPixelFormatPalette.

Referenced by SDL_CreateRGBSurface(), and SDL_FreeSurface().

143 {
144  if (!surface) {
145  return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
146  }
147  if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
148  return -1;
149  }
150  SDL_InvalidateMap(surface->map);
151 
152  return 0;
153 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
#define SDL_SetPixelFormatPalette
int SDL_SetSurfaceRLE ( SDL_Surface surface,
int  flag 
)

Sets the RLE acceleration hint for a surface.

Returns
0 on success, or -1 if the surface is not valid
Note
If RLE is enabled, colorkey and alpha blending blits are much faster, but the surface must be locked before directly accessing the pixels.

Definition at line 156 of file SDL_surface.c.

References SDL_BlitInfo::flags, SDL_BlitMap::info, SDL_Surface::map, SDL_COPY_RLE_DESIRED, and SDL_InvalidateMap().

Referenced by SDL_ConvertSurface(), and SDL_SetColorKey().

157 {
158  int flags;
159 
160  if (!surface) {
161  return -1;
162  }
163 
164  flags = surface->map->info.flags;
165  if (flag) {
166  surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
167  } else {
168  surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
169  }
170  if (surface->map->info.flags != flags) {
171  SDL_InvalidateMap(surface->map);
172  }
173  return 0;
174 }
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
SDL_BlitInfo info
Definition: SDL_blit.h:91
int SDL_SoftStretch ( SDL_Surface src,
const SDL_Rect srcrect,
SDL_Surface dst,
const SDL_Rect dstrect 
)

Perform a fast, low quality, stretch blit between two surfaces of the same pixel format.

Note
This function uses a static buffer, and is not thread-safe.

Definition at line 203 of file SDL_stretch.c.

References SDL_PixelFormat::BytesPerPixel, copy_row3(), SDL_Surface::format, SDL_PixelFormat::format, SDL_Rect::h, SDL_Surface::h, NULL, SDL_Surface::pitch, SDL_Surface::pixels, pop, SDL_FALSE, SDL_LockSurface, SDL_MUSTLOCK, SDL_SetError, SDL_TRUE, SDL_UnlockSurface, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

205 {
206  int src_locked;
207  int dst_locked;
208  int pos, inc;
209  int dst_maxrow;
210  int src_row, dst_row;
211  Uint8 *srcp = NULL;
212  Uint8 *dstp;
213  SDL_Rect full_src;
214  SDL_Rect full_dst;
215 #ifdef USE_ASM_STRETCH
216  SDL_bool use_asm = SDL_TRUE;
217 #ifdef __GNUC__
218  int u1, u2;
219 #endif
220 #endif /* USE_ASM_STRETCH */
221  const int bpp = dst->format->BytesPerPixel;
222 
223  if (src->format->format != dst->format->format) {
224  return SDL_SetError("Only works with same format surfaces");
225  }
226 
227  /* Verify the blit rectangles */
228  if (srcrect) {
229  if ((srcrect->x < 0) || (srcrect->y < 0) ||
230  ((srcrect->x + srcrect->w) > src->w) ||
231  ((srcrect->y + srcrect->h) > src->h)) {
232  return SDL_SetError("Invalid source blit rectangle");
233  }
234  } else {
235  full_src.x = 0;
236  full_src.y = 0;
237  full_src.w = src->w;
238  full_src.h = src->h;
239  srcrect = &full_src;
240  }
241  if (dstrect) {
242  if ((dstrect->x < 0) || (dstrect->y < 0) ||
243  ((dstrect->x + dstrect->w) > dst->w) ||
244  ((dstrect->y + dstrect->h) > dst->h)) {
245  return SDL_SetError("Invalid destination blit rectangle");
246  }
247  } else {
248  full_dst.x = 0;
249  full_dst.y = 0;
250  full_dst.w = dst->w;
251  full_dst.h = dst->h;
252  dstrect = &full_dst;
253  }
254 
255  /* Lock the destination if it's in hardware */
256  dst_locked = 0;
257  if (SDL_MUSTLOCK(dst)) {
258  if (SDL_LockSurface(dst) < 0) {
259  return SDL_SetError("Unable to lock destination surface");
260  }
261  dst_locked = 1;
262  }
263  /* Lock the source if it's in hardware */
264  src_locked = 0;
265  if (SDL_MUSTLOCK(src)) {
266  if (SDL_LockSurface(src) < 0) {
267  if (dst_locked) {
268  SDL_UnlockSurface(dst);
269  }
270  return SDL_SetError("Unable to lock source surface");
271  }
272  src_locked = 1;
273  }
274 
275  /* Set up the data... */
276  pos = 0x10000;
277  inc = (srcrect->h << 16) / dstrect->h;
278  src_row = srcrect->y;
279  dst_row = dstrect->y;
280 
281 #ifdef USE_ASM_STRETCH
282  /* Write the opcodes for this stretch */
283  if ((bpp == 3) || (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0)) {
284  use_asm = SDL_FALSE;
285  }
286 #endif
287 
288  /* Perform the stretch blit */
289  for (dst_maxrow = dst_row + dstrect->h; dst_row < dst_maxrow; ++dst_row) {
290  dstp = (Uint8 *) dst->pixels + (dst_row * dst->pitch)
291  + (dstrect->x * bpp);
292  while (pos >= 0x10000L) {
293  srcp = (Uint8 *) src->pixels + (src_row * src->pitch)
294  + (srcrect->x * bpp);
295  ++src_row;
296  pos -= 0x10000L;
297  }
298 #ifdef USE_ASM_STRETCH
299  if (use_asm) {
300 #ifdef __GNUC__
301  __asm__ __volatile__("call *%4":"=&D"(u1), "=&S"(u2)
302  :"0"(dstp), "1"(srcp), "r"(copy_row)
303  :"memory");
304 #elif defined(_MSC_VER) || defined(__WATCOMC__)
305  /* *INDENT-OFF* */
306  {
307  void *code = copy_row;
308  __asm {
309  push edi
310  push esi
311  mov edi, dstp
312  mov esi, srcp
313  call dword ptr code
314  pop esi
315  pop edi
316  }
317  }
318  /* *INDENT-ON* */
319 #else
320 #error Need inline assembly for this compiler
321 #endif
322  } else
323 #endif
324  switch (bpp) {
325  case 1:
326  copy_row1(srcp, srcrect->w, dstp, dstrect->w);
327  break;
328  case 2:
329  copy_row2((Uint16 *) srcp, srcrect->w,
330  (Uint16 *) dstp, dstrect->w);
331  break;
332  case 3:
333  copy_row3(srcp, srcrect->w, dstp, dstrect->w);
334  break;
335  case 4:
336  copy_row4((Uint32 *) srcp, srcrect->w,
337  (Uint32 *) dstp, dstrect->w);
338  break;
339  }
340  pos += inc;
341  }
342 
343  /* We need to unlock the surfaces if they're locked */
344  if (dst_locked) {
345  SDL_UnlockSurface(dst);
346  }
347  if (src_locked) {
348  SDL_UnlockSurface(src);
349  }
350  return (0);
351 }
#define SDL_UnlockSurface
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
Uint8 BytesPerPixel
Definition: SDL_pixels.h:304
GLfixed u1
static void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w)
Definition: SDL_stretch.c:177
SDL_bool
Definition: SDL_stdinc.h:126
GLfixed GLfixed u2
void * pixels
Definition: SDL_surface.h:75
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
#define pop
Definition: SDL_qsort.c:192
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define NULL
Definition: begin_code.h:143
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
#define SDL_LockSurface
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:61
int h
Definition: SDL_rect.h:67
uint16_t Uint16
An unsigned 16-bit integer type.
Definition: SDL_stdinc.h:147
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
void SDL_UnlockSurface ( SDL_Surface surface)
See also
SDL_LockSurface()

Definition at line 824 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::locked, SDL_RLEACCEL, and SDL_RLESurface().

Referenced by SDL_ConvertColorkeyToAlpha(), and SDL_FreeSurface().

825 {
826  /* Only perform an unlock if we are locked */
827  if (!surface->locked || (--surface->locked > 0)) {
828  return;
829  }
830 
831  /* Update RLE encoded surface with new data */
832  if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
833  surface->flags &= ~SDL_RLEACCEL; /* stop lying */
834  SDL_RLESurface(surface);
835  }
836 }
Uint32 flags
Definition: SDL_surface.h:71
int SDL_RLESurface(SDL_Surface *surface)
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
int SDL_UpperBlit ( SDL_Surface src,
const SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is the public blit function, SDL_BlitSurface(), and it performs rectangle validation and clipping before passing it to SDL_LowerBlit()

Definition at line 523 of file SDL_surface.c.

References SDL_Surface::clip_rect, SDL_BlitInfo::flags, SDL_Rect::h, SDL_Surface::h, SDL_BlitMap::info, SDL_Surface::locked, SDL_Surface::map, NULL, SDL_COPY_NEAREST, SDL_InvalidateMap(), SDL_LowerBlit(), SDL_SetError, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

525 {
526  SDL_Rect fulldst;
527  int srcx, srcy, w, h;
528 
529  /* Make sure the surfaces aren't locked */
530  if (!src || !dst) {
531  return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
532  }
533  if (src->locked || dst->locked) {
534  return SDL_SetError("Surfaces must not be locked during blit");
535  }
536 
537  /* If the destination rectangle is NULL, use the entire dest surface */
538  if (dstrect == NULL) {
539  fulldst.x = fulldst.y = 0;
540  fulldst.w = dst->w;
541  fulldst.h = dst->h;
542  dstrect = &fulldst;
543  }
544 
545  /* clip the source rectangle to the source surface */
546  if (srcrect) {
547  int maxw, maxh;
548 
549  srcx = srcrect->x;
550  w = srcrect->w;
551  if (srcx < 0) {
552  w += srcx;
553  dstrect->x -= srcx;
554  srcx = 0;
555  }
556  maxw = src->w - srcx;
557  if (maxw < w)
558  w = maxw;
559 
560  srcy = srcrect->y;
561  h = srcrect->h;
562  if (srcy < 0) {
563  h += srcy;
564  dstrect->y -= srcy;
565  srcy = 0;
566  }
567  maxh = src->h - srcy;
568  if (maxh < h)
569  h = maxh;
570 
571  } else {
572  srcx = srcy = 0;
573  w = src->w;
574  h = src->h;
575  }
576 
577  /* clip the destination rectangle against the clip rectangle */
578  {
579  SDL_Rect *clip = &dst->clip_rect;
580  int dx, dy;
581 
582  dx = clip->x - dstrect->x;
583  if (dx > 0) {
584  w -= dx;
585  dstrect->x += dx;
586  srcx += dx;
587  }
588  dx = dstrect->x + w - clip->x - clip->w;
589  if (dx > 0)
590  w -= dx;
591 
592  dy = clip->y - dstrect->y;
593  if (dy > 0) {
594  h -= dy;
595  dstrect->y += dy;
596  srcy += dy;
597  }
598  dy = dstrect->y + h - clip->y - clip->h;
599  if (dy > 0)
600  h -= dy;
601  }
602 
603  /* Switch back to a fast blit if we were previously stretching */
604  if (src->map->info.flags & SDL_COPY_NEAREST) {
605  src->map->info.flags &= ~SDL_COPY_NEAREST;
606  SDL_InvalidateMap(src->map);
607  }
608 
609  if (w > 0 && h > 0) {
610  SDL_Rect sr;
611  sr.x = srcx;
612  sr.y = srcy;
613  sr.w = dstrect->w = w;
614  sr.h = dstrect->h = h;
615  return SDL_LowerBlit(src, &sr, dst, dstrect);
616  }
617  dstrect->w = dstrect->h = 0;
618  return 0;
619 }
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:500
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:974
#define SDL_COPY_NEAREST
Definition: SDL_blit.h:40
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
int h
Definition: SDL_rect.h:67
GLubyte GLubyte GLubyte GLubyte w
int y
Definition: SDL_rect.h:66
GLfloat GLfloat GLfloat GLfloat h
SDL_BlitInfo info
Definition: SDL_blit.h:91
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
int SDL_UpperBlitScaled ( SDL_Surface src,
const SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)

This is the public scaled blit function, SDL_BlitScaled(), and it performs rectangle validation and clipping before passing it to SDL_LowerBlitScaled()

Definition at line 622 of file SDL_surface.c.

References SDL_Surface::clip_rect, SDL_Rect::h, SDL_Surface::h, SDL_Surface::locked, NULL, SDL_BlitSurface, SDL_floor, SDL_LowerBlitScaled(), SDL_SetError, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

624 {
625  double src_x0, src_y0, src_x1, src_y1;
626  double dst_x0, dst_y0, dst_x1, dst_y1;
627  SDL_Rect final_src, final_dst;
628  double scaling_w, scaling_h;
629  int src_w, src_h;
630  int dst_w, dst_h;
631 
632  /* Make sure the surfaces aren't locked */
633  if (!src || !dst) {
634  return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
635  }
636  if (src->locked || dst->locked) {
637  return SDL_SetError("Surfaces must not be locked during blit");
638  }
639 
640  if (NULL == srcrect) {
641  src_w = src->w;
642  src_h = src->h;
643  } else {
644  src_w = srcrect->w;
645  src_h = srcrect->h;
646  }
647 
648  if (NULL == dstrect) {
649  dst_w = dst->w;
650  dst_h = dst->h;
651  } else {
652  dst_w = dstrect->w;
653  dst_h = dstrect->h;
654  }
655 
656  if (dst_w == src_w && dst_h == src_h) {
657  /* No scaling, defer to regular blit */
658  return SDL_BlitSurface(src, srcrect, dst, dstrect);
659  }
660 
661  scaling_w = (double)dst_w / src_w;
662  scaling_h = (double)dst_h / src_h;
663 
664  if (NULL == dstrect) {
665  dst_x0 = 0;
666  dst_y0 = 0;
667  dst_x1 = dst_w - 1;
668  dst_y1 = dst_h - 1;
669  } else {
670  dst_x0 = dstrect->x;
671  dst_y0 = dstrect->y;
672  dst_x1 = dst_x0 + dst_w - 1;
673  dst_y1 = dst_y0 + dst_h - 1;
674  }
675 
676  if (NULL == srcrect) {
677  src_x0 = 0;
678  src_y0 = 0;
679  src_x1 = src_w - 1;
680  src_y1 = src_h - 1;
681  } else {
682  src_x0 = srcrect->x;
683  src_y0 = srcrect->y;
684  src_x1 = src_x0 + src_w - 1;
685  src_y1 = src_y0 + src_h - 1;
686 
687  /* Clip source rectangle to the source surface */
688 
689  if (src_x0 < 0) {
690  dst_x0 -= src_x0 * scaling_w;
691  src_x0 = 0;
692  }
693 
694  if (src_x1 >= src->w) {
695  dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
696  src_x1 = src->w - 1;
697  }
698 
699  if (src_y0 < 0) {
700  dst_y0 -= src_y0 * scaling_h;
701  src_y0 = 0;
702  }
703 
704  if (src_y1 >= src->h) {
705  dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
706  src_y1 = src->h - 1;
707  }
708  }
709 
710  /* Clip destination rectangle to the clip rectangle */
711 
712  /* Translate to clip space for easier calculations */
713  dst_x0 -= dst->clip_rect.x;
714  dst_x1 -= dst->clip_rect.x;
715  dst_y0 -= dst->clip_rect.y;
716  dst_y1 -= dst->clip_rect.y;
717 
718  if (dst_x0 < 0) {
719  src_x0 -= dst_x0 / scaling_w;
720  dst_x0 = 0;
721  }
722 
723  if (dst_x1 >= dst->clip_rect.w) {
724  src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
725  dst_x1 = dst->clip_rect.w - 1;
726  }
727 
728  if (dst_y0 < 0) {
729  src_y0 -= dst_y0 / scaling_h;
730  dst_y0 = 0;
731  }
732 
733  if (dst_y1 >= dst->clip_rect.h) {
734  src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
735  dst_y1 = dst->clip_rect.h - 1;
736  }
737 
738  /* Translate back to surface coordinates */
739  dst_x0 += dst->clip_rect.x;
740  dst_x1 += dst->clip_rect.x;
741  dst_y0 += dst->clip_rect.y;
742  dst_y1 += dst->clip_rect.y;
743 
744  final_src.x = (int)SDL_floor(src_x0 + 0.5);
745  final_src.y = (int)SDL_floor(src_y0 + 0.5);
746  final_src.w = (int)SDL_floor(src_x1 - src_x0 + 1.5);
747  final_src.h = (int)SDL_floor(src_y1 - src_y0 + 1.5);
748 
749  final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
750  final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
751  final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
752  final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
753 
754  if (final_dst.w < 0)
755  final_dst.w = 0;
756  if (final_dst.h < 0)
757  final_dst.h = 0;
758 
759  if (dstrect)
760  *dstrect = final_dst;
761 
762  if (final_dst.w == 0 || final_dst.h == 0 ||
763  final_src.w <= 0 || final_src.h <= 0) {
764  /* No-op. */
765  return 0;
766  }
767 
768  return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
769 }
#define SDL_BlitSurface
Definition: SDL_surface.h:447
#define SDL_floor
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
#define NULL
Definition: begin_code.h:143
#define SDL_SetError
int h
Definition: SDL_rect.h:67
int SDL_LowerBlitScaled(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:776
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64