From cc1bd2d2d9f81e6c24d68c0d314ea7e965d08ff5 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Fri, 23 Jun 2023 19:01:03 -0400 Subject: [PATCH] LibGfx/JPEG: Use a look-up table for cosine values This solution is a middle ground between re-computing `cos` every time and a much more mathematically complicated approach (as we have in the decoder). While still being far from optimal it already gives us a 10x improvement, not that bad :^) Co-authored-by: Tim Flynn --- .../LibGfx/ImageFormats/JPEGWriter.cpp | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp index 43114680e1..ba7eb37e46 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp @@ -136,20 +136,35 @@ public: return {}; } + static Array create_cosine_lookup_table() + { + static constexpr double pi_over_16 = AK::Pi / 16; + + Array table; + + for (u8 u = 0; u < 8; ++u) { + for (u8 x = 0; x < 8; ++x) + table[u * 8 + x] = cos((2 * x + 1) * u * pi_over_16); + } + + return table; + } + void fdct_and_quantization() { + static auto cosine_table = create_cosine_lookup_table(); + for (auto& macroblock : m_macroblocks) { - constexpr double pi_over_16 = AK::Pi / 16; constexpr double inverse_sqrt_2 = M_SQRT1_2; - auto const convert_one_component = [](i16 component[], QuantizationTable const& table) { + auto const convert_one_component = [&](i16 component[], QuantizationTable const& table) { Array result {}; - auto const sum_xy = [&component](u8 u, u8 v) { + auto const sum_xy = [&](u8 u, u8 v) { double sum {}; for (u8 x {}; x < 8; ++x) { for (u8 y {}; y < 8; ++y) - sum += component[x * 8 + y] * cos((2 * x + 1) * u * pi_over_16) * cos((2 * y + 1) * v * pi_over_16); + sum += component[x * 8 + y] * cosine_table[u * 8 + x] * cosine_table[v * 8 + y]; } return sum; };