@@ -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 {
0 commit comments