Browse Source

Throw NotSupported exception when luma and chroma subsampling is not equal

pull/1731/head
Brian Popow 5 years ago
parent
commit
1ec3394589
  1. 32
      src/ImageSharp/Formats/Tiff/PhotometricInterpretation/YCbCrTiffColor{TPixel}.cs
  2. 12
      src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
  3. 1
      tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
  4. 4
      tests/ImageSharp.Tests/TestImages.cs
  5. 0
      tests/Images/Input/Tiff/flower-ycbcr-contig-08.tiff

32
src/ImageSharp/Formats/Tiff/PhotometricInterpretation/YCbCrTiffColor{TPixel}.cs

@ -12,37 +12,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
{
private readonly YCbCrConverter converter;
private static readonly Rational[] DefaultLuma =
{
new Rational(299, 1000),
new Rational(587, 1000),
new Rational(114, 1000)
};
private static readonly Rational[] DefaultReferenceBlackWhite =
{
new Rational(0, 1), new Rational(255, 1),
new Rational(128, 1), new Rational(255, 1),
new Rational(128, 1), new Rational(255, 1)
};
public YCbCrTiffColor(Rational[] referenceBlackAndWhite, Rational[] coefficients)
{
referenceBlackAndWhite ??= DefaultReferenceBlackWhite;
coefficients ??= DefaultLuma;
if (referenceBlackAndWhite.Length != 6)
{
TiffThrowHelper.ThrowImageFormatException("reference black and white array should have 6 entry's");
}
if (coefficients.Length != 3)
{
TiffThrowHelper.ThrowImageFormatException("luma coefficients array should have 6 entry's");
}
this.converter = new YCbCrConverter(referenceBlackAndWhite, coefficients);
}
public YCbCrTiffColor(Rational[] referenceBlackAndWhite, Rational[] coefficients) => this.converter = new YCbCrConverter(referenceBlackAndWhite, coefficients);
/// <inheritdoc/>
public override void Decode(ReadOnlySpan<byte> data, Buffer2D<TPixel> pixels, int left, int top, int width, int height)

12
src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs

@ -57,6 +57,12 @@ namespace SixLabors.ImageSharp.Formats.Tiff
}
}
ushort[] ycbcrSubSampling = exifProfile.GetValue(ExifTag.YCbCrSubsampling)?.Value;
if (ycbcrSubSampling != null && ycbcrSubSampling[0] != ycbcrSubSampling[1])
{
TiffThrowHelper.ThrowNotSupported("ImageSharp only supports YCbCr images with equal luma and chroma samples.");
}
if (exifProfile.GetValue(ExifTag.StripRowCounts)?.Value != null)
{
TiffThrowHelper.ThrowNotSupported("Variable-sized strips are not supported.");
@ -274,6 +280,12 @@ namespace SixLabors.ImageSharp.Formats.Tiff
TiffThrowHelper.ThrowNotSupported("The number of samples in the TIFF BitsPerSample entry is not supported.");
}
ushort bitsPerChannel = options.BitsPerSample.Channel0;
if (bitsPerChannel != 8)
{
TiffThrowHelper.ThrowNotSupported("Only 8 bits per channel is supported for YCbCr images.");
}
options.ColorType = options.PlanarConfiguration == TiffPlanarConfiguration.Chunky ? TiffColorType.YCbCr : TiffColorType.YCbCrPlanar;
break;

1
tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs

@ -160,6 +160,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
[Theory]
[WithFile(FlowerYCbCr888Contiguous, PixelTypes.Rgba32)]
[WithFile(FlowerYCbCr888Planar, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_YCbCr_24Bit<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);

4
tests/ImageSharp.Tests/TestImages.cs

@ -575,8 +575,8 @@ namespace SixLabors.ImageSharp.Tests
public const string FlowerRgb888Contiguous = "Tiff/flower-rgb-contig-08.tiff";
public const string FlowerRgb888Planar6Strips = "Tiff/flower-rgb-planar-08-6strips.tiff";
public const string FlowerRgb888Planar15Strips = "Tiff/flower-rgb-planar-08-15strips.tiff";
public const string FlowerYCbCr888Contiguous = "Tiff/flower_ycbcr_contig-08.tiff";
public const string FlowerYCbCr888Planar = "Tiff/flower_ycbcr_planar-08.tiff";
public const string FlowerYCbCr888Contiguous = "Tiff/flower-ycbcr-contig-08.tiff";
public const string FlowerYCbCr888Planar = "Tiff/flower-ycbcr-planar-08.tiff";
public const string FlowerRgb444Contiguous = "Tiff/flower-rgb-contig-04.tiff";
public const string FlowerRgb444Planar = "Tiff/flower-rgb-planar-04.tiff";
public const string FlowerRgb222Contiguous = "Tiff/flower-rgb-contig-02.tiff";

0
tests/Images/Input/Tiff/flower_ycbcr_contig-08.tiff → tests/Images/Input/Tiff/flower-ycbcr-contig-08.tiff

Loading…
Cancel
Save