Browse Source

Fixing file type detection

Former-commit-id: 111a537976f3adfc876d502855a5933d2b530180
af/merge-core
James South 12 years ago
parent
commit
a985e2def0
  1. 6
      src/ImageProcessor/Imaging/Formats/BitmapFormat.cs
  2. 4
      src/ImageProcessor/Imaging/Formats/FormatBase.cs
  3. 14
      src/ImageProcessor/Imaging/Formats/FormatUtilities.cs
  4. 6
      src/ImageProcessor/Imaging/Formats/GifFormat.cs
  5. 4
      src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs
  6. 6
      src/ImageProcessor/Imaging/Formats/JpegFormat.cs
  7. 6
      src/ImageProcessor/Imaging/Formats/PngFormat.cs
  8. 10
      src/ImageProcessor/Imaging/Formats/TiffFormat.cs

6
src/ImageProcessor/Imaging/Formats/BitmapFormat.cs

@ -19,13 +19,13 @@ namespace ImageProcessor.Imaging.Formats
public class BitmapFormat : FormatBase public class BitmapFormat : FormatBase
{ {
/// <summary> /// <summary>
/// Gets the file header. /// Gets the file headers.
/// </summary> /// </summary>
public override byte[] FileHeader public override byte[][] FileHeaders
{ {
get get
{ {
return Encoding.ASCII.GetBytes("BM"); return new[] { Encoding.ASCII.GetBytes("BM") };
} }
} }

4
src/ImageProcessor/Imaging/Formats/FormatBase.cs

@ -21,9 +21,9 @@ namespace ImageProcessor.Imaging.Formats
public abstract class FormatBase : ISupportedImageFormat public abstract class FormatBase : ISupportedImageFormat
{ {
/// <summary> /// <summary>
/// Gets the file header. /// Gets the file headers.
/// </summary> /// </summary>
public abstract byte[] FileHeader { get; } public abstract byte[][] FileHeaders { get; }
/// <summary> /// <summary>
/// Gets the list of file extensions. /// Gets the list of file extensions.

14
src/ImageProcessor/Imaging/Formats/FormatUtilities.cs

@ -43,14 +43,18 @@ namespace ImageProcessor.Imaging.Formats
byte[] buffer = new byte[4]; byte[] buffer = new byte[4];
stream.Read(buffer, 0, buffer.Length); stream.Read(buffer, 0, buffer.Length);
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (ISupportedImageFormat supportedImageFormat in supportedImageFormats) foreach (ISupportedImageFormat supportedImageFormat in supportedImageFormats)
{ {
byte[] header = supportedImageFormat.FileHeader; byte[][] headers = supportedImageFormat.FileHeaders;
if (header.SequenceEqual(buffer.Take(header.Length)))
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (byte[] header in headers)
{ {
stream.Position = 0; if (header.SequenceEqual(buffer.Take(header.Length)))
return supportedImageFormat; {
stream.Position = 0;
return supportedImageFormat;
}
} }
} }

6
src/ImageProcessor/Imaging/Formats/GifFormat.cs

@ -23,13 +23,13 @@ namespace ImageProcessor.Imaging.Formats
public class GifFormat : FormatBase public class GifFormat : FormatBase
{ {
/// <summary> /// <summary>
/// Gets the file header. /// Gets the file headers.
/// </summary> /// </summary>
public override byte[] FileHeader public override byte[][] FileHeaders
{ {
get get
{ {
return Encoding.ASCII.GetBytes("GIF"); return new[] { Encoding.ASCII.GetBytes("GIF") };
} }
} }

4
src/ImageProcessor/Imaging/Formats/ISupportedImageFormat.cs

@ -21,9 +21,9 @@ namespace ImageProcessor.Imaging.Formats
public interface ISupportedImageFormat public interface ISupportedImageFormat
{ {
/// <summary> /// <summary>
/// Gets the file header. /// Gets the file headers.
/// </summary> /// </summary>
byte[] FileHeader { get; } byte[][] FileHeaders { get; }
/// <summary> /// <summary>
/// Gets the list of file extensions. /// Gets the list of file extensions.

6
src/ImageProcessor/Imaging/Formats/JpegFormat.cs

@ -23,13 +23,13 @@ namespace ImageProcessor.Imaging.Formats
public sealed class JpegFormat : FormatBase public sealed class JpegFormat : FormatBase
{ {
/// <summary> /// <summary>
/// Gets the file header. /// Gets the file headers.
/// </summary> /// </summary>
public override byte[] FileHeader public override byte[][] FileHeaders
{ {
get get
{ {
return new byte[] { 255, 216, 255 }; return new[] { new byte[] { 255, 216, 255 } };
} }
} }

6
src/ImageProcessor/Imaging/Formats/PngFormat.cs

@ -20,13 +20,13 @@ namespace ImageProcessor.Imaging.Formats
public class PngFormat : FormatBase public class PngFormat : FormatBase
{ {
/// <summary> /// <summary>
/// Gets the file header. /// Gets the file headers.
/// </summary> /// </summary>
public override byte[] FileHeader public override byte[][] FileHeaders
{ {
get get
{ {
return new byte[] { 137, 80, 78, 71 }; return new[] { new byte[] { 137, 80, 78, 71 } };
} }
} }

10
src/ImageProcessor/Imaging/Formats/TiffFormat.cs

@ -21,13 +21,17 @@ namespace ImageProcessor.Imaging.Formats
public class TiffFormat : FormatBase public class TiffFormat : FormatBase
{ {
/// <summary> /// <summary>
/// Gets the file header. /// Gets the file headers.
/// </summary> /// </summary>
public override byte[] FileHeader public override byte[][] FileHeaders
{ {
get get
{ {
return new byte[] { 77, 77, 42 }; return new[]
{
new byte[] { 73, 73, 42 },
new byte[] { 77, 77, 42 }
};
} }
} }

Loading…
Cancel
Save