<|start_filename|>LMCSHD/LMCSHD/App.xaml.cs<|end_filename|>
﻿using System.Windows;
using System;

namespace LMCSHD
{
    public partial class App : Application {}

    #region public structures
    //structures to exist in the LMCSHD namespace
    public struct Pixel
    {
        public byte R, G, B;
        public Pixel(byte r, byte g, byte b)
        {
            R = r; G = g; B = b;
        }

        public Int32 GetBPP24RGB_Int32()
        {
           return R << 16 | G << 8 | B;
        }
        public Int32 GetBPP16RGB_Int32()
        {
            return ((R & 0xF8) << 16) | ((G & 0xFC) << 8) | (B & 0xF8);
        }
        public Int32 GetBPP8RGB_Int32()
        {
           return ((R & 0xE0) << 16) | ((G & 0xE0) << 8) | (B & 0xC0);
        }
        public Int32 GetBPP8Grayscale_Int32()
        {
            byte color = (byte)((R + G + B) / 3);
            return color << 16 | color << 8 | color;
        }
        public Int32 GetBPP1Monochrome_Int32()
        {
            byte color = (byte)(((R + G + B) / 3) > 127 ? 255 : 0);
            return color << 16 | color << 8 | color;
        }
    }

    public struct PixelOrder
    {
        public enum Orientation { HZ, VT }
        public enum StartCorner { TL, TR, BL, BR }
        public enum NewLine { SC, SN }
    }

    public struct MatrixTitle
    {
        public int MatrixTitleDimensionX;
        public int MatrixTitleDimensionY;
        public string MatrixTitleColormode;

        public MatrixTitle(int x, int y, string cm)
        {
            MatrixTitleDimensionX = x;
            MatrixTitleDimensionY = y;
            MatrixTitleColormode = cm;
        }

        public string GetTitle()
        {
            return MatrixTitleDimensionX.ToString() + " x " + MatrixTitleDimensionY.ToString() + " | " + MatrixTitleColormode;
        }
    }
    #endregion
}


<|start_filename|>LMCSHD/LMCSHD/MatrixDimensions.xaml.cs<|end_filename|>
﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LMCSHD
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MatrixDimensions : INotifyPropertyChanged
    {
        public MatrixDimensions()
        {
            DataContext = this;
            InitializeComponent();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private int _width =  MatrixFrame.Width;
        private int _height = MatrixFrame.Height;

        public int MatrixWidth
        {
            get { return _width; }
            set
            {
                if(value != _width)
                {
                    _width = value;
                    OnPropertyChanged();
                }
            }
        }

        public int MatrixHeight
        {
            get { return _height; }
            set
            {
                if(value != _height)
                {
                    _height = value;
                    OnPropertyChanged();
                }
            }
        }

        private void Button_Accept_Click(object sender, RoutedEventArgs e)
        {
            MatrixFrame.SetDimensions(MatrixWidth, MatrixHeight);
            this.Close();
        }

        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}


<|start_filename|>LMCSHD/LMCSHD/ScreenRecorder.cs<|end_filename|>
﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;

namespace LMCSHD
{
    public static class ScreenRecorder
    {
        public static Rectangle CaptureRect { get; set; }
        public static bool doCapture { get; set; }
        public static bool doOutline { get; set; }
        public static System.Drawing.Drawing2D.InterpolationMode InterpMode { get; set; } = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        public delegate void Callback(Bitmap capturedFrame);
        public static void StartRecording(Callback pixelDataCallback)
        {
            doCapture = true;
            while (doCapture)
                pixelDataCallback(ScreenToBitmap());
        }
        public static Bitmap ScreenToBitmap()
        {
            IntPtr handle = IntPtr.Zero;
            IntPtr hdcSrc = GetDC(handle);
            IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, CaptureRect.Width, CaptureRect.Height);
            IntPtr hOld = SelectObject(hdcDest, hBitmap);
            BitBlt(hdcDest, 0, 0, CaptureRect.Width, CaptureRect.Height, hdcSrc, CaptureRect.X, CaptureRect.Y, CopyPixelOperation.SourceCopy);

            SelectObject(hdcDest, hOld);
            DeleteDC(hdcDest);
            ReleaseDC(handle, hdcSrc);
            Bitmap bitmap = Bitmap.FromHbitmap(hBitmap);

            DeleteObject(hBitmap);

            return bitmap;
        }

        public static void ShowOutline()
        {
            doOutline = true;
            while (doOutline)
            {
                IntPtr ptr = GetDC(IntPtr.Zero);
                using (Graphics g = Graphics.FromHdc(ptr))
                    g.DrawRectangle(new Pen(Color.DarkMagenta, 1), new Rectangle(CaptureRect.X - 1, CaptureRect.Y - 1, CaptureRect.Width + 1, CaptureRect.Height + 1));
                ReleaseDC(IntPtr.Zero, ptr);

                Thread.Sleep(6);
            }
        }
        public static void HideOutline()
        {
            InvalidateRect(IntPtr.Zero, IntPtr.Zero, false);
        }

        #region DLL Imports
        [DllImport("user32.dll")]
        static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
        [DllImport("user32.dll")]
        static extern bool ValidateRect(IntPtr hWnd, IntPtr lpRect);

        [DllImport("User32.dll")]
        public static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("user32.dll")]
        static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, CopyPixelOperation dwRop);

        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hDC);

        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        #endregion
    }
}


<|start_filename|>LMCSHD/LMCSHD/MatrixConnection.xaml.cs<|end_filename|>
﻿using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LMCSHD
{
    /// <summary>
    /// Interaction logic for MatrixConnection.xaml
    /// </summary>
    public partial class MatrixConnection : INotifyPropertyChanged
    {
        public MatrixConnection()
        {
            InitializeComponent();
            RefreshSerialPorts();
            SSColorModeList.SelectedIndex = (int)SerialManager.ColorMode;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        void RefreshSerialPorts()
        {
            string[] ports = SerialManager.GetPortNames();
            SSerialPortList.ItemsSource = ports;
            if (ports.Length > 0)
            {
                SSerialPortList.SelectedIndex = 0;
                SSerialConnect.IsEnabled = true;
            }
            else
            {
                SSerialConnect.IsEnabled = false;
            }
        }
        private void SSerialConnect_Click(object sender, RoutedEventArgs e)
        {
            switch (SSColorModeList.SelectedIndex)
            {
                case 0: SerialManager.ColorMode = SerialManager.CMode.BPP24RGB; break;
                case 1: SerialManager.ColorMode = SerialManager.CMode.BPP16RGB; break;
                case 2: SerialManager.ColorMode = SerialManager.CMode.BPP8RGB; break;
                case 3: SerialManager.ColorMode = SerialManager.CMode.BPP8Gray; break;
                case 4: SerialManager.ColorMode = SerialManager.CMode.BPP1Mono; break;
            }
            int[] matrixDef = null;

                matrixDef = SerialManager.Connect(SSerialPortList.SelectedValue.ToString(), int.Parse(SBaudRate.Text));

            if (matrixDef != null)
            {
                MatrixFrame.SetDimensions(matrixDef[0], matrixDef[1]);
               // ((MainWindow)Application.Current.MainWindow).SetMatrixDimensions(matrixDef[0], matrixDef[1]);
                Close();
            }
            else
            {
                MessageBox.Show("Cannot establish connection on: " + SSerialPortList.SelectedValue.ToString());
            }
        }
        private void SSerialRefreshPorts_Click(object sender, RoutedEventArgs e)
        {
            RefreshSerialPorts();
        }

        private static readonly Regex _regex = new Regex("[^0-9]+"); //regex that matches disallowed text
        private static bool IsTextAllowed(string text)
        {
            return !_regex.IsMatch(text);
        }
        private void SBaudRate_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !IsTextAllowed(e.Text);
        }
        private void SBaudRate_TextBoxPasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!IsTextAllowed(text))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            }
        }
    }
}


<|start_filename|>LMCSHD/LMCSHD/ExportWindow.xaml.cs<|end_filename|>
﻿using System;
using System.Windows;
using System.Windows.Forms;

namespace LMCSHD
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ExportWindow : Window
    {
        public ExportWindow()
        {
            InitializeComponent();
        }

        private void Button_Export_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                WriteFile(saveFileDialog.FileName);
                //   
            }
        }

        private void WriteFile(string path)
        {
            //System.Windows.Forms.MessageBox.Show(path);
            string[] formattedData = new string[MatrixFrame.Height];
            /*
            for (int i = 0; i < MatrixFrame.Height; i++)
            {
                for(int e = 0; e < MatrixFrame.Width; e++)
                {
                    formattedData[i] += "0x" + Convert.ToString(MatrixFrame.Frame[i * MatrixFrame.Width + e].R, 16) + ",";
                    formattedData[i] += "0x" + Convert.ToString(MatrixFrame.Frame[i * MatrixFrame.Width + e].G, 16) + ",";
                    formattedData[i] += "0x" + Convert.ToString(MatrixFrame.Frame[i * MatrixFrame.Width + e].B, 16) + ", ";
                }
                formattedData[i] += "\n";
            }

                        for (int i = 0; i < MatrixFrame.Height; i++)
            {
                for (int e = 0; e < MatrixFrame.Width; e++)
                {
                    formattedData[i] += "B" + Convert.ToString(MatrixFrame.Frame[i * MatrixFrame.Width + e].R, 2) + ",";
                    formattedData[i] += "B" + Convert.ToString(MatrixFrame.Frame[i * MatrixFrame.Width + e].G, 2) + ",";
                    formattedData[i] += "B" + Convert.ToString(MatrixFrame.Frame[i * MatrixFrame.Width + e].B, 2) + ", ";
                }
                formattedData[i] += "\n";
            }
            */

            string[] d = { BitConverter.ToString(MatrixFrame.GetOrderedSerialFrame()) };

            for (int i = 0; i < MatrixFrame.Height; i++)
            {
                for (int e = 0; e < MatrixFrame.Width; e++)
                {
                    formattedData[i] += "0x" + MatrixFrame.Frame[i * MatrixFrame.Width + e].R.ToString("X2") + ",";
                    formattedData[i] += "0x" + MatrixFrame.Frame[i * MatrixFrame.Width + e].G.ToString("X2") + ",";
                    formattedData[i] += "0x" + MatrixFrame.Frame[i * MatrixFrame.Width + e].B.ToString("X2") + ",";
                }
                formattedData[i] += "\n";
            }




            System.IO.File.WriteAllLines(path, formattedData);

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();
        }
    }
}
