Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #2446: rotozoom keeps the colorkey flag. #2491

Merged
merged 16 commits into from
Oct 28, 2023
23 changes: 23 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
@@ -72,6 +72,8 @@
#define PG_ConvertSurface SDL_ConvertSurface
#define PG_ConvertSurfaceFormat SDL_ConvertSurfaceFormat

#define PG_SurfaceHasRLE SDL_SurfaceHasRLE
ankith26 marked this conversation as resolved.
Show resolved Hide resolved

#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
@@ -100,6 +102,27 @@
#define PG_ConvertSurface(src, fmt) SDL_ConvertSurface(src, fmt, 0)
#define PG_ConvertSurfaceFormat(src, pixel_format) \
SDL_ConvertSurfaceFormat(src, pixel_format, 0)
#if SDL_VERSION_ATLEAST(2, 0, 14)
#define PG_SurfaceHasRLE SDL_HasSurfaceRLE
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
#else
// vendored in until our lowest SDL version is 2.0.14
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
#include "_blit_info.h"
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
#define SDL_COPY_RLE_DESIRED 0x00001000

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;
}
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
#endif

#endif

25 changes: 25 additions & 0 deletions src_c/rotozoom.c
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

*/

#include "_pygame.h"
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
#define NO_PYGAME_C_API
#include "pygame.h"

@@ -511,6 +512,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
int dstwidth, dstheight;
int is32bit;
int src_converted;
Uint32 colorkey;

/*
* Sanity check
@@ -583,6 +585,17 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
* Target surface is 32bit with source RGBA/ABGR ordering
*/
rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);
if (SDL_GetColorKey(src, &colorkey) == 0) {
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
SDL_FreeSurface(rz_dst);
return NULL;
}
if (PG_SurfaceHasRLE(src) &&
SDL_SetSurfaceRLE(rz_dst, SDL_TRUE) != 0) {
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
SDL_FreeSurface(rz_dst);
return NULL;
}
}

/*
* Lock source surface
@@ -628,7 +641,19 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
/*
* Target surface is 32bit with source RGBA/ABGR ordering
*/

rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);
if (SDL_GetColorKey(src, &colorkey) == 0) {
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
SDL_FreeSurface(rz_dst);
return NULL;
}
if (PG_SurfaceHasRLE(src) &&
SDL_SetSurfaceRLE(rz_dst, SDL_TRUE) != 0) {
SDL_FreeSurface(rz_dst);
return NULL;
}
}

/*
* Lock source surface
4 changes: 4 additions & 0 deletions src_c/transform.c
Original file line number Diff line number Diff line change
@@ -891,6 +891,10 @@ surf_rotozoom(PyObject *self, PyObject *args, PyObject *kwargs)
Py_BEGIN_ALLOW_THREADS;
newsurf = rotozoomSurface(surf32, angle, scale, 1);
Py_END_ALLOW_THREADS;
if (newsurf == NULL) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return NULL;
}
igordsm marked this conversation as resolved.
Show resolved Hide resolved

if (surf32 == surf)
pgSurface_Unlock(surfobj);
11 changes: 11 additions & 0 deletions test/transform_test.py
Original file line number Diff line number Diff line change
@@ -1287,6 +1287,17 @@ def test_rotozoom(self):
self.assertEqual(s1.get_rect(), pygame.Rect(0, 0, 0, 0))
self.assertEqual(s2.get_rect(), pygame.Rect(0, 0, 0, 0))

def test_rotozoom_keeps_colorkey(self):
image = pygame.Surface((64, 64))
image.set_colorkey("black")
pygame.draw.circle(image, "red", (32, 32), 32, width=0)

no_rot = pygame.transform.rotozoom(image, 0, 1.1)
self.assertEqual(image.get_colorkey(), no_rot.get_colorkey())

with_rot = pygame.transform.rotozoom(image, 5, 1.1)
self.assertEqual(image.get_colorkey(), with_rot.get_colorkey())

def test_invert(self):
surface = pygame.Surface((10, 10), depth=32)