Skip to content

Commit

Permalink
Fix issue #2446: rotozoom keeps the colorkey flag.
Browse files Browse the repository at this point in the history
Co-authored-by: Joras <jorascco@al.insper.edu.br>
Co-authored-by: Antonio <antonioarf@al.insper.edu.br>
Co-authored-by: João <joaopga1@al.insper.edu.br>
Co-authored-by: Caio <caioesr@al.insper.edu.br>
Co-authored-by: Cícero <cicerotcv@al.insper.edu.br>
Co-authored-by: Natália <nataliaqmc@al.insper.edu.br>
  • Loading branch information
7 people authored and MyreMylar committed Oct 27, 2023
1 parent b79c4bf commit 23e9a38
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
16 changes: 16 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 @@ -556,6 +557,7 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
/*
* -----------------------
*/

int dstwidthhalf, dstheighthalf;
double sanglezoom, canglezoom, sanglezoominv, canglezoominv;

Expand Down Expand Up @@ -583,6 +585,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 Expand Up @@ -629,6 +638,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
3 changes: 3 additions & 0 deletions src_c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,9 @@ surf_rotozoom(PyObject *self, PyObject *args, PyObject *kwargs)

Py_BEGIN_ALLOW_THREADS;
newsurf = rotozoomSurface(surf32, angle, scale, 1);
if (newsurf == NULL) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
}
Py_END_ALLOW_THREADS;

if (surf32 == surf)
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

0 comments on commit 23e9a38

Please sign in to comment.