123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import 'dart:typed_data';
- import 'package:vid/us/vid_us_data_reader.dart';
- import 'package:vid/us/vid_us_data_writer.dart';
- import 'package:vid/us/vid_us_visual.dart';
- /// Used for update the image data.
- abstract class IImageDataContainer {
- abstract Uint8List imageData;
- }
- /// This class is used for updating the image data.
- class VidUsImageUpdater {
- final IImageDataContainer _imageDataContainer;
- VidUsImageUpdater(this._imageDataContainer);
- /// Update image data
- void update(Uint8List imageData) {
- _imageDataContainer.imageData = imageData;
- }
- }
- class VidUsImage implements IImageDataContainer {
- @override
- late Uint8List imageData;
- late int _index;
- late int _width;
- late int _height;
- late List<VidUsVisual> _visuals;
- /// Gets the index of this image.
- int get index => _index;
- /// Gets the width of this image data.
- int get width => _width;
- /// Gets the height of this image data.
- int get height => _height;
- /// Gets all visuals of this image.
- List<VidUsVisual> get visuals => _visuals;
- VidUsImage(int index, int width, int height, Uint8List data) {
- _visuals = [];
- _index = index;
- _width = width;
- _height = height;
- imageData = data;
- }
- /// VidUsImage image to binary data.
- Uint8List toBytes() {
- var writer = new VidUsDataWriter();
- writer.writeInt(_index);
- writer.writeByte(_visuals.length);
- _visuals.forEach((visual) {
- var visualData = visual.toBytes();
- writer.writeBytes(visualData);
- });
- writer.writeInt16(_width);
- writer.writeInt16(_height);
- writer.writeBytes(imageData);
- return writer.data;
- }
- /// Convert binary data to VidUsImage
- static VidUsImage fromBytes(Uint8List bytes) {
- var reader = new VidUsDataReader(bytes);
- var index = reader.readInt();
- var visualCount = reader.readByte();
- List<VidUsVisual> visuals = [];
- for (int i = 0; i < visualCount; i++) {
- var visual = VidUsVisual.fromBytes(reader.readBytes());
- visuals.add(visual);
- }
- var width = reader.readInt16();
- var height = reader.readInt16();
- var imageData = reader.readBytes();
- var result = new VidUsImage(index, width, height, imageData);
- visuals.forEach((visual) {
- result._visuals.add(visual);
- });
- return result;
- }
- }
|