From f4014f898d7b839a8a808d6ee12b9fc1bcbf66c0 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 29 Apr 2023 15:45:20 -0400 Subject: [PATCH] LibGfx/JPEG: Use `peek_bits` in `next_symbol` This allows us to read all bits in a single shot instead of one by one. --- Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index eef448bea4..98ab7eb354 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -229,15 +229,16 @@ public: ErrorOr next_symbol(HuffmanTableSpec const& table) { - unsigned code = 0; + u16 const code = peek_bits(16); u64 code_cursor = 0; for (int i = 0; i < 16; i++) { // Codes can't be longer than 16 bits. - auto result = TRY(read_bits()); - code = (code << 1) | result; + auto const result = code >> (15 - i); for (int j = 0; j < table.code_counts[i]; j++) { - if (code == table.codes[code_cursor]) + if (result == table.codes[code_cursor]) { + discard_bits(i + 1); return table.symbols[code_cursor]; + } code_cursor++; } }