ImageHeader.cs 8.0 KB

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