Skip to content

Commit

Permalink
Merge pull request #2804 from Starbuck5/prep-BitsBytesPerPixel-change…
Browse files Browse the repository at this point in the history
…s-sdl3

Use macros for (Bytes|Bits)PerPixel compat in SDL3
  • Loading branch information
MyreMylar authored Apr 21, 2024
2 parents 1f8fe8d + 4230f16 commit 7057f16
Show file tree
Hide file tree
Showing 21 changed files with 363 additions and 317 deletions.
28 changes: 14 additions & 14 deletions src_c/SDL_gfx/SDL_gfxPrimitives.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fastPixelColorNolock(SDL_Surface *dst, Sint16 x, Sint16 y, Uint32 color)
/*
* Get destination format
*/
bpp = dst->format->BytesPerPixel;
bpp = GFX_SURF_BytesPerPixel(dst);
p = (Uint8 *)dst->pixels + y * dst->pitch + x * bpp;
switch (bpp) {
case 1:
Expand Down Expand Up @@ -131,7 +131,7 @@ int fastPixelColorNolockNoclip(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 col
/*
* Get destination format
*/
bpp = dst->format->BytesPerPixel;
bpp = GFX_SURF_BytesPerPixel(dst);
p = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
switch (bpp) {
case 1:
Expand Down Expand Up @@ -287,7 +287,7 @@ _putPixelAlpha(SDL_Surface *dst, Sint16 x, Sint16 y, Uint32 color, Uint8 alpha)
y <= clip_ymax(dst)) {
format = dst->format;

switch (format->BytesPerPixel) {
switch (GFX_FORMAT_BytesPerPixel(format)) {
case 1: { /* Assuming 8-bpp */
if (alpha == 255) {
*((Uint8 *)dst->pixels + y * dst->pitch + x) = color;
Expand Down Expand Up @@ -602,7 +602,7 @@ _filledRectAlpha(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
Sint16 x, y;

format = dst->format;
switch (format->BytesPerPixel) {
switch (GFX_FORMAT_BytesPerPixel(format)) {
case 1: { /* Assuming 8-bpp */
Uint8 *row, *pixel;
Uint8 dR, dG, dB;
Expand Down Expand Up @@ -1098,14 +1098,14 @@ hlineColorStore(SDL_Surface *dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color)
* More variable setup
*/
dx = w;
pixx = dst->format->BytesPerPixel;
pixx = GFX_SURF_BytesPerPixel(dst);
pixy = dst->pitch;
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y;

/*
* Draw
*/
switch (dst->format->BytesPerPixel) {
switch (GFX_SURF_BytesPerPixel(dst)) {
case 1:
memset(pixel, color, dx + 1);
break;
Expand Down Expand Up @@ -1288,14 +1288,14 @@ hlineColor(SDL_Surface *dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color)
/*
* More variable setup
*/
pixx = dst->format->BytesPerPixel;
pixx = GFX_SURF_BytesPerPixel(dst);
pixy = dst->pitch;
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y;

/*
* Draw
*/
switch (dst->format->BytesPerPixel) {
switch (GFX_SURF_BytesPerPixel(dst)) {
case 1:
memset(pixel, color, dx + 1);
break;
Expand Down Expand Up @@ -1484,15 +1484,15 @@ vlineColor(SDL_Surface *dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color)
* More variable setup
*/
dy = h;
pixx = dst->format->BytesPerPixel;
pixx = GFX_SURF_BytesPerPixel(dst);
pixy = dst->pitch;
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x + pixy * (int)y1;
pixellast = pixel + pixy * dy;

/*
* Draw
*/
switch (dst->format->BytesPerPixel) {
switch (GFX_SURF_BytesPerPixel(dst)) {
case 1:
for (; pixel <= pixellast; pixel += pixy) {
*(Uint8 *)pixel = color;
Expand Down Expand Up @@ -2265,7 +2265,7 @@ boxColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
*/
dx = w;
dy = h;
pixx = dst->format->BytesPerPixel;
pixx = GFX_SURF_BytesPerPixel(dst);
pixy = dst->pitch;
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y1;
pixellast = pixel + pixx * dx + pixy * dy;
Expand All @@ -2274,7 +2274,7 @@ boxColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
/*
* Draw
*/
switch (dst->format->BytesPerPixel) {
switch (GFX_SURF_BytesPerPixel(dst)) {
case 1:
for (; pixel <= pixellast; pixel += pixy) {
memset(pixel, (Uint8)color, dx);
Expand Down Expand Up @@ -2463,7 +2463,7 @@ lineColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
*/
dx = sx * dx + 1;
dy = sy * dy + 1;
pixx = dst->format->BytesPerPixel;
pixx = GFX_SURF_BytesPerPixel(dst);
pixy = dst->pitch;
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y1;
pixx *= sx;
Expand All @@ -2482,7 +2482,7 @@ lineColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
*/
x = 0;
y = 0;
switch (dst->format->BytesPerPixel) {
switch (GFX_SURF_BytesPerPixel(dst)) {
case 1:
for (; x < dx; x++, pixel += pixx) {
*pixel = color;
Expand Down
14 changes: 14 additions & 0 deletions src_c/SDL_gfx/SDL_gfxPrimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ extern "C" {
#define SDL_GFXPRIMITIVES_MINOR 0
#define SDL_GFXPRIMITIVES_MICRO 23

/* ---- Compatibility */

#if SDL_VERSION_ATLEAST(3, 0, 0)
#define GFX_SURF_BitsPerPixel(surf) surf->format->bits_per_pixel
#define GFX_SURF_BytesPerPixel(surf) surf->format->bytes_per_pixel
#define GFX_FORMAT_BitsPerPixel(format) format->bits_per_pixel
#define GFX_FORMAT_BytesPerPixel(format) format->bytes_per_pixel
#else
#define GFX_SURF_BitsPerPixel(surf) surf->format->BitsPerPixel
#define GFX_SURF_BytesPerPixel(surf) surf->format->BytesPerPixel
#define GFX_FORMAT_BitsPerPixel(format) format->BitsPerPixel
#define GFX_FORMAT_BytesPerPixel(format) format->BytesPerPixel
#endif

/* ---- Function Prototypes */

#ifdef _MSC_VER
Expand Down
36 changes: 18 additions & 18 deletions src_c/_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ surf_colorspace(PyObject *self, PyObject *arg)
"Surfaces not the same width and height.");

/* check to see if the format of the surface is the same. */
if (surf->format->BitsPerPixel != newsurf->format->BitsPerPixel)
if (PG_SURF_BitsPerPixel(surf) != PG_SURF_BitsPerPixel(newsurf))
return RAISE(PyExc_ValueError, "Surfaces not the same depth");

SDL_LockSurface(newsurf);
Expand Down Expand Up @@ -505,7 +505,7 @@ rgb24_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
gloss = format->Gloss;
bloss = format->Bloss;

switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
d8 = (Uint8 *)dst;
while (length--) {
Expand Down Expand Up @@ -567,7 +567,7 @@ bgr32_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
gloss = format->Gloss;
bloss = format->Bloss;

switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
d8 = (Uint8 *)dst;
while (length--) {
Expand Down Expand Up @@ -681,7 +681,7 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
h = 170 + 43 * (r - g) / delta;
}
}
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
*d8++ = ((h >> rloss) << rshift) |
((s >> gloss) << gshift) |
Expand All @@ -707,7 +707,7 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
}
else { /* for use as stage 2 in yuv or bayer to hsv, r and b switched */
while (length--) {
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
r = *s8 >> rshift << rloss;
g = *s8 >> gshift << gloss;
Expand Down Expand Up @@ -749,7 +749,7 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
h = 170 + 43 * (r - g) / delta;
}
}
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
*d8++ = ((h >> rloss) << rshift) |
((s >> gloss) << gshift) |
Expand Down Expand Up @@ -826,7 +826,7 @@ rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128; /* V */
u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128; /* U */
y = (77 * r + 150 * g + 29 * b + 128) >> 8; /* Y */
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
*d8++ = ((y >> rloss) << rshift) |
((u >> gloss) << gshift) |
Expand All @@ -851,7 +851,7 @@ rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
}
}
else { /* for use as stage 2 in bayer to yuv, r and b switched */
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
while (length--) {
r = *s8 >> rshift << rloss;
Expand Down Expand Up @@ -932,7 +932,7 @@ rgb444_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
gloss = format->Gloss;
bloss = format->Bloss;

switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
d8 = (Uint8 *)dst;
while (length--) {
Expand Down Expand Up @@ -1026,7 +1026,7 @@ yuyv_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
b2 = SAT2(y2 + u1);

/* choose the right pixel packing for the destination surface depth */
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
*d8++ = ((r1 >> rloss) << rshift) | ((g1 >> gloss) << gshift) |
((b1 >> bloss) << bshift);
Expand Down Expand Up @@ -1076,7 +1076,7 @@ yuyv_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
bloss = format->Bloss;
s = (Uint8 *)src;

switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
d8 = (Uint8 *)dst;
while (i--) {
Expand Down Expand Up @@ -1180,7 +1180,7 @@ uyvy_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
b2 = SAT2(y2 + u1);

/* choose the right pixel packing for the destination surface depth */
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
*d8++ = ((r1 >> rloss) << rshift) | ((g1 >> gloss) << gshift) |
((b1 >> bloss) << bshift);
Expand Down Expand Up @@ -1229,7 +1229,7 @@ uyvy_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
bloss = format->Bloss;
s = (Uint8 *)src;

switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
d8 = (Uint8 *)dst;
while (i--) {
Expand Down Expand Up @@ -1405,7 +1405,7 @@ sbggr8_to_rgb(const void *src, void *dst, int width, int height,
}
}
rawpt++;
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
*d8++ = ((r >> rloss) << rshift) | ((g >> gloss) << gshift) |
((b >> bloss) << bshift);
Expand Down Expand Up @@ -1456,15 +1456,15 @@ yuv420_to_rgb(const void *src, void *dst, int width, int height,
/* prepare the destination pointers for different surface depths. */
d8_1 = (Uint8 *)dst;
/* the following is because d8 used for both 8 and 24 bit surfaces */
d8_2 = d8_1 + (format->BytesPerPixel == 3 ? width * 3 : 3);
d8_2 = d8_1 + (PG_FORMAT_BytesPerPixel(format) == 3 ? width * 3 : 3);
d16_1 = (Uint16 *)dst;
d16_2 = d16_1 + width;
d32_1 = (Uint32 *)dst;
d32_2 = d32_1 + width;

/* for the sake of speed, the nested while loops are inside of the switch
statement for the different surface bit depths */
switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
while (j--) {
i = width / 2;
Expand Down Expand Up @@ -1648,7 +1648,7 @@ yuv420_to_yuv(const void *src, void *dst, int width, int height,
bloss = format->Bloss;

d8_1 = (Uint8 *)dst;
d8_2 = d8_1 + (format->BytesPerPixel == 3 ? width * 3 : 3);
d8_2 = d8_1 + (PG_FORMAT_BytesPerPixel(format) == 3 ? width * 3 : 3);
d16_1 = (Uint16 *)dst;
d16_2 = d16_1 + width;
d32_1 = (Uint32 *)dst;
Expand All @@ -1659,7 +1659,7 @@ yuv420_to_yuv(const void *src, void *dst, int width, int height,
v = u + (width * height) / 4;
j = height / 2;

switch (format->BytesPerPixel) {
switch (PG_FORMAT_BytesPerPixel(format)) {
case 1:
while (j--) {
i = width / 2;
Expand Down
10 changes: 10 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ PG_UnlockMutex(SDL_mutex *mutex)
return 0;
}

#define PG_SURF_BitsPerPixel(surf) surf->format->bits_per_pixel
#define PG_SURF_BytesPerPixel(surf) surf->format->bytes_per_pixel
#define PG_FORMAT_BitsPerPixel(format) format->bits_per_pixel
#define PG_FORMAT_BytesPerPixel(format) format->bytes_per_pixel

#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
Expand Down Expand Up @@ -137,6 +142,11 @@ PG_UnlockMutex(SDL_mutex *mutex)
return SDL_UnlockMutex(mutex);
}

#define PG_SURF_BitsPerPixel(surf) surf->format->BitsPerPixel
#define PG_SURF_BytesPerPixel(surf) surf->format->BytesPerPixel
#define PG_FORMAT_BitsPerPixel(format) format->BitsPerPixel
#define PG_FORMAT_BytesPerPixel(format) format->BytesPerPixel

#if SDL_VERSION_ATLEAST(2, 0, 14)
#define PG_SurfaceHasRLE SDL_HasSurfaceRLE
#else
Expand Down
Loading

0 comments on commit 7057f16

Please sign in to comment.