From e8bbdf703ab55acb98f39320b22093f0f20558e1 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 4 Aug 2024 15:29:06 +0200 Subject: [PATCH] Always set YcbcrSubSampling to 1:1 for TiffCompression.Jpeg, fixes issue #2679 --- .../Formats/Tiff/TiffDecoderOptionsParser.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs index 7b0f439b1..864452fd2 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs @@ -620,7 +620,12 @@ internal static class TiffDecoderOptionsParser } options.CompressionType = TiffDecoderCompressionType.OldJpeg; - AdjustOptionsYCbCrJpegCompression(options); + if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr) + { + // Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data. + options.PhotometricInterpretation = TiffPhotometricInterpretation.Rgb; + options.ColorType = TiffColorType.Rgb; + } break; } @@ -628,7 +633,20 @@ internal static class TiffDecoderOptionsParser case TiffCompression.Jpeg: { options.CompressionType = TiffDecoderCompressionType.Jpeg; - AdjustOptionsYCbCrJpegCompression(options); + if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr && options.JpegTables is null) + { + // Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data. + options.PhotometricInterpretation = TiffPhotometricInterpretation.Rgb; + options.ColorType = TiffColorType.Rgb; + } + + // Some tiff encoder set this to values different from [1, 1]. The jpeg decoder already handles this, + // so we set this always to [1, 1], see: https://github.com/SixLabors/ImageSharp/issues/2679 + if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr) + { + options.YcbcrSubSampling[0] = 1; + options.YcbcrSubSampling[1] = 1; + } break; } @@ -647,24 +665,6 @@ internal static class TiffDecoderOptionsParser } } - private static void AdjustOptionsYCbCrJpegCompression(TiffDecoderCore options) - { - if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr) - { - // Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data. - options.PhotometricInterpretation = TiffPhotometricInterpretation.Rgb; - options.ColorType = TiffColorType.Rgb; - } - - if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr) - { - // Some tiff encoder set this to values different from [1, 1]. The jpeg decoder already handles this, - // so we set this always to [1, 1], see: https://github.com/SixLabors/ImageSharp/issues/2679 - options.YcbcrSubSampling[0] = 1; - options.YcbcrSubSampling[1] = 1; - } - } - private static bool IsBiColorCompression(TiffCompression? compression) { if (compression is TiffCompression.Ccitt1D or TiffCompression.CcittGroup3Fax or