aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Tools
diff options
context:
space:
mode:
authorMichael VanOverbeek <[email protected]>2017-05-21 13:05:31 +0000
committerMichael VanOverbeek <[email protected]>2017-05-21 13:05:31 +0000
commit68785f73f027b9dd4555b9f1ab7a22a1ca8794f8 (patch)
treef0c6dea13195bc516f90c2aa29cf85fa1488f6cf /ShiftOS.WinForms/Tools
parent76b54853ba726179f9fddb30c6f838991b7aa71a (diff)
parent9999324bd7751f536741c108322766421dae1a52 (diff)
downloadshiftos_thereturn-68785f73f027b9dd4555b9f1ab7a22a1ca8794f8.tar.gz
shiftos_thereturn-68785f73f027b9dd4555b9f1ab7a22a1ca8794f8.tar.bz2
shiftos_thereturn-68785f73f027b9dd4555b9f1ab7a22a1ca8794f8.zip
Merge branch 'master' of https://github.com/shiftos-game/ShiftOS
I should hire Ash Ketchum to do this for me. He'd do a better job.
Diffstat (limited to 'ShiftOS.WinForms/Tools')
-rw-r--r--ShiftOS.WinForms/Tools/ControlManager.cs343
-rw-r--r--ShiftOS.WinForms/Tools/DitheringEngine.cs425
2 files changed, 768 insertions, 0 deletions
diff --git a/ShiftOS.WinForms/Tools/ControlManager.cs b/ShiftOS.WinForms/Tools/ControlManager.cs
new file mode 100644
index 0000000..4f888ab
--- /dev/null
+++ b/ShiftOS.WinForms/Tools/ControlManager.cs
@@ -0,0 +1,343 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+using ShiftOS.Engine;
+using static ShiftOS.Engine.AppearanceManager;
+
+
+namespace ShiftOS.WinForms.Tools
+{
+ public static class ControlManager
+ {
+ [DllImport("user32.dll")]
+ public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
+
+ private const int WM_SETREDRAW = 11;
+
+ public static void SuspendDrawing(Control parent)
+ {
+ SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
+ }
+
+ public static void ResumeDrawing(Control parent)
+ {
+ SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
+ parent.Refresh();
+ }
+
+ public static void Close(this UserControl ctrl)
+ {
+ for (int i = 0; i < AppearanceManager.OpenForms.Count; i++)
+ {
+ if (OpenForms[i].ParentWindow == ctrl)
+ {
+ (OpenForms[i] as Form).Close();
+ return;
+ }
+ }
+ }
+
+
+ internal static Color ConvertColor(ConsoleColor cCol)
+ {
+ switch (cCol)
+ {
+ case ConsoleColor.Black:
+ return Color.Black;
+ case ConsoleColor.Gray:
+ return Color.Gray;
+ case ConsoleColor.DarkGray:
+ return Color.DarkGray;
+ case ConsoleColor.Blue:
+ return Color.Blue;
+ case ConsoleColor.Cyan:
+ return Color.Cyan;
+ case ConsoleColor.DarkBlue:
+ return Color.DarkBlue;
+ case ConsoleColor.DarkCyan:
+ return Color.DarkCyan;
+ case ConsoleColor.DarkGreen:
+ return Color.DarkGreen;
+ case ConsoleColor.DarkMagenta:
+ return Color.DarkMagenta;
+ case ConsoleColor.DarkRed:
+ return Color.DarkRed;
+ case ConsoleColor.DarkYellow:
+ return Color.YellowGreen;
+ case ConsoleColor.Yellow:
+ return Color.Yellow;
+ case ConsoleColor.Green:
+ return Color.Green;
+ case ConsoleColor.Magenta:
+ return Color.Magenta;
+ case ConsoleColor.Red:
+ return Color.Red;
+ case ConsoleColor.White:
+ return Color.White;
+ default:
+ return Color.Black;
+ }
+
+ }
+
+ public static void SetCursor(Control ctrl)
+ {
+#if STUPID
+ if (!(ctrl is WebBrowser))
+ {
+ var mouse = SkinEngine.GetImage("mouse");
+ if (mouse == null)
+ mouse = Properties.Resources.DefaultMouse;
+
+ var mBmp = new Bitmap(mouse);
+ mBmp.MakeTransparent(Color.FromArgb(1, 0, 1));
+ var gfx = Graphics.FromImage(mBmp);
+ var handle = mBmp.GetHicon();
+
+ var cursor = new Cursor(handle);
+ ctrl.Cursor = cursor;
+ }
+#endif
+ }
+
+ /// <summary>
+ /// Centers the control along its parent.
+ /// </summary>
+ /// <param name="ctrl">The control to center (this is an extension method - you can call it on a control as though it was a method in that control)</param>
+ public static void CenterParent(this Control ctrl)
+ {
+ ctrl.Location = new Point(
+ (ctrl.Parent.Width - ctrl.Width) / 2,
+ (ctrl.Parent.Height - ctrl.Height) / 2
+ );
+ }
+
+ public static void SetupControl(Control ctrl)
+ {
+ Desktop.InvokeOnWorkerThread(new Action(() =>
+ {
+ ctrl.SuspendLayout();
+ }));
+ if (!(ctrl is MenuStrip) && !(ctrl is ToolStrip) && !(ctrl is StatusStrip) && !(ctrl is ContextMenuStrip))
+ {
+ string tag = "";
+
+ try
+ {
+ if(ctrl.Tag != null)
+ tag = ctrl.Tag.ToString();
+ }
+ catch { }
+
+ if (!tag.Contains("keepbg"))
+ {
+ if (ctrl.BackColor != Control.DefaultBackColor)
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ ctrl.BackColor = SkinEngine.LoadedSkin.ControlColor;
+ });
+ }
+ }
+
+ if (!tag.Contains("keepfont"))
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ ctrl.ForeColor = SkinEngine.LoadedSkin.ControlTextColor;
+ ctrl.Font = SkinEngine.LoadedSkin.MainFont;
+ });
+ if (tag.Contains("header1"))
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ ctrl.Font = SkinEngine.LoadedSkin.HeaderFont;
+ });
+ }
+
+ if (tag.Contains("header2"))
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ ctrl.Font = SkinEngine.LoadedSkin.Header2Font;
+ });
+ }
+
+ if (tag.Contains("header3"))
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+
+ ctrl.Font = SkinEngine.LoadedSkin.Header3Font;
+ });
+ }
+ }
+ try
+ {
+ string ctrlText = Localization.Parse(ctrl.Text);
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ ctrl.Text = ctrlText;
+ });
+ }
+ catch
+ {
+
+ }
+
+ if(ctrl is Button)
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ Button b = ctrl as Button;
+ b.BackColor = SkinEngine.LoadedSkin.ButtonBackgroundColor;
+ b.BackgroundImage = SkinEngine.GetImage("buttonidle");
+ b.BackgroundImageLayout = SkinEngine.GetImageLayout("buttonidle");
+ b.FlatAppearance.BorderSize = SkinEngine.LoadedSkin.ButtonBorderWidth;
+ b.FlatAppearance.BorderColor = SkinEngine.LoadedSkin.ButtonForegroundColor;
+ b.ForeColor = SkinEngine.LoadedSkin.ButtonForegroundColor;
+ b.Font = SkinEngine.LoadedSkin.ButtonTextFont;
+
+ b.MouseEnter += (o, a) =>
+ {
+ b.BackColor = SkinEngine.LoadedSkin.ButtonHoverColor;
+ b.BackgroundImage = SkinEngine.GetImage("buttonhover");
+ b.BackgroundImageLayout = SkinEngine.GetImageLayout("buttonhover");
+ };
+ b.MouseLeave += (o, a) =>
+ {
+ b.BackColor = SkinEngine.LoadedSkin.ButtonBackgroundColor;
+ b.BackgroundImage = SkinEngine.GetImage("buttonidle");
+ b.BackgroundImageLayout = SkinEngine.GetImageLayout("buttonidle");
+ };
+ b.MouseUp += (o, a) =>
+ {
+ b.BackColor = SkinEngine.LoadedSkin.ButtonBackgroundColor;
+ b.BackgroundImage = SkinEngine.GetImage("buttonidle");
+ b.BackgroundImageLayout = SkinEngine.GetImageLayout("buttonidle");
+ };
+
+ b.MouseDown += (o, a) =>
+ {
+ b.BackColor = SkinEngine.LoadedSkin.ButtonPressedColor;
+ b.BackgroundImage = SkinEngine.GetImage("buttonpressed");
+ b.BackgroundImageLayout = SkinEngine.GetImageLayout("buttonpressed");
+
+ };
+ });
+ }
+
+ ctrl.KeyDown += (o, a) =>
+ {
+ if (a.Control && a.KeyCode == Keys.T)
+ {
+ a.SuppressKeyPress = true;
+
+
+ Engine.AppearanceManager.SetupWindow(new Applications.Terminal());
+ }
+
+ ShiftOS.Engine.Scripting.LuaInterpreter.RaiseEvent("on_key_down", a);
+ //a.Handled = true;
+ };
+ if (ctrl is Button)
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ (ctrl as Button).FlatStyle = FlatStyle.Flat;
+ });
+ }
+ else if (ctrl is WindowBorder)
+ {
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+ (ctrl as WindowBorder).Setup();
+ });
+ }
+ }
+ Desktop.InvokeOnWorkerThread(() =>
+ {
+
+ MakeDoubleBuffered(ctrl);
+ ctrl.ResumeLayout();
+ });
+ ControlSetup?.Invoke(ctrl);
+ }
+
+ public static event Action<Control> ControlSetup;
+
+ public static void MakeDoubleBuffered(Control c)
+ {
+ if (System.Windows.Forms.SystemInformation.TerminalServerSession)
+ return;
+
+ System.Reflection.PropertyInfo aProp =
+ typeof(System.Windows.Forms.Control).GetProperty(
+ "DoubleBuffered",
+ System.Reflection.BindingFlags.NonPublic |
+ System.Reflection.BindingFlags.Instance);
+
+ aProp.SetValue(c, true, null);
+
+ }
+
+ public static void SetupControls(Control frm, bool runInThread = true)
+ {
+ SetupControl(frm);
+ frm.Click += (o, a) =>
+ {
+ Desktop.HideAppLauncher();
+ };
+ ThreadStart ts = () =>
+ {
+ for (int i = 0; i < frm.Controls.Count; i++)
+ {
+ SetupControls(frm.Controls[i], false);
+ }
+
+ };
+
+ if (runInThread == true)
+ {
+ var t = new Thread(ts);
+ t.IsBackground = true;
+ t.Start();
+ }
+ else
+ {
+ ts?.Invoke();
+ }
+ }
+
+ }
+}
diff --git a/ShiftOS.WinForms/Tools/DitheringEngine.cs b/ShiftOS.WinForms/Tools/DitheringEngine.cs
new file mode 100644
index 0000000..d042a40
--- /dev/null
+++ b/ShiftOS.WinForms/Tools/DitheringEngine.cs
@@ -0,0 +1,425 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#define FLOYDSTEINBERG
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Drawing;
+using System.Threading;
+using ShiftOS.Engine;
+using System.Runtime.InteropServices;
+using System.IO;
+
+namespace ShiftOS.WinForms.Tools
+{
+ public static class DitheringEngine
+ {
+ public static Color GetColor(Color source)
+ {
+ if (Shiftorium.UpgradeInstalled("color_depth_24_bits"))
+ {
+ return Color.FromArgb(source.R, source.G, source.B); //get rid of the alpha channel.
+ }
+ else
+ {
+ if (Shiftorium.UpgradeInstalled("color_depth_16_bits"))
+ {
+ byte r = (byte)linear(source.R, 0, 0xFF, 0, 0x1F);
+ byte g = (byte)linear(source.G, 0, 0xFF, 0, 0x3F);
+ byte b = (byte)linear(source.B, 0, 0xFF, 0, 0x1F);
+
+ return Color.FromArgb(r, g, b);
+ }
+ else
+ {
+ int gray = (source.R + source.G + source.B) / 3;
+
+ if (Shiftorium.UpgradeInstalled("color_depth_8_bits"))
+ return Color.FromArgb(gray, gray, gray);
+ else
+ {
+ if (Shiftorium.UpgradeInstalled("color_depth_6_bits"))
+ {
+ int gray6 = (int)linear(gray, 0, 0xFF, 0, 0x3F) * 3;
+
+
+
+ return Color.FromArgb(gray6, gray6, gray6);
+ }
+ else
+ {
+ if (Shiftorium.UpgradeInstalled("color_depth_4_bits"))
+ {
+ int gray4 = (int)linear(linear(gray, 0, 0xFF, 0, 0xF), 0, 0xF, 0, 0xFF) * 0xF;
+ return Color.FromArgb(gray4, gray4, gray4);
+ }
+ else
+ {
+ if (Shiftorium.UpgradeInstalled("color_depth_2_bits"))
+ {
+ int gray2 = (int)linear(linear(gray, 0, 0xFF, 0, 0x3), 0, 0x3, 0, 0xFF) * 63;
+ return Color.FromArgb(gray2, gray2, gray2);
+ }
+ else {
+ if (gray >= 127)
+ {
+ return Color.Black;
+ }
+ else
+ {
+ return Color.White;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ public static Image DitherColor(Color source, int width, int height)
+ {
+ var bmp = new Bitmap(width + 1, height + 1);
+ var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
+ byte[] rgb = new byte[Math.Abs(data.Stride) * data.Height];
+ Marshal.Copy(data.Scan0, rgb, 0, rgb.Length);
+ for (int i = 0; i < rgb.Length - 3; i += 3)
+ {
+ rgb[i] = source.R;
+ rgb[i + 1] = source.G;
+ rgb[i + 2] = source.B;
+ }
+ Marshal.Copy(rgb, 0, data.Scan0, rgb.Length);
+ bmp.UnlockBits(data);
+ return DitherImage(bmp);
+
+ }
+
+ static private double linear(double x, double x0, double x1, double y0, double y1)
+ {
+ if ((x1 - x0) == 0)
+ {
+ return (y0 + y1) / 2;
+ }
+ return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
+ }
+
+#if NODITHER
+ public static void DitherImage(Image source, Action<Image> result)
+ {
+ Desktop.InvokeOnWorkerThread(new Action(() =>
+ {
+ result?.Invoke(source);
+ }));
+ }
+#endif
+
+#if BINARIZE
+ public static void DitherImage(Image source, Action<Image> result)
+ {
+ if (source == null)
+ {
+ result?.Invoke(source);
+ return;
+ }
+
+
+ var t = new Thread(new ThreadStart(() =>
+ {
+ var bmp = new Bitmap(source.Width, source.Height);
+ var sourceBmp = (Bitmap)source;
+ int error = 0;
+ for (int y = 0; y < bmp.Height; y++)
+ {
+ for (int x = 0; x < bmp.Width; x++)
+ {
+
+ Color c = sourceBmp.GetPixel(x, y);
+ int gray = ((c.R + c.G + c.B) / 3);
+ if (gray >= 127)
+ {
+ error = gray - 255;
+ bmp.SetPixel(x, y, Color.White);
+ }
+ else
+ {
+ error = gray;
+ bmp.SetPixel(x, y, Color.Black);
+ }
+
+
+ }
+ }
+ Desktop.InvokeOnWorkerThread(new Action(() => { result?.Invoke(bmp); }));
+ }));
+ t.IsBackground = true;
+ t.Start();
+
+ }
+#endif
+
+#if DITHER1D
+ public static void DitherImage(Image source, Action<Image> result)
+ {
+ if (source == null)
+ {
+ result?.Invoke(source);
+ return;
+ }
+
+
+ var t = new Thread(new ThreadStart(() =>
+ {
+ var bmp = new Bitmap(source.Width, source.Height);
+ var sourceBmp = (Bitmap)source;
+ int error = 0;
+ for (int y = 0; y < bmp.Height; y++)
+ {
+ for (int x = 0; x < bmp.Width; x++)
+ {
+
+ Color c = sourceBmp.GetPixel(x, y);
+ int gray = ((c.R + c.G + c.B) / 3) + error;
+ if (gray >= 127)
+ {
+ error = gray - 255;
+ bmp.SetPixel(x, y, Color.White);
+ }
+ else
+ {
+ error = gray;
+ bmp.SetPixel(x, y, Color.Black);
+ }
+
+
+ }
+ }
+ Desktop.InvokeOnWorkerThread(new Action(() => { result?.Invoke(bmp); }));
+ }));
+ t.IsBackground = true;
+ t.Start();
+ }
+#endif
+
+ public static int GetClosestColor(int gray, bool eightBits, bool sixBits, bool fourBits, bool twoBits)
+ {
+ int newgray = gray;
+ if (!eightBits)
+ {
+ if (sixBits)
+ {
+ newgray = gray >> 2;
+ }
+ else
+ {
+ if (fourBits)
+ {
+ newgray = (int)linear(gray, 0, 255, 0, 15) * 4;
+ }
+ else
+ {
+ if (twoBits)
+ {
+ if (gray > 127 + 63)
+ {
+ newgray = 255;
+ }
+ else if (gray > 127)
+ newgray = 127 + 63;
+ else if (gray > 63)
+ {
+ newgray = 127;
+ }
+ else if (gray > 0)
+ newgray = 63;
+ else
+ newgray = 0;
+ }
+ else
+ {
+ if (gray > 127)
+ newgray = 255;
+ else
+ newgray = 0;
+ }
+ }
+ }
+ }
+ return newgray;
+ }
+
+#if FLOYDSTEINBERG
+ public static Image DitherImage(Image source)
+ {
+ var bmp = new Bitmap(source.Width, source.Height);
+ var sourceBmp = (Bitmap)source;
+ var sourceLck = sourceBmp.LockBits(new Rectangle(0, 0, sourceBmp.Width, sourceBmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
+ var destLck = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
+ int error_r = 0;
+
+
+ int sourceBytes = Math.Abs(sourceLck.Stride) * sourceLck.Height;
+ int destBytes = Math.Abs(destLck.Stride) * destLck.Height;
+
+ IntPtr sourcePtr = sourceLck.Scan0;
+ IntPtr destPtr = destLck.Scan0;
+
+ byte[] destArr = new byte[destBytes];
+ byte[] sourceArr = new byte[sourceBytes];
+
+ byte[] grays = new byte[destBytes];
+
+ Marshal.Copy(sourcePtr, sourceArr, 0, sourceBytes);
+ Marshal.Copy(destPtr, destArr, 0, destBytes);
+
+ int width = Math.Abs(destLck.Stride);
+ int height = destLck.Height;
+
+ bool sixteenBits = Shiftorium.UpgradeInstalled("color_depth_16_bits");
+ bool twoBits = Shiftorium.UpgradeInstalled("color_depth_2_bits");
+ bool sixBits = Shiftorium.UpgradeInstalled("color_depth_6_bits");
+ bool fourBits = Shiftorium.UpgradeInstalled("color_depth_4_bits");
+ bool eightBits = Shiftorium.UpgradeInstalled("color_depth_8_bits");
+ bool color_depth_floydsteinberg = Shiftorium.UpgradeInstalled("color_depth_floyd-steinberg_dithering");
+ bool dithering = Shiftorium.UpgradeInstalled("color_depth_dithering");
+ bool twentyfourbits = Shiftorium.UpgradeInstalled("color_depth_24_bits");
+ if (twentyfourbits)
+ {
+ sourceArr.CopyTo(destArr, 0);
+ }
+ else
+ {
+
+ if (!sixteenBits)
+ {
+ if (dithering == true)
+ {
+ if (false == true)
+ {
+
+ }
+ else
+ {
+ int error = 0;
+ for (int i = 0; i < destArr.Length; i += 3)
+ {
+ byte r = sourceArr[i];
+ byte g = sourceArr[i + 1];
+ byte b = sourceArr[i + 2];
+
+ if (SkinEngine.LoadedSkin.SystemKey == Color.FromArgb(r, g, b))
+ {
+ destArr[i] = r;
+ destArr[i + 1] = g;
+ destArr[i + 2] = b;
+ continue;
+ }
+
+
+ int gray = (((r + g + b) / 3) + error);
+ int newgray = gray;
+ newgray = GetClosestColor(gray, eightBits, sixBits, fourBits, twoBits);
+ if (newgray > 255)
+ newgray = 255;
+ if (newgray < 0)
+ newgray = 0;
+ error = gray - newgray;
+ destArr[i] = (byte)newgray;
+ destArr[i + 1] = (byte)newgray;
+ destArr[i + 2] = (byte)newgray;
+
+ }
+ }
+ }
+
+ else
+ {
+ for (int i = 0; i < sourceArr.Length; i += 3)
+ {
+ byte r = sourceArr[i];
+ byte g = sourceArr[i + 1];
+ byte b = sourceArr[i + 2];
+ if (SkinEngine.LoadedSkin.SystemKey == Color.FromArgb(r, g, b))
+ {
+ destArr[i] = r;
+ destArr[i + 1] = g;
+ destArr[i + 2] = b;
+ continue;
+ }
+
+ int gray = (r + g + b) / 3;
+ int newgray = GetClosestColor(gray, eightBits, sixBits, fourBits, twoBits);
+ destArr[i] = (byte)newgray;
+ destArr[i + 1] = (byte)newgray;
+ destArr[i + 2] = (byte)newgray;
+
+
+
+ }
+ }
+ }
+ }
+
+ Marshal.Copy(destArr, 0, destPtr, destBytes);
+
+
+ bmp.UnlockBits(destLck);
+
+
+
+ return bmp;
+ }
+#endif
+
+ private static int getIndexFromXY(int x, int y, int width)
+ {
+ return (width * y) + x;
+ }
+ }
+
+ public class DitheringSkinPostProcessor : ISkinPostProcessor
+ {
+ public byte[] ProcessImage(byte[] original)
+ {
+ try
+ {
+ var img = SkinEngine.ImageFromBinary(original);
+ var dithered = DitheringEngine.DitherImage(img);
+ using (var mstr = new MemoryStream())
+ {
+ dithered.Save(mstr, System.Drawing.Imaging.ImageFormat.Bmp);
+ return mstr.ToArray();
+ }
+ }
+ catch
+ {
+ return original;
+ }
+ }
+ }
+}