Skip to content

Commit 6e0e0c6

Browse files
authored
Merge pull request #3169 from pygame-community/ankith26-tga-pallete
Fix TGA save bug, add test
2 parents 31b4c18 + e26303b commit 6e0e0c6

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src_c/image.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,10 @@ rle_line(Uint8 *src, Uint8 *dst, int w, int bpp)
15751575
two bytes or more */
15761576
if ((x - x0 - 1) * bpp >= 2 || x == w) {
15771577
/* output previous raw chunks */
1578+
if (x - x0 == 1) {
1579+
/* No need for repeat chunk, do a raw chunk */
1580+
x0++;
1581+
}
15781582
while (raw < x0) {
15791583
int n = MIN(TGA_RLE_MAX, x0 - raw);
15801584
dst[out++] = n - 1;

test/image_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,50 @@ def test_save_tga(self):
363363
# clean up the temp file, even if test fails
364364
os.remove(temp_filename)
365365

366+
# Test palettized
367+
s = pygame.Surface((3, 3), depth=8)
368+
pixels = [
369+
(223, 236, 110, 201),
370+
(33, 82, 34, 26),
371+
(226, 194, 83, 208),
372+
(10, 181, 81, 165),
373+
(220, 95, 96, 11),
374+
(208, 7, 143, 158),
375+
(194, 140, 64, 27),
376+
(215, 152, 89, 126),
377+
(36, 83, 107, 225),
378+
]
379+
result_pixels = [
380+
(255, 219, 85, 255),
381+
(0, 73, 0, 255),
382+
(255, 182, 85, 255),
383+
(0, 182, 85, 255),
384+
(255, 109, 85, 255),
385+
(170, 0, 170, 255),
386+
(170, 146, 85, 255),
387+
(255, 146, 85, 255),
388+
(0, 73, 85, 255),
389+
]
390+
for pixelnum, pixelval in enumerate(pixels):
391+
y, x = divmod(pixelnum, 3)
392+
s.set_at((x, y), pixelval)
393+
394+
# No palette = pygame.error, this asserts there is a palette.
395+
s.get_palette()
396+
397+
with tempfile.NamedTemporaryFile(suffix=".tga", delete=False) as f:
398+
temp_filename = f.name
399+
400+
try:
401+
pygame.image.save(s, temp_filename)
402+
s2 = pygame.image.load(temp_filename)
403+
for pixelnum, pixelval in enumerate(result_pixels):
404+
y, x = divmod(pixelnum, 3)
405+
self.assertEqual(s2.get_at((x, y)), pixelval)
406+
finally:
407+
# clean up the temp file, even if test fails
408+
os.remove(temp_filename)
409+
366410
def test_save_pathlib(self):
367411
surf = pygame.Surface((1, 1))
368412
surf.fill((23, 23, 23))

0 commit comments

Comments
 (0)