Skip to content

Commit 6da0210

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. Revert "Get rid of PG_SURF_BitsPerPixel" This reverts commit 1276caa. Fix PG_SURF_BitsPerPixel SDL3 implementation
1 parent 83d395f commit 6da0210

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src_c/_pygame.h

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

108-
#define PG_SURF_BitsPerPixel(surf) SDL_BITSPERPIXEL(surf->format)
108+
// Implementation from SDL_GetMasksForPixelFormat, which is used by
109+
// SDL_InitPixelFormatDetails in SDL_pixels.c
110+
// Created to match surf->format->BitsPerPixel in SDL2,
111+
// details->bits_per_pixel in SDL3.
112+
static inline int
113+
PG_SURF_BitsPerPixel(SDL_Surface *surf)
114+
{
115+
if (SDL_ISPIXELFORMAT_FOURCC(surf->format)) {
116+
// however, some of these are packed formats, and can legit declare
117+
// bits-per-pixel!
118+
switch (surf->format) {
119+
case SDL_PIXELFORMAT_YUY2:
120+
case SDL_PIXELFORMAT_UYVY:
121+
case SDL_PIXELFORMAT_YVYU:
122+
return 32;
123+
default:
124+
return 0; // oh well.
125+
}
126+
}
127+
128+
if (SDL_BYTESPERPIXEL(surf->format) <= 2) {
129+
return SDL_BITSPERPIXEL(surf->format);
130+
}
131+
return SDL_BYTESPERPIXEL(surf->format) * 8;
132+
}
133+
109134
#define PG_SURF_BytesPerPixel(surf) SDL_BYTESPERPIXEL(surf->format)
110135
#define PG_FORMAT_BitsPerPixel(format) format->bits_per_pixel
111136
#define PG_FORMAT_BytesPerPixel(format) format->bytes_per_pixel

0 commit comments

Comments
 (0)