Skip to content

Commit

Permalink
Hash stuff; misc
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Feb 14, 2021
1 parent 9783804 commit bf8a148
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 77 deletions.
2 changes: 1 addition & 1 deletion SmartImage/Core/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 12 additions & 14 deletions SmartImage/Core/Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,22 @@ private static NConsoleOption[] AllOptions
/// Utility color
/// </summary>
internal static readonly Color ColorUtility = Color.DarkOrange;


/// <summary>
/// Version color
/// </summary>
internal static readonly Color ColorVersion = Color.LightGreen;


/// <summary>
/// Console window width (initial)
/// </summary>
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;

/// <summary>
/// Console window height (initial)
/// </summary>
internal const int ConsoleWindowHeight = 35;

/// <summary>
/// Main option
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 6 additions & 7 deletions SmartImage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SmartImage.Searching;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using SimpleCore.Cli;
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand Down
8 changes: 3 additions & 5 deletions SmartImage/Searching/FullSearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -524,12 +524,10 @@ public static FullSearchResult GetOriginalImageResult(ImageInputInfo info)
return result;
}

private const int TAKE_N = 10;

public static ISearchResult[] FilterAndSelectBestImages(List<BasicSearchResult> rg, int take = TAKE_N)

public static ISearchResult[] FilterAndSelectBestImages(List<BasicSearchResult> rg)
{
var best = rg.OrderByDescending(i => i.FullResolution)
.Take(take)
.Cast<ISearchResult>()
.ToArray();

Expand Down
53 changes: 3 additions & 50 deletions SmartImage/Searching/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ private SearchClient(string imgInput)
/// </summary>
public async void Start()
{

NConsole.Resize(Core.Interface.ResultsWindowWidth, Core.Interface.ResultsWindowHeight);

int len = SearchTasks.Count;

while (SearchTasks.Any()) {
Expand Down Expand Up @@ -192,56 +195,6 @@ public async void Start()
/// </summary>
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
Expand Down
89 changes: 89 additions & 0 deletions SmartImage/Utilities/ImageUtilities.cs
Original file line number Diff line number Diff line change
@@ -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<bool> GetHash(Bitmap bmpSource, int height)
{
List<bool> lResult = new List<bool>();
//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<bool> 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;
}
}
}

0 comments on commit bf8a148

Please sign in to comment.