Skip to content

Remove redundant JFIF and EXIF checks in JpegImageFormatDetector - #3283

Open
EnesKartal wants to merge 1 commit into
SixLabors:mainfrom
EnesKartal:fix/jpeg-detector-redundant-checks
Open

EnesKartal wants to merge 1 commit into
SixLabors:mainfrom
EnesKartal:fix/jpeg-detector-redundant-checks

Conversation

@EnesKartal

Copy link
Copy Markdown
Contributor

Fixes #2805

Summary

  • Removed the redundant IsJfif and IsExif checks from JpegImageFormatDetector, since every valid JPEG file (including JFIF and EXIF) starts with the JPEG SOI marker (0xFF 0xD8).
  • Updated HeaderSize from 11 to 2 to match the two SOI bytes inspected by IsJpeg, which also avoids false positives on non-JPEG headers that happen to contain JFIF\0 or Exif\0 at offset 6 without an SOI marker.
  • Added a unit test JpegImageFormatDetector_DetectsSoiWithoutJfifOrExif in ImageExtensionsTest.cs.

Test Plan

  • Verified locally with dotnet test tests/ImageSharp.Tests/ImageSharp.Tests.csproj --filter "FullyQualifiedName~Formats.Jpg.ImageExtensionsTest" --no-restore (9/9 passed on net10.0).

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gfoidl

gfoidl commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

While you're on it can you also update

private static bool IsJpeg(ReadOnlySpan<byte> header) =>
header[0] == 0xFF && // 255
header[1] == 0xD8; // 216
(and the others) to use something like

static bool IsJpeg(ReadOnlySpan<byte> header)
{
    ReadOnlySpan<byte> jpeg = [0xFF, 0xD8];
    return header.StartsWith(jpeg);
}
  • avoids the bound checks
  • the JIT unrolls the check, so more performant code
  • code is written so that the intention is clearer

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JpegImageFormatDetector IsJfif and IsExif checks are not needed as IsJpeg is always true for valid JPEG files.

3 participants