From 714ee9f862b67792b5f91ebdc28969d63a6c5f3e Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sun, 14 Jan 2024 18:48:43 -0500 Subject: [PATCH] LibGfx/JPEG: Allow decoding more subsampling factors We now allow all subsampling factors where the subsampling factors of follow-on components evenly decode the ones of the first component. In practice, this allows YCCK 2111, CMYK 2112, and CMYK 2111. --- Tests/LibGfx/TestImageDecoder.cpp | 2 +- Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp index ffd47e6a3e6b288..a89fdd5c642e1ae 100644 --- a/Tests/LibGfx/TestImageDecoder.cpp +++ b/Tests/LibGfx/TestImageDecoder.cpp @@ -314,7 +314,7 @@ TEST_CASE(test_jpeg_ycck) { Array test_inputs = { TEST_INPUT("jpg/ycck-1111.jpg"sv), - // TEST_INPUT("jpg/ycck-2111.jpg"sv), // FIXME: Enable once this decodes correctly + TEST_INPUT("jpg/ycck-2111.jpg"sv), TEST_INPUT("jpg/ycck-2112.jpg"sv), }; diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index cb851bef9f12f8f..ab1a477687783da 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -1287,12 +1287,9 @@ static ErrorOr read_start_of_frame(JPEGStream& stream, JPEGLoadingContext& return Error::from_string_literal("Unsupported luma subsampling factors"); } } else { - // YCCK with just CC subsampled and K matching Y is fine. auto const& y_component = context.components[0]; - bool channel_matches_y_factor = component.sampling_factors == y_component.sampling_factors; - bool k_channel_matches_y = context.color_transform == ColorTransform::YCCK && i == 3 && channel_matches_y_factor; - - if (((component.sampling_factors != SamplingFactors { 1, 1 }) && !k_channel_matches_y) || (i == 3 && !channel_matches_y_factor)) { + if (y_component.sampling_factors.horizontal % component.sampling_factors.horizontal != 0 + || y_component.sampling_factors.vertical % component.sampling_factors.vertical != 0) { dbgln_if(JPEG_DEBUG, "Unsupported chroma subsampling factors: horizontal: {}, vertical: {}", component.sampling_factors.horizontal, component.sampling_factors.vertical); @@ -1589,8 +1586,7 @@ static void inverse_dct(JPEGLoadingContext const& context, Vector& m static void undo_subsampling(JPEGLoadingContext const& context, Vector& macroblocks) { // The first component has sampling factors of context.sampling_factors, while the others - // are either 1x1 or for the 4th component also context.sampling_factors. See - // read_start_of_frame() which currently enforces these restrictions. + // are divide the first component's sampling factors. This is enforced by read_start_of_frame(). // This function undoes the subsampling by duplicating the values of the smaller components. // See https://www.w3.org/Graphics/JPEG/itu-t81.pdf, A.2 Order of source image data encoding. //