Browse Source

format detector for BigTiff

pull/1760/head
Ildar Khayrutdinov 5 years ago
parent
commit
c5c4f64c89
  1. 4
      src/ImageSharp/Formats/Tiff/TiffConfigurationModule.cs
  2. 61
      src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs

4
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));
}
}
}
}

61
src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs

@ -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;
}
}
}
}

Loading…
Cancel
Save