Skip to content

Commit

Permalink
Merge pull request #3169 from pygame-community/ankith26-tga-pallete
Browse files Browse the repository at this point in the history
Fix TGA save bug, add test
  • Loading branch information
Matiiss authored Oct 13, 2024
2 parents 31b4c18 + e26303b commit 6e0e0c6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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

0 comments on commit 6e0e0c6

Please sign in to comment.