From f0237696f92007385affdacc3147a71d8f81dab0 Mon Sep 17 00:00:00 2001 From: Andrew Wilkinson Date: Sun, 5 Mar 2017 11:26:41 +0000 Subject: [PATCH] Make TiffDecoderCore more unit-testable --- .../TiffDecoderCore.cs | 24 ++++++++----- .../Formats/Tiff/TiffDecoderHeaderTests.cs | 34 +++++++++++++++++++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp.Formats.Tiff/TiffDecoderCore.cs b/src/ImageSharp.Formats.Tiff/TiffDecoderCore.cs index 18a5c3f5e..aee57b1ea 100644 --- a/src/ImageSharp.Formats.Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp.Formats.Tiff/TiffDecoderCore.cs @@ -20,11 +20,6 @@ namespace ImageSharp.Formats /// private readonly IDecoderOptions options; - /// - /// A flag indicating if the file is encoded in little-endian or big-endian format. - /// - private bool isLittleEndian; - /// /// Initializes a new instance of the class. /// @@ -34,11 +29,22 @@ namespace ImageSharp.Formats this.options = options ?? new DecoderOptions(); } + public TiffDecoderCore(Stream stream, bool isLittleEndian, IDecoderOptions options) : this(options) + { + this.InputStream = stream; + this.IsLittleEndian = isLittleEndian; + } + /// /// Gets the input stream. /// public Stream InputStream { get; private set; } + /// + /// A flag indicating if the file is encoded in little-endian or big-endian format. + /// + public bool IsLittleEndian; + /// /// Decodes the image from the specified and sets /// the data to image. @@ -62,13 +68,13 @@ namespace ImageSharp.Formats { } - private uint ReadHeader() + public uint ReadHeader() { byte[] headerBytes = new byte[8]; ReadBytes(headerBytes, 8); if (headerBytes[0] == TiffConstants.ByteOrderLittleEndian && headerBytes[1] == TiffConstants.ByteOrderLittleEndian) - isLittleEndian = true; + IsLittleEndian = true; else if (headerBytes[0] != TiffConstants.ByteOrderBigEndian && headerBytes[1] != TiffConstants.ByteOrderBigEndian) throw new ImageFormatException("Invalid TIFF file header."); @@ -102,7 +108,7 @@ namespace ImageSharp.Formats private Int16 ToInt16(byte[] bytes, int offset) { - if (isLittleEndian) + if (IsLittleEndian) return (short)(bytes[offset + 0] | (bytes[offset + 1] << 8)); else return (short)((bytes[offset + 0] << 8) | bytes[offset + 1]); @@ -110,7 +116,7 @@ namespace ImageSharp.Formats private Int32 ToInt32(byte[] bytes, int offset) { - if (isLittleEndian) + if (IsLittleEndian) return bytes[offset + 0] | (bytes[offset + 1] << 8) | (bytes[offset + 2] << 16) | (bytes[offset + 3] << 24); else return (bytes[offset + 0] << 24) | (bytes[offset + 1] << 16) | (bytes[offset + 2] << 8) | bytes[offset + 3]; diff --git a/tests/ImageSharp.Formats.Tiff.Tests/Formats/Tiff/TiffDecoderHeaderTests.cs b/tests/ImageSharp.Formats.Tiff.Tests/Formats/Tiff/TiffDecoderHeaderTests.cs index 00b826ef0..9394519e3 100644 --- a/tests/ImageSharp.Formats.Tiff.Tests/Formats/Tiff/TiffDecoderHeaderTests.cs +++ b/tests/ImageSharp.Formats.Tiff.Tests/Formats/Tiff/TiffDecoderHeaderTests.cs @@ -16,6 +16,40 @@ namespace ImageSharp.Tests public static object[][] IsLittleEndianValues = new[] { new object[] { false }, new object[] { true } }; + [Theory] + [MemberData(nameof(IsLittleEndianValues))] + public void ReadHeader_ReadsEndianness(bool isLittleEndian) + { + Stream stream = new TiffGenHeader() + { + FirstIfd = new TiffGenIfd() + } + .ToStream(isLittleEndian); + + TiffDecoderCore decoder = new TiffDecoderCore(stream, false, null); + + decoder.ReadHeader(); + + Assert.Equal(isLittleEndian, decoder.IsLittleEndian); + } + + [Theory] + [MemberData(nameof(IsLittleEndianValues))] + public void ReadHeader_ReadsFirstIfdOffset(bool isLittleEndian) + { + Stream stream = new TiffGenHeader() + { + FirstIfd = new TiffGenIfd() + } + .ToStream(isLittleEndian); + + TiffDecoderCore decoder = new TiffDecoderCore(stream, false, null); + + uint firstIfdOffset = decoder.ReadHeader(); + + Assert.Equal(8u, firstIfdOffset); + } + [Theory] [MemberData(nameof(IsLittleEndianValues))] public void Decode_ThrowsException_WithInvalidByteOrderMarkers(bool isLittleEndian)