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
175pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight
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
    }
}