Skip to content

Commit b46fa7d

Browse files
committed
Fix failing tests on non-libpng backends
1 parent 0940fb6 commit b46fa7d

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src_c/imageext.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ 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+
SDL_HasColorKey(surf)) {
155+
Uint32 colorkey = -1;
156+
SDL_GetColorKey(surf, &colorkey);
157+
SDL_Palette *pal = PG_GetSurfacePalette(surf);
158+
if (pal) {
159+
pal->colors[colorkey].a = SDL_ALPHA_OPAQUE;
160+
}
161+
}
162+
151163
final = (PyObject *)pgSurface_New(surf);
152164
if (final == NULL) {
153165
SDL_FreeSurface(surf);

test/image_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ def testSavePNG32(self):
203203
pygame.image.get_sdl_image_version() == (2, 0, 5),
204204
"SDL image 2.0.5 png saving will save this 24 bit as RGBA, causing reader.asRGB8 to fail",
205205
)
206+
@unittest.skipIf(
207+
"PG_DEPS_FROM_SYSTEM" in os.environ,
208+
"If we are using system dependencies, we don't know the backend used "
209+
"for PNG saving, and this test only works with libpng.",
210+
)
206211
def testSavePNG24(self):
207212
"""see if we can save a png with color values in the proper channels."""
208213
# Create a PNG file with known colors
@@ -238,6 +243,11 @@ def testSavePNG24(self):
238243
del reader
239244
os.remove(f_path)
240245

246+
@unittest.skipIf(
247+
"PG_DEPS_FROM_SYSTEM" in os.environ,
248+
"If we are using system dependencies, we don't know the backend used "
249+
"for PNG saving, and this test only works with libpng.",
250+
)
241251
def testSavePNG8(self):
242252
"""see if we can save an 8 bit png correctly"""
243253
# Create an 8-bit PNG file with known colors

test/surface_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,10 @@ 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+
self.assertEqual(
1232+
im.get_palette()[:2], ((0, 0, 0, 255), (255, 255, 255, 255))
1233+
)
1234+
self.assertEqual(im2.get_palette()[:2], ((0, 0, 0, 255), (0, 0, 0, 255)))
12331235

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

0 commit comments

Comments
 (0)