jellyfin/MediaBrowser.Server.Implementations/Drawing/ImageHeader.cs

254 lines
8.9 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
2014-12-09 04:57:18 +00:00
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Logging;
2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-09-18 18:49:06 +00:00
namespace MediaBrowser.Server.Implementations.Drawing
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Taken from http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file/111349
/// http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height
/// Minor improvements including supporting unsigned 16-bit integers when decoding Jfif and added logic
/// to load the image using new Bitmap if reading the headers fails
/// </summary>
public static class ImageHeader
{
/// <summary>
/// The error message
/// </summary>
2013-09-18 02:43:34 +00:00
const string ErrorMessage = "Could not recognize image format.";
2013-02-21 01:33:05 +00:00
/// <summary>
/// The image format decoders
/// </summary>
2014-12-09 04:57:18 +00:00
private static readonly KeyValuePair<byte[], Func<BinaryReader, ImageSize>>[] ImageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, ImageSize>>
2013-02-21 01:33:05 +00:00
{
{ new byte[] { 0x42, 0x4D }, DecodeBitmap },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
{ new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
{ new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
2013-09-18 02:43:34 +00:00
{ new byte[] { 0xff, 0xd8 }, DecodeJfif }
}.ToArray();
private static readonly int MaxMagicBytesLength = ImageFormatDecoders.Select(i => i.Key.Length).OrderByDescending(i => i).First();
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the dimensions of an image.
/// </summary>
/// <param name="path">The path of the image to get the dimensions of.</param>
2013-02-21 20:26:35 +00:00
/// <param name="logger">The logger.</param>
/// <param name="fileSystem">The file system.</param>
2013-02-21 01:33:05 +00:00
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognised format.</exception>
2014-12-09 04:57:18 +00:00
public static ImageSize GetDimensions(string path, ILogger logger, IFileSystem fileSystem)
2013-02-21 01:33:05 +00:00
{
try
{
using (var fs = File.OpenRead(path))
{
using (var binaryReader = new BinaryReader(fs))
{
return GetDimensions(binaryReader);
}
}
}
catch
{
2013-02-21 20:26:35 +00:00
logger.Info("Failed to read image header for {0}. Doing it the slow way.", path);
}
2013-02-21 01:33:05 +00:00
// Buffer to memory stream to avoid image locking file
using (var fs = fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var memoryStream = new MemoryStream())
2013-02-21 01:33:05 +00:00
{
2013-09-17 02:44:06 +00:00
fs.CopyTo(memoryStream);
2013-12-13 04:06:38 +00:00
memoryStream.Position = 0;
2013-02-21 01:33:05 +00:00
// Co it the old fashioned way
2014-12-09 04:57:18 +00:00
using (var b = System.Drawing.Image.FromStream(memoryStream, true, false))
2013-02-21 01:33:05 +00:00
{
2014-12-09 04:57:18 +00:00
var size = b.Size;
return new ImageSize
{
Width = size.Width,
Height = size.Height
};
2013-02-21 01:33:05 +00:00
}
}
}
}
/// <summary>
/// Gets the dimensions of an image.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
/// <exception cref="System.ArgumentException">binaryReader</exception>
/// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
2014-12-09 04:57:18 +00:00
private static ImageSize GetDimensions(BinaryReader binaryReader)
2013-02-21 01:33:05 +00:00
{
2013-09-18 02:43:34 +00:00
var magicBytes = new byte[MaxMagicBytesLength];
for (var i = 0; i < MaxMagicBytesLength; i += 1)
2013-02-21 01:33:05 +00:00
{
magicBytes[i] = binaryReader.ReadByte();
2013-09-18 02:43:34 +00:00
foreach (var kvPair in ImageFormatDecoders)
2013-02-21 01:33:05 +00:00
{
if (StartsWith(magicBytes, kvPair.Key))
{
return kvPair.Value(binaryReader);
}
}
}
2013-09-18 02:43:34 +00:00
throw new ArgumentException(ErrorMessage, "binaryReader");
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Startses the with.
/// </summary>
/// <param name="thisBytes">The this bytes.</param>
/// <param name="thatBytes">The that bytes.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
private static bool StartsWith(byte[] thisBytes, byte[] thatBytes)
{
for (int i = 0; i < thatBytes.Length; i += 1)
{
if (thisBytes[i] != thatBytes[i])
{
return false;
}
}
return true;
}
/// <summary>
/// Reads the little endian int16.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>System.Int16.</returns>
private static short ReadLittleEndianInt16(BinaryReader binaryReader)
{
var bytes = new byte[sizeof(short)];
for (int i = 0; i < sizeof(short); i += 1)
{
bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
}
return BitConverter.ToInt16(bytes, 0);
}
/// <summary>
/// Reads the little endian int32.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>System.Int32.</returns>
private static int ReadLittleEndianInt32(BinaryReader binaryReader)
{
var bytes = new byte[sizeof(int)];
for (int i = 0; i < sizeof(int); i += 1)
{
bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
}
return BitConverter.ToInt32(bytes, 0);
}
/// <summary>
/// Decodes the bitmap.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
2014-12-09 04:57:18 +00:00
private static ImageSize DecodeBitmap(BinaryReader binaryReader)
2013-02-21 01:33:05 +00:00
{
binaryReader.ReadBytes(16);
int width = binaryReader.ReadInt32();
int height = binaryReader.ReadInt32();
2014-12-09 04:57:18 +00:00
return new ImageSize
{
Width = width,
Height = height
};
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Decodes the GIF.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
2014-12-09 04:57:18 +00:00
private static ImageSize DecodeGif(BinaryReader binaryReader)
2013-02-21 01:33:05 +00:00
{
int width = binaryReader.ReadInt16();
int height = binaryReader.ReadInt16();
2014-12-09 04:57:18 +00:00
return new ImageSize
{
Width = width,
Height = height
};
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Decodes the PNG.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
2014-12-09 04:57:18 +00:00
private static ImageSize DecodePng(BinaryReader binaryReader)
2013-02-21 01:33:05 +00:00
{
binaryReader.ReadBytes(8);
int width = ReadLittleEndianInt32(binaryReader);
int height = ReadLittleEndianInt32(binaryReader);
2014-12-09 04:57:18 +00:00
return new ImageSize
{
Width = width,
Height = height
};
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Decodes the jfif.
/// </summary>
/// <param name="binaryReader">The binary reader.</param>
/// <returns>Size.</returns>
/// <exception cref="System.ArgumentException"></exception>
2014-12-09 04:57:18 +00:00
private static ImageSize DecodeJfif(BinaryReader binaryReader)
2013-02-21 01:33:05 +00:00
{
while (binaryReader.ReadByte() == 0xff)
{
byte marker = binaryReader.ReadByte();
short chunkLength = ReadLittleEndianInt16(binaryReader);
if (marker == 0xc0)
{
binaryReader.ReadByte();
int height = ReadLittleEndianInt16(binaryReader);
int width = ReadLittleEndianInt16(binaryReader);
2014-12-09 04:57:18 +00:00
return new ImageSize
{
Width = width,
Height = height
};
2013-02-21 01:33:05 +00:00
}
if (chunkLength < 0)
{
var uchunkLength = (ushort)chunkLength;
binaryReader.ReadBytes(uchunkLength - 2);
}
else
{
binaryReader.ReadBytes(chunkLength - 2);
}
}
2013-09-18 02:43:34 +00:00
throw new ArgumentException(ErrorMessage);
2013-02-21 01:33:05 +00:00
}
}
}