diff --git a/Emby.Drawing/Common/ImageHeader.cs b/Emby.Drawing/Common/ImageHeader.cs deleted file mode 100644 index c08cdabb2f..0000000000 --- a/Emby.Drawing/Common/ImageHeader.cs +++ /dev/null @@ -1,242 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using MediaBrowser.Model.Drawing; -using MediaBrowser.Model.IO; -using Microsoft.Extensions.Logging; - -namespace Emby.Drawing.Common -{ - /// - /// 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 - /// - public static class ImageHeader - { - /// - /// The error message - /// - const string ErrorMessage = "Could not recognize image format."; - - /// - /// The image format decoders - /// - private static readonly KeyValuePair>[] ImageFormatDecoders = new Dictionary> - { - { 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 }, - { new byte[] { 0xff, 0xd8 }, DecodeJfif } - - }.ToArray(); - - private static readonly int MaxMagicBytesLength = ImageFormatDecoders.Select(i => i.Key.Length).OrderByDescending(i => i).First(); - - private static string[] SupportedExtensions = new string[] { ".jpg", ".jpeg", ".png", ".gif" }; - - /// - /// Gets the dimensions of an image. - /// - /// The path of the image to get the dimensions of. - /// The logger. - /// The file system. - /// The dimensions of the specified image. - /// The image was of an unrecognised format. - public static ImageSize GetDimensions(string path, ILogger logger, IFileSystem fileSystem) - { - if (string.IsNullOrEmpty(path)) - { - throw new ArgumentNullException(nameof(path)); - } - - string extension = Path.GetExtension(path).ToLower(); - - if (!SupportedExtensions.Contains(extension)) - { - throw new ArgumentException("ImageHeader doesn't support " + extension); - } - - using (var fs = fileSystem.OpenRead(path)) - { - using (var binaryReader = new BinaryReader(fs)) - { - return GetDimensions(binaryReader); - } - } - } - - /// - /// Gets the dimensions of an image. - /// - /// The binary reader. - /// Size. - /// binaryReader - /// The image was of an unrecognized format. - private static ImageSize GetDimensions(BinaryReader binaryReader) - { - var magicBytes = new byte[MaxMagicBytesLength]; - - for (var i = 0; i < MaxMagicBytesLength; i += 1) - { - magicBytes[i] = binaryReader.ReadByte(); - - foreach (var kvPair in ImageFormatDecoders) - { - if (StartsWith(magicBytes, kvPair.Key)) - { - return kvPair.Value(binaryReader); - } - } - } - - throw new ArgumentException(ErrorMessage, nameof(binaryReader)); - } - - /// - /// Startses the with. - /// - /// The this bytes. - /// The that bytes. - /// true if XXXX, false otherwise - 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; - } - - /// - /// Reads the little endian int16. - /// - /// The binary reader. - /// System.Int16. - private static short ReadLittleEndianInt16(this 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); - } - - /// - /// Reads the little endian int32. - /// - /// The binary reader. - /// System.Int32. - private static int ReadLittleEndianInt32(this 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); - } - - /// - /// Decodes the bitmap. - /// - /// The binary reader. - /// Size. - private static ImageSize DecodeBitmap(BinaryReader binaryReader) - { - binaryReader.ReadBytes(16); - int width = binaryReader.ReadInt32(); - int height = binaryReader.ReadInt32(); - return new ImageSize - { - Width = width, - Height = height - }; - } - - /// - /// Decodes the GIF. - /// - /// The binary reader. - /// Size. - private static ImageSize DecodeGif(BinaryReader binaryReader) - { - int width = binaryReader.ReadInt16(); - int height = binaryReader.ReadInt16(); - return new ImageSize - { - Width = width, - Height = height - }; - } - - /// - /// Decodes the PNG. - /// - /// The binary reader. - /// Size. - private static ImageSize DecodePng(BinaryReader binaryReader) - { - binaryReader.ReadBytes(8); - int width = ReadLittleEndianInt32(binaryReader); - int height = ReadLittleEndianInt32(binaryReader); - return new ImageSize - { - Width = width, - Height = height - }; - } - - /// - /// Decodes the jfif. - /// - /// The binary reader. - /// Size. - /// - private static ImageSize DecodeJfif(BinaryReader binaryReader) - { - // A JPEG image consists of a sequence of segments, - // each beginning with a marker, each of which begins with a 0xFF byte - // followed by a byte indicating what kind of marker it is. - // Source: https://en.wikipedia.org/wiki/JPEG#Syntax_and_structure - while (binaryReader.ReadByte() == 0xff) - { - byte marker = binaryReader.ReadByte(); - short chunkLength = binaryReader.ReadLittleEndianInt16(); - // SOF0: Indicates that this is a baseline DCT-based JPEG, - // and specifies the width, height, number of components, and component subsampling - // SOF2: Indicates that this is a progressive DCT-based JPEG, - // and specifies the width, height, number of components, and component subsampling - if (marker == 0xc0 || marker == 0xc2) - { - // https://help.accusoft.com/ImageGear/v18.2/Windows/ActiveX/IGAX-10-12.html - binaryReader.ReadByte(); // We don't care about the first byte - int height = binaryReader.ReadLittleEndianInt16(); - int width = binaryReader.ReadLittleEndianInt16(); - return new ImageSize(width, height); - } - - if (chunkLength < 0) - { - ushort uchunkLength = (ushort)chunkLength; - binaryReader.ReadBytes(uchunkLength - 2); - } - else - { - binaryReader.ReadBytes(chunkLength - 2); - } - } - - throw new ArgumentException(ErrorMessage); - } - } -} diff --git a/Emby.Drawing/Emby.Drawing.csproj b/Emby.Drawing/Emby.Drawing.csproj index ba29c656bf..542d7f5639 100644 --- a/Emby.Drawing/Emby.Drawing.csproj +++ b/Emby.Drawing/Emby.Drawing.csproj @@ -5,6 +5,10 @@ + + + + diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index 28aae9caec..4095e4176b 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -1,3 +1,4 @@ +using SkiaSharp; using System; using System.Collections.Generic; using System.Globalization; @@ -5,7 +6,6 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; -using Emby.Drawing.Common; using MediaBrowser.Common.Extensions; using MediaBrowser.Controller; using MediaBrowser.Controller.Drawing; @@ -472,7 +472,16 @@ namespace Emby.Drawing try { - return ImageHeader.GetDimensions(path, _logger, _fileSystem); + using (var s = new SKFileStream(path)) + using (var codec = SKCodec.Create(s)) + { + var info = codec.Info; + return new ImageSize + { + Height = info.Height, + Width = info.Width + }; + } } catch {