From bf8a148938e6a7d0f0600a5ec41ea78f3fad5266 Mon Sep 17 00:00:00 2001 From: Read Date: Sun, 14 Feb 2021 11:53:09 -0600 Subject: [PATCH] Hash stuff; misc --- SmartImage/Core/Info.cs | 2 +- SmartImage/Core/Interface.cs | 26 ++++--- SmartImage/Program.cs | 13 ++-- SmartImage/Searching/FullSearchResult.cs | 8 +-- SmartImage/Searching/SearchClient.cs | 53 +------------- SmartImage/Utilities/ImageUtilities.cs | 89 ++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 77 deletions(-) create mode 100644 SmartImage/Utilities/ImageUtilities.cs diff --git a/SmartImage/Core/Info.cs b/SmartImage/Core/Info.cs index 908dc5a1..ec26c9ae 100644 --- a/SmartImage/Core/Info.cs +++ b/SmartImage/Core/Info.cs @@ -109,7 +109,7 @@ internal static void ShowInfo() { Console.Clear(); - NConsole.Resize(ConsoleWindowWidth, 30); + NConsole.Resize(ResultsWindowWidth, 30); var sb = new StringBuilder(); sb.AppendColor(ColorPrimary, NAME_BANNER); diff --git a/SmartImage/Core/Interface.cs b/SmartImage/Core/Interface.cs index 688730f2..3d586193 100644 --- a/SmartImage/Core/Interface.cs +++ b/SmartImage/Core/Interface.cs @@ -87,7 +87,7 @@ private static NConsoleOption[] AllOptions /// Utility color /// internal static readonly Color ColorUtility = Color.DarkOrange; - + /// /// Version color @@ -95,16 +95,14 @@ private static NConsoleOption[] AllOptions internal static readonly Color ColorVersion = Color.LightGreen; - /// - /// Console window width (initial) - /// - internal const int ConsoleWindowWidth = 100; + internal const int ResultsWindowWidth = 120; + internal const int ResultsWindowHeight = 60; + + internal const int MainWindowWidth = 110; + + internal const int MainWindowHeight = 30; - /// - /// Console window height (initial) - /// - internal const int ConsoleWindowHeight = 35; /// /// Main option @@ -378,13 +376,13 @@ private static string GetContextMenuString(bool added) => Name = "[DEBUG] Run test", Function = () => { - - var rgOption = NConsoleOption.FromArray(TestImages, s => s); - - string? testImg = (string) NConsole.ReadOptions(rgOption).First(); + + var rgOption = NConsoleOption.FromArray(TestImages, s => s); + + string? testImg = (string) NConsole.ReadOptions(rgOption).First(); SearchConfig.Config.ImageInput = testImg; - //SearchConfig.Config.PriorityEngines = SearchEngineOptions.None; + SearchConfig.Config.PriorityEngines = SearchEngineOptions.None; return true; } diff --git a/SmartImage/Program.cs b/SmartImage/Program.cs index 736ce3c8..c2dae70c 100644 --- a/SmartImage/Program.cs +++ b/SmartImage/Program.cs @@ -5,6 +5,7 @@ using SmartImage.Searching; using System; using System.Diagnostics; +using System.Linq; using System.Text; using System.Threading; using SimpleCore.Cli; @@ -38,10 +39,7 @@ public static class Program private static void Main(string[] args) { - ulong hash = SearchClient.hash(@"C:\Users\Deci\Desktop\d.jpg"); - ulong hash1 = SearchClient.hash(@"C:\Users\Deci\Desktop\a.jpg"); - Console.WriteLine($"{hash1} {hash} {hash^hash1}"); - return; + /* * Setup @@ -54,10 +52,11 @@ private static void Main(string[] args) * Set up console */ - Console.Title = Info.NAME; - - NConsole.Resize(Interface.ConsoleWindowWidth, Interface.ConsoleWindowHeight); + Console.Title = Info.NAME; + NConsole.AutoResizeHeight = false; + NConsole.Resize(Interface.MainWindowWidth, Interface.MainWindowHeight); + Console.Clear(); diff --git a/SmartImage/Searching/FullSearchResult.cs b/SmartImage/Searching/FullSearchResult.cs index 44befe97..3200ab14 100644 --- a/SmartImage/Searching/FullSearchResult.cs +++ b/SmartImage/Searching/FullSearchResult.cs @@ -505,7 +505,7 @@ public static FullSearchResult GetOriginalImageResult(ImageInputInfo info) name = imageFile.Name; bytes = FileSystem.GetFileSize(imageFile.FullName); - result.Metadata.Add("hash", SearchClient.hash(imageFile.FullName)); + } else { throw new SmartImageException(); @@ -524,12 +524,10 @@ public static FullSearchResult GetOriginalImageResult(ImageInputInfo info) return result; } - private const int TAKE_N = 10; - - public static ISearchResult[] FilterAndSelectBestImages(List rg, int take = TAKE_N) + + public static ISearchResult[] FilterAndSelectBestImages(List rg) { var best = rg.OrderByDescending(i => i.FullResolution) - .Take(take) .Cast() .ToArray(); diff --git a/SmartImage/Searching/SearchClient.cs b/SmartImage/Searching/SearchClient.cs index 7af4fad9..67a5b707 100644 --- a/SmartImage/Searching/SearchClient.cs +++ b/SmartImage/Searching/SearchClient.cs @@ -121,6 +121,9 @@ private SearchClient(string imgInput) /// public async void Start() { + + NConsole.Resize(Core.Interface.ResultsWindowWidth, Core.Interface.ResultsWindowHeight); + int len = SearchTasks.Count; while (SearchTasks.Any()) { @@ -192,56 +195,6 @@ public async void Start() /// public FullSearchResult Original { get; } - - public static ulong hash(string s, int size = 256) - { - //widthAndLength := uint(math.Ceil(math.Sqrt(float64(hashLength)/2.0)) + 1) - var wl = (int) (Math.Ceiling(Math.Sqrt(((float) size) / 2.0)) + 1); - - Debug.WriteLine($"{wl}"); - - Image im = Image.FromFile(s); - //new Bitmap(9, 8, PixelFormat.Format16bppGrayScale); - - Bitmap c = new Bitmap(im, new Size(wl + 1, wl)); - - - ulong h = 0; - - // Loop through the images pixels to reset color. - for (int i = 0; i < c.Width; i++) { - for (int x = 0; x < c.Height; x++) { - Color oc = c.GetPixel(i, x); - int grayScale = (int) ((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11)); - Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale); - c.SetPixel(i, x, nc); - } - } - //c = MakeGrayscale3(c); - - // int x, y; - // - // for (x = 0; x < c.Width; x++) - // { - // for (y = 0; y < c.Height; y++) - // { - // Color pixelColor = c.GetPixel(x, y); - // Color newColor = Color.FromArgb(pixelColor.R, 0, 0); - // c.SetPixel(x, y, newColor); // Now greyscale - // } - // } - - for (int j = 0; j < wl; j++) { - for (int k = 0; k < wl; k++) { - var b = (c.GetPixel(j, k).R > c.GetPixel(j + 1, k).R); - var bit = Convert.ToUInt64(b) << (j + k * 8); - h |= bit; - } - } - - return h; - } - public static string ResolveDirectLink(string s) { //todo diff --git a/SmartImage/Utilities/ImageUtilities.cs b/SmartImage/Utilities/ImageUtilities.cs new file mode 100644 index 00000000..3256b43d --- /dev/null +++ b/SmartImage/Utilities/ImageUtilities.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SmartImage.Utilities +{ + public static class ImageUtilities + { + //todo + + public static List GetHash(Bitmap bmpSource, int height) + { + List lResult = new List(); + //create new image with 16x16 pixel + + Bitmap bmpMin = new Bitmap(bmpSource, new Size(height, height)); + + for (int j = 0; j < bmpMin.Height; j++) { + for (int i = 0; i < bmpMin.Width; i++) { + //reduce colors to true / false + lResult.Add(bmpMin.GetPixel(i, j).GetBrightness() < 0.5f); + } + } + + //int equalElements = hash.Zip(hash1, (i, j) => i == j).Count(eq => eq); + + + return lResult; + } + + public static List GetHash(string bmpSource, int height) + { + return GetHash((Bitmap) Image.FromFile(bmpSource), height); + } + + public static ulong Hash_d(string s, int size = 256) + { + //widthAndLength := uint(math.Ceil(math.Sqrt(float64(hashLength)/2.0)) + 1) + var wl = (int) (Math.Ceiling(Math.Sqrt(((float) size) / 2.0)) + 1); + + Debug.WriteLine($"{wl}"); + + Image im = Image.FromFile(s); + //new Bitmap(9, 8, PixelFormat.Format16bppGrayScale); + + Bitmap c = new Bitmap(im, new Size(wl + 1, wl)); + + + ulong h = 0; + + // Loop through the images pixels to reset color. + for (int i = 0; i < c.Width; i++) { + for (int x = 0; x < c.Height; x++) { + Color oc = c.GetPixel(i, x); + int grayScale = (int) ((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11)); + Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale); + c.SetPixel(i, x, nc); + } + } + //c = MakeGrayscale3(c); + + // int x, y; + // + // for (x = 0; x < c.Width; x++) + // { + // for (y = 0; y < c.Height; y++) + // { + // Color pixelColor = c.GetPixel(x, y); + // Color newColor = Color.FromArgb(pixelColor.R, 0, 0); + // c.SetPixel(x, y, newColor); // Now greyscale + // } + // } + + for (int j = 0; j < wl; j++) { + for (int k = 0; k < wl; k++) { + var b = (c.GetPixel(j, k).R > c.GetPixel(j + 1, k).R); + var bit = Convert.ToUInt64(b) << (j + k * 8); + h |= bit; + } + } + + return h; + } + } +}