diff --git a/src_c/imageext.c b/src_c/imageext.c index 3ab183a2df..18b2cfef98 100644 --- a/src_c/imageext.c +++ b/src_c/imageext.c @@ -148,6 +148,25 @@ image_load_ext(PyObject *self, PyObject *arg, PyObject *kwarg) return RAISE(pgExc_SDLError, IMG_GetError()); } + /* Vendor in fix from https://github.com/libsdl-org/SDL_image/pull/559. + * When that PR is merged this block can be removed. */ + if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) { + Uint32 colorkey; +#if SDL_VERSION_ATLEAST(3, 0, 0) + if (SDL_GetSurfaceColorKey(surf, &colorkey)) +#else + if (SDL_GetColorKey(surf, &colorkey) == 0) +#endif + { + SDL_Palette *pal = PG_GetSurfacePalette(surf); + if (pal && colorkey < (Uint32)pal->ncolors) { + SDL_Color c = pal->colors[colorkey]; + c.a = SDL_ALPHA_OPAQUE; + SDL_SetPaletteColors(pal, &c, (int)colorkey, 1); + } + } + } + final = (PyObject *)pgSurface_New(surf); if (final == NULL) { SDL_FreeSurface(surf); diff --git a/test/image_test.py b/test/image_test.py index a8750e7df9..2af6db9169 100644 --- a/test/image_test.py +++ b/test/image_test.py @@ -20,6 +20,8 @@ _sdl_image_ver <= (2, 0, 5) and pygame.get_sdl_byteorder() == pygame.BIG_ENDIAN ) +PG_DEPS_FROM_SYSTEM = "PG_DEPS_FROM_SYSTEM" in os.environ + def check_magic(f, magic_hexes): """Tests a given file to see if the magic hex matches.""" @@ -203,6 +205,11 @@ def testSavePNG32(self): pygame.image.get_sdl_image_version() == (2, 0, 5), "SDL image 2.0.5 png saving will save this 24 bit as RGBA, causing reader.asRGB8 to fail", ) + @unittest.skipIf( + PG_DEPS_FROM_SYSTEM, + "If we are using system dependencies, we don't know the backend used " + "for PNG saving, and this test only works with libpng.", + ) def testSavePNG24(self): """see if we can save a png with color values in the proper channels.""" # Create a PNG file with known colors @@ -238,6 +245,11 @@ def testSavePNG24(self): del reader os.remove(f_path) + @unittest.skipIf( + PG_DEPS_FROM_SYSTEM, + "If we are using system dependencies, we don't know the backend used " + "for PNG saving, and this test only works with libpng.", + ) def testSavePNG8(self): """see if we can save an 8 bit png correctly""" # Create an 8-bit PNG file with known colors @@ -269,7 +281,7 @@ def testSavePNG8(self): os.remove(f_path) @unittest.skipIf( - "PG_DEPS_FROM_SYSTEM" in os.environ, + PG_DEPS_FROM_SYSTEM, "If we are using system dependencies, we don't know the backend used " "for PNG saving, and this test only works with libpng.", ) diff --git a/test/surface_test.py b/test/surface_test.py index c3bb3d098a..4272aee0bc 100644 --- a/test/surface_test.py +++ b/test/surface_test.py @@ -1228,8 +1228,12 @@ def test_image_convert_bug_131(self): im = pygame.image.load(example_path(os.path.join("data", "city.png"))) im2 = pygame.image.load(example_path(os.path.join("data", "brick.png"))) - self.assertEqual(im.get_palette(), ((0, 0, 0, 255), (255, 255, 255, 255))) - self.assertEqual(im2.get_palette(), ((0, 0, 0, 255), (0, 0, 0, 255))) + # Only validate the first two entries; stbimage backend gives a 256 + # length palette while libpng doesn't. + self.assertEqual( + im.get_palette()[:2], ((0, 0, 0, 255), (255, 255, 255, 255)) + ) + self.assertEqual(im2.get_palette()[:2], ((0, 0, 0, 255), (0, 0, 0, 255))) self.assertEqual( repr(im.convert(32)), ""