Browse Source

LibGfx/JPEG: Use `peek_bits` in `next_symbol`

This allows us to read all bits in a single shot instead of one by one.
Lucas CHOLLET 2 năm trước cách đây
mục cha
commit
f4014f898d
1 tập tin đã thay đổi với 5 bổ sung4 xóa
  1. 5 4
      Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp

+ 5 - 4
Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp

@@ -229,15 +229,16 @@ public:
 
     ErrorOr<u8> 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++;
             }
         }