Fixed image file extension detection

Previously this would fail on `jpeg` and would detect something such as
`notajpg` as `jpg`. This fixes #929.
This commit is contained in:
Mario Liebisch 2015-07-23 09:03:15 +02:00 committed by Lukas Dürrenberger
parent 561eb82f14
commit b3b48a507d

View File

@ -227,35 +227,34 @@ bool ImageLoader::saveImageToFile(const std::string& filename, const std::vector
if (!pixels.empty() && (size.x > 0) && (size.y > 0)) if (!pixels.empty() && (size.x > 0) && (size.y > 0))
{ {
// Deduce the image type from its extension // Deduce the image type from its extension
if (filename.size() > 3)
{
// Extract the extension
std::string extension = toLower(filename.substr(filename.size() - 3));
if (extension == "bmp") // Extract the extension
{ const std::size_t dot = filename.find_last_of('.');
// BMP format const std::string extension = dot != std::string::npos ? toLower(filename.substr(dot + 1)) : "";
if (stbi_write_bmp(filename.c_str(), size.x, size.y, 4, &pixels[0]))
return true; if (extension == "bmp")
} {
else if (extension == "tga") // BMP format
{ if (stbi_write_bmp(filename.c_str(), size.x, size.y, 4, &pixels[0]))
// TGA format return true;
if (stbi_write_tga(filename.c_str(), size.x, size.y, 4, &pixels[0])) }
return true; else if (extension == "tga")
} {
else if (extension == "png") // TGA format
{ if (stbi_write_tga(filename.c_str(), size.x, size.y, 4, &pixels[0]))
// PNG format return true;
if (stbi_write_png(filename.c_str(), size.x, size.y, 4, &pixels[0], 0)) }
return true; else if (extension == "png")
} {
else if (extension == "jpg") // PNG format
{ if (stbi_write_png(filename.c_str(), size.x, size.y, 4, &pixels[0], 0))
// JPG format return true;
if (writeJpg(filename, pixels, size.x, size.y)) }
return true; else if (extension == "jpg" || extension == "jpeg")
} {
// JPG format
if (writeJpg(filename, pixels, size.x, size.y))
return true;
} }
} }