From d577d181e36c0376dacf572542ce3e2013941286 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 19 Dec 2023 21:44:40 -0500 Subject: [PATCH] LibPDF: Clamp linear_srgb values in convert_to_srgb() This is very crude gamut mapping, but it's better than producing NaNs when passing negative values to powf(x, 1/2.2). --- Userland/Libraries/LibPDF/ColorSpace.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Userland/Libraries/LibPDF/ColorSpace.cpp b/Userland/Libraries/LibPDF/ColorSpace.cpp index c420f4e002..a8f0383180 100644 --- a/Userland/Libraries/LibPDF/ColorSpace.cpp +++ b/Userland/Libraries/LibPDF/ColorSpace.cpp @@ -302,6 +302,9 @@ constexpr Array convert_to_srgb(Array xyz) }; auto linear_srgb = matrix_multiply(conversion_matrix, xyz); + linear_srgb[0] = clamp(linear_srgb[0], 0.0f, 1.0f); + linear_srgb[1] = clamp(linear_srgb[1], 0.0f, 1.0f); + linear_srgb[2] = clamp(linear_srgb[2], 0.0f, 1.0f); // FIXME: Use the real sRGB curve by replacing this function with Gfx::ICC::sRGB().from_pcs(). return { pow(linear_srgb[0], 1.0f / 2.2f), pow(linear_srgb[1], 1.0f / 2.2f), pow(linear_srgb[2], 1.0f / 2.2f) };