From c5c4f64c89e6a274dbbc00aa24a49d93c21ba3d3 Mon Sep 17 00:00:00 2001 From: Ildar Khayrutdinov Date: Sat, 11 Sep 2021 22:28:54 +0300 Subject: [PATCH] format detector for BigTiff --- .../Formats/Tiff/TiffConfigurationModule.cs | 4 +- .../Formats/Tiff/TiffImageFormatDetector.cs | 61 +++++++++++++++++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/TiffConfigurationModule.cs b/src/ImageSharp/Formats/Tiff/TiffConfigurationModule.cs index e96dba2077..5dfda95e84 100644 --- a/src/ImageSharp/Formats/Tiff/TiffConfigurationModule.cs +++ b/src/ImageSharp/Formats/Tiff/TiffConfigurationModule.cs @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff { configuration.ImageFormatsManager.SetEncoder(TiffFormat.Instance, new TiffEncoder()); configuration.ImageFormatsManager.SetDecoder(TiffFormat.Instance, new TiffDecoder()); - configuration.ImageFormatsManager.AddImageFormatDetector(new TiffImageFormatDetector()); + configuration.ImageFormatsManager.AddImageFormatDetector(new TiffImageFormatDetector(false)); } } -} \ No newline at end of file +} diff --git a/src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs b/src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs index f7e6f7a997..297da2dd8a 100644 --- a/src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs +++ b/src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs @@ -10,8 +10,16 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// public sealed class TiffImageFormatDetector : IImageFormatDetector { + private readonly bool isBigTiff; + + /// + /// Initializes a new instance of the class. + /// + /// if set to true [is big tiff]. + public TiffImageFormatDetector(bool isBigTiff) => this.isBigTiff = isBigTiff; + /// - public int HeaderSize => 4; + public int HeaderSize => this.isBigTiff ? 8 : 4; /// public IImageFormat DetectFormat(ReadOnlySpan header) @@ -26,9 +34,52 @@ namespace SixLabors.ImageSharp.Formats.Tiff private bool IsSupportedFileFormat(ReadOnlySpan header) { - return header.Length >= this.HeaderSize && - ((header[0] == 0x49 && header[1] == 0x49 && header[2] == 0x2A && header[3] == 0x00) || // Little-endian - (header[0] == 0x4D && header[1] == 0x4D && header[2] == 0x00 && header[3] == 0x2A)); // Big-endian + if (header.Length >= this.HeaderSize) + { + if (header[0] == 0x49 && header[1] == 0x49) + { + // Little-endian + if (!this.isBigTiff) + { + if (header[2] == 0x2A && header[3] == 0x00) + { + return true; + } + } + else + { + if (header[2] == 0x2B && header[3] == 0x00 && header[4] == 8 && header[5] == 0 && header[6] == 0 && header[7] == 0) + { + return true; + } + } + } + else if (header[0] == 0x4D && header[1] == 0x4D) + { + // Big-endian + if (header[2] == 0x00 && (header[3] == 0x2A || header[3] == 0x2B)) + { + return true; + } + + if (!this.isBigTiff) + { + if (header[2] == 0 && header[3] == 0x2A) + { + return true; + } + } + else + { + if (header[2] == 0 && header[3] == 0x2A && header[4] == 0 && header[5] == 8 && header[6] == 0 && header[7] == 0) + { + return true; + } + } + } + } + + return false; } } -} \ No newline at end of file +}