Browse Source

Change deducing color space according to review

pull/2124/head
Brian Popow 4 years ago
parent
commit
e42fdd09f2
  1. 53
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  2. 3
      src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs

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

@ -519,21 +519,21 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
if (componentCount == 3) if (componentCount == 3)
{ {
if (!this.adobe.Equals(default) && this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown) // We prioritize adobe marker over jfif marker, if somebody really encoded this image with redundant adobe marker,
// then it's most likely an adobe jfif image.
if (!this.adobe.Equals(default))
{ {
return JpegColorSpace.RGB; if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYCbCr)
} {
return JpegColorSpace.YCbCr;
}
if (!this.adobe.Equals(default) && this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYCbCr) if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown)
{ {
return JpegColorSpace.YCbCr; return JpegColorSpace.RGB;
} }
// If the component Id's are R, G, B in ASCII the colorspace is RGB and not YCbCr. JpegThrowHelper.ThrowNotSupportedColorSpace();
// See: https://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html#color
if (this.Components[2].Id == 66 && this.Components[1].Id == 71 && this.Components[0].Id == 82)
{
return JpegColorSpace.RGB;
} }
if (this.hasJFif) if (this.hasJFif)
@ -542,10 +542,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
return JpegColorSpace.YCbCr; return JpegColorSpace.YCbCr;
} }
// If these values are 1-3 for a 3-channel image, then the image is assumed to be YCbCr. // Fallback to the id color deduction.
if (this.Components[2].Id == 3 && this.Components[1].Id == 2 && this.Components[0].Id == 1) // If the component Id's are R, G, B in ASCII the colorspace is RGB and not YCbCr.
// See: https://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html#color
if (this.Components[2].Id == 66 && this.Components[1].Id == 71 && this.Components[0].Id == 82)
{ {
return JpegColorSpace.YCbCr; return JpegColorSpace.RGB;
} }
// 3-channel non-subsampled images are assumed to be RGB. // 3-channel non-subsampled images are assumed to be RGB.
@ -562,9 +564,24 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
if (componentCount == 4) if (componentCount == 4)
{ {
return this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYcck // jfif images doesn't not support 4 component images, so we only check adobe.
? JpegColorSpace.Ycck if (!this.adobe.Equals(default))
: JpegColorSpace.Cmyk; {
if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformYcck)
{
return JpegColorSpace.Ycck;
}
if (this.adobe.ColorTransform == JpegConstants.Adobe.ColorTransformUnknown)
{
return JpegColorSpace.Cmyk;
}
JpegThrowHelper.ThrowNotSupportedColorSpace();
}
// Fallback to cmyk as neither of cmyk nor ycck have 'special' component ids.
return JpegColorSpace.Cmyk;
} }
JpegThrowHelper.ThrowNotSupportedComponentCount(componentCount); JpegThrowHelper.ThrowNotSupportedComponentCount(componentCount);

3
src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs

@ -51,5 +51,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
[MethodImpl(InliningOptions.ColdPath)] [MethodImpl(InliningOptions.ColdPath)]
public static void ThrowNotSupportedComponentCount(int componentCount) => throw new NotSupportedException($"Images with {componentCount} components are not supported."); public static void ThrowNotSupportedComponentCount(int componentCount) => throw new NotSupportedException($"Images with {componentCount} components are not supported.");
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowNotSupportedColorSpace() => throw new NotSupportedException("Image color space could not be deduced.");
} }
} }

Loading…
Cancel
Save