Fix for crash when loading PNG in indexed mode

For example, some icons in SatCom
dev-ui
Piotr Dziwinski 2013-11-15 10:22:11 +01:00
parent be3d92ba03
commit 81b940cc25
2 changed files with 31 additions and 5 deletions

View File

@ -218,15 +218,30 @@ void CImage::PadToNearestPowerOfTwo()
int w = Math::NextPowerOfTwo(m_data->surface->w);
int h = Math::NextPowerOfTwo(m_data->surface->h);
BlitToNewRGBASurface(w, h);
}
void CImage::ConvertToRGBA()
{
assert(m_data != nullptr);
int w = m_data->surface->w;
int h = m_data->surface->h;
BlitToNewRGBASurface(w, h);
}
void CImage::BlitToNewRGBASurface(int width, int height)
{
m_data->surface->flags &= (~SDL_SRCALPHA);
SDL_Surface* resizedSurface = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00,
0x000000ff, 0xff000000);
assert(resizedSurface != NULL);
SDL_BlitSurface(m_data->surface, NULL, resizedSurface, NULL);
SDL_Surface* convertedSurface = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00,
0x000000FF, 0xFF000000);
assert(convertedSurface != nullptr);
SDL_BlitSurface(m_data->surface, nullptr, convertedSurface, nullptr);
SDL_FreeSurface(m_data->surface);
m_data->surface = resizedSurface;
m_data->surface = convertedSurface;
}
/**
@ -376,6 +391,11 @@ bool CImage::Load(const std::string& fileName)
return false;
}
if (m_data->surface->format->palette != nullptr)
{
ConvertToRGBA();
}
return true;
}

View File

@ -97,6 +97,9 @@ public:
//! Pads the image to nearest power of 2 dimensions
void PadToNearestPowerOfTwo();
//! Convert the image to RGBA surface
void ConvertToRGBA();
//! Loads an image from the specified file
bool Load(const std::string &fileName);
@ -107,6 +110,9 @@ public:
std::string GetError();
private:
//! Blit to new RGBA surface with given size
void BlitToNewRGBASurface(int width, int height);
//! Last encountered error
std::string m_error;
//! Image data