Skip to content

Commit

Permalink
LibGfx/JPEG: Still iterate over AC coefficients of a EOB targeted block
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LucasChollet authored and linusg committed Apr 3, 2023
1 parent ef98b06 commit 902d0ab
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ static ErrorOr<bool> 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;
}

Expand All @@ -390,6 +396,9 @@ static ErrorOr<void> 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.
Expand Down Expand Up @@ -462,18 +471,18 @@ static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macro
continue;
}

// 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;
}

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;
}
}
}
}
Expand Down

0 comments on commit 902d0ab

Please sign in to comment.