From 479501b07c0afd8912a0e0f5b4740cfce9a89a4c Mon Sep 17 00:00:00 2001 From: DRC Date: Mon, 21 Jan 2019 13:57:55 -0600 Subject: [PATCH] TurboJPEG: Decompress 4:4:4 JPEGs with unusual SFs Normally, 4:4:4 JPEGs have horizontal x vertical luminance & chrominance sampling factors of 1x1. However, it is technically legal to create 4:4:4 JPEGs with sampling factors of 2x1, 1x2, 3x1, or 1x3, since the sums of the products of those sampling factors are still <= 10. The libjpeg API correctly decodes such images, so the TurboJPEG API should as well. Fixes #323 --- ChangeLog.md | 6 ++++++ turbojpeg.c | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index ebe26b6..461931b 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -22,6 +22,12 @@ djpeg. decompress a specially-crafted malformed JPEG image with a specified image width or height of 0 using the C version of TJBench. +5. The TurboJPEG API will now decompress 4:4:4 JPEG images with 2x1, 1x2, 3x1, +or 1x3 luminance and chrominance sampling factors. This is a non-standard way +of specifying 1x subsampling (normally 4:4:4 JPEGs have 1x1 luminance and +chrominance sampling factors), but the JPEG format and the libjpeg API both +allow it. + 2.0.1 ===== diff --git a/turbojpeg.c b/turbojpeg.c index 3f7cd64..b3caa0d 100644 --- a/turbojpeg.c +++ b/turbojpeg.c @@ -359,6 +359,23 @@ static int getSubsamp(j_decompress_ptr dinfo) retval = i; break; } } + /* Handle 4:4:4 images whose sampling factors are specified in + non-standard ways. */ + if (dinfo->comp_info[0].h_samp_factor * + dinfo->comp_info[0].v_samp_factor <= + D_MAX_BLOCKS_IN_MCU / pixelsize[i] && i == TJSAMP_444) { + int match = 0; + for (k = 1; k < dinfo->num_components; k++) { + if (dinfo->comp_info[i].h_samp_factor == + dinfo->comp_info[0].h_samp_factor && + dinfo->comp_info[i].v_samp_factor == + dinfo->comp_info[0].v_samp_factor) + match++; + if (match == dinfo->num_components - 1) { + retval = i; break; + } + } + } } } return retval; -- 2.40.0