ImageHeader.cs 7.8 KB

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