Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src_c/imageext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion test/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.",
)
Expand Down
8 changes: 6 additions & 2 deletions test/surface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)), "<Surface(24x24x32, colorkey=(0, 0, 0, 255))>"
Expand Down
Loading