diff --git a/DotImaging.sln b/DotImaging.sln index 5848b99..154e97d 100644 --- a/DotImaging.sln +++ b/DotImaging.sln @@ -14,7 +14,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Deploy", "Deploy", "{80FC5E Deploy\Licence.txt = Deploy\Licence.txt EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Playground", "Test\Playground.csproj", "{F34AE966-F8EB-46EC-A1A9-E7D8B1CA3DAB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Playground", "Test\Playground.csproj", "{F34AE966-F8EB-46EC-A1A9-E7D8B1CA3DAB}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Logo", "Logo", "{817B040E-5F27-4ACD-B1E4-F46452668A0E}" ProjectSection(SolutionItems) = preProject @@ -43,27 +43,33 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UI", "Source\UI\UI.csproj", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IO.Web", "Source\IO.Web\IO.Web.csproj", "{B8404D66-B64E-43E0-9653-35FC6782072F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.UI", "Samples\Sample.UI\Sample.UI.csproj", "{4DA857A9-0CE3-43DF-A455-D38A2FA64B07}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.UI", "Samples\Sample.UI\Sample.UI.csproj", "{4DA857A9-0CE3-43DF-A455-D38A2FA64B07}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.VideoStreaming", "Samples\Sample.VideoStreaming\Sample.VideoStreaming.csproj", "{CB088D8A-3438-4DB9-87C2-99425FE27F07}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.VideoStreaming", "Samples\Sample.VideoStreaming\Sample.VideoStreaming.csproj", "{CB088D8A-3438-4DB9-87C2-99425FE27F07}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Basic", "Samples\Sample.Basic\Sample.Basic.csproj", "{5F5FE08B-37C1-4C62-BB1A-DFFF61B3516A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Basic", "Samples\Sample.Basic\Sample.Basic.csproj", "{5F5FE08B-37C1-4C62-BB1A-DFFF61B3516A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Interop", "Samples\Sample.Interop\Sample.Interop.csproj", "{E4ED626D-359B-493C-A72E-3738A5353D1B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Interop", "Samples\Sample.Interop\Sample.Interop.csproj", "{E4ED626D-359B-493C-A72E-3738A5353D1B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Capture", "Samples\Sample.Capture\Sample.Capture.csproj", "{D0C18418-5282-4BA8-ABF7-CC2A9A179517}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.Capture", "Samples\Sample.Capture\Sample.Capture.csproj", "{D0C18418-5282-4BA8-ABF7-CC2A9A179517}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.CaptureAndRecord", "Samples\Sample.CaptureAndRecord\Sample.CaptureAndRecord.csproj", "{867ED0BD-FBFB-4C0C-A34B-D71361874AA0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.CaptureAndRecord", "Samples\Sample.CaptureAndRecord\Sample.CaptureAndRecord.csproj", "{867ED0BD-FBFB-4C0C-A34B-D71361874AA0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.ImageExtractor", "Samples\Sample.ImageExtractor\Sample.ImageExtractor.csproj", "{55FF1668-9501-4007-84D8-26EF860F0D0D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.ImageExtractor", "Samples\Sample.ImageExtractor\Sample.ImageExtractor.csproj", "{55FF1668-9501-4007-84D8-26EF860F0D0D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.MultipleCameraCapture", "Samples\Sample.MultipleCameraCapture\Sample.MultipleCameraCapture.csproj", "{C30A145E-02FB-49B4-8F3E-6673AF9D121A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample.MultipleCameraCapture", "Samples\Sample.MultipleCameraCapture\Sample.MultipleCameraCapture.csproj", "{C30A145E-02FB-49B4-8F3E-6673AF9D121A}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NuGet", "NuGet", "{44744CCA-2769-4607-AC90-78B6A11A2E77}" ProjectSection(SolutionItems) = preProject Deploy\Nuget\Push.cmd = Deploy\Nuget\Push.cmd EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaseOperations", "Source\BaseOperations\BaseOperations.csproj", "{E8F6A81C-6806-41D5-B795-0E214B308D5C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImageDebuggerVisualizer", "Tools\ImageDebuggerVisualizer\ImageDebuggerVisualizer.csproj", "{0EBA2590-3F39-4F8E-81A2-1F20CB934398}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{99AC6620-13B1-4543-91E7-08CD049B13B1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -146,6 +152,14 @@ Global {C30A145E-02FB-49B4-8F3E-6673AF9D121A}.Debug|Any CPU.Build.0 = Debug|Any CPU {C30A145E-02FB-49B4-8F3E-6673AF9D121A}.Release|Any CPU.ActiveCfg = Release|Any CPU {C30A145E-02FB-49B4-8F3E-6673AF9D121A}.Release|Any CPU.Build.0 = Release|Any CPU + {E8F6A81C-6806-41D5-B795-0E214B308D5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8F6A81C-6806-41D5-B795-0E214B308D5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8F6A81C-6806-41D5-B795-0E214B308D5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8F6A81C-6806-41D5-B795-0E214B308D5C}.Release|Any CPU.Build.0 = Release|Any CPU + {0EBA2590-3F39-4F8E-81A2-1F20CB934398}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0EBA2590-3F39-4F8E-81A2-1F20CB934398}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0EBA2590-3F39-4F8E-81A2-1F20CB934398}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0EBA2590-3F39-4F8E-81A2-1F20CB934398}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -172,6 +186,8 @@ Global {55FF1668-9501-4007-84D8-26EF860F0D0D} = {05BDC895-7F7B-4B6F-8D69-E7916837092D} {C30A145E-02FB-49B4-8F3E-6673AF9D121A} = {05BDC895-7F7B-4B6F-8D69-E7916837092D} {44744CCA-2769-4607-AC90-78B6A11A2E77} = {80FC5EA1-3478-4E7F-8B05-190DF6750EA4} + {E8F6A81C-6806-41D5-B795-0E214B308D5C} = {80058A54-6223-480C-AA19-F99E7AE593F3} + {0EBA2590-3F39-4F8E-81A2-1F20CB934398} = {99AC6620-13B1-4543-91E7-08CD049B13B1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DEAC2BC6-C697-4361-9909-CF7055FE9B05} diff --git a/Samples/Sample.Capture/Program.cs b/Samples/Sample.Capture/Program.cs index 388600f..6b3602c 100644 --- a/Samples/Sample.Capture/Program.cs +++ b/Samples/Sample.Capture/Program.cs @@ -52,6 +52,7 @@ static void Main() while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)); reader.Dispose(); + UI.CloseAll(); } private static string getResourceDir() diff --git a/Samples/Sample.Interop/Program.cs b/Samples/Sample.Interop/Program.cs index 86b8610..d313f63 100644 --- a/Samples/Sample.Interop/Program.cs +++ b/Samples/Sample.Interop/Program.cs @@ -36,7 +36,7 @@ static void Main(string[] args) var img = new Bgr[480, 640]; //*********************************************************************************************************************************************************************** - Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("********* TColor[,] <=> Image<> conversions (built-in) ****************"); Console.ResetColor(); //to Image<> Image> lockedImg = img.Lock(); //from Image<> @@ -44,7 +44,7 @@ static void Main(string[] args) //*********************************************************************************************************************************************************************** Console.WriteLine(); - Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("********* Image<,> <=> OpenCV conversions (built-in) ****************"); Console.ResetColor(); //to IplImage IplImage iplImage; using (var uImg = img.Lock()) @@ -56,7 +56,7 @@ static void Main(string[] args) //*********************************************************************************************************************************************************************** Console.WriteLine(); - Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("*********** Image<,> <=> Bitmap conversions (BitmapInterop) ****************"); Console.ResetColor(); //to Bitmap var bmp = img.ToBitmap(); //from Bitmap @@ -64,7 +64,7 @@ static void Main(string[] args) //*********************************************************************************************************************************************************************** Console.WriteLine(); - Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("*********** Image<,> <=> BitmapSource conversions (BitmapSourceInterop) ****************"); Console.ResetColor(); + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("*********** Image<,> <=> BitmapSource conversions (BitmapSourceInterop) ****************"); Console.ResetColor(); //to generic-image var colorBitmap = new BitmapImage(new Uri("http://www.online-image-editor.com//styles/2014/images/example_image.png")); Bgra[,] colorImg = colorBitmap.ToArray>(); diff --git a/Samples/Sample.VideoStreaming/Program.cs b/Samples/Sample.VideoStreaming/Program.cs index ccf97bb..186597f 100644 --- a/Samples/Sample.VideoStreaming/Program.cs +++ b/Samples/Sample.VideoStreaming/Program.cs @@ -28,7 +28,7 @@ public static void Main() //read video frames Bgr[,] frame = null; - do + while(true) { reader.ReadTo(ref frame); if (frame == null) @@ -37,11 +37,10 @@ public static void Main() frame.Show(scaleForm: true); ((double)reader.Position / reader.Length).Progress(); } - while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)); - Console.WriteLine("The end."); //--------------------------------------------------------------------------- + UI.CloseAll(); Console.WriteLine("Downloading video..."); string fileExtension; diff --git a/Source/BaseOperations/Base/CvInvoke.cs b/Source/BaseOperations/Base/CvInvoke.cs new file mode 100644 index 0000000..fd6a79e --- /dev/null +++ b/Source/BaseOperations/Base/CvInvoke.cs @@ -0,0 +1,8 @@ +using System; + +namespace DotImaging +{ + public class Class1 + { + } +} diff --git a/Source/BaseOperations/BaseOperations.cs b/Source/BaseOperations/BaseOperations.cs new file mode 100644 index 0000000..537f6c2 --- /dev/null +++ b/Source/BaseOperations/BaseOperations.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DotImaging +{ + public static class BaseOperations + { + public static Bgr[,] Resize(Bgr[,] image, float scale) + { + return null; + } + + public static Bgr[,] Resize(Bgr[,] image, int newWidth, int newHeight) + { + return null; + } + + + public static Bgr[,] Flip(Bgr[,] image) + { + return null; + } + } +} diff --git a/Source/BaseOperations/BaseOperations.csproj b/Source/BaseOperations/BaseOperations.csproj new file mode 100644 index 0000000..5913819 --- /dev/null +++ b/Source/BaseOperations/BaseOperations.csproj @@ -0,0 +1,15 @@ + + + + netcoreapp2.0 + + + + + + + + + + + diff --git a/Source/BitmapInterop/BitmapInterop.csproj b/Source/BitmapInterop/BitmapInterop.csproj index 51e8f98..2434ff4 100644 --- a/Source/BitmapInterop/BitmapInterop.csproj +++ b/Source/BitmapInterop/BitmapInterop.csproj @@ -1,11 +1,9 @@  net47;netcoreapp2.0 - - - - DotImaging bin\ + DotImaging.BitmapInterop + DotImaging diff --git a/Source/BitmapInterop/BitmapConversion.cs b/Source/BitmapInterop/Extensions/BitmapConversion.cs similarity index 97% rename from Source/BitmapInterop/BitmapConversion.cs rename to Source/BitmapInterop/Extensions/BitmapConversion.cs index 5d0bb13..42c1f46 100644 --- a/Source/BitmapInterop/BitmapConversion.cs +++ b/Source/BitmapInterop/Extensions/BitmapConversion.cs @@ -1,390 +1,390 @@ -#region Licence and Terms -// DotImaging Framework -// https://github.com/dajuric/dot-imaging -// -// Copyright © Darko Jurić, 2014-2018 -// darko.juric2@gmail.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion - -using System; -using System.Drawing; -using System.Drawing.Imaging; - -namespace DotImaging -{ - /// - /// Provides conversion extension methods between generic image and . - /// - public static class BitmapConversionExtensions - { - #region Conversion from Bitmap - - private static TColor[,] toArray(Bitmap bmp) - where TColor: struct, IColor - { - var bmpData = bmp.LockBits(ImageLockMode.ReadOnly); - - var arr = new TColor[bmp.Height, bmp.Width]; - using (var img = arr.Lock()) - { - Copy.UnsafeCopy2D(bmpData.Scan0, img.ImageData, bmpData.Stride, img.Stride, bmpData.Height); - } - - bmp.UnlockBits(bmpData); - return arr; - } - - /// - /// Converts a bitmap to an image (copies data). - /// - /// Input bitmap. - /// 2D array. - public static Array ToArray(this Bitmap bmp) - { - Array arr = null; - switch (bmp.PixelFormat) - { - case PixelFormat.Format8bppIndexed: - arr = toArray>(bmp); - break; - case PixelFormat.Format16bppGrayScale: - arr = toArray>(bmp); - break; - case PixelFormat.Format24bppRgb: - arr = toArray>(bmp); - break; - case PixelFormat.Format32bppArgb: - arr = toArray>(bmp); - break; - case PixelFormat.Format48bppRgb: - arr = toArray>(bmp); - break; - case PixelFormat.Format64bppArgb: - arr = toArray>(bmp); - break; - default: - throw new NotSupportedException(); - } - - return arr; - } - - /// - /// Converts the specified bitmap into Bgr managed image. - /// - /// Bgr, Bgra or Gray type bitmap. - /// Bgr image or null if conversion can not be performed. - public static Bgr[,] ToBgr(this Bitmap bitmap) - { - var arr = bitmap.ToArray(); - return arr.ToBgr(); - } - - /// - /// Converts the specified bitmap into Bgra managed image. - /// - /// Bgra, Bgr or Gray type bitmap. - /// Bgra image or null if conversion can not be performed. - public static Bgra[,] ToBgra(this Bitmap bitmap) - { - var arr = bitmap.ToArray(); - return arr.ToBgra(); - } - - #endregion - - #region Conversion To Bitmap - - private static Bitmap toBitmap(IImage img, PixelFormat pixelFormat) - { - var bmp = new Bitmap(img.Width, img.Height, pixelFormat); - var bmpData = bmp.LockBits(ImageLockMode.WriteOnly); - Copy.UnsafeCopy2D(img.ImageData, bmpData.Scan0, img.Stride, bmpData.Stride, bmpData.Height); - bmp.UnlockBits(bmpData); - - if (pixelFormat == PixelFormat.Format8bppIndexed) - bmp.SetGrayscalePalette(); - - return bmp; - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Image> img) - { - return toBitmap(img, PixelFormat.Format8bppIndexed); - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Image> img) - { - return toBitmap(img, PixelFormat.Format16bppGrayScale); - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Image> img) - { - return toBitmap(img, PixelFormat.Format24bppRgb); - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Image> img) - { - return toBitmap(img, PixelFormat.Format32bppArgb); - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Image> img) - { - return toBitmap(img, PixelFormat.Format48bppRgb); - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Image> img) - { - return toBitmap(img, PixelFormat.Format64bppArgb); - } - - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Gray[,] img) - { - Bitmap bmp = null; - using (var uImg = img.Lock()) - { - bmp = toBitmap(uImg, PixelFormat.Format8bppIndexed); - } - return bmp; - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Gray[,] img) - { - Bitmap bmp = null; - using (var uImg = img.Lock()) - { - bmp = toBitmap(uImg, PixelFormat.Format16bppGrayScale); - } - return bmp; - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Bgr[,] img) - { - Bitmap bmp = null; - using (var uImg = img.Lock()) - { - bmp = toBitmap(uImg, PixelFormat.Format24bppRgb); - } - return bmp; - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Bgra[,] img) - { - Bitmap bmp = null; - using (var uImg = img.Lock()) - { - bmp = toBitmap(uImg, PixelFormat.Format32bppArgb); - } - return bmp; - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Bgr[,] img) - { - Bitmap bmp = null; - using (var uImg = img.Lock()) - { - bmp = toBitmap(uImg, PixelFormat.Format48bppRgb); - } - return bmp; - } - - /// - /// Converts an image to an bitmap. - /// - /// Input image. - /// Bitmap - public static Bitmap ToBitmap(this Bgra[,] img) - { - Bitmap bmp = null; - using (var uImg = img.Lock()) - { - bmp = toBitmap(uImg, PixelFormat.Format64bppArgb); - } - return bmp; - } - - #endregion - - #region Cast to Bitmap - - private static Bitmap asBitmap(IImage img, PixelFormat pixelFormat) - { - var bmp = new Bitmap(img.Width, img.Height, img.Stride, pixelFormat, img.ImageData); - - if (pixelFormat == PixelFormat.Format8bppIndexed) - bmp.SetGrayscalePalette(); - - return bmp; - } - - /// - /// Casts an image to an bitmap. - /// Notice that GDI+ does not support bitmaps which stride is not 4. - /// - /// Input image. - /// Bitmap - public static Bitmap AsBitmap(this Image> img) - { - return asBitmap(img, PixelFormat.Format8bppIndexed); - } - - /// - /// Casts an image to an bitmap. - /// Notice that GDI+ does not support bitmaps which stride is not 4. - /// - /// Input image. - /// Bitmap - public static Bitmap AsBitmap(this Image> img) - { - return asBitmap(img, PixelFormat.Format16bppGrayScale); - } - - /// - /// Casts an image to an bitmap. - /// Notice that GDI+ does not support bitmaps which stride is not 4. - /// - /// Input image. - /// Bitmap - public static Bitmap AsBitmap(this Image> img) - { - return asBitmap(img, PixelFormat.Format24bppRgb); - } - - /// - /// Casts an image to an bitmap. - /// Notice that GDI+ does not support bitmaps which stride is not 4. - /// - /// Input image. - /// Bitmap - public static Bitmap AsBitmap(this Image> img) - { - return asBitmap(img, PixelFormat.Format32bppArgb); - } - - /// - /// Casts an image to an bitmap. - /// Notice that GDI+ does not support bitmaps which stride is not 4. - /// - /// Input image. - /// Bitmap - public static Bitmap AsBitmap(this Image> img) - { - return asBitmap(img, PixelFormat.Format48bppRgb); - } - - /// - /// Casts an image to an bitmap. - /// Notice that GDI+ does not support bitmaps which stride is not 4. - /// - /// Input image. - /// Bitmap - public static Bitmap AsBitmap(this Image> img) - { - return asBitmap(img, PixelFormat.Format64bppArgb); - } - - #endregion - - #region Misc - - /// - /// Replaces color palette entries with grayscale intensities (256 entries). - /// - /// The 8-bpp grayscale image. - public static void SetGrayscalePalette(this Bitmap image) - { - if (image.PixelFormat != PixelFormat.Format8bppIndexed) - throw new ArgumentException("The provided image must have 8bpp pixel format."); - - var palette = image.Palette; - for (int i = 0; i < (Byte.MaxValue + 1); i++) - { - palette.Entries[i] = Color.FromArgb(i, i, i); - } - - image.Palette = palette; - } - - /// - /// Lock a into system memory. - /// - /// Bitmap to lock. - /// Specifies the access level. - /// Bitmap data. - public static BitmapData LockBits(this Bitmap bmp, ImageLockMode imageLockMode = ImageLockMode.ReadWrite) - { - return bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), imageLockMode, bmp.PixelFormat); - } - - #endregion - } -} +#region Licence and Terms +// DotImaging Framework +// https://github.com/dajuric/dot-imaging +// +// Copyright © Darko Jurić, 2014-2018 +// darko.juric2@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +using System; +using System.Drawing; +using System.Drawing.Imaging; + +namespace DotImaging +{ + /// + /// Provides conversion extension methods between generic image and . + /// + public static class BitmapConversionExtensions + { + #region Conversion from Bitmap + + private static TColor[,] toArray(Bitmap bmp) + where TColor: struct, IColor + { + var bmpData = bmp.LockBits(ImageLockMode.ReadOnly); + + var arr = new TColor[bmp.Height, bmp.Width]; + using (var img = arr.Lock()) + { + Copy.UnsafeCopy2D(bmpData.Scan0, img.ImageData, bmpData.Stride, img.Stride, bmpData.Height); + } + + bmp.UnlockBits(bmpData); + return arr; + } + + /// + /// Converts a bitmap to an image (copies data). + /// + /// Input bitmap. + /// 2D array. + public static Array ToArray(this Bitmap bmp) + { + Array arr = null; + switch (bmp.PixelFormat) + { + case PixelFormat.Format8bppIndexed: + arr = toArray>(bmp); + break; + case PixelFormat.Format16bppGrayScale: + arr = toArray>(bmp); + break; + case PixelFormat.Format24bppRgb: + arr = toArray>(bmp); + break; + case PixelFormat.Format32bppArgb: + arr = toArray>(bmp); + break; + case PixelFormat.Format48bppRgb: + arr = toArray>(bmp); + break; + case PixelFormat.Format64bppArgb: + arr = toArray>(bmp); + break; + default: + throw new NotSupportedException(); + } + + return arr; + } + + /// + /// Converts the specified bitmap into Bgr managed image. + /// + /// Bgr, Bgra or Gray type bitmap. + /// Bgr image or null if conversion can not be performed. + public static Bgr[,] ToBgr(this Bitmap bitmap) + { + var arr = bitmap.ToArray(); + return arr.ToBgr(); + } + + /// + /// Converts the specified bitmap into Bgra managed image. + /// + /// Bgra, Bgr or Gray type bitmap. + /// Bgra image or null if conversion can not be performed. + public static Bgra[,] ToBgra(this Bitmap bitmap) + { + var arr = bitmap.ToArray(); + return arr.ToBgra(); + } + + #endregion + + #region Conversion To Bitmap + + private static Bitmap toBitmap(IImage img, PixelFormat pixelFormat) + { + var bmp = new Bitmap(img.Width, img.Height, pixelFormat); + var bmpData = bmp.LockBits(ImageLockMode.WriteOnly); + Copy.UnsafeCopy2D(img.ImageData, bmpData.Scan0, img.Stride, bmpData.Stride, bmpData.Height); + bmp.UnlockBits(bmpData); + + if (pixelFormat == PixelFormat.Format8bppIndexed) + bmp.SetGrayscalePalette(); + + return bmp; + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Image> img) + { + return toBitmap(img, PixelFormat.Format8bppIndexed); + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Image> img) + { + return toBitmap(img, PixelFormat.Format16bppGrayScale); + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Image> img) + { + return toBitmap(img, PixelFormat.Format24bppRgb); + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Image> img) + { + return toBitmap(img, PixelFormat.Format32bppArgb); + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Image> img) + { + return toBitmap(img, PixelFormat.Format48bppRgb); + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Image> img) + { + return toBitmap(img, PixelFormat.Format64bppArgb); + } + + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Gray[,] img) + { + Bitmap bmp = null; + using (var uImg = img.Lock()) + { + bmp = toBitmap(uImg, PixelFormat.Format8bppIndexed); + } + return bmp; + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Gray[,] img) + { + Bitmap bmp = null; + using (var uImg = img.Lock()) + { + bmp = toBitmap(uImg, PixelFormat.Format16bppGrayScale); + } + return bmp; + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Bgr[,] img) + { + Bitmap bmp = null; + using (var uImg = img.Lock()) + { + bmp = toBitmap(uImg, PixelFormat.Format24bppRgb); + } + return bmp; + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Bgra[,] img) + { + Bitmap bmp = null; + using (var uImg = img.Lock()) + { + bmp = toBitmap(uImg, PixelFormat.Format32bppArgb); + } + return bmp; + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Bgr[,] img) + { + Bitmap bmp = null; + using (var uImg = img.Lock()) + { + bmp = toBitmap(uImg, PixelFormat.Format48bppRgb); + } + return bmp; + } + + /// + /// Converts an image to an bitmap. + /// + /// Input image. + /// Bitmap + public static Bitmap ToBitmap(this Bgra[,] img) + { + Bitmap bmp = null; + using (var uImg = img.Lock()) + { + bmp = toBitmap(uImg, PixelFormat.Format64bppArgb); + } + return bmp; + } + + #endregion + + #region Cast to Bitmap + + private static Bitmap asBitmap(IImage img, PixelFormat pixelFormat) + { + var bmp = new Bitmap(img.Width, img.Height, img.Stride, pixelFormat, img.ImageData); + + if (pixelFormat == PixelFormat.Format8bppIndexed) + bmp.SetGrayscalePalette(); + + return bmp; + } + + /// + /// Casts an image to an bitmap. + /// Notice that GDI+ does not support bitmaps which stride is not 4. + /// + /// Input image. + /// Bitmap + public static Bitmap AsBitmap(this Image> img) + { + return asBitmap(img, PixelFormat.Format8bppIndexed); + } + + /// + /// Casts an image to an bitmap. + /// Notice that GDI+ does not support bitmaps which stride is not 4. + /// + /// Input image. + /// Bitmap + public static Bitmap AsBitmap(this Image> img) + { + return asBitmap(img, PixelFormat.Format16bppGrayScale); + } + + /// + /// Casts an image to an bitmap. + /// Notice that GDI+ does not support bitmaps which stride is not 4. + /// + /// Input image. + /// Bitmap + public static Bitmap AsBitmap(this Image> img) + { + return asBitmap(img, PixelFormat.Format24bppRgb); + } + + /// + /// Casts an image to an bitmap. + /// Notice that GDI+ does not support bitmaps which stride is not 4. + /// + /// Input image. + /// Bitmap + public static Bitmap AsBitmap(this Image> img) + { + return asBitmap(img, PixelFormat.Format32bppArgb); + } + + /// + /// Casts an image to an bitmap. + /// Notice that GDI+ does not support bitmaps which stride is not 4. + /// + /// Input image. + /// Bitmap + public static Bitmap AsBitmap(this Image> img) + { + return asBitmap(img, PixelFormat.Format48bppRgb); + } + + /// + /// Casts an image to an bitmap. + /// Notice that GDI+ does not support bitmaps which stride is not 4. + /// + /// Input image. + /// Bitmap + public static Bitmap AsBitmap(this Image> img) + { + return asBitmap(img, PixelFormat.Format64bppArgb); + } + + #endregion + + #region Misc + + /// + /// Replaces color palette entries with grayscale intensities (256 entries). + /// + /// The 8-bpp grayscale image. + public static void SetGrayscalePalette(this Bitmap image) + { + if (image.PixelFormat != PixelFormat.Format8bppIndexed) + throw new ArgumentException("The provided image must have 8bpp pixel format."); + + var palette = image.Palette; + for (int i = 0; i < (Byte.MaxValue + 1); i++) + { + palette.Entries[i] = Color.FromArgb(i, i, i); + } + + image.Palette = palette; + } + + /// + /// Lock a into system memory. + /// + /// Bitmap to lock. + /// Specifies the access level. + /// Bitmap data. + public static BitmapData LockBits(this Bitmap bmp, ImageLockMode imageLockMode = ImageLockMode.ReadWrite) + { + return bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), imageLockMode, bmp.PixelFormat); + } + + #endregion + } +} diff --git a/Source/BitmapInterop/BmpSaveExtensions.cs b/Source/BitmapInterop/Extensions/BmpIO.cs similarity index 70% rename from Source/BitmapInterop/BmpSaveExtensions.cs rename to Source/BitmapInterop/Extensions/BmpIO.cs index 96f92ad..c3126a2 100644 --- a/Source/BitmapInterop/BmpSaveExtensions.cs +++ b/Source/BitmapInterop/Extensions/BmpIO.cs @@ -1,98 +1,123 @@ -#region Licence and Terms -// DotImaging Framework -// https://github.com/dajuric/dot-imaging -// -// Copyright © Darko Jurić, 2014-2018 -// darko.juric2@gmail.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion - -using System; -using System.Collections.Generic; -using System.Drawing.Imaging; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.IO; - -namespace DotImaging -{ - /// - /// Bitmap file save extensions. - /// - public static class BmpSaveExtensions - { - /// - /// Saves the specified image. - /// - /// Quality parameter is only supported for JPEG, PNG file types. - /// For other file types this value is omitted. - /// - /// - /// Image. - /// File name. - /// Quality parameter [0..100] where 0 means maximum compression. - public static void Save(this System.Drawing.Image image, string filename, int quality = 90) - { - var encoder = getEncoder(new FileInfo(filename).Extension); - - if (encoder != null) - { - System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; - EncoderParameters myEncoderParameters = new EncoderParameters(1); - myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, quality); - - image.Save(filename, encoder, myEncoderParameters); - } - else - { - image.Save(filename); - } - } - - private static ImageCodecInfo getEncoder(string extension) - { - switch (extension) - { - case ".jpg": - case ".jpeg": - return getEncoderCashed(ImageFormat.Jpeg); - case ".png": - return getEncoderCashed(ImageFormat.Png); - default: - return null; - } - } - - private static ImageCodecInfo getEncoderCashed(ImageFormat imageFormat) - { - return MethodCache.Global.Invoke((format) => getEncoder(format), imageFormat); - } - - private static ImageCodecInfo getEncoder(ImageFormat format) - { - ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); - - foreach (ImageCodecInfo codec in codecs) - { - if (codec.FormatID == format.Guid) - { - return codec; - } - } - return null; - } - } -} +#region Licence and Terms +// DotImaging Framework +// https://github.com/dajuric/dot-imaging +// +// Copyright © Darko Jurić, 2014-2018 +// darko.juric2@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +using System.Drawing.Imaging; +using System.IO; + +namespace DotImaging +{ + /// + /// Bitmap file save extensions. + /// + public static class BmpIO + { + /// + /// Saves the specified image. + /// + /// Quality parameter is only supported for JPEG, PNG file types. + /// For other file types this value is omitted. + /// + /// + /// Image. + /// Target stream. + /// Image format. + /// Quality parameter [0..100] where 0 means maximum compression. + public static void Save(this System.Drawing.Image image, Stream targetStream, ImageFormat imageFormat, int quality = 90) + { + var encoder = getEncoder(imageFormat); + + if (encoder != null) + { + System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; + EncoderParameters myEncoderParameters = new EncoderParameters(1); + myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, quality); + + image.Save(targetStream, encoder, myEncoderParameters); + } + else + { + image.Save(targetStream, imageFormat); + } + } + + + /// + /// Saves the specified image. + /// + /// Quality parameter is only supported for JPEG, PNG file types. + /// For other file types this value is omitted. + /// + /// + /// Image. + /// File name. + /// Quality parameter [0..100] where 0 means maximum compression. + public static void Save(this System.Drawing.Image image, string filename, int quality = 90) + { + var encoder = getEncoder(new FileInfo(filename).Extension); + + if (encoder != null) + { + System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; + EncoderParameters myEncoderParameters = new EncoderParameters(1); + myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, quality); + + image.Save(filename, encoder, myEncoderParameters); + } + else + { + image.Save(filename); + } + } + + private static ImageCodecInfo getEncoder(string extension) + { + switch (extension) + { + case ".jpg": + case ".jpeg": + return getEncoderCashed(ImageFormat.Jpeg); + case ".png": + return getEncoderCashed(ImageFormat.Png); + default: + return null; + } + } + + private static ImageCodecInfo getEncoderCashed(ImageFormat imageFormat) + { + return MethodCache.Global.Invoke((format) => getEncoder(format), imageFormat); + } + + private static ImageCodecInfo getEncoder(ImageFormat format) + { + ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); + + foreach (ImageCodecInfo codec in codecs) + { + if (codec.FormatID == format.Guid) + { + return codec; + } + } + return null; + } + } +} diff --git a/Source/BitmapInterop/ColorExtensions.cs b/Source/BitmapInterop/Extensions/ColorConversion.cs similarity index 95% rename from Source/BitmapInterop/ColorExtensions.cs rename to Source/BitmapInterop/Extensions/ColorConversion.cs index 20a1720..6e9d05f 100644 --- a/Source/BitmapInterop/ColorExtensions.cs +++ b/Source/BitmapInterop/Extensions/ColorConversion.cs @@ -1,74 +1,74 @@ -#region Licence and Terms -// DotImaging Framework -// https://github.com/dajuric/dot-imaging -// -// Copyright © Darko Jurić, 2014-2018 -// darko.juric2@gmail.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion - -using System; -using System.Drawing; - -namespace DotImaging -{ - /// - /// Contains color format conversion extensions. - /// - public static class ColorExtensions - { - /// - /// Gets System.Drawing.Color from Bgr8 color. - /// - /// Color. - /// Opacity. If color has 4 channels opacity is discarded. - /// System.Drawing.Color - public static System.Drawing.Color ToColor(this Gray color, byte opacity = Byte.MaxValue) - { - return Color.FromArgb(opacity, color.Intensity, color.Intensity, color.Intensity); - } - - /// - /// Gets System.Drawing.Color from Bgr8 color. - /// - /// Color. - /// Opacity. If color has 4 channels opacity is discarded. - /// System.Drawing.Color - public static System.Drawing.Color ToColor(this Bgr color, byte opacity = Byte.MaxValue) - { - return Color.FromArgb(opacity, color.R, color.G, color.B); - } - - /// - /// Gets System.Drawing.Color from Bgra8 color. - /// - /// Color. - /// System.Drawing.Color - public static System.Drawing.Color ToColor(this Bgra color) - { - return Color.FromArgb(color.A, color.R, color.G, color.B); - } - - /// - /// Converts (casts) the color into 32-bit BGR color. - /// - /// Color. - /// Bgr representation. - public static Bgr ToBgr(this System.Drawing.Color color) - { - return new Bgr { B = color.B, G = color.G, R = color.R }; - } - } -} +#region Licence and Terms +// DotImaging Framework +// https://github.com/dajuric/dot-imaging +// +// Copyright © Darko Jurić, 2014-2018 +// darko.juric2@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +using System; +using System.Drawing; + +namespace DotImaging +{ + /// + /// Contains color format conversion extensions. + /// + public static class ColorConversion + { + /// + /// Gets System.Drawing.Color from Bgr8 color. + /// + /// Color. + /// Opacity. If color has 4 channels opacity is discarded. + /// System.Drawing.Color + public static System.Drawing.Color ToColor(this Gray color, byte opacity = Byte.MaxValue) + { + return Color.FromArgb(opacity, color.Intensity, color.Intensity, color.Intensity); + } + + /// + /// Gets System.Drawing.Color from Bgr8 color. + /// + /// Color. + /// Opacity. If color has 4 channels opacity is discarded. + /// System.Drawing.Color + public static System.Drawing.Color ToColor(this Bgr color, byte opacity = Byte.MaxValue) + { + return Color.FromArgb(opacity, color.R, color.G, color.B); + } + + /// + /// Gets System.Drawing.Color from Bgra8 color. + /// + /// Color. + /// System.Drawing.Color + public static System.Drawing.Color ToColor(this Bgra color) + { + return Color.FromArgb(color.A, color.R, color.G, color.B); + } + + /// + /// Converts (casts) the color into 32-bit BGR color. + /// + /// Color. + /// Bgr representation. + public static Bgr ToBgr(this System.Drawing.Color color) + { + return new Bgr { B = color.B, G = color.G, R = color.R }; + } + } +} diff --git a/Source/BitmapInterop/DrawingStructureConversions.cs b/Source/BitmapInterop/Extensions/DrawingStructureConversions.cs similarity index 97% rename from Source/BitmapInterop/DrawingStructureConversions.cs rename to Source/BitmapInterop/Extensions/DrawingStructureConversions.cs index d335e4c..1665563 100644 --- a/Source/BitmapInterop/DrawingStructureConversions.cs +++ b/Source/BitmapInterop/Extensions/DrawingStructureConversions.cs @@ -1,164 +1,164 @@ -#region Licence and Terms -// DotImaging Framework -// https://github.com/dajuric/dot-imaging -// -// Copyright © Darko Jurić, 2014-2018 -// darko.juric2@gmail.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#endregion - -using DotImaging.Primitives2D; - -namespace DotImaging -{ - /// - /// Contains extension methods for System.Drawing namespace and Accord.NET extensions structure conversion. - /// - public static class DrawingStructureConversions - { - #region Point conversions - - /// - /// Converts to the . - /// - /// - /// - public static Point ToPt(this System.Drawing.Point point) - { - return new Point(point.X, point.Y); - } - - /// - /// Converts to the . - /// - /// - /// - public static PointF ToPt(this System.Drawing.PointF point) - { - return new PointF(point.X, point.Y); - } - - /// - /// Converts to to the . - /// - /// - /// - public static System.Drawing.Point ToPt(this Point point) - { - return new System.Drawing.Point(point.X, point.Y); - } - - /// - /// Converts to to the . - /// - /// - /// - public static System.Drawing.PointF ToPt(this PointF point) - { - return new System.Drawing.PointF(point.X, point.Y); - } - - #endregion - - #region Rectangle conversions - - /// - /// Converts the to the . - /// - /// - /// - public static Rectangle ToRect(this System.Drawing.Rectangle rect) - { - return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height); - } - - /// - /// Converts the to the . - /// - /// - /// - public static RectangleF ToRect(this System.Drawing.RectangleF rect) - { - return new RectangleF(rect.X, rect.Y, rect.Width, rect.Height); - } - - /// - /// Converts the to the . - /// - /// - /// - public static System.Drawing.Rectangle ToRect(this Rectangle rect) - { - return new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height); - } - - /// - /// Converts the to the . - /// - /// - /// - public static System.Drawing.RectangleF ToRect(this RectangleF rect) - { - return new System.Drawing.RectangleF(rect.X, rect.Y, rect.Width, rect.Height); - } - - #endregion - - #region Size conversions - - /// - /// Converts the to the . - /// - /// - /// - public static Size ToSize(this System.Drawing.Size size) - { - return new Size(size.Width, size.Height); - } - - /// - /// Converts the to the . - /// - /// - /// - public static SizeF ToSize(this System.Drawing.SizeF size) - { - return new SizeF(size.Width, size.Height); - } - - /// - /// Converts the to the . - /// - /// - /// - public static System.Drawing.Size ToSize(this Size size) - { - return new System.Drawing.Size(size.Width, size.Height); - } - - /// - /// Converts the to the . - /// - /// - /// - public static System.Drawing.SizeF ToSize(this SizeF size) - { - return new System.Drawing.SizeF(size.Width, size.Height); - } - - #endregion - - } -} +#region Licence and Terms +// DotImaging Framework +// https://github.com/dajuric/dot-imaging +// +// Copyright © Darko Jurić, 2014-2018 +// darko.juric2@gmail.com +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#endregion + +using DotImaging.Primitives2D; + +namespace DotImaging +{ + /// + /// Contains extension methods for System.Drawing namespace and Accord.NET extensions structure conversion. + /// + public static class DrawingStructureConversions + { + #region Point conversions + + /// + /// Converts to the . + /// + /// + /// + public static Point ToPt(this System.Drawing.Point point) + { + return new Point(point.X, point.Y); + } + + /// + /// Converts to the . + /// + /// + /// + public static PointF ToPt(this System.Drawing.PointF point) + { + return new PointF(point.X, point.Y); + } + + /// + /// Converts to to the . + /// + /// + /// + public static System.Drawing.Point ToPt(this Point point) + { + return new System.Drawing.Point(point.X, point.Y); + } + + /// + /// Converts to to the . + /// + /// + /// + public static System.Drawing.PointF ToPt(this PointF point) + { + return new System.Drawing.PointF(point.X, point.Y); + } + + #endregion + + #region Rectangle conversions + + /// + /// Converts the to the . + /// + /// + /// + public static Rectangle ToRect(this System.Drawing.Rectangle rect) + { + return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height); + } + + /// + /// Converts the to the . + /// + /// + /// + public static RectangleF ToRect(this System.Drawing.RectangleF rect) + { + return new RectangleF(rect.X, rect.Y, rect.Width, rect.Height); + } + + /// + /// Converts the to the . + /// + /// + /// + public static System.Drawing.Rectangle ToRect(this Rectangle rect) + { + return new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height); + } + + /// + /// Converts the to the . + /// + /// + /// + public static System.Drawing.RectangleF ToRect(this RectangleF rect) + { + return new System.Drawing.RectangleF(rect.X, rect.Y, rect.Width, rect.Height); + } + + #endregion + + #region Size conversions + + /// + /// Converts the to the . + /// + /// + /// + public static Size ToSize(this System.Drawing.Size size) + { + return new Size(size.Width, size.Height); + } + + /// + /// Converts the to the . + /// + /// + /// + public static SizeF ToSize(this System.Drawing.SizeF size) + { + return new SizeF(size.Width, size.Height); + } + + /// + /// Converts the to the . + /// + /// + /// + public static System.Drawing.Size ToSize(this Size size) + { + return new System.Drawing.Size(size.Width, size.Height); + } + + /// + /// Converts the to the . + /// + /// + /// + public static System.Drawing.SizeF ToSize(this SizeF size) + { + return new System.Drawing.SizeF(size.Width, size.Height); + } + + #endregion + + } +} diff --git a/Source/BitmapSourceInterop/BitmapSourceInterop.csproj b/Source/BitmapSourceInterop/BitmapSourceInterop.csproj index 983ccdb..99c7e01 100644 --- a/Source/BitmapSourceInterop/BitmapSourceInterop.csproj +++ b/Source/BitmapSourceInterop/BitmapSourceInterop.csproj @@ -1,11 +1,9 @@  net47 - - - - DotImaging bin\ + DotImaging.BitmapSourceInterop + DotImaging diff --git a/Source/Core/Core.csproj b/Source/Core/Core.csproj index bac0c69..21efdab 100644 --- a/Source/Core/Core.csproj +++ b/Source/Core/Core.csproj @@ -1,6 +1,9 @@  net47;netcoreapp2.0 + bin\ + DotImaging.Core + DotImaging diff --git a/Source/Drawing/Drawing.csproj b/Source/Drawing/Drawing.csproj index 9b86e7c..6ef9eeb 100644 --- a/Source/Drawing/Drawing.csproj +++ b/Source/Drawing/Drawing.csproj @@ -1,11 +1,9 @@  net47;netcoreapp2.0 - - - - DotImaging bin\ + DotImaging.Drawing + DotImaging true diff --git a/Source/IO.Web/IO.Web.csproj b/Source/IO.Web/IO.Web.csproj index 97ff641..4464f89 100644 --- a/Source/IO.Web/IO.Web.csproj +++ b/Source/IO.Web/IO.Web.csproj @@ -1,11 +1,9 @@  net47 - - - - DotImaging bin\ + DotImaging.IO.Web + DotImaging diff --git a/Source/IO/IO.csproj b/Source/IO/IO.csproj index 2f9fd83..922fc46 100644 --- a/Source/IO/IO.csproj +++ b/Source/IO/IO.csproj @@ -1,11 +1,9 @@  net47;netcoreapp2.0 - - - - DotImaging bin\ + DotImaging.IO + DotImaging true diff --git a/Source/Image/Image.csproj b/Source/Image/Image.csproj index af86a14..cd08029 100644 --- a/Source/Image/Image.csproj +++ b/Source/Image/Image.csproj @@ -1,6 +1,10 @@  net47;netcoreapp2.0 + bin\ + DotImaging.Image + DotImaging + true diff --git a/Source/Image/Extensions/Interop/CvMat.cs b/Source/Image/Interop/CvMat.cs similarity index 100% rename from Source/Image/Extensions/Interop/CvMat.cs rename to Source/Image/Interop/CvMat.cs diff --git a/Source/Image/Extensions/Interop/IplImage.cs b/Source/Image/Interop/IplImage.cs similarity index 100% rename from Source/Image/Extensions/Interop/IplImage.cs rename to Source/Image/Interop/IplImage.cs diff --git a/Source/Linq/Linq.csproj b/Source/Linq/Linq.csproj index a6dea93..443ac23 100644 --- a/Source/Linq/Linq.csproj +++ b/Source/Linq/Linq.csproj @@ -1,11 +1,9 @@  net47;netcoreapp2.0 - - - - DotImaging bin\ + DotImaging.Linq + DotImaging diff --git a/Source/Primitives2D/Primitives2D.csproj b/Source/Primitives2D/Primitives2D.csproj index d5bcebe..da75fe1 100644 --- a/Source/Primitives2D/Primitives2D.csproj +++ b/Source/Primitives2D/Primitives2D.csproj @@ -1,11 +1,9 @@  net47;netcoreapp2.0 - - - - DotImaging bin\ + DotImaging.Primitives2D + DotImaging diff --git a/Source/UI/Components/PictureBox.cs b/Source/UI/Components/PictureBox.cs index b44ba74..e5aeb38 100644 --- a/Source/UI/Components/PictureBox.cs +++ b/Source/UI/Components/PictureBox.cs @@ -88,12 +88,12 @@ public float ZoomFactor private Bitmap image; public Bitmap Image - { + { get { return image; } set { this.image = value; - + if (image != null) { if (this.AutoScale || this.imageBounds == default(RectangleF)) diff --git a/Source/UI/UI.cs b/Source/UI/UI.cs index e4efa7e..f7a26d6 100644 --- a/Source/UI/UI.cs +++ b/Source/UI/UI.cs @@ -202,7 +202,27 @@ public static string SelectFolder(string windowTitle = "Select folder") #region Image related widgets /// - /// Displays the specified image in a window. + /// Displays the specified image in a window and pauses the execution flow. + /// Press and hold shift + mouse (track-pad) to translate and zoom an image. + /// + /// Image to show. + /// Window title (ID). + /// True to adjust form to the image size, false otherwise. + public static void ShowDialog(this Bgr[,] image, string windowTitle = "Image", bool scaleForm = false) + { + FormCollection.CreateAndShowDialog(() => + { + var f = new ImageForm(windowTitle); + f.ScaleForm = scaleForm; + f.SetImage(image); + + return f; + }, + f => true); + } + + /// + /// Displays the specified image in a window (non blocking mode). /// Press and hold shift + mouse (track-pad) to translate and zoom an image. /// /// Image to show. diff --git a/Source/UI/UI.csproj b/Source/UI/UI.csproj index 4b990c9..472bc99 100644 --- a/Source/UI/UI.csproj +++ b/Source/UI/UI.csproj @@ -1,6 +1,9 @@  net47 + bin\ + DotImaging.UI + DotImaging @@ -12,12 +15,7 @@ - - - DotImaging - bin\ - - + bin\$(TargetFramework)\DotImaging.UI.xml true diff --git a/Test/Playground.csproj b/Test/Playground.csproj index 28a56f7..d37bfc6 100644 --- a/Test/Playground.csproj +++ b/Test/Playground.csproj @@ -13,6 +13,7 @@ + diff --git a/Test/Program.cs b/Test/Program.cs index 6d93b6e..e604512 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -27,6 +27,8 @@ using DotImaging.Linq; using DotImaging.Primitives2D; using System.Runtime.InteropServices; +using System.Drawing.Imaging; +using System.Drawing; namespace Test { @@ -34,7 +36,7 @@ class Program { static void TestDrawingFunctions() { - var resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources"); + var resourceDir = Path.Combine(Directory.GetCurrentDirectory(), "Resources"); var imgColor = ImageIO.LoadColor(Path.Combine(resourceDir, "testColorBig.jpg")).Clone(); //imgColor.DrawAnnotation(new Rectangle(10, 10, 500, 500), "Some text.", Font.Big); @@ -43,9 +45,9 @@ static void TestDrawingFunctions() //imgColor.Draw(new Circle(500, 500, 250), Bgr.Blue, -1, 128); //imgColor.Draw(new Ellipse(new PointF(500, 500), new SizeF(200, 500), 45), Bgr.Green, 10, 25); //imgColor.Draw(new Box2D(new PointF(500, 500), new SizeF(200, 500), 45), Bgr.Green, 10, 255); - imgColor.Draw(new Point[] { new Point(10, 10), new Point(500, 10), new Point(500, 600), new Point(25, 10) }, Bgr.Red, 10, 128); + //imgColor.Draw(new Point[] { new Point(10, 10), new Point(500, 10), new Point(500, 600), new Point(25, 10) }, Bgr.Red, 10, 128); - imgColor.Show(); + //imgColor.Show(); } /// @@ -57,7 +59,7 @@ unsafe static void Main() TestDrawingFunctions(); return; - //test get-rectangle async + /*//test get-rectangle async var resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources"); var imgColor = ImageIO.LoadColor(Path.Combine(resourceDir, "testColorBig.jpg")).Clone(); @@ -86,7 +88,7 @@ unsafe static void Main() var decodedIm = arr.DecodeAsColorImage(); decodedIm.Save("out.bmp"); - var rImg = imgColor.AsEnumerable().Select(x => x.G).ToArray2D(imgColor.Width(), imgColor.Height()); + var rImg = imgColor.AsEnumerable().Select(x => x.G).ToArray2D(imgColor.Width(), imgColor.Height()); */ } } diff --git a/Tools/ImageDebuggerVisualizer/ImageDebuggerVisualizer.cs b/Tools/ImageDebuggerVisualizer/ImageDebuggerVisualizer.cs new file mode 100644 index 0000000..4c5c564 --- /dev/null +++ b/Tools/ImageDebuggerVisualizer/ImageDebuggerVisualizer.cs @@ -0,0 +1,40 @@ +using DotImaging; +using Microsoft.VisualStudio.DebuggerVisualizers; +using System; +using System.Diagnostics; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Reflection; + +[assembly: DebuggerVisualizer( +typeof(ImageDebuggerVisualizer), +typeof(ImageVisualizerObjectSource), +Target = typeof(Bgr[,]))] + +namespace DotImaging +{ + public class ImageVisualizerObjectSource : VisualizerObjectSource + { + public override void GetData(object target, Stream outgoingData) + { + var image = target as Bgr[,]; + if (image == null) return; + + image.ToBitmap().Save(outgoingData, ImageFormat.Png); + outgoingData.Flush(); + } + } + + public sealed class ImageDebuggerVisualizer : DialogDebuggerVisualizer + { + protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) + { + var bmp = Image.FromStream(objectProvider.GetData()); + var image = (bmp as Bitmap).ToBgr(); + + image.ShowDialog(); + } + } + +} diff --git a/Tools/ImageDebuggerVisualizer/ImageDebuggerVisualizer.csproj b/Tools/ImageDebuggerVisualizer/ImageDebuggerVisualizer.csproj new file mode 100644 index 0000000..2d61c88 --- /dev/null +++ b/Tools/ImageDebuggerVisualizer/ImageDebuggerVisualizer.csproj @@ -0,0 +1,45 @@ + + + + net47 + bin\ + DotImaging.ImageDebuggerVisualizer + DotImaging + + + + + + + + + + + + + + + + + + C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.VisualStudio.DebuggerVisualizers.dll + C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.VisualStudio.DebuggerVisualizers.dll + C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.VisualStudio.DebuggerVisualizers.dll + + + + + + + 5.0.0 + DotImaging.ImageDebuggerVisualizer + + Darko Jurić + Darko Jurić + https://raw.githubusercontent.com/dajuric/dot-imaging/master/Deploy/Licence.txt + https://raw.githubusercontent.com/dajuric/dot-imaging/master/Deploy/Logo/logo-small.png + https://raw.githubusercontent.com/dajuric/dot-imaging/ + https://raw.githubusercontent.com/dajuric/dot-imaging/ + + + diff --git a/Tools/ImageDebuggerVisualizer/Install.txt b/Tools/ImageDebuggerVisualizer/Install.txt new file mode 100644 index 0000000..42775e0 --- /dev/null +++ b/Tools/ImageDebuggerVisualizer/Install.txt @@ -0,0 +1,4 @@ +Intended for VS2017 + +Copy the produced dlls to: + C:\Program Files (x86)\Microsoft Visual Studio\2017\\Common7\Packages\Debugger\Visualizers, where version is 'Community', 'Professional' or 'Enterprise'