Skip to content

Commit

Permalink
Prefer using _SRGB formats + force it if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Aemony committed Jun 26, 2024
1 parent 8c4f89e commit 934b666
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
26 changes: 17 additions & 9 deletions src/SKIV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,8 @@ wWinMain ( _In_ HINSTANCE hInstance,

ImVec2 vDesktopSize (0.0f, 0.0f);

bool SKIV_HDR = (_registry.iHDRMode > 0 && SKIF_Util_IsHDRActive ( ));
bool SKIV_HDR = (_registry.iHDRMode > 0 && SKIF_Util_IsHDRActive ( ));
bool sRGB_Hack = false;

extern CComPtr <ID3D11ShaderResourceView> SKIV_DesktopImage;
CComPtr <ID3D11Resource> pDesktopRes;
Expand All @@ -1799,16 +1800,23 @@ wWinMain ( _In_ HINSTANCE hInstance,

vDesktopSize.x = static_cast <float> (texDesc.Width);
vDesktopSize.y = static_cast <float> (texDesc.Height);

// Non-sRGB DXGI formats still use sRGB gamma so they need a hack to appear properly
if (texDesc.Format == DXGI_FORMAT_R8G8B8A8_UNORM ||
texDesc.Format == DXGI_FORMAT_B8G8R8X8_UNORM)
sRGB_Hack = true;
}
}

static const ImVec2 srgb_uv0 = ImVec2 (-4096.0f, -4096.0f), // SDR is in sRGB colorspace
srgb_uv1 = ImVec2 (-5120.0f, -5120.0f), // SDR is in sRGB colorspace
hdr_uv0 = ImVec2 (-1024.0f, -1024.0f),
hdr_uv1 = ImVec2 (-2048.0f, -2048.0f);
static const ImVec2 srgb_uv0 = ImVec2 (0, 0), // _SRGB format
srgb_uv1 = ImVec2 (1, 1), // _SRGB format
force_srgb_uv0 = ImVec2 (-4096.0f, -4096.0f), // Non-sRGB formats needs a hack to force them to appear properly
force_srgb_uv1 = ImVec2 (-5120.0f, -5120.0f), // Non-sRGB formats needs a hack to force them to appear properly
hdr_uv0 = ImVec2 (-1024.0f, -1024.0f),
hdr_uv1 = ImVec2 (-2048.0f, -2048.0f);

SKIF_ImGui_OptImage (SKIV_DesktopImage, vDesktopSize, (SKIV_HDR) ? hdr_uv0 : srgb_uv0,
(SKIV_HDR) ? hdr_uv1 : srgb_uv1);
SKIF_ImGui_OptImage (SKIV_DesktopImage, vDesktopSize, (SKIV_HDR) ? hdr_uv0 : (sRGB_Hack) ? force_srgb_uv0 : srgb_uv0,
(SKIV_HDR) ? hdr_uv1 : (sRGB_Hack) ? force_srgb_uv1 : srgb_uv1);

ImDrawList* draw_list =
ImGui::GetForegroundDrawList ();
Expand Down Expand Up @@ -2074,8 +2082,8 @@ wWinMain ( _In_ HINSTANCE hInstance,
PLOG_VERBOSE << "DirectX::CopyRectangle ( ): SUCCEEDED";

extern bool
SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, bool isHDR);
if (SKIV_Image_CopyToClipboard (subrect.GetImages (), true, SKIV_HDR))
SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, bool isHDR, bool force_sRGB);
if (SKIV_Image_CopyToClipboard (subrect.GetImages (), true, SKIV_HDR, sRGB_Hack))
PLOG_VERBOSE << "SKIV_Image_CopyToClipboard ( ): SUCCEEDED";
else
PLOG_WARNING << "SKIV_Image_CopyToClipboard ( ): FAILED";
Expand Down
32 changes: 19 additions & 13 deletions src/tabs/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,9 @@ static int getTextureLoadQueuePos (void) {
extern void SKIF_Shell_AddJumpList (std::wstring name, std::wstring path, std::wstring parameters, std::wstring directory, std::wstring icon_path, bool bService);

// Forward declarations
bool SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, bool isHDR);
bool SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, bool isHDR, bool force_sRGB);
HRESULT SKIF_Image_SaveToDisk_HDR (const DirectX::Image& image, const wchar_t* wszFileName);
HRESULT SKIF_Image_SaveToDisk_SDR (const DirectX::Image& image, const wchar_t* wszFileName);
HRESULT SKIF_Image_SaveToDisk_SDR (const DirectX::Image& image, const wchar_t* wszFileName, bool force_sRGB);

// Functions / Structs

Expand Down Expand Up @@ -1479,7 +1479,7 @@ SKIF_UI_Tab_DrawViewer (void)
SUCCEEDED (DirectX::CopyRectangle (*captured_img.GetImages (), src_rect,
*subrect.GetImages (), DirectX::TEX_FILTER_DEFAULT, 0, 0)))
{
if (SKIV_Image_CopyToClipboard (subrect.GetImages (), true, cover.is_hdr))
if (SKIV_Image_CopyToClipboard (subrect.GetImages (), true, cover.is_hdr, false))
{
ImGui::InsertNotification (
{
Expand Down Expand Up @@ -1514,7 +1514,7 @@ SKIF_UI_Tab_DrawViewer (void)

else
{
if (SKIV_Image_CopyToClipboard (captured_img.GetImages (), false, cover.is_hdr))
if (SKIV_Image_CopyToClipboard (captured_img.GetImages (), false, cover.is_hdr, false))
{
ImGui::InsertNotification (
{
Expand Down Expand Up @@ -3125,7 +3125,7 @@ SKIF_UI_Tab_DrawViewer (void)
else
{
hr =
SKIF_Image_SaveToDisk_SDR (*captured_img.GetImages (), pwszFilePath);
SKIF_Image_SaveToDisk_SDR (*captured_img.GetImages (), pwszFilePath, false);
}
}
}
Expand Down Expand Up @@ -3258,14 +3258,14 @@ SKIF_UI_Tab_DrawViewer (void)
if (cover.is_hdr)
{
hr =
SKIF_Image_SaveToDisk_SDR (*captured_img.GetImages (), pwszFilePath);
SKIF_Image_SaveToDisk_SDR (*captured_img.GetImages (), pwszFilePath, false);
}

else
{
// WTF? It's already SDR... oh well, save it anyway
hr =
SKIF_Image_SaveToDisk_SDR (*captured_img.GetImages (), pwszFilePath);
SKIF_Image_SaveToDisk_SDR (*captured_img.GetImages (), pwszFilePath, false);
}
}
}
Expand Down Expand Up @@ -4372,7 +4372,7 @@ void SKIV_HandleCopyShortcut (void)

CComPtr <ID3D11ShaderResourceView> SKIV_DesktopImage;

bool SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, bool isHDR)
bool SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, bool isHDR, bool force_sRGB)
{
if (pImage == nullptr)
return false;
Expand Down Expand Up @@ -4401,7 +4401,7 @@ bool SKIV_Image_CopyToClipboard (const DirectX::Image* pImage, bool snipped, boo
}

else {
if (SUCCEEDED (SKIF_Image_SaveToDisk_SDR (*pImage, wsPNGPath.c_str())))
if (SUCCEEDED (SKIF_Image_SaveToDisk_SDR (*pImage, wsPNGPath.c_str(), force_sRGB)))
{
PLOG_VERBOSE << "SKIF_Image_SaveToDisk_SDR ( ): SUCCEEDED";

Expand Down Expand Up @@ -4485,9 +4485,15 @@ SKIV_Image_CaptureDesktop (DirectX::ScratchImage& image, int flags = 0x0)
return E_NOTIMPL;
}

// The ordering goes from the highest prioritized format to the lowest
DXGI_FORMAT capture_formats [] = {
DXGI_FORMAT_R8G8B8A8_UNORM, // Not HDR...
DXGI_FORMAT_B8G8R8X8_UNORM, // Not HDR...
// SDR:
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, // Prefer SRGB formats as even non-SRGB formats use sRGB gamma
DXGI_FORMAT_R8G8B8A8_UNORM, // The most common format for the desktop
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8X8_UNORM,

// HDR:
DXGI_FORMAT_R10G10B10A2_UNORM,
DXGI_FORMAT_R16G16B16A16_FLOAT,
DXGI_FORMAT_R32G32B32A32_FLOAT
Expand Down Expand Up @@ -4628,7 +4634,7 @@ SKIV_Image_CaptureDesktop (DirectX::ScratchImage& image, int flags = 0x0)
}

HRESULT
SKIF_Image_SaveToDisk_SDR (const DirectX::Image& image, const wchar_t* wszFileName)
SKIF_Image_SaveToDisk_SDR (const DirectX::Image& image, const wchar_t* wszFileName, const bool force_sRGB)
{
using namespace DirectX;

Expand Down Expand Up @@ -4836,7 +4842,7 @@ SKIF_Image_SaveToDisk_SDR (const DirectX::Image& image, const wchar_t* wszFileNa
}

GUID wic_codec;
WIC_FLAGS wic_flags = WIC_FLAGS_DITHER_DIFFUSION;
WIC_FLAGS wic_flags = WIC_FLAGS_DITHER_DIFFUSION | (force_sRGB ? WIC_FLAGS_FORCE_SRGB : WIC_FLAGS_NONE);

if (StrStrIW (wszExtension, L"jpg") ||
StrStrIW (wszExtension, L"jpeg"))
Expand Down

0 comments on commit 934b666

Please sign in to comment.