Skip to content

Fix SRCALPHA TGA image saving #3432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src_c/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
int alpha = 0;
struct TGAheader h;
int srcbpp;
Uint8 surf_alpha;
SDL_BlendMode surf_blendmode;
int have_surf_colorkey = 0;
Uint32 surf_colorkey;
SDL_Rect r;
Expand Down Expand Up @@ -1721,7 +1721,15 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
SDL_PixelFormatEnum output_format;
#endif

SDL_GetSurfaceAlphaMod(surface, &surf_alpha);
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_GetSurfaceBlendMode(surface, &surf_blendmode))
#else
if (SDL_GetSurfaceBlendMode(surface, &surf_blendmode) < 0)
#endif
{
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
if ((have_surf_colorkey = SDL_HasColorKey(surface))) {
SDL_GetColorKey(surface, &surf_colorkey);
}
Expand Down Expand Up @@ -1811,12 +1819,8 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
}
}

/* Temporarily remove colorkey and alpha from surface so copies are
opaque */
SDL_SetSurfaceAlphaMod(surface, SDL_ALPHA_OPAQUE);
if (have_surf_colorkey) {
SDL_SetColorKey(surface, SDL_FALSE, surf_colorkey);
}
/* Temporarily set SDL_BLENDMODE_NONE so that copies are opaque */
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);

r.x = 0;
r.w = surface->w;
Expand Down Expand Up @@ -1847,10 +1851,7 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
}

/* restore flags */
SDL_SetSurfaceAlphaMod(surface, surf_alpha);
if (have_surf_colorkey) {
SDL_SetColorKey(surface, SDL_TRUE, surf_colorkey);
}
SDL_SetSurfaceBlendMode(surface, surf_blendmode);

free(rlebuf);
SDL_FreeSurface(linebuf);
Expand Down
34 changes: 34 additions & 0 deletions test/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io
import os
import pathlib
import random
import tempfile
import unittest
from concurrent.futures import ThreadPoolExecutor
Expand Down Expand Up @@ -409,6 +410,39 @@ def test_save_tga(self):
# clean up the temp file, even if test fails
os.remove(temp_filename)

def test_save_tga_srcalpha(self):
WIDTH = 10
HEIGHT = 10
s = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
pixels = [
[
(
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
for _ in range(WIDTH)
]
for _ in range(HEIGHT)
]
for y in range(HEIGHT):
for x in range(WIDTH):
s.set_at((x, y), pixels[y][x])

with tempfile.NamedTemporaryFile(suffix=".tga", delete=False) as f:
temp_filename = f.name

try:
pygame.image.save(s, temp_filename)
s2 = pygame.image.load(temp_filename)
for y in range(HEIGHT):
for x in range(WIDTH):
self.assertEqual(s2.get_at((x, y)), pixels[y][x])
finally:
# clean up the temp file, even if test fails
os.remove(temp_filename)

def test_save_pathlib(self):
surf = pygame.Surface((1, 1))
surf.fill((23, 23, 23))
Expand Down
Loading