forked from petesimard/Magic-Vision
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aaron Marasco
committed
Nov 24, 2011
1 parent
f8307f0
commit 6214bee
Showing
9 changed files
with
469 additions
and
445 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,12 +1,12 @@ | |||
using System; | using System; | ||
using System.Collections.Generic; | using System.Collections.Generic; | ||
|
|
||
namespace Data | namespace Data | ||
{ | { | ||
public interface CardStore | public interface CardStore | ||
{ | { | ||
IEnumerable<ReferenceCard> GetCards(); | IEnumerable<ReferenceCard> GetCards(); | ||
void UpdateHash(string id, ulong phash); | void UpdateHash(string id, ulong phash); | ||
} | } | ||
} | } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,113 +1,113 @@ | |||
using System; | //using System; | ||
using System.Collections.Generic; | //using System.Collections.Generic; | ||
using System.Linq; | //using System.Linq; | ||
using System.Text; | //using System.Text; | ||
using System.Data; | //using System.Data; | ||
using System.Configuration; | //using System.Configuration; | ||
using MySql.Data.MySqlClient; | ////using MySql.Data.MySqlClient; | ||
|
// | ||
namespace Data | //namespace Data | ||
{ | //{ | ||
public class MySqlClient : CardStore | // public class MySqlClient : CardStore | ||
{ | // { | ||
private MySqlConnection sql; | // private MySqlConnection sql; | ||
private static string MySqlConnectionString = ConfigurationManager.AppSettings["MySqlConnectionString"]; | // private static string MySqlConnectionString = ConfigurationManager.AppSettings["MySqlConnectionString"]; | ||
|
// | ||
private DataRow dbRow(String query) | // private DataRow dbRow(String query) | ||
{ | // { | ||
MySqlCommand command = sql.CreateCommand(); | // MySqlCommand command = sql.CreateCommand(); | ||
command.CommandText = query; | // command.CommandText = query; | ||
|
// | ||
DataTable selectDT = new DataTable(); | // DataTable selectDT = new DataTable(); | ||
MySqlDataAdapter dataAd = new MySqlDataAdapter(command); | // MySqlDataAdapter dataAd = new MySqlDataAdapter(command); | ||
|
// | ||
dataAd.Fill(selectDT); | // dataAd.Fill(selectDT); | ||
|
// | ||
if (selectDT.Rows.Count > 0) | // if (selectDT.Rows.Count > 0) | ||
return selectDT.Rows[0]; | // return selectDT.Rows[0]; | ||
else | // else | ||
return null; | // return null; | ||
} | // } | ||
|
// | ||
private int lastInsertId() | // private int lastInsertId() | ||
{ | // { | ||
DataRow r = dbRow("SELECT last_insert_id() as lid"); | // DataRow r = dbRow("SELECT last_insert_id() as lid"); | ||
|
// | ||
Int64 id = (Int64)r[0]; | // Int64 id = (Int64)r[0]; | ||
|
// | ||
return (int)id; | // return (int)id; | ||
} | // } | ||
|
// | ||
private int affectedRows() | // private int affectedRows() | ||
{ | // { | ||
DataRow r = dbRow("SELECT ROW_COUNT()"); | // DataRow r = dbRow("SELECT ROW_COUNT()"); | ||
int id = (int)r[0]; | // int id = (int)r[0]; | ||
|
// | ||
return id; | // return id; | ||
} | // } | ||
|
// | ||
private DataTable dbResult(String query) | // private DataTable dbResult(String query) | ||
{ | // { | ||
MySqlCommand command = sql.CreateCommand(); | // MySqlCommand command = sql.CreateCommand(); | ||
command.CommandText = query; | // command.CommandText = query; | ||
|
// | ||
DataTable selectDT = new DataTable(); | // DataTable selectDT = new DataTable(); | ||
MySqlDataAdapter dataAd = new MySqlDataAdapter(command); | // MySqlDataAdapter dataAd = new MySqlDataAdapter(command); | ||
|
// | ||
dataAd.Fill(selectDT); | // dataAd.Fill(selectDT); | ||
|
// | ||
return selectDT; | // return selectDT; | ||
|
// | ||
} | // } | ||
|
// | ||
internal int dbNone(string query) | // internal int dbNone(string query) | ||
{ | // { | ||
MySqlCommand command = sql.CreateCommand(); | // MySqlCommand command = sql.CreateCommand(); | ||
command.CommandText = query; | // command.CommandText = query; | ||
return command.ExecuteNonQuery(); | // return command.ExecuteNonQuery(); | ||
} | // } | ||
|
// | ||
public MySqlClient() | // public MySqlClient() | ||
{ | // { | ||
sql = new MySqlConnection(MySqlConnectionString); | // sql = new MySqlConnection(MySqlConnectionString); | ||
sql.Open(); | // sql.Open(); | ||
} | // } | ||
|
// | ||
private DateTime ConvertFromUnixTimestamp(double timestamp) | // private DateTime ConvertFromUnixTimestamp(double timestamp) | ||
{ | // { | ||
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); | // DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); | ||
return origin.AddSeconds(timestamp); | // return origin.AddSeconds(timestamp); | ||
} | // } | ||
|
// | ||
private double ConvertToUnixTimestamp(DateTime date) | // private double ConvertToUnixTimestamp(DateTime date) | ||
{ | // { | ||
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); | // DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); | ||
TimeSpan diff = date - origin; | // TimeSpan diff = date - origin; | ||
return Math.Floor(diff.TotalSeconds); | // return Math.Floor(diff.TotalSeconds); | ||
} | // } | ||
|
// | ||
public IEnumerable<ReferenceCard> GetCards() | // public IEnumerable<ReferenceCard> GetCards() | ||
{ | // { | ||
var ret = new List<ReferenceCard>(); | // var ret = new List<ReferenceCard>(); | ||
|
// | ||
var reader = this.dbResult("SELECT * FROM cards"); | // var reader = this.dbResult("SELECT * FROM cards"); | ||
foreach (DataRow r in reader.Rows) | // foreach (DataRow r in reader.Rows) | ||
{ | // { | ||
ReferenceCard card = new ReferenceCard(); | // ReferenceCard card = new ReferenceCard(); | ||
card.cardId = (String)r["id"]; | // card.cardId = (String)r["id"]; | ||
card.name = (String)r["Name"]; | // card.name = (String)r["Name"]; | ||
card.pHash = (UInt64)r["pHash"]; | // card.pHash = (UInt64)r["pHash"]; | ||
card.dataRow = r; | // card.dataRow = r; | ||
|
// | ||
ret.Add(card); | // ret.Add(card); | ||
} | // } | ||
|
// | ||
return ret; | // return ret; | ||
} | // } | ||
|
// | ||
public void UpdateHash(string id, ulong phash) | // public void UpdateHash(string id, ulong phash) | ||
{ | // { | ||
this.dbNone("UPDATE cards SET pHash=" + phash.ToString() + " WHERE id=" + id); | // this.dbNone("UPDATE cards SET pHash=" + phash.ToString() + " WHERE id=" + id); | ||
} | // } | ||
} | // } | ||
} | //} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,14 +1,14 @@ | |||
using System; | using System; | ||
using System.Data; | using System.Data; | ||
|
|
||
namespace Data | namespace Data | ||
{ | { | ||
public class ReferenceCard | public class ReferenceCard | ||
{ | { | ||
public string cardId; | public string cardId; | ||
public string name; | public string name; | ||
public UInt64 pHash; | public UInt64 pHash; | ||
public DataRow dataRow; | public DataRow dataRow; | ||
} | } | ||
} | } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,32 +1,32 @@ | |||
using System; | using System; | ||
using System.Runtime.InteropServices; | using System.Runtime.InteropServices; | ||
|
|
||
namespace Data | namespace Data | ||
{ | { | ||
public class Phash | public class Phash | ||
{ | { | ||
|
|
||
[DllImport("pHash.dll", CallingConvention = CallingConvention.Cdecl)] | [DllImport("pHash.dll", CallingConvention = CallingConvention.Cdecl)] | ||
public static extern int ph_dct_imagehash(string file_name, ref UInt64 Hash); | public static extern int ph_dct_imagehash(string file_name, ref UInt64 Hash); | ||
|
|
||
private static UInt64 m1 = 0x5555555555555555; | private static UInt64 m1 = 0x5555555555555555; | ||
private static UInt64 m2 = 0x3333333333333333; | private static UInt64 m2 = 0x3333333333333333; | ||
private static UInt64 h01 = 0x0101010101010101; | private static UInt64 h01 = 0x0101010101010101; | ||
private static UInt64 m4 = 0x0f0f0f0f0f0f0f0f; | private static UInt64 m4 = 0x0f0f0f0f0f0f0f0f; | ||
|
|
||
// Calculate the similarity between two hashes | // Calculate the similarity between two hashes | ||
public static int HammingDistance(UInt64 hash1, UInt64 hash2) | public static int HammingDistance(UInt64 hash1, UInt64 hash2) | ||
{ | { | ||
UInt64 x = hash1 ^ hash2; | UInt64 x = hash1 ^ hash2; | ||
|
|
||
|
|
||
x -= (x >> 1) & m1; | x -= (x >> 1) & m1; | ||
x = (x & m2) + ((x >> 2) & m2); | x = (x & m2) + ((x >> 2) & m2); | ||
x = (x + (x >> 4)) & m4; | x = (x + (x >> 4)) & m4; | ||
UInt64 res = (x * h01) >> 56; | UInt64 res = (x * h01) >> 56; | ||
|
|
||
return (int)res; | return (int)res; | ||
} | } | ||
} | } | ||
} | } | ||
|
Oops, something went wrong.