From 79dc857527fff5cc5d0f58acd3198db6dd74d580 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 21 Dec 2017 15:44:49 -0800 Subject: [PATCH] Core: Refactor screenshot saving. So we can reuse the actual saving. --- Core/SaveState.cpp | 2 +- Core/Screenshot.cpp | 83 ++++++++++++++++++++++----------------------- Core/Screenshot.h | 7 ++-- UI/NativeApp.cpp | 2 +- UI/ReportScreen.cpp | 2 +- 5 files changed, 48 insertions(+), 48 deletions(-) diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 60a2012476..1f79ebc143 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -689,7 +689,7 @@ namespace SaveState break; case SAVESTATE_SAVE_SCREENSHOT: - callbackResult = TakeGameScreenshot(op.filename.c_str(), SCREENSHOT_JPG, SCREENSHOT_DISPLAY); + callbackResult = TakeGameScreenshot(op.filename.c_str(), ScreenshotFormat::JPG, SCREENSHOT_DISPLAY); if (!callbackResult) { ERROR_LOG(SAVESTATE, "Failed to take a screenshot for the savestate! %s", op.filename.c_str()); } diff --git a/Core/Screenshot.cpp b/Core/Screenshot.cpp index c423fab9cf..2f02b71cba 100644 --- a/Core/Screenshot.cpp +++ b/Core/Screenshot.cpp @@ -240,54 +240,53 @@ bool TakeGameScreenshot(const char *filename, ScreenshotFormat fmt, ScreenshotTy return false; } -#ifdef USING_QT_UI + u8 *flipbuffer = nullptr; if (success) { - u8 *flipbuffer = nullptr; const u8 *buffer = ConvertBufferTo888RGB(buf, flipbuffer, w, h); - // TODO: Handle other formats (e.g. Direct3D, raw framebuffers.) - QImage image(buffer, w, h, QImage::Format_RGB888); - success = image.save(filename, fmt == SCREENSHOT_PNG ? "PNG" : "JPG"); - delete [] flipbuffer; - } -#else - if (success) { - u8 *flipbuffer = nullptr; - const u8 *buffer = ConvertBufferTo888RGB(buf, flipbuffer, w, h); - if (buffer == nullptr) { - success = false; + success = buffer != nullptr; + if (success) { + if (width) + *width = w; + if (height) + *height = h; + + success = Save888RGBScreenshot(filename, fmt, buffer, w, h); } - - if (width) - *width = w; - if (height) - *height = h; - - if (success && fmt == SCREENSHOT_PNG) { - png_image png; - memset(&png, 0, sizeof(png)); - png.version = PNG_IMAGE_VERSION; - png.format = PNG_FORMAT_RGB; - png.width = w; - png.height = h; - success = WriteScreenshotToPNG(&png, filename, 0, buffer, w * 3, nullptr); - png_image_free(&png); - - if (png.warning_or_error >= 2) { - ERROR_LOG(SYSTEM, "Saving screenshot to PNG produced errors."); - success = false; - } - } else if (success && fmt == SCREENSHOT_JPG) { - jpge::params params; - params.m_quality = 90; - success = WriteScreenshotToJPEG(filename, w, h, 3, buffer, params); - } else { - success = false; - } - delete [] flipbuffer; } -#endif + delete [] flipbuffer; + if (!success) { ERROR_LOG(SYSTEM, "Failed to write screenshot."); } return success; } + +bool Save888RGBScreenshot(const char *filename, ScreenshotFormat fmt, const u8 *bufferRGB888, int w, int h) { +#ifdef USING_QT_UI + QImage image(bufferRGB888, w, h, QImage::Format_RGB888); + return image.save(filename, fmt == ScreenshotFormat::PNG ? "PNG" : "JPG"); +#else + if (fmt == ScreenshotFormat::PNG) { + png_image png; + memset(&png, 0, sizeof(png)); + png.version = PNG_IMAGE_VERSION; + png.format = PNG_FORMAT_RGB; + png.width = w; + png.height = h; + bool success = WriteScreenshotToPNG(&png, filename, 0, bufferRGB888, w * 3, nullptr); + png_image_free(&png); + + if (png.warning_or_error >= 2) { + ERROR_LOG(SYSTEM, "Saving screenshot to PNG produced errors."); + success = false; + } + return success; + } else if (fmt == ScreenshotFormat::JPG) { + jpge::params params; + params.m_quality = 90; + return WriteScreenshotToJPEG(filename, w, h, 3, bufferRGB888, params); + } else { + return false; + } +#endif +} diff --git a/Core/Screenshot.h b/Core/Screenshot.h index b529fad494..75214b54a2 100644 --- a/Core/Screenshot.h +++ b/Core/Screenshot.h @@ -19,9 +19,9 @@ struct GPUDebugBuffer; -enum ScreenshotFormat { - SCREENSHOT_PNG, - SCREENSHOT_JPG, +enum class ScreenshotFormat { + PNG, + JPG, }; enum ScreenshotType { @@ -37,3 +37,4 @@ const u8 *ConvertBufferTo888RGB(const GPUDebugBuffer &buf, u8 *&temp, u32 &w, u3 // Can only be used while in game. bool TakeGameScreenshot(const char *filename, ScreenshotFormat fmt, ScreenshotType type, int *width = nullptr, int *height = nullptr, int maxRes = -1); +bool Save888RGBScreenshot(const char *filename, ScreenshotFormat fmt, const u8 *bufferRGB888, int w, int h); diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 1cc8c8aaeb..bf866a211d 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -763,7 +763,7 @@ void TakeScreenshot() { i++; } - bool success = TakeGameScreenshot(filename, g_Config.bScreenshotsAsPNG ? SCREENSHOT_PNG : SCREENSHOT_JPG, SCREENSHOT_OUTPUT); + bool success = TakeGameScreenshot(filename, g_Config.bScreenshotsAsPNG ? ScreenshotFormat::PNG : ScreenshotFormat::JPG, SCREENSHOT_OUTPUT); if (success) { osm.Show(filename); } else { diff --git a/UI/ReportScreen.cpp b/UI/ReportScreen.cpp index 60003b25ad..ce2556e5f7 100644 --- a/UI/ReportScreen.cpp +++ b/UI/ReportScreen.cpp @@ -247,7 +247,7 @@ void ReportScreen::CreateViews() { } screenshotFilename_ = path + ".reporting.jpg"; int shotWidth = 0, shotHeight = 0; - if (TakeGameScreenshot(screenshotFilename_.c_str(), SCREENSHOT_JPG, SCREENSHOT_DISPLAY, &shotWidth, &shotHeight, 4)) { + if (TakeGameScreenshot(screenshotFilename_.c_str(), ScreenshotFormat::JPG, SCREENSHOT_DISPLAY, &shotWidth, &shotHeight, 4)) { float scale = 340.0f * (1.0f / g_dpi_scale_y) * (1.0f / shotHeight); leftColumnItems->Add(new CheckBox(&includeScreenshot_, rp->T("FeedbackIncludeScreen", "Include a screenshot")))->SetEnabledPtr(&enableReporting_); screenshot_ = leftColumnItems->Add(new AsyncImageFileView(screenshotFilename_, IS_DEFAULT, nullptr, new LinearLayoutParams(shotWidth * scale, shotHeight * scale, Margins(12, 0))));