VinnoCameraExtendedData.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace fis.Vid
  6. {
  7. public class VinnoCameraExtendedData:IDisposable
  8. {
  9. private readonly OperationMode _operationMode;
  10. private readonly Stream _stream;
  11. private readonly VinnoStreamReader _reader;
  12. private readonly VinnoStreamWriter _writer;
  13. private readonly string _filePath;
  14. private bool _disposed;
  15. private bool _closed;
  16. private readonly List<long> _imagePositionList;
  17. private readonly object _addGetLocker = new object();
  18. private const string Header = "VINNO Camera Extended Data";
  19. public int ImageCount { get; private set; }
  20. /// <summary>
  21. /// Create Mode
  22. /// </summary>
  23. /// <param name="path"></param>
  24. public VinnoCameraExtendedData(string path)
  25. {
  26. _filePath = path + ".ced";
  27. _operationMode = OperationMode.Create;
  28. _stream = new FileStream(_filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
  29. _writer = new VinnoStreamWriter(_stream);
  30. _imagePositionList = new List<long>();
  31. }
  32. /// <summary>
  33. /// Open Mode
  34. /// </summary>
  35. /// <param name="path"></param>
  36. /// <param name="positionList"></param>
  37. internal VinnoCameraExtendedData(string path,List<long> positionList)
  38. {
  39. _filePath = path + ".ced";
  40. _operationMode = OperationMode.Open;
  41. _stream = new FileStream(_filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  42. _reader = new VinnoStreamReader(_stream);
  43. _imagePositionList = positionList;
  44. ImageCount = _imagePositionList.Count;
  45. }
  46. public void AddImage(VinnoImage image)
  47. {
  48. if (_operationMode != OperationMode.Create)
  49. {
  50. throw new InvalidOperationException("Can not add image under open mode.");
  51. }
  52. lock (_addGetLocker)
  53. {
  54. var postion = _stream.Position;
  55. _writer.WriteBytes(image.ToBytes());
  56. _imagePositionList.Add(postion);
  57. ImageCount++;
  58. }
  59. }
  60. public VinnoImage GetImage(int index)
  61. {
  62. if (_operationMode != OperationMode.Open)
  63. {
  64. throw new InvalidOperationException("Can not open image under create mode.");
  65. }
  66. if (index >= ImageCount || index < 0)
  67. {
  68. throw new IndexOutOfRangeException("Can not find image Data");
  69. }
  70. lock (_addGetLocker)
  71. {
  72. //Jump to image.
  73. _stream.Position = _imagePositionList[index];
  74. var imageData = _reader.ReadBytes();
  75. return VinnoImage.FromBytes(imageData);
  76. }
  77. }
  78. public void Close()
  79. {
  80. if (!_closed)
  81. {
  82. lock (_addGetLocker)
  83. {
  84. _stream.Dispose();
  85. File.Delete(_filePath);
  86. }
  87. _closed = true;
  88. }
  89. }
  90. public void Dispose()
  91. {
  92. if (!_disposed)
  93. {
  94. Close();
  95. GC.SuppressFinalize(this);
  96. _disposed = true;
  97. }
  98. }
  99. public byte[] ToBytes()
  100. {
  101. lock (_addGetLocker)
  102. {
  103. if (!_imagePositionList.Any())
  104. {
  105. return new byte[0];
  106. }
  107. using (var stream = new MemoryStream())
  108. {
  109. var writer = new VinnoStreamWriter(stream);
  110. writer.WriteString(Header);
  111. writer.WriteLongs(_imagePositionList.ToArray());
  112. _stream.Position = 0;
  113. _stream.CopyTo(stream);
  114. return stream.ToArray();
  115. }
  116. }
  117. }
  118. public static VinnoCameraExtendedData FromBytes(byte[] bytes,string path)
  119. {
  120. using (var stream = new MemoryStream(bytes))
  121. {
  122. stream.Position = 0;
  123. var reader = new VinnoStreamReader(stream);
  124. var header = reader.ReadString();
  125. if (!string.Equals(header,Header))
  126. {
  127. return null;
  128. }
  129. var imagePositions = reader.ReadLongs();
  130. using (var fileStream = new FileStream(path + ".ced", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
  131. {
  132. var writer = new VinnoStreamWriter(fileStream);
  133. for (int i = 0; i < imagePositions.Length; i++)
  134. {
  135. writer.WriteBytes(reader.ReadBytes());
  136. }
  137. }
  138. return new VinnoCameraExtendedData(path, imagePositions.ToList());
  139. }
  140. }
  141. }
  142. }