vid_us_image_data.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import 'dart:typed_data';
  2. import 'package:vid/us/vid_us_data_http_reader.dart';
  3. import 'package:vid/us/vid_us_data_reader.dart';
  4. import 'package:vid/us/vid_us_image.dart';
  5. import 'package:vid/us/vid_us_probe.dart';
  6. enum VidUsImageFormat {
  7. Jpeg,
  8. Png,
  9. H264,
  10. Zip,
  11. Diff,
  12. }
  13. ///Only suport read for now.
  14. ///Not suport carotid3d for now.
  15. class VidUsImageData {
  16. late VidUsDataReader _reader;
  17. late List<int> _imagePositionList;
  18. late int _version;
  19. late int _imageCount;
  20. late VidUsProbe _probe;
  21. late VidUsImageFormat _imageFormat;
  22. late Uint8List _extendedData;
  23. final String header = "VINNO IMAGE DATA";
  24. /// Gets the version of this image data.
  25. int get version => _version;
  26. /// Gets the image count of this image data.
  27. int get imageCount => _imageCount;
  28. /// Gets the probe information.
  29. VidUsProbe get probe => _probe;
  30. /// Gets the image format of this image data.
  31. VidUsImageFormat get imageFormat => _imageFormat;
  32. /// Gets or sets the extended data.
  33. Uint8List get extendedData => _extendedData;
  34. /// Create a VINNO Image Data.
  35. VidUsImageData(Uint8List data) {
  36. _reader = VidUsDataReader(data);
  37. var header = _reader.readString();
  38. if (header != header) {
  39. throw Exception("The input data is not a VID data.");
  40. }
  41. _version = _reader.readInt();
  42. //Get probe info
  43. var probeData = _reader.readBytes();
  44. _probe = VidUsProbe.fromBytes(probeData);
  45. _imageFormat = VidUsImageFormat.values[_reader.readInt()];
  46. _extendedData = _reader.readBytes();
  47. _imagePositionList = [];
  48. var imagePositionListData = _reader.readBytes();
  49. _imageCount = imagePositionListData.length ~/ 8;
  50. var imagePositionReader = VidUsDataReader(imagePositionListData);
  51. for (var i = 0; i < _imageCount; i++) {
  52. _imagePositionList.add(imagePositionReader.readInt64V2());
  53. }
  54. }
  55. /// Get one image from the vid.
  56. VidUsImage getImage(int index) {
  57. if (index >= _imageCount || index < 0) {
  58. throw Exception("Can not find image Data");
  59. }
  60. //Jump to image.
  61. var imageData = _reader.readBytes(_imagePositionList[index]);
  62. return VidUsImage.fromBytes(imageData);
  63. }
  64. }
  65. class ReadTimeoutException implements Exception {}
  66. class AlreadyClosedException implements Exception {}
  67. ///Only suport read for now.
  68. ///Not suport carotid3d for now.
  69. class HttpVidUsImageData {
  70. late VidUsDataHttpReader _reader;
  71. late List<int> _imagePositionList;
  72. late int _version;
  73. late int _imageCount;
  74. late VidUsProbe _probe;
  75. late VidUsImageFormat _imageFormat;
  76. late Uint8List _extendedData;
  77. late int _readHeaderTimeout;
  78. late int _readImageTimeout;
  79. late bool _closed;
  80. final Duration delayDuration = const Duration(milliseconds: 10);
  81. final String header = "VINNO IMAGE DATA";
  82. /// Gets the version of this image data.
  83. int get version => _version;
  84. /// Gets the image count of this image data.
  85. int get imageCount => _imageCount;
  86. /// Gets the probe information.
  87. VidUsProbe get probe => _probe;
  88. /// Gets the image format of this image data.
  89. VidUsImageFormat get imageFormat => _imageFormat;
  90. /// Gets or sets the extended data.
  91. Uint8List get extendedData => _extendedData;
  92. /// Create a VINNO Image Data.
  93. /// [readHeaderTimeout] is the timeout value of reading header and probe information etc...
  94. /// [readImageTimeout] is the timeout value of reading one frame.
  95. HttpVidUsImageData(String url, {DownloadCallback? downloadCallback, int readHeaderTimeout = 1000, int readImageTimeout = 300}) {
  96. _closed = false;
  97. _readHeaderTimeout = readHeaderTimeout;
  98. _readImageTimeout = readImageTimeout;
  99. _reader = VidUsDataHttpReader(url, downloadCallback: downloadCallback);
  100. }
  101. //Initialize the StreamedVidUsImageData
  102. Future initialize() async {
  103. var header = await _readHeader();
  104. if (header != header) {
  105. throw Exception("The input data is not a VID data.");
  106. }
  107. _version = await _readVersion();
  108. //Get probe info
  109. _probe = await _readProbe();
  110. _imageFormat = await _readImageFormat();
  111. _extendedData = await _readExtendedData();
  112. _imagePositionList = [];
  113. var imagePositionListData = await _readImagePositionListData();
  114. _imageCount = imagePositionListData.length ~/ 8;
  115. var imagePositionReader = VidUsDataReader(imagePositionListData);
  116. for (var i = 0; i < _imageCount; i++) {
  117. _imagePositionList.add(imagePositionReader.readInt64V2());
  118. }
  119. }
  120. void close() {
  121. _closed = true;
  122. _reader.close();
  123. }
  124. //Read header from http data.
  125. Future<String> _readHeader() async {
  126. var timeout = 0;
  127. while (!_closed) {
  128. try {
  129. var header = _reader.readString();
  130. return header;
  131. } catch (ex) {
  132. if (ex is NotReadyException) {
  133. if (timeout >= _readHeaderTimeout) {
  134. throw ReadTimeoutException();
  135. }
  136. await Future.delayed(delayDuration);
  137. timeout += delayDuration.inMilliseconds;
  138. } else {
  139. rethrow;
  140. }
  141. }
  142. }
  143. throw AlreadyClosedException();
  144. }
  145. //Read the version from http data.
  146. Future<int> _readVersion() async {
  147. var timeout = 0;
  148. while (!_closed) {
  149. try {
  150. var version = _reader.readInt();
  151. return version;
  152. } catch (ex) {
  153. if (ex is NotReadyException) {
  154. if (timeout >= _readHeaderTimeout) {
  155. throw ReadTimeoutException();
  156. }
  157. await Future.delayed(delayDuration);
  158. timeout += delayDuration.inMilliseconds;
  159. } else {
  160. rethrow;
  161. }
  162. }
  163. }
  164. throw AlreadyClosedException();
  165. }
  166. //Read the probe info from http data.
  167. Future<VidUsProbe> _readProbe() async {
  168. var timeout = 0;
  169. while (!_closed) {
  170. try {
  171. var probeData = _reader.readBytes();
  172. return VidUsProbe.fromBytes(probeData);
  173. } catch (ex) {
  174. if (ex is NotReadyException) {
  175. if (timeout >= _readHeaderTimeout) {
  176. throw ReadTimeoutException();
  177. }
  178. await Future.delayed(delayDuration);
  179. timeout += delayDuration.inMilliseconds;
  180. } else {
  181. rethrow;
  182. }
  183. }
  184. }
  185. throw AlreadyClosedException();
  186. }
  187. //Read the image format from http data.
  188. Future<VidUsImageFormat> _readImageFormat() async {
  189. var timeout = 0;
  190. while (!_closed) {
  191. try {
  192. return VidUsImageFormat.values[_reader.readInt()];
  193. } catch (ex) {
  194. if (ex is NotReadyException) {
  195. if (timeout >= _readHeaderTimeout) {
  196. throw ReadTimeoutException();
  197. }
  198. await Future.delayed(delayDuration);
  199. timeout += delayDuration.inMilliseconds;
  200. } else {
  201. rethrow;
  202. }
  203. }
  204. }
  205. throw AlreadyClosedException();
  206. }
  207. //Read extended data from http data.
  208. Future<Uint8List> _readExtendedData() async {
  209. var timeout = 0;
  210. while (!_closed) {
  211. try {
  212. return _reader.readBytes();
  213. } catch (ex) {
  214. if (ex is NotReadyException) {
  215. if (timeout >= _readHeaderTimeout) {
  216. throw ReadTimeoutException();
  217. }
  218. await Future.delayed(delayDuration);
  219. timeout += delayDuration.inMilliseconds;
  220. } else {
  221. rethrow;
  222. }
  223. }
  224. }
  225. throw AlreadyClosedException();
  226. }
  227. //Read the image positions data from http data.
  228. Future<Uint8List> _readImagePositionListData() async {
  229. var timeout = 0;
  230. while (!_closed) {
  231. try {
  232. return _reader.readBytes();
  233. } catch (ex) {
  234. if (ex is NotReadyException) {
  235. if (timeout >= _readHeaderTimeout) {
  236. throw ReadTimeoutException();
  237. }
  238. await Future.delayed(delayDuration);
  239. timeout += delayDuration.inMilliseconds;
  240. } else {
  241. rethrow;
  242. }
  243. }
  244. }
  245. throw AlreadyClosedException();
  246. }
  247. /// Get one image from the vid.
  248. Future<VidUsImage> getImage(int index) async {
  249. if (index >= _imageCount || index < 0) {
  250. throw Exception("Can not find image Data");
  251. }
  252. //Jump to image.
  253. var timeout = 0;
  254. while (!_closed) {
  255. try {
  256. var imageData = _reader.readBytes(_imagePositionList[index]);
  257. return VidUsImage.fromBytes(imageData);
  258. } catch (ex) {
  259. if (ex is NotReadyException) {
  260. if (timeout >= _readImageTimeout) {
  261. throw ReadTimeoutException();
  262. }
  263. await Future.delayed(delayDuration);
  264. timeout += delayDuration.inMilliseconds;
  265. } else {
  266. rethrow;
  267. }
  268. }
  269. }
  270. throw AlreadyClosedException();
  271. }
  272. }