From f0a52d34ee614c77a07780e9c371b86d0a88dcad Mon Sep 17 00:00:00 2001 From: Struma Date: Wed, 28 Apr 2021 22:52:21 -0400 Subject: [PATCH] Support saving bitmaps to JPEG and PNG format --- src/display/bitmap.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/display/bitmap.cpp b/src/display/bitmap.cpp index 5b11e0d..abce165 100644 --- a/src/display/bitmap.cpp +++ b/src/display/bitmap.cpp @@ -931,8 +931,37 @@ void Bitmap::saveToFile(const char *filename) getRaw(surf->pixels, surf->w * surf->h * 4); + // Try and determine the intended image format from the filename extension + const char *period = strrchr(filename, '.'); + int filetype = 0; + if (period) { + period++; + std::string ext; + for (int i = 0; i < (int)strlen(period); i++) { + ext += tolower(period[i]); + } + + if (!ext.compare("png")) { + filetype = 1; + } + else if (!ext.compare("jpg") || !ext.compare("jpeg")) { + filetype = 2; + } + } + std::string fn_normalized = shState->fileSystem().normalize(filename, 1, 1); - int rc = SDL_SaveBMP(surf, fn_normalized.c_str()); + int rc; + switch (filetype) { + case 2: + rc = IMG_SaveJPG(surf, fn_normalized.c_str(), 90); + break; + case 1: + rc = IMG_SavePNG(surf, fn_normalized.c_str()); + break; + case 0: default: + rc = SDL_SaveBMP(surf, fn_normalized.c_str()); + break; + } SDL_FreeSurface(surf); if (rc) throw Exception(Exception::SDLError, "%s", SDL_GetError());