Browse Source

Fix deduce jpeg color type

pull/1734/head
Brian Popow 5 years ago
parent
commit
2e89d3c932
  1. 2
      src/ImageSharp/Formats/Jpeg/JpegColorType.cs
  2. 52
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

2
src/ImageSharp/Formats/Jpeg/JpegColorType.cs

@ -30,6 +30,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// <summary>
/// The pixel data will be preserved as RGB without any sub sampling.
/// </summary>
Rgb,
Rgb = 3,
}
}

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

@ -409,8 +409,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// <summary>
/// Returns the correct colorspace based on the image component count and the jpeg frame component id's.
/// </summary>
/// <param name="componentCount">The number of components.</param>
/// <returns>The <see cref="JpegColorSpace"/></returns>
private JpegColorSpace DeduceJpegColorSpace(byte componentCount, JpegComponent[] components)
private JpegColorSpace DeduceJpegColorSpace(byte componentCount)
{
if (componentCount == 1)
{
@ -425,7 +426,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
}
// If the component Id's are R, G, B in ASCII the colorspace is RGB and not YCbCr.
if (components[2].Id == 66 && components[1].Id == 71 && components[0].Id == 82)
if (this.Components[2].Id == 66 && this.Components[1].Id == 71 && this.Components[0].Id == 82)
{
return JpegColorSpace.RGB;
}
@ -446,6 +447,35 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
return default;
}
/// <summary>
/// Returns the jpeg color type based on the colorspace and subsampling used.
/// </summary>
/// <returns>Jpeg color type.</returns>
private JpegColorType DeduceJpegColorType()
{
switch (this.ColorSpace)
{
case JpegColorSpace.Grayscale:
return JpegColorType.Luminance;
case JpegColorSpace.RGB:
return JpegColorType.Rgb;
case JpegColorSpace.YCbCr:
if (this.Frame.Components[0].HorizontalSamplingFactor == 1 && this.Frame.Components[0].VerticalSamplingFactor == 1 &&
this.Frame.Components[1].HorizontalSamplingFactor == 1 && this.Frame.Components[1].VerticalSamplingFactor == 1 &&
this.Frame.Components[2].HorizontalSamplingFactor == 1 && this.Frame.Components[2].VerticalSamplingFactor == 1)
{
return JpegColorType.YCbCrRatio444;
}
else
{
return JpegColorType.YCbCrRatio420;
}
default:
return JpegColorType.YCbCrRatio420;
}
}
/// <summary>
/// Initializes the EXIF profile.
/// </summary>
@ -859,7 +889,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
}
/// <summary>
/// Processes the Start of Frame marker. Specified in section B.2.2.
/// Processes the Start of Frame marker. Specified in section B.2.2.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <param name="remaining">The remaining bytes in the segment block.</param>
@ -951,20 +981,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
index += componentBytes;
}
this.ColorSpace = this.DeduceJpegColorSpace(componentCount, this.Frame.Components);
switch (this.ColorSpace)
{
case JpegColorSpace.Grayscale:
this.Metadata.GetJpegMetadata().ColorType = JpegColorType.Luminance;
break;
case JpegColorSpace.RGB:
this.Metadata.GetJpegMetadata().ColorType = JpegColorType.Rgb;
break;
default:
this.Metadata.GetJpegMetadata().ColorType = JpegColorType.YCbCrRatio420;
break;
}
this.ColorSpace = this.DeduceJpegColorSpace(componentCount);
this.Metadata.GetJpegMetadata().ColorType = this.DeduceJpegColorType();
if (!metadataOnly)
{

Loading…
Cancel
Save