Skip to content

Commit

Permalink
Mask SDL3: don't set python error without GIL
Browse files Browse the repository at this point in the history
  • Loading branch information
Starbuck5 committed Jan 26, 2025
1 parent 78800a4 commit ddf97c3
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions src_c/mask.c
Original file line number Diff line number Diff line change
Expand Up @@ -956,9 +956,11 @@ palette_colors - this only affects surfaces with a palette
*/

int
bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
Uint32 color, Uint32 threshold, int palette_colors)
void
bitmask_threshold(bitmask_t *m, SDL_Surface *surf, PG_PixelFormat *format,
SDL_Palette *palette, SDL_Surface *surf2,
PG_PixelFormat *format2, Uint32 color, Uint32 threshold,
int palette_colors)
{
int x, y, rshift, gshift, bshift, rshift2, gshift2, bshift2;
int rloss, gloss, bloss, rloss2, gloss2, bloss2;
Expand All @@ -968,13 +970,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
Uint8 r, g, b, a;
Uint8 tr, tg, tb, ta;
int bpp1, bpp2;
PG_PixelFormat *format, *format2;
SDL_Palette *palette, *palette2;

if (!PG_GetSurfaceDetails(surf, &format, &palette)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}

rmask = format->Rmask;
gmask = format->Gmask;
Expand All @@ -988,10 +983,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
bpp1 = PG_FORMAT_BytesPerPixel(format);

if (surf2) {
if (!PG_GetSurfaceDetails(surf2, &format2, &palette2)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
rmask2 = format2->Rmask;
gmask2 = format2->Gmask;
bmask2 = format2->Bmask;
Expand All @@ -1005,12 +996,9 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
bpp2 = PG_FORMAT_BytesPerPixel(format2);
}
else { /* make gcc stop complaining */
format2 = NULL;
palette2 = NULL;
rmask2 = gmask2 = bmask2 = 0;
rshift2 = gshift2 = bshift2 = 0;
rloss2 = gloss2 = bloss2 = 0;
format2 = NULL;
pixels2 = NULL;
bpp2 = 0;
}
Expand Down Expand Up @@ -1120,7 +1108,6 @@ bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
}
}
}
return 0; // No errors!
}

static PyObject *
Expand All @@ -1130,6 +1117,8 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
pgSurfaceObject *surfobj2 = NULL;
pgMaskObject *maskobj = NULL;
SDL_Surface *surf = NULL, *surf2 = NULL;
PG_PixelFormat *surf_format, *surf2_format = NULL;
SDL_Palette *surf_palette;
PyObject *rgba_obj_color, *rgba_obj_threshold = NULL;
Uint8 rgba_threshold[4] = {0, 0, 0, 255};
Uint32 color;
Expand All @@ -1150,6 +1139,17 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
surf2 = pgSurface_AsSurface(surfobj2);
}

if (!PG_GetSurfaceDetails(surf, &surf_format, &surf_palette)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}

if (surf2) {
surf2_format = PG_GetSurfaceFormat(surf2);
if (!surf2_format) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}

if (!pg_MappedColorFromObj(rgba_obj_color, surf, &color,
PG_COLOR_HANDLE_ALL)) {
return NULL;
Expand All @@ -1162,15 +1162,9 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
}
}
else {
#if SDL_VERSION_ATLEAST(3, 0, 0)
color_threshold =
SDL_MapSurfaceRGBA(surf, rgba_threshold[0], rgba_threshold[1],
rgba_threshold[2], rgba_threshold[3]);
#else
color_threshold =
SDL_MapRGBA(surf->format, rgba_threshold[0], rgba_threshold[1],
rgba_threshold[2], rgba_threshold[3]);
#endif
color_threshold = PG_MapRGBA(surf_format, surf_palette,
rgba_threshold[0], rgba_threshold[1],
rgba_threshold[2], rgba_threshold[3]);
}

maskobj = CREATE_MASK_OBJ(surf->w, surf->h, 0);
Expand All @@ -1185,8 +1179,8 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
}

Py_BEGIN_ALLOW_THREADS;
threshold_ret = bitmask_threshold(maskobj->mask, surf, surf2, color,
color_threshold, palette_colors);
bitmask_threshold(maskobj->mask, surf, surf_format, surf_palette, surf2,
surf2_format, color, color_threshold, palette_colors);
Py_END_ALLOW_THREADS;

pgSurface_Unlock(surfobj);
Expand Down

0 comments on commit ddf97c3

Please sign in to comment.