|
|
|
@ -10,8 +10,16 @@ namespace SixLabors.ImageSharp.Formats.Tiff |
|
|
|
/// </summary>
|
|
|
|
public sealed class TiffImageFormatDetector : IImageFormatDetector |
|
|
|
{ |
|
|
|
private readonly bool isBigTiff; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="TiffImageFormatDetector"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="isBigTiff">if set to <c>true</c> [is big tiff].</param>
|
|
|
|
public TiffImageFormatDetector(bool isBigTiff) => this.isBigTiff = isBigTiff; |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public int HeaderSize => 4; |
|
|
|
public int HeaderSize => this.isBigTiff ? 8 : 4; |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) |
|
|
|
@ -26,9 +34,52 @@ namespace SixLabors.ImageSharp.Formats.Tiff |
|
|
|
|
|
|
|
private bool IsSupportedFileFormat(ReadOnlySpan<byte> 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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|