From fcaebe56d7a59c7c390927954fdf568d7dc7bcb2 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 9 Nov 2023 22:46:37 -0500 Subject: [PATCH] LibCompress/LZW: Use a parameter to choose when to change the code size Some users of the LZW algorithm use a different value to determine when the size of the code changes. GIF increments the size when the number of elements in the table is equal to 2^code_size while TIFF does it for a count of 2^code_size - 1. This patch adds the parameter m_offset_for_size_change with a default value of 0 and our decoder will increment the code size when we reach a table length of 2^code_size + m_offset_for_size_change. This allows us to support both situations. --- Userland/Libraries/LibCompress/LZWDecoder.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCompress/LZWDecoder.h b/Userland/Libraries/LibCompress/LZWDecoder.h index e0e1af8711..19d24c7e45 100644 --- a/Userland/Libraries/LibCompress/LZWDecoder.h +++ b/Userland/Libraries/LibCompress/LZWDecoder.h @@ -22,11 +22,13 @@ private: static constexpr int max_code_size = 12; public: - explicit LZWDecoder(MaybeOwned lzw_stream, u8 min_code_size) + explicit LZWDecoder(MaybeOwned lzw_stream, u8 min_code_size, i32 offset_for_size_change = 0) : m_bit_stream(move(lzw_stream)) , m_code_size(min_code_size) , m_original_code_size(min_code_size) , m_table_capacity(AK::exp2(min_code_size)) + , m_offset_for_size_change(offset_for_size_change) + { init_code_table(); } @@ -102,7 +104,7 @@ private: { if (entry.size() > 1 && m_code_table.size() < 4096) { m_code_table.append(entry); - if (m_code_table.size() >= m_table_capacity && m_code_size < max_code_size) { + if (m_code_table.size() >= (m_table_capacity + m_offset_for_size_change) && m_code_size < max_code_size) { ++m_code_size; m_table_capacity *= 2; } @@ -118,6 +120,7 @@ private: u8 m_original_code_size { 0 }; u32 m_table_capacity { 0 }; + i32 m_offset_for_size_change {}; u16 m_current_code { 0 }; Vector m_output {};