From a985e2def00b4e344e156868e16b39e301420e8b Mon Sep 17 00:00:00 2001 From: James South Date: Fri, 20 Jun 2014 15:27:45 +0100 Subject: [PATCH] Fixing file type detection Former-commit-id: 111a537976f3adfc876d502855a5933d2b530180 --- src/ImageProcessor/Imaging/Formats/BitmapFormat.cs | 6 +++--- src/ImageProcessor/Imaging/Formats/FormatBase.cs | 4 ++-- .../Imaging/Formats/FormatUtilities.cs | 14 +++++++++----- src/ImageProcessor/Imaging/Formats/GifFormat.cs | 6 +++--- .../Imaging/Formats/ISupportedImageFormat.cs | 4 ++-- src/ImageProcessor/Imaging/Formats/JpegFormat.cs | 6 +++--- src/ImageProcessor/Imaging/Formats/PngFormat.cs | 6 +++--- src/ImageProcessor/Imaging/Formats/TiffFormat.cs | 10 +++++++--- 8 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/ImageProcessor/Imaging/Formats/BitmapFormat.cs b/src/ImageProcessor/Imaging/Formats/BitmapFormat.cs index b971f276e..de1efd4c0 100644 --- a/src/ImageProcessor/Imaging/Formats/BitmapFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/BitmapFormat.cs @@ -19,13 +19,13 @@ namespace ImageProcessor.Imaging.Formats public class BitmapFormat : FormatBase { /// - /// Gets the file header. + /// Gets the file headers. /// - public override byte[] FileHeader + public override byte[][] FileHeaders { get { - return Encoding.ASCII.GetBytes("BM"); + return new[] { Encoding.ASCII.GetBytes("BM") }; } } diff --git a/src/ImageProcessor/Imaging/Formats/FormatBase.cs b/src/ImageProcessor/Imaging/Formats/FormatBase.cs index de590aea2..a90ee8ce8 100644 --- a/src/ImageProcessor/Imaging/Formats/FormatBase.cs +++ b/src/ImageProcessor/Imaging/Formats/FormatBase.cs @@ -21,9 +21,9 @@ namespace ImageProcessor.Imaging.Formats public abstract class FormatBase : ISupportedImageFormat { /// - /// Gets the file header. + /// Gets the file headers. /// - public abstract byte[] FileHeader { get; } + public abstract byte[][] FileHeaders { get; } /// /// Gets the list of file extensions. diff --git a/src/ImageProcessor/Imaging/Formats/FormatUtilities.cs b/src/ImageProcessor/Imaging/Formats/FormatUtilities.cs index 3693d23e1..36067736c 100644 --- a/src/ImageProcessor/Imaging/Formats/FormatUtilities.cs +++ b/src/ImageProcessor/Imaging/Formats/FormatUtilities.cs @@ -43,14 +43,18 @@ namespace ImageProcessor.Imaging.Formats byte[] buffer = new byte[4]; stream.Read(buffer, 0, buffer.Length); - // ReSharper disable once LoopCanBeConvertedToQuery foreach (ISupportedImageFormat supportedImageFormat in supportedImageFormats) { - byte[] header = supportedImageFormat.FileHeader; - if (header.SequenceEqual(buffer.Take(header.Length))) + byte[][] headers = supportedImageFormat.FileHeaders; + + // ReSharper disable once LoopCanBeConvertedToQuery + foreach (byte[] header in headers) { - stream.Position = 0; - return supportedImageFormat; + if (header.SequenceEqual(buffer.Take(header.Length))) + { + stream.Position = 0; + return supportedImageFormat; + } } } diff --git a/src/ImageProcessor/Imaging/Formats/GifFormat.cs b/src/ImageProcessor/Imaging/Formats/GifFormat.cs index 87c9a0d30..0ce3f1fef 100644 --- a/src/ImageProcessor/Imaging/Formats/GifFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/GifFormat.cs @@ -23,13 +23,13 @@ namespace ImageProcessor.Imaging.Formats public class GifFormat : FormatBase { /// - /// Gets the file header. + /// Gets the file headers. /// - public override byte[] FileHeader + public override byte[][] FileHeaders { get { - return Encoding.ASCII.GetBytes("GIF"); + return new[] { Encoding.ASCII.GetBytes("GIF") }; } } diff --git a/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs b/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs index 8ad4e0763..7c3fdcaa4 100644 --- a/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs @@ -21,9 +21,9 @@ namespace ImageProcessor.Imaging.Formats public interface ISupportedImageFormat { /// - /// Gets the file header. + /// Gets the file headers. /// - byte[] FileHeader { get; } + byte[][] FileHeaders { get; } /// /// Gets the list of file extensions. diff --git a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs index 78265d458..dc35db6ed 100644 --- a/src/ImageProcessor/Imaging/Formats/JpegFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/JpegFormat.cs @@ -23,13 +23,13 @@ namespace ImageProcessor.Imaging.Formats public sealed class JpegFormat : FormatBase { /// - /// Gets the file header. + /// Gets the file headers. /// - public override byte[] FileHeader + public override byte[][] FileHeaders { get { - return new byte[] { 255, 216, 255 }; + return new[] { new byte[] { 255, 216, 255 } }; } } diff --git a/src/ImageProcessor/Imaging/Formats/PngFormat.cs b/src/ImageProcessor/Imaging/Formats/PngFormat.cs index 1c4e96aba..6949b3655 100644 --- a/src/ImageProcessor/Imaging/Formats/PngFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/PngFormat.cs @@ -20,13 +20,13 @@ namespace ImageProcessor.Imaging.Formats public class PngFormat : FormatBase { /// - /// Gets the file header. + /// Gets the file headers. /// - public override byte[] FileHeader + public override byte[][] FileHeaders { get { - return new byte[] { 137, 80, 78, 71 }; + return new[] { new byte[] { 137, 80, 78, 71 } }; } } diff --git a/src/ImageProcessor/Imaging/Formats/TiffFormat.cs b/src/ImageProcessor/Imaging/Formats/TiffFormat.cs index dacc0dd1a..b708abd7d 100644 --- a/src/ImageProcessor/Imaging/Formats/TiffFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/TiffFormat.cs @@ -21,13 +21,17 @@ namespace ImageProcessor.Imaging.Formats public class TiffFormat : FormatBase { /// - /// Gets the file header. + /// Gets the file headers. /// - public override byte[] FileHeader + public override byte[][] FileHeaders { get { - return new byte[] { 77, 77, 42 }; + return new[] + { + new byte[] { 73, 73, 42 }, + new byte[] { 77, 77, 42 } + }; } }