mirror of https://github.com/SixLabors/ImageSharp
11 changed files with 132 additions and 125 deletions
@ -1,53 +0,0 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Tiff |
|||
{ |
|||
/// <summary>
|
|||
/// The tiff data stream factory class.
|
|||
/// </summary>
|
|||
internal static class TiffStreamFactory |
|||
{ |
|||
/// <summary>
|
|||
/// Creates the specified byte order.
|
|||
/// </summary>
|
|||
/// <param name="byteOrder">The byte order.</param>
|
|||
/// <param name="stream">The stream.</param>
|
|||
public static TiffStream Create(TiffByteOrder byteOrder, Stream stream) |
|||
{ |
|||
if (byteOrder == TiffByteOrder.BigEndian) |
|||
{ |
|||
return new TiffBigEndianStream(stream); |
|||
} |
|||
else if (byteOrder == TiffByteOrder.LittleEndian) |
|||
{ |
|||
return new TiffLittleEndianStream(stream); |
|||
} |
|||
|
|||
throw new ArgumentOutOfRangeException(nameof(byteOrder)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Reads the byte order of stream.
|
|||
/// </summary>
|
|||
/// <param name="stream">The stream.</param>
|
|||
public static TiffByteOrder ReadByteOrder(Stream stream) |
|||
{ |
|||
byte[] headerBytes = new byte[2]; |
|||
stream.Read(headerBytes, 0, 2); |
|||
if (headerBytes[0] == TiffConstants.ByteOrderLittleEndian && headerBytes[1] == TiffConstants.ByteOrderLittleEndian) |
|||
{ |
|||
return TiffByteOrder.LittleEndian; |
|||
} |
|||
else if (headerBytes[0] == TiffConstants.ByteOrderBigEndian && headerBytes[1] == TiffConstants.ByteOrderBigEndian) |
|||
{ |
|||
return TiffByteOrder.BigEndian; |
|||
} |
|||
|
|||
throw new ImageFormatException("Invalid TIFF file header."); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Tiff |
|||
{ |
|||
/// <summary>
|
|||
/// Cold path optimizations for throwing tiff format based exceptions.
|
|||
/// </summary>
|
|||
internal static class TiffThrowHelper |
|||
{ |
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static Exception TagNotFound(string tagName) |
|||
=> new ArgumentException("Required tag is not found.", tagName); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static void ThrowTagNotFound(string tagName) |
|||
=> throw TagNotFound(tagName); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static void ThrowBadZlibHeader(int cmf) => throw new ImageFormatException($"Bad compression method for ZLIB header: cmf={cmf}"); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static Exception NotSupportedCompression(string compressionType) => new NotSupportedException("Not supported compression: " + compressionType); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static void ThrowNotSupportedCompression(string compressionType) => throw NotSupportedCompression(compressionType); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static Exception InvalidColorType(string colorType) => new NotSupportedException("Invalid color type: " + colorType); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static Exception InvalidHeader() => new ImageFormatException("Invalid TIFF file header."); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static void ThrowInvalidHeader() => throw InvalidHeader(); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static void ThrowOutOfRange(string structure) => throw new InvalidDataException($"Out of range of {structure} structure."); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static void ThrowBadStringEntry() => throw new ImageFormatException("The retrieved string is not null terminated."); |
|||
|
|||
[MethodImpl(InliningOptions.ColdPath)] |
|||
public static void ThrowNotSupported(string message) => throw new NotSupportedException(message); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue