1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:57:44 +00:00

LibGfx: Put code to add AC coefficients to a macroblock in a function

This commit is contained in:
Lucas CHOLLET 2023-02-21 00:46:49 -05:00 committed by Andreas Kling
parent 48f7b93a23
commit 3a8c52cabc

View file

@ -297,40 +297,10 @@ static ErrorOr<void> add_dc(JPEGLoadingContext& context, Macroblock& macroblock,
return {};
}
/**
* Build the macroblocks possible by reading single (MCU) subsampled pair of CbCr.
* Depending on the sampling factors, we may not see triples of y, cb, cr in that
* order. If sample factors differ from one, we'll read more than one block of y-
* coefficients before we get to read a cb-cr block.
* In the function below, `hcursor` and `vcursor` denote the location of the block
* we're building in the macroblock matrix. `vfactor_i` and `hfactor_i` are cursors
* that iterate over the vertical and horizontal subsampling factors, respectively.
* When we finish one iteration of the innermost loop, we'll have the coefficients
* of one of the components of block at position `mb_index`. When the outermost loop
* finishes first iteration, we'll have all the luminance coefficients for all the
* macroblocks that share the chrominance data. Next two iterations (assuming that
* we are dealing with three components) will fill up the blocks with chroma data.
*/
static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks, u32 hcursor, u32 vcursor)
static ErrorOr<void> add_ac(JPEGLoadingContext& context, Macroblock& macroblock, ComponentSpec const& component, unsigned component_index)
{
for (unsigned component_i = 0; component_i < context.components.size(); component_i++) {
auto& component = context.components[component_i];
if (component.dc_destination_id >= context.dc_tables.size())
return Error::from_string_literal("DC destination ID is greater than number of DC tables");
if (component.ac_destination_id >= context.ac_tables.size())
return Error::from_string_literal("AC destination ID is greater than number of AC tables");
for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
Macroblock& block = macroblocks[mb_index];
TRY(add_dc(context, block, component, component_i));
auto& ac_table = context.ac_tables.find(component.ac_destination_id)->value;
auto* select_component = get_component(block, component_i);
auto* select_component = get_component(macroblock, component_index);
// Compute the AC coefficients.
for (int j = 1; j < 64;) {
@ -364,6 +334,42 @@ static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macro
select_component[zigzag_map[j++]] = ac_coefficient;
}
}
return {};
}
/**
* Build the macroblocks possible by reading single (MCU) subsampled pair of CbCr.
* Depending on the sampling factors, we may not see triples of y, cb, cr in that
* order. If sample factors differ from one, we'll read more than one block of y-
* coefficients before we get to read a cb-cr block.
* In the function below, `hcursor` and `vcursor` denote the location of the block
* we're building in the macroblock matrix. `vfactor_i` and `hfactor_i` are cursors
* that iterate over the vertical and horizontal subsampling factors, respectively.
* When we finish one iteration of the innermost loop, we'll have the coefficients
* of one of the components of block at position `mb_index`. When the outermost loop
* finishes first iteration, we'll have all the luminance coefficients for all the
* macroblocks that share the chrominance data. Next two iterations (assuming that
* we are dealing with three components) will fill up the blocks with chroma data.
*/
static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks, u32 hcursor, u32 vcursor)
{
for (unsigned component_i = 0; component_i < context.components.size(); component_i++) {
auto& component = context.components[component_i];
if (component.dc_destination_id >= context.dc_tables.size())
return Error::from_string_literal("DC destination ID is greater than number of DC tables");
if (component.ac_destination_id >= context.ac_tables.size())
return Error::from_string_literal("AC destination ID is greater than number of AC tables");
for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
Macroblock& block = macroblocks[mb_index];
TRY(add_dc(context, block, component, component_i));
TRY(add_ac(context, block, component, component_i));
}
}
}