mirror of https://github.com/SixLabors/ImageSharp
5 changed files with 201 additions and 5 deletions
@ -0,0 +1,22 @@ |
|||||
|
// <copyright file="TiffIfd.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>
|
||||
|
/// Data structure for holding details of each TIFF IFD
|
||||
|
/// </summary>
|
||||
|
internal struct TiffIfd |
||||
|
{ |
||||
|
public TiffIfdEntry[] Entries; |
||||
|
public uint NextIfdOffset; |
||||
|
|
||||
|
public TiffIfd(TiffIfdEntry[] entries, uint nextIfdOffset) |
||||
|
{ |
||||
|
this.Entries = entries; |
||||
|
this.NextIfdOffset = nextIfdOffset; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
// <copyright file="TiffIfdEntry.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>
|
||||
|
/// Data structure for holding details of each TIFF IFD entry
|
||||
|
/// </summary>
|
||||
|
internal struct TiffIfdEntry |
||||
|
{ |
||||
|
public ushort Tag; |
||||
|
public TiffType Type; |
||||
|
public uint Count; |
||||
|
public byte[] Value; |
||||
|
|
||||
|
public TiffIfdEntry(ushort tag, TiffType type, uint count, byte[] value) |
||||
|
{ |
||||
|
this.Tag = tag; |
||||
|
this.Type = type; |
||||
|
this.Count = count; |
||||
|
this.Value = value; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,111 @@ |
|||||
|
// <copyright file="TiffDecoderIfdTests.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 System.Linq; |
||||
|
using Xunit; |
||||
|
|
||||
|
using ImageSharp.Formats; |
||||
|
|
||||
|
public class TiffDecoderIfdTests |
||||
|
{ |
||||
|
public static object[][] IsLittleEndianValues = new[] { new object[] { false }, |
||||
|
new object[] { true } }; |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void ReadIfd_ReadsNextIfdOffset_IfPresent(bool isLittleEndian) |
||||
|
{ |
||||
|
Stream stream = new TiffGenIfd() |
||||
|
{ |
||||
|
Entries = |
||||
|
{ |
||||
|
TiffGenEntry.Integer(TiffTags.ImageWidth, TiffType.Long, 150) |
||||
|
}, |
||||
|
NextIfd = new TiffGenIfd() |
||||
|
} |
||||
|
.ToStream(isLittleEndian); |
||||
|
|
||||
|
TiffDecoderCore decoder = new TiffDecoderCore(stream, isLittleEndian, null); |
||||
|
TiffIfd ifd = decoder.ReadIfd(0); |
||||
|
|
||||
|
Assert.Equal(18u, ifd.NextIfdOffset); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void ReadIfd_ReadsNextIfdOffset_ZeroIfLastIfd(bool isLittleEndian) |
||||
|
{ |
||||
|
Stream stream = new TiffGenIfd() |
||||
|
{ |
||||
|
Entries = |
||||
|
{ |
||||
|
TiffGenEntry.Integer(TiffTags.ImageWidth, TiffType.Long, 150) |
||||
|
} |
||||
|
} |
||||
|
.ToStream(isLittleEndian); |
||||
|
|
||||
|
TiffDecoderCore decoder = new TiffDecoderCore(stream, isLittleEndian, null); |
||||
|
TiffIfd ifd = decoder.ReadIfd(0); |
||||
|
|
||||
|
Assert.Equal(0u, ifd.NextIfdOffset); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void ReadIfd_ReturnsCorrectNumberOfEntries(bool isLittleEndian) |
||||
|
{ |
||||
|
Stream stream = new TiffGenIfd() |
||||
|
{ |
||||
|
Entries = |
||||
|
{ |
||||
|
TiffGenEntry.Integer(TiffTags.ImageWidth, TiffType.Long, 150), |
||||
|
TiffGenEntry.Integer(TiffTags.ImageLength, TiffType.Long, 210), |
||||
|
TiffGenEntry.Integer(TiffTags.Orientation, TiffType.Short, 1), |
||||
|
TiffGenEntry.Ascii(TiffTags.Artist, "Image Artist Name"), |
||||
|
TiffGenEntry.Ascii(TiffTags.HostComputer, "Host Computer Name") |
||||
|
}, |
||||
|
NextIfd = new TiffGenIfd() |
||||
|
} |
||||
|
.ToStream(isLittleEndian); |
||||
|
|
||||
|
TiffDecoderCore decoder = new TiffDecoderCore(stream, isLittleEndian, null); |
||||
|
TiffIfd ifd = decoder.ReadIfd(0); |
||||
|
|
||||
|
Assert.NotNull(ifd.Entries); |
||||
|
Assert.Equal(5, ifd.Entries.Length); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[MemberData(nameof(IsLittleEndianValues))] |
||||
|
public void ReadIfd_ReadsRawTiffEntryData(bool isLittleEndian) |
||||
|
{ |
||||
|
Stream stream = new TiffGenIfd() |
||||
|
{ |
||||
|
Entries = |
||||
|
{ |
||||
|
TiffGenEntry.Integer(TiffTags.ImageWidth, TiffType.Long, 150), |
||||
|
TiffGenEntry.Integer(TiffTags.ImageLength, TiffType.Long, 210), |
||||
|
TiffGenEntry.Integer(TiffTags.Orientation, TiffType.Short, 1) |
||||
|
}, |
||||
|
NextIfd = new TiffGenIfd() |
||||
|
} |
||||
|
.ToStream(isLittleEndian); |
||||
|
|
||||
|
TiffDecoderCore decoder = new TiffDecoderCore(stream, isLittleEndian, null); |
||||
|
TiffIfd ifd = decoder.ReadIfd(0); |
||||
|
TiffIfdEntry entry = ifd.Entries[1]; |
||||
|
|
||||
|
byte[] expectedData = isLittleEndian ? new byte[] {210,0,0,0} : new byte[] {0,0,0,210}; |
||||
|
Assert.NotNull(entry); |
||||
|
Assert.Equal(TiffTags.ImageLength, entry.Tag); |
||||
|
Assert.Equal(TiffType.Long, entry.Type); |
||||
|
Assert.Equal(4u, entry.Count); |
||||
|
Assert.Equal(expectedData, entry.Value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue