Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. namespace ExifLib
  3. {
  4. /// <summary>
  5. /// Utility to handle multi-byte primitives in both big and little endian.
  6. /// http://msdn.microsoft.com/en-us/library/system.bitconverter.islittleendian.aspx
  7. /// http://en.wikipedia.org/wiki/Endianness
  8. /// </summary>
  9. public static class ExifIO
  10. {
  11. public static short ReadShort(byte[] Data, int offset, bool littleEndian)
  12. {
  13. if ((littleEndian && BitConverter.IsLittleEndian) ||
  14. (!littleEndian && !BitConverter.IsLittleEndian))
  15. {
  16. return BitConverter.ToInt16(Data, offset);
  17. }
  18. else
  19. {
  20. byte[] beBytes = new byte[2] { Data[offset + 1], Data[offset] };
  21. return BitConverter.ToInt16(beBytes, 0);
  22. }
  23. }
  24. public static ushort ReadUShort(byte[] Data, int offset, bool littleEndian)
  25. {
  26. if ((littleEndian && BitConverter.IsLittleEndian) ||
  27. (!littleEndian && !BitConverter.IsLittleEndian))
  28. {
  29. return BitConverter.ToUInt16(Data, offset);
  30. }
  31. else
  32. {
  33. byte[] beBytes = new byte[2] { Data[offset + 1], Data[offset] };
  34. return BitConverter.ToUInt16(beBytes, 0);
  35. }
  36. }
  37. public static int ReadInt(byte[] Data, int offset, bool littleEndian)
  38. {
  39. if ((littleEndian && BitConverter.IsLittleEndian) ||
  40. (!littleEndian && !BitConverter.IsLittleEndian))
  41. {
  42. return BitConverter.ToInt32(Data, offset);
  43. }
  44. else
  45. {
  46. byte[] beBytes = new byte[4] { Data[offset + 3], Data[offset + 2], Data[offset + 1], Data[offset] };
  47. return BitConverter.ToInt32(beBytes, 0);
  48. }
  49. }
  50. public static uint ReadUInt(byte[] Data, int offset, bool littleEndian)
  51. {
  52. if ((littleEndian && BitConverter.IsLittleEndian) ||
  53. (!littleEndian && !BitConverter.IsLittleEndian))
  54. {
  55. return BitConverter.ToUInt32(Data, offset);
  56. }
  57. else
  58. {
  59. byte[] beBytes = new byte[4] { Data[offset + 3], Data[offset + 2], Data[offset + 1], Data[offset] };
  60. return BitConverter.ToUInt32(beBytes, 0);
  61. }
  62. }
  63. public static float ReadSingle(byte[] Data, int offset, bool littleEndian)
  64. {
  65. if ((littleEndian && BitConverter.IsLittleEndian) ||
  66. (!littleEndian && !BitConverter.IsLittleEndian))
  67. {
  68. return BitConverter.ToSingle(Data, offset);
  69. }
  70. else
  71. {
  72. // need to swap the data first
  73. byte[] beBytes = new byte[4] { Data[offset + 3], Data[offset + 2], Data[offset + 1], Data[offset] };
  74. return BitConverter.ToSingle(beBytes, 0);
  75. }
  76. }
  77. public static double ReadDouble(byte[] Data, int offset, bool littleEndian)
  78. {
  79. if ((littleEndian && BitConverter.IsLittleEndian) ||
  80. (!littleEndian && !BitConverter.IsLittleEndian))
  81. {
  82. return BitConverter.ToDouble(Data, offset);
  83. }
  84. else
  85. {
  86. // need to swap the data first
  87. byte[] beBytes = new byte[8] {
  88. Data[offset + 7], Data[offset + 6], Data[offset + 5], Data[offset + 4],
  89. Data[offset + 3], Data[offset + 2], Data[offset + 1], Data[offset]};
  90. return BitConverter.ToDouble(beBytes, 0);
  91. }
  92. }
  93. }
  94. }