vid_us_image.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'dart:typed_data';
  2. import 'package:vid/us/vid_us_data_reader.dart';
  3. import 'package:vid/us/vid_us_data_writer.dart';
  4. import 'package:vid/us/vid_us_visual.dart';
  5. /// Used for update the image data.
  6. abstract class IImageDataContainer {
  7. abstract Uint8List imageData;
  8. }
  9. /// This class is used for updating the image data.
  10. class VidUsImageUpdater {
  11. final IImageDataContainer _imageDataContainer;
  12. VidUsImageUpdater(this._imageDataContainer);
  13. /// Update image data
  14. void update(Uint8List imageData) {
  15. _imageDataContainer.imageData = imageData;
  16. }
  17. }
  18. class VidUsImage implements IImageDataContainer {
  19. @override
  20. late Uint8List imageData;
  21. late int _index;
  22. late int _width;
  23. late int _height;
  24. late List<VidUsVisual> _visuals;
  25. /// Gets the index of this image.
  26. int get index => _index;
  27. /// Gets the width of this image data.
  28. int get width => _width;
  29. /// Gets the height of this image data.
  30. int get height => _height;
  31. /// Gets all visuals of this image.
  32. List<VidUsVisual> get visuals => _visuals;
  33. VidUsImage(int index, int width, int height, Uint8List data) {
  34. _visuals = [];
  35. _index = index;
  36. _width = width;
  37. _height = height;
  38. imageData = data;
  39. }
  40. /// VidUsImage image to binary data.
  41. Uint8List toBytes() {
  42. var writer = new VidUsDataWriter();
  43. writer.writeInt(_index);
  44. writer.writeByte(_visuals.length);
  45. _visuals.forEach((visual) {
  46. var visualData = visual.toBytes();
  47. writer.writeBytes(visualData);
  48. });
  49. writer.writeInt16(_width);
  50. writer.writeInt16(_height);
  51. writer.writeBytes(imageData);
  52. return writer.data;
  53. }
  54. /// Convert binary data to VidUsImage
  55. static VidUsImage fromBytes(Uint8List bytes) {
  56. var reader = new VidUsDataReader(bytes);
  57. var index = reader.readInt();
  58. var visualCount = reader.readByte();
  59. List<VidUsVisual> visuals = [];
  60. for (int i = 0; i < visualCount; i++) {
  61. var visual = VidUsVisual.fromBytes(reader.readBytes());
  62. visuals.add(visual);
  63. }
  64. var width = reader.readInt16();
  65. var height = reader.readInt16();
  66. var imageData = reader.readBytes();
  67. var result = new VidUsImage(index, width, height, imageData);
  68. visuals.forEach((visual) {
  69. result._visuals.add(visual);
  70. });
  71. return result;
  72. }
  73. }