Skip to content

Commit c7e9c15

Browse files
authored
Merge pull request #3570 from pygame-community/ankith26-png-fixes
Fix failing tests on non-libpng backends
2 parents 97aeea6 + 570bd98 commit c7e9c15

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src_c/imageext.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ image_load_ext(PyObject *self, PyObject *arg, PyObject *kwarg)
148148
return RAISE(pgExc_SDLError, IMG_GetError());
149149
}
150150

151+
/* Vendor in fix from https://github.com/libsdl-org/SDL_image/pull/559.
152+
* When that PR is merged this block can be removed. */
153+
if (SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) {
154+
Uint32 colorkey;
155+
#if SDL_VERSION_ATLEAST(3, 0, 0)
156+
if (SDL_GetSurfaceColorKey(surf, &colorkey))
157+
#else
158+
if (SDL_GetColorKey(surf, &colorkey) == 0)
159+
#endif
160+
{
161+
SDL_Palette *pal = PG_GetSurfacePalette(surf);
162+
if (pal && colorkey < (Uint32)pal->ncolors) {
163+
SDL_Color c = pal->colors[colorkey];
164+
c.a = SDL_ALPHA_OPAQUE;
165+
SDL_SetPaletteColors(pal, &c, (int)colorkey, 1);
166+
}
167+
}
168+
}
169+
151170
final = (PyObject *)pgSurface_New(surf);
152171
if (final == NULL) {
153172
SDL_FreeSurface(surf);

test/image_test.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
_sdl_image_ver <= (2, 0, 5) and pygame.get_sdl_byteorder() == pygame.BIG_ENDIAN
2121
)
2222

23+
PG_DEPS_FROM_SYSTEM = "PG_DEPS_FROM_SYSTEM" in os.environ
24+
2325

2426
def check_magic(f, magic_hexes):
2527
"""Tests a given file to see if the magic hex matches."""
@@ -203,6 +205,11 @@ def testSavePNG32(self):
203205
pygame.image.get_sdl_image_version() == (2, 0, 5),
204206
"SDL image 2.0.5 png saving will save this 24 bit as RGBA, causing reader.asRGB8 to fail",
205207
)
208+
@unittest.skipIf(
209+
PG_DEPS_FROM_SYSTEM,
210+
"If we are using system dependencies, we don't know the backend used "
211+
"for PNG saving, and this test only works with libpng.",
212+
)
206213
def testSavePNG24(self):
207214
"""see if we can save a png with color values in the proper channels."""
208215
# Create a PNG file with known colors
@@ -238,6 +245,11 @@ def testSavePNG24(self):
238245
del reader
239246
os.remove(f_path)
240247

248+
@unittest.skipIf(
249+
PG_DEPS_FROM_SYSTEM,
250+
"If we are using system dependencies, we don't know the backend used "
251+
"for PNG saving, and this test only works with libpng.",
252+
)
241253
def testSavePNG8(self):
242254
"""see if we can save an 8 bit png correctly"""
243255
# Create an 8-bit PNG file with known colors
@@ -269,7 +281,7 @@ def testSavePNG8(self):
269281
os.remove(f_path)
270282

271283
@unittest.skipIf(
272-
"PG_DEPS_FROM_SYSTEM" in os.environ,
284+
PG_DEPS_FROM_SYSTEM,
273285
"If we are using system dependencies, we don't know the backend used "
274286
"for PNG saving, and this test only works with libpng.",
275287
)

test/surface_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,12 @@ def test_image_convert_bug_131(self):
12281228
im = pygame.image.load(example_path(os.path.join("data", "city.png")))
12291229
im2 = pygame.image.load(example_path(os.path.join("data", "brick.png")))
12301230

1231-
self.assertEqual(im.get_palette(), ((0, 0, 0, 255), (255, 255, 255, 255)))
1232-
self.assertEqual(im2.get_palette(), ((0, 0, 0, 255), (0, 0, 0, 255)))
1231+
# Only validate the first two entries; stbimage backend gives a 256
1232+
# length palette while libpng doesn't.
1233+
self.assertEqual(
1234+
im.get_palette()[:2], ((0, 0, 0, 255), (255, 255, 255, 255))
1235+
)
1236+
self.assertEqual(im2.get_palette()[:2], ((0, 0, 0, 255), (0, 0, 0, 255)))
12331237

12341238
self.assertEqual(
12351239
repr(im.convert(32)), "<Surface(24x24x32, colorkey=(0, 0, 0, 255))>"

0 commit comments

Comments
 (0)