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
15 changes: 15 additions & 0 deletions src_c/rotozoom.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
int dstwidth, dstheight;
int is32bit;
int src_converted;
Uint32 colorkey;

/*
* Sanity check
Expand Down Expand Up @@ -583,6 +584,13 @@ 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_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
Expand Down Expand Up @@ -629,6 +637,13 @@ 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_SetSurfaceRLE(rz_dst, SDL_TRUE) != 0) {
SDL_FreeSurface(rz_dst);
return NULL;
}
}

/*
* Lock source surface
Expand Down
4 changes: 4 additions & 0 deletions src_c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions test/transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down