Skip to content

Commit 18edc46

Browse files
committed
If component id's are R, G, B in ASCII the color space should be RGB
1 parent 25dadda commit 18edc46

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,10 @@ public void Dispose()
345345
}
346346

347347
/// <summary>
348-
/// Returns the correct colorspace based on the image component count
348+
/// Returns the correct colorspace based on the image component count and the jpeg frame components.
349349
/// </summary>
350350
/// <returns>The <see cref="JpegColorSpace"/></returns>
351-
private JpegColorSpace DeduceJpegColorSpace(byte componentCount)
351+
private JpegColorSpace DeduceJpegColorSpace(byte componentCount, JpegComponent[] components)
352352
{
353353
if (componentCount == 1)
354354
{
@@ -362,6 +362,12 @@ private JpegColorSpace DeduceJpegColorSpace(byte componentCount)
362362
return JpegColorSpace.RGB;
363363
}
364364

365+
// If the component Id's are R, G, B in ASCII the colorspace is RGB and not YCbCr.
366+
if (components[0].Id == 82 && components[1].Id == 71 && components[2].Id == 66)
367+
{
368+
return JpegColorSpace.RGB;
369+
}
370+
365371
// Some images are poorly encoded and contain incorrect colorspace transform metadata.
366372
// We ignore that and always fall back to the default colorspace.
367373
return JpegColorSpace.YCbCr;
@@ -836,9 +842,6 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining,
836842

837843
// 1 byte: Number of components
838844
byte componentCount = this.temp[5];
839-
this.ColorSpace = this.DeduceJpegColorSpace(componentCount);
840-
841-
this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
842845

843846
this.Frame = new JpegFrame(frameMarker, precision, frameWidth, frameHeight, componentCount);
844847

@@ -888,6 +891,9 @@ private void ProcessStartOfFrameMarker(BufferedReadStream stream, int remaining,
888891
index += componentBytes;
889892
}
890893

894+
this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components);
895+
this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr;
896+
891897
this.Frame.Init(maxH, maxV);
892898

893899
this.scanDecoder.InjectFrameData(this.Frame, this);

0 commit comments

Comments
 (0)