23
23
#include "pygame.h"
24
24
25
25
#include "pgcompat.h"
26
+ #include <stddef.h>
26
27
27
28
#include "doc/pixelarray_doc.h"
28
29
@@ -971,6 +972,13 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
971
972
return -1 ;
972
973
}
973
974
975
+ PG_PixelFormat * surf_format = PG_GetSurfaceFormat (surf );
976
+ PG_PixelFormat * val_surf_format = PG_GetSurfaceFormat (val_surf );
977
+ if (surf_format == NULL || val_surf_format == NULL ) {
978
+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
979
+ return -1 ;
980
+ }
981
+
974
982
/* If we reassign the same array, we need to copy the pixels
975
983
* first. */
976
984
if (SURFACE_EQUALS (array , val )) {
@@ -1021,20 +1029,23 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
1021
1029
}
1022
1030
break ;
1023
1031
case 3 : {
1032
+ // Note:
1033
+ // Why is the 24 bit case pixelformat aware but none of the rest are?
1034
+ // - Starbuck, jan. 2025
1024
1035
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN )
1025
- Uint32 Roffset = surf -> format -> Rshift >> 3 ;
1026
- Uint32 Goffset = surf -> format -> Gshift >> 3 ;
1027
- Uint32 Boffset = surf -> format -> Bshift >> 3 ;
1028
- Uint32 vRoffset = val_surf -> format -> Rshift >> 3 ;
1029
- Uint32 vGoffset = val_surf -> format -> Gshift >> 3 ;
1030
- Uint32 vBoffset = val_surf -> format -> Bshift >> 3 ;
1036
+ Uint32 Roffset = surf_format -> Rshift >> 3 ;
1037
+ Uint32 Goffset = surf_format -> Gshift >> 3 ;
1038
+ Uint32 Boffset = surf_format -> Bshift >> 3 ;
1039
+ Uint32 vRoffset = val_surf_format -> Rshift >> 3 ;
1040
+ Uint32 vGoffset = val_surf_format -> Gshift >> 3 ;
1041
+ Uint32 vBoffset = val_surf_format -> Bshift >> 3 ;
1031
1042
#else
1032
- Uint32 Roffset = 2 - (surf -> format -> Rshift >> 3 );
1033
- Uint32 Goffset = 2 - (surf -> format -> Gshift >> 3 );
1034
- Uint32 Boffset = 2 - (surf -> format -> Bshift >> 3 );
1035
- Uint32 vRoffset = 2 - (val_surf -> format -> Rshift >> 3 );
1036
- Uint32 vGoffset = 2 - (val_surf -> format -> Gshift >> 3 );
1037
- Uint32 vBoffset = 2 - (val_surf -> format -> Bshift >> 3 );
1043
+ Uint32 Roffset = 2 - (surf_format -> Rshift >> 3 );
1044
+ Uint32 Goffset = 2 - (surf_format -> Gshift >> 3 );
1045
+ Uint32 Boffset = 2 - (surf_format -> Bshift >> 3 );
1046
+ Uint32 vRoffset = 2 - (val_surf_format -> Rshift >> 3 );
1047
+ Uint32 vGoffset = 2 - (val_surf_format -> Gshift >> 3 );
1048
+ Uint32 vBoffset = 2 - (val_surf_format -> Bshift >> 3 );
1038
1049
#endif
1039
1050
for (y = 0 ; y < dim1 ; ++ y ) {
1040
1051
pixel_p = pixelrow ;
@@ -1076,7 +1087,6 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low,
1076
1087
Py_ssize_t high , PyObject * val )
1077
1088
{
1078
1089
SDL_Surface * surf = pgSurface_AsSurface (array -> surface );
1079
- SDL_PixelFormat * format ;
1080
1090
Py_ssize_t dim0 = ABS (high - low );
1081
1091
Py_ssize_t dim1 = array -> shape [1 ];
1082
1092
Py_ssize_t stride0 = high >= low ? array -> strides [0 ] : - array -> strides [0 ];
@@ -1097,8 +1107,13 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low,
1097
1107
return -1 ;
1098
1108
}
1099
1109
1100
- format = surf -> format ;
1101
- bpp = PG_FORMAT_BytesPerPixel (format );
1110
+ PG_PixelFormat * surf_format = PG_GetSurfaceFormat (surf );
1111
+ if (surf_format == NULL ) {
1112
+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
1113
+ return -1 ;
1114
+ }
1115
+
1116
+ bpp = PG_FORMAT_BytesPerPixel (surf_format );
1102
1117
1103
1118
if (!dim1 ) {
1104
1119
dim1 = 1 ;
@@ -1150,13 +1165,13 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low,
1150
1165
break ;
1151
1166
case 3 : {
1152
1167
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN )
1153
- Uint32 Roffset = surf -> format -> Rshift >> 3 ;
1154
- Uint32 Goffset = surf -> format -> Gshift >> 3 ;
1155
- Uint32 Boffset = surf -> format -> Bshift >> 3 ;
1168
+ Uint32 Roffset = surf_format -> Rshift >> 3 ;
1169
+ Uint32 Goffset = surf_format -> Gshift >> 3 ;
1170
+ Uint32 Boffset = surf_format -> Bshift >> 3 ;
1156
1171
#else
1157
- Uint32 Roffset = 2 - (surf -> format -> Rshift >> 3 );
1158
- Uint32 Goffset = 2 - (surf -> format -> Gshift >> 3 );
1159
- Uint32 Boffset = 2 - (surf -> format -> Bshift >> 3 );
1172
+ Uint32 Roffset = 2 - (surf_format -> Rshift >> 3 );
1173
+ Uint32 Goffset = 2 - (surf_format -> Gshift >> 3 );
1174
+ Uint32 Boffset = 2 - (surf_format -> Bshift >> 3 );
1160
1175
#endif
1161
1176
for (y = 0 ; y < dim1 ; ++ y ) {
1162
1177
pixel_p = pixelrow ;
@@ -1206,7 +1221,13 @@ _array_assign_slice(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
1206
1221
Py_ssize_t x ;
1207
1222
Py_ssize_t y ;
1208
1223
1209
- bpp = PG_SURF_BytesPerPixel (surf );
1224
+ PG_PixelFormat * surf_format = PG_GetSurfaceFormat (surf );
1225
+ if (surf_format == NULL ) {
1226
+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
1227
+ return -1 ;
1228
+ }
1229
+
1230
+ bpp = PG_FORMAT_BytesPerPixel (surf_format );
1210
1231
1211
1232
if (!dim1 ) {
1212
1233
dim1 = 1 ;
@@ -1241,13 +1262,13 @@ _array_assign_slice(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high,
1241
1262
} break ;
1242
1263
case 3 : {
1243
1264
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN )
1244
- Uint32 Roffset = surf -> format -> Rshift >> 3 ;
1245
- Uint32 Goffset = surf -> format -> Gshift >> 3 ;
1246
- Uint32 Boffset = surf -> format -> Bshift >> 3 ;
1265
+ Uint32 Roffset = surf_format -> Rshift >> 3 ;
1266
+ Uint32 Goffset = surf_format -> Gshift >> 3 ;
1267
+ Uint32 Boffset = surf_format -> Bshift >> 3 ;
1247
1268
#else
1248
- Uint32 Roffset = 2 - (surf -> format -> Rshift >> 3 );
1249
- Uint32 Goffset = 2 - (surf -> format -> Gshift >> 3 );
1250
- Uint32 Boffset = 2 - (surf -> format -> Bshift >> 3 );
1269
+ Uint32 Roffset = 2 - (surf_format -> Rshift >> 3 );
1270
+ Uint32 Goffset = 2 - (surf_format -> Gshift >> 3 );
1271
+ Uint32 Boffset = 2 - (surf_format -> Bshift >> 3 );
1251
1272
#endif
1252
1273
Uint8 r = (Uint8 )(color >> 16 );
1253
1274
Uint8 g = (Uint8 )(color >> 8 );
@@ -1352,6 +1373,12 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value)
1352
1373
dim1 = 1 ;
1353
1374
}
1354
1375
1376
+ PG_PixelFormat * surf_format = PG_GetSurfaceFormat (surf );
1377
+ if (surf_format == NULL ) {
1378
+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
1379
+ return -1 ;
1380
+ }
1381
+
1355
1382
Py_BEGIN_ALLOW_THREADS ;
1356
1383
/* Single value assignment. */
1357
1384
switch (bpp ) {
@@ -1369,13 +1396,13 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value)
1369
1396
break ;
1370
1397
case 3 : {
1371
1398
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN )
1372
- Uint32 Roffset = surf -> format -> Rshift >> 3 ;
1373
- Uint32 Goffset = surf -> format -> Gshift >> 3 ;
1374
- Uint32 Boffset = surf -> format -> Bshift >> 3 ;
1399
+ Uint32 Roffset = surf_format -> Rshift >> 3 ;
1400
+ Uint32 Goffset = surf_format -> Gshift >> 3 ;
1401
+ Uint32 Boffset = surf_format -> Bshift >> 3 ;
1375
1402
#else
1376
- Uint32 Roffset = 2 - (surf -> format -> Rshift >> 3 );
1377
- Uint32 Goffset = 2 - (surf -> format -> Gshift >> 3 );
1378
- Uint32 Boffset = 2 - (surf -> format -> Bshift >> 3 );
1403
+ Uint32 Roffset = 2 - (surf_format -> Rshift >> 3 );
1404
+ Uint32 Goffset = 2 - (surf_format -> Gshift >> 3 );
1405
+ Uint32 Boffset = 2 - (surf_format -> Bshift >> 3 );
1379
1406
#endif
1380
1407
for (y = 0 ; y < dim1 ; ++ y ) {
1381
1408
pixel_p [Roffset ] = (Uint8 )(color >> 16 );
0 commit comments