Skip to content

Commit 5a85ea7

Browse files
Handle incorrect colorspace metadata. Fix #882 (#885)
1 parent e9b9e27 commit 5a85ea7

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,14 @@ private JpegColorSpace DeduceJpegColorSpace()
401401

402402
if (this.ComponentCount == 3)
403403
{
404-
if (this.adobe.Equals(default) || this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYCbCr)
405-
{
406-
return JpegColorSpace.YCbCr;
407-
}
408-
409-
if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown)
404+
if (!this.adobe.Equals(default) && this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown)
410405
{
411406
return JpegColorSpace.RGB;
412407
}
408+
409+
// Some images are poorly encoded and contain incorrect colorspace transform metadata.
410+
// We ignore that and always fall back to the default colorspace.
411+
return JpegColorSpace.YCbCr;
413412
}
414413

415414
if (this.ComponentCount == 4)
@@ -419,7 +418,7 @@ private JpegColorSpace DeduceJpegColorSpace()
419418
: JpegColorSpace.Cmyk;
420419
}
421420

422-
JpegThrowHelper.ThrowImageFormatException($"Unsupported color mode. Max components 4; found {this.ComponentCount}");
421+
JpegThrowHelper.ThrowImageFormatException($"Unsupported color mode. Supported component counts 1, 3, and 4; found {this.ComponentCount}");
423422
return default;
424423
}
425424

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ public partial class JpegDecoderTests
3131
TestImages.Jpeg.Issues.ExifGetString750Load,
3232
TestImages.Jpeg.Issues.ExifGetString750Transform,
3333

34-
// LibJpeg can open this despite the invalid desity units.
34+
// LibJpeg can open this despite the invalid density units.
3535
TestImages.Jpeg.Issues.Fuzz.ArgumentOutOfRangeException825B,
3636

37+
// LibJpeg can open this despite incorrect colorspace metadata.
38+
TestImages.Jpeg.Issues.IncorrectColorspace855,
39+
3740
// High depth images
3841
TestImages.Jpeg.Baseline.Testorig12bit,
3942
};

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public static class Issues
175175
public const string ExifGetString750Transform = "Jpg/issues/issue750-exif-tranform.jpg";
176176
public const string ExifGetString750Load = "Jpg/issues/issue750-exif-load.jpg";
177177
public const string IncorrectQuality845 = "Jpg/issues/Issue845-Incorrect-Quality99.jpg";
178+
public const string IncorrectColorspace855 = "Jpg/issues/issue855-incorrect-colorspace.jpg";
178179

179180
public static class Fuzz
180181
{

tests/Images/External

Submodule External updated 43 files
139 KB
Loading

0 commit comments

Comments
 (0)