From 3b4e32ec6271598f5bff1d5d766b0e70ea3dd9dc 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: ecec072f33fde156aa0aaf64d92ae5f22907d77c --- 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 b971f276e9..de1efd4c0b 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 de590aea2b..a90ee8ce8d 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 3693d23e15..36067736c0 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 87c9a0d30d..0ce3f1fef8 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 8ad4e07639..7c3fdcaa4a 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 78265d458e..dc35db6ed7 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 1c4e96aba0..6949b36551 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 dacc0dd1a0..b708abd7de 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 } + }; } }