mirror of https://github.com/SixLabors/ImageSharp
6 changed files with 238 additions and 0 deletions
@ -0,0 +1,84 @@ |
|||
// <copyright file="TiffDecoderImageTests.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.IO; |
|||
using Xunit; |
|||
|
|||
using ImageSharp.Formats; |
|||
|
|||
public class TiffDecoderImageTests |
|||
{ |
|||
public const int ImageWidth = 200; |
|||
public const int ImageHeight = 150; |
|||
|
|||
public static object[][] IsLittleEndianValues = new[] { new object[] { false }, |
|||
new object[] { true } }; |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(IsLittleEndianValues))] |
|||
public void DecodeImage_SetsImageDimensions(bool isLittleEndian) |
|||
{ |
|||
Stream stream = CreateTiffGenIfd() |
|||
.ToStream(isLittleEndian); |
|||
|
|||
TiffDecoderCore decoder = new TiffDecoderCore(stream, isLittleEndian, null); |
|||
TiffIfd ifd = decoder.ReadIfd(0); |
|||
Image<Color> image = new Image<Color>(1,1); |
|||
|
|||
decoder.DecodeImage(ifd, image); |
|||
|
|||
Assert.Equal(ImageWidth, image.Width); |
|||
Assert.Equal(ImageHeight, image.Height); |
|||
} |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(IsLittleEndianValues))] |
|||
public void DecodeImage_ThrowsException_WithMissingImageWidth(bool isLittleEndian) |
|||
{ |
|||
Stream stream = CreateTiffGenIfd() |
|||
.WithoutEntry(TiffTags.ImageWidth) |
|||
.ToStream(isLittleEndian); |
|||
|
|||
TiffDecoderCore decoder = new TiffDecoderCore(stream, isLittleEndian, null); |
|||
TiffIfd ifd = decoder.ReadIfd(0); |
|||
Image<Color> image = new Image<Color>(1,1); |
|||
|
|||
var e = Assert.Throws<ImageFormatException>(() => decoder.DecodeImage(ifd, image)); |
|||
|
|||
Assert.Equal("The TIFF IFD does not specify the image dimensions.", e.Message); |
|||
} |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(IsLittleEndianValues))] |
|||
public void DecodeImage_ThrowsException_WithMissingImageLength(bool isLittleEndian) |
|||
{ |
|||
Stream stream = CreateTiffGenIfd() |
|||
.WithoutEntry(TiffTags.ImageLength) |
|||
.ToStream(isLittleEndian); |
|||
|
|||
TiffDecoderCore decoder = new TiffDecoderCore(stream, isLittleEndian, null); |
|||
TiffIfd ifd = decoder.ReadIfd(0); |
|||
Image<Color> image = new Image<Color>(1,1); |
|||
|
|||
var e = Assert.Throws<ImageFormatException>(() => decoder.DecodeImage(ifd, image)); |
|||
|
|||
Assert.Equal("The TIFF IFD does not specify the image dimensions.", e.Message); |
|||
} |
|||
|
|||
private TiffGenIfd CreateTiffGenIfd() |
|||
{ |
|||
return new TiffGenIfd() |
|||
{ |
|||
Entries = |
|||
{ |
|||
TiffGenEntry.Integer(TiffTags.ImageWidth, TiffType.Long, ImageWidth), |
|||
TiffGenEntry.Integer(TiffTags.ImageLength, TiffType.Long, ImageHeight), |
|||
} |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// <copyright file="TiffIfdEntryTests.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.IO; |
|||
using Xunit; |
|||
|
|||
using ImageSharp.Formats; |
|||
|
|||
public class TiffIfdEntryTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_SetsProperties() |
|||
{ |
|||
var entry = new TiffIfdEntry((ushort)10u, TiffType.Short, 20u, new byte[] { 2, 4, 6, 8 }); |
|||
|
|||
Assert.Equal(10u, entry.Tag); |
|||
Assert.Equal(TiffType.Short, entry.Type); |
|||
Assert.Equal(20u, entry.Count); |
|||
Assert.Equal(new byte[] { 2, 4, 6, 8 }, entry.Value); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
// <copyright file="TiffIfdTests.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.IO; |
|||
using Xunit; |
|||
|
|||
using ImageSharp.Formats; |
|||
|
|||
public class TiffIfdTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_SetsProperties() |
|||
{ |
|||
var entries = new TiffIfdEntry[10]; |
|||
var ifd = new TiffIfd(entries, 1234u); |
|||
|
|||
Assert.Equal(entries, ifd.Entries); |
|||
Assert.Equal(1234u, ifd.NextIfdOffset); |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryGetIfdEntry_ReturnsIfdIfExists() |
|||
{ |
|||
var entries = new[] |
|||
{ |
|||
new TiffIfdEntry(10, TiffType.Short, 20, new byte[4]), |
|||
new TiffIfdEntry(20, TiffType.Short, 20, new byte[4]), |
|||
new TiffIfdEntry(30, TiffType.Short, 20, new byte[4]), |
|||
new TiffIfdEntry(40, TiffType.Short, 20, new byte[4]) |
|||
}; |
|||
var ifd = new TiffIfd(entries, 1234u); |
|||
|
|||
bool success = ifd.TryGetIfdEntry(30, out var entry); |
|||
|
|||
Assert.Equal(true, success); |
|||
Assert.Equal(30, entry.Tag); |
|||
} |
|||
|
|||
[Fact] |
|||
public void TryGetIfdEntry_ReturnsFalseOtherwise() |
|||
{ |
|||
var entries = new[] |
|||
{ |
|||
new TiffIfdEntry(10, TiffType.Short, 20, new byte[4]), |
|||
new TiffIfdEntry(20, TiffType.Short, 20, new byte[4]), |
|||
new TiffIfdEntry(30, TiffType.Short, 20, new byte[4]), |
|||
new TiffIfdEntry(40, TiffType.Short, 20, new byte[4]) |
|||
}; |
|||
var ifd = new TiffIfd(entries, 1234u); |
|||
|
|||
bool success = ifd.TryGetIfdEntry(25, out var entry); |
|||
|
|||
Assert.Equal(false, success); |
|||
Assert.Equal(0, entry.Tag); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
// <copyright file="TiffGenIfdExtensions.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; |
|||
using System.IO; |
|||
using System.Linq; |
|||
|
|||
/// <summary>
|
|||
/// A utility class for manipulating in-memory Tiff files for use in unit tests.
|
|||
/// </summary>
|
|||
internal static class TiffGenIfdExtensions |
|||
{ |
|||
public static TiffGenIfd WithoutEntry(this TiffGenIfd ifd, ushort tag) |
|||
{ |
|||
TiffGenEntry entry = ifd.Entries.First(e => e.Tag == tag); |
|||
ifd.Entries.Remove(entry); |
|||
return ifd; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue