From 833fd08bae25b86be2736440b8f1ff023a91ad68 Mon Sep 17 00:00:00 2001 From: Starbuck5 <46412508+Starbuck5@users.noreply.github.com> Date: Sat, 30 Aug 2025 13:58:33 -0700 Subject: [PATCH] Fix PG_SURF_BitsPerPixel SDL3 implementation --- src_c/_pygame.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src_c/_pygame.h b/src_c/_pygame.h index c9d88a98b2..ea461a6943 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -105,7 +105,32 @@ PG_UnlockMutex(SDL_mutex *mutex) return 0; } -#define PG_SURF_BitsPerPixel(surf) SDL_BITSPERPIXEL(surf->format) +// Implementation from SDL_GetMasksForPixelFormat, which is used by +// SDL_InitPixelFormatDetails in SDL_pixels.c +// Created to match surf->format->BitsPerPixel in SDL2, +// details->bits_per_pixel in SDL3. +static inline int +PG_SURF_BitsPerPixel(SDL_Surface *surf) +{ + if (SDL_ISPIXELFORMAT_FOURCC(surf->format)) { + // however, some of these are packed formats, and can legit declare + // bits-per-pixel! + switch (surf->format) { + case SDL_PIXELFORMAT_YUY2: + case SDL_PIXELFORMAT_UYVY: + case SDL_PIXELFORMAT_YVYU: + return 32; + default: + return 0; // oh well. + } + } + + if (SDL_BYTESPERPIXEL(surf->format) <= 2) { + return SDL_BITSPERPIXEL(surf->format); + } + return SDL_BYTESPERPIXEL(surf->format) * 8; +} + #define PG_SURF_BytesPerPixel(surf) SDL_BYTESPERPIXEL(surf->format) #define PG_FORMAT_BitsPerPixel(format) format->bits_per_pixel #define PG_FORMAT_BytesPerPixel(format) format->bytes_per_pixel