using System.Collections.Generic;
using System.IO;
using fis.Vid.Visuals;
namespace fis.Vid
{
///
/// Used for update the image data.
///
internal interface IImageDataContainer
{
byte[] ImageData { get; set; }
}
///
/// This class is used for updating the image data.
///
public class VinnoImageUpdater
{
private readonly IImageDataContainer _imageDataContainer;
public VinnoImageUpdater(VinnoImage image)
{
_imageDataContainer = image;
}
///
/// Update VinnoImage's image data
///
/// The image data to be updated.
public void Update(byte[] imageData)
{
_imageDataContainer.ImageData = imageData;
}
}
public class VinnoImage : IImageDataContainer
{
///
/// Gets the index of this image.
///
public int Index { get; }
///
/// Gets the width of this image data.
///
public int Width { get; }
///
/// Gets the height of this image data.
///
public int Height { get; }
///
/// Gets the image data of this image.
///
public byte[] ImageData { get; private set; }
///
/// Gets all visuals of this image.
///
public IList Visuals { get; }
///
/// Implement the interface for update the image data.
///
byte[] IImageDataContainer.ImageData
{
get => ImageData;
set => ImageData = value;
}
public VinnoImage(int index, int width, int height, byte[] imageData)
{
Visuals = new List();
Index = index;
Width = width;
Height = height;
ImageData = imageData;
}
///
/// Convert image to bytes.
///
/// The converted bytes.
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;
}
///
/// Convert bytes to a
///
/// The bytes to be converted.
/// The converted
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();
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;
}
}
}