Browse Source

If component id's are R, G, B in ASCII the color space should be RGB

pull/1732/head
Brian Popow 5 years ago
parent
commit
18edc46b0f
  1. 16
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

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

@ -345,10 +345,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <returns>The <see cref="JpegColorSpace"/></returns> /// <returns>The <see cref="JpegColorSpace"/></returns>
private JpegColorSpace DeduceJpegColorSpace(byte componentCount) private JpegColorSpace DeduceJpegColorSpace(byte componentCount, JpegComponent[] components)
{ {
if (componentCount == 1) if (componentCount == 1)
{ {
@ -362,6 +362,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
return JpegColorSpace.RGB; 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. // Some images are poorly encoded and contain incorrect colorspace transform metadata.
// We ignore that and always fall back to the default colorspace. // We ignore that and always fall back to the default colorspace.
return JpegColorSpace.YCbCr; return JpegColorSpace.YCbCr;
@ -836,9 +842,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
// 1 byte: Number of components // 1 byte: Number of components
byte componentCount = this.temp[5]; 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); this.Frame = new JpegFrame(frameMarker, precision, frameWidth, frameHeight, componentCount);
@ -888,6 +891,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
index += componentBytes; 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.Frame.Init(maxH, maxV);
this.scanDecoder.InjectFrameData(this.Frame, this); this.scanDecoder.InjectFrameData(this.Frame, this);

Loading…
Cancel
Save