From 9389177e5f9bd1d0ce558356f653963f0e438e52 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 29 Apr 2023 20:34:33 -0400 Subject: [PATCH] LibGfx/JPEG: Make `generate_huffman_codes` be a method of `HuffmanTable` And call it when reading the table definition instead of when starting to decode the stream. --- .../LibGfx/ImageFormats/JPEGLoader.cpp | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index be3a0f45b7..7654c87d82 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -186,6 +186,16 @@ struct HuffmanTable { u8 code_counts[16] = { 0 }; Vector symbols; Vector codes; + + void generate_codes() + { + unsigned code = 0; + for (auto number_of_codes : code_counts) { + for (int i = 0; i < number_of_codes; i++) + codes.append(code++); + code <<= 1; + } + } }; class HuffmanStream { @@ -378,16 +388,6 @@ struct JPEGLoadingContext { Optional icc_data; }; -static void generate_huffman_codes(HuffmanTable& table) -{ - unsigned code = 0; - for (auto number_of_codes : table.code_counts) { - for (int i = 0; i < number_of_codes; i++) - table.codes.append(code++); - code <<= 1; - } -} - static inline auto* get_component(Macroblock& block, unsigned component) { switch (component) { @@ -671,13 +671,6 @@ static void reset_decoder(JPEGLoadingContext& context) static ErrorOr decode_huffman_stream(JPEGLoadingContext& context, Vector& macroblocks) { - // Compute huffman codes for DC and AC tables. - for (auto it = context.dc_tables.begin(); it != context.dc_tables.end(); ++it) - generate_huffman_codes(it->value); - - for (auto it = context.ac_tables.begin(); it != context.ac_tables.end(); ++it) - generate_huffman_codes(it->value); - for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) { for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) { u32 i = vcursor * context.mblock_meta.hpadded_count + hcursor; @@ -900,6 +893,8 @@ static ErrorOr read_huffman_table(Stream& stream, JPEGLoadingContext& cont table.symbols.append(symbol); } + table.generate_codes(); + auto& huffman_table = table.type == 0 ? context.dc_tables : context.ac_tables; huffman_table.set(table.destination_id, table); VERIFY(huffman_table.size() <= 2);