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