Skip to content
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

Fix TGA save bug, add test #3169

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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
4 changes: 4 additions & 0 deletions src_c/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,10 @@ rle_line(Uint8 *src, Uint8 *dst, int w, int bpp)
two bytes or more */
if ((x - x0 - 1) * bpp >= 2 || x == w) {
/* output previous raw chunks */
if (x - x0 == 1) {
/* No need for repeat chunk, do a raw chunk */
x0++;
}
while (raw < x0) {
int n = MIN(TGA_RLE_MAX, x0 - raw);
dst[out++] = n - 1;
Expand Down
44 changes: 44 additions & 0 deletions test/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,50 @@ def test_save_tga(self):
# clean up the temp file, even if test fails
os.remove(temp_filename)

# Test palettized
s = pygame.Surface((3, 3), depth=8)
pixels = [
(223, 236, 110, 201),
(33, 82, 34, 26),
(226, 194, 83, 208),
(10, 181, 81, 165),
(220, 95, 96, 11),
(208, 7, 143, 158),
(194, 140, 64, 27),
(215, 152, 89, 126),
(36, 83, 107, 225),
]
result_pixels = [
(255, 219, 85, 255),
(0, 73, 0, 255),
(255, 182, 85, 255),
(0, 182, 85, 255),
(255, 109, 85, 255),
(170, 0, 170, 255),
(170, 146, 85, 255),
(255, 146, 85, 255),
(0, 73, 85, 255),
]
for pixelnum, pixelval in enumerate(pixels):
y, x = divmod(pixelnum, 3)
s.set_at((x, y), pixelval)

# No palette = pygame.error, this asserts there is a palette.
s.get_palette()

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 pixelnum, pixelval in enumerate(result_pixels):
y, x = divmod(pixelnum, 3)
self.assertEqual(s2.get_at((x, y)), pixelval)
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