public static int LoadImageHashes(string rootdir, HashCache cache, uint bits = 32 *32) { LoadImageExtensions(); string[] files = Directory.GetFiles(rootdir, "*", SearchOption.AllDirectories); int iters = 0; foreach (string file in files) { var fi = new FileInfo(file); if (!ImageExtensions.Contains(fi.Extension)) { continue; } string stoname = Utils.GetRelativePath(fi.FullName, Path.GetFullPath(rootdir)).UrlDecode(); if (cache.HasFile(stoname)) { continue; } //CompareUtils.GetHashBitmap(new Bitmap(file), 32 * 32).Save(Path.Combine(odir, fi.Name + ".png")); long[] hsh = GetHash(new Bitmap(file), bits); cache.Cache(stoname, bits, hsh); iters++; } return(iters); }
public static List <Pair <string> > CompareImages(HashCache cache, uint diffq = 10, bool debug = false) { List <Pair <string> > outp = new List <Pair <string> >(); var files = cache.Filenames; for (int i = 0; i < files.Length; i++) { for (int j = i + 1; j < files.Length; j++) { string key = files[i]; string kez = files[j]; cache.TryGetHash(key, out long[] hsha); cache.TryGetHashBits(key, out uint bita); cache.TryGetHash(kez, out long[] hshb); cache.TryGetHashBits(key, out uint bitb); if (bita != bitb) { if (debug) { Console.WriteLine("Bits in hash inequal for {0} and {1}.".SFormat(key, kez)); } continue; } long[] xord = new long[hsha.Length]; for (int k = 0; k < hsha.Length; k++) { xord[k] = hsha[k] ^ hshb[k]; } uint diffs = 0; for (int k = 0; k < xord.Length; k++) { diffs += xord[k].SetBits(); } if (debug) { Console.WriteLine("Diffs in {0} and {1}: {2}".SFormat(key, kez, diffs)); } if (diffs <= diffq) { outp.Add(new Pair <string>(key, kez)); } } } return(outp); }
private static int LoadCachedFiles(string cachefile = "cache.hash") { Cache = new HashCache(new FileInfo(cachefile)); int iters = CompareUtils.LoadImageHashes(RelDir, Cache); foreach (string file in Cache.Filenames) { var fulf = Path.GetFullPath(Path.Combine(RelDir, file)); if (!new FileInfo(fulf).Exists) { Cache.RemoveFile(file); } } Cache.WriteCache(); return(iters); }