From b7c48b7c9bf3c3a08d03c0c9ff2b0561c8d735db Mon Sep 17 00:00:00 2001 From: Logan Saso Date: Wed, 10 Feb 2016 21:25:06 -0800 Subject: [PATCH] Upgraded bitmap resize method --- Artemis/Artemis/Artemis.csproj | 947 +++++++++--------- .../Artemis/KeyboardProviders/Corsair/K95.cs | 18 +- Artemis/Artemis/Utilities/ImageUtilities.cs | 135 +++ 3 files changed, 616 insertions(+), 484 deletions(-) create mode 100644 Artemis/Artemis/Utilities/ImageUtilities.cs diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index d371857ed..dfc6cda5d 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -1,480 +1,481 @@ - - - - - Debug - AnyCPU - {ED9997A2-E54C-4E9F-9350-62BE672C3ABE} - WinExe - Properties - Artemis - Artemis - v4.5.2 - 512 - {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - 4 - true - - - - - - - - - false - C:\Users\spoin\Desktop\Artemis builds\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.0 - false - true - true - - - - - x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - EAC088BE27A2DE790AE6F37A020409F4A1B5EC0E - - - Artemis_TemporaryKey.pfx - - - true - - - false - - - logo.ico - - - - ..\packages\Caliburn.Micro.Core.2.0.2\lib\net45\Caliburn.Micro.dll - True - - - ..\packages\Caliburn.Micro.2.0.2\lib\net45\Caliburn.Micro.Platform.dll - True - - - ..\packages\Colore.3.0.2\lib\net35\Corale.Colore.dll - True - - - ..\packages\CUE.NET.1.0.0\lib\net45\CUE.NET.dll - True - - - ..\packages\log4net.2.0.5\lib\net45-full\log4net.dll - True - - - ..\packages\MahApps.Metro.1.3.0-ALPHA017\lib\net45\MahApps.Metro.dll - True - - - False - - - False - .\MyMemory-x64.dll - False - - - ..\packages\NAudio.1.7.3\lib\net35\NAudio.dll - True - - - ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll - True - - - ..\packages\Open.WinKeyboardHook.1.0.10.0\lib\net45\Open.WinKeyboardHook.dll - True - - - - - - - - - - - - - - - - 4.0 - - - ..\packages\WpfExceptionViewer.1.0.0.0\lib\VioletTape.WpfExceptionViewer.dll - True - - - - - - ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.dll - True - - - ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.Themes.Aero.dll - True - - - ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.Themes.Metro.dll - True - - - ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.Themes.VS2010.dll - True - - - ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.DataGrid.dll - True - - - ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.Toolkit.dll - True - - - - - MSBuild:Compile - Designer - - - App.xaml - Code - - - - - - - - - - - - - - - - - - - AudioVisualization.settings - True - True - - - - - - TypeWave.settings - True - True - - - - - CounterStrike.settings - True - True - - - - - RocketLeague.settings - True - True - - - - True - True - Witcher3.settings - - - - - - - VolumeDisplay.settings - True - True - - - - - - - - - - - True - True - General.settings - - - True - True - Offsets.settings - - - - - - - - - - - - - - - - - - - - - - - - - - - - EffectsView.xaml - - - FlyoutSettingsView.xaml - - - DebugEffectView.xaml - - - AudioVisualizerView.xaml - - - TypeHoleView.xaml - - - TypeWaveView.xaml - - - GamesView.xaml - - - CounterStrikeView.xaml - - - Dota2View.xaml - - - RocketLeagueView.xaml - - - Witcher3View.xaml - - - OverlaysView.xaml - - - VolumeDisplayView.xaml - - - ShellView.xaml - - - - - Code - - - True - True - Resources.resx - - - ResXFileCodeGenerator - Resources.Designer.cs - - - SettingsSingleFileGenerator - AudioVisualization.Designer.cs - - - SettingsSingleFileGenerator - TypeWave.Designer.cs - - - SettingsSingleFileGenerator - CounterStrike.Designer.cs - - - SettingsSingleFileGenerator - RocketLeague.Designer.cs - - - SettingsSingleFileGenerator - Witcher3.Designer.cs - - - SettingsSingleFileGenerator - VolumeDisplay.Designer.cs - - - - - - SettingsSingleFileGenerator - Offsets.Designer.cs - - - - SettingsSingleFileGenerator - General.Designer.cs - - - - - - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - MSBuild:Compile - Designer - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - Designer - MSBuild:Compile - - - - - - PreserveNewest - - - PreserveNewest - - - - - - - - - False - Microsoft .NET Framework 4.5.2 %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 - false - - - - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - + + + + + Debug + AnyCPU + {ED9997A2-E54C-4E9F-9350-62BE672C3ABE} + WinExe + Properties + Artemis + Artemis + v4.5.2 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + + + + + + + false + C:\Users\spoin\Desktop\Artemis builds\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.0 + false + true + true + + + + + x64 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + EAC088BE27A2DE790AE6F37A020409F4A1B5EC0E + + + Artemis_TemporaryKey.pfx + + + true + + + false + + + logo.ico + + + + ..\packages\Caliburn.Micro.Core.2.0.2\lib\net45\Caliburn.Micro.dll + True + + + ..\packages\Caliburn.Micro.2.0.2\lib\net45\Caliburn.Micro.Platform.dll + True + + + ..\packages\Colore.3.0.2\lib\net35\Corale.Colore.dll + True + + + ..\packages\CUE.NET.1.0.0\lib\net45\CUE.NET.dll + True + + + ..\packages\log4net.2.0.5\lib\net45-full\log4net.dll + True + + + ..\packages\MahApps.Metro.1.3.0-ALPHA017\lib\net45\MahApps.Metro.dll + True + + + False + + + False + .\MyMemory-x64.dll + False + + + ..\packages\NAudio.1.7.3\lib\net35\NAudio.dll + True + + + ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll + True + + + ..\packages\Open.WinKeyboardHook.1.0.10.0\lib\net45\Open.WinKeyboardHook.dll + True + + + + + + + + + + + + + + + + 4.0 + + + ..\packages\WpfExceptionViewer.1.0.0.0\lib\VioletTape.WpfExceptionViewer.dll + True + + + + + + ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.dll + True + + + ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.Themes.Aero.dll + True + + + ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.Themes.Metro.dll + True + + + ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.AvalonDock.Themes.VS2010.dll + True + + + ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.DataGrid.dll + True + + + ..\packages\Extended.Wpf.Toolkit.2.6\lib\net40\Xceed.Wpf.Toolkit.dll + True + + + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + + + + + + + + + + + + + + + + + AudioVisualization.settings + True + True + + + + + + TypeWave.settings + True + True + + + + + CounterStrike.settings + True + True + + + + + RocketLeague.settings + True + True + + + + True + True + Witcher3.settings + + + + + + + VolumeDisplay.settings + True + True + + + + + + + + + + + True + True + General.settings + + + True + True + Offsets.settings + + + + + + + + + + + + + + + + + + + + + + + + + + + + + EffectsView.xaml + + + FlyoutSettingsView.xaml + + + DebugEffectView.xaml + + + AudioVisualizerView.xaml + + + TypeHoleView.xaml + + + TypeWaveView.xaml + + + GamesView.xaml + + + CounterStrikeView.xaml + + + Dota2View.xaml + + + RocketLeagueView.xaml + + + Witcher3View.xaml + + + OverlaysView.xaml + + + VolumeDisplayView.xaml + + + ShellView.xaml + + + + + Code + + + True + True + Resources.resx + + + ResXFileCodeGenerator + Resources.Designer.cs + + + SettingsSingleFileGenerator + AudioVisualization.Designer.cs + + + SettingsSingleFileGenerator + TypeWave.Designer.cs + + + SettingsSingleFileGenerator + CounterStrike.Designer.cs + + + SettingsSingleFileGenerator + RocketLeague.Designer.cs + + + SettingsSingleFileGenerator + Witcher3.Designer.cs + + + SettingsSingleFileGenerator + VolumeDisplay.Designer.cs + + + + + + SettingsSingleFileGenerator + Offsets.Designer.cs + + + + SettingsSingleFileGenerator + General.Designer.cs + + + + + + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + + + + PreserveNewest + + + PreserveNewest + + + + + + + + + False + Microsoft .NET Framework 4.5.2 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + --> \ No newline at end of file diff --git a/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs b/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs index df000f39f..ccac93af7 100644 --- a/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs +++ b/Artemis/Artemis/KeyboardProviders/Corsair/K95.cs @@ -1,10 +1,7 @@ using System.Drawing; using CUE.NET; -using CUE.NET.Devices.Generic.Enums; using CUE.NET.Devices.Keyboard; -using CUE.NET.Brushes; -using CUE.NET.Devices.Keyboard.Keys; -using System; +using Artemis.Utilities; namespace Artemis.KeyboardProviders.Corsair { @@ -47,17 +44,16 @@ namespace Artemis.KeyboardProviders.Corsair public override void DrawBitmap(Bitmap bitmap) { //bitmap = new Bitmap(@"C:\Users\Hazard\Desktop\1920x1080.png"); - Bitmap resize = new Bitmap(bitmap, new Size((int)_keyboard.KeyboardRectangle.Width, (int)_keyboard.KeyboardRectangle.Height)); - foreach (var item in _keyboard.Keys) + using ( + var resized = ImageUtilities.ResizeImage(bitmap, (int)_keyboard.KeyboardRectangle.Width, + (int)_keyboard.KeyboardRectangle.Height)) { - Color pixelColor = resize.GetPixel((int)item.KeyRectangle.X, (int)item.KeyRectangle.Y); - if (pixelColor.Name == "0") + foreach (var item in _keyboard.Keys) { - pixelColor = Color.Black; + item.Led.Color = resized.GetPixel((int)item.KeyRectangle.X, (int)item.KeyRectangle.Y); } - item.Led.Color = pixelColor; } - _keyboard.Update(); + _keyboard.Update(true); } /* diff --git a/Artemis/Artemis/Utilities/ImageUtilities.cs b/Artemis/Artemis/Utilities/ImageUtilities.cs new file mode 100644 index 000000000..0b200b35c --- /dev/null +++ b/Artemis/Artemis/Utilities/ImageUtilities.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; + + +namespace Artemis.Utilities +{ + class ImageUtilities + { + private static Dictionary encoders = null; + + /// + /// A lock to prevent concurrency issues loading the encoders. + /// + private static object encodersLock = new object(); + + /// + /// A quick lookup for getting image encoders + /// + public static Dictionary Encoders + { + //get accessor that creates the dictionary on demand + get + { + //if the quick lookup isn't initialised, initialise it + if (encoders == null) + { + //protect against concurrency issues + lock (encodersLock) + { + //check again, we might not have been the first person to acquire the lock (see the double checked lock pattern) + if (encoders == null) + { + encoders = new Dictionary(); + + //get all the codecs + foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders()) + { + //add each codec to the quick lookup + encoders.Add(codec.MimeType.ToLower(), codec); + } + } + } + } + + //return the lookup + return encoders; + } + } + + /// + /// Resize the image to the specified width and height. + /// + /// The image to resize. + /// The width to resize to. + /// The height to resize to. + /// The resized image. + public static Bitmap ResizeImage(Image image, int width, int height) + { + //a holder for the result + Bitmap result = new Bitmap(width, height); + //set the resolutions the same to avoid cropping due to resolution differences + result.SetResolution(image.HorizontalResolution, image.VerticalResolution); + + //use a graphics object to draw the resized image into the bitmap + using (Graphics graphics = Graphics.FromImage(result)) + { + //set the resize quality modes to high quality + graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; + graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; + graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; + //draw the image into the target bitmap + graphics.DrawImage(image, 0, 0, result.Width, result.Height); + } + + //return the resulting bitmap + return result; + } + + /// + /// Saves an image as a jpeg image, with the given quality + /// + /// Path to which the image would be saved. + /// An integer from 0 to 100, with 100 being the + /// highest quality + /// + /// An invalid value was entered for image quality. + /// + public static void SaveJpeg(string path, Image image, int quality) + { + //ensure the quality is within the correct range + if ((quality < 0) || (quality > 100)) + { + //create the error message + string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality. A value of {0} was specified.", quality); + //throw a helpful exception + throw new ArgumentOutOfRangeException(error); + } + + //create an encoder parameter for the image quality + EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); + //get the jpeg codec + ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg"); + + //create a collection of all parameters that we will pass to the encoder + EncoderParameters encoderParams = new EncoderParameters(1); + //set the quality parameter for the codec + encoderParams.Param[0] = qualityParam; + //save the image using the codec and the parameters + image.Save(path, jpegCodec, encoderParams); + } + + /// + /// Returns the image codec with the given mime type + /// + public static ImageCodecInfo GetEncoderInfo(string mimeType) + { + //do a case insensitive search for the mime type + string lookupKey = mimeType.ToLower(); + + //the codec to return, default to null + ImageCodecInfo foundCodec = null; + + //if we have the encoder, get it to return + if (Encoders.ContainsKey(lookupKey)) + { + //pull the codec from the lookup + foundCodec = Encoders[lookupKey]; + } + + return foundCodec; + } + } +}