aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.RemoteDesktop/Network/RemoteDesktopCommand.cs
blob: 8283770adbab1d2dad2dc82730301805e0ca684b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.RemoteDesktop.Network
{
    public enum RemoteDesktopCommand
    {
        HideAndOpenShell,
        RestartApplication,
    }
}
pace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.RemoteDesktop.Frames;

namespace Tango.RemoteDesktop.Comparers
{
    /// <summary>
    /// Represents a raster <see cref="IBitmapComparer{TFrame}"/> comparer which will compare and return a <see cref="RasterFrame"/> difference.
    /// </summary>
    /// <seealso cref="Tango.RemoteDesktop.IBitmapComparer{Tango.RemoteDesktop.Frames.RasterFrame}" />
    public class RasterBitmapComparer : IBitmapComparer<RasterFrame>
    {
        /// <summary>
        /// When max number of differences reached, the comparer will immediately return the current difference frame.
        /// This should enforce the screen capture engine to report 'No Difference Available'.
        /// </summary>
        public long? MaxDifferencesThrow { get; set; }

        /// <summary>
        /// Creates the difference as <see cref="RasterFrame"/>.
        /// </summary>
        /// <param name="previousBitmap">The previous bitmap.</param>
        /// <param name="currentBitmap">The current bitmap.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">
        /// Cannot compare image. They are the same instance
        /// or
        /// Cannot compare image of different size.
        /// </exception>
        unsafe public BitmapComparerResult<RasterFrame> CreateDifference(Bitmap previousBitmap, Bitmap currentBitmap)
        {
            if (previousBitmap == null | currentBitmap == null)
                throw new InvalidOperationException("Cannot compare image. They are the same instance");

            if (previousBitmap.Height != currentBitmap.Height || previousBitmap.Width != currentBitmap.Width)
                throw new InvalidOperationException("Cannot compare image of different size.");

            uint count = 0;

            Color matchColor = Color.Transparent;

            Bitmap diffImage = currentBitmap.Clone() as Bitmap;

            int height = previousBitmap.Height;
            int width = previousBitmap.Width;

            BitmapData data1 = previousBitmap.LockBits(new Rectangle(0, 0, width, height),
                                               ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            BitmapData data2 = currentBitmap.LockBits(new Rectangle(0, 0, width, height),
                                               ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            BitmapData diffData = diffImage.LockBits(new Rectangle(0, 0, width, height),
                                                   ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            byte* data1Ptr = (byte*)data1.Scan0;
            byte* data2Ptr = (byte*)data2.Scan0;
            byte* diffPtr = (byte*)diffData.Scan0;

            byte[] swapColor = new byte[4];
            swapColor[0] = matchColor.B;
            swapColor[1] = matchColor.G;
            swapColor[2] = matchColor.R;
            swapColor[3] = matchColor.A;

            int rowPadding = data1.Stride - (previousBitmap.Width * 4);

            // iterate over height (rows)
            for (int i = 0; i < height; i++)
            {
                // iterate over width (columns)
                for (int j = 0; j < width; j++)
                {
                    int same = 0;

                    byte[] tmp = new byte[4];

                    // compare pixels and copy new values into temporary array
                    for (int x = 0; x < 4; x++)
                    {
                        tmp[x] = data2Ptr[0];
                        if (data1Ptr[0] == data2Ptr[0])
                        {
                            same++;
                        }
                        data1Ptr++; // advance image1 ptr
                        data2Ptr++; // advance image2 ptr
                    }

                    // swap color or add new values
                    for (int x = 0; x < 4; x++)
                    {
                        diffPtr[0] = (same == 4) ? swapColor[x] : tmp[x];
                        diffPtr++; // advance diff image ptr
                    }

                    if (same != 4)
                    {
                        count++;

                        if (MaxDifferencesThrow != null && count > MaxDifferencesThrow.Value)
                        {
                            previousBitmap.UnlockBits(data1);
                            currentBitmap.UnlockBits(data2);
                            diffImage.UnlockBits(diffData);
                            diffImage.Dispose();
                            throw new MaxDifferencesReachedException();
                        }
                    }
                }

                // at the end of each column, skip extra padding
                if (rowPadding > 0)
                {
                    data1Ptr += rowPadding;
                    data2Ptr += rowPadding;
                    diffPtr += rowPadding;
                }
            }

            previousBitmap.UnlockBits(data1);
            currentBitmap.UnlockBits(data2);
            diffImage.UnlockBits(diffData);

            return new BitmapComparerResult<RasterFrame>()
            {
                Frame = new RasterFrame(diffImage),
                DifferenceCount = count
            };
        }
    }
}