ImageHeader.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Model.Drawing;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. namespace Emby.Drawing.Common
  9. {
  10. /// <summary>
  11. /// Taken from http://stackoverflow.com/questions/111345/getting-image-dimensions-without-reading-the-entire-file/111349
  12. /// http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height
  13. /// Minor improvements including supporting unsigned 16-bit integers when decoding Jfif and added logic
  14. /// to load the image using new Bitmap if reading the headers fails
  15. /// </summary>
  16. public static class ImageHeader
  17. {
  18. /// <summary>
  19. /// The error message
  20. /// </summary>
  21. const string ErrorMessage = "Could not recognize image format.";
  22. /// <summary>
  23. /// The image format decoders
  24. /// </summary>
  25. private static readonly KeyValuePair<byte[], Func<BinaryReader, ImageSize>>[] ImageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, ImageSize>>
  26. {
  27. { new byte[] { 0x42, 0x4D }, DecodeBitmap },
  28. { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
  29. { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
  30. { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
  31. { new byte[] { 0xff, 0xd8 }, DecodeJfif }
  32. }.ToArray();
  33. private static readonly int MaxMagicBytesLength = ImageFormatDecoders.Select(i => i.Key.Length).OrderByDescending(i => i).First();
  34. /// <summary>
  35. /// Gets the dimensions of an image.
  36. /// </summary>
  37. /// <param name="path">The path of the image to get the dimensions of.</param>
  38. /// <param name="logger">The logger.</param>
  39. /// <param name="fileSystem">The file system.</param>
  40. /// <returns>The dimensions of the specified image.</returns>
  41. /// <exception cref="ArgumentException">The image was of an unrecognised format.</exception>
  42. public static ImageSize GetDimensions(string path, ILogger logger, IFileSystem fileSystem)
  43. {
  44. using (var fs = fileSystem.OpenRead(path))
  45. {
  46. using (var binaryReader = new BinaryReader(fs))
  47. {
  48. return GetDimensions(binaryReader);
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// Gets the dimensions of an image.
  54. /// </summary>
  55. /// <param name="binaryReader">The binary reader.</param>
  56. /// <returns>Size.</returns>
  57. /// <exception cref="System.ArgumentException">binaryReader</exception>
  58. /// <exception cref="ArgumentException">The image was of an unrecognized format.</exception>
  59. private static ImageSize GetDimensions(BinaryReader binaryReader)
  60. {
  61. var magicBytes = new byte[MaxMagicBytesLength];
  62. for (var i = 0; i < MaxMagicBytesLength; i += 1)
  63. {
  64. magicBytes[i] = binaryReader.ReadByte();
  65. foreach (var kvPair in ImageFormatDecoders)
  66. {
  67. if (StartsWith(magicBytes, kvPair.Key))
  68. {
  69. return kvPair.Value(binaryReader);
  70. }
  71. }
  72. }
  73. throw new ArgumentException(ErrorMessage, "binaryReader");
  74. }
  75. /// <summary>
  76. /// Startses the with.
  77. /// </summary>
  78. /// <param name="thisBytes">The this bytes.</param>
  79. /// <param name="thatBytes">The that bytes.</param>
  80. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  81. private static bool StartsWith(byte[] thisBytes, byte[] thatBytes)
  82. {
  83. for (int i = 0; i < thatBytes.Length; i += 1)
  84. {
  85. if (thisBytes[i] != thatBytes[i])
  86. {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. /// <summary>
  93. /// Reads the little endian int16.
  94. /// </summary>
  95. /// <param name="binaryReader">The binary reader.</param>
  96. /// <returns>System.Int16.</returns>
  97. private static short ReadLittleEndianInt16(BinaryReader binaryReader)
  98. {
  99. var bytes = new byte[sizeof(short)];
  100. for (int i = 0; i < sizeof(short); i += 1)
  101. {
  102. bytes[sizeof(short) - 1 - i] = binaryReader.ReadByte();
  103. }
  104. return BitConverter.ToInt16(bytes, 0);
  105. }
  106. /// <summary>
  107. /// Reads the little endian int32.
  108. /// </summary>
  109. /// <param name="binaryReader">The binary reader.</param>
  110. /// <returns>System.Int32.</returns>
  111. private static int ReadLittleEndianInt32(BinaryReader binaryReader)
  112. {
  113. var bytes = new byte[sizeof(int)];
  114. for (int i = 0; i < sizeof(int); i += 1)
  115. {
  116. bytes[sizeof(int) - 1 - i] = binaryReader.ReadByte();
  117. }
  118. return BitConverter.ToInt32(bytes, 0);
  119. }
  120. /// <summary>
  121. /// Decodes the bitmap.
  122. /// </summary>
  123. /// <param name="binaryReader">The binary reader.</param>
  124. /// <returns>Size.</returns>
  125. private static ImageSize DecodeBitmap(BinaryReader binaryReader)
  126. {
  127. binaryReader.ReadBytes(16);
  128. int width = binaryReader.ReadInt32();
  129. int height = binaryReader.ReadInt32();
  130. return new ImageSize
  131. {
  132. Width = width,
  133. Height = height
  134. };
  135. }
  136. /// <summary>
  137. /// Decodes the GIF.
  138. /// </summary>
  139. /// <param name="binaryReader">The binary reader.</param>
  140. /// <returns>Size.</returns>
  141. private static ImageSize DecodeGif(BinaryReader binaryReader)
  142. {
  143. int width = binaryReader.ReadInt16();
  144. int height = binaryReader.ReadInt16();
  145. return new ImageSize
  146. {
  147. Width = width,
  148. Height = height
  149. };
  150. }
  151. /// <summary>
  152. /// Decodes the PNG.
  153. /// </summary>
  154. /// <param name="binaryReader">The binary reader.</param>
  155. /// <returns>Size.</returns>
  156. private static ImageSize DecodePng(BinaryReader binaryReader)
  157. {
  158. binaryReader.ReadBytes(8);
  159. int width = ReadLittleEndianInt32(binaryReader);
  160. int height = ReadLittleEndianInt32(binaryReader);
  161. return new ImageSize
  162. {
  163. Width = width,
  164. Height = height
  165. };
  166. }
  167. /// <summary>
  168. /// Decodes the jfif.
  169. /// </summary>
  170. /// <param name="binaryReader">The binary reader.</param>
  171. /// <returns>Size.</returns>
  172. /// <exception cref="System.ArgumentException"></exception>
  173. private static ImageSize DecodeJfif(BinaryReader binaryReader)
  174. {
  175. while (binaryReader.ReadByte() == 0xff)
  176. {
  177. byte marker = binaryReader.ReadByte();
  178. short chunkLength = ReadLittleEndianInt16(binaryReader);
  179. if (marker == 0xc0)
  180. {
  181. binaryReader.ReadByte();
  182. int height = ReadLittleEndianInt16(binaryReader);
  183. int width = ReadLittleEndianInt16(binaryReader);
  184. return new ImageSize
  185. {
  186. Width = width,
  187. Height = height
  188. };
  189. }
  190. if (chunkLength < 0)
  191. {
  192. var uchunkLength = (ushort)chunkLength;
  193. binaryReader.ReadBytes(uchunkLength - 2);
  194. }
  195. else
  196. {
  197. binaryReader.ReadBytes(chunkLength - 2);
  198. }
  199. }
  200. throw new ArgumentException(ErrorMessage);
  201. }
  202. }
  203. }