Skip to content

Commit fc05a25

Browse files
authored
Merge pull request #3432 from pygame-community/ankith26-fix-alpha-tga
Fix SRCALPHA TGA image saving
2 parents 3fc1dbe + 3f9d8b6 commit fc05a25

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
@@ -1687,7 +1687,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
16871687
int alpha = 0;
16881688
struct TGAheader h;
16891689
int srcbpp;
1690-
Uint8 surf_alpha;
1690+
SDL_BlendMode surf_blendmode;
16911691
int have_surf_colorkey = 0;
16921692
Uint32 surf_colorkey;
16931693
SDL_Rect r;
@@ -1718,7 +1718,10 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
17181718
SDL_PixelFormatEnum output_format;
17191719
#endif
17201720

1721-
SDL_GetSurfaceAlphaMod(surface, &surf_alpha);
1721+
if (!PG_GetSurfaceBlendMode(surface, &surf_blendmode)) {
1722+
return -1;
1723+
}
1724+
17221725
if ((have_surf_colorkey = SDL_HasColorKey(surface))) {
17231726
SDL_GetColorKey(surface, &surf_colorkey);
17241727
}
@@ -1805,11 +1808,9 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
18051808
}
18061809
}
18071810

1808-
/* Temporarily remove colorkey and alpha from surface so copies are
1809-
opaque */
1810-
SDL_SetSurfaceAlphaMod(surface, SDL_ALPHA_OPAQUE);
1811-
if (have_surf_colorkey) {
1812-
SDL_SetColorKey(surface, SDL_FALSE, surf_colorkey);
1811+
/* Temporarily set SDL_BLENDMODE_NONE so that copies are opaque */
1812+
if (!PG_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE)) {
1813+
goto error;
18131814
}
18141815

18151816
r.x = 0;
@@ -1841,9 +1842,8 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
18411842
}
18421843

18431844
/* restore flags */
1844-
SDL_SetSurfaceAlphaMod(surface, surf_alpha);
1845-
if (have_surf_colorkey) {
1846-
SDL_SetColorKey(surface, SDL_TRUE, surf_colorkey);
1845+
if (!PG_SetSurfaceBlendMode(surface, surf_blendmode)) {
1846+
goto error;
18471847
}
18481848

18491849
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
@@ -426,6 +427,39 @@ def test_save_tga(self):
426427
# clean up the temp file, even if test fails
427428
os.remove(temp_filename)
428429

430+
def test_save_tga_srcalpha(self):
431+
WIDTH = 10
432+
HEIGHT = 10
433+
s = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
434+
pixels = [
435+
[
436+
(
437+
random.randint(0, 255),
438+
random.randint(0, 255),
439+
random.randint(0, 255),
440+
random.randint(0, 255),
441+
)
442+
for _ in range(WIDTH)
443+
]
444+
for _ in range(HEIGHT)
445+
]
446+
for y in range(HEIGHT):
447+
for x in range(WIDTH):
448+
s.set_at((x, y), pixels[y][x])
449+
450+
with tempfile.NamedTemporaryFile(suffix=".tga", delete=False) as f:
451+
temp_filename = f.name
452+
453+
try:
454+
pygame.image.save(s, temp_filename)
455+
s2 = pygame.image.load(temp_filename)
456+
for y in range(HEIGHT):
457+
for x in range(WIDTH):
458+
self.assertEqual(s2.get_at((x, y)), pixels[y][x])
459+
finally:
460+
# clean up the temp file, even if test fails
461+
os.remove(temp_filename)
462+
429463
def test_save_pathlib(self):
430464
surf = pygame.Surface((1, 1))
431465
surf.fill((23, 23, 23))

0 commit comments

Comments
 (0)