123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System.Collections.Generic;
- using System.IO;
- using fis.Vid.Visuals;
- namespace fis.Vid
- {
-
- /// <summary>
- /// Used for update the image data.
- /// </summary>
- internal interface IImageDataContainer
- {
- byte[] ImageData { get; set; }
- }
- /// <summary>
- /// This class is used for updating the image data.
- /// </summary>
- public class VinnoImageUpdater
- {
- private readonly IImageDataContainer _imageDataContainer;
- public VinnoImageUpdater(VinnoImage image)
- {
- _imageDataContainer = image;
- }
- /// <summary>
- /// Update VinnoImage's image data
- /// </summary>
- /// <param name="imageData">The image data to be updated.</param>
- public void Update(byte[] imageData)
- {
- _imageDataContainer.ImageData = imageData;
- }
- }
- public class VinnoImage : IImageDataContainer
- {
- /// <summary>
- /// Gets the index of this image.
- /// </summary>
- public int Index { get; }
- /// <summary>
- /// Gets the width of this image data.
- /// </summary>
- public int Width { get; }
- /// <summary>
- /// Gets the height of this image data.
- /// </summary>
- public int Height { get; }
- /// <summary>
- /// Gets the image data of this image.
- /// </summary>
- public byte[] ImageData { get; private set; }
- /// <summary>
- /// Gets all visuals of this image.
- /// </summary>
- public IList<VinnoVisual> Visuals { get; }
- /// <summary>
- /// Implement the interface for update the image data.
- /// </summary>
- byte[] IImageDataContainer.ImageData
- {
- get => ImageData;
- set => ImageData = value;
- }
- public VinnoImage(int index, int width, int height, byte[] imageData)
- {
- Visuals = new List<VinnoVisual>();
- Index = index;
- Width = width;
- Height = height;
- ImageData = imageData;
- }
- /// <summary>
- /// Convert image to bytes.
- /// </summary>
- /// <returns>The converted bytes.</returns>
- public byte[] ToBytes()
- {
- byte[] result = new byte[0];
- using (var stream = new MemoryStream())
- {
- var writer = new VinnoStreamWriter(stream);
- if (writer != null)
- {
- writer.WriteInt(Index);
- writer.WriteByte((byte)Visuals.Count);
- foreach (var visual in Visuals)
- {
- var buffer = visual?.ToBytes();
- if (buffer != null)
- {
- writer.WriteBytes(buffer);
- }
- }
- writer.WriteShort((short)Width);
- writer.WriteShort((short)Height);
- writer.WriteBytes(ImageData);
- result = stream.ToArray();
- }
- }
- return result;
- }
- /// <summary>
- /// Convert bytes to a <see cref="VinnoImage"/>
- /// </summary>
- /// <param name="bytes">The bytes to be converted.</param>
- /// <returns>The converted <see cref="VinnoImage"/></returns>
- public static VinnoImage FromBytes(byte[] bytes)
- {
- VinnoImage result;
- using (var stream = new MemoryStream(bytes))
- {
- stream.Position = 0;
- var reader = new VinnoStreamReader(stream);
- var index = reader.ReadInt();
- var visualCount = reader.ReadByte();
- var visuals = new List<VinnoVisual>();
- for (int i = 0; i < visualCount; i++)
- {
- var visual = VinnoVisual.FromBytes(reader.ReadBytes());
- visuals.Add(visual);
- }
- var widht = reader.ReadShort();
- var height = reader.ReadShort();
- var imageData = reader.ReadBytes();
- result = new VinnoImage(index, widht, height, imageData);
- foreach (var visual in visuals)
- {
- result.Visuals.Add(visual);
- }
- }
- return result;
- }
- }
- }
|