diff --git a/src_c/image.c b/src_c/image.c index 90f52f0988..74bf992e5d 100644 --- a/src_c/image.c +++ b/src_c/image.c @@ -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; diff --git a/test/image_test.py b/test/image_test.py index 8bff1dd037..eaeef65075 100644 --- a/test/image_test.py +++ b/test/image_test.py @@ -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))