Skip to content

Commit 3f9d8b6

Browse files
committed
Fix SRCALPHA TGA image saving
1 parent 787650b commit 3f9d8b6

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src_c/image.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
16901690
int alpha = 0;
16911691
struct TGAheader h;
16921692
int srcbpp;
1693-
Uint8 surf_alpha;
1693+
SDL_BlendMode surf_blendmode;
16941694
int have_surf_colorkey = 0;
16951695
Uint32 surf_colorkey;
16961696
SDL_Rect r;
@@ -1721,7 +1721,10 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
17211721
SDL_PixelFormatEnum output_format;
17221722
#endif
17231723

1724-
SDL_GetSurfaceAlphaMod(surface, &surf_alpha);
1724+
if (!PG_GetSurfaceBlendMode(surface, &surf_blendmode)) {
1725+
return -1;
1726+
}
1727+
17251728
if ((have_surf_colorkey = SDL_HasColorKey(surface))) {
17261729
SDL_GetColorKey(surface, &surf_colorkey);
17271730
}
@@ -1811,11 +1814,9 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
18111814
}
18121815
}
18131816

1814-
/* Temporarily remove colorkey and alpha from surface so copies are
1815-
opaque */
1816-
SDL_SetSurfaceAlphaMod(surface, SDL_ALPHA_OPAQUE);
1817-
if (have_surf_colorkey) {
1818-
SDL_SetColorKey(surface, SDL_FALSE, surf_colorkey);
1817+
/* Temporarily set SDL_BLENDMODE_NONE so that copies are opaque */
1818+
if (!PG_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE)) {
1819+
goto error;
18191820
}
18201821

18211822
r.x = 0;
@@ -1847,9 +1848,8 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
18471848
}
18481849

18491850
/* restore flags */
1850-
SDL_SetSurfaceAlphaMod(surface, surf_alpha);
1851-
if (have_surf_colorkey) {
1852-
SDL_SetColorKey(surface, SDL_TRUE, surf_colorkey);
1851+
if (!PG_SetSurfaceBlendMode(surface, surf_blendmode)) {
1852+
goto error;
18531853
}
18541854

18551855
free(rlebuf);

test/image_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io
55
import os
66
import pathlib
7+
import random
78
import tempfile
89
import unittest
910
from concurrent.futures import ThreadPoolExecutor
@@ -414,6 +415,39 @@ def test_save_tga(self):
414415
# clean up the temp file, even if test fails
415416
os.remove(temp_filename)
416417

418+
def test_save_tga_srcalpha(self):
419+
WIDTH = 10
420+
HEIGHT = 10
421+
s = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
422+
pixels = [
423+
[
424+
(
425+
random.randint(0, 255),
426+
random.randint(0, 255),
427+
random.randint(0, 255),
428+
random.randint(0, 255),
429+
)
430+
for _ in range(WIDTH)
431+
]
432+
for _ in range(HEIGHT)
433+
]
434+
for y in range(HEIGHT):
435+
for x in range(WIDTH):
436+
s.set_at((x, y), pixels[y][x])
437+
438+
with tempfile.NamedTemporaryFile(suffix=".tga", delete=False) as f:
439+
temp_filename = f.name
440+
441+
try:
442+
pygame.image.save(s, temp_filename)
443+
s2 = pygame.image.load(temp_filename)
444+
for y in range(HEIGHT):
445+
for x in range(WIDTH):
446+
self.assertEqual(s2.get_at((x, y)), pixels[y][x])
447+
finally:
448+
# clean up the temp file, even if test fails
449+
os.remove(temp_filename)
450+
417451
def test_save_pathlib(self):
418452
surf = pygame.Surface((1, 1))
419453
surf.fill((23, 23, 23))

0 commit comments

Comments
 (0)