SDL  2.0
SDL_surface.c File Reference
#include "../SDL_internal.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_RLEaccel_c.h"
#include "SDL_pixels_c.h"
+ Include dependency graph for SDL_surface.c:

Go to the source code of this file.

Macros

#define CLAMP(val)   ((val) > 0 ? ((val) < 255 ? (val) : 255) : 0)
 
#define MAKE_Y(r, g, b)   (int)( 0.29900f * (r) + 0.58700f * (g) + 0.11400f * (b))
 
#define MAKE_U(r, g, b)   (int)(-0.16874f * (r) - 0.33126f * (g) + 0.50000f * (b) + 128)
 
#define MAKE_V(r, g, b)   (int)( 0.50000f * (r) - 0.41869f * (g) - 0.08131f * (b) + 128)
 
#define MAKE_R(y, u, v)   CLAMP((int)((y) + 1.40200f * ((v) - 128)))
 
#define MAKE_G(y, u, v)   CLAMP((int)((y) - 0.34414f * ((u) - 128) - 0.71414f * ((v) - 128)))
 
#define MAKE_B(y, u, v)   CLAMP((int)((y) + 1.77200f * ((u) - 128) ))
 
#define WRITE_RGB_PIXEL(y, u, v)
 
#define READ_PACKED_YUV(var1, var2, var3, var4)
 
#define READ_2x2_PIXELS
 
#define READ_2x1_PIXELS
 
#define READ_1x2_PIXELS
 
#define READ_1x1_PIXEL
 
#define READ_TWO_RGB_PIXELS
 
#define READ_ONE_RGB_PIXEL
 

Functions

static int SDL_ConvertPixels_YUV_to_ARGB8888 (int width, int height, Uint32 src_format, const void *src, void *dst, int dst_pitch)
 
static int SDL_ConvertPixels_ARGB8888_to_YUV (int width, int height, const void *src, int src_pitch, Uint32 dst_format, void *dst)
 
 SDL_COMPILE_TIME_ASSERT (surface_size_assumptions, sizeof(int)==sizeof(Sint32) &&sizeof(size_t) >=sizeof(Sint32))
 
SDL_SurfaceSDL_CreateRGBSurfaceWithFormat (Uint32 flags, int width, int height, int depth, Uint32 format)
 
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)
 
SDL_SurfaceSDL_CreateRGBSurfaceWithFormatFrom (void *pixels, int width, int height, int depth, int pitch, Uint32 format)
 
int SDL_SetSurfacePalette (SDL_Surface *surface, SDL_Palette *palette)
 Set the palette used by a surface. More...
 
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...
 
static void SDL_ConvertColorkeyToAlpha (SDL_Surface *surface)
 
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)
 
int SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
int SDL_UpperBlit (SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
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)
 
int SDL_LockSurface (SDL_Surface *surface)
 Sets up a surface for directly accessing the pixels. More...
 
void SDL_UnlockSurface (SDL_Surface *surface)
 
SDL_SurfaceSDL_DuplicateSurface (SDL_Surface *surface)
 
SDL_SurfaceSDL_ConvertSurface (SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
 
SDL_SurfaceSDL_ConvertSurfaceFormat (SDL_Surface *surface, Uint32 pixel_format, Uint32 flags)
 
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)
 
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...
 
void SDL_FreeSurface (SDL_Surface *surface)
 

Macro Definition Documentation

◆ CLAMP

#define CLAMP (   val)    ((val) > 0 ? ((val) < 255 ? (val) : 255) : 0)

Definition at line 1327 of file SDL_surface.c.

◆ MAKE_B

#define MAKE_B (   y,
  u,
  v 
)    CLAMP((int)((y) + 1.77200f * ((u) - 128) ))

Definition at line 1338 of file SDL_surface.c.

◆ MAKE_G

#define MAKE_G (   y,
  u,
  v 
)    CLAMP((int)((y) - 0.34414f * ((u) - 128) - 0.71414f * ((v) - 128)))

Definition at line 1337 of file SDL_surface.c.

◆ MAKE_R

#define MAKE_R (   y,
  u,
  v 
)    CLAMP((int)((y) + 1.40200f * ((v) - 128)))

Definition at line 1336 of file SDL_surface.c.

◆ MAKE_U

#define MAKE_U (   r,
  g,
  b 
)    (int)(-0.16874f * (r) - 0.33126f * (g) + 0.50000f * (b) + 128)

Definition at line 1333 of file SDL_surface.c.

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ MAKE_V

#define MAKE_V (   r,
  g,
  b 
)    (int)( 0.50000f * (r) - 0.41869f * (g) - 0.08131f * (b) + 128)

Definition at line 1334 of file SDL_surface.c.

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ MAKE_Y

#define MAKE_Y (   r,
  g,
  b 
)    (int)( 0.29900f * (r) + 0.58700f * (g) + 0.11400f * (b))

Definition at line 1332 of file SDL_surface.c.

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ READ_1x1_PIXEL

#define READ_1x1_PIXEL
Value:
const Uint8 b = curr_row[8 * i + 0]; \
const Uint8 g = curr_row[8 * i + 1]; \
const Uint8 r = curr_row[8 * i + 2]; \
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
uint8_t Uint8
Definition: SDL_stdinc.h:157
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
GLboolean GLboolean g
GLboolean GLboolean GLboolean b

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ READ_1x2_PIXELS

#define READ_1x2_PIXELS
Value:
const Uint8 b = (curr_row[8 * i + 0] + curr_row[8 * i + 4]) >> 1; \
const Uint8 g = (curr_row[8 * i + 1] + curr_row[8 * i + 5]) >> 1; \
const Uint8 r = (curr_row[8 * i + 2] + curr_row[8 * i + 6]) >> 1; \
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
uint8_t Uint8
Definition: SDL_stdinc.h:157
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
GLboolean GLboolean g
GLboolean GLboolean GLboolean b

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ READ_2x1_PIXELS

#define READ_2x1_PIXELS
Value:
const Uint8 b = (curr_row[8 * i + 0] + next_row[8 * i + 0]) >> 1; \
const Uint8 g = (curr_row[8 * i + 1] + next_row[8 * i + 1]) >> 1; \
const Uint8 r = (curr_row[8 * i + 2] + next_row[8 * i + 2]) >> 1; \
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
uint8_t Uint8
Definition: SDL_stdinc.h:157
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
GLboolean GLboolean g
GLboolean GLboolean GLboolean b

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ READ_2x2_PIXELS

#define READ_2x2_PIXELS
Value:
const Uint32 p1 = ((const Uint32 *)curr_row)[2 * i]; \
const Uint32 p2 = ((const Uint32 *)curr_row)[2 * i + 1]; \
const Uint32 p3 = ((const Uint32 *)next_row)[2 * i]; \
const Uint32 p4 = ((const Uint32 *)next_row)[2 * i + 1]; \
const Uint32 b = ((p1 & 0x000000ff) + (p2 & 0x000000ff) + (p3 & 0x000000ff) + (p4 & 0x000000ff)) >> 2; \
const Uint32 g = ((p1 & 0x0000ff00) + (p2 & 0x0000ff00) + (p3 & 0x0000ff00) + (p4 & 0x0000ff00)) >> 10; \
const Uint32 r = ((p1 & 0x00ff0000) + (p2 & 0x00ff0000) + (p3 & 0x00ff0000) + (p4 & 0x00ff0000)) >> 18; \
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
uint32_t Uint32
Definition: SDL_stdinc.h:181
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
GLboolean GLboolean g
GLboolean GLboolean GLboolean b

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ READ_ONE_RGB_PIXEL

#define READ_ONE_RGB_PIXEL
Value:
const Uint8 b = curr_row[8 * i + 0]; \
const Uint8 g = curr_row[8 * i + 1]; \
const Uint8 r = curr_row[8 * i + 2]; \
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
uint8_t Uint8
Definition: SDL_stdinc.h:157
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
GLboolean GLboolean g
GLboolean GLboolean GLboolean b

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ READ_PACKED_YUV

#define READ_PACKED_YUV (   var1,
  var2,
  var3,
  var4 
)
Value:
const Uint8 var1 = plane[0]; \
const Uint8 var2 = plane[1]; \
const Uint8 var3 = plane[2]; \
const Uint8 var4 = plane[3]; \
plane += 4; \
uint8_t Uint8
Definition: SDL_stdinc.h:157

Referenced by SDL_ConvertPixels_YUV_to_ARGB8888().

◆ READ_TWO_RGB_PIXELS

#define READ_TWO_RGB_PIXELS
Value:
const Uint8 b = curr_row[8 * i + 0]; \
const Uint8 g = curr_row[8 * i + 1]; \
const Uint8 r = curr_row[8 * i + 2]; \
const Uint8 b1 = curr_row[8 * i + 4]; \
const Uint8 g1 = curr_row[8 * i + 5]; \
const Uint8 r1 = curr_row[8 * i + 6]; \
const Uint8 B = (b + b1) >> 1; \
const Uint8 G = (g + g1) >> 1; \
const Uint8 R = (r + r1) >> 1; \
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
uint8_t Uint8
Definition: SDL_stdinc.h:157
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define G(x, y, z)
Definition: SDL_test_md5.c:74
GLboolean GLboolean g
GLboolean GLboolean GLboolean b

Referenced by SDL_ConvertPixels_ARGB8888_to_YUV().

◆ WRITE_RGB_PIXEL

#define WRITE_RGB_PIXEL (   y,
  u,
  v 
)
Value:
*((Uint32*)curr_row) = \
(MAKE_B((y), (u), (v)) \
| (MAKE_G((y), (u), (v)) << 8) \
| (MAKE_R((y), (u), (v)) << 16) \
| 0xff000000); \
curr_row += 4; \
const GLdouble * v
Definition: SDL_opengl.h:2064
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define MAKE_G(y, u, v)
Definition: SDL_surface.c:1337
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
#define MAKE_R(y, u, v)
Definition: SDL_surface.c:1336
#define MAKE_B(y, u, v)
Definition: SDL_surface.c:1338

Referenced by SDL_ConvertPixels_YUV_to_ARGB8888().

Function Documentation

◆ SDL_COMPILE_TIME_ASSERT()

SDL_COMPILE_TIME_ASSERT ( surface_size_assumptions  ,
sizeof(int)  = =sizeof(Sint32) &&sizeof(size_t) >=sizeof(Sint32) 
)

◆ SDL_ConvertColorkeyToAlpha()

static void SDL_ConvertColorkeyToAlpha ( SDL_Surface surface)
static

Definition at line 299 of file SDL_surface.c.

References SDL_PixelFormat::Amask, SDL_PixelFormat::BytesPerPixel, SDL_BlitInfo::colorkey, SDL_BlitInfo::flags, SDL_Surface::format, SDL_Surface::h, SDL_BlitMap::info, SDL_Surface::map, SDL_Surface::pitch, SDL_Surface::pixels, SDL_BLENDMODE_BLEND, SDL_COPY_COLORKEY, SDL_LockSurface(), SDL_SetColorKey(), SDL_SetSurfaceBlendMode(), SDL_UnlockSurface(), and SDL_Surface::w.

Referenced by SDL_ConvertSurface().

300 {
301  int x, y;
302 
303  if (!surface) {
304  return;
305  }
306 
307  if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
308  !surface->format->Amask) {
309  return;
310  }
311 
312  SDL_LockSurface(surface);
313 
314  switch (surface->format->BytesPerPixel) {
315  case 2:
316  {
317  Uint16 *row, *spot;
318  Uint16 ckey = (Uint16) surface->map->info.colorkey;
319  Uint16 mask = (Uint16) (~surface->format->Amask);
320 
321  /* Ignore alpha in colorkey comparison */
322  ckey &= mask;
323  row = (Uint16 *) surface->pixels;
324  for (y = surface->h; y--;) {
325  spot = row;
326  for (x = surface->w; x--;) {
327  if ((*spot & mask) == ckey) {
328  *spot &= mask;
329  }
330  ++spot;
331  }
332  row += surface->pitch / 2;
333  }
334  }
335  break;
336  case 3:
337  /* FIXME */
338  break;
339  case 4:
340  {
341  Uint32 *row, *spot;
342  Uint32 ckey = surface->map->info.colorkey;
343  Uint32 mask = ~surface->format->Amask;
344 
345  /* Ignore alpha in colorkey comparison */
346  ckey &= mask;
347  row = (Uint32 *) surface->pixels;
348  for (y = surface->h; y--;) {
349  spot = row;
350  for (x = surface->w; x--;) {
351  if ((*spot & mask) == ckey) {
352  *spot &= mask;
353  }
354  ++spot;
355  }
356  row += surface->pitch / 4;
357  }
358  }
359  break;
360  }
361 
362  SDL_UnlockSurface(surface);
363 
364  SDL_SetColorKey(surface, 0, 0);
366 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:883
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
int SDL_LockSurface(SDL_Surface *surface)
Sets up a surface for directly accessing the pixels.
Definition: SDL_surface.c:862
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
Set the blend mode used for blit operations.
Definition: SDL_surface.c:450
uint32_t Uint32
Definition: SDL_stdinc.h:181
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:236
Uint32 colorkey
Definition: SDL_blit.h:69
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
GLenum GLint GLuint mask
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
SDL_PixelFormat * format
Definition: SDL_surface.h:72
uint16_t Uint16
Definition: SDL_stdinc.h:169
GLenum GLenum void * row
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_ConvertPixels()

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 1123 of file SDL_surface.c.

References SDL_Rect::h, i, NULL, rect, SDL_BYTESPERPIXEL, SDL_ConvertPixels_ARGB8888_to_YUV(), SDL_ConvertPixels_YUV_to_ARGB8888(), SDL_CreateSurfaceOnStack(), SDL_free, SDL_InvalidParamError, SDL_ISPIXELFORMAT_FOURCC, SDL_LowerBlit(), SDL_malloc, SDL_memcpy, SDL_PIXELFORMAT_ARGB8888, 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.

1126 {
1127  SDL_Surface src_surface, dst_surface;
1128  SDL_PixelFormat src_fmt, dst_fmt;
1129  SDL_BlitMap src_blitmap, dst_blitmap;
1130  SDL_Rect rect;
1131  void *nonconst_src = (void *) src;
1132 
1133  /* Check to make sure we are blitting somewhere, so we don't crash */
1134  if (!dst) {
1135  return SDL_InvalidParamError("dst");
1136  }
1137  if (!dst_pitch) {
1138  return SDL_InvalidParamError("dst_pitch");
1139  }
1140 
1141  /* Fast path for same format copy */
1142  if (src_format == dst_format) {
1143  int i;
1144 
1145  if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
1146  switch (src_format) {
1147  case SDL_PIXELFORMAT_YUY2:
1148  case SDL_PIXELFORMAT_UYVY:
1149  case SDL_PIXELFORMAT_YVYU:
1150  /* Packed planes */
1151  width = 4 * ((width + 1) / 2);
1152  for (i = height; i--;) {
1153  SDL_memcpy(dst, src, width);
1154  src = (const Uint8*)src + src_pitch;
1155  dst = (Uint8*)dst + dst_pitch;
1156  }
1157  break;
1158  case SDL_PIXELFORMAT_YV12:
1159  case SDL_PIXELFORMAT_IYUV:
1160  case SDL_PIXELFORMAT_NV12:
1161  case SDL_PIXELFORMAT_NV21:
1162  {
1163  /* Y plane */
1164  for (i = height; i--;) {
1165  SDL_memcpy(dst, src, width);
1166  src = (const Uint8*)src + src_pitch;
1167  dst = (Uint8*)dst + dst_pitch;
1168  }
1169 
1170  /* not sure the pitch is relevant here.
1171  this also works to add the size of two chroma planes */
1172 #if 0
1173  SDL_memcpy(dst, src, 2 * ((width + 1)/2) * ((height+1)/2));
1174 #else
1175 
1176  if (src_format == SDL_PIXELFORMAT_YV12 || src_format == SDL_PIXELFORMAT_IYUV) {
1177  /* U and V planes are a quarter the size of the Y plane */
1178  width = (width + 1) / 2;
1179  height = (height + 1) / 2;
1180  src_pitch = (src_pitch + 1) / 2;
1181  dst_pitch = (dst_pitch + 1) / 2;
1182  for (i = height * 2; i--;) {
1183  SDL_memcpy(dst, src, width);
1184  src = (const Uint8*)src + src_pitch;
1185  dst = (Uint8*)dst + dst_pitch;
1186  }
1187  } else if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
1188  /* U/V plane is half the height of the Y plane */
1189  height = (height + 1) / 2;
1190  width = (width + 1) / 2;
1191  src_pitch = (src_pitch + 1) / 2;
1192  dst_pitch = (dst_pitch + 1) / 2;
1193  for (i = height; i--;) {
1194  SDL_memcpy(dst, src, 2 * width);
1195  src = (const Uint8*)src + 2 * src_pitch;
1196  dst = (Uint8*)dst + 2 * dst_pitch;
1197  }
1198  }
1199 #endif
1200  }
1201  break;
1202  default:
1203  return SDL_SetError("Unknown FOURCC pixel format");
1204  }
1205  } else {
1206  const int bpp = SDL_BYTESPERPIXEL(src_format);
1207  width *= bpp;
1208  for (i = height; i--;) {
1209  SDL_memcpy(dst, src, width);
1210  src = (const Uint8*)src + src_pitch;
1211  dst = (Uint8*)dst + dst_pitch;
1212  }
1213  }
1214  return 0;
1215  }
1216 
1217  /* FOURCC to Any */
1218  if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
1219  /* FOURCC to ARGB8888 */
1220  if (dst_format == SDL_PIXELFORMAT_ARGB8888) {
1221  SDL_ConvertPixels_YUV_to_ARGB8888(width, height, src_format, src, dst, dst_pitch);
1222  return 0;
1223  }
1224  else /* FOURCC to not(ARGB8888) : need an intermediate conversion */
1225  {
1226  int ret;
1227  void *tmp = SDL_malloc(width * height * 4);
1228  if (tmp == NULL) {
1229  return -1;
1230  }
1231 
1232  /* convert src/FOURCC to tmp/ARGB8888 */
1233  SDL_ConvertPixels_YUV_to_ARGB8888(width, height, src_format, src, tmp, width * 4);
1234 
1235  /* convert tmp/ARGB8888 to dst/dst_format */
1236  ret = SDL_ConvertPixels(width, height, SDL_PIXELFORMAT_ARGB8888, tmp, width * 4, dst_format, dst, dst_pitch);
1237  SDL_free(tmp);
1238  return ret;
1239  }
1240  }
1241 
1242  /* Any to FOURCC */
1243  if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
1244  /* ARGB8888 to FOURCC */
1245  if (src_format == SDL_PIXELFORMAT_ARGB8888) {
1246  SDL_ConvertPixels_ARGB8888_to_YUV(width, height, src, src_pitch, dst_format, dst);
1247  return 0;
1248  }
1249  else /* not(ARGB8888) to FOURCC : need an intermediate conversion */
1250  {
1251  int ret;
1252  void *tmp = SDL_malloc(width * height * 4);
1253  if (tmp == NULL) {
1254  return -1;
1255  }
1256  /* convert src/src_format to tmp/ARGB8888 */
1257  ret = SDL_ConvertPixels(width, height, src_format, src, src_pitch, SDL_PIXELFORMAT_ARGB8888, tmp, width * 4);
1258  if (ret == -1) {
1259  SDL_free(tmp);
1260  return ret;
1261  }
1262  /* convert tmp/ARGB8888 to dst/FOURCC */
1263  SDL_ConvertPixels_ARGB8888_to_YUV(width, height, tmp, width * 4, dst_format, dst);
1264 
1265  SDL_free(tmp);
1266  return 0;
1267  }
1268  }
1269 
1270  if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
1271  src_pitch,
1272  &src_surface, &src_fmt, &src_blitmap)) {
1273  return -1;
1274  }
1275  if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
1276  &dst_surface, &dst_fmt, &dst_blitmap)) {
1277  return -1;
1278  }
1279 
1280  /* Set up the rect and go! */
1281  rect.x = 0;
1282  rect.y = 0;
1283  rect.w = width;
1284  rect.h = height;
1285  return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
1286 }
static int SDL_ConvertPixels_YUV_to_ARGB8888(int width, int height, Uint32 src_format, const void *src, void *dst, int dst_pitch)
Definition: SDL_surface.c:1355
GLenum GLenum dst
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:1085
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_BYTESPERPIXEL(X)
Definition: SDL_pixels.h:128
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
GLenum src
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:559
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
#define SDL_memcpy
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_free
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.
Definition: SDL_surface.c:1123
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:50
#define NULL
Definition: begin_code.h:164
#define SDL_SetError
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
int h
Definition: SDL_rect.h:67
#define SDL_malloc
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:167
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
static int SDL_ConvertPixels_ARGB8888_to_YUV(int width, int height, const void *src, int src_pitch, Uint32 dst_format, void *dst)
Definition: SDL_surface.c:1537

◆ SDL_ConvertPixels_ARGB8888_to_YUV()

static int SDL_ConvertPixels_ARGB8888_to_YUV ( int  width,
int  height,
const void src,
int  src_pitch,
Uint32  dst_format,
void dst 
)
static

Definition at line 1537 of file SDL_surface.c.

References G, i, j, MAKE_U, MAKE_V, MAKE_Y, READ_1x1_PIXEL, READ_1x2_PIXELS, READ_2x1_PIXELS, READ_2x2_PIXELS, READ_ONE_RGB_PIXEL, READ_TWO_RGB_PIXELS, SDL_PIXELFORMAT_IYUV, SDL_PIXELFORMAT_NV12, SDL_PIXELFORMAT_NV21, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_YUY2, SDL_PIXELFORMAT_YV12, and SDL_PIXELFORMAT_YVYU.

Referenced by SDL_ConvertPixels().

1538 {
1539  const int src_pitch_x_2 = src_pitch * 2;
1540  const int sz_plane = width * height;
1541  const int sz_plane_chroma = ((width + 1) / 2) * ((height + 1) / 2);
1542  const int height_half = height / 2;
1543  const int height_remainder = (height & 0x1);
1544  const int width_half = width / 2;
1545  const int width_remainder = (width & 0x1);
1546  int i, j;
1547 
1548  // SDL_Log("SDL_ConvertPixels_ARGB8888_to_YUV (to %s)", SDL_GetPixelFormatName(dst_format));
1549 
1550  switch (dst_format)
1551  {
1552  case SDL_PIXELFORMAT_YV12:
1553  case SDL_PIXELFORMAT_IYUV:
1554  case SDL_PIXELFORMAT_NV12:
1555  case SDL_PIXELFORMAT_NV21:
1556  {
1557  const Uint8 *curr_row, *next_row;
1558 
1559  Uint8 *plane_y = (Uint8*) dst;
1560  Uint8 *plane_u = (dst_format == SDL_PIXELFORMAT_YV12 ? plane_y + sz_plane + sz_plane_chroma : plane_y + sz_plane);
1561  Uint8 *plane_v = (dst_format == SDL_PIXELFORMAT_YV12 ? plane_y + sz_plane : plane_y + sz_plane + sz_plane_chroma);
1562  Uint8 *plane_interleaved_uv = plane_y + sz_plane;
1563 
1564  curr_row = (const Uint8*)src;
1565 
1566  /* Write Y plane */
1567  for (j = 0; j < height; j++) {
1568  for (i = 0; i < width; i++) {
1569  const Uint8 b = curr_row[4 * i + 0];
1570  const Uint8 g = curr_row[4 * i + 1];
1571  const Uint8 r = curr_row[4 * i + 2];
1572  *plane_y++ = MAKE_Y(r, g, b);
1573  }
1574  curr_row += src_pitch;
1575  }
1576 
1577  curr_row = (const Uint8*)src;
1578  next_row = (const Uint8*)src;
1579  next_row += src_pitch;
1580 
1581 #if 1
1582 /* slightly faster */
1583 #define READ_2x2_PIXELS \
1584  const Uint32 p1 = ((const Uint32 *)curr_row)[2 * i]; \
1585  const Uint32 p2 = ((const Uint32 *)curr_row)[2 * i + 1]; \
1586  const Uint32 p3 = ((const Uint32 *)next_row)[2 * i]; \
1587  const Uint32 p4 = ((const Uint32 *)next_row)[2 * i + 1]; \
1588  const Uint32 b = ((p1 & 0x000000ff) + (p2 & 0x000000ff) + (p3 & 0x000000ff) + (p4 & 0x000000ff)) >> 2; \
1589  const Uint32 g = ((p1 & 0x0000ff00) + (p2 & 0x0000ff00) + (p3 & 0x0000ff00) + (p4 & 0x0000ff00)) >> 10; \
1590  const Uint32 r = ((p1 & 0x00ff0000) + (p2 & 0x00ff0000) + (p3 & 0x00ff0000) + (p4 & 0x00ff0000)) >> 18; \
1591 
1592 #else
1593 
1594 #define READ_2x2_PIXELS \
1595  const Uint8 b = (curr_row[8 * i + 0] + curr_row[8 * i + 4] \
1596  + next_row[8 * i + 0] + next_row[8 * i + 4] ) >> 2; \
1597  const Uint8 g = (curr_row[8 * i + 1] + curr_row[8 * i + 5] \
1598  + next_row[8 * i + 1] + next_row[8 * i + 5] ) >> 2; \
1599  const Uint8 r = (curr_row[8 * i + 2] + curr_row[8 * i + 6] \
1600  + next_row[8 * i + 2] + next_row[8 * i + 6] ) >> 2; \
1601 
1602 #endif
1603 
1604 #define READ_2x1_PIXELS \
1605  const Uint8 b = (curr_row[8 * i + 0] + next_row[8 * i + 0]) >> 1; \
1606  const Uint8 g = (curr_row[8 * i + 1] + next_row[8 * i + 1]) >> 1; \
1607  const Uint8 r = (curr_row[8 * i + 2] + next_row[8 * i + 2]) >> 1; \
1608 
1609 #define READ_1x2_PIXELS \
1610  const Uint8 b = (curr_row[8 * i + 0] + curr_row[8 * i + 4]) >> 1; \
1611  const Uint8 g = (curr_row[8 * i + 1] + curr_row[8 * i + 5]) >> 1; \
1612  const Uint8 r = (curr_row[8 * i + 2] + curr_row[8 * i + 6]) >> 1; \
1613 
1614 #define READ_1x1_PIXEL \
1615  const Uint8 b = curr_row[8 * i + 0]; \
1616  const Uint8 g = curr_row[8 * i + 1]; \
1617  const Uint8 r = curr_row[8 * i + 2]; \
1618 
1619  if (dst_format == SDL_PIXELFORMAT_YV12 || dst_format == SDL_PIXELFORMAT_IYUV)
1620  {
1621  /* Write UV planes, not interleaved */
1622  for (j = 0; j < height_half; j++) {
1623  for (i = 0; i < width_half; i++) {
1625  *plane_u++ = MAKE_U(r, g, b);
1626  *plane_v++ = MAKE_V(r, g, b);
1627  }
1628  if (width_remainder) {
1630  *plane_u++ = MAKE_U(r, g, b);
1631  *plane_v++ = MAKE_V(r, g, b);
1632  }
1633  curr_row += src_pitch_x_2;
1634  next_row += src_pitch_x_2;
1635  }
1636  if (height_remainder) {
1637  for (i = 0; i < width_half; i++) {
1639  *plane_u++ = MAKE_U(r, g, b);
1640  *plane_v++ = MAKE_V(r, g, b);
1641  }
1642  if (width_remainder) {
1644  *plane_u++ = MAKE_U(r, g, b);
1645  *plane_v++ = MAKE_V(r, g, b);
1646  }
1647  }
1648  }
1649  else if (dst_format == SDL_PIXELFORMAT_NV12)
1650  {
1651  for (j = 0; j < height_half; j++) {
1652  for (i = 0; i < width_half; i++) {
1654  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1655  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1656  }
1657  if (width_remainder) {
1659  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1660  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1661  }
1662  curr_row += src_pitch_x_2;
1663  next_row += src_pitch_x_2;
1664  }
1665  if (height_remainder) {
1666  for (i = 0; i < width_half; i++) {
1668  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1669  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1670  }
1671  if (width_remainder) {
1673  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1674  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1675  }
1676  }
1677  }
1678  else /* dst_format == SDL_PIXELFORMAT_NV21 */
1679  {
1680  for (j = 0; j < height_half; j++) {
1681  for (i = 0; i < width_half; i++) {
1683  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1684  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1685  }
1686  if (width_remainder) {
1688  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1689  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1690  }
1691  curr_row += src_pitch_x_2;
1692  next_row += src_pitch_x_2;
1693  }
1694  if (height_remainder) {
1695  for (i = 0; i < width_half; i++) {
1697  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1698  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1699  }
1700  if (width_remainder) {
1702  *plane_interleaved_uv++ = MAKE_V(r, g, b);
1703  *plane_interleaved_uv++ = MAKE_U(r, g, b);
1704  }
1705  }
1706  }
1707 #undef READ_2x2_PIXELS
1708 #undef READ_2x1_PIXELS
1709 #undef READ_1x2_PIXELS
1710 #undef READ_1x1_PIXEL
1711  }
1712  break;
1713 
1714  case SDL_PIXELFORMAT_YUY2:
1715  case SDL_PIXELFORMAT_UYVY:
1716  case SDL_PIXELFORMAT_YVYU:
1717  {
1718  const Uint8 *curr_row = (const Uint8*) src;
1719  Uint8 *plane = (Uint8*) dst;
1720 
1721 #define READ_TWO_RGB_PIXELS \
1722  const Uint8 b = curr_row[8 * i + 0]; \
1723  const Uint8 g = curr_row[8 * i + 1]; \
1724  const Uint8 r = curr_row[8 * i + 2]; \
1725  const Uint8 b1 = curr_row[8 * i + 4]; \
1726  const Uint8 g1 = curr_row[8 * i + 5]; \
1727  const Uint8 r1 = curr_row[8 * i + 6]; \
1728  const Uint8 B = (b + b1) >> 1; \
1729  const Uint8 G = (g + g1) >> 1; \
1730  const Uint8 R = (r + r1) >> 1; \
1731 
1732 #define READ_ONE_RGB_PIXEL \
1733  const Uint8 b = curr_row[8 * i + 0]; \
1734  const Uint8 g = curr_row[8 * i + 1]; \
1735  const Uint8 r = curr_row[8 * i + 2]; \
1736 
1737  /* Write YUV plane, packed */
1738  if (dst_format == SDL_PIXELFORMAT_YUY2)
1739  {
1740  for (j = 0; j < height; j++) {
1741  for (i = 0; i < width_half; i++) {
1743  /* Y U Y1 V */
1744  *plane++ = MAKE_Y(r, g, b);
1745  *plane++ = MAKE_U(R, G, B);
1746  *plane++ = MAKE_Y(r1, g1, b1);
1747  *plane++ = MAKE_V(R, G, B);
1748  }
1749  if (width_remainder) {
1751  /* Y U Y V */
1752  *plane++ = MAKE_Y(r, g, b);
1753  *plane++ = MAKE_U(r, g, b);
1754  *plane++ = MAKE_Y(r, g, b);
1755  *plane++ = MAKE_V(r, g, b);
1756  }
1757  curr_row += src_pitch;
1758  }
1759  }
1760  else if (dst_format == SDL_PIXELFORMAT_UYVY)
1761  {
1762  for (j = 0; j < height; j++) {
1763  for (i = 0; i < width_half; i++) {
1765  /* U Y V Y1 */
1766  *plane++ = MAKE_U(R, G, B);
1767  *plane++ = MAKE_Y(r, g, b);
1768  *plane++ = MAKE_V(R, G, B);
1769  *plane++ = MAKE_Y(r1, g1, b1);
1770  }
1771  if (width_remainder) {
1773  /* U Y V Y */
1774  *plane++ = MAKE_U(r, g, b);
1775  *plane++ = MAKE_Y(r, g, b);
1776  *plane++ = MAKE_V(r, g, b);
1777  *plane++ = MAKE_Y(r, g, b);
1778  }
1779  curr_row += src_pitch;
1780  }
1781  }
1782  else if (dst_format == SDL_PIXELFORMAT_YVYU)
1783  {
1784  for (j = 0; j < height; j++) {
1785  for (i = 0; i < width_half; i++) {
1787  /* Y V Y1 U */
1788  *plane++ = MAKE_Y(r, g, b);
1789  *plane++ = MAKE_V(R, G, B);
1790  *plane++ = MAKE_Y(r1, g1, b1);
1791  *plane++ = MAKE_U(R, G, B);
1792  }
1793  if (width_remainder) {
1795  /* Y V Y U */
1796  *plane++ = MAKE_Y(r, g, b);
1797  *plane++ = MAKE_V(r, g, b);
1798  *plane++ = MAKE_Y(r, g, b);
1799  *plane++ = MAKE_U(r, g, b);
1800  }
1801  curr_row += src_pitch;
1802  }
1803  }
1804 #undef READ_TWO_RGB_PIXELS
1805 #undef READ_ONE_RGB_PIXEL
1806  }
1807  break;
1808  }
1809  return 0;
1810 }
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
#define READ_TWO_RGB_PIXELS
GLenum GLenum dst
#define READ_2x2_PIXELS
#define READ_2x1_PIXELS
#define MAKE_U(r, g, b)
Definition: SDL_surface.c:1333
#define MAKE_Y(r, g, b)
Definition: SDL_surface.c:1332
GLenum src
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
#define READ_1x1_PIXEL
#define READ_ONE_RGB_PIXEL
#define READ_1x2_PIXELS
uint8_t Uint8
Definition: SDL_stdinc.h:157
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define G(x, y, z)
Definition: SDL_test_md5.c:74
#define MAKE_V(r, g, b)
Definition: SDL_surface.c:1334
GLboolean GLboolean g
GLboolean GLboolean GLboolean b

◆ SDL_ConvertPixels_YUV_to_ARGB8888()

static int SDL_ConvertPixels_YUV_to_ARGB8888 ( int  width,
int  height,
Uint32  src_format,
const void src,
void dst,
int  dst_pitch 
)
static

Definition at line 1355 of file SDL_surface.c.

References i, j, READ_PACKED_YUV, SDL_PIXELFORMAT_IYUV, SDL_PIXELFORMAT_NV12, SDL_PIXELFORMAT_NV21, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_YUY2, SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_YVYU, void, and WRITE_RGB_PIXEL.

Referenced by SDL_ConvertPixels().

1358 {
1359  const int sz_plane = width * height;
1360  const int sz_plane_chroma = ((width + 1) / 2) * ((height + 1) / 2);
1361  const int width_remainder = (width & 0x1);
1362  const int width_half = width / 2;
1363  const int curr_row_padding = dst_pitch - 4 * width;
1364  int i, j;
1365  Uint8 *curr_row = (Uint8*)dst;
1366 
1367  // SDL_Log("SDL_ConvertPixels_YUV_to_ARGB8888 (from %s)", SDL_GetPixelFormatName(src_format));
1368 
1369 #define WRITE_RGB_PIXEL(y, u, v) \
1370  *((Uint32*)curr_row) = \
1371  (MAKE_B((y), (u), (v)) \
1372  | (MAKE_G((y), (u), (v)) << 8) \
1373  | (MAKE_R((y), (u), (v)) << 16) \
1374  | 0xff000000); \
1375  curr_row += 4; \
1376 
1377  switch (src_format)
1378  {
1379  case SDL_PIXELFORMAT_YV12:
1380  case SDL_PIXELFORMAT_IYUV:
1381  case SDL_PIXELFORMAT_NV12:
1382  case SDL_PIXELFORMAT_NV21:
1383  {
1384  const Uint8 *plane_y = (const Uint8*)src;
1385 
1386  if (src_format == SDL_PIXELFORMAT_YV12 || src_format == SDL_PIXELFORMAT_IYUV)
1387  {
1388  const Uint8 *plane_u = (src_format == SDL_PIXELFORMAT_YV12 ? plane_y + sz_plane + sz_plane_chroma : plane_y + sz_plane);
1389  const Uint8 *plane_v = (src_format == SDL_PIXELFORMAT_YV12 ? plane_y + sz_plane : plane_y + sz_plane + sz_plane_chroma);
1390 
1391  for (j = 0; j < height; j++) {
1392  for (i = 0; i < width_half; i++) {
1393  const Uint8 u = *plane_u++;
1394  const Uint8 v = *plane_v++;
1395  const Uint8 y = *plane_y++;
1396  const Uint8 y1 = *plane_y++;
1397  WRITE_RGB_PIXEL(y, u, v);
1398  WRITE_RGB_PIXEL(y1, u, v);
1399  }
1400  if (width_remainder) {
1401  const Uint8 u = *plane_u++;
1402  const Uint8 v = *plane_v++;
1403  const Uint8 y = *plane_y++;
1404  WRITE_RGB_PIXEL(y, u, v);
1405  }
1406  /* Re-use the same line of chroma planes */
1407  if ((j & 0x1) == 0x0) {
1408  plane_u -= width_half + width_remainder;
1409  plane_v -= width_half + width_remainder;
1410  }
1411  curr_row += curr_row_padding;
1412  }
1413  }
1414  else if (src_format == SDL_PIXELFORMAT_NV12)
1415  {
1416  const Uint8 *plane_interleaved_uv = plane_y + sz_plane;
1417  for (j = 0; j < height; j++) {
1418  for (i = 0; i < width_half; i++) {
1419  const Uint8 y = *plane_y++;
1420  const Uint8 y1 = *plane_y++;
1421  const Uint8 u = *plane_interleaved_uv++;
1422  const Uint8 v = *plane_interleaved_uv++;
1423  WRITE_RGB_PIXEL(y, u, v);
1424  WRITE_RGB_PIXEL(y1, u, v);
1425  }
1426  if (width_remainder) {
1427  const Uint8 y = *plane_y++;
1428  const Uint8 u = *plane_interleaved_uv++;
1429  const Uint8 v = *plane_interleaved_uv++;
1430  WRITE_RGB_PIXEL(y, u, v);
1431  }
1432  /* Re-use the same line of chroma planes */
1433  if ((j & 0x1) == 0x0) {
1434  plane_interleaved_uv -= 2 * (width_half + width_remainder);
1435  }
1436  curr_row += curr_row_padding;
1437  }
1438  }
1439  else /* src_format == SDL_PIXELFORMAT_NV21 */
1440  {
1441  const Uint8 *plane_interleaved_uv = plane_y + sz_plane;
1442  for (j = 0; j < height; j++) {
1443  for (i = 0; i < width_half; i++) {
1444  const Uint8 y = *plane_y++;
1445  const Uint8 y1 = *plane_y++;
1446  const Uint8 v = *plane_interleaved_uv++;
1447  const Uint8 u = *plane_interleaved_uv++;
1448  WRITE_RGB_PIXEL(y, u, v);
1449  WRITE_RGB_PIXEL(y1, u, v);
1450  }
1451  if (width_remainder) {
1452  const Uint8 y = *plane_y++;
1453  const Uint8 v = *plane_interleaved_uv++;
1454  const Uint8 u = *plane_interleaved_uv++;
1455  WRITE_RGB_PIXEL(y, u, v);
1456  }
1457  /* Re-use the same line of chroma planes */
1458  if ((j & 0x1) == 0x0) {
1459  plane_interleaved_uv -= 2 * (width_half + width_remainder);
1460  }
1461  curr_row += curr_row_padding;
1462  }
1463  }
1464  }
1465  break;
1466 
1467  case SDL_PIXELFORMAT_YUY2:
1468  case SDL_PIXELFORMAT_UYVY:
1469  case SDL_PIXELFORMAT_YVYU:
1470  {
1471  const Uint8 *plane = (const Uint8 *)src;
1472 
1473 #define READ_PACKED_YUV(var1, var2, var3, var4) \
1474  const Uint8 var1 = plane[0]; \
1475  const Uint8 var2 = plane[1]; \
1476  const Uint8 var3 = plane[2]; \
1477  const Uint8 var4 = plane[3]; \
1478  plane += 4; \
1479 
1480  if (src_format == SDL_PIXELFORMAT_YUY2) /* Y U Y1 V */
1481  {
1482  for (j = 0; j < height; j++) {
1483  for (i = 0; i < width_half; i++) {
1484  READ_PACKED_YUV(y, u, y1, v);
1485  WRITE_RGB_PIXEL(y, u, v);
1486  WRITE_RGB_PIXEL(y1, u, v);
1487  }
1488  if (width_remainder) {
1489  READ_PACKED_YUV(y, u, y1, v);
1490  (void)y1; /* y1 unused */
1491  WRITE_RGB_PIXEL(y, u, v);
1492  }
1493  curr_row += curr_row_padding;
1494  }
1495  }
1496  else if (src_format == SDL_PIXELFORMAT_UYVY) /* U Y V Y1 */
1497  {
1498  for (j = 0; j < height; j++) {
1499  for (i = 0; i < width_half; i++) {
1500  READ_PACKED_YUV(u, y, v, y1);
1501  WRITE_RGB_PIXEL(y, u, v);
1502  WRITE_RGB_PIXEL(y1, u, v);
1503  }
1504  if (width_remainder) {
1505  READ_PACKED_YUV(u, y, v, y1);
1506  (void) y1; /* y1 unused */
1507  WRITE_RGB_PIXEL(y, u, v);
1508  }
1509  curr_row += curr_row_padding;
1510  }
1511  }
1512  else if (src_format == SDL_PIXELFORMAT_YVYU) /* Y V Y1 U */
1513  {
1514  for (j = 0; j < height; j++) {
1515  for (i = 0; i < width_half; i++) {
1516  READ_PACKED_YUV(y, v, y1, u);
1517  WRITE_RGB_PIXEL(y, u, v);
1518  WRITE_RGB_PIXEL(y1, u, v);
1519  }
1520  if (width_remainder) {
1521  READ_PACKED_YUV(y, v, y1, u);
1522  (void) y1; /* y1 unused */
1523  WRITE_RGB_PIXEL(y, u, v);
1524  }
1525  curr_row += curr_row_padding;
1526  }
1527  }
1528 #undef READ_PACKED_YUV
1529  }
1530  break;
1531  }
1532 #undef WRITE_RGB_PIXEL
1533  return 0;
1534 }
GLuint GLfloat GLfloat GLfloat x1
const GLdouble * v
Definition: SDL_opengl.h:2064
GLenum GLenum dst
#define WRITE_RGB_PIXEL(y, u, v)
#define READ_PACKED_YUV(var1, var2, var3, var4)
GLenum src
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLfixed y1
uint8_t Uint8
Definition: SDL_stdinc.h:157
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
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
GLuint GLfloat x0

◆ SDL_ConvertSurface()

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 910 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_PixelFormat::BytesPerPixel, 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_Surface::pixels, 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_FillRect, SDL_FreeSurface(), SDL_InvalidateMap(), SDL_InvalidParamError, SDL_LowerBlit(), SDL_memcmp, SDL_memcpy, SDL_RLEACCEL, SDL_SetClipRect(), SDL_SetColorKey(), SDL_SetError, SDL_SetSurfaceBlendMode(), SDL_SetSurfacePalette(), SDL_SetSurfaceRLE(), SDL_TRUE, SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_ConvertSurfaceFormat(), and SDL_DuplicateSurface().

912 {
913  SDL_Surface *convert;
914  Uint32 copy_flags;
915  SDL_Color copy_color;
916  SDL_Rect bounds;
917 
918  if (!surface) {
919  SDL_InvalidParamError("surface");
920  return NULL;
921  }
922  if (!format) {
923  SDL_InvalidParamError("format");
924  return NULL;
925  }
926 
927  /* Check for empty destination palette! (results in empty image) */
928  if (format->palette != NULL) {
929  int i;
930  for (i = 0; i < format->palette->ncolors; ++i) {
931  if ((format->palette->colors[i].r != 0xFF) ||
932  (format->palette->colors[i].g != 0xFF) ||
933  (format->palette->colors[i].b != 0xFF))
934  break;
935  }
936  if (i == format->palette->ncolors) {
937  SDL_SetError("Empty destination palette");
938  return (NULL);
939  }
940  }
941 
942  /* Create a new surface with the desired format */
943  convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
944  format->BitsPerPixel, format->Rmask,
945  format->Gmask, format->Bmask,
946  format->Amask);
947  if (convert == NULL) {
948  return (NULL);
949  }
950 
951  /* Copy the palette if any */
952  if (format->palette && convert->format->palette) {
953  SDL_memcpy(convert->format->palette->colors,
954  format->palette->colors,
955  format->palette->ncolors * sizeof(SDL_Color));
956  convert->format->palette->ncolors = format->palette->ncolors;
957  }
958 
959  /* Save the original copy flags */
960  copy_flags = surface->map->info.flags;
961  copy_color.r = surface->map->info.r;
962  copy_color.g = surface->map->info.g;
963  copy_color.b = surface->map->info.b;
964  copy_color.a = surface->map->info.a;
965  surface->map->info.r = 0xFF;
966  surface->map->info.g = 0xFF;
967  surface->map->info.b = 0xFF;
968  surface->map->info.a = 0xFF;
969  surface->map->info.flags = 0;
970  SDL_InvalidateMap(surface->map);
971 
972  /* Copy over the image data */
973  bounds.x = 0;
974  bounds.y = 0;
975  bounds.w = surface->w;
976  bounds.h = surface->h;
977  SDL_LowerBlit(surface, &bounds, convert, &bounds);
978 
979  /* Clean up the original surface, and update converted surface */
980  convert->map->info.r = copy_color.r;
981  convert->map->info.g = copy_color.g;
982  convert->map->info.b = copy_color.b;
983  convert->map->info.a = copy_color.a;
984  convert->map->info.flags =
985  (copy_flags &
989  surface->map->info.r = copy_color.r;
990  surface->map->info.g = copy_color.g;
991  surface->map->info.b = copy_color.b;
992  surface->map->info.a = copy_color.a;
993  surface->map->info.flags = copy_flags;
994  SDL_InvalidateMap(surface->map);
995  if (copy_flags & SDL_COPY_COLORKEY) {
996  SDL_bool set_colorkey_by_color = SDL_FALSE;
997 
998  if (surface->format->palette) {
999  if (format->palette &&
1000  surface->format->palette->ncolors <= format->palette->ncolors &&
1001  (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
1002  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
1003  /* The palette is identical, just set the same colorkey */
1004  SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
1005  } else if (format->Amask) {
1006  /* The alpha was set in the destination from the palette */
1007  } else {
1008  set_colorkey_by_color = SDL_TRUE;
1009  }
1010  } else {
1011  set_colorkey_by_color = SDL_TRUE;
1012  }
1013 
1014  if (set_colorkey_by_color) {
1015  SDL_Surface *tmp;
1016  SDL_Surface *tmp2;
1017  int converted_colorkey = 0;
1018 
1019  /* Create a dummy surface to get the colorkey converted */
1020  tmp = SDL_CreateRGBSurface(0, 1, 1,
1021  surface->format->BitsPerPixel, surface->format->Rmask,
1022  surface->format->Gmask, surface->format->Bmask,
1023  surface->format->Amask);
1024 
1025  /* Share the palette, if any */
1026  if (surface->format->palette) {
1027  SDL_SetSurfacePalette(tmp, surface->format->palette);
1028  }
1029 
1030  SDL_FillRect(tmp, NULL, surface->map->info.colorkey);
1031 
1032  tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
1033 
1034  /* Convertion of the colorkey */
1035  tmp2 = SDL_ConvertSurface(tmp, format, 0);
1036 
1037  /* Get the converted colorkey */
1038  SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel);
1039 
1040  SDL_FreeSurface(tmp);
1041  SDL_FreeSurface(tmp2);
1042 
1043  /* Set the converted colorkey on the new surface */
1044  SDL_SetColorKey(convert, 1, converted_colorkey);
1045 
1046  /* This is needed when converting for 3D texture upload */
1047  SDL_ConvertColorkeyToAlpha(convert);
1048  }
1049  }
1050  SDL_SetClipRect(convert, &surface->clip_rect);
1051 
1052  /* Enable alpha blending by default if the new surface has an
1053  * alpha channel or alpha modulation */
1054  if ((surface->format->Amask && format->Amask) ||
1055  (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
1057  }
1058  if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
1059  SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
1060  }
1061 
1062  /* We're ready to go! */
1063  return (convert);
1064 }
Uint8 r
Definition: SDL_blit.h:70
Uint8 b
Definition: SDL_blit.h:70
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:516
#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:215
Uint8 g
Definition: SDL_pixels.h:296
Uint8 BytesPerPixel
Definition: SDL_pixels.h:318
Uint8 g
Definition: SDL_blit.h:70
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:910
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:450
static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
Definition: SDL_surface.c:299
Uint8 b
Definition: SDL_pixels.h:297
#define SDL_COPY_RLE_COLORKEY
Definition: SDL_blit.h:42
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:559
int SDL_SetColorKey(SDL_Surface *surface, int flag, Uint32 key)
Sets the color key (transparent pixel) in a blittable surface.
Definition: SDL_surface.c:236
Uint32 colorkey
Definition: SDL_blit.h:69
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
#define SDL_memcpy
Uint8 r
Definition: SDL_pixels.h:295
void * pixels
Definition: SDL_surface.h:75
Uint8 a
Definition: SDL_pixels.h:298
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:201
#define SDL_memcmp
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
SDL_Rect clip_rect
Definition: SDL_surface.h:85
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1292
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
SDL_Color * colors
Definition: SDL_pixels.h:305
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:138
GLbitfield flags
#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
#define SDL_FillRect
SDL_Palette * palette
Definition: SDL_pixels.h:316
int y
Definition: SDL_rect.h:66
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitInfo info
Definition: SDL_blit.h:92
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_ConvertSurfaceFormat()

SDL_Surface* SDL_ConvertSurfaceFormat ( SDL_Surface surface,
Uint32  pixel_format,
Uint32  flags 
)

Definition at line 1067 of file SDL_surface.c.

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

1069 {
1070  SDL_PixelFormat *fmt;
1071  SDL_Surface *convert = NULL;
1072 
1074  if (fmt) {
1075  convert = SDL_ConvertSurface(surface, fmt, flags);
1076  SDL_FreeFormat(fmt);
1077  }
1078  return convert;
1079 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:910
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:164
GLbitfield flags
Uint32 pixel_format
Definition: testoverlay2.c:152

◆ SDL_CreateRGBSurface()

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 138 of file SDL_surface.c.

References NULL, SDL_CreateRGBSurfaceWithFormat(), SDL_MasksToPixelFormatEnum, SDL_PIXELFORMAT_UNKNOWN, and SDL_SetError.

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceFrom().

141 {
142  Uint32 format;
143 
144  /* Get the pixel format */
145  format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
146  if (format == SDL_PIXELFORMAT_UNKNOWN) {
147  SDL_SetError("Unknown pixel format");
148  return NULL;
149  }
150 
152 }
#define SDL_MasksToPixelFormatEnum
uint32_t Uint32
Definition: SDL_stdinc.h:181
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
SDL_Surface * SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format)
Definition: SDL_surface.c:51
#define NULL
Definition: begin_code.h:164
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1572
#define SDL_SetError
GLbitfield flags
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572

◆ SDL_CreateRGBSurfaceFrom()

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 158 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.

162 {
164 
165  surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
166  if (surface != NULL) {
167  surface->flags |= SDL_PREALLOC;
168  surface->pixels = pixels;
169  surface->w = width;
170  surface->h = height;
171  surface->pitch = pitch;
172  SDL_SetClipRect(surface, NULL);
173  }
174  return surface;
175 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:516
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
Uint32 flags
Definition: SDL_surface.h:71
void * pixels
Definition: SDL_surface.h:75
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define NULL
Definition: begin_code.h:164
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1572
SDL_Surface * SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
Definition: SDL_surface.c:138
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_PREALLOC
Definition: SDL_surface.h:53

◆ SDL_CreateRGBSurfaceWithFormat()

SDL_Surface* SDL_CreateRGBSurfaceWithFormat ( Uint32  flags,
int  width,
int  height,
int  depth,
Uint32  format 
)

Definition at line 51 of file SDL_surface.c.

References SDL_PixelFormat::Amask, 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_MAX_SINT32, SDL_memset, SDL_OutOfMemory, SDL_SetClipRect(), SDL_SetSurfaceBlendMode(), SDL_SetSurfacePalette(), void, and SDL_Surface::w.

Referenced by SDL_CreateRGBSurface(), and SDL_CreateRGBSurfaceWithFormatFrom().

53 {
55 
56  /* The flags are no longer used, make the compiler happy */
57  (void)flags;
58 
59  /* Allocate the surface */
60  surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
61  if (surface == NULL) {
63  return NULL;
64  }
65 
66  surface->format = SDL_AllocFormat(format);
67  if (!surface->format) {
68  SDL_FreeSurface(surface);
69  return NULL;
70  }
71  surface->w = width;
72  surface->h = height;
73  surface->pitch = SDL_CalculatePitch(surface);
74  SDL_SetClipRect(surface, NULL);
75 
76  if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
77  SDL_Palette *palette =
78  SDL_AllocPalette((1 << surface->format->BitsPerPixel));
79  if (!palette) {
80  SDL_FreeSurface(surface);
81  return NULL;
82  }
83  if (palette->ncolors == 2) {
84  /* Create a black and white bitmap palette */
85  palette->colors[0].r = 0xFF;
86  palette->colors[0].g = 0xFF;
87  palette->colors[0].b = 0xFF;
88  palette->colors[1].r = 0x00;
89  palette->colors[1].g = 0x00;
90  palette->colors[1].b = 0x00;
91  }
92  SDL_SetSurfacePalette(surface, palette);
93  SDL_FreePalette(palette);
94  }
95 
96  /* Get the pixels */
97  if (surface->w && surface->h) {
98  /* Assumptions checked in surface_size_assumptions assert above */
99  Sint64 size = ((Sint64)surface->h * surface->pitch);
101  /* Overflow... */
102  SDL_FreeSurface(surface);
103  SDL_OutOfMemory();
104  return NULL;
105  }
106 
107  surface->pixels = SDL_malloc((size_t)size);
108  if (!surface->pixels) {
109  SDL_FreeSurface(surface);
110  SDL_OutOfMemory();
111  return NULL;
112  }
113  /* This is important for bitmaps */
114  SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
115  }
116 
117  /* Allocate an empty mapping */
118  surface->map = SDL_AllocBlitMap();
119  if (!surface->map) {
120  SDL_FreeSurface(surface);
121  return NULL;
122  }
123 
124  /* By default surface with an alpha mask are set up for blending */
125  if (surface->format->Amask) {
127  }
128 
129  /* The surface is ready to go */
130  surface->refcount = 1;
131  return surface;
132 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:516
Uint8 g
Definition: SDL_pixels.h:296
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
EGLSurface surface
Definition: eglext.h:248
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:450
Uint8 b
Definition: SDL_pixels.h:297
#define SDL_AllocFormat
#define SDL_MAX_SINT32
A signed 32-bit integer type.
Definition: SDL_stdinc.h:173
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
Uint8 r
Definition: SDL_pixels.h:295
void * pixels
Definition: SDL_surface.h:75
Uint8 BitsPerPixel
Definition: SDL_pixels.h:317
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:201
GLsizeiptr size
#define SDL_AllocPalette
void SDL_FreeSurface(SDL_Surface *surface)
Definition: SDL_surface.c:1292
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_FreePalette
GLbitfield flags
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_calloc
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:961
int SDL_CalculatePitch(SDL_Surface *surface)
Definition: SDL_pixels.c:755
#define SDL_malloc
int64_t Sint64
Definition: SDL_stdinc.h:188
#define SDL_memset

◆ SDL_CreateRGBSurfaceWithFormatFrom()

SDL_Surface* SDL_CreateRGBSurfaceWithFormatFrom ( void pixels,
int  width,
int  height,
int  depth,
int  pitch,
Uint32  format 
)

Definition at line 182 of file SDL_surface.c.

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

185 {
187 
188  surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
189  if (surface != NULL) {
190  surface->flags |= SDL_PREALLOC;
191  surface->pixels = pixels;
192  surface->w = width;
193  surface->h = height;
194  surface->pitch = pitch;
195  SDL_SetClipRect(surface, NULL);
196  }
197  return surface;
198 }
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
Definition: SDL_surface.c:516
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
Uint32 flags
Definition: SDL_surface.h:71
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
SDL_Surface * SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth, Uint32 format)
Definition: SDL_surface.c:51
void * pixels
Definition: SDL_surface.h:75
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define NULL
Definition: begin_code.h:164
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
#define SDL_PREALLOC
Definition: SDL_surface.h:53

◆ SDL_CreateSurfaceOnStack()

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 
)
static

Definition at line 1085 of file SDL_surface.c.

References SDL_BlitInfo::a, SDL_BlitInfo::b, SDL_Surface::flags, SDL_Surface::format, SDL_BlitInfo::g, SDL_Surface::h, SDL_BlitMap::info, SDL_Surface::map, SDL_Surface::pitch, SDL_Surface::pixels, SDL_BlitInfo::r, SDL_Surface::refcount, SDL_FALSE, SDL_InitFormat(), SDL_ISPIXELFORMAT_INDEXED, SDL_PREALLOC, SDL_SetError, SDL_TRUE, SDL_zerop, and SDL_Surface::w.

Referenced by SDL_ConvertPixels().

1088 {
1090  SDL_SetError("Indexed pixel formats not supported");
1091  return SDL_FALSE;
1092  }
1093  if (SDL_InitFormat(format, pixel_format) < 0) {
1094  return SDL_FALSE;
1095  }
1096 
1097  SDL_zerop(surface);
1098  surface->flags = SDL_PREALLOC;
1099  surface->format = format;
1100  surface->pixels = pixels;
1101  surface->w = width;
1102  surface->h = height;
1103  surface->pitch = pitch;
1104  /* We don't actually need to set up the clip rect for our purposes */
1105  /* SDL_SetClipRect(surface, NULL); */
1106 
1107  /* Allocate an empty mapping */
1108  SDL_zerop(blitmap);
1109  blitmap->info.r = 0xFF;
1110  blitmap->info.g = 0xFF;
1111  blitmap->info.b = 0xFF;
1112  blitmap->info.a = 0xFF;
1113  surface->map = blitmap;
1114 
1115  /* The surface is ready to go */
1116  surface->refcount = 1;
1117  return SDL_TRUE;
1118 }
Uint8 r
Definition: SDL_blit.h:70
Uint8 b
Definition: SDL_blit.h:70
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
Uint8 g
Definition: SDL_blit.h:70
#define SDL_zerop(x)
Definition: SDL_stdinc.h:417
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
Uint32 flags
Definition: SDL_surface.h:71
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format)
Definition: SDL_pixels.c:528
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
Uint32 pixel_format
Definition: testoverlay2.c:152
SDL_BlitInfo info
Definition: SDL_blit.h:92
#define SDL_PREALLOC
Definition: SDL_surface.h:53
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_DuplicateSurface()

SDL_Surface* SDL_DuplicateSurface ( SDL_Surface surface)

Definition at line 901 of file SDL_surface.c.

References SDL_Surface::flags, SDL_Surface::format, and SDL_ConvertSurface().

902 {
903  return SDL_ConvertSurface(surface, surface->format, surface->flags);
904 }
SDL_Surface * SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format, Uint32 flags)
Definition: SDL_surface.c:910
Uint32 flags
Definition: SDL_surface.h:71
SDL_PixelFormat * format
Definition: SDL_surface.h:72

◆ SDL_FreeSurface()

void SDL_FreeSurface ( SDL_Surface surface)

Definition at line 1292 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_InvalidateMap(), SDL_PREALLOC, SDL_RLEACCEL, SDL_SetSurfacePalette(), SDL_UnlockSurface(), and SDL_UnRLESurface().

Referenced by SDL_ConvertSurface(), and SDL_CreateRGBSurfaceWithFormat().

1293 {
1294  if (surface == NULL) {
1295  return;
1296  }
1297  if (surface->flags & SDL_DONTFREE) {
1298  return;
1299  }
1300  SDL_InvalidateMap(surface->map);
1301 
1302  if (--surface->refcount > 0) {
1303  return;
1304  }
1305  while (surface->locked > 0) {
1306  SDL_UnlockSurface(surface);
1307  }
1308  if (surface->flags & SDL_RLEACCEL) {
1309  SDL_UnRLESurface(surface, 0);
1310  }
1311  if (surface->format) {
1312  SDL_SetSurfacePalette(surface, NULL);
1313  SDL_FreeFormat(surface->format);
1314  surface->format = NULL;
1315  }
1316  if (!(surface->flags & SDL_PREALLOC)) {
1317  SDL_free(surface->pixels);
1318  }
1319  if (surface->map) {
1320  SDL_FreeBlitMap(surface->map);
1321  }
1322  SDL_free(surface);
1323 }
void SDL_UnlockSurface(SDL_Surface *surface)
Definition: SDL_surface.c:883
#define SDL_DONTFREE
Definition: SDL_surface.h:55
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 flags
Definition: SDL_surface.h:71
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
void * pixels
Definition: SDL_surface.h:75
#define SDL_free
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
Set the palette used by a surface.
Definition: SDL_surface.c:201
#define SDL_FreeFormat
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
void SDL_FreeBlitMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1086
#define SDL_PREALLOC
Definition: SDL_surface.h:53
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_GetClipRect()

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 540 of file SDL_surface.c.

References SDL_Surface::clip_rect.

541 {
542  if (surface && rect) {
543  *rect = surface->clip_rect;
544  }
545 }
SDL_Rect clip_rect
Definition: SDL_surface.h:85

◆ SDL_GetColorKey()

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 281 of file SDL_surface.c.

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

282 {
283  if (!surface) {
284  return -1;
285  }
286 
287  if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
288  return -1;
289  }
290 
291  if (key) {
292  *key = surface->map->info.colorkey;
293  }
294  return 0;
295 }
#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
GLuint64 key
Definition: gl2ext.h:2192
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_GetSurfaceAlphaMod()

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 437 of file SDL_surface.c.

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

438 {
439  if (!surface) {
440  return -1;
441  }
442 
443  if (alpha) {
444  *alpha = surface->map->info.a;
445  }
446  return 0;
447 }
GLfloat GLfloat GLfloat alpha
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_BlitInfo info
Definition: SDL_blit.h:92
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_GetSurfaceBlendMode()

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 487 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.

488 {
489  if (!surface) {
490  return -1;
491  }
492 
493  if (!blendMode) {
494  return 0;
495  }
496 
497  switch (surface->map->
498  info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
499  case SDL_COPY_BLEND:
501  break;
502  case SDL_COPY_ADD:
504  break;
505  case SDL_COPY_MOD:
507  break;
508  default:
510  break;
511  }
512  return 0;
513 }
#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

◆ SDL_GetSurfaceColorMod()

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 395 of file SDL_surface.c.

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

396 {
397  if (!surface) {
398  return -1;
399  }
400 
401  if (r) {
402  *r = surface->map->info.r;
403  }
404  if (g) {
405  *g = surface->map->info.g;
406  }
407  if (b) {
408  *b = surface->map->info.b;
409  }
410  return 0;
411 }
Uint8 r
Definition: SDL_blit.h:70
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Uint8 b
Definition: SDL_blit.h:70
Uint8 g
Definition: SDL_blit.h:70
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLboolean GLboolean g
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_LockSurface()

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 862 of file SDL_surface.c.

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

Referenced by SDL_ConvertColorkeyToAlpha().

863 {
864  if (!surface->locked) {
865  /* Perform the lock */
866  if (surface->flags & SDL_RLEACCEL) {
867  SDL_UnRLESurface(surface, 1);
868  surface->flags |= SDL_RLEACCEL; /* save accel'd state */
869  }
870  }
871 
872  /* Increment the surface lock count, for recursive locks */
873  ++surface->locked;
874 
875  /* Ready to go.. */
876  return (0);
877 }
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
Uint32 flags
Definition: SDL_surface.h:71
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_LowerBlit()

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

561 {
562  /* Check to make sure the blit mapping is valid */
563  if ((src->map->dst != dst) ||
564  (dst->format->palette &&
565  src->map->dst_palette_version != dst->format->palette->version) ||
566  (src->format->palette &&
567  src->map->src_palette_version != src->format->palette->version)) {
568  if (SDL_MapSurface(src, dst) < 0) {
569  return (-1);
570  }
571  /* just here for debugging */
572 /* printf */
573 /* ("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", */
574 /* src, dst->flags, src->map->info.flags, dst, dst->flags, */
575 /* dst->map->info.flags, src->map->blit); */
576  }
577  return (src->map->blit(src, srcrect, dst, dstrect));
578 }
Uint32 version
Definition: SDL_pixels.h:306
SDL_blit blit
Definition: SDL_blit.h:90
Uint32 dst_palette_version
Definition: SDL_blit.h:96
Uint32 src_palette_version
Definition: SDL_blit.h:97
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
int SDL_MapSurface(SDL_Surface *src, SDL_Surface *dst)
Definition: SDL_pixels.c:1000
SDL_Surface * dst
Definition: SDL_blit.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_Palette * palette
Definition: SDL_pixels.h:316

◆ SDL_LowerBlitScaled()

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

837 {
838  static const Uint32 complex_copy_flags = (
842  );
843 
844  if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
845  src->map->info.flags |= SDL_COPY_NEAREST;
846  SDL_InvalidateMap(src->map);
847  }
848 
849  if ( !(src->map->info.flags & complex_copy_flags) &&
850  src->format->format == dst->format->format &&
852  return SDL_SoftStretch( src, srcrect, dst, dstrect );
853  } else {
854  return SDL_LowerBlit( src, srcrect, dst, dstrect );
855  }
856 }
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:39
#define SDL_SoftStretch
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
uint32_t Uint32
Definition: SDL_stdinc.h:181
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:559
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
#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:92

◆ SDL_SetClipRect()

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 516 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_CreateRGBSurfaceFrom(), SDL_CreateRGBSurfaceWithFormat(), and SDL_CreateRGBSurfaceWithFormatFrom().

517 {
518  SDL_Rect full_rect;
519 
520  /* Don't do anything if there's no surface to act on */
521  if (!surface) {
522  return SDL_FALSE;
523  }
524 
525  /* Set up the full surface rectangle */
526  full_rect.x = 0;
527  full_rect.y = 0;
528  full_rect.w = surface->w;
529  full_rect.h = surface->h;
530 
531  /* Set the clipping rectangle */
532  if (!rect) {
533  surface->clip_rect = full_rect;
534  return SDL_TRUE;
535  }
536  return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
537 }
#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

◆ SDL_SetColorKey()

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

237 {
238  int flags;
239 
240  if (!surface) {
241  return SDL_InvalidParamError("surface");
242  }
243 
244  if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
245  return SDL_InvalidParamError("key");
246  }
247 
248  if (flag & SDL_RLEACCEL) {
249  SDL_SetSurfaceRLE(surface, 1);
250  }
251 
252  flags = surface->map->info.flags;
253  if (flag) {
254  surface->map->info.flags |= SDL_COPY_COLORKEY;
255  surface->map->info.colorkey = key;
256  if (surface->format->palette) {
257  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
258  ++surface->format->palette->version;
259  if (!surface->format->palette->version) {
260  surface->format->palette->version = 1;
261  }
262  }
263  } else {
264  if (surface->format->palette) {
265  surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
266  ++surface->format->palette->version;
267  if (!surface->format->palette->version) {
268  surface->format->palette->version = 1;
269  }
270  }
271  surface->map->info.flags &= ~SDL_COPY_COLORKEY;
272  }
273  if (surface->map->info.flags != flags) {
274  SDL_InvalidateMap(surface->map);
275  }
276 
277  return 0;
278 }
Uint32 version
Definition: SDL_pixels.h:306
#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:215
uint32_t Uint32
Definition: SDL_stdinc.h:181
#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:981
#define SDL_ALPHA_TRANSPARENT
Definition: SDL_pixels.h:47
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLuint64 key
Definition: gl2ext.h:2192
Uint8 a
Definition: SDL_pixels.h:298
SDL_Color * colors
Definition: SDL_pixels.h:305
SDL_PixelFormat * format
Definition: SDL_surface.h:72
GLbitfield flags
SDL_Palette * palette
Definition: SDL_pixels.h:316
#define SDL_ALPHA_OPAQUE
Definition: SDL_pixels.h:46
SDL_BlitInfo info
Definition: SDL_blit.h:92
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_SetSurfaceAlphaMod()

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

415 {
416  int flags;
417 
418  if (!surface) {
419  return -1;
420  }
421 
422  surface->map->info.a = alpha;
423 
424  flags = surface->map->info.flags;
425  if (alpha != 0xFF) {
426  surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
427  } else {
428  surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
429  }
430  if (surface->map->info.flags != flags) {
431  SDL_InvalidateMap(surface->map);
432  }
433  return 0;
434 }
GLfloat GLfloat GLfloat alpha
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
SDL_BlitInfo info
Definition: SDL_blit.h:92
Uint8 a
Definition: SDL_blit.h:70

◆ SDL_SetSurfaceBlendMode()

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

451 {
452  int flags, status;
453 
454  if (!surface) {
455  return -1;
456  }
457 
458  status = 0;
459  flags = surface->map->info.flags;
460  surface->map->info.flags &=
462  switch (blendMode) {
463  case SDL_BLENDMODE_NONE:
464  break;
465  case SDL_BLENDMODE_BLEND:
466  surface->map->info.flags |= SDL_COPY_BLEND;
467  break;
468  case SDL_BLENDMODE_ADD:
469  surface->map->info.flags |= SDL_COPY_ADD;
470  break;
471  case SDL_BLENDMODE_MOD:
472  surface->map->info.flags |= SDL_COPY_MOD;
473  break;
474  default:
475  status = SDL_Unsupported();
476  break;
477  }
478 
479  if (surface->map->info.flags != flags) {
480  SDL_InvalidateMap(surface->map);
481  }
482 
483  return status;
484 }
#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:981
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:92

◆ SDL_SetSurfaceColorMod()

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

370 {
371  int flags;
372 
373  if (!surface) {
374  return -1;
375  }
376 
377  surface->map->info.r = r;
378  surface->map->info.g = g;
379  surface->map->info.b = b;
380 
381  flags = surface->map->info.flags;
382  if (r != 0xFF || g != 0xFF || b != 0xFF) {
383  surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
384  } else {
385  surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
386  }
387  if (surface->map->info.flags != flags) {
388  SDL_InvalidateMap(surface->map);
389  }
390  return 0;
391 }
Uint8 r
Definition: SDL_blit.h:70
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
Uint8 b
Definition: SDL_blit.h:70
Uint8 g
Definition: SDL_blit.h:70
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
GLboolean GLboolean g
GLboolean GLboolean GLboolean b
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_SetSurfacePalette()

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 201 of file SDL_surface.c.

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

Referenced by SDL_ConvertSurface(), SDL_CreateRGBSurfaceWithFormat(), and SDL_FreeSurface().

202 {
203  if (!surface) {
204  return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
205  }
206  if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
207  return -1;
208  }
209  SDL_InvalidateMap(surface->map);
210 
211  return 0;
212 }
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_SetError
#define SDL_SetPixelFormatPalette

◆ SDL_SetSurfaceRLE()

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

216 {
217  int flags;
218 
219  if (!surface) {
220  return -1;
221  }
222 
223  flags = surface->map->info.flags;
224  if (flag) {
225  surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
226  } else {
227  surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
228  }
229  if (surface->map->info.flags != flags) {
230  SDL_InvalidateMap(surface->map);
231  }
232  return 0;
233 }
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:41
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLbitfield flags
SDL_BlitInfo info
Definition: SDL_blit.h:92

◆ SDL_UnlockSurface()

void SDL_UnlockSurface ( SDL_Surface surface)
See also
SDL_LockSurface()

Definition at line 883 of file SDL_surface.c.

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

Referenced by SDL_ConvertColorkeyToAlpha(), and SDL_FreeSurface().

884 {
885  /* Only perform an unlock if we are locked */
886  if (!surface->locked || (--surface->locked > 0)) {
887  return;
888  }
889 
890  /* Update RLE encoded surface with new data */
891  if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
892  surface->flags &= ~SDL_RLEACCEL; /* stop lying */
893  SDL_RLESurface(surface);
894  }
895 }
Uint32 flags
Definition: SDL_surface.h:71
int SDL_RLESurface(SDL_Surface *surface)
#define SDL_RLEACCEL
Definition: SDL_surface.h:54

◆ SDL_UpperBlit()

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 582 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.

584 {
585  SDL_Rect fulldst;
586  int srcx, srcy, w, h;
587 
588  /* Make sure the surfaces aren't locked */
589  if (!src || !dst) {
590  return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
591  }
592  if (src->locked || dst->locked) {
593  return SDL_SetError("Surfaces must not be locked during blit");
594  }
595 
596  /* If the destination rectangle is NULL, use the entire dest surface */
597  if (dstrect == NULL) {
598  fulldst.x = fulldst.y = 0;
599  fulldst.w = dst->w;
600  fulldst.h = dst->h;
601  dstrect = &fulldst;
602  }
603 
604  /* clip the source rectangle to the source surface */
605  if (srcrect) {
606  int maxw, maxh;
607 
608  srcx = srcrect->x;
609  w = srcrect->w;
610  if (srcx < 0) {
611  w += srcx;
612  dstrect->x -= srcx;
613  srcx = 0;
614  }
615  maxw = src->w - srcx;
616  if (maxw < w)
617  w = maxw;
618 
619  srcy = srcrect->y;
620  h = srcrect->h;
621  if (srcy < 0) {
622  h += srcy;
623  dstrect->y -= srcy;
624  srcy = 0;
625  }
626  maxh = src->h - srcy;
627  if (maxh < h)
628  h = maxh;
629 
630  } else {
631  srcx = srcy = 0;
632  w = src->w;
633  h = src->h;
634  }
635 
636  /* clip the destination rectangle against the clip rectangle */
637  {
638  SDL_Rect *clip = &dst->clip_rect;
639  int dx, dy;
640 
641  dx = clip->x - dstrect->x;
642  if (dx > 0) {
643  w -= dx;
644  dstrect->x += dx;
645  srcx += dx;
646  }
647  dx = dstrect->x + w - clip->x - clip->w;
648  if (dx > 0)
649  w -= dx;
650 
651  dy = clip->y - dstrect->y;
652  if (dy > 0) {
653  h -= dy;
654  dstrect->y += dy;
655  srcy += dy;
656  }
657  dy = dstrect->y + h - clip->y - clip->h;
658  if (dy > 0)
659  h -= dy;
660  }
661 
662  /* Switch back to a fast blit if we were previously stretching */
663  if (src->map->info.flags & SDL_COPY_NEAREST) {
664  src->map->info.flags &= ~SDL_COPY_NEAREST;
665  SDL_InvalidateMap(src->map);
666  }
667 
668  if (w > 0 && h > 0) {
669  SDL_Rect sr;
670  sr.x = srcx;
671  sr.y = srcy;
672  sr.w = dstrect->w = w;
673  sr.h = dstrect->h = h;
674  return SDL_LowerBlit(src, &sr, dst, dstrect);
675  }
676  dstrect->w = dstrect->h = 0;
677  return 0;
678 }
GLfloat GLfloat GLfloat GLfloat h
int SDL_LowerBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_surface.c:559
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:981
#define SDL_COPY_NEAREST
Definition: SDL_blit.h:40
struct SDL_BlitMap * map
Definition: SDL_surface.h:88
GLubyte GLubyte GLubyte GLubyte w
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:164
#define SDL_SetError
int h
Definition: SDL_rect.h:67
int y
Definition: SDL_rect.h:66
SDL_BlitInfo info
Definition: SDL_blit.h:92
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ SDL_UpperBlitScaled()

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 681 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.

683 {
684  double src_x0, src_y0, src_x1, src_y1;
685  double dst_x0, dst_y0, dst_x1, dst_y1;
686  SDL_Rect final_src, final_dst;
687  double scaling_w, scaling_h;
688  int src_w, src_h;
689  int dst_w, dst_h;
690 
691  /* Make sure the surfaces aren't locked */
692  if (!src || !dst) {
693  return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
694  }
695  if (src->locked || dst->locked) {
696  return SDL_SetError("Surfaces must not be locked during blit");
697  }
698 
699  if (NULL == srcrect) {
700  src_w = src->w;
701  src_h = src->h;
702  } else {
703  src_w = srcrect->w;
704  src_h = srcrect->h;
705  }
706 
707  if (NULL == dstrect) {
708  dst_w = dst->w;
709  dst_h = dst->h;
710  } else {
711  dst_w = dstrect->w;
712  dst_h = dstrect->h;
713  }
714 
715  if (dst_w == src_w && dst_h == src_h) {
716  /* No scaling, defer to regular blit */
717  return SDL_BlitSurface(src, srcrect, dst, dstrect);
718  }
719 
720  scaling_w = (double)dst_w / src_w;
721  scaling_h = (double)dst_h / src_h;
722 
723  if (NULL == dstrect) {
724  dst_x0 = 0;
725  dst_y0 = 0;
726  dst_x1 = dst_w - 1;
727  dst_y1 = dst_h - 1;
728  } else {
729  dst_x0 = dstrect->x;
730  dst_y0 = dstrect->y;
731  dst_x1 = dst_x0 + dst_w - 1;
732  dst_y1 = dst_y0 + dst_h - 1;
733  }
734 
735  if (NULL == srcrect) {
736  src_x0 = 0;
737  src_y0 = 0;
738  src_x1 = src_w - 1;
739  src_y1 = src_h - 1;
740  } else {
741  src_x0 = srcrect->x;
742  src_y0 = srcrect->y;
743  src_x1 = src_x0 + src_w - 1;
744  src_y1 = src_y0 + src_h - 1;
745 
746  /* Clip source rectangle to the source surface */
747 
748  if (src_x0 < 0) {
749  dst_x0 -= src_x0 * scaling_w;
750  src_x0 = 0;
751  }
752 
753  if (src_x1 >= src->w) {
754  dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
755  src_x1 = src->w - 1;
756  }
757 
758  if (src_y0 < 0) {
759  dst_y0 -= src_y0 * scaling_h;
760  src_y0 = 0;
761  }
762 
763  if (src_y1 >= src->h) {
764  dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
765  src_y1 = src->h - 1;
766  }
767  }
768 
769  /* Clip destination rectangle to the clip rectangle */
770 
771  /* Translate to clip space for easier calculations */
772  dst_x0 -= dst->clip_rect.x;
773  dst_x1 -= dst->clip_rect.x;
774  dst_y0 -= dst->clip_rect.y;
775  dst_y1 -= dst->clip_rect.y;
776 
777  if (dst_x0 < 0) {
778  src_x0 -= dst_x0 / scaling_w;
779  dst_x0 = 0;
780  }
781 
782  if (dst_x1 >= dst->clip_rect.w) {
783  src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
784  dst_x1 = dst->clip_rect.w - 1;
785  }
786 
787  if (dst_y0 < 0) {
788  src_y0 -= dst_y0 / scaling_h;
789  dst_y0 = 0;
790  }
791 
792  if (dst_y1 >= dst->clip_rect.h) {
793  src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
794  dst_y1 = dst->clip_rect.h - 1;
795  }
796 
797  /* Translate back to surface coordinates */
798  dst_x0 += dst->clip_rect.x;
799  dst_x1 += dst->clip_rect.x;
800  dst_y0 += dst->clip_rect.y;
801  dst_y1 += dst->clip_rect.y;
802 
803  final_src.x = (int)SDL_floor(src_x0 + 0.5);
804  final_src.y = (int)SDL_floor(src_y0 + 0.5);
805  final_src.w = (int)SDL_floor(src_x1 + 1 + 0.5) - (int)SDL_floor(src_x0 + 0.5);
806  final_src.h = (int)SDL_floor(src_y1 + 1 + 0.5) - (int)SDL_floor(src_y0 + 0.5);
807 
808  final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
809  final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
810  final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
811  final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
812 
813  if (final_dst.w < 0)
814  final_dst.w = 0;
815  if (final_dst.h < 0)
816  final_dst.h = 0;
817 
818  if (dstrect)
819  *dstrect = final_dst;
820 
821  if (final_dst.w == 0 || final_dst.h == 0 ||
822  final_src.w <= 0 || final_src.h <= 0) {
823  /* No-op. */
824  return 0;
825  }
826 
827  return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
828 }
#define SDL_BlitSurface
Definition: SDL_surface.h:465
#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:164
#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:835
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64