From 7dcfb82e16ef40bf636094a5fb7b37b8f395fa4d Mon Sep 17 00:00:00 2001 From: Michiel Visser Date: Sat, 19 Feb 2022 21:05:05 +0100 Subject: [PATCH] LibGfx: Fix JPG decoding bug on rare grayscale images Some grayscale JPG images might have the horizontal and vertical sample factor set to 2. However, the macroblocks in these images are not interleaved as they would be in color images. --- Userland/Libraries/LibGfx/JPGLoader.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Userland/Libraries/LibGfx/JPGLoader.cpp b/Userland/Libraries/LibGfx/JPGLoader.cpp index 650684bf88b..6c31590c475 100644 --- a/Userland/Libraries/LibGfx/JPGLoader.cpp +++ b/Userland/Libraries/LibGfx/JPGLoader.cpp @@ -742,6 +742,12 @@ static bool read_start_of_frame(InputMemoryStream& stream, JPGLoadingContext& co component.vsample_factor = subsample_factors & 0x0F; if (i == 0) { + // If there is only a single component, i.e. grayscale, the macroblocks will not be interleaved, even if + // the horizontal or vertical sample factor is larger than 1. + if (context.component_count == 1) { + component.hsample_factor = 1; + component.vsample_factor = 1; + } // By convention, downsampling is applied only on chroma components. So we should // hope to see the maximum sampling factor in the luma component. if (!validate_luma_and_modify_context(component, context)) {