Skip to content

Commit

Permalink
Fix RLE usage in the transform module (#2535)
Browse files Browse the repository at this point in the history
* Only add RLE flag when original surf had RLE
  • Loading branch information
MyreMylar authored Nov 6, 2023
1 parent 9e76626 commit c1618b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
33 changes: 17 additions & 16 deletions src_c/rotozoom.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@
*/

#include "_pygame.h"

#if !SDL_VERSION_ATLEAST(2, 0, 14)
SDL_bool
PG_SurfaceHasRLE(SDL_Surface *surface)
{
if (surface == NULL) {
return SDL_FALSE;
}

if (!(surface->map->info.flags & SDL_COPY_RLE_DESIRED)) {
return SDL_FALSE;
}

return SDL_TRUE;
}
#endif
#define NO_PYGAME_C_API
#include "pygame.h"

Expand All @@ -48,6 +32,23 @@ typedef struct tColorRGBA {
#define M_PI 3.141592654
#endif

#if !SDL_VERSION_ATLEAST(2, 0, 14)
// Remove this when our minimum version is 2.0.14 or larger
SDL_bool
PG_SurfaceHasRLE(SDL_Surface *surface)
{
if (surface == NULL) {
return SDL_FALSE;
}

if (!(surface->map->info.flags & SDL_COPY_RLE_DESIRED)) {
return SDL_FALSE;
}

return SDL_TRUE;
}
#endif

/*
32bit Zoomer with optional anti-aliasing by bilinear interpolation.
Expand Down
7 changes: 6 additions & 1 deletion src_c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
}

if (SDL_GetColorKey(surf, &colorkey) == 0) {
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0 ||
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
return NULL;
}
if (PG_SurfaceHasRLE(surf) &&
SDL_SetSurfaceRLE(newsurf, SDL_TRUE) != 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(newsurf);
Expand Down
16 changes: 16 additions & 0 deletions test/transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,22 @@ def test_flip_alpha(self):
self.assertEqual(surf.get_at((0, 0)), surf2.get_at((0, 0)))
self.assertEqual(surf2.get_at((0, 0)), (255, 0, 0, 255))

def test_unwanted_rle_not_added(self):
surf = pygame.Surface((16, 16))
surf.fill((255, 0, 0))
surf.set_colorkey((0, 0, 0))
surf.set_alpha(64)

# scale it to the same size (size doesn't matter here)
scaled_surf = pygame.transform.scale(surf, (16, 16))
pygame.Surface((100, 100)).blit(scaled_surf, (0, 0))

self.assertEqual(
surf.get_flags() & pygame.RLEACCEL,
scaled_surf.get_flags() & pygame.RLEACCEL,
)
self.assertEqual(scaled_surf.get_at((8, 8)), (255, 0, 0, 255))


if __name__ == "__main__":
unittest.main()

0 comments on commit c1618b3

Please sign in to comment.