diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 896e5f0aaf..269b2fe76b 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -345,10 +345,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } /// - /// Returns the correct colorspace based on the image component count + /// Returns the correct colorspace based on the image component count and the jpeg frame components. /// /// The - private JpegColorSpace DeduceJpegColorSpace(byte componentCount) + private JpegColorSpace DeduceJpegColorSpace(byte componentCount, JpegComponent[] components) { if (componentCount == 1) { @@ -362,6 +362,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg return JpegColorSpace.RGB; } + // If the component Id's are R, G, B in ASCII the colorspace is RGB and not YCbCr. + if (components[0].Id == 82 && components[1].Id == 71 && components[2].Id == 66) + { + 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; @@ -836,9 +842,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg // 1 byte: Number of components byte componentCount = this.temp[5]; - this.ColorSpace = this.DeduceJpegColorSpace(componentCount); - - this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr; this.Frame = new JpegFrame(frameMarker, precision, frameWidth, frameHeight, componentCount); @@ -888,6 +891,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg index += componentBytes; } + this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components); + this.Metadata.GetJpegMetadata().ColorType = this.ColorSpace == JpegColorSpace.Grayscale ? JpegColorType.Luminance : JpegColorType.YCbCr; + this.Frame.Init(maxH, maxV); this.scanDecoder.InjectFrameData(this.Frame, this);