Skip to content

Commit 1276caa

Browse files
committed
Get rid of PG_SURF_BitsPerPixel
It's inconsistent between SDL2 and SDL3, and I see no way to fix it. In SDL2, (surf->format->BitsPerPixel), the value means the number of bits each pixel occupies in total. For example, RGBX => 32. In SDL3, (SDL_BITSPERPIXEL(surf->format)), the value means the number of pixels occupied by the pixel's data, not including padding. So RGBX => 24. These changes reduce SDL3 unit test failures.
1 parent 83d395f commit 1276caa

File tree

7 files changed

+20
-10
lines changed

7 files changed

+20
-10
lines changed

src_c/_camera.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ surf_colorspace(PyObject *self, PyObject *arg)
126126
}
127127

128128
/* check to see if the format of the surface is the same. */
129-
if (PG_SURF_BitsPerPixel(surf) != PG_SURF_BitsPerPixel(newsurf)) {
129+
if (PG_SURF_BytesPerPixel(surf) != PG_SURF_BytesPerPixel(newsurf)) {
130130
return RAISE(PyExc_ValueError, "Surfaces not the same depth");
131131
}
132132

src_c/_pygame.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ PG_UnlockMutex(SDL_mutex *mutex)
105105
return 0;
106106
}
107107

108-
#define PG_SURF_BitsPerPixel(surf) SDL_BITSPERPIXEL(surf->format)
109108
#define PG_SURF_BytesPerPixel(surf) SDL_BYTESPERPIXEL(surf->format)
110109
#define PG_FORMAT_BitsPerPixel(format) format->bits_per_pixel
111110
#define PG_FORMAT_BytesPerPixel(format) format->bytes_per_pixel
@@ -218,7 +217,6 @@ PG_UnlockMutex(SDL_mutex *mutex)
218217
return SDL_UnlockMutex(mutex);
219218
}
220219

221-
#define PG_SURF_BitsPerPixel(surf) surf->format->BitsPerPixel
222220
#define PG_SURF_BytesPerPixel(surf) surf->format->BytesPerPixel
223221
#define PG_FORMAT_BitsPerPixel(format) format->BitsPerPixel
224222
#define PG_FORMAT_BytesPerPixel(format) format->BytesPerPixel

src_c/display.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds)
18051805
SDL_SetWindowIcon(win, pgSurface_AsSurface(state->icon));
18061806
}
18071807

1808-
if (depth != 0 && PG_SURF_BitsPerPixel(surface->surf) != depth) {
1808+
if (depth != 0 && PG_SURF_BytesPerPixel(surface->surf) * 8 != depth) {
18091809
if (PyErr_WarnEx(PyExc_DeprecationWarning,
18101810
"The depth argument is deprecated, and is ignored",
18111811
1)) {

src_c/image.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,9 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
17081708
h.infolen = 0;
17091709
SETLE16(h.cmap_start, 0);
17101710

1711-
srcbpp = PG_SURF_BitsPerPixel(surface);
1711+
// This bpp = Bpp*8 strategy only works on pixels a byte or larger,
1712+
// which are the only types of surface this function supports.
1713+
srcbpp = PG_SURF_BytesPerPixel(surface) * 8;
17121714
if (srcbpp < 8) {
17131715
SDL_SetError("cannot save <8bpp images as TGA");
17141716
return -1;

src_c/rotozoom.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,8 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
524524
/*
525525
* Determine if source surface is 32bit or 8bit
526526
*/
527-
is32bit = (PG_SURF_BitsPerPixel(src) == 32);
528-
if ((is32bit) || (PG_SURF_BitsPerPixel(src) == 8)) {
527+
is32bit = (PG_SURF_BytesPerPixel(src) == 4);
528+
if ((is32bit) || (PG_SURF_BytesPerPixel(src) == 1)) {
529529
/*
530530
* Use source surface 'as is'
531531
*/

src_c/surface.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ surface_str(PyObject *self)
441441
return PyUnicode_FromString("<Surface(Dead Display)>");
442442
}
443443

444+
PG_PixelFormat *fmt = PG_GetSurfaceFormat(surf);
445+
if (fmt == NULL) {
446+
return RAISE(pgExc_SDLError, SDL_GetError());
447+
}
448+
444449
PyObject *colorkey = surf_get_colorkey((pgSurfaceObject *)self, NULL);
445450
if (colorkey == NULL) {
446451
return NULL;
@@ -465,7 +470,7 @@ surface_str(PyObject *self)
465470
// but alpha, we can "move up" the global alpha to this spot No need to do
466471
// this vice-versa, as it ignores extra args
467472
PyObject *formatted_str = PyUnicode_FromFormat(
468-
format, surf->w, surf->h, PG_SURF_BitsPerPixel(surf),
473+
format, surf->w, surf->h, PG_FORMAT_BitsPerPixel(fmt),
469474
PyObject_IsTrue(colorkey) ? colorkey : global_alpha, global_alpha);
470475

471476
Py_DECREF(colorkey);
@@ -3114,7 +3119,12 @@ surf_get_bitsize(PyObject *self, PyObject *_null)
31143119
SDL_Surface *surf = pgSurface_AsSurface(self);
31153120
SURF_INIT_CHECK(surf)
31163121

3117-
return PyLong_FromLong(PG_SURF_BitsPerPixel(surf));
3122+
PG_PixelFormat *fmt = PG_GetSurfaceFormat(surf);
3123+
if (fmt == NULL) {
3124+
return RAISE(pgExc_SDLError, SDL_GetError());
3125+
}
3126+
3127+
return PyLong_FromLong(PG_FORMAT_BitsPerPixel(fmt));
31183128
}
31193129

31203130
static PyObject *

src_c/transform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ surf_rotozoom(PyObject *self, PyObject *args, PyObject *kwargs)
951951
return (PyObject *)pgSurface_New(newsurf);
952952
}
953953

954-
if (PG_SURF_BitsPerPixel(surf) == 32) {
954+
if (PG_SURF_BytesPerPixel(surf) == 4) {
955955
surf32 = surf;
956956
pgSurface_Lock(surfobj);
957957
}

0 commit comments

Comments
 (0)