|
10 | 10 | *****************************************************************************/ |
11 | 11 |
|
12 | 12 | #include "StdInc.h" |
| 13 | +#include "DXHook/CProxyDirect3DDevice9.h" |
| 14 | +#include <math.h> |
13 | 15 | #include <libpng/png.h> |
14 | 16 |
|
15 | 17 | extern CCore* g_pCore; |
@@ -38,6 +40,86 @@ static uint ms_uiHeight = 0; |
38 | 40 | // whether we want to actually save photo in documents folder |
39 | 41 | static bool savePhotoInDocuments = false; |
40 | 42 |
|
| 43 | +namespace |
| 44 | +{ |
| 45 | +float Clamp(float value, float minValue, float maxValue) |
| 46 | +{ |
| 47 | + if (value < minValue) |
| 48 | + return minValue; |
| 49 | + if (value > maxValue) |
| 50 | + return maxValue; |
| 51 | + return value; |
| 52 | +} |
| 53 | + |
| 54 | +void ApplyBorderlessAdjustmentsToBuffer(void* rawData, uint width, uint height) |
| 55 | +{ |
| 56 | + if (!rawData || width == 0 || height == 0) |
| 57 | + return; |
| 58 | + |
| 59 | + bool isBorderless = false; |
| 60 | + if (CVideoModeManagerInterface* videoModeManager = GetVideoModeManager()) |
| 61 | + isBorderless = videoModeManager->IsDisplayModeWindowed() || videoModeManager->IsDisplayModeFullScreenWindow(); |
| 62 | + |
| 63 | + if (!isBorderless && ::g_pDeviceState) |
| 64 | + isBorderless = (::g_pDeviceState->CreationState.PresentationParameters.Windowed != 0); |
| 65 | + |
| 66 | + float gammaPower = 1.0f; |
| 67 | + float brightnessScale = 1.0f; |
| 68 | + float contrastScale = 1.0f; |
| 69 | + float saturationScale = 1.0f; |
| 70 | + bool applyWindowed = true; |
| 71 | + bool applyFullscreen = false; |
| 72 | + ::BorderlessGamma::FetchSettings(gammaPower, brightnessScale, contrastScale, saturationScale, applyWindowed, applyFullscreen); |
| 73 | + |
| 74 | + const bool adjustmentsEnabled = isBorderless ? applyWindowed : applyFullscreen; |
| 75 | + if (!adjustmentsEnabled) |
| 76 | + return; |
| 77 | + |
| 78 | + if (!::BorderlessGamma::ShouldApplyAdjustments(gammaPower, brightnessScale, contrastScale, saturationScale)) |
| 79 | + return; |
| 80 | + |
| 81 | + BYTE* data = static_cast<BYTE*>(rawData); |
| 82 | + const size_t pixelCount = static_cast<size_t>(width) * static_cast<size_t>(height); |
| 83 | + const float inv255 = 1.0f / 255.0f; |
| 84 | + const float contrastPivot = 0.5f; |
| 85 | + |
| 86 | + for (size_t i = 0; i < pixelCount; ++i) |
| 87 | + { |
| 88 | + float r = Clamp(data[0] * inv255, 0.0f, 1.0f); |
| 89 | + float g = Clamp(data[1] * inv255, 0.0f, 1.0f); |
| 90 | + float b = Clamp(data[2] * inv255, 0.0f, 1.0f); |
| 91 | + |
| 92 | + r = powf(r, gammaPower); |
| 93 | + g = powf(g, gammaPower); |
| 94 | + b = powf(b, gammaPower); |
| 95 | + |
| 96 | + r *= brightnessScale; |
| 97 | + g *= brightnessScale; |
| 98 | + b *= brightnessScale; |
| 99 | + |
| 100 | + r = (r - contrastPivot) * contrastScale + contrastPivot; |
| 101 | + g = (g - contrastPivot) * contrastScale + contrastPivot; |
| 102 | + b = (b - contrastPivot) * contrastScale + contrastPivot; |
| 103 | + |
| 104 | + float luminance = Clamp(0.299f * r + 0.587f * g + 0.114f * b, 0.0f, 1.0f); |
| 105 | + |
| 106 | + r = luminance + (r - luminance) * saturationScale; |
| 107 | + g = luminance + (g - luminance) * saturationScale; |
| 108 | + b = luminance + (b - luminance) * saturationScale; |
| 109 | + |
| 110 | + r = Clamp(r, 0.0f, 1.0f); |
| 111 | + g = Clamp(g, 0.0f, 1.0f); |
| 112 | + b = Clamp(b, 0.0f, 1.0f); |
| 113 | + |
| 114 | + data[0] = static_cast<BYTE>(r * 255.0f + 0.5f); |
| 115 | + data[1] = static_cast<BYTE>(g * 255.0f + 0.5f); |
| 116 | + data[2] = static_cast<BYTE>(b * 255.0f + 0.5f); |
| 117 | + |
| 118 | + data += 4; |
| 119 | + } |
| 120 | +} |
| 121 | +} // namespace |
| 122 | + |
41 | 123 | void CScreenShot::InitiateScreenShot(bool bIsCameraShot) |
42 | 124 | { |
43 | 125 | if (ms_bScreenShot || ms_bIsSaving || IsRateLimited(bIsCameraShot)) |
@@ -109,6 +191,7 @@ void CScreenShot::CheckForScreenShot(bool bBeforeGUI) |
109 | 191 |
|
110 | 192 | if (uiDataSize == uiReqDataSize) |
111 | 193 | { |
| 194 | + ApplyBorderlessAdjustmentsToBuffer(ms_ScreenShotBuffer.GetData(), ms_uiWidth, ms_uiHeight); |
112 | 195 | // Start the save thread |
113 | 196 | StartSaveThread(); |
114 | 197 | } |
|
0 commit comments