aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/Frames/VectorFrame.cs
blob: 89287ad4287cc36cac91bce031d4d1e28d3a7075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.RemoteDesktop.Utils;

namespace Tango.RemoteDesktop.Frames
{
    /// <summary>
    /// Represents a vector frame encapsulating a set of <see cref="VectorFramePixel"/>.
    /// </summary>
    /// <seealso cref="Tango.RemoteDesktop.Frame" />
    public class VectorFrame : Frame
    {
        private int _width;
        /// <summary>
        /// Gets the frame width.
        /// </summary>
        public override int Width
        {
            get { return _width; }
        }

        private int _height;
        /// <summary>
        /// Gets the frame height.
        /// </summary>
        public override int Height
        {
            get { return _height; }
        }

        /// <summary>
        /// Gets or sets the colors dictionary.
        /// </summary>
        public Dictionary<int, VectorFrameColor> Colors { get; set; }

        /// <summary>
        /// Gets the pixels.
        /// </summary>
        /// <value>
        /// The pixels.
        /// </value>
        public List<VectorFramePixel> Pixels { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="VectorFrame"/> class.
        /// </summary>
        /// <param name="width">The frame width.</param>
        /// <param name="height">The frame height.</param>
        public VectorFrame(int width, int height)
        {
            _width = width;
            _height = height;
            Pixels = new List<VectorFramePixel>();
            Colors = new Dictionary<int, VectorFrameColor>();
        }

        /// <summary>
        /// Returns a GDI bitmap representing the frame.
        /// </summary>
        /// <returns></returns>
        public override Bitmap ToBitmap()
        {
            DirectBitmap directBitmap = new DirectBitmap(Width, Height);
            foreach (var pixel in Pixels)
            {
                directBitmap.SetPixel((int)pixel.X, (int)pixel.Y, pixel.Color.ToColor());
            }

            directBitmap.Dispose();
            return directBitmap.Bitmap;
        }

        /// <summary>
        /// Applies this frame onto an existing bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        public override void Apply(Bitmap bitmap)
        {
            using (FastBitmap fast = bitmap.FastLock())
            {
                foreach (var pixel in Pixels)
                {
                    fast.SetPixel((int)pixel.X, (int)pixel.Y, pixel.Color.ToColor());
                }
            }
        }

        /// <summary>
        /// Returns a byte array containing the binary serialized version of this frame.
        /// </summary>
        /// <returns></returns>
        public byte[] Serialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write((UInt16)Width);
                    writer.Write((UInt16)Height);
                    writer.Write((UInt16)Colors.Count);

                    if (Colors.Count > UInt16.MaxValue)
                    {
                        throw new InvalidOperationException("Number of color exceeded the maximum colors allowed.");
                    }

                    foreach (var color in Colors)
                    {
                        writer.Write(color.Value.R);
                        writer.Write(color.Value.G);
                        writer.Write(color.Value.B);
                    }

                    foreach (var pixel in Pixels)
                    {
                        writer.Write((UInt16)pixel.X);
                        writer.Write((UInt16)pixel.ColorIndex);
                    }

                    ms.Position = 0;
                    return ms.ToArray();
                }
            }
        }

        /// <summary>
        /// Calculates the size of <see cref="Serialize"/> byte output.
        /// </summary>
        /// <returns></returns>
        public int CalculateSize()
        {
            int size = 0;

            size += sizeof(int) * 3; //Width, Height, Count
            size += sizeof(ushort) * 2 * Pixels.Count; //X, Y
            size += sizeof(byte) * 3 * Pixels.Count; //RGB

            return size;
        }

        /// <summary>
        /// Creates a new instance of <see cref="VectorFrame"/> from the specified serialized byte array.
        /// </summary>
        /// <param name="data">The byte array.</param>
        /// <returns></returns>
        public static VectorFrame Deserialize(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader reader = new BinaryReader(ms))
                {
                    ms.Position = 0;
                    var vector = new VectorFrame(reader.ReadUInt16(), reader.ReadUInt16());

                    int colorCount = reader.ReadUInt16(); //Number of colors in color table.

                    for (int i = 0; i < colorCount; i++)
                    {
                        VectorFrameColor color = new VectorFrameColor()
                        {
                            R = reader.ReadByte(),
                            G = reader.ReadByte(),
                            B = reader.ReadByte(),
                            Index = i,
                        };

                        vector.Colors.Add(color.ToInt32(), color);
                    }

                    for (int i = 0; i < vector.Height; i++)
                    {
                        VectorFramePixel pixel = new VectorFramePixel();
                        pixel.X = reader.ReadUInt16();
                        pixel.Y = i;
                        pixel.ColorIndex = reader.ReadUInt16();
                        pixel.Color = vector.Colors.ElementAt(pixel.ColorIndex).Value;
                        vector.Pixels.Add(pixel);
                    }

                    return vector;
                }
            }
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        public override void Dispose()
        {
            Pixels.Clear();
            Pixels = null;
        }

        #region Color Helpers

        /// <summary>
        /// Converts the specified color to integer.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns></returns>
        public static int ColorToInteger(byte r, byte g, byte b)
        {
            return (int)((255 << 24) | (r << 16) | (g << 8) | (b << 0));
        }

        /// <summary>
        /// Converts the specified integer to color.
        /// </summary>
        /// <param name="integer">The integer.</param>
        /// <returns></returns>
        public static Color IntegerToColor(int integer)
        {
            byte a = (byte)(integer >> 24);
            byte r = (byte)(integer >> 16);
            byte g = (byte)(integer >> 8);
            byte b = (byte)(integer >> 0);
            return Color.FromArgb(a, r, g, b);
        }

        #endregion
    }
}