Skip to content

Commit 7057f16

Browse files
authored
Merge pull request #2804 from Starbuck5/prep-BitsBytesPerPixel-changes-sdl3
Use macros for (Bytes|Bits)PerPixel compat in SDL3
2 parents 1f8fe8d + 4230f16 commit 7057f16

21 files changed

+363
-317
lines changed

src_c/SDL_gfx/SDL_gfxPrimitives.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fastPixelColorNolock(SDL_Surface *dst, Sint16 x, Sint16 y, Uint32 color)
7979
/*
8080
* Get destination format
8181
*/
82-
bpp = dst->format->BytesPerPixel;
82+
bpp = GFX_SURF_BytesPerPixel(dst);
8383
p = (Uint8 *)dst->pixels + y * dst->pitch + x * bpp;
8484
switch (bpp) {
8585
case 1:
@@ -131,7 +131,7 @@ int fastPixelColorNolockNoclip(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 col
131131
/*
132132
* Get destination format
133133
*/
134-
bpp = dst->format->BytesPerPixel;
134+
bpp = GFX_SURF_BytesPerPixel(dst);
135135
p = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
136136
switch (bpp) {
137137
case 1:
@@ -287,7 +287,7 @@ _putPixelAlpha(SDL_Surface *dst, Sint16 x, Sint16 y, Uint32 color, Uint8 alpha)
287287
y <= clip_ymax(dst)) {
288288
format = dst->format;
289289

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

604604
format = dst->format;
605-
switch (format->BytesPerPixel) {
605+
switch (GFX_FORMAT_BytesPerPixel(format)) {
606606
case 1: { /* Assuming 8-bpp */
607607
Uint8 *row, *pixel;
608608
Uint8 dR, dG, dB;
@@ -1098,14 +1098,14 @@ hlineColorStore(SDL_Surface *dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color)
10981098
* More variable setup
10991099
*/
11001100
dx = w;
1101-
pixx = dst->format->BytesPerPixel;
1101+
pixx = GFX_SURF_BytesPerPixel(dst);
11021102
pixy = dst->pitch;
11031103
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y;
11041104

11051105
/*
11061106
* Draw
11071107
*/
1108-
switch (dst->format->BytesPerPixel) {
1108+
switch (GFX_SURF_BytesPerPixel(dst)) {
11091109
case 1:
11101110
memset(pixel, color, dx + 1);
11111111
break;
@@ -1288,14 +1288,14 @@ hlineColor(SDL_Surface *dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color)
12881288
/*
12891289
* More variable setup
12901290
*/
1291-
pixx = dst->format->BytesPerPixel;
1291+
pixx = GFX_SURF_BytesPerPixel(dst);
12921292
pixy = dst->pitch;
12931293
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y;
12941294

12951295
/*
12961296
* Draw
12971297
*/
1298-
switch (dst->format->BytesPerPixel) {
1298+
switch (GFX_SURF_BytesPerPixel(dst)) {
12991299
case 1:
13001300
memset(pixel, color, dx + 1);
13011301
break;
@@ -1484,15 +1484,15 @@ vlineColor(SDL_Surface *dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color)
14841484
* More variable setup
14851485
*/
14861486
dy = h;
1487-
pixx = dst->format->BytesPerPixel;
1487+
pixx = GFX_SURF_BytesPerPixel(dst);
14881488
pixy = dst->pitch;
14891489
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x + pixy * (int)y1;
14901490
pixellast = pixel + pixy * dy;
14911491

14921492
/*
14931493
* Draw
14941494
*/
1495-
switch (dst->format->BytesPerPixel) {
1495+
switch (GFX_SURF_BytesPerPixel(dst)) {
14961496
case 1:
14971497
for (; pixel <= pixellast; pixel += pixy) {
14981498
*(Uint8 *)pixel = color;
@@ -2265,7 +2265,7 @@ boxColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
22652265
*/
22662266
dx = w;
22672267
dy = h;
2268-
pixx = dst->format->BytesPerPixel;
2268+
pixx = GFX_SURF_BytesPerPixel(dst);
22692269
pixy = dst->pitch;
22702270
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y1;
22712271
pixellast = pixel + pixx * dx + pixy * dy;
@@ -2274,7 +2274,7 @@ boxColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
22742274
/*
22752275
* Draw
22762276
*/
2277-
switch (dst->format->BytesPerPixel) {
2277+
switch (GFX_SURF_BytesPerPixel(dst)) {
22782278
case 1:
22792279
for (; pixel <= pixellast; pixel += pixy) {
22802280
memset(pixel, (Uint8)color, dx);
@@ -2463,7 +2463,7 @@ lineColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
24632463
*/
24642464
dx = sx * dx + 1;
24652465
dy = sy * dy + 1;
2466-
pixx = dst->format->BytesPerPixel;
2466+
pixx = GFX_SURF_BytesPerPixel(dst);
24672467
pixy = dst->pitch;
24682468
pixel = ((Uint8 *)dst->pixels) + pixx * (int)x1 + pixy * (int)y1;
24692469
pixx *= sx;
@@ -2482,7 +2482,7 @@ lineColor(SDL_Surface *dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
24822482
*/
24832483
x = 0;
24842484
y = 0;
2485-
switch (dst->format->BytesPerPixel) {
2485+
switch (GFX_SURF_BytesPerPixel(dst)) {
24862486
case 1:
24872487
for (; x < dx; x++, pixel += pixx) {
24882488
*pixel = color;

src_c/SDL_gfx/SDL_gfxPrimitives.h

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ extern "C" {
2727
#define SDL_GFXPRIMITIVES_MINOR 0
2828
#define SDL_GFXPRIMITIVES_MICRO 23
2929

30+
/* ---- Compatibility */
31+
32+
#if SDL_VERSION_ATLEAST(3, 0, 0)
33+
#define GFX_SURF_BitsPerPixel(surf) surf->format->bits_per_pixel
34+
#define GFX_SURF_BytesPerPixel(surf) surf->format->bytes_per_pixel
35+
#define GFX_FORMAT_BitsPerPixel(format) format->bits_per_pixel
36+
#define GFX_FORMAT_BytesPerPixel(format) format->bytes_per_pixel
37+
#else
38+
#define GFX_SURF_BitsPerPixel(surf) surf->format->BitsPerPixel
39+
#define GFX_SURF_BytesPerPixel(surf) surf->format->BytesPerPixel
40+
#define GFX_FORMAT_BitsPerPixel(format) format->BitsPerPixel
41+
#define GFX_FORMAT_BytesPerPixel(format) format->BytesPerPixel
42+
#endif
43+
3044
/* ---- Function Prototypes */
3145

3246
#ifdef _MSC_VER

src_c/_camera.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ surf_colorspace(PyObject *self, PyObject *arg)
124124
"Surfaces not the same width and height.");
125125

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

130130
SDL_LockSurface(newsurf);
@@ -505,7 +505,7 @@ rgb24_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
505505
gloss = format->Gloss;
506506
bloss = format->Bloss;
507507

508-
switch (format->BytesPerPixel) {
508+
switch (PG_FORMAT_BytesPerPixel(format)) {
509509
case 1:
510510
d8 = (Uint8 *)dst;
511511
while (length--) {
@@ -567,7 +567,7 @@ bgr32_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
567567
gloss = format->Gloss;
568568
bloss = format->Bloss;
569569

570-
switch (format->BytesPerPixel) {
570+
switch (PG_FORMAT_BytesPerPixel(format)) {
571571
case 1:
572572
d8 = (Uint8 *)dst;
573573
while (length--) {
@@ -681,7 +681,7 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
681681
h = 170 + 43 * (r - g) / delta;
682682
}
683683
}
684-
switch (format->BytesPerPixel) {
684+
switch (PG_FORMAT_BytesPerPixel(format)) {
685685
case 1:
686686
*d8++ = ((h >> rloss) << rshift) |
687687
((s >> gloss) << gshift) |
@@ -707,7 +707,7 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
707707
}
708708
else { /* for use as stage 2 in yuv or bayer to hsv, r and b switched */
709709
while (length--) {
710-
switch (format->BytesPerPixel) {
710+
switch (PG_FORMAT_BytesPerPixel(format)) {
711711
case 1:
712712
r = *s8 >> rshift << rloss;
713713
g = *s8 >> gshift << gloss;
@@ -749,7 +749,7 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
749749
h = 170 + 43 * (r - g) / delta;
750750
}
751751
}
752-
switch (format->BytesPerPixel) {
752+
switch (PG_FORMAT_BytesPerPixel(format)) {
753753
case 1:
754754
*d8++ = ((h >> rloss) << rshift) |
755755
((s >> gloss) << gshift) |
@@ -826,7 +826,7 @@ rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
826826
v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128; /* V */
827827
u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128; /* U */
828828
y = (77 * r + 150 * g + 29 * b + 128) >> 8; /* Y */
829-
switch (format->BytesPerPixel) {
829+
switch (PG_FORMAT_BytesPerPixel(format)) {
830830
case 1:
831831
*d8++ = ((y >> rloss) << rshift) |
832832
((u >> gloss) << gshift) |
@@ -851,7 +851,7 @@ rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
851851
}
852852
}
853853
else { /* for use as stage 2 in bayer to yuv, r and b switched */
854-
switch (format->BytesPerPixel) {
854+
switch (PG_FORMAT_BytesPerPixel(format)) {
855855
case 1:
856856
while (length--) {
857857
r = *s8 >> rshift << rloss;
@@ -932,7 +932,7 @@ rgb444_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
932932
gloss = format->Gloss;
933933
bloss = format->Bloss;
934934

935-
switch (format->BytesPerPixel) {
935+
switch (PG_FORMAT_BytesPerPixel(format)) {
936936
case 1:
937937
d8 = (Uint8 *)dst;
938938
while (length--) {
@@ -1026,7 +1026,7 @@ yuyv_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
10261026
b2 = SAT2(y2 + u1);
10271027

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

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

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

1232-
switch (format->BytesPerPixel) {
1232+
switch (PG_FORMAT_BytesPerPixel(format)) {
12331233
case 1:
12341234
d8 = (Uint8 *)dst;
12351235
while (i--) {
@@ -1405,7 +1405,7 @@ sbggr8_to_rgb(const void *src, void *dst, int width, int height,
14051405
}
14061406
}
14071407
rawpt++;
1408-
switch (format->BytesPerPixel) {
1408+
switch (PG_FORMAT_BytesPerPixel(format)) {
14091409
case 1:
14101410
*d8++ = ((r >> rloss) << rshift) | ((g >> gloss) << gshift) |
14111411
((b >> bloss) << bshift);
@@ -1456,15 +1456,15 @@ yuv420_to_rgb(const void *src, void *dst, int width, int height,
14561456
/* prepare the destination pointers for different surface depths. */
14571457
d8_1 = (Uint8 *)dst;
14581458
/* the following is because d8 used for both 8 and 24 bit surfaces */
1459-
d8_2 = d8_1 + (format->BytesPerPixel == 3 ? width * 3 : 3);
1459+
d8_2 = d8_1 + (PG_FORMAT_BytesPerPixel(format) == 3 ? width * 3 : 3);
14601460
d16_1 = (Uint16 *)dst;
14611461
d16_2 = d16_1 + width;
14621462
d32_1 = (Uint32 *)dst;
14631463
d32_2 = d32_1 + width;
14641464

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

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

1662-
switch (format->BytesPerPixel) {
1662+
switch (PG_FORMAT_BytesPerPixel(format)) {
16631663
case 1:
16641664
while (j--) {
16651665
i = width / 2;

src_c/_pygame.h

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ PG_UnlockMutex(SDL_mutex *mutex)
9393
return 0;
9494
}
9595

96+
#define PG_SURF_BitsPerPixel(surf) surf->format->bits_per_pixel
97+
#define PG_SURF_BytesPerPixel(surf) surf->format->bytes_per_pixel
98+
#define PG_FORMAT_BitsPerPixel(format) format->bits_per_pixel
99+
#define PG_FORMAT_BytesPerPixel(format) format->bytes_per_pixel
100+
96101
#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
97102
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
98103
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
@@ -137,6 +142,11 @@ PG_UnlockMutex(SDL_mutex *mutex)
137142
return SDL_UnlockMutex(mutex);
138143
}
139144

145+
#define PG_SURF_BitsPerPixel(surf) surf->format->BitsPerPixel
146+
#define PG_SURF_BytesPerPixel(surf) surf->format->BytesPerPixel
147+
#define PG_FORMAT_BitsPerPixel(format) format->BitsPerPixel
148+
#define PG_FORMAT_BytesPerPixel(format) format->BytesPerPixel
149+
140150
#if SDL_VERSION_ATLEAST(2, 0, 14)
141151
#define PG_SurfaceHasRLE SDL_HasSurfaceRLE
142152
#else

0 commit comments

Comments
 (0)