Browse Source

Handle incorrect colorspace metadata. Fix #882 (#885)

pull/891/head
James Jackson-South 7 years ago
committed by GitHub
parent
commit
5a85ea72b5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  2. 5
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs
  3. 1
      tests/ImageSharp.Tests/TestImages.cs
  4. 2
      tests/Images/External
  5. BIN
      tests/Images/Input/Jpg/issues/issue855-incorrect-colorspace.jpg

13
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -401,15 +401,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
if (this.ComponentCount == 3)
{
if (this.adobe.Equals(default) || this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYCbCr)
{
return JpegColorSpace.YCbCr;
}
if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown)
if (!this.adobe.Equals(default) && this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown)
{
return JpegColorSpace.RGB;
}
// Some images are poorly encoded and contain incorrect colorspace transform metadata.
// We ignore that and always fall back to the default colorspace.
return JpegColorSpace.YCbCr;
}
if (this.ComponentCount == 4)
@ -419,7 +418,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
: JpegColorSpace.Cmyk;
}
JpegThrowHelper.ThrowImageFormatException($"Unsupported color mode. Max components 4; found {this.ComponentCount}");
JpegThrowHelper.ThrowImageFormatException($"Unsupported color mode. Supported component counts 1, 3, and 4; found {this.ComponentCount}");
return default;
}

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

@ -31,9 +31,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
TestImages.Jpeg.Issues.ExifGetString750Load,
TestImages.Jpeg.Issues.ExifGetString750Transform,
// LibJpeg can open this despite the invalid desity units.
// LibJpeg can open this despite the invalid density units.
TestImages.Jpeg.Issues.Fuzz.ArgumentOutOfRangeException825B,
// LibJpeg can open this despite incorrect colorspace metadata.
TestImages.Jpeg.Issues.IncorrectColorspace855,
// High depth images
TestImages.Jpeg.Baseline.Testorig12bit,
};

1
tests/ImageSharp.Tests/TestImages.cs

@ -175,6 +175,7 @@ namespace SixLabors.ImageSharp.Tests
public const string ExifGetString750Transform = "Jpg/issues/issue750-exif-tranform.jpg";
public const string ExifGetString750Load = "Jpg/issues/issue750-exif-load.jpg";
public const string IncorrectQuality845 = "Jpg/issues/Issue845-Incorrect-Quality99.jpg";
public const string IncorrectColorspace855 = "Jpg/issues/issue855-incorrect-colorspace.jpg";
public static class Fuzz
{

2
tests/Images/External

@ -1 +1 @@
Subproject commit 94b5a8e11b33ba62c15d1d03f1b8b721468764f1
Subproject commit 802725dec2a6b1ca02f9e2f9a4c3f625583d0696

BIN
tests/Images/Input/Jpg/issues/issue855-incorrect-colorspace.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

Loading…
Cancel
Save