@@ -956,9 +956,11 @@ palette_colors - this only affects surfaces with a palette
956
956
957
957
*/
958
958
959
- int
960
- bitmask_threshold (bitmask_t * m , SDL_Surface * surf , SDL_Surface * surf2 ,
961
- Uint32 color , Uint32 threshold , int palette_colors )
959
+ void
960
+ bitmask_threshold (bitmask_t * m , SDL_Surface * surf , PG_PixelFormat * format ,
961
+ SDL_Palette * palette , SDL_Surface * surf2 ,
962
+ PG_PixelFormat * format2 , Uint32 color , Uint32 threshold ,
963
+ int palette_colors )
962
964
{
963
965
int x , y , rshift , gshift , bshift , rshift2 , gshift2 , bshift2 ;
964
966
int rloss , gloss , bloss , rloss2 , gloss2 , bloss2 ;
@@ -968,13 +970,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
968
970
Uint8 r , g , b , a ;
969
971
Uint8 tr , tg , tb , ta ;
970
972
int bpp1 , bpp2 ;
971
- PG_PixelFormat * format , * format2 ;
972
- SDL_Palette * palette , * palette2 ;
973
-
974
- if (!PG_GetSurfaceDetails (surf , & format , & palette )) {
975
- PyErr_SetString (pgExc_SDLError , SDL_GetError ());
976
- return -1 ;
977
- }
978
973
979
974
rmask = format -> Rmask ;
980
975
gmask = format -> Gmask ;
@@ -988,10 +983,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
988
983
bpp1 = PG_FORMAT_BytesPerPixel (format );
989
984
990
985
if (surf2 ) {
991
- if (!PG_GetSurfaceDetails (surf2 , & format2 , & palette2 )) {
992
- PyErr_SetString (pgExc_SDLError , SDL_GetError ());
993
- return -1 ;
994
- }
995
986
rmask2 = format2 -> Rmask ;
996
987
gmask2 = format2 -> Gmask ;
997
988
bmask2 = format2 -> Bmask ;
@@ -1005,12 +996,9 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
1005
996
bpp2 = PG_FORMAT_BytesPerPixel (format2 );
1006
997
}
1007
998
else { /* make gcc stop complaining */
1008
- format2 = NULL ;
1009
- palette2 = NULL ;
1010
999
rmask2 = gmask2 = bmask2 = 0 ;
1011
1000
rshift2 = gshift2 = bshift2 = 0 ;
1012
1001
rloss2 = gloss2 = bloss2 = 0 ;
1013
- format2 = NULL ;
1014
1002
pixels2 = NULL ;
1015
1003
bpp2 = 0 ;
1016
1004
}
@@ -1120,7 +1108,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
1120
1108
}
1121
1109
}
1122
1110
}
1123
- return 0 ; // No errors!
1124
1111
}
1125
1112
1126
1113
static PyObject *
@@ -1130,6 +1117,8 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
1130
1117
pgSurfaceObject * surfobj2 = NULL ;
1131
1118
pgMaskObject * maskobj = NULL ;
1132
1119
SDL_Surface * surf = NULL , * surf2 = NULL ;
1120
+ PG_PixelFormat * surf_format , * surf2_format = NULL ;
1121
+ SDL_Palette * surf_palette ;
1133
1122
PyObject * rgba_obj_color , * rgba_obj_threshold = NULL ;
1134
1123
Uint8 rgba_threshold [4 ] = {0 , 0 , 0 , 255 };
1135
1124
Uint32 color ;
@@ -1150,6 +1139,17 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
1150
1139
surf2 = pgSurface_AsSurface (surfobj2 );
1151
1140
}
1152
1141
1142
+ if (!PG_GetSurfaceDetails (surf , & surf_format , & surf_palette )) {
1143
+ return RAISE (pgExc_SDLError , SDL_GetError ());
1144
+ }
1145
+
1146
+ if (surf2 ) {
1147
+ surf2_format = PG_GetSurfaceFormat (surf2 );
1148
+ if (!surf2_format ) {
1149
+ return RAISE (pgExc_SDLError , SDL_GetError ());
1150
+ }
1151
+ }
1152
+
1153
1153
if (!pg_MappedColorFromObj (rgba_obj_color , surf , & color ,
1154
1154
PG_COLOR_HANDLE_ALL )) {
1155
1155
return NULL ;
@@ -1162,15 +1162,9 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
1162
1162
}
1163
1163
}
1164
1164
else {
1165
- #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1166
- color_threshold =
1167
- SDL_MapSurfaceRGBA (surf , rgba_threshold [0 ], rgba_threshold [1 ],
1168
- rgba_threshold [2 ], rgba_threshold [3 ]);
1169
- #else
1170
- color_threshold =
1171
- SDL_MapRGBA (surf -> format , rgba_threshold [0 ], rgba_threshold [1 ],
1172
- rgba_threshold [2 ], rgba_threshold [3 ]);
1173
- #endif
1165
+ color_threshold = PG_MapRGBA (surf_format , surf_palette ,
1166
+ rgba_threshold [0 ], rgba_threshold [1 ],
1167
+ rgba_threshold [2 ], rgba_threshold [3 ]);
1174
1168
}
1175
1169
1176
1170
maskobj = CREATE_MASK_OBJ (surf -> w , surf -> h , 0 );
@@ -1185,8 +1179,8 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
1185
1179
}
1186
1180
1187
1181
Py_BEGIN_ALLOW_THREADS ;
1188
- threshold_ret = bitmask_threshold (maskobj -> mask , surf , surf2 , color ,
1189
- color_threshold , palette_colors );
1182
+ bitmask_threshold (maskobj -> mask , surf , surf_format , surf_palette , surf2 ,
1183
+ surf2_format , color , color_threshold , palette_colors );
1190
1184
Py_END_ALLOW_THREADS ;
1191
1185
1192
1186
pgSurface_Unlock (surfobj );
0 commit comments