Skip to content

Commit c1618b3

Browse files
authored
Fix RLE usage in the transform module (#2535)
* Only add RLE flag when original surf had RLE
1 parent 9e76626 commit c1618b3

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src_c/rotozoom.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,6 @@
1212
*/
1313

1414
#include "_pygame.h"
15-
16-
#if !SDL_VERSION_ATLEAST(2, 0, 14)
17-
SDL_bool
18-
PG_SurfaceHasRLE(SDL_Surface *surface)
19-
{
20-
if (surface == NULL) {
21-
return SDL_FALSE;
22-
}
23-
24-
if (!(surface->map->info.flags & SDL_COPY_RLE_DESIRED)) {
25-
return SDL_FALSE;
26-
}
27-
28-
return SDL_TRUE;
29-
}
30-
#endif
3115
#define NO_PYGAME_C_API
3216
#include "pygame.h"
3317

@@ -48,6 +32,23 @@ typedef struct tColorRGBA {
4832
#define M_PI 3.141592654
4933
#endif
5034

35+
#if !SDL_VERSION_ATLEAST(2, 0, 14)
36+
// Remove this when our minimum version is 2.0.14 or larger
37+
SDL_bool
38+
PG_SurfaceHasRLE(SDL_Surface *surface)
39+
{
40+
if (surface == NULL) {
41+
return SDL_FALSE;
42+
}
43+
44+
if (!(surface->map->info.flags & SDL_COPY_RLE_DESIRED)) {
45+
return SDL_FALSE;
46+
}
47+
48+
return SDL_TRUE;
49+
}
50+
#endif
51+
5152
/*
5253
5354
32bit Zoomer with optional anti-aliasing by bilinear interpolation.

src_c/transform.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,12 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
179179
}
180180

181181
if (SDL_GetColorKey(surf, &colorkey) == 0) {
182-
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0 ||
182+
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
183+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
184+
SDL_FreeSurface(newsurf);
185+
return NULL;
186+
}
187+
if (PG_SurfaceHasRLE(surf) &&
183188
SDL_SetSurfaceRLE(newsurf, SDL_TRUE) != 0) {
184189
PyErr_SetString(pgExc_SDLError, SDL_GetError());
185190
SDL_FreeSurface(newsurf);

test/transform_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,22 @@ def test_flip_alpha(self):
16271627
self.assertEqual(surf.get_at((0, 0)), surf2.get_at((0, 0)))
16281628
self.assertEqual(surf2.get_at((0, 0)), (255, 0, 0, 255))
16291629

1630+
def test_unwanted_rle_not_added(self):
1631+
surf = pygame.Surface((16, 16))
1632+
surf.fill((255, 0, 0))
1633+
surf.set_colorkey((0, 0, 0))
1634+
surf.set_alpha(64)
1635+
1636+
# scale it to the same size (size doesn't matter here)
1637+
scaled_surf = pygame.transform.scale(surf, (16, 16))
1638+
pygame.Surface((100, 100)).blit(scaled_surf, (0, 0))
1639+
1640+
self.assertEqual(
1641+
surf.get_flags() & pygame.RLEACCEL,
1642+
scaled_surf.get_flags() & pygame.RLEACCEL,
1643+
)
1644+
self.assertEqual(scaled_surf.get_at((8, 8)), (255, 0, 0, 255))
1645+
16301646

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

0 commit comments

Comments
 (0)