VinnoImage.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using fis.Vid.Visuals;
  4. namespace fis.Vid
  5. {
  6. /// <summary>
  7. /// Used for update the image data.
  8. /// </summary>
  9. internal interface IImageDataContainer
  10. {
  11. byte[] ImageData { get; set; }
  12. }
  13. /// <summary>
  14. /// This class is used for updating the image data.
  15. /// </summary>
  16. public class VinnoImageUpdater
  17. {
  18. private readonly IImageDataContainer _imageDataContainer;
  19. public VinnoImageUpdater(VinnoImage image)
  20. {
  21. _imageDataContainer = image;
  22. }
  23. /// <summary>
  24. /// Update VinnoImage's image data
  25. /// </summary>
  26. /// <param name="imageData">The image data to be updated.</param>
  27. public void Update(byte[] imageData)
  28. {
  29. _imageDataContainer.ImageData = imageData;
  30. }
  31. }
  32. public class VinnoImage : IImageDataContainer
  33. {
  34. /// <summary>
  35. /// Gets the index of this image.
  36. /// </summary>
  37. public int Index { get; }
  38. /// <summary>
  39. /// Gets the width of this image data.
  40. /// </summary>
  41. public int Width { get; }
  42. /// <summary>
  43. /// Gets the height of this image data.
  44. /// </summary>
  45. public int Height { get; }
  46. /// <summary>
  47. /// Gets the image data of this image.
  48. /// </summary>
  49. public byte[] ImageData { get; private set; }
  50. /// <summary>
  51. /// Gets all visuals of this image.
  52. /// </summary>
  53. public IList<VinnoVisual> Visuals { get; }
  54. /// <summary>
  55. /// Implement the interface for update the image data.
  56. /// </summary>
  57. byte[] IImageDataContainer.ImageData
  58. {
  59. get => ImageData;
  60. set => ImageData = value;
  61. }
  62. public VinnoImage(int index, int width, int height, byte[] imageData)
  63. {
  64. Visuals = new List<VinnoVisual>();
  65. Index = index;
  66. Width = width;
  67. Height = height;
  68. ImageData = imageData;
  69. }
  70. /// <summary>
  71. /// Convert image to bytes.
  72. /// </summary>
  73. /// <returns>The converted bytes.</returns>
  74. public byte[] ToBytes()
  75. {
  76. byte[] result = new byte[0];
  77. using (var stream = new MemoryStream())
  78. {
  79. var writer = new VinnoStreamWriter(stream);
  80. if (writer != null)
  81. {
  82. writer.WriteInt(Index);
  83. writer.WriteByte((byte)Visuals.Count);
  84. foreach (var visual in Visuals)
  85. {
  86. var buffer = visual?.ToBytes();
  87. if (buffer != null)
  88. {
  89. writer.WriteBytes(buffer);
  90. }
  91. }
  92. writer.WriteShort((short)Width);
  93. writer.WriteShort((short)Height);
  94. writer.WriteBytes(ImageData);
  95. result = stream.ToArray();
  96. }
  97. }
  98. return result;
  99. }
  100. /// <summary>
  101. /// Convert bytes to a <see cref="VinnoImage"/>
  102. /// </summary>
  103. /// <param name="bytes">The bytes to be converted.</param>
  104. /// <returns>The converted <see cref="VinnoImage"/></returns>
  105. public static VinnoImage FromBytes(byte[] bytes)
  106. {
  107. VinnoImage result;
  108. using (var stream = new MemoryStream(bytes))
  109. {
  110. stream.Position = 0;
  111. var reader = new VinnoStreamReader(stream);
  112. var index = reader.ReadInt();
  113. var visualCount = reader.ReadByte();
  114. var visuals = new List<VinnoVisual>();
  115. for (int i = 0; i < visualCount; i++)
  116. {
  117. var visual = VinnoVisual.FromBytes(reader.ReadBytes());
  118. visuals.Add(visual);
  119. }
  120. var widht = reader.ReadShort();
  121. var height = reader.ReadShort();
  122. var imageData = reader.ReadBytes();
  123. result = new VinnoImage(index, widht, height, imageData);
  124. foreach (var visual in visuals)
  125. {
  126. result.Visuals.Add(visual);
  127. }
  128. }
  129. return result;
  130. }
  131. }
  132. }