Skip to content

Commit e757512

Browse files
authored
Merge pull request #2031 from Temmie3754/surface-fixes
Fix issue with converting surfaces to 8 bit surfaces with palettes
2 parents c172028 + 7883b33 commit e757512

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src_c/surface.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
14921492
/* will be updated later, initialize to make static analyzer happy
14931493
*/
14941494
int bpp = 0;
1495+
SDL_Palette *palette = SDL_AllocPalette(default_palette_size);
14951496
SDL_PixelFormat format;
14961497

14971498
memcpy(&format, surf->format, sizeof(format));
@@ -1591,8 +1592,24 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
15911592
* empty-- that at least one entry is not black.
15921593
*/
15931594
format.palette = NULL;
1595+
if (SDL_ISPIXELFORMAT_INDEXED(SDL_MasksToPixelFormatEnum(
1596+
format.BitsPerPixel, format.Rmask, format.Gmask,
1597+
format.Bmask, format.Amask))) {
1598+
if (SDL_ISPIXELFORMAT_INDEXED(surf->format->format)) {
1599+
SDL_SetPixelFormatPalette(&format, surf->format->palette);
1600+
}
1601+
else {
1602+
/* Give the surface something other than an all white
1603+
* palette.
1604+
*/
1605+
SDL_SetPaletteColors(palette, default_palette_colors, 0,
1606+
default_palette_size);
1607+
SDL_SetPixelFormatPalette(&format, palette);
1608+
}
1609+
}
15941610
newsurf = SDL_ConvertSurface(surf, &format, 0);
15951611
SDL_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE);
1612+
SDL_FreePalette(palette);
15961613
}
15971614
}
15981615
else {

test/surface_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,22 @@ def test_convert_alpha_SRCALPHA(self):
12321232
finally:
12331233
pygame.display.quit()
12341234

1235+
def test_convert_palettize(self):
1236+
pygame.display.init()
1237+
try:
1238+
pygame.display.set_mode((640, 480))
1239+
1240+
surf = pygame.Surface((150, 250))
1241+
surf.fill((255, 50, 0))
1242+
surf = surf.convert(8)
1243+
surf2 = pygame.Surface((150, 250), depth=8)
1244+
surf2.fill((255, 50, 0))
1245+
1246+
self.assertEqual(surf.get_at((50, 50)), surf2.get_at((50, 50)))
1247+
1248+
finally:
1249+
pygame.display.quit()
1250+
12351251
def test_src_alpha_issue_1289(self):
12361252
"""blit should be white."""
12371253
surf1 = pygame.Surface((1, 1), pygame.SRCALPHA, 32)

0 commit comments

Comments
 (0)