Skip to content

Commit 9783804

Browse files
committed
Hash stuff
1 parent 3a04d61 commit 9783804

File tree

4 files changed

+78
-24
lines changed

4 files changed

+78
-24
lines changed

SmartImage/Engines/Other/TidderEngine.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override FullSearchResult GetResult(string url)
5151
//Debug.WriteLine(findings.Count);
5252

5353
var list = new List<ISearchResult>();
54-
54+
long distl;
5555
for (int i = 0; i < findings.Count; i++) {
5656

5757

@@ -73,6 +73,7 @@ public override FullSearchResult GetResult(string url)
7373
string? author = authorNode.InnerText;
7474
string? subreddit = subredditNode.InnerText;
7575

76+
distl = long.Parse(dist);
7677

7778
string link = titleNode.FirstChild.Attributes["href"].DeEntitizeValue;
7879

@@ -85,12 +86,13 @@ public override FullSearchResult GetResult(string url)
8586
Date = DateTime.Parse(posted)
8687
};
8788

89+
8890

8991
list.Add(bsr);
9092

9193

92-
//Debug.WriteLine(
93-
// $"{i}: {sub.Count} {dist} {score} {posted} {title} {author} {subreddit} --> {link}");
94+
Debug.WriteLine(
95+
$"tidder {i}: {sub.Count} {dist} {score} {posted} {title} {author} {subreddit} --> {link}");
9496
}
9597

9698
var best = list[0];

SmartImage/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public static class Program
3838

3939
private static void Main(string[] args)
4040
{
41-
Console.WriteLine("{0}",1);
41+
ulong hash = SearchClient.hash(@"C:\Users\Deci\Desktop\d.jpg");
42+
ulong hash1 = SearchClient.hash(@"C:\Users\Deci\Desktop\a.jpg");
43+
Console.WriteLine($"{hash1} {hash} {hash^hash1}");
44+
return;
45+
4246
/*
4347
* Setup
4448
* Check compatibility

SmartImage/Searching/FullSearchResult.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public override string ToString()
363363

364364
AppendResultInfo(sb, nameof(Similarity), $"{Similarity / 100:P}", Similarity.HasValue && !IsOriginal);
365365

366-
AppendResultInfo(sb, "Resolution",
366+
AppendResultInfo(sb, "Resolution",
367367
$"{Width}x{Height} ({AspectRatio}) ({PixelResolution:F} MP)", HasResolution);
368368

369369
AppendResultInfo(sb, nameof(Artist), Artist);
@@ -415,7 +415,7 @@ public void UpdateFrom(ISearchResult result)
415415
Source = result.Source;
416416
Characters = result.Characters;
417417
Artist = result.Artist;
418-
Site = result.Site;
418+
Site = result.Site;
419419
Description = result.Description;
420420
Date = result.Date;
421421
}
@@ -430,8 +430,11 @@ private FullSearchResult CreateExtendedResult(ISearchResult result)
430430
Artist = result.Artist,
431431
Source = result.Source,
432432
Characters = result.Characters,
433-
Site = result.Site
433+
Site = result.Site
434434
};
435+
436+
437+
435438
return extendedResult;
436439
}
437440

@@ -501,6 +504,8 @@ public static FullSearchResult GetOriginalImageResult(ImageInputInfo info)
501504

502505
name = imageFile.Name;
503506
bytes = FileSystem.GetFileSize(imageFile.FullName);
507+
508+
result.Metadata.Add("hash", SearchClient.hash(imageFile.FullName));
504509
}
505510
else {
506511
throw new SmartImageException();

SmartImage/Searching/SearchClient.cs

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Drawing;
67
using System.Linq;
78
using System.Media;
89
using System.Text;
@@ -191,29 +192,78 @@ public async void Start()
191192
/// </summary>
192193
public FullSearchResult Original { get; }
193194

195+
196+
public static ulong hash(string s, int size = 256)
197+
{
198+
//widthAndLength := uint(math.Ceil(math.Sqrt(float64(hashLength)/2.0)) + 1)
199+
var wl = (int) (Math.Ceiling(Math.Sqrt(((float) size) / 2.0)) + 1);
200+
201+
Debug.WriteLine($"{wl}");
202+
203+
Image im = Image.FromFile(s);
204+
//new Bitmap(9, 8, PixelFormat.Format16bppGrayScale);
205+
206+
Bitmap c = new Bitmap(im, new Size(wl + 1, wl));
207+
208+
209+
ulong h = 0;
210+
211+
// Loop through the images pixels to reset color.
212+
for (int i = 0; i < c.Width; i++) {
213+
for (int x = 0; x < c.Height; x++) {
214+
Color oc = c.GetPixel(i, x);
215+
int grayScale = (int) ((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11));
216+
Color nc = Color.FromArgb(oc.A, grayScale, grayScale, grayScale);
217+
c.SetPixel(i, x, nc);
218+
}
219+
}
220+
//c = MakeGrayscale3(c);
221+
222+
// int x, y;
223+
//
224+
// for (x = 0; x < c.Width; x++)
225+
// {
226+
// for (y = 0; y < c.Height; y++)
227+
// {
228+
// Color pixelColor = c.GetPixel(x, y);
229+
// Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
230+
// c.SetPixel(x, y, newColor); // Now greyscale
231+
// }
232+
// }
233+
234+
for (int j = 0; j < wl; j++) {
235+
for (int k = 0; k < wl; k++) {
236+
var b = (c.GetPixel(j, k).R > c.GetPixel(j + 1, k).R);
237+
var bit = Convert.ToUInt64(b) << (j + k * 8);
238+
h |= bit;
239+
}
240+
}
241+
242+
return h;
243+
}
244+
194245
public static string ResolveDirectLink(string s)
195246
{
196-
string d="";
197-
247+
//todo
248+
string d = "";
249+
198250
try {
199251
var uri = new Uri(s);
200252
var host = uri.Host;
201253

202-
203254

204255
var doc = new HtmlDocument();
205256
var html = Network.GetSimpleResponse(s);
206257

207-
if (host.Contains("danbooru"))
208-
{
258+
if (host.Contains("danbooru")) {
209259
Debug.WriteLine("danbooru");
210-
211260

212-
var jobj=JObject.Parse(html.Content);
261+
262+
var jobj = JObject.Parse(html.Content);
213263

214264
d = (string) jobj["file_url"];
215265

216-
266+
217267
return d;
218268
}
219269

@@ -223,26 +273,19 @@ public static string ResolveDirectLink(string s)
223273

224274
var nodes = doc.DocumentNode.SelectNodes(sel);
225275

226-
if (nodes == null)
227-
{
276+
if (nodes == null) {
228277
return null;
229278
}
279+
230280
Debug.WriteLine($"{nodes.Count}");
231281
Debug.WriteLine($"{nodes[0]}");
232282

233283

234-
235-
236-
237-
238-
239-
240284
}
241285
catch (Exception e) {
242286
Debug.WriteLine($"direct {e.Message}");
243287
return d;
244288
}
245-
246289

247290

248291
return d;

0 commit comments

Comments
 (0)