Skip to content

Commit 363769d

Browse files
committed
Avoid code duplication
1 parent 14ced6f commit 363769d

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -620,39 +620,15 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
620620
}
621621

622622
options.CompressionType = TiffDecoderCompressionType.OldJpeg;
623-
624-
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr)
625-
{
626-
// Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data.
627-
options.PhotometricInterpretation = TiffPhotometricInterpretation.Rgb;
628-
options.ColorType = TiffColorType.Rgb;
629-
}
630-
631-
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr)
632-
{
633-
options.YcbcrSubSampling[0] = 1;
634-
options.YcbcrSubSampling[1] = 1;
635-
}
623+
AdjustOptionsYCbCrJpegCompression(options);
636624

637625
break;
638626
}
639627

640628
case TiffCompression.Jpeg:
641629
{
642630
options.CompressionType = TiffDecoderCompressionType.Jpeg;
643-
644-
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr && options.JpegTables is null)
645-
{
646-
// Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data.
647-
options.PhotometricInterpretation = TiffPhotometricInterpretation.Rgb;
648-
options.ColorType = TiffColorType.Rgb;
649-
}
650-
651-
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr)
652-
{
653-
options.YcbcrSubSampling[0] = 1;
654-
options.YcbcrSubSampling[1] = 1;
655-
}
631+
AdjustOptionsYCbCrJpegCompression(options);
656632

657633
break;
658634
}
@@ -671,6 +647,24 @@ private static void ParseCompression(this TiffDecoderCore options, TiffCompressi
671647
}
672648
}
673649

650+
private static void AdjustOptionsYCbCrJpegCompression(TiffDecoderCore options)
651+
{
652+
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr)
653+
{
654+
// Note: Setting PhotometricInterpretation and color type to RGB here, since the jpeg decoder will handle the conversion of the pixel data.
655+
options.PhotometricInterpretation = TiffPhotometricInterpretation.Rgb;
656+
options.ColorType = TiffColorType.Rgb;
657+
}
658+
659+
if (options.PhotometricInterpretation is TiffPhotometricInterpretation.YCbCr)
660+
{
661+
// Some tiff encoder set this to values different from [1, 1]. The jpeg decoder already handles this,
662+
// so we set this always to [1, 1], see: https://github.com/SixLabors/ImageSharp/issues/2679
663+
options.YcbcrSubSampling[0] = 1;
664+
options.YcbcrSubSampling[1] = 1;
665+
}
666+
}
667+
674668
private static bool IsBiColorCompression(TiffCompression? compression)
675669
{
676670
if (compression is TiffCompression.Ccitt1D or TiffCompression.CcittGroup3Fax or

0 commit comments

Comments
 (0)