Skip to content

Commit

Permalink
Handle incorrect colorspace metadata. Fix #882 (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants authored Apr 15, 2019
1 parent e9b9e27 commit 5a85ea7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
13 changes: 6 additions & 7 deletions src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,14 @@ private JpegColorSpace DeduceJpegColorSpace()

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)
Expand All @@ -419,7 +418,7 @@ private JpegColorSpace DeduceJpegColorSpace()
: 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ public partial class JpegDecoderTests
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,
};
Expand Down
1 change: 1 addition & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public static class Issues
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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Images/External
Submodule External updated 43 files
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_BikeGrayscale_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_BikeGrayscale_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_BikeGrayscale_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bike_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bike_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bike_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_BikeGrayscale_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_BikeGrayscale_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_BikeGrayscale_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_Bike_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_Bike_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_Bike_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_CalliphoraPartial_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_CalliphoraPartial_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_CalliphoraPartial_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_cross_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_cross_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Bounded_cross_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_CalliphoraPartial_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_CalliphoraPartial_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_CalliphoraPartial_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Solid50x50_(255,0,0,255)_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Solid50x50_(255,0,0,255)_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_Solid50x50_(255,0,0,255)_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern200x100_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern200x100_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern200x100_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern23x31_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern23x31_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern23x31_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern30x20_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern30x20_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_TestPattern30x20_R8_C1_G1.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_WorksWithAllPixelTypes_Bgr24.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_WorksWithAllPixelTypes_Bgra32.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_WorksWithAllPixelTypes_Gray8.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_cross_R16_C1_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_cross_R16_C2_G3.png
+ ReferenceOutput/BokehBlurTest/BokehBlurFilterProcessor_cross_R8_C1_G1.png
+ ReferenceOutput/JpegDecoderTests/DecodeBaselineJpeg_issue855-incorrect-colorspace.png
+ ReferenceOutput/ResizeTests/Resize_BasicSmall_BasicTestPattern15x12_(2÷3,1÷2).png
+ ReferenceOutput/ResizeTests/Resize_BasicSmall_BasicTestPattern2x256_(1÷1,1÷8).png
+ ReferenceOutput/ResizeTests/Resize_BasicSmall_BasicTestPattern2x32_(1÷1,1÷2).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5a85ea7

Please sign in to comment.