-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make "imageHash" async, add sync function
closes #7
- Loading branch information
Aaron Marasco
committed
Jun 28, 2013
1 parent
258f592
commit e0cd3fb
Showing
7 changed files
with
162 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ lib-cov | |
*.out | ||
*.pid | ||
*.gz | ||
|
||
/demo | ||
pids | ||
logs | ||
results | ||
|
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 was deleted.
Oops, something went wrong.
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
var pHash = require('../build/Release/pHash'); | ||
|
||
exports.getOldHash = function(filePath) { | ||
return pHash.oldHash(filePath); | ||
exports.imageHashSync = function(file) { | ||
return pHash.imageHashSync(file); | ||
}; | ||
|
||
exports.getImageHash = function(filePath) { | ||
return pHash.imagehash(filePath); | ||
exports.imageHash = function(file, cb) { | ||
return pHash.imageHash(file, cb); | ||
}; | ||
|
||
exports.hammingDistance = function(hasha,hashb) { | ||
return pHash.hammingDistance(hasha,hashb); | ||
}; | ||
|
||
/* | ||
Deprecated, use "imageHashSync" | ||
*/ | ||
exports.getImageHash = exports.imageHashSync; | ||
|
||
exports.getOldHash = function(filePath) { | ||
return pHash.oldHash(filePath); | ||
}; | ||
|
||
|
||
|
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,82 @@ | ||
var should = require('should'); | ||
|
||
var assert = require("assert"); | ||
var pHash = require('../index'); | ||
require("should"); | ||
|
||
var examples = [ | ||
{ path: "./examples/a.jpg", hash: "2816561582497829945" }, | ||
{ path: "./examples/b.png", hash: "2816561582497829945" }, | ||
{ path: "./examples/c.png", hash: "15433968795834791622" }, | ||
{ path: "./examples/d.jpg", hash: "12040987020241900649" } | ||
]; | ||
|
||
describe('pHash', function() { | ||
var oldHashA = pHash.getOldHash('./examples/a.jpg'); | ||
|
||
var hashA = pHash.getImageHash("./examples/a.jpg"); | ||
var hashB = pHash.getImageHash("./examples/b.png"); | ||
var hashC = pHash.getImageHash("./examples/c.png"); | ||
var hashD = pHash.getImageHash("./examples/d.jpg"); | ||
describe("pHash", function() { | ||
describe("sync test", function() { | ||
it("should match", function() { | ||
examples.forEach(function(i) { | ||
var hash = pHash.imageHashSync(i.path); | ||
assert.equal(i.hash, hash); | ||
}) | ||
|
||
//not really an image attempting to create an exception | ||
var hashF = pHash.getImageHash("./examples/f.png"); | ||
it("should fail", function() { | ||
var hashF = pHash.imageHashSync("./examples/f.png"); | ||
hashF.should.equal('0'); | ||
}); | ||
}) | ||
}); | ||
|
||
// nonRBG image | ||
var hashG = pHash.getImageHash("./examples/g.jpg"); | ||
describe("async test", function() { | ||
var test = examples[0]; | ||
examples.forEach(function(i) { | ||
it("should match", function(done) { | ||
pHash.imageHash(i.path, function(err, hash) { | ||
if (err) { | ||
done(err); | ||
} | ||
|
||
// empty image | ||
var hashH = pHash.getImageHash("./examples/h.jpg"); | ||
assert.equal(i.hash, hash); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getImageHash()', function() { | ||
it('should be done', function() { | ||
oldHashA.should.equal(2816561582497830000); | ||
|
||
hashA.should.equal('2816561582497829945'); | ||
hashB.should.equal('2816561582497829945'); | ||
hashC.should.equal('15433968795834791622'); | ||
hashD.should.equal('12040987020241900649'); | ||
it("should fail", function(done) { | ||
pHash.imageHash("./examples/f.png", function(err, hash) { | ||
assert(err); | ||
done(); | ||
}); | ||
}); | ||
|
||
hashF.should.equal('0'); | ||
hashG.should.equal('18182116099082822440'); | ||
hashF.should.equal('0'); | ||
it("should throw", function() { | ||
assert.throws(function() { | ||
pHash.imageHash("not enough arguments"); | ||
}); | ||
}) | ||
}); | ||
}); | ||
|
||
describe('hammingDistance()', function() { | ||
it('should be done', function() { | ||
var hammingAB = pHash.hammingDistance(hashA,hashB); | ||
var hammingAC = pHash.hammingDistance(hashA,hashC); | ||
var hammingBC = pHash.hammingDistance(hashB,hashC); | ||
var hammingAD = pHash.hammingDistance(hashA,hashD); | ||
|
||
hammingAB.should.equal(0); | ||
hammingAC.should.equal(38); | ||
hammingBC.should.equal(38); | ||
hammingAD.should.equal(12); | ||
|
||
describe("legacy test", function() { | ||
it("should match", function() { | ||
examples.forEach(function(i) { | ||
var hash = pHash.getImageHash(i.path); | ||
assert.equal(i.hash, hash); | ||
}); | ||
}); | ||
|
||
it("should fail", function() { | ||
var hashF = pHash.getImageHash("./examples/f.png"); | ||
hashF.should.equal('0'); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
describe('hammingDistance()', function() { | ||
it('should be done', function() { | ||
var hammingAB = pHash.hammingDistance(examples[0].hash,examples[1].hash); | ||
var hammingAC = pHash.hammingDistance(examples[0].hash,examples[2].hash); | ||
var hammingBC = pHash.hammingDistance(examples[1].hash,examples[2].hash); | ||
var hammingAD = pHash.hammingDistance(examples[1].hash,examples[3].hash); | ||
hammingAB.should.equal(0); | ||
hammingAC.should.equal(38); | ||
hammingBC.should.equal(38); | ||
hammingAD.should.equal(12); | ||
}); | ||
}); | ||
}); |