Skip to content

Commit 1b475ad

Browse files
authored
Merge pull request #2135 from SixLabors/bp/Issue2133
Deduce color space as YCbCr if component id's are 1, 2, 3
2 parents 0eb411c + 9d04ae4 commit 1b475ad

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ private JpegColorSpace DeduceJpegColorSpace(byte componentCount)
556556
return JpegColorSpace.RGB;
557557
}
558558

559+
// If these values are 1-3 for a 3-channel image, then the image is assumed to be YCbCr.
560+
if (this.Components[2].Id == 3 && this.Components[1].Id == 2 && this.Components[0].Id == 1)
561+
{
562+
return JpegColorSpace.YCbCr;
563+
}
564+
559565
// 3-channel non-subsampled images are assumed to be RGB.
560566
if (this.Components[2].VerticalSamplingFactor == 1 && this.Components[1].VerticalSamplingFactor == 1 && this.Components[0].VerticalSamplingFactor == 1 &&
561567
this.Components[2].HorizontalSamplingFactor == 1 && this.Components[1].HorizontalSamplingFactor == 1 && this.Components[0].HorizontalSamplingFactor == 1)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ public void Issue2057_DecodeWorks<TPixel>(TestImageProvider<TPixel> provider)
218218
}
219219
}
220220

221+
// https://github.com/SixLabors/ImageSharp/issues/2133
222+
[Theory]
223+
[WithFile(TestImages.Jpeg.Issues.Issue2133DeduceColorSpace, PixelTypes.Rgba32)]
224+
public void Issue2133_DeduceColorSpace<TPixel>(TestImageProvider<TPixel> provider)
225+
where TPixel : unmanaged, IPixel<TPixel>
226+
{
227+
using (Image<TPixel> image = provider.GetImage(JpegDecoder))
228+
{
229+
image.DebugSave(provider);
230+
image.CompareToOriginal(provider);
231+
}
232+
}
233+
221234
// DEBUG ONLY!
222235
// The PDF.js output should be saved by "tests\ImageSharp.Tests\Formats\Jpg\pdfjs\jpeg-converter.htm"
223236
// into "\tests\Images\ActualOutput\JpegDecoderTests\"

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ public static class Issues
272272
public const string Issue2057App1Parsing = "Jpg/issues/Issue2057-App1Parsing.jpg";
273273
public const string ExifNullArrayTag = "Jpg/issues/issue-2056-exif-null-array.jpg";
274274
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
275+
public const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg";
275276

276277
public static class Fuzz
277278
{
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)