From 9a317267de78aa48a9326ad5416f7dee617f8b08 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 1 Jun 2023 22:41:28 -0400 Subject: [PATCH] LibGfx/JPEG: Make `JPEGLoadingContext::current_scan` optional This allows us to construct a context without calling `Scan`'s constructor. --- .../LibGfx/ImageFormats/JPEGLoader.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index a95810f7a1..2e4560f01b 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -515,7 +515,7 @@ struct JPEGLoadingContext { u8 hsample_factor { 0 }; u8 vsample_factor { 0 }; - Scan current_scan; + Optional current_scan {}; Vector components; RefPtr bitmap; @@ -568,12 +568,12 @@ static ErrorOr add_dc(JPEGLoadingContext& context, Macroblock& macroblock, } auto& dc_table = maybe_table.value(); - auto& scan = context.current_scan; + auto& scan = *context.current_scan; auto* select_component = get_component(macroblock, scan_component.component.index); auto& coefficient = select_component[0]; - if (context.current_scan.successive_approximation_high > 0) { + if (scan.successive_approximation_high > 0) { TRY(refine_coefficient(scan, coefficient)); return {}; } @@ -644,7 +644,7 @@ static ErrorOr add_ac(JPEGLoadingContext& context, Macroblock& macroblock, auto& ac_table = maybe_table.value(); auto* select_component = get_component(macroblock, scan_component.component.index); - auto& scan = context.current_scan; + auto& scan = *context.current_scan; // Compute the AC coefficients. @@ -755,12 +755,12 @@ static ErrorOr add_ac(JPEGLoadingContext& context, Macroblock& macroblock, */ static ErrorOr build_macroblocks(JPEGLoadingContext& context, Vector& macroblocks, u32 hcursor, u32 vcursor) { - for (auto const& scan_component : context.current_scan.components) { + for (auto const& scan_component : context.current_scan->components) { for (u8 vfactor_i = 0; vfactor_i < scan_component.component.vsample_factor; vfactor_i++) { for (u8 hfactor_i = 0; hfactor_i < scan_component.component.hsample_factor; hfactor_i++) { // A.2.3 - Interleaved order u32 macroblock_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor); - if (!context.current_scan.are_components_interleaved()) { + if (!context.current_scan->are_components_interleaved()) { macroblock_index = vcursor * context.mblock_meta.hpadded_count + (hfactor_i + (hcursor * scan_component.component.vsample_factor) + (vfactor_i * scan_component.component.hsample_factor)); // A.2.4 Completion of partial MCU @@ -777,14 +777,14 @@ static ErrorOr build_macroblocks(JPEGLoadingContext& context, Vectorspectral_selection_start == 0) TRY(add_dc(context, block, scan_component)); - if (context.current_scan.spectral_selection_end != 0) + 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; + if (context.current_scan->end_of_bands_run_count > 0) { + --context.current_scan->end_of_bands_run_count; continue; } } @@ -809,7 +809,7 @@ static bool is_dct_based(StartOfFrame::FrameType frame_type) static void reset_decoder(JPEGLoadingContext& context) { // G.1.2.2 - Progressive encoding of AC coefficients with Huffman coding - context.current_scan.end_of_bands_run_count = 0; + context.current_scan->end_of_bands_run_count = 0; // E.2.4 Control procedure for decoding a restart interval if (is_dct_based(context.frame.type)) { @@ -826,7 +826,7 @@ static ErrorOr decode_huffman_stream(JPEGLoadingContext& context, Vectorhuffman_stream; if (context.dc_restart_interval > 0) { if (i != 0 && i % (context.dc_restart_interval * context.vsample_factor * context.hsample_factor) == 0) {