Skip to content

Commit 350e439

Browse files
Merge pull request #2789 from SixLabors/bp/Issue2679
Tiff decoder: Fix issue 2679
2 parents 8da27c9 + 1c6e1d9 commit 350e439

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,6 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
572572
}
573573

574574
options.CompressionType = TiffDecoderCompressionType.OldJpeg;
575-
576575
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr)
577576
{
578577
// Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data.
@@ -585,6 +584,14 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
585584
case TiffCompression.Jpeg:
586585
options.CompressionType = TiffDecoderCompressionType.Jpeg;
587586

587+
// Some tiff encoder set this to values different from [1, 1]. The jpeg decoder already handles this,
588+
// so we set this always to [1, 1], see: https://github.com/SixLabors/ImageSharp/issues/2679
589+
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr && options.YcbcrSubSampling != null)
590+
{
591+
options.YcbcrSubSampling[0] = 1;
592+
options.YcbcrSubSampling[1] = 1;
593+
}
594+
588595
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr && options.JpegTables is null)
589596
{
590597
// Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,23 @@ public void TiffDecoder_CanDecode_TiledWithNonEqualWidthAndHeight<TPixel>(TestIm
671671
public void TiffDecoder_CanDecode_BiColorWithMissingBitsPerSample<TPixel>(TestImageProvider<TPixel> provider)
672672
where TPixel : unmanaged, IPixel<TPixel> => TestTiffDecoder(provider);
673673

674+
// https://github.com/SixLabors/ImageSharp/issues/2679
675+
[Theory]
676+
[WithFile(Issues2679, PixelTypes.Rgba32)]
677+
public void TiffDecoder_CanDecode_JpegCompressedWithIssue2679<TPixel>(TestImageProvider<TPixel> provider)
678+
where TPixel : unmanaged, IPixel<TPixel>
679+
{
680+
using Image<TPixel> image = provider.GetImage(TiffDecoder.Instance);
681+
682+
// The image is handcrafted to simulate issue 2679. ImageMagick will throw an expection here and wont decode,
683+
// so we compare to rererence output instead.
684+
image.DebugSave(provider);
685+
image.CompareToReferenceOutput(
686+
ImageComparer.Exact,
687+
provider,
688+
appendPixelTypeToFileName: false);
689+
}
690+
674691
[Theory]
675692
[WithFile(JpegCompressedGray0000539558, PixelTypes.Rgba32)]
676693
public void TiffDecoder_ThrowsException_WithCircular_IFD_Offsets<TPixel>(TestImageProvider<TPixel> provider)

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ public static class Tiff
10471047
public const string Issues2255 = "Tiff/Issues/Issue2255.png";
10481048
public const string Issues2435 = "Tiff/Issues/Issue2435.tiff";
10491049
public const string Issues2587 = "Tiff/Issues/Issue2587.tiff";
1050+
public const string Issues2679 = "Tiff/Issues/Issue2679.tiff";
10501051
public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";
10511052
public const string Tiled0000023664 = "Tiff/Issues/tiled-0000023664.tiff";
10521053

Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:feb938396b9d5b4c258244197ba382937a52c93f72cc91081c7e6810e4a3b94c
3+
size 6136

0 commit comments

Comments
 (0)