From cf6bceeb2c49bab2aeab10473784b347e934ff16 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Tue, 21 Feb 2023 01:16:56 -0500 Subject: [PATCH] LibGfx: Don't assume that scans are always full when filling macroblocks In other words: only consider coefficient of the current scan when adding coefficients to a macroblock. Information about which coefficients are present in the stream are passed through the spectral information in the context. --- Userland/Libraries/LibGfx/JPEGLoader.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/JPEGLoader.cpp b/Userland/Libraries/LibGfx/JPEGLoader.cpp index 4984b3db11..2c5c28ecf2 100644 --- a/Userland/Libraries/LibGfx/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/JPEGLoader.cpp @@ -306,7 +306,11 @@ static ErrorOr add_ac(JPEGLoadingContext& context, Macroblock& macroblock, auto* select_component = get_component(macroblock, component_index); // Compute the AC coefficients. - for (int j = 1; j < 64;) { + + // 0th coefficient is the dc, which is already handled + auto first_coefficient = max(1, context.spectral_selection_start); + + for (int j = first_coefficient; j <= context.spectral_selection_end;) { // AC symbols encode 2 pieces of information, the high 4 bits represent // number of zeroes to be stuffed before reading the coefficient. Low 4 // bits represent the magnitude of the coefficient. @@ -318,7 +322,7 @@ static ErrorOr add_ac(JPEGLoadingContext& context, Macroblock& macroblock, u8 run_length = ac_symbol == 0xF0 ? 16 : ac_symbol >> 4; j += run_length; - if (j >= 64) { + if (j > context.spectral_selection_end) { dbgln_if(JPEG_DEBUG, "Run-length exceeded boundaries. Cursor: {}, Skipping: {}!", j, run_length); return Error::from_string_literal("Run-length exceeded boundaries"); } @@ -371,7 +375,8 @@ static ErrorOr build_macroblocks(JPEGLoadingContext& context, Vector