Skip to content

Commit 310feac

Browse files
committed
Fix SRCALPHA TGA image saving
1 parent c1e8884 commit 310feac

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src_c/image.c

Lines changed: 13 additions & 12 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,15 @@ 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 SDL_VERSION_ATLEAST(3, 0, 0)
1725+
if (!SDL_GetSurfaceBlendMode(surface, &surf_blendmode))
1726+
#else
1727+
if (SDL_GetSurfaceBlendMode(surface, &surf_blendmode) < 0)
1728+
#endif
1729+
{
1730+
PyErr_SetString(pgExc_SDLError, SDL_GetError());
1731+
return -1;
1732+
}
17251733
if ((have_surf_colorkey = SDL_HasColorKey(surface))) {
17261734
SDL_GetColorKey(surface, &surf_colorkey);
17271735
}
@@ -1811,12 +1819,8 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
18111819
}
18121820
}
18131821

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);
1819-
}
1822+
/* Temporarily set SDL_BLENDMODE_NONE so that copies are opaque */
1823+
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
18201824

18211825
r.x = 0;
18221826
r.w = surface->w;
@@ -1847,10 +1851,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
18471851
}
18481852

18491853
/* restore flags */
1850-
SDL_SetSurfaceAlphaMod(surface, surf_alpha);
1851-
if (have_surf_colorkey) {
1852-
SDL_SetColorKey(surface, SDL_TRUE, surf_colorkey);
1853-
}
1854+
SDL_SetSurfaceBlendMode(surface, surf_blendmode);
18541855

18551856
free(rlebuf);
18561857
SDL_FreeSurface(linebuf);

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
@@ -409,6 +410,39 @@ def test_save_tga(self):
409410
# clean up the temp file, even if test fails
410411
os.remove(temp_filename)
411412

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

0 commit comments

Comments
 (0)