mirror of https://github.com/SixLabors/ImageSharp
18 changed files with 227 additions and 200 deletions
@ -0,0 +1,18 @@ |
|||
// <copyright file="ITiffDecoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="TiffDecoder"/>.
|
|||
/// </summary>
|
|||
public interface ITiffDecoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets a value indicating whether the metadata should be ignored when the image is being decoded.
|
|||
/// </summary>
|
|||
bool IgnoreMetadata { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
// <copyright file="TiffConfigurationModule.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Registers the image encoders, decoders and mime type detectors for the TIFF format.
|
|||
/// </summary>
|
|||
public sealed class TiffConfigurationModule : IConfigurationModule |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public void Configure(Configuration host) |
|||
{ |
|||
host.SetEncoder(ImageFormats.Tiff, new TiffEncoder()); |
|||
host.SetDecoder(ImageFormats.Tiff, new TiffDecoder()); |
|||
host.AddImageFormatDetector(new TiffImageFormatDetector()); |
|||
} |
|||
} |
|||
} |
|||
@ -1,40 +0,0 @@ |
|||
// <copyright file="TiffEncoderOptions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Formats |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates the options for the <see cref="TiffEncoder"/>.
|
|||
/// </summary>
|
|||
public sealed class TiffEncoderOptions : EncoderOptions, ITiffEncoderOptions |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="TiffEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
public TiffEncoderOptions() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="TiffEncoderOptions"/> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
private TiffEncoderOptions(IEncoderOptions options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts the options to a <see cref="ITiffEncoderOptions"/> instance with a
|
|||
/// cast or by creating a new instance with the specfied options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options for the encoder.</param>
|
|||
/// <returns>The options for the <see cref="TiffEncoder"/>.</returns>
|
|||
internal static ITiffEncoderOptions Create(IEncoderOptions options) |
|||
{ |
|||
return options as ITiffEncoderOptions ?? new TiffEncoderOptions(options); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// <copyright file="TiffImageFormatDetector.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; |
|||
|
|||
/// <summary>
|
|||
/// Detects tiff file headers
|
|||
/// </summary>
|
|||
public sealed class TiffImageFormatDetector : IImageFormatDetector |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public int HeaderSize => 4; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) |
|||
{ |
|||
if (this.IsSupportedFileFormat(header)) |
|||
{ |
|||
return ImageFormats.Tiff; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
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
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,89 @@ |
|||
// <copyright file="TiffImageFormatDetectorTests.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 TiffImageFormatDetectorTests |
|||
{ |
|||
public static object[][] IsLittleEndianValues = new[] { new object[] { false }, |
|||
new object[] { true } }; |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(IsLittleEndianValues))] |
|||
public void DetectFormat_ReturnsTiffFormat_ForValidFile(bool isLittleEndian) |
|||
{ |
|||
byte[] bytes = new TiffGenHeader() |
|||
{ |
|||
FirstIfd = new TiffGenIfd() |
|||
} |
|||
.ToBytes(isLittleEndian); |
|||
|
|||
TiffImageFormatDetector formatDetector = new TiffImageFormatDetector(); |
|||
byte[] headerBytes = bytes.Take(formatDetector.HeaderSize).ToArray(); |
|||
var format = formatDetector.DetectFormat(headerBytes); |
|||
|
|||
Assert.NotNull(format); |
|||
Assert.IsType<TiffFormat>(format); |
|||
} |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(IsLittleEndianValues))] |
|||
public void DetectFormat_ReturnsNull_WithInvalidByteOrderMarkers(bool isLittleEndian) |
|||
{ |
|||
byte[] bytes = new TiffGenHeader() |
|||
{ |
|||
FirstIfd = new TiffGenIfd(), |
|||
ByteOrderMarker = 0x1234 |
|||
} |
|||
.ToBytes(isLittleEndian); |
|||
|
|||
TiffImageFormatDetector formatDetector = new TiffImageFormatDetector(); |
|||
byte[] headerBytes = bytes.Take(formatDetector.HeaderSize).ToArray(); |
|||
var format = formatDetector.DetectFormat(headerBytes); |
|||
|
|||
Assert.Null(format); |
|||
} |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(IsLittleEndianValues))] |
|||
public void DetectFormat_ReturnsNull_WithIncorrectMagicNumber(bool isLittleEndian) |
|||
{ |
|||
byte[] bytes = new TiffGenHeader() |
|||
{ |
|||
FirstIfd = new TiffGenIfd(), |
|||
MagicNumber = 32 |
|||
} |
|||
.ToBytes(isLittleEndian); |
|||
|
|||
TiffImageFormatDetector formatDetector = new TiffImageFormatDetector(); |
|||
byte[] headerBytes = bytes.Take(formatDetector.HeaderSize).ToArray(); |
|||
var format = formatDetector.DetectFormat(headerBytes); |
|||
|
|||
Assert.Null(format); |
|||
} |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(IsLittleEndianValues))] |
|||
public void DetectFormat_ReturnsNull_WithShortHeader(bool isLittleEndian) |
|||
{ |
|||
byte[] bytes = new TiffGenHeader() |
|||
{ |
|||
FirstIfd = new TiffGenIfd() |
|||
} |
|||
.ToBytes(isLittleEndian); |
|||
|
|||
TiffImageFormatDetector formatDetector = new TiffImageFormatDetector(); |
|||
byte[] headerBytes = bytes.Take(formatDetector.HeaderSize - 1).ToArray(); |
|||
var format = formatDetector.DetectFormat(headerBytes); |
|||
|
|||
Assert.Null(format); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue