Browse Source

Refactor code.

justin.xing 3 years ago
parent
commit
173b68819c
2 changed files with 19 additions and 35 deletions
  1. 11 32
      lib/us/vid_us_data_reader.dart
  2. 8 3
      lib/us/vid_us_image_data.dart

+ 11 - 32
lib/us/vid_us_data_reader.dart

@@ -3,11 +3,13 @@ import 'dart:typed_data';
 
 class VidUsDataReader {
   late final ByteBuffer _buffer;
+  late final int _bufferOffset;
   late int _index;
 
   VidUsDataReader(Uint8List data, [int offset = 0]) {
     _buffer = data.buffer;
-    _index = data.offsetInBytes + offset;
+    _bufferOffset = data.offsetInBytes;
+    _index = _bufferOffset + offset;
   }
 
   ///Read int value from binary data(little endian),
@@ -18,7 +20,7 @@ class VidUsDataReader {
       return intData.getInt32(0, Endian.little);
     } else {
       var intData = _buffer.asByteData(index, 4);
-      _index = index + 4;
+      _index = _bufferOffset + index + 4;
       return intData.getInt32(0, Endian.little);
     }
   }
@@ -46,7 +48,7 @@ class VidUsDataReader {
       return intData.getInt16(0, Endian.little);
     } else {
       var intData = _buffer.asByteData(index, 2);
-      _index = index + 2;
+      _index = _bufferOffset + index + 2;
       return intData.getInt16(0, Endian.little);
     }
   }
@@ -60,7 +62,7 @@ class VidUsDataReader {
       return intData.getInt64(0, Endian.little);
     } else {
       var intData = _buffer.asByteData(index, 8);
-      _index = index + 8;
+      _index = _bufferOffset + index + 8;
       return intData.getInt64(0, Endian.little);
     }
   }
@@ -76,6 +78,7 @@ class VidUsDataReader {
       return value;
     } else {
       int low = readInt(index);
+      //Read the next int.
       int high = readInt();
       int value = high >> 32;
       value |= low;
@@ -91,7 +94,7 @@ class VidUsDataReader {
       return floatData.getFloat32(0, Endian.little);
     } else {
       var floatData = _buffer.asByteData(index, 4);
-      _index = index + 4;
+      _index = _bufferOffset + index + 4;
       return floatData.getFloat32(0, Endian.little);
     }
   }
@@ -104,7 +107,7 @@ class VidUsDataReader {
       return floatData.getFloat64(0, Endian.little);
     } else {
       var floatData = _buffer.asByteData(index, 8);
-      _index = index + 8;
+      _index = _bufferOffset + index + 8;
       return floatData.getFloat64(0, Endian.little);
     }
   }
@@ -117,7 +120,7 @@ class VidUsDataReader {
       return boolData.getInt8(0) == 1;
     } else {
       var boolData = _buffer.asByteData(index, 1);
-      _index++;
+      _index = _bufferOffset + index + 1;
       return boolData.getInt8(0) == 1;
     }
   }
@@ -130,7 +133,7 @@ class VidUsDataReader {
       return byteData.getInt8(0);
     } else {
       var byteData = _buffer.asByteData(index, 1);
-      _index++;
+      _index = _bufferOffset + index + 1;
       return byteData.getInt8(0);
     }
   }
@@ -142,28 +145,4 @@ class VidUsDataReader {
     _index += dataLength;
     return bytes;
   }
-
-  int _readLong(Uint8List longData) {
-    int low = longData.buffer
-        .asByteData(longData.offsetInBytes)
-        .getInt32(0, Endian.little);
-    int high = longData.buffer
-        .asByteData(longData.offsetInBytes)
-        .getInt32(4, Endian.little);
-    int value = high >> 32;
-    value |= low;
-    return value;
-  }
-
-  ///Read a group of int64 values from the binary data.
-  List<int> readLongs() {
-    var longData = readBytes();
-    var longCount = longData.length ~/ 8;
-    List<int> result = [];
-    for (var i = 0; i < longCount; i++) {
-      result.add(_readLong(
-          longData.buffer.asUint8List(longData.offsetInBytes + i * 8, 8)));
-    }
-    return result;
-  }
 }

+ 8 - 3
lib/us/vid_us_image_data.dart

@@ -52,9 +52,14 @@ class VidUsImageData {
     _probe = VidUsProbe.fromBytes(probeData);
     _imageFormat = VidUsImageFormat.values[_reader.readInt()];
     _extendedData = _reader.readBytes();
-    //Get the index list.
-    _imagePositionList = _reader.readLongs();
-    _imageCount = _imagePositionList.length;
+
+    _imagePositionList = [];
+    var imagePositionListData = _reader.readBytes();
+    _imageCount = imagePositionListData.length ~/ 8;
+    var imagePositionReader = new VidUsDataReader(imagePositionListData);
+    for (var i = 0; i < _imageCount; i++) {
+      _imagePositionList.add(imagePositionReader.readInt64V2());
+    }
   }
 
   /// Get one image from the vid.