Skip to content

Commit

Permalink
Continue moving away from format masks again
Browse files Browse the repository at this point in the history
Since SDL3 is all about pixelformat instead.
  • Loading branch information
Starbuck5 committed Jul 19, 2023
1 parent c41f75a commit 85161b5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
51 changes: 12 additions & 39 deletions src_c/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,14 +1141,9 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Bytes length does not equal format and resolution size");
surf = SDL_CreateRGBSurface(0, w, h, 32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0xFF, 0xFF << 8, 0xFF << 16,
(alphamult ? 0xFF << 24 : 0));
#else
0xFF << 24, 0xFF << 16, 0xFF << 8,
(alphamult ? 0xFF : 0));
#endif
surf = SDL_CreateRGBSurfaceWithFormat(
0, w, h, 32,
(alphamult ? SDL_PIXELFORMAT_RGBA32 : PG_PIXELFORMAT_RGBX32));
if (!surf)
return RAISE(pgExc_SDLError, SDL_GetError());
SDL_LockSurface(surf);
Expand Down Expand Up @@ -1332,13 +1327,8 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
surf = SDL_CreateRGBSurfaceFrom(data, w, h, 24, pitch, 0xFF, 0xFF << 8,
0xFF << 16, 0);
#else
surf = SDL_CreateRGBSurfaceFrom(data, w, h, 24, pitch, 0xFF << 16,
0xFF << 8, 0xFF, 0);
#endif
surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 24, pitch,
SDL_PIXELFORMAT_RGB24);
}
else if (!strcmp(format, "BGR")) {
if (pitch == -1) {
Expand All @@ -1354,13 +1344,8 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
surf = SDL_CreateRGBSurfaceFrom(data, w, h, 24, pitch, 0xFF << 16,
0xFF << 8, 0xFF, 0);
#else
surf = SDL_CreateRGBSurfaceFrom(data, w, h, 24, pitch, 0xFF, 0xFF << 8,
0xFF << 16, 0);
#endif
surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 24, pitch,
SDL_PIXELFORMAT_BGR24);
}
else if (!strcmp(format, "BGRA")) {
if (pitch == -1) {
Expand All @@ -1376,15 +1361,8 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
surf = SDL_CreateRGBSurfaceFrom(data, w, h, 32, pitch, 0xFF << 16,
0xFF << 8, 0xFF, 0xFF << 24);

#else
surf = SDL_CreateRGBSurfaceFrom(data, w, h, 32, pitch, 0xFF << 8,
0xFF << 16, 0xFF << 24, 0xFF);
#endif
surf = SDL_CreateRGBSurfaceWithFormatFrom(data, w, h, 32, pitch,
SDL_PIXELFORMAT_BGRA32);
}
else if (!strcmp(format, "RGBA") || !strcmp(format, "RGBX")) {
if (pitch == -1) {
Expand All @@ -1401,14 +1379,9 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds)
return RAISE(
PyExc_ValueError,
"Buffer length does not equal format and resolution size");
surf = SDL_CreateRGBSurfaceFrom(data, w, h, 32, pitch,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0xFF, 0xFF << 8, 0xFF << 16,
(alphamult ? 0xFF << 24 : 0));
#else
0xFF << 24, 0xFF << 16, 0xFF << 8,
(alphamult ? 0xFF : 0));
#endif
surf = SDL_CreateRGBSurfaceWithFormatFrom(
data, w, h, 32, pitch,
(alphamult ? SDL_PIXELFORMAT_RGBA32 : PG_PIXELFORMAT_RGBX32));
}
else if (!strcmp(format, "ARGB")) {
if (pitch == -1) {
Expand Down
36 changes: 30 additions & 6 deletions src_c/include/pgcompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,47 @@ typedef uint32_t Uint32;
typedef uint8_t Uint8;
#endif /* no SDL */

// SDL_PIXELFORMAT_XRGB8888 is new in SDL 2.0.14, so let's make a custom
// macro to use the new less confusing naming and still build on older
// systems. SDL_PIXELFORMAT_RGB888 == SDL_PIXELFORMAT_XRGB8888.
/* SDL_VERSION_ATLEAST is in every supported SDL version, but the code gets a
* warning without this check here, which is very weird. */
#ifdef SDL_VERSION_ATLEAST

// SDL_PIXELFORMAT_XRGB8888 and SDL_PIXELFORMAT_XBGR8888 are new names
// in SDL 2.0.14, the macros below let us use the new (less confusing)
// naming while still building on old versions.

#if SDL_VERSION_ATLEAST(2, 0, 14)
#define PG_PIXELFORMAT_XRGB8888 SDL_PIXELFORMAT_XRGB8888
#else
#define PG_PIXELFORMAT_XRGB8888 SDL_PIXELFORMAT_RGB888
#endif

#if SDL_VERSION_ATLEAST(2, 0, 14)
#define PG_PIXELFORMAT_XBGR8888 SDL_PIXELFORMAT_XBGR8888
#else
#define PG_PIXELFORMAT_XBGR8888 SDL_PIXELFORMAT_BGR888
#endif

#if defined(SDL_VERSION_ATLEAST)
// SDL does not provide endian independent names for 32 bit formats without
// alpha channels the way they do for ones wiht alpha channels.
// E.g. SDL_PIXELFORMAT_RGBA32. This macro allows us the convenience of the
// endian independent name.

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define PG_PIXELFORMAT_RGBX32 PG_PIXELFORMAT_XBGR8888
#else
#define PG_PIXELFORMAT_RGBX32 SDL_PIXELFORMAT_RGBX8888
#endif

#if SDL_VERSION_ATLEAST(2, 0, 18)
#define PG_GetTicks SDL_GetTicks64
#else
#define PG_GetTicks SDL_GetTicks
#endif /* SDL_VERSION_ATLEAST(2, 0, 18) */

#endif /* defined(SDL_VERSION_ATLEAST) */

#ifndef SDL_MOUSEWHEEL_FLIPPED
#define NO_SDL_MOUSEWHEEL_FLIPPED
#endif

#endif /* defined(SDL_VERSION_ATLEAST) */

#endif /* ~defined(PGCOMPAT_H) */

0 comments on commit 85161b5

Please sign in to comment.