mirror of https://github.com/SixLabors/ImageSharp
3 changed files with 159 additions and 0 deletions
@ -0,0 +1,30 @@ |
|||||
|
// <copyright file="TiffConstants.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Formats |
||||
|
{ |
||||
|
using System.Text; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines constants defined in the TIFF specification.
|
||||
|
/// </summary>
|
||||
|
internal static class GifConstants |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Byte order markers for indicating little endian encoding.
|
||||
|
/// </summary>
|
||||
|
public const ushort ByteOrderLittleEndian = 0x4949; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Byte order markers for indicating big endian encoding.
|
||||
|
/// </summary>
|
||||
|
public const ushort ByteOrderBigEndian = 0x4D4D; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Magic number used within the image file header to identify a TIFF format file.
|
||||
|
/// </summary>
|
||||
|
public const ushort HeaderMagicNumber = 42; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
// <copyright file="TiffFormat.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Formats |
||||
|
{ |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates the means to encode and decode Tiff images.
|
||||
|
/// </summary>
|
||||
|
public class TiffFormat //: IImageFormat
|
||||
|
{ |
||||
|
/// <inheritdoc/>
|
||||
|
public string MimeType => "image/tiff"; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public string Extension => "tif"; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public IEnumerable<string> SupportedExtensions => new string[] { "tif", "tiff" }; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
//public IImageDecoder Decoder => new TiffDecoder();
|
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
//public IImageEncoder Encoder => throw new System.NotImplementedException();
|
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public int HeaderSize => 4; |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public bool IsSupportedFileFormat(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
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,88 @@ |
|||||
|
// <copyright file="TiffFormatTests.cs" company="James Jackson-South">
|
||||
|
// Copyright (c) James Jackson-South and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
// </copyright>
|
||||
|
|
||||
|
namespace ImageSharp.Tests |
||||
|
{ |
||||
|
using System.Linq; |
||||
|
using Xunit; |
||||
|
|
||||
|
using ImageSharp.Formats; |
||||
|
|
||||
|
public class TiffFormatTests |
||||
|
{ |
||||
|
public static object[][] IsLittleEndianValues = new[] { new object[] { false }, |
||||
|
new object[] { true } }; |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void IsSupportedFileFormat_ReturnsTrue_ForValidFile(bool isLittleEndian) |
||||
|
{ |
||||
|
byte[] bytes = new TiffGenHeader() |
||||
|
{ |
||||
|
FirstIfd = new TiffGenIfd() |
||||
|
} |
||||
|
.ToBytes(isLittleEndian); |
||||
|
|
||||
|
TiffFormat tiffFormat = new TiffFormat(); |
||||
|
byte[] headerBytes = bytes.Take(tiffFormat.HeaderSize).ToArray(); |
||||
|
bool isSupported = tiffFormat.IsSupportedFileFormat(headerBytes); |
||||
|
|
||||
|
Assert.True(isSupported); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void IsSupportedFileFormat_ReturnsFalse_WithInvalidByteOrderMarkers(bool isLittleEndian) |
||||
|
{ |
||||
|
byte[] bytes = new TiffGenHeader() |
||||
|
{ |
||||
|
FirstIfd = new TiffGenIfd(), |
||||
|
ByteOrderMarker = 0x1234 |
||||
|
} |
||||
|
.ToBytes(isLittleEndian); |
||||
|
|
||||
|
TiffFormat tiffFormat = new TiffFormat(); |
||||
|
byte[] headerBytes = bytes.Take(tiffFormat.HeaderSize).ToArray(); |
||||
|
bool isSupported = tiffFormat.IsSupportedFileFormat(headerBytes); |
||||
|
|
||||
|
Assert.False(isSupported); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void IsSupportedFileFormat_ReturnsFalse_WithIncorrectMagicNumber(bool isLittleEndian) |
||||
|
{ |
||||
|
byte[] bytes = new TiffGenHeader() |
||||
|
{ |
||||
|
FirstIfd = new TiffGenIfd(), |
||||
|
MagicNumber = 32 |
||||
|
} |
||||
|
.ToBytes(isLittleEndian); |
||||
|
|
||||
|
TiffFormat tiffFormat = new TiffFormat(); |
||||
|
byte[] headerBytes = bytes.Take(tiffFormat.HeaderSize).ToArray(); |
||||
|
bool isSupported = tiffFormat.IsSupportedFileFormat(headerBytes); |
||||
|
|
||||
|
Assert.False(isSupported); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void IsSupportedFileFormat_ReturnsFalse_WithShortHeader(bool isLittleEndian) |
||||
|
{ |
||||
|
byte[] bytes = new TiffGenHeader() |
||||
|
{ |
||||
|
FirstIfd = new TiffGenIfd() |
||||
|
} |
||||
|
.ToBytes(isLittleEndian); |
||||
|
|
||||
|
TiffFormat tiffFormat = new TiffFormat(); |
||||
|
byte[] headerBytes = bytes.Take(tiffFormat.HeaderSize - 1).ToArray(); |
||||
|
bool isSupported = tiffFormat.IsSupportedFileFormat(headerBytes); |
||||
|
|
||||
|
Assert.False(isSupported); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue