From 902d0ab58e5b022c6b1ba9816fee9d77def2c747 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sun, 26 Feb 2023 16:47:24 -0500 Subject: [PATCH] LibGfx/JPEG: Still iterate over AC coefficients of a EOB targeted block This commit is nonsense for anything else than SOF2 images with spectral approximation. For this particular case, skips like EOB or ZRL only apply to coefficients with a zero-history. This commit prepares the code to handle this behavior. --- .../LibGfx/ImageFormats/JPEGLoader.cpp | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index 4728485b4c..0f462e2450 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -365,6 +365,12 @@ static ErrorOr read_eob(Scan& scan, u32 symbol) scan.end_of_bands_run_count = additional_value + (1 << eob_base) - 1; + if (scan.end_of_bands_run_count >= 1) { + // end_of_bands_run_count is decremented at the end of `build_macroblocks`. + // This line compensate this effect. + ++scan.end_of_bands_run_count; + } + return true; } @@ -390,6 +396,9 @@ static ErrorOr add_ac(JPEGLoadingContext& context, Macroblock& macroblock, auto first_coefficient = max(1, scan.spectral_selection_start); for (int j = first_coefficient; j <= scan.spectral_selection_end;) { + if (scan.end_of_bands_run_count > 0) + continue; + // 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. @@ -462,18 +471,18 @@ static ErrorOr build_macroblocks(JPEGLoadingContext& context, Vector 0) { - --context.current_scan.end_of_bands_run_count; - continue; - } - Macroblock& block = macroblocks[macroblock_index]; if (context.current_scan.spectral_selection_start == 0) TRY(add_dc(context, block, scan_component)); if (context.current_scan.spectral_selection_end != 0) TRY(add_ac(context, block, scan_component)); + + // G.1.2.2 - Progressive encoding of AC coefficients with Huffman coding + if (context.current_scan.end_of_bands_run_count > 0) { + --context.current_scan.end_of_bands_run_count; + continue; + } } } }